blob: 584968663a72218e2a50f7b3e7be8a42b97ab0dc [file] [log] [blame]
Andrey Andreev81f03672014-02-13 17:57:41 +02001#######################
2Compatibility Functions
3#######################
4
5CodeIgniter provides a set of compatibility functions that enable
6you to use functions what are otherwise natively available in PHP,
7but only in higher versions or depending on a certain extension.
8
9Being custom implementations, these functions will also have some
Andrey Andreev5b3fe7c2014-07-07 10:55:53 +030010set of dependencies on their own, but are still useful if your
Andrey Andreev81f03672014-02-13 17:57:41 +020011PHP setup doesn't offer them natively.
12
Andrey Andreevb6359a62016-10-24 09:41:29 +030013.. note:: Much like the :doc:`common functions <common_functions>`, the
Andrey Andreev81f03672014-02-13 17:57:41 +020014 compatibility functions are always available, as long as
Andrey Andreev5b3fe7c2014-07-07 10:55:53 +030015 their dependencies are met.
Andrey Andreev81f03672014-02-13 17:57:41 +020016
17.. contents::
18 :local:
19
20.. raw:: html
21
22 <div class="custom-index container"></div>
23
24****************
25Password Hashing
26****************
27
28This set of compatibility functions offers a "backport" of PHP's
29standard `Password Hashing extension <http://php.net/password>`_
30that is otherwise available only since PHP 5.5.
31
Andrey Andreev5b3fe7c2014-07-07 10:55:53 +030032Dependencies
Andrey Andreev81f03672014-02-13 17:57:41 +020033============
34
35- PHP 5.3.7
36- ``CRYPT_BLOWFISH`` support for ``crypt()``
37
38Constants
39=========
40
41- ``PASSWORD_BCRYPT``
42- ``PASSWORD_DEFAULT``
43
44Function reference
45==================
46
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020047.. php:function:: password_get_info($hash)
Andrey Andreev81f03672014-02-13 17:57:41 +020048
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 Andreevcd3d9db2015-02-02 13:41:01 +020056.. php:function:: password_hash($password, $algo[, $options = array()])
Andrey Andreev81f03672014-02-13 17:57:41 +020057
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 Andreev5b3fe7c2014-07-07 10:55:53 +030068 has a further dependency on an available CSPRNG source. Each
Andrey Andreev81f03672014-02-13 17:57:41 +020069 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 Andreevcd3d9db2015-02-02 13:41:01 +020075.. php:function:: password_needs_rehash()
Andrey Andreev81f03672014-02-13 17:57:41 +020076
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 Andreevcd3d9db2015-02-02 13:41:01 +020086.. php:function:: password_verify($password, $hash)
Andrey Andreev81f03672014-02-13 17:57:41 +020087
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 Andreev9a152a92014-02-18 16:29:53 +020096*********************
97Hash (Message Digest)
98*********************
99
Andrey Andreeva5621b82014-05-09 11:23:08 +0300100This compatibility layer contains backports for the ``hash_equals()``
101and ``hash_pbkdf2()`` functions, which otherwise require PHP 5.6 and/or
102PHP 5.5 respectively.
Andrey Andreev9a152a92014-02-18 16:29:53 +0200103
Andrey Andreev5b3fe7c2014-07-07 10:55:53 +0300104Dependencies
Andrey Andreev9a152a92014-02-18 16:29:53 +0200105============
106
107- None
108
109Function reference
110==================
111
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200112.. php:function:: hash_equals($known_string, $user_string)
Andrey Andreeva5621b82014-05-09 11:23:08 +0300113
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 Andreevcd3d9db2015-02-02 13:41:01 +0200122.. php:function:: hash_pbkdf2($algo, $password, $salt, $iterations[, $length = 0[, $raw_output = FALSE]])
Andrey Andreev9a152a92014-02-18 16:29:53 +0200123
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 Andreev81f03672014-02-13 17:57:41 +0200136****************
137Multibyte String
138****************
139
140This set of compatibility functions offers limited support for PHP's
141`Multibyte String extension <http://php.net/mbstring>`_. Because of
142the 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 Andreev5b3fe7c2014-07-07 10:55:53 +0300147Dependencies
Andrey Andreev81f03672014-02-13 17:57:41 +0200148============
149
150- `iconv <http://php.net/iconv>`_ extension
151
Andrey Andreev5b3fe7c2014-07-07 10:55:53 +0300152.. important:: This dependency is optional and these functions will
Andrey Andreev81f03672014-02-13 17:57:41 +0200153 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 Andreev5b3fe7c2014-07-07 10:55:53 +0300159.. note:: For you own dependency check on the actual mbstring
Andrey Andreev81f03672014-02-13 17:57:41 +0200160 extension, use the ``MB_ENABLED`` constant.
161
162Function reference
163==================
164
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200165.. php:function:: mb_strlen($str[, $encoding = NULL])
Andrey Andreev81f03672014-02-13 17:57:41 +0200166
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 Andreevcd3d9db2015-02-02 13:41:01 +0200175.. php:function:: mb_strpos($haystack, $needle[, $offset = 0[, $encoding = NULL]])
Andrey Andreev81f03672014-02-13 17:57:41 +0200176
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 Andreevcd3d9db2015-02-02 13:41:01 +0200187.. php:function:: mb_substr($str, $start[, $length = NULL[, $encoding = NULL]])
Andrey Andreev81f03672014-02-13 17:57:41 +0200188
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 Andreev02545892014-02-19 23:49:31 +0200197 mb_substr() <http://php.net/mb_substr>`_.
198
Andrey Andreev5b3fe7c2014-07-07 10:55:53 +0300199******************
200Standard Functions
201******************
Andrey Andreev02545892014-02-19 23:49:31 +0200202
203This set of compatibility functions offers support for a few
Andrey Andreev5b3fe7c2014-07-07 10:55:53 +0300204standard functions in PHP that otherwise require a newer PHP version.
Andrey Andreev02545892014-02-19 23:49:31 +0200205
Andrey Andreev5b3fe7c2014-07-07 10:55:53 +0300206Dependencies
Andrey Andreev02545892014-02-19 23:49:31 +0200207============
208
209- None
210
211Function reference
212==================
213
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200214.. php:function:: array_column(array $array, $column_key[, $index_key = NULL])
Andrey Andreev02545892014-02-19 23:49:31 +0200215
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 Andreevcd3d9db2015-02-02 13:41:01 +0200225.. php:function:: hex2bin($data)
Andrey Andreev5b3fe7c2014-07-07 10:55:53 +0300226
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 Andreev6500bc72014-07-07 14:11:26 +0300232 <http://php.net/hex2bin>`_.