PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/compat/standard.php

https://gitlab.com/nadeermalangadan/CodeIgniter
PHP | 389 lines | 258 code | 43 blank | 88 comment | 53 complexity | 207c476ed74b6c74b0ce15db3ef425a2 MD5 | raw file
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link http://codeigniter.com
  35. * @since Version 3.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * PHP ext/standard compatibility package
  41. *
  42. * @package CodeIgniter
  43. * @subpackage CodeIgniter
  44. * @category Compatibility
  45. * @author Andrey Andreev
  46. * @link http://codeigniter.com/user_guide/
  47. */
  48. // ------------------------------------------------------------------------
  49. if (is_php('5.5'))
  50. {
  51. return;
  52. }
  53. // ------------------------------------------------------------------------
  54. if ( ! function_exists('array_column'))
  55. {
  56. /**
  57. * array_column()
  58. *
  59. * @link http://php.net/array_column
  60. * @param string $array
  61. * @param mixed $column_key
  62. * @param mixed $index_key
  63. * @return array
  64. */
  65. function array_column(array $array, $column_key, $index_key = NULL)
  66. {
  67. if ( ! in_array($type = gettype($column_key), array('integer', 'string', 'NULL'), TRUE))
  68. {
  69. if ($type === 'double')
  70. {
  71. $column_key = (int) $column_key;
  72. }
  73. elseif ($type === 'object' && method_exists($column_key, '__toString'))
  74. {
  75. $column_key = (string) $column_key;
  76. }
  77. else
  78. {
  79. trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
  80. return FALSE;
  81. }
  82. }
  83. if ( ! in_array($type = gettype($index_key), array('integer', 'string', 'NULL'), TRUE))
  84. {
  85. if ($type === 'double')
  86. {
  87. $index_key = (int) $index_key;
  88. }
  89. elseif ($type === 'object' && method_exists($index_key, '__toString'))
  90. {
  91. $index_key = (string) $index_key;
  92. }
  93. else
  94. {
  95. trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
  96. return FALSE;
  97. }
  98. }
  99. $result = array();
  100. foreach ($array as &$a)
  101. {
  102. if ($column_key === NULL)
  103. {
  104. $value = $a;
  105. }
  106. elseif (is_array($a) && array_key_exists($column_key, $a))
  107. {
  108. $value = $a[$column_key];
  109. }
  110. else
  111. {
  112. continue;
  113. }
  114. if ($index_key === NULL OR ! array_key_exists($index_key, $a))
  115. {
  116. $result[] = $value;
  117. }
  118. else
  119. {
  120. $result[$a[$index_key]] = $value;
  121. }
  122. }
  123. return $result;
  124. }
  125. }
  126. // ------------------------------------------------------------------------
  127. if (is_php('5.4'))
  128. {
  129. return;
  130. }
  131. // ------------------------------------------------------------------------
  132. if ( ! function_exists('hex2bin'))
  133. {
  134. /**
  135. * hex2bin()
  136. *
  137. * @link http://php.net/hex2bin
  138. * @param string $data
  139. * @return string
  140. */
  141. function hex2bin($data)
  142. {
  143. if (in_array($type = gettype($data), array('array', 'double', 'object'), TRUE))
  144. {
  145. if ($type === 'object' && method_exists($data, '__toString'))
  146. {
  147. $data = (string) $data;
  148. }
  149. else
  150. {
  151. trigger_error('hex2bin() expects parameter 1 to be string, '.$type.' given', E_USER_WARNING);
  152. return NULL;
  153. }
  154. }
  155. if (strlen($data) % 2 !== 0)
  156. {
  157. trigger_error('Hexadecimal input string must have an even length', E_USER_WARNING);
  158. return FALSE;
  159. }
  160. elseif ( ! preg_match('/^[0-9a-f]*$/i', $data))
  161. {
  162. trigger_error('Input string must be hexadecimal string', E_USER_WARNING);
  163. return FALSE;
  164. }
  165. return pack('H*', $data);
  166. }
  167. }
  168. // ------------------------------------------------------------------------
  169. if (is_php('5.3'))
  170. {
  171. return;
  172. }
  173. // ------------------------------------------------------------------------
  174. if ( ! function_exists('array_replace'))
  175. {
  176. /**
  177. * array_replace()
  178. *
  179. * @link http://php.net/array_replace
  180. * @return array
  181. */
  182. function array_replace()
  183. {
  184. $arrays = func_get_args();
  185. if (($c = count($arrays)) === 0)
  186. {
  187. trigger_error('array_replace() expects at least 1 parameter, 0 given', E_USER_WARNING);
  188. return NULL;
  189. }
  190. elseif ($c === 1)
  191. {
  192. if ( ! is_array($arrays[0]))
  193. {
  194. trigger_error('array_replace(): Argument #1 is not an array', E_USER_WARNING);
  195. return NULL;
  196. }
  197. return $arrays[0];
  198. }
  199. $array = array_shift($arrays);
  200. $c--;
  201. for ($i = 0; $i < $c; $i++)
  202. {
  203. if ( ! is_array($arrays[$i]))
  204. {
  205. trigger_error('array_replace(): Argument #'.($i + 2).' is not an array', E_USER_WARNING);
  206. return NULL;
  207. }
  208. elseif (empty($arrays[$i]))
  209. {
  210. continue;
  211. }
  212. foreach (array_keys($arrays[$i]) as $key)
  213. {
  214. $array[$key] = $arrays[$i][$key];
  215. }
  216. }
  217. return $array;
  218. }
  219. }
  220. // ------------------------------------------------------------------------
  221. if ( ! function_exists('array_replace_recursive'))
  222. {
  223. /**
  224. * array_replace_recursive()
  225. *
  226. * @link http://php.net/array_replace_recursive
  227. * @return array
  228. */
  229. function array_replace_recursive()
  230. {
  231. $arrays = func_get_args();
  232. if (($c = count($arrays)) === 0)
  233. {
  234. trigger_error('array_replace_recursive() expects at least 1 parameter, 0 given', E_USER_WARNING);
  235. return NULL;
  236. }
  237. elseif ($c === 1)
  238. {
  239. if ( ! is_array($arrays[0]))
  240. {
  241. trigger_error('array_replace_recursive(): Argument #1 is not an array', E_USER_WARNING);
  242. return NULL;
  243. }
  244. return $arrays[0];
  245. }
  246. $array = array_shift($arrays);
  247. $c--;
  248. for ($i = 0; $i < $c; $i++)
  249. {
  250. if ( ! is_array($arrays[$i]))
  251. {
  252. trigger_error('array_replace_recursive(): Argument #'.($i + 2).' is not an array', E_USER_WARNING);
  253. return NULL;
  254. }
  255. elseif (empty($arrays[$i]))
  256. {
  257. continue;
  258. }
  259. foreach (array_keys($arrays[$i]) as $key)
  260. {
  261. $array[$key] = (is_array($arrays[$i][$key]) && isset($array[$key]) && is_array($array[$key]))
  262. ? array_replace_recursive($array[$key], $arrays[$i][$key])
  263. : $arrays[$i][$key];
  264. }
  265. }
  266. return $array;
  267. }
  268. }
  269. // ------------------------------------------------------------------------
  270. if ( ! function_exists('quoted_printable_encode'))
  271. {
  272. /**
  273. * quoted_printable_encode()
  274. *
  275. * @link http://php.net/quoted_printable_encode
  276. * @param string $str
  277. * @return string
  278. */
  279. function quoted_printable_encode($str)
  280. {
  281. if (strlen($str) === 0)
  282. {
  283. return '';
  284. }
  285. elseif (in_array($type = gettype($str), array('array', 'object'), TRUE))
  286. {
  287. if ($type === 'object' && method_exists($str, '__toString'))
  288. {
  289. $str = (string) $str;
  290. }
  291. else
  292. {
  293. trigger_error('quoted_printable_encode() expects parameter 1 to be string, '.$type.' given', E_USER_WARNING);
  294. return NULL;
  295. }
  296. }
  297. if (function_exists('imap_8bit'))
  298. {
  299. return imap_8bit($str);
  300. }
  301. $i = $lp = 0;
  302. $output = '';
  303. $hex = '0123456789ABCDEF';
  304. $length = (extension_loaded('mbstring') && ini_get('mbstring.func_overload'))
  305. ? mb_strlen($str, '8bit')
  306. : strlen($str);
  307. while ($length--)
  308. {
  309. if ((($c = $str[$i++]) === "\015") && isset($str[$i]) && ($str[$i] === "\012") && $length > 0)
  310. {
  311. $output .= "\015".$str[$i++];
  312. $length--;
  313. $lp = 0;
  314. continue;
  315. }
  316. if (
  317. ctype_cntrl($c)
  318. OR (ord($c) === 0x7f)
  319. OR (ord($c) & 0x80)
  320. OR ($c === '=')
  321. OR ($c === ' ' && isset($str[$i]) && $str[$i] === "\015")
  322. )
  323. {
  324. if (
  325. (($lp += 3) > 75 && ord($c) <= 0x7f)
  326. OR (ord($c) > 0x7f && ord($c) <= 0xdf && ($lp + 3) > 75)
  327. OR (ord($c) > 0xdf && ord($c) <= 0xef && ($lp + 6) > 75)
  328. OR (ord($c) > 0xef && ord($c) <= 0xf4 && ($lp + 9) > 75)
  329. )
  330. {
  331. $output .= "=\015\012";
  332. $lp = 3;
  333. }
  334. $output .= '='.$hex[ord($c) >> 4].$hex[ord($c) & 0xf];
  335. continue;
  336. }
  337. if ((++$lp) > 75)
  338. {
  339. $output .= "=\015\012";
  340. $lp = 1;
  341. }
  342. $output .= $c;
  343. }
  344. return $output;
  345. }
  346. }