PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/thecodingmachine/safe/lib/special_cases.php

https://gitlab.com/wendy-du-973/club-invest-gr2
PHP | 241 lines | 75 code | 10 blank | 156 comment | 11 complexity | f5f8e8ef20a6d4a06fae258d9d92994b MD5 | raw file
  1. <?php
  2. /**
  3. * This file contains all the functions that could not be dealt with automatically using the code generator.
  4. * If you add a function in this list, do not forget to add it in the generator/config/specialCasesFunctions.php
  5. *
  6. */
  7. namespace Safe;
  8. use Safe\Exceptions\SocketsException;
  9. use const PREG_NO_ERROR;
  10. use Safe\Exceptions\ApcException;
  11. use Safe\Exceptions\ApcuException;
  12. use Safe\Exceptions\JsonException;
  13. use Safe\Exceptions\OpensslException;
  14. use Safe\Exceptions\PcreException;
  15. /**
  16. * Wrapper for json_decode that throws when an error occurs.
  17. *
  18. * @param string $json JSON data to parse
  19. * @param bool $assoc When true, returned objects will be converted
  20. * into associative arrays.
  21. * @param int $depth User specified recursion depth.
  22. * @param int $options Bitmask of JSON decode options.
  23. *
  24. * @return mixed
  25. * @throws JsonException if the JSON cannot be decoded.
  26. * @link http://www.php.net/manual/en/function.json-decode.php
  27. */
  28. function json_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
  29. {
  30. $data = \json_decode($json, $assoc, $depth, $options);
  31. if (JSON_ERROR_NONE !== json_last_error()) {
  32. throw JsonException::createFromPhpError();
  33. }
  34. return $data;
  35. }
  36. /**
  37. * Fetchs a stored variable from the cache.
  38. *
  39. * @param mixed $key The key used to store the value (with
  40. * apc_store). If an array is passed then each
  41. * element is fetched and returned.
  42. * @return mixed The stored variable or array of variables on success; FALSE on failure
  43. * @throws ApcException
  44. *
  45. */
  46. function apc_fetch($key)
  47. {
  48. error_clear_last();
  49. $result = \apc_fetch($key, $success);
  50. if ($success === false) {
  51. throw ApcException::createFromPhpError();
  52. }
  53. return $result;
  54. }
  55. /**
  56. * Fetchs an entry from the cache.
  57. *
  58. * @param string|string[] $key The key used to store the value (with
  59. * apcu_store). If an array is passed then each
  60. * element is fetched and returned.
  61. * @return mixed The stored variable or array of variables on success
  62. * @throws ApcuException
  63. *
  64. */
  65. function apcu_fetch($key)
  66. {
  67. error_clear_last();
  68. $result = \apcu_fetch($key, $success);
  69. if ($success === false) {
  70. throw ApcuException::createFromPhpError();
  71. }
  72. return $result;
  73. }
  74. /**
  75. * Searches subject for matches to
  76. * pattern and replaces them with
  77. * replacement.
  78. *
  79. * @param mixed $pattern The pattern to search for. It can be either a string or an array with
  80. * strings.
  81. *
  82. * Several PCRE modifiers
  83. * are also available.
  84. * @param mixed $replacement The string or an array with strings to replace. If this parameter is a
  85. * string and the pattern parameter is an array,
  86. * all patterns will be replaced by that string. If both
  87. * pattern and replacement
  88. * parameters are arrays, each pattern will be
  89. * replaced by the replacement counterpart. If
  90. * there are fewer elements in the replacement
  91. * array than in the pattern array, any extra
  92. * patterns will be replaced by an empty string.
  93. *
  94. * replacement may contain references of the form
  95. * \\n or
  96. * $n, with the latter form
  97. * being the preferred one. Every such reference will be replaced by the text
  98. * captured by the n'th parenthesized pattern.
  99. * n can be from 0 to 99, and
  100. * \\0 or $0 refers to the text matched
  101. * by the whole pattern. Opening parentheses are counted from left to right
  102. * (starting from 1) to obtain the number of the capturing subpattern.
  103. * To use backslash in replacement, it must be doubled
  104. * ("\\\\" PHP string).
  105. *
  106. * When working with a replacement pattern where a backreference is
  107. * immediately followed by another number (i.e.: placing a literal number
  108. * immediately after a matched pattern), you cannot use the familiar
  109. * \\1 notation for your backreference.
  110. * \\11, for example, would confuse
  111. * preg_replace since it does not know whether you
  112. * want the \\1 backreference followed by a literal
  113. * 1, or the \\11 backreference
  114. * followed by nothing. In this case the solution is to use
  115. * ${1}1. This creates an isolated
  116. * $1 backreference, leaving the 1
  117. * as a literal.
  118. *
  119. * When using the deprecated e modifier, this function escapes
  120. * some characters (namely ', ",
  121. * \ and NULL) in the strings that replace the
  122. * backreferences. This is done to ensure that no syntax errors arise
  123. * from backreference usage with either single or double quotes (e.g.
  124. * 'strlen(\'$1\')+strlen("$2")'). Make sure you are
  125. * aware of PHP's string
  126. * syntax to know exactly how the interpreted string will look.
  127. * @param string|array|string[] $subject The string or an array with strings to search and replace.
  128. *
  129. * If subject is an array, then the search and
  130. * replace is performed on every entry of subject,
  131. * and the return value is an array as well.
  132. * @param int $limit The maximum possible replacements for each pattern in each
  133. * subject string. Defaults to
  134. * -1 (no limit).
  135. * @param int $count If specified, this variable will be filled with the number of
  136. * replacements done.
  137. * @return string|array|string[] preg_replace returns an array if the
  138. * subject parameter is an array, or a string
  139. * otherwise.
  140. *
  141. * If matches are found, the new subject will
  142. * be returned, otherwise subject will be
  143. * returned unchanged.
  144. *
  145. * @throws PcreException
  146. *
  147. */
  148. function preg_replace($pattern, $replacement, $subject, int $limit = -1, int &$count = null)
  149. {
  150. error_clear_last();
  151. $result = \preg_replace($pattern, $replacement, $subject, $limit, $count);
  152. if (preg_last_error() !== PREG_NO_ERROR || $result === null) {
  153. throw PcreException::createFromPhpError();
  154. }
  155. return $result;
  156. }
  157. /**
  158. * @param resource|null $dir_handle
  159. * @return string|false
  160. * @deprecated
  161. * This function is only in safe because the php documentation is wrong
  162. */
  163. function readdir($dir_handle = null)
  164. {
  165. if ($dir_handle !== null) {
  166. $result = \readdir($dir_handle);
  167. } else {
  168. $result = \readdir();
  169. }
  170. return $result;
  171. }
  172. /**
  173. * Encrypts given data with given method and key, returns a raw
  174. * or base64 encoded string
  175. *
  176. * @param string $data The plaintext message data to be encrypted.
  177. * @param string $method The cipher method. For a list of available cipher methods, use openssl_get_cipher_methods.
  178. * @param string $key The key.
  179. * @param int $options options is a bitwise disjunction of the flags
  180. * OPENSSL_RAW_DATA and
  181. * OPENSSL_ZERO_PADDING.
  182. * @param string $iv A non-NULL Initialization Vector.
  183. * @param string $tag The authentication tag passed by reference when using AEAD cipher mode (GCM or CCM).
  184. * @param string $aad Additional authentication data.
  185. * @param int $tag_length The length of the authentication tag. Its value can be between 4 and 16 for GCM mode.
  186. * @return string Returns the encrypted string.
  187. * @throws OpensslException
  188. *
  189. */
  190. function openssl_encrypt(string $data, string $method, string $key, int $options = 0, string $iv = "", string &$tag = "", string $aad = "", int $tag_length = 16): string
  191. {
  192. error_clear_last();
  193. // The $tag parameter is handled in a weird way by openssl_encrypt. It cannot be provided unless encoding is AEAD
  194. if (func_num_args() <= 5) {
  195. $result = \openssl_encrypt($data, $method, $key, $options, $iv);
  196. } else {
  197. $result = \openssl_encrypt($data, $method, $key, $options, $iv, $tag, $aad, $tag_length);
  198. }
  199. if ($result === false) {
  200. throw OpensslException::createFromPhpError();
  201. }
  202. return $result;
  203. }
  204. /**
  205. * The function socket_write writes to the
  206. * socket from the given
  207. * buffer.
  208. *
  209. * @param resource $socket
  210. * @param string $buffer The buffer to be written.
  211. * @param int $length The optional parameter length can specify an
  212. * alternate length of bytes written to the socket. If this length is
  213. * greater than the buffer length, it is silently truncated to the length
  214. * of the buffer.
  215. * @return int Returns the number of bytes successfully written to the socket.
  216. * The error code can be retrieved with
  217. * socket_last_error. This code may be passed to
  218. * socket_strerror to get a textual explanation of the
  219. * error.
  220. * @throws SocketsException
  221. *
  222. */
  223. function socket_write($socket, string $buffer, int $length = 0): int
  224. {
  225. error_clear_last();
  226. $result = $length === 0 ? \socket_write($socket, $buffer) : \socket_write($socket, $buffer, $length);
  227. if ($result === false) {
  228. throw SocketsException::createFromPhpError();
  229. }
  230. return $result;
  231. }