| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\MiscException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Defines a named constant at runtime. |
| 9 |
* |
| 10 |
* @param string $name The name of the constant. |
| 11 |
* |
| 12 |
* It is possible to define constants with reserved or |
| 13 |
* even invalid names, whose value can (only) be retrieved with |
| 14 |
* constant. However, doing so is not recommended. |
| 15 |
* @param mixed $value The value of the constant. In PHP 5, value must |
| 16 |
* be a scalar value (integer, |
| 17 |
* float, string, boolean, or |
| 18 |
* NULL). In PHP 7, array values are also accepted. |
| 19 |
* |
| 20 |
* While it is possible to define resource constants, it is |
| 21 |
* not recommended and may cause unpredictable behavior. |
| 22 |
* @param bool $case_insensitive If set to TRUE, the constant will be defined case-insensitive. |
| 23 |
* The default behavior is case-sensitive; i.e. |
| 24 |
* CONSTANT and Constant represent |
| 25 |
* different values. |
| 26 |
* |
| 27 |
* Case-insensitive constants are stored as lower-case. |
| 28 |
* @throws MiscException |
| 29 |
* |
| 30 |
*/ |
| 31 |
function define(string $name, $value, bool $case_insensitive = false): void |
| 32 |
{ |
| 33 |
error_clear_last(); |
| 34 |
$result = \define($name, $value, $case_insensitive); |
| 35 |
if ($result === false) { |
| 36 |
throw MiscException::createFromPhpError(); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Prints out or returns a syntax highlighted version of the code contained |
| 43 |
* in filename using the colors defined in the |
| 44 |
* built-in syntax highlighter for PHP. |
| 45 |
* |
| 46 |
* Many servers are configured to automatically highlight files |
| 47 |
* with a phps extension. For example, |
| 48 |
* example.phps when viewed will show the |
| 49 |
* syntax highlighted source of the file. To enable this, add this |
| 50 |
* line to the httpd.conf: |
| 51 |
* |
| 52 |
* @param string $filename Path to the PHP file to be highlighted. |
| 53 |
* @param bool $return Set this parameter to TRUE to make this function return the |
| 54 |
* highlighted code. |
| 55 |
* @return string|bool If return is set to TRUE, returns the highlighted |
| 56 |
* code as a string instead of printing it out. Otherwise, it will return |
| 57 |
* TRUE on success, FALSE on failure. |
| 58 |
* @throws MiscException |
| 59 |
* |
| 60 |
*/ |
| 61 |
function highlight_file(string $filename, bool $return = false) |
| 62 |
{ |
| 63 |
error_clear_last(); |
| 64 |
$result = \highlight_file($filename, $return); |
| 65 |
if ($result === false) { |
| 66 |
throw MiscException::createFromPhpError(); |
| 67 |
} |
| 68 |
return $result; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* |
| 74 |
* |
| 75 |
* @param string $str The PHP code to be highlighted. This should include the opening tag. |
| 76 |
* @param bool $return Set this parameter to TRUE to make this function return the |
| 77 |
* highlighted code. |
| 78 |
* @return string|bool If return is set to TRUE, returns the highlighted |
| 79 |
* code as a string instead of printing it out. Otherwise, it will return |
| 80 |
* TRUE on success, FALSE on failure. |
| 81 |
* @throws MiscException |
| 82 |
* |
| 83 |
*/ |
| 84 |
function highlight_string(string $str, bool $return = false) |
| 85 |
{ |
| 86 |
error_clear_last(); |
| 87 |
$result = \highlight_string($str, $return); |
| 88 |
if ($result === false) { |
| 89 |
throw MiscException::createFromPhpError(); |
| 90 |
} |
| 91 |
return $result; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* Pack given arguments into a binary string according to |
| 97 |
* format. |
| 98 |
* |
| 99 |
* The idea for this function was taken from Perl and all formatting codes |
| 100 |
* work the same as in Perl. However, there are some formatting codes that are |
| 101 |
* missing such as Perl's "u" format code. |
| 102 |
* |
| 103 |
* Note that the distinction between signed and unsigned values only |
| 104 |
* affects the function unpack, where as |
| 105 |
* function pack gives the same result for |
| 106 |
* signed and unsigned format codes. |
| 107 |
* |
| 108 |
* @param string $format The format string consists of format codes |
| 109 |
* followed by an optional repeater argument. The repeater argument can |
| 110 |
* be either an integer value or * for repeating to |
| 111 |
* the end of the input data. For a, A, h, H the repeat count specifies |
| 112 |
* how many characters of one data argument are taken, for @ it is the |
| 113 |
* absolute position where to put the next data, for everything else the |
| 114 |
* repeat count specifies how many data arguments are consumed and packed |
| 115 |
* into the resulting binary string. |
| 116 |
* |
| 117 |
* Currently implemented formats are: |
| 118 |
* |
| 119 |
* pack format characters |
| 120 |
* |
| 121 |
* |
| 122 |
* |
| 123 |
* Code |
| 124 |
* Description |
| 125 |
* |
| 126 |
* |
| 127 |
* |
| 128 |
* |
| 129 |
* a |
| 130 |
* NUL-padded string |
| 131 |
* |
| 132 |
* |
| 133 |
* A |
| 134 |
* SPACE-padded string |
| 135 |
* |
| 136 |
* h |
| 137 |
* Hex string, low nibble first |
| 138 |
* |
| 139 |
* H |
| 140 |
* Hex string, high nibble first |
| 141 |
* csigned char |
| 142 |
* |
| 143 |
* C |
| 144 |
* unsigned char |
| 145 |
* |
| 146 |
* s |
| 147 |
* signed short (always 16 bit, machine byte order) |
| 148 |
* |
| 149 |
* |
| 150 |
* S |
| 151 |
* unsigned short (always 16 bit, machine byte order) |
| 152 |
* |
| 153 |
* |
| 154 |
* n |
| 155 |
* unsigned short (always 16 bit, big endian byte order) |
| 156 |
* |
| 157 |
* |
| 158 |
* v |
| 159 |
* unsigned short (always 16 bit, little endian byte order) |
| 160 |
* |
| 161 |
* |
| 162 |
* i |
| 163 |
* signed integer (machine dependent size and byte order) |
| 164 |
* |
| 165 |
* |
| 166 |
* I |
| 167 |
* unsigned integer (machine dependent size and byte order) |
| 168 |
* |
| 169 |
* |
| 170 |
* l |
| 171 |
* signed long (always 32 bit, machine byte order) |
| 172 |
* |
| 173 |
* |
| 174 |
* L |
| 175 |
* unsigned long (always 32 bit, machine byte order) |
| 176 |
* |
| 177 |
* |
| 178 |
* N |
| 179 |
* unsigned long (always 32 bit, big endian byte order) |
| 180 |
* |
| 181 |
* |
| 182 |
* V |
| 183 |
* unsigned long (always 32 bit, little endian byte order) |
| 184 |
* |
| 185 |
* |
| 186 |
* q |
| 187 |
* signed long long (always 64 bit, machine byte order) |
| 188 |
* |
| 189 |
* |
| 190 |
* Q |
| 191 |
* unsigned long long (always 64 bit, machine byte order) |
| 192 |
* |
| 193 |
* |
| 194 |
* J |
| 195 |
* unsigned long long (always 64 bit, big endian byte order) |
| 196 |
* |
| 197 |
* |
| 198 |
* P |
| 199 |
* unsigned long long (always 64 bit, little endian byte order) |
| 200 |
* |
| 201 |
* |
| 202 |
* f |
| 203 |
* float (machine dependent size and representation) |
| 204 |
* |
| 205 |
* |
| 206 |
* g |
| 207 |
* float (machine dependent size, little endian byte order) |
| 208 |
* |
| 209 |
* |
| 210 |
* G |
| 211 |
* float (machine dependent size, big endian byte order) |
| 212 |
* |
| 213 |
* |
| 214 |
* d |
| 215 |
* double (machine dependent size and representation) |
| 216 |
* |
| 217 |
* |
| 218 |
* e |
| 219 |
* double (machine dependent size, little endian byte order) |
| 220 |
* |
| 221 |
* |
| 222 |
* E |
| 223 |
* double (machine dependent size, big endian byte order) |
| 224 |
* |
| 225 |
* |
| 226 |
* x |
| 227 |
* NUL byte |
| 228 |
* |
| 229 |
* |
| 230 |
* X |
| 231 |
* Back up one byte |
| 232 |
* |
| 233 |
* |
| 234 |
* Z |
| 235 |
* NUL-padded string (new in PHP 5.5) |
| 236 |
* |
| 237 |
* |
| 238 |
* @ |
| 239 |
* NUL-fill to absolute position |
| 240 |
* |
| 241 |
* |
| 242 |
* |
| 243 |
* |
| 244 |
* @param mixed $params |
| 245 |
* @return string Returns a binary string containing data. |
| 246 |
* @throws MiscException |
| 247 |
* |
| 248 |
*/ |
| 249 |
function pack(string $format, ...$params): string |
| 250 |
{ |
| 251 |
error_clear_last(); |
| 252 |
if ($params !== []) { |
| 253 |
$result = \pack($format, ...$params); |
| 254 |
} else { |
| 255 |
$result = \pack($format); |
| 256 |
} |
| 257 |
if ($result === false) { |
| 258 |
throw MiscException::createFromPhpError(); |
| 259 |
} |
| 260 |
return $result; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
/** |
| 265 |
* Convert string from one codepage to another. |
| 266 |
* |
| 267 |
* @param int|string $in_codepage The codepage of the subject string. |
| 268 |
* Either the codepage name or identifier. |
| 269 |
* @param int|string $out_codepage The codepage to convert the subject string to. |
| 270 |
* Either the codepage name or identifier. |
| 271 |
* @param string $subject The string to convert. |
| 272 |
* @return string The subject string converted to |
| 273 |
* out_codepage. |
| 274 |
* @throws MiscException |
| 275 |
* |
| 276 |
*/ |
| 277 |
function sapi_windows_cp_conv($in_codepage, $out_codepage, string $subject): string |
| 278 |
{ |
| 279 |
error_clear_last(); |
| 280 |
$result = \sapi_windows_cp_conv($in_codepage, $out_codepage, $subject); |
| 281 |
if ($result === null) { |
| 282 |
throw MiscException::createFromPhpError(); |
| 283 |
} |
| 284 |
return $result; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
/** |
| 289 |
* Set the codepage of the current process. |
| 290 |
* |
| 291 |
* @param int $cp A codepage identifier. |
| 292 |
* @throws MiscException |
| 293 |
* |
| 294 |
*/ |
| 295 |
function sapi_windows_cp_set(int $cp): void |
| 296 |
{ |
| 297 |
error_clear_last(); |
| 298 |
$result = \sapi_windows_cp_set($cp); |
| 299 |
if ($result === false) { |
| 300 |
throw MiscException::createFromPhpError(); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
/** |
| 306 |
* Sends a CTRL event to another process in the same process group. |
| 307 |
* |
| 308 |
* @param int $event The CTRL even to send; |
| 309 |
* either PHP_WINDOWS_EVENT_CTRL_C |
| 310 |
* or PHP_WINDOWS_EVENT_CTRL_BREAK. |
| 311 |
* @param int $pid The ID of the process to which to send the event to. If 0 |
| 312 |
* is given, the event is sent to all processes of the process group. |
| 313 |
* @throws MiscException |
| 314 |
* |
| 315 |
*/ |
| 316 |
function sapi_windows_generate_ctrl_event(int $event, int $pid = 0): void |
| 317 |
{ |
| 318 |
error_clear_last(); |
| 319 |
$result = \sapi_windows_generate_ctrl_event($event, $pid); |
| 320 |
if ($result === false) { |
| 321 |
throw MiscException::createFromPhpError(); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
/** |
| 327 |
* If enable is omitted, the function returns TRUE if the stream stream has VT100 control codes enabled, FALSE otherwise. |
| 328 |
* |
| 329 |
* If enable is specified, the function will try to enable or disable the VT100 features of the stream stream. |
| 330 |
* If the feature has been successfully enabled (or disabled). |
| 331 |
* |
| 332 |
* At startup, PHP tries to enable the VT100 feature of the STDOUT/STDERR streams. By the way, if those streams are redirected to a file, the VT100 features may not be enabled. |
| 333 |
* |
| 334 |
* If VT100 support is enabled, it is possible to use control sequences as they are known from the VT100 terminal. |
| 335 |
* They allow the modification of the terminal's output. On Windows these sequences are called Console Virtual Terminal Sequences. |
| 336 |
* |
| 337 |
* @param resource $stream The stream on which the function will operate. |
| 338 |
* @param bool|null $enable If specified, the VT100 feature will be enabled (if TRUE) or disabled (if FALSE). |
| 339 |
* |
| 340 |
* @throws MiscException |
| 341 |
* |
| 342 |
*/ |
| 343 |
function sapi_windows_vt100_support($stream, ?bool $enable = null): void |
| 344 |
{ |
| 345 |
error_clear_last(); |
| 346 |
if ($enable !== null) { |
| 347 |
$result = \sapi_windows_vt100_support($stream, $enable); |
| 348 |
} else { |
| 349 |
$result = \sapi_windows_vt100_support($stream); |
| 350 |
} |
| 351 |
if ($result === false) { |
| 352 |
throw MiscException::createFromPhpError(); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
/** |
| 358 |
* |
| 359 |
* |
| 360 |
* @param int $seconds Halt time in seconds. |
| 361 |
* @return int Returns zero on success. |
| 362 |
* |
| 363 |
* If the call was interrupted by a signal, sleep returns |
| 364 |
* a non-zero value. On Windows, this value will always be |
| 365 |
* 192 (the value of the |
| 366 |
* WAIT_IO_COMPLETION constant within the Windows API). |
| 367 |
* On other platforms, the return value will be the number of seconds left to |
| 368 |
* sleep. |
| 369 |
* @throws MiscException |
| 370 |
* |
| 371 |
*/ |
| 372 |
function sleep(int $seconds): int |
| 373 |
{ |
| 374 |
error_clear_last(); |
| 375 |
$result = \sleep($seconds); |
| 376 |
if ($result === false) { |
| 377 |
throw MiscException::createFromPhpError(); |
| 378 |
} |
| 379 |
return $result; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
/** |
| 384 |
* Delays program execution for the given number of |
| 385 |
* seconds and nanoseconds. |
| 386 |
* |
| 387 |
* @param int $seconds Must be a non-negative integer. |
| 388 |
* @param int $nanoseconds Must be a non-negative integer less than 1 billion. |
| 389 |
* @return array{0:int,1:int}|bool Returns TRUE on success. |
| 390 |
* |
| 391 |
* If the delay was interrupted by a signal, an associative array will be |
| 392 |
* returned with the components: |
| 393 |
* |
| 394 |
* |
| 395 |
* |
| 396 |
* seconds - number of seconds remaining in |
| 397 |
* the delay |
| 398 |
* |
| 399 |
* |
| 400 |
* |
| 401 |
* |
| 402 |
* nanoseconds - number of nanoseconds |
| 403 |
* remaining in the delay |
| 404 |
* |
| 405 |
* |
| 406 |
* |
| 407 |
* @throws MiscException |
| 408 |
* |
| 409 |
*/ |
| 410 |
function time_nanosleep(int $seconds, int $nanoseconds) |
| 411 |
{ |
| 412 |
error_clear_last(); |
| 413 |
$result = \time_nanosleep($seconds, $nanoseconds); |
| 414 |
if ($result === false) { |
| 415 |
throw MiscException::createFromPhpError(); |
| 416 |
} |
| 417 |
return $result; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
/** |
| 422 |
* Makes the script sleep until the specified |
| 423 |
* timestamp. |
| 424 |
* |
| 425 |
* @param float $timestamp The timestamp when the script should wake. |
| 426 |
* @throws MiscException |
| 427 |
* |
| 428 |
*/ |
| 429 |
function time_sleep_until(float $timestamp): void |
| 430 |
{ |
| 431 |
error_clear_last(); |
| 432 |
$result = \time_sleep_until($timestamp); |
| 433 |
if ($result === false) { |
| 434 |
throw MiscException::createFromPhpError(); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
/** |
| 440 |
* Unpacks from a binary string into an array according to the given |
| 441 |
* format. |
| 442 |
* |
| 443 |
* The unpacked data is stored in an associative array. To |
| 444 |
* accomplish this you have to name the different format codes and |
| 445 |
* separate them by a slash /. If a repeater argument is present, |
| 446 |
* then each of the array keys will have a sequence number behind |
| 447 |
* the given name. |
| 448 |
* |
| 449 |
* @param string $format See pack for an explanation of the format codes. |
| 450 |
* @param string $data The packed data. |
| 451 |
* @param int $offset The offset to begin unpacking from. |
| 452 |
* @return array Returns an associative array containing unpacked elements of binary |
| 453 |
* string. |
| 454 |
* @throws MiscException |
| 455 |
* |
| 456 |
*/ |
| 457 |
function unpack(string $format, string $data, int $offset = 0): array |
| 458 |
{ |
| 459 |
error_clear_last(); |
| 460 |
$result = \unpack($format, $data, $offset); |
| 461 |
if ($result === false) { |
| 462 |
throw MiscException::createFromPhpError(); |
| 463 |
} |
| 464 |
return $result; |
| 465 |
} |
| 466 |
|