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 |
| 10 | set of dependancies on their own, but are still useful if your |
| 11 | PHP setup doesn't offer them natively. |
| 12 | |
| 13 | .. note:: Much like the `common functions <common_functions>`, the |
| 14 | compatibility functions are always available, as long as |
| 15 | their dependancies are met. |
| 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 | |
| 32 | Dependancies |
| 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 | |
| 47 | .. function:: password_get_info($hash) |
| 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 | |
| 56 | .. function:: password_hash($password, $algo[, $options = array()]) |
| 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 |
| 68 | has a further dependancy on an available CSPRNG source. Each |
| 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 | |
| 75 | .. function:: password_needs_rehash() |
| 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 | |
| 86 | .. function:: password_verify($password, $hash) |
| 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 | |
| 100 | This compatibility layer contains only a single function at |
| 101 | this time - ``hash_pbkdf2()``, which otherwise requires PHP 5.5. |
| 102 | |
| 103 | Dependancies |
| 104 | ============ |
| 105 | |
| 106 | - None |
| 107 | |
| 108 | Function reference |
| 109 | ================== |
| 110 | |
| 111 | .. function:: hash_pbkdf2($algo, $password, $salt, $iterations[, $length = 0[, $raw_output = FALSE]]) |
| 112 | |
| 113 | :param string $algo: Hashing algorithm |
| 114 | :param string $password: Password |
| 115 | :param string $salt: Hash salt |
| 116 | :param int $iterations: Number of iterations to perform during derivation |
| 117 | :param int $length: Output string length |
| 118 | :param bool $raw_output: Whether to return raw binary data |
| 119 | :returns: Password-derived key or FALSE on failure |
| 120 | :rtype: string |
| 121 | |
| 122 | For more information, please refer to the `PHP manual for |
| 123 | hash_pbkdf2() <http://php.net/hash_pbkdf2>`_. |
| 124 | |
Andrey Andreev | 81f0367 | 2014-02-13 17:57:41 +0200 | [diff] [blame] | 125 | **************** |
| 126 | Multibyte String |
| 127 | **************** |
| 128 | |
| 129 | This set of compatibility functions offers limited support for PHP's |
| 130 | `Multibyte String extension <http://php.net/mbstring>`_. Because of |
| 131 | the limited alternative solutions, only a few functions are available. |
| 132 | |
| 133 | .. note:: When a character set parameter is ommited, |
| 134 | ``$config['charset']`` will be used. |
| 135 | |
| 136 | Dependancies |
| 137 | ============ |
| 138 | |
| 139 | - `iconv <http://php.net/iconv>`_ extension |
| 140 | |
| 141 | .. important:: This dependancy is optional and these functions will |
| 142 | always be declared. If iconv is not available, they WILL |
| 143 | fall-back to their non-mbstring versions. |
| 144 | |
| 145 | .. important:: Where a character set is supplied, it must be |
| 146 | supported by iconv and in a format that it recognizes. |
| 147 | |
| 148 | .. note:: For you own dependancy check on the actual mbstring |
| 149 | extension, use the ``MB_ENABLED`` constant. |
| 150 | |
| 151 | Function reference |
| 152 | ================== |
| 153 | |
| 154 | .. function:: mb_strlen($str[, $encoding = NULL]) |
| 155 | |
| 156 | :param string $str: Input string |
| 157 | :param string $encoding: Character set |
| 158 | :returns: Number of characters in the input string or FALSE on failure |
| 159 | :rtype: string |
| 160 | |
| 161 | For more information, please refer to the `PHP manual for |
| 162 | mb_strlen() <http://php.net/mb_strlen>`_. |
| 163 | |
| 164 | .. function:: mb_strpos($haystack, $needle[, $offset = 0[, $encoding = NULL]]) |
| 165 | |
| 166 | :param string $haystack: String to search in |
| 167 | :param string $needle: Part of string to search for |
| 168 | :param int $offset: Search offset |
| 169 | :param string $encoding: Character set |
| 170 | :returns: Numeric character position of where $needle was found or FALSE if not found |
| 171 | :rtype: mixed |
| 172 | |
| 173 | For more information, please refer to the `PHP manual for |
| 174 | mb_strpos() <http://php.net/mb_strpos>`_. |
| 175 | |
| 176 | .. function:: mb_substr($str, $start[, $length = NULL[, $encoding = NULL]]) |
| 177 | |
| 178 | :param string $str: Input string |
| 179 | :param int $start: Position of first character |
| 180 | :param int $length: Maximum number of characters |
| 181 | :param string $encoding: Character set |
| 182 | :returns: Portion of $str specified by $start and $length or FALSE on failure |
| 183 | :rtype: string |
| 184 | |
| 185 | For more information, please refer to the `PHP manual for |
Andrey Andreev | 0254589 | 2014-02-19 23:49:31 +0200 | [diff] [blame] | 186 | mb_substr() <http://php.net/mb_substr>`_. |
| 187 | |
| 188 | *************** |
| 189 | Array Functions |
| 190 | *************** |
| 191 | |
| 192 | This set of compatibility functions offers support for a few |
| 193 | standard `Array Functions <http://php.net/book.array>`_ in PHP |
| 194 | that otherwise require a newer PHP version. |
| 195 | |
| 196 | Dependancies |
| 197 | ============ |
| 198 | |
| 199 | - None |
| 200 | |
| 201 | Function reference |
| 202 | ================== |
| 203 | |
| 204 | .. function:: array_column(array $array, $column_key[, $index_key = NULL]) |
| 205 | |
| 206 | :param array $array: Array to fetch results from |
| 207 | :param mixed $column_key: Key of the column to return values from |
| 208 | :param mixed $index_key: Key to use for the returned values |
| 209 | :returns: An array of values representing a single column from the input array |
| 210 | :rtype: array |
| 211 | |
| 212 | For more information, please refer to the `PHP manual for |
| 213 | array_column() <http://php.net/array_column>`_. |
| 214 | |
| 215 | .. function:: array_replace(array $array1[, ...]) |
| 216 | |
| 217 | :param array $array1: Array in which to replace elements |
| 218 | :param array ...: Array (or multiple ones) from which to extract elements |
| 219 | :returns: Modified array |
| 220 | :rtype: array |
| 221 | |
| 222 | For more information, please refer to the `PHP manual for |
| 223 | array_replace() <http://php.net/array_replace>`_. |
| 224 | |
| 225 | .. function:: array_replace_recursive(array $array1[, ...]) |
| 226 | |
| 227 | :param array $array1: Array in which to replace elements |
| 228 | :param array ...: Array (or multiple ones) from which to extract elements |
| 229 | :returns: Modified array |
| 230 | :rtype: array |
| 231 | |
| 232 | For more information, please refer to the `PHP manual for |
| 233 | array_replace_recursive() <http://php.net/array_replace_recursive>`_. |
| 234 | |
| 235 | .. important:: Only PHP's native function can detect endless recursion. |
| 236 | Unless you are running PHP 5.3+, be careful with references! |