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