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 | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 9 | is_php() |
| 10 | ======== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 11 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 12 | .. php:function:: is_php($version = '5.3.0') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 13 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 14 | :param string $version: Version number |
| 15 | :returns: bool |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 16 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 17 | Determines of the PHP version being used is greater than the |
| 18 | supplied version number. |
| 19 | |
| 20 | Example:: |
| 21 | |
| 22 | if (is_php('5.3')) |
Derek Jones | 46715e5 | 2011-10-05 17:36:22 -0500 | [diff] [blame] | 23 | { |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 24 | $str = quoted_printable_encode($str); |
Derek Jones | 46715e5 | 2011-10-05 17:36:22 -0500 | [diff] [blame] | 25 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | |
| 27 | Returns boolean TRUE if the installed version of PHP is equal to or |
| 28 | greater than the supplied version number. Returns FALSE if the installed |
| 29 | version of PHP is lower than the supplied version number. |
| 30 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 31 | is_really_writable() |
| 32 | ==================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 33 | |
vlakoff | 9a6032d | 2013-03-05 23:03:12 +0100 | [diff] [blame^] | 34 | .. php:function:: is_really_writable($file) |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 35 | |
| 36 | :param string $file: File path |
| 37 | :returns: bool |
| 38 | |
| 39 | ``is_writable()`` returns TRUE on Windows servers when you really can't |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | write to the file as the OS reports to PHP as FALSE only if the |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 41 | read-only attribute is marked. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 42 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 43 | This function determines if a file is actually writable by attempting |
| 44 | to write to it first. Generally only recommended on platforms where |
| 45 | this information may be unreliable. |
| 46 | |
| 47 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | |
Derek Jones | 46715e5 | 2011-10-05 17:36:22 -0500 | [diff] [blame] | 49 | if (is_really_writable('file.txt')) |
| 50 | { |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 51 | echo "I could write to this if I wanted to"; |
Derek Jones | 46715e5 | 2011-10-05 17:36:22 -0500 | [diff] [blame] | 52 | } |
| 53 | else |
| 54 | { |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 55 | echo "File is not writable"; |
Derek Jones | 46715e5 | 2011-10-05 17:36:22 -0500 | [diff] [blame] | 56 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 57 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 58 | config_item() |
| 59 | ============= |
| 60 | |
| 61 | .. php:function:: config_item($key) |
| 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 | 7b18a3f | 2012-11-04 20:27:35 +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. |
| 70 | |
| 71 | .. important:: This function only returns values set in your configuration |
| 72 | files. It does not take into account config values that are |
| 73 | dynamically set at runtime. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 75 | show_error() |
| 76 | ============ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 78 | .. php:function:: show_error($message, $status_code, $heading = 'An Error Was Encountered') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 79 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 80 | :param mixed $message: Error message |
| 81 | :param int $status_code: HTTP Response status code |
| 82 | :param string $heading: Error page heading |
| 83 | :returns: void |
| 84 | |
| 85 | This function calls ``CI_Exception::show_error()``. For more info, |
| 86 | please see the :doc:`Error Handling <errors>` documentation. |
| 87 | |
| 88 | show_404() |
| 89 | ========== |
| 90 | |
| 91 | .. php:function:: show_404($page = '', $log_error = TRUE) |
| 92 | |
| 93 | :param string $page: URI string |
| 94 | :param bool $log_error: Whether to log the error |
| 95 | :returns: void |
| 96 | |
| 97 | This function calls ``CI_Exception::show_404()``. For more info, |
| 98 | please see the :doc:`Error Handling <errors>` documentation. |
| 99 | |
| 100 | log_message() |
| 101 | ============= |
| 102 | |
| 103 | .. php:function:: log_message($level = 'error', $message, $php_error = FALSE) |
| 104 | |
| 105 | :param string $level: Log level |
| 106 | :param string $message: Message to log |
| 107 | :param bool $php_error: Whether we're loggin a native PHP error message |
| 108 | :returns: void |
| 109 | |
| 110 | This function is an alias for ``CI_Log::write_log()``. For more info, |
| 111 | please see the :doc:`Error Handling <errors>` documentation. |
| 112 | |
| 113 | set_status_header() |
Andrey Andreev | 3fb0267 | 2012-10-22 16:48:01 +0300 | [diff] [blame] | 114 | =============================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 115 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 116 | .. php:function:: set_status_header($code, $text = '') |
| 117 | |
| 118 | :param int $code: HTTP Reponse status code |
| 119 | :param string $text: A custom message to set with the status code |
| 120 | :returns: void |
| 121 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 122 | Permits you to manually set a server status header. Example:: |
| 123 | |
Derek Jones | 46715e5 | 2011-10-05 17:36:22 -0500 | [diff] [blame] | 124 | set_status_header(401); |
| 125 | // Sets the header as: Unauthorized |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | |
| 127 | `See here <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>`_ for |
| 128 | a full list of headers. |
| 129 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 130 | remove_invisible_characters() |
| 131 | ============================= |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 132 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 133 | .. php:function:: remove_invisible_characters($str, $url_encoded = TRUE) |
| 134 | |
| 135 | :param string $str: Input string |
| 136 | :param bool $url_encoded: Whether to remove URL-encoded characters as well |
| 137 | :returns: string |
| 138 | |
| 139 | This function prevents inserting NULL characters between ASCII |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 140 | characters, like Java\\0script. |
| 141 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 142 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 144 | remove_invisible_characters('Java\\0script'); |
| 145 | // Returns: 'Javascript' |
| 146 | |
| 147 | html_escape() |
| 148 | ============= |
| 149 | |
| 150 | .. php:function:: html_escape($var) |
| 151 | |
| 152 | :param mixed $var: Variable to escape |
| 153 | (string or array) |
| 154 | :returns: mixed |
| 155 | |
| 156 | This function acts as an alias for PHP's native ``htmlspecialchars()`` |
| 157 | function, with the advantage of being able to accept an array of strings. |
| 158 | |
| 159 | It is useful in preventing Cross Site Scripting (XSS). |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 160 | |
| 161 | get_mimes() |
Andrey Andreev | 3fb0267 | 2012-10-22 16:48:01 +0300 | [diff] [blame] | 162 | =========== |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 163 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 164 | .. php:function:: get_mimes() |
| 165 | |
| 166 | :returns: array |
| 167 | |
| 168 | This function returns a *reference* to the MIMEs array from |
| 169 | *application/config/mimes.php*. |
Andrey Andreev | 3fb0267 | 2012-10-22 16:48:01 +0300 | [diff] [blame] | 170 | |
| 171 | is_https() |
| 172 | ========== |
| 173 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 174 | .. php:function:: is_https() |
| 175 | |
| 176 | :returns: bool |
| 177 | |
Andrey Andreev | 3fb0267 | 2012-10-22 16:48:01 +0300 | [diff] [blame] | 178 | Returns TRUE if a secure (HTTPS) connection is used and FALSE |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 179 | in any other case (including non-HTTP requests). |
| 180 | |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 181 | function_usable() |
| 182 | ================= |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 183 | |
Andrey Andreev | 1bc3026 | 2012-11-09 11:30:51 +0200 | [diff] [blame] | 184 | .. php:function:: function_usable($function_name) |
| 185 | |
| 186 | :param string $function_name: Function name |
| 187 | :returns: bool |
| 188 | |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 189 | Returns TRUE if a function exists and is usable, FALSE otherwise. |
| 190 | |
| 191 | This function runs a ``function_exists()`` check and if the |
| 192 | `Suhosin extension <http://www.hardened-php.net/suhosin/>` is loaded, |
| 193 | checks if it doesn't disable the function being checked. |
| 194 | |
| 195 | It is useful if you want to check for the availability of functions |
| 196 | such as ``eval()`` and ``exec()``, which are dangerous and might be |
| 197 | disabled on servers with highly restrictive security policies. |