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