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