Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

174 rindas
5.5 KiB

  1. <?php
  2. /**
  3. * Random_* Compatibility Library
  4. * for using the new PHP 7 random_* API in PHP 5 projects
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2015 Paragon Initiative Enterprises
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. * SOFTWARE.
  27. */
  28. if (!function_exists('RandomCompat_strlen')) {
  29. if (
  30. defined('MB_OVERLOAD_STRING') &&
  31. ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
  32. ) {
  33. /**
  34. * strlen() implementation that isn't brittle to mbstring.func_overload
  35. *
  36. * This version uses mb_strlen() in '8bit' mode to treat strings as raw
  37. * binary rather than UTF-8, ISO-8859-1, etc
  38. *
  39. * @param string $binary_string
  40. *
  41. * @throws TypeError
  42. *
  43. * @return int
  44. */
  45. function RandomCompat_strlen($binary_string)
  46. {
  47. if (!is_string($binary_string)) {
  48. throw new TypeError(
  49. 'RandomCompat_strlen() expects a string'
  50. );
  51. }
  52. return mb_strlen($binary_string, '8bit');
  53. }
  54. } else {
  55. /**
  56. * strlen() implementation that isn't brittle to mbstring.func_overload
  57. *
  58. * This version just used the default strlen()
  59. *
  60. * @param string $binary_string
  61. *
  62. * @throws TypeError
  63. *
  64. * @return int
  65. */
  66. function RandomCompat_strlen($binary_string)
  67. {
  68. if (!is_string($binary_string)) {
  69. throw new TypeError(
  70. 'RandomCompat_strlen() expects a string'
  71. );
  72. }
  73. return strlen($binary_string);
  74. }
  75. }
  76. }
  77. if (!function_exists('RandomCompat_substr')) {
  78. if (
  79. defined('MB_OVERLOAD_STRING')
  80. &&
  81. ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
  82. ) {
  83. /**
  84. * substr() implementation that isn't brittle to mbstring.func_overload
  85. *
  86. * This version uses mb_substr() in '8bit' mode to treat strings as raw
  87. * binary rather than UTF-8, ISO-8859-1, etc
  88. *
  89. * @param string $binary_string
  90. * @param int $start
  91. * @param int $length (optional)
  92. *
  93. * @throws TypeError
  94. *
  95. * @return string
  96. */
  97. function RandomCompat_substr($binary_string, $start, $length = null)
  98. {
  99. if (!is_string($binary_string)) {
  100. throw new TypeError(
  101. 'RandomCompat_substr(): First argument should be a string'
  102. );
  103. }
  104. if (!is_int($start)) {
  105. throw new TypeError(
  106. 'RandomCompat_substr(): Second argument should be an integer'
  107. );
  108. }
  109. if ($length === null) {
  110. /**
  111. * mb_substr($str, 0, NULL, '8bit') returns an empty string on
  112. * PHP 5.3, so we have to find the length ourselves.
  113. */
  114. $length = RandomCompat_strlen($length) - $start;
  115. } elseif (!is_int($length)) {
  116. throw new TypeError(
  117. 'RandomCompat_substr(): Third argument should be an integer, or omitted'
  118. );
  119. }
  120. return mb_substr($binary_string, $start, $length, '8bit');
  121. }
  122. } else {
  123. /**
  124. * substr() implementation that isn't brittle to mbstring.func_overload
  125. *
  126. * This version just uses the default substr()
  127. *
  128. * @param string $binary_string
  129. * @param int $start
  130. * @param int $length (optional)
  131. *
  132. * @throws TypeError
  133. *
  134. * @return string
  135. */
  136. function RandomCompat_substr($binary_string, $start, $length = null)
  137. {
  138. if (!is_string($binary_string)) {
  139. throw new TypeError(
  140. 'RandomCompat_substr(): First argument should be a string'
  141. );
  142. }
  143. if (!is_int($start)) {
  144. throw new TypeError(
  145. 'RandomCompat_substr(): Second argument should be an integer'
  146. );
  147. }
  148. if ($length !== null) {
  149. if (!is_int($length)) {
  150. throw new TypeError(
  151. 'RandomCompat_substr(): Third argument should be an integer, or omitted'
  152. );
  153. }
  154. return substr($binary_string, $start, $length);
  155. }
  156. return substr($binary_string, $start);
  157. }
  158. }
  159. }