Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 1 | ####################### |
| 2 | Compatibility Functions |
| 3 | ####################### |
| 4 | |
| 5 | CodeIgniter provides a set of compatibility functions that enable |
| 6 | you to use functions what are otherwise natively available in PHP, |
| 7 | but only in higher versions or depending on a certain extension. |
| 8 | |
| 9 | Being custom implementations, these functions will also have some |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 10 | set of dependencies on their own, but are still useful if your |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 11 | PHP setup doesn't offer them natively. |
| 12 | |
Andrey Andreev | b6359a6 | 2016-10-24 09:41:29 +0300 | [diff] [blame] | 13 | .. note:: Much like the :doc:`common functions <common_functions>`, the |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 14 | compatibility functions are always available, as long as |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 15 | their dependencies are met. |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 16 | |
| 17 | .. contents:: |
| 18 | :local: |
| 19 | |
| 20 | .. raw:: html |
| 21 | |
| 22 | <div class="custom-index container"></div> |
| 23 | |
| 24 | **************** |
| 25 | Password Hashing |
| 26 | **************** |
| 27 | |
| 28 | This set of compatibility functions offers a "backport" of PHP's |
| 29 | standard `Password Hashing extension <http://php.net/password>`_ |
| 30 | that is otherwise available only since PHP 5.5. |
| 31 | |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 32 | Dependencies |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 33 | ============ |
| 34 | |
| 35 | - PHP 5.3.7 |
| 36 | - ``CRYPT_BLOWFISH`` support for ``crypt()`` |
| 37 | |
| 38 | Constants |
| 39 | ========= |
| 40 | |
| 41 | - ``PASSWORD_BCRYPT`` |
| 42 | - ``PASSWORD_DEFAULT`` |
| 43 | |
| 44 | Function reference |
| 45 | ================== |
| 46 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 47 | .. php:function:: password_get_info($hash) |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 48 | |
| 49 | :param string $hash: Password hash |
| 50 | :returns: Information about the hashed password |
| 51 | :rtype: array |
| 52 | |
| 53 | For more information, please refer to the `PHP manual for |
| 54 | password_get_info() <http://php.net/password_get_info>`_. |
| 55 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 56 | .. php:function:: password_hash($password, $algo[, $options = array()]) |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 57 | |
| 58 | :param string $password: Plain-text password |
| 59 | :param int $algo: Hashing algorithm |
| 60 | :param array $options: Hashing options |
| 61 | :returns: Hashed password or FALSE on failure |
| 62 | :rtype: string |
| 63 | |
| 64 | For more information, please refer to the `PHP manual for |
| 65 | password_hash() <http://php.net/password_hash>`_. |
| 66 | |
| 67 | .. note:: Unless you provide your own (and valid) salt, this function |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 68 | has a further dependency on an available CSPRNG source. Each |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 69 | of the following would satisfy that: |
| 70 | - ``mcrypt_create_iv()`` with ``MCRYPT_DEV_URANDOM`` |
| 71 | - ``openssl_random_pseudo_bytes()`` |
| 72 | - /dev/arandom |
| 73 | - /dev/urandom |
| 74 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 75 | .. php:function:: password_needs_rehash() |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 76 | |
| 77 | :param string $hash: Password hash |
| 78 | :param int $algo: Hashing algorithm |
| 79 | :param array $options: Hashing options |
| 80 | :returns: TRUE if the hash should be rehashed to match the given algorithm and options, FALSE otherwise |
| 81 | :rtype: bool |
| 82 | |
| 83 | For more information, please refer to the `PHP manual for |
| 84 | password_needs_rehash() <http://php.net/password_needs_rehash>`_. |
| 85 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 86 | .. php:function:: password_verify($password, $hash) |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 87 | |
| 88 | :param string $password: Plain-text password |
| 89 | :param string $hash: Password hash |
| 90 | :returns: TRUE if the password matches the hash, FALSE if not |
| 91 | :rtype: bool |
| 92 | |
| 93 | For more information, please refer to the `PHP manual for |
| 94 | password_verify() <http://php.net/password_verify>`_. |
| 95 | |
Andrey Andreev | 9a152a9 | 2014-02-18 16:29:53 +0200 | [diff] [blame] | 96 | ********************* |
| 97 | Hash (Message Digest) |
| 98 | ********************* |
| 99 | |
Andrey Andreev | a5621b8 | 2014-05-09 11:23:08 +0300 | [diff] [blame] | 100 | This compatibility layer contains backports for the ``hash_equals()`` |
| 101 | and ``hash_pbkdf2()`` functions, which otherwise require PHP 5.6 and/or |
| 102 | PHP 5.5 respectively. |
Andrey Andreev | 9a152a9 | 2014-02-18 16:29:53 +0200 | [diff] [blame] | 103 | |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 104 | Dependencies |
Andrey Andreev | 9a152a9 | 2014-02-18 16:29:53 +0200 | [diff] [blame] | 105 | ============ |
| 106 | |
| 107 | - None |
| 108 | |
| 109 | Function reference |
| 110 | ================== |
| 111 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 112 | .. php:function:: hash_equals($known_string, $user_string) |
Andrey Andreev | a5621b8 | 2014-05-09 11:23:08 +0300 | [diff] [blame] | 113 | |
| 114 | :param string $known_string: Known string |
| 115 | :param string $user_string: User-supplied string |
| 116 | :returns: TRUE if the strings match, FALSE otherwise |
| 117 | :rtype: string |
| 118 | |
| 119 | For more information, please refer to the `PHP manual for |
| 120 | hash_equals() <http://php.net/hash_equals>`_. |
| 121 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 122 | .. php:function:: hash_pbkdf2($algo, $password, $salt, $iterations[, $length = 0[, $raw_output = FALSE]]) |
Andrey Andreev | 9a152a9 | 2014-02-18 16:29:53 +0200 | [diff] [blame] | 123 | |
| 124 | :param string $algo: Hashing algorithm |
| 125 | :param string $password: Password |
| 126 | :param string $salt: Hash salt |
| 127 | :param int $iterations: Number of iterations to perform during derivation |
| 128 | :param int $length: Output string length |
| 129 | :param bool $raw_output: Whether to return raw binary data |
| 130 | :returns: Password-derived key or FALSE on failure |
| 131 | :rtype: string |
| 132 | |
| 133 | For more information, please refer to the `PHP manual for |
| 134 | hash_pbkdf2() <http://php.net/hash_pbkdf2>`_. |
| 135 | |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 136 | **************** |
| 137 | Multibyte String |
| 138 | **************** |
| 139 | |
| 140 | This set of compatibility functions offers limited support for PHP's |
| 141 | `Multibyte String extension <http://php.net/mbstring>`_. Because of |
| 142 | the limited alternative solutions, only a few functions are available. |
| 143 | |
| 144 | .. note:: When a character set parameter is ommited, |
| 145 | ``$config['charset']`` will be used. |
| 146 | |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 147 | Dependencies |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 148 | ============ |
| 149 | |
| 150 | - `iconv <http://php.net/iconv>`_ extension |
| 151 | |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 152 | .. important:: This dependency is optional and these functions will |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 153 | always be declared. If iconv is not available, they WILL |
| 154 | fall-back to their non-mbstring versions. |
| 155 | |
| 156 | .. important:: Where a character set is supplied, it must be |
| 157 | supported by iconv and in a format that it recognizes. |
| 158 | |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 159 | .. note:: For you own dependency check on the actual mbstring |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 160 | extension, use the ``MB_ENABLED`` constant. |
| 161 | |
| 162 | Function reference |
| 163 | ================== |
| 164 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 165 | .. php:function:: mb_strlen($str[, $encoding = NULL]) |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 166 | |
| 167 | :param string $str: Input string |
| 168 | :param string $encoding: Character set |
| 169 | :returns: Number of characters in the input string or FALSE on failure |
| 170 | :rtype: string |
| 171 | |
| 172 | For more information, please refer to the `PHP manual for |
| 173 | mb_strlen() <http://php.net/mb_strlen>`_. |
| 174 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 175 | .. php:function:: mb_strpos($haystack, $needle[, $offset = 0[, $encoding = NULL]]) |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 176 | |
| 177 | :param string $haystack: String to search in |
| 178 | :param string $needle: Part of string to search for |
| 179 | :param int $offset: Search offset |
| 180 | :param string $encoding: Character set |
| 181 | :returns: Numeric character position of where $needle was found or FALSE if not found |
| 182 | :rtype: mixed |
| 183 | |
| 184 | For more information, please refer to the `PHP manual for |
| 185 | mb_strpos() <http://php.net/mb_strpos>`_. |
| 186 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 187 | .. php:function:: mb_substr($str, $start[, $length = NULL[, $encoding = NULL]]) |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 188 | |
| 189 | :param string $str: Input string |
| 190 | :param int $start: Position of first character |
| 191 | :param int $length: Maximum number of characters |
| 192 | :param string $encoding: Character set |
| 193 | :returns: Portion of $str specified by $start and $length or FALSE on failure |
| 194 | :rtype: string |
| 195 | |
| 196 | For more information, please refer to the `PHP manual for |
Andrey Andreev | 0254589 | 2014-02-19 23:49:31 +0200 | [diff] [blame] | 197 | mb_substr() <http://php.net/mb_substr>`_. |
| 198 | |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 199 | ****************** |
| 200 | Standard Functions |
| 201 | ****************** |
Andrey Andreev | 0254589 | 2014-02-19 23:49:31 +0200 | [diff] [blame] | 202 | |
| 203 | This set of compatibility functions offers support for a few |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 204 | standard functions in PHP that otherwise require a newer PHP version. |
Andrey Andreev | 0254589 | 2014-02-19 23:49:31 +0200 | [diff] [blame] | 205 | |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 206 | Dependencies |
Andrey Andreev | 0254589 | 2014-02-19 23:49:31 +0200 | [diff] [blame] | 207 | ============ |
| 208 | |
| 209 | - None |
| 210 | |
| 211 | Function reference |
| 212 | ================== |
| 213 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 214 | .. php:function:: array_column(array $array, $column_key[, $index_key = NULL]) |
Andrey Andreev | 0254589 | 2014-02-19 23:49:31 +0200 | [diff] [blame] | 215 | |
| 216 | :param array $array: Array to fetch results from |
| 217 | :param mixed $column_key: Key of the column to return values from |
| 218 | :param mixed $index_key: Key to use for the returned values |
| 219 | :returns: An array of values representing a single column from the input array |
| 220 | :rtype: array |
| 221 | |
| 222 | For more information, please refer to the `PHP manual for |
| 223 | array_column() <http://php.net/array_column>`_. |
| 224 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 225 | .. php:function:: hex2bin($data) |
Andrey Andreev | 5b3fe7c | 2014-07-07 10:55:53 +0300 | [diff] [blame] | 226 | |
| 227 | :param array $data: Hexadecimal representation of data |
| 228 | :returns: Binary representation of the given data |
| 229 | :rtype: string |
| 230 | |
| 231 | For more information, please refer to the `PHP manual for hex2bin() |
Andrey Andreev | 6500bc7 | 2014-07-07 14:11:26 +0300 | [diff] [blame] | 232 | <http://php.net/hex2bin>`_. |