Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################ |
| 2 | Common Functions |
| 3 | ################ |
| 4 | |
| 5 | CodeIgniter uses a few functions for its operation that are globally |
| 6 | defined, and are available to you at any point. These do not require |
| 7 | loading any libraries or helpers. |
| 8 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 9 | .. contents:: |
| 10 | :local: |
| 11 | |
| 12 | .. raw:: html |
| 13 | |
| 14 | <div class="custom-index container"></div> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 15 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 16 | .. function:: is_php([$version = '5.3.0']) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 17 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 18 | :param string $version: Version number |
| 19 | :returns: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 20 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 21 | Determines of the PHP version being used is greater than the |
| 22 | supplied version number. |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 23 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 24 | Example:: |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 25 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 26 | if (is_php('5.3')) |
| 27 | { |
| 28 | $str = quoted_printable_encode($str); |
| 29 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 31 | Returns boolean TRUE if the installed version of PHP is equal to or |
| 32 | greater than the supplied version number. Returns FALSE if the installed |
| 33 | version of PHP is lower than the supplied version number. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 34 | |
Andrey Andreev | af9aa2b | 2014-01-25 00:28:10 +0200 | [diff] [blame] | 35 | .. function:: is_really_writable($file) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 36 | |
Andrey Andreev | af9aa2b | 2014-01-25 00:28:10 +0200 | [diff] [blame] | 37 | :param string $file: File path |
| 38 | :returns: bool |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 39 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 40 | ``is_writable()`` returns TRUE on Windows servers when you really can't |
| 41 | write to the file as the OS reports to PHP as FALSE only if the |
| 42 | read-only attribute is marked. |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 43 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 44 | This function determines if a file is actually writable by attempting |
| 45 | to write to it first. Generally only recommended on platforms where |
| 46 | this information may be unreliable. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 47 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 48 | Example:: |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 49 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 50 | if (is_really_writable('file.txt')) |
| 51 | { |
| 52 | echo "I could write to this if I wanted to"; |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | echo "File is not writable"; |
| 57 | } |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 58 | |
Andrey Andreev | af9aa2b | 2014-01-25 00:28:10 +0200 | [diff] [blame] | 59 | .. note:: See also `PHP bug #54709 <https://bugs.php.net/bug.php?id=54709>`_ for more info. |
| 60 | |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 61 | .. function:: config_item($key) |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 62 | |
| 63 | :param string $key: Config item key |
| 64 | :returns: mixed |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 65 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 66 | The :doc:`Config Library <../libraries/config>` is the preferred way of |
| 67 | accessing configuration information, however ``config_item()`` can be used |
| 68 | to retrieve single keys. See :doc:`Config Library <../libraries/config>` |
| 69 | documentation for more information. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 70 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 71 | .. :noindex: function:: show_error($message, $status_code[, $heading = 'An Error Was Encountered']) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 73 | :param mixed $message: Error message |
| 74 | :param int $status_code: HTTP Response status code |
| 75 | :param string $heading: Error page heading |
| 76 | :returns: void |
| 77 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 78 | This function calls ``CI_Exception::show_error()``. For more info, |
| 79 | please see the :doc:`Error Handling <errors>` documentation. |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 80 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 81 | .. :noindex: function:: show_404([$page = ''[, $log_error = TRUE]]) |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 82 | |
| 83 | :param string $page: URI string |
| 84 | :param bool $log_error: Whether to log the error |
| 85 | :returns: void |
| 86 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 87 | This function calls ``CI_Exception::show_404()``. For more info, |
| 88 | please see the :doc:`Error Handling <errors>` documentation. |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 89 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 90 | .. :noindex: function:: log_message($level, $message) |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 91 | |
vlakoff | d0c30ab | 2013-05-07 07:49:23 +0200 | [diff] [blame] | 92 | :param string $level: Log level: 'error', 'debug' or 'info' |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 93 | :param string $message: Message to log |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 94 | :returns: void |
| 95 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 96 | This function is an alias for ``CI_Log::write_log()``. For more info, |
| 97 | please see the :doc:`Error Handling <errors>` documentation. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 98 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 99 | .. function:: set_status_header($code[, $text = '']) |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 100 | |
| 101 | :param int $code: HTTP Reponse status code |
| 102 | :param string $text: A custom message to set with the status code |
| 103 | :returns: void |
| 104 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 105 | Permits you to manually set a server status header. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 106 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 107 | set_status_header(401); |
| 108 | // Sets the header as: Unauthorized |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 109 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 110 | `See here <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>`_ for |
| 111 | a full list of headers. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 113 | .. function:: remove_invisible_characters($str[, $url_encoded = TRUE]) |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 114 | |
| 115 | :param string $str: Input string |
| 116 | :param bool $url_encoded: Whether to remove URL-encoded characters as well |
| 117 | :returns: string |
| 118 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 119 | This function prevents inserting NULL characters between ASCII |
| 120 | characters, like Java\\0script. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 122 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 123 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 124 | remove_invisible_characters('Java\\0script'); |
| 125 | // Returns: 'Javascript' |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 126 | |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 127 | .. function:: html_escape($var) |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 128 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 129 | :param mixed $var: Variable to escape (string or array) |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 130 | :returns: mixed |
| 131 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 132 | This function acts as an alias for PHP's native ``htmlspecialchars()`` |
| 133 | function, with the advantage of being able to accept an array of strings. |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 134 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 135 | It is useful in preventing Cross Site Scripting (XSS). |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 136 | |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 137 | .. function:: get_mimes() |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 138 | |
| 139 | :returns: array |
| 140 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 141 | This function returns a *reference* to the MIMEs array from |
| 142 | *application/config/mimes.php*. |
Andrey Andreev | 3fb0267 | 2012-10-22 16:48:01 +0300 | [diff] [blame] | 143 | |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 144 | .. function:: is_https() |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 145 | |
| 146 | :returns: bool |
| 147 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 148 | Returns TRUE if a secure (HTTPS) connection is used and FALSE |
| 149 | in any other case (including non-HTTP requests). |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 150 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 151 | .. function:: is_cli() |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 152 | |
| 153 | :returns: bool |
| 154 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 155 | Returns TRUE if the application is run through the command line |
| 156 | and FALSE if not. |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 157 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 158 | .. note:: This function checks both if the ``PHP_SAPI`` value is 'cli' |
| 159 | or if the ``STDIN`` constant is defined. |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 160 | |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 161 | .. function:: function_usable($function_name) |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 162 | |
| 163 | :param string $function_name: Function name |
| 164 | :returns: bool |
| 165 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 166 | Returns TRUE if a function exists and is usable, FALSE otherwise. |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 167 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 168 | This function runs a ``function_exists()`` check and if the |
| 169 | `Suhosin extension <http://www.hardened-php.net/suhosin/>` is loaded, |
| 170 | checks if it doesn't disable the function being checked. |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 171 | |
Andrey Andreev | 9f387e7 | 2014-01-07 11:50:34 +0200 | [diff] [blame] | 172 | It is useful if you want to check for the availability of functions |
| 173 | such as ``eval()`` and ``exec()``, which are dangerous and might be |
Andrey Andreev | ff6d1a8 | 2014-02-07 15:35:51 +0200 | [diff] [blame^] | 174 | disabled on servers with highly restrictive security policies. |
Andrey Andreev | aaa8ddb | 2014-02-03 14:10:44 +0200 | [diff] [blame] | 175 | |
Andrey Andreev | ff6d1a8 | 2014-02-07 15:35:51 +0200 | [diff] [blame^] | 176 | .. note:: This function was introduced because Suhosin terminated |
| 177 | script execution, but this turned out to be a bug. A fix |
| 178 | has been available for some time (version 0.9.34), but is |
| 179 | unfortunately not released yet. |