You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.6 KiB

7 years ago
  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 - 2017, 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. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://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 https://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 array $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', 'resource'), 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. }