| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_DEFENDER_VENDOR\Safe; |
| 4 |
|
| 5 |
use WP_DEFENDER_VENDOR\Safe\Exceptions\PosixException; |
| 6 |
|
| 7 |
/** |
| 8 |
* posix_access checks the user's permission of a file. |
| 9 |
* |
| 10 |
* @param string $file The name of the file to be tested. |
| 11 |
* @param int $mode A mask consisting of one or more of POSIX_F_OK, |
| 12 |
* POSIX_R_OK, POSIX_W_OK and |
| 13 |
* POSIX_X_OK. |
| 14 |
* |
| 15 |
* POSIX_R_OK, POSIX_W_OK and |
| 16 |
* POSIX_X_OK request checking whether the file |
| 17 |
* exists and has read, write and execute permissions, respectively. |
| 18 |
* POSIX_F_OK just requests checking for the |
| 19 |
* existence of the file. |
| 20 |
* @throws PosixException |
| 21 |
* |
| 22 |
*/ |
| 23 |
function posix_access(string $file, int $mode = POSIX_F_OK): void |
| 24 |
{ |
| 25 |
error_clear_last(); |
| 26 |
$result = \posix_access($file, $mode); |
| 27 |
if ($result === false) { |
| 28 |
throw PosixException::createFromPhpError(); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Gets information about a group provided its name. |
| 35 |
* |
| 36 |
* @param string $name The name of the group |
| 37 |
* @return array Returns an array on success. |
| 38 |
* The array elements returned are: |
| 39 |
* |
| 40 |
* The group information array |
| 41 |
* |
| 42 |
* |
| 43 |
* |
| 44 |
* Element |
| 45 |
* Description |
| 46 |
* |
| 47 |
* |
| 48 |
* |
| 49 |
* |
| 50 |
* name |
| 51 |
* |
| 52 |
* The name element contains the name of the group. This is |
| 53 |
* a short, usually less than 16 character "handle" of the |
| 54 |
* group, not the real, full name. This should be the same as |
| 55 |
* the name parameter used when |
| 56 |
* calling the function, and hence redundant. |
| 57 |
* |
| 58 |
* |
| 59 |
* |
| 60 |
* passwd |
| 61 |
* |
| 62 |
* The passwd element contains the group's password in an |
| 63 |
* encrypted format. Often, for example on a system employing |
| 64 |
* "shadow" passwords, an asterisk is returned instead. |
| 65 |
* |
| 66 |
* |
| 67 |
* |
| 68 |
* gid |
| 69 |
* |
| 70 |
* Group ID of the group in numeric form. |
| 71 |
* |
| 72 |
* |
| 73 |
* |
| 74 |
* members |
| 75 |
* |
| 76 |
* This consists of an array of |
| 77 |
* string's for all the members in the group. |
| 78 |
* |
| 79 |
* |
| 80 |
* |
| 81 |
* |
| 82 |
* |
| 83 |
* @throws PosixException |
| 84 |
* |
| 85 |
*/ |
| 86 |
function posix_getgrnam(string $name): array |
| 87 |
{ |
| 88 |
error_clear_last(); |
| 89 |
$result = \posix_getgrnam($name); |
| 90 |
if ($result === false) { |
| 91 |
throw PosixException::createFromPhpError(); |
| 92 |
} |
| 93 |
return $result; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Returns the process group identifier of the process |
| 99 |
* pid. |
| 100 |
* |
| 101 |
* @param int $pid The process id. |
| 102 |
* @return int Returns the identifier, as an integer. |
| 103 |
* @throws PosixException |
| 104 |
* |
| 105 |
*/ |
| 106 |
function posix_getpgid(int $pid): int |
| 107 |
{ |
| 108 |
error_clear_last(); |
| 109 |
$result = \posix_getpgid($pid); |
| 110 |
if ($result === false) { |
| 111 |
throw PosixException::createFromPhpError(); |
| 112 |
} |
| 113 |
return $result; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
* Calculates the group access list for the user specified in name. |
| 119 |
* |
| 120 |
* @param string $name The user to calculate the list for. |
| 121 |
* @param int $base_group_id Typically the group number from the password file. |
| 122 |
* @throws PosixException |
| 123 |
* |
| 124 |
*/ |
| 125 |
function posix_initgroups(string $name, int $base_group_id): void |
| 126 |
{ |
| 127 |
error_clear_last(); |
| 128 |
$result = \posix_initgroups($name, $base_group_id); |
| 129 |
if ($result === false) { |
| 130 |
throw PosixException::createFromPhpError(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* Send the signal sig to the process with |
| 137 |
* the process identifier pid. |
| 138 |
* |
| 139 |
* @param int $pid The process identifier. |
| 140 |
* @param int $sig One of the PCNTL signals constants. |
| 141 |
* @throws PosixException |
| 142 |
* |
| 143 |
*/ |
| 144 |
function posix_kill(int $pid, int $sig): void |
| 145 |
{ |
| 146 |
error_clear_last(); |
| 147 |
$result = \posix_kill($pid, $sig); |
| 148 |
if ($result === false) { |
| 149 |
throw PosixException::createFromPhpError(); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
/** |
| 155 |
* posix_mkfifo creates a special |
| 156 |
* FIFO file which exists in the file system and acts as |
| 157 |
* a bidirectional communication endpoint for processes. |
| 158 |
* |
| 159 |
* @param string $pathname Path to the FIFO file. |
| 160 |
* @param int $mode The second parameter mode has to be given in |
| 161 |
* octal notation (e.g. 0644). The permission of the newly created |
| 162 |
* FIFO also depends on the setting of the current |
| 163 |
* umask. The permissions of the created file are |
| 164 |
* (mode & ~umask). |
| 165 |
* @throws PosixException |
| 166 |
* |
| 167 |
*/ |
| 168 |
function posix_mkfifo(string $pathname, int $mode): void |
| 169 |
{ |
| 170 |
error_clear_last(); |
| 171 |
$result = \posix_mkfifo($pathname, $mode); |
| 172 |
if ($result === false) { |
| 173 |
throw PosixException::createFromPhpError(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Creates a special or ordinary file. |
| 180 |
* |
| 181 |
* @param string $pathname The file to create |
| 182 |
* @param int $mode This parameter is constructed by a bitwise OR between file type (one of |
| 183 |
* the following constants: POSIX_S_IFREG, |
| 184 |
* POSIX_S_IFCHR, POSIX_S_IFBLK, |
| 185 |
* POSIX_S_IFIFO or |
| 186 |
* POSIX_S_IFSOCK) and permissions. |
| 187 |
* @param int $major The major device kernel identifier (required to pass when using |
| 188 |
* S_IFCHR or S_IFBLK). |
| 189 |
* @param int $minor The minor device kernel identifier. |
| 190 |
* @throws PosixException |
| 191 |
* |
| 192 |
*/ |
| 193 |
function posix_mknod(string $pathname, int $mode, int $major = 0, int $minor = 0): void |
| 194 |
{ |
| 195 |
error_clear_last(); |
| 196 |
$result = \posix_mknod($pathname, $mode, $major, $minor); |
| 197 |
if ($result === false) { |
| 198 |
throw PosixException::createFromPhpError(); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
/** |
| 204 |
* Set the effective group ID of the current process. This is a |
| 205 |
* privileged function and needs appropriate privileges (usually |
| 206 |
* root) on the system to be able to perform this function. |
| 207 |
* |
| 208 |
* @param int $gid The group id. |
| 209 |
* @throws PosixException |
| 210 |
* |
| 211 |
*/ |
| 212 |
function posix_setegid(int $gid): void |
| 213 |
{ |
| 214 |
error_clear_last(); |
| 215 |
$result = \posix_setegid($gid); |
| 216 |
if ($result === false) { |
| 217 |
throw PosixException::createFromPhpError(); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Set the effective user ID of the current process. This is a privileged |
| 224 |
* function and needs appropriate privileges (usually root) on |
| 225 |
* the system to be able to perform this function. |
| 226 |
* |
| 227 |
* @param int $uid The user id. |
| 228 |
* @throws PosixException |
| 229 |
* |
| 230 |
*/ |
| 231 |
function posix_seteuid(int $uid): void |
| 232 |
{ |
| 233 |
error_clear_last(); |
| 234 |
$result = \posix_seteuid($uid); |
| 235 |
if ($result === false) { |
| 236 |
throw PosixException::createFromPhpError(); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
/** |
| 242 |
* Set the real group ID of the current process. This is a |
| 243 |
* privileged function and needs appropriate privileges (usually |
| 244 |
* root) on the system to be able to perform this function. The |
| 245 |
* appropriate order of function calls is |
| 246 |
* posix_setgid first, |
| 247 |
* posix_setuid last. |
| 248 |
* |
| 249 |
* @param int $gid The group id. |
| 250 |
* @throws PosixException |
| 251 |
* |
| 252 |
*/ |
| 253 |
function posix_setgid(int $gid): void |
| 254 |
{ |
| 255 |
error_clear_last(); |
| 256 |
$result = \posix_setgid($gid); |
| 257 |
if ($result === false) { |
| 258 |
throw PosixException::createFromPhpError(); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* Let the process pid join the process group |
| 265 |
* pgid. |
| 266 |
* |
| 267 |
* @param int $pid The process id. |
| 268 |
* @param int $pgid The process group id. |
| 269 |
* @throws PosixException |
| 270 |
* |
| 271 |
*/ |
| 272 |
function posix_setpgid(int $pid, int $pgid): void |
| 273 |
{ |
| 274 |
error_clear_last(); |
| 275 |
$result = \posix_setpgid($pid, $pgid); |
| 276 |
if ($result === false) { |
| 277 |
throw PosixException::createFromPhpError(); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
/** |
| 283 |
* posix_setrlimit sets the soft and hard limits for a |
| 284 |
* given system resource. |
| 285 |
* |
| 286 |
* |
| 287 |
* Each resource has an associated soft and hard limit. The soft |
| 288 |
* limit is the value that the kernel enforces for the corresponding |
| 289 |
* resource. The hard limit acts as a ceiling for the soft limit. |
| 290 |
* An unprivileged process may only set its soft limit to a value |
| 291 |
* from 0 to the hard limit, and irreversibly lower its hard limit. |
| 292 |
* |
| 293 |
* @param int $resource The |
| 294 |
* resource limit constant |
| 295 |
* corresponding to the limit that is being set. |
| 296 |
* @param int $softlimit The soft limit, in whatever unit the resource limit requires, or |
| 297 |
* POSIX_RLIMIT_INFINITY. |
| 298 |
* @param int $hardlimit The hard limit, in whatever unit the resource limit requires, or |
| 299 |
* POSIX_RLIMIT_INFINITY. |
| 300 |
* @throws PosixException |
| 301 |
* |
| 302 |
*/ |
| 303 |
function posix_setrlimit(int $resource, int $softlimit, int $hardlimit): void |
| 304 |
{ |
| 305 |
error_clear_last(); |
| 306 |
$result = \posix_setrlimit($resource, $softlimit, $hardlimit); |
| 307 |
if ($result === false) { |
| 308 |
throw PosixException::createFromPhpError(); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
/** |
| 314 |
* Set the real user ID of the current process. This is a privileged |
| 315 |
* function that needs appropriate privileges (usually root) on |
| 316 |
* the system to be able to perform this function. |
| 317 |
* |
| 318 |
* @param int $uid The user id. |
| 319 |
* @throws PosixException |
| 320 |
* |
| 321 |
*/ |
| 322 |
function posix_setuid(int $uid): void |
| 323 |
{ |
| 324 |
error_clear_last(); |
| 325 |
$result = \posix_setuid($uid); |
| 326 |
if ($result === false) { |
| 327 |
throw PosixException::createFromPhpError(); |
| 328 |
} |
| 329 |
} |
| 330 |
|