Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/worker/src/MWP/Worker
File: Request.php
<?php
[0] Fix | Delete
/*
[1] Fix | Delete
* This file is part of the ManageWP Worker plugin.
[2] Fix | Delete
*
[3] Fix | Delete
* (c) ManageWP LLC <contact@managewp.com>
[4] Fix | Delete
*
[5] Fix | Delete
* For the full copyright and license information, please view the LICENSE
[6] Fix | Delete
* file that was distributed with this source code.
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Class MWP_Worker_Request
[11] Fix | Delete
*/
[12] Fix | Delete
class MWP_Worker_Request
[13] Fix | Delete
{
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Header that contains the name of the action to execute.
[17] Fix | Delete
* Must be compliant with {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616}
[18] Fix | Delete
*
[19] Fix | Delete
* @var string
[20] Fix | Delete
*/
[21] Fix | Delete
protected $actionHeaderName = 'MWP-Action';
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Header that contains the ID of the action to execute.
[25] Fix | Delete
* Must be compliant with {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616}
[26] Fix | Delete
*
[27] Fix | Delete
* @var string
[28] Fix | Delete
*/
[29] Fix | Delete
protected $messageIdHeaderName = 'MWP-Message-ID';
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Header that contains message signature.
[33] Fix | Delete
* Must be compliant with {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616}
[34] Fix | Delete
*
[35] Fix | Delete
* @var string
[36] Fix | Delete
*/
[37] Fix | Delete
protected $signatureHeaderName = 'MWP-Signature';
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Header that contains the name of the key used for signing.
[41] Fix | Delete
* Must be compliant with {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616}
[42] Fix | Delete
*
[43] Fix | Delete
* @var string
[44] Fix | Delete
*/
[45] Fix | Delete
protected $keyNameHeaderName = 'MWP-Key-Name';
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* Header that contains the message signed by the corresponding service.
[49] Fix | Delete
* Must be compliant with {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616}
[50] Fix | Delete
*
[51] Fix | Delete
* @var string
[52] Fix | Delete
*/
[53] Fix | Delete
protected $serviceSignatureHeaderName = 'MWP-Service-Signature';
[54] Fix | Delete
[55] Fix | Delete
protected $signatureNoHostHeaderName = 'MWP-Signature-G';
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* Header that contains the communication key.
[59] Fix | Delete
* Must be compliant with {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616}
[60] Fix | Delete
*
[61] Fix | Delete
* @var string
[62] Fix | Delete
*/
[63] Fix | Delete
protected $communicationKeyHeaderName = 'MWP-Communication-Key';
[64] Fix | Delete
[65] Fix | Delete
protected $siteIdHeaderName = 'MWP-Site-Id';
[66] Fix | Delete
[67] Fix | Delete
protected $protocolVersionHeaderName = 'MWP-Protocol';
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* @var bool
[71] Fix | Delete
*/
[72] Fix | Delete
private $initialized = false;
[73] Fix | Delete
[74] Fix | Delete
/**
[75] Fix | Delete
* The GET parameters.
[76] Fix | Delete
*
[77] Fix | Delete
* @var array
[78] Fix | Delete
*/
[79] Fix | Delete
public $query;
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* The POST parameters.
[83] Fix | Delete
*
[84] Fix | Delete
* @var array
[85] Fix | Delete
*/
[86] Fix | Delete
public $request;
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* The request attributes.
[90] Fix | Delete
*
[91] Fix | Delete
* @var array
[92] Fix | Delete
*/
[93] Fix | Delete
public $attributes;
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* The COOKIE parameters.
[97] Fix | Delete
*
[98] Fix | Delete
* @var array
[99] Fix | Delete
*/
[100] Fix | Delete
public $cookies;
[101] Fix | Delete
[102] Fix | Delete
/**
[103] Fix | Delete
* The FILES parameters.
[104] Fix | Delete
*
[105] Fix | Delete
* @var array
[106] Fix | Delete
*/
[107] Fix | Delete
public $files;
[108] Fix | Delete
[109] Fix | Delete
/**
[110] Fix | Delete
* The SERVER parameters.
[111] Fix | Delete
*
[112] Fix | Delete
* @var array
[113] Fix | Delete
*/
[114] Fix | Delete
public $server;
[115] Fix | Delete
[116] Fix | Delete
/**
[117] Fix | Delete
* The raw request body data.
[118] Fix | Delete
*
[119] Fix | Delete
* @var string
[120] Fix | Delete
*/
[121] Fix | Delete
private $content;
[122] Fix | Delete
[123] Fix | Delete
private $method;
[124] Fix | Delete
[125] Fix | Delete
/**
[126] Fix | Delete
* @param array $query The GET parameters.
[127] Fix | Delete
* @param array $request The POST parameters.
[128] Fix | Delete
* @param array $attributes The request attributes.
[129] Fix | Delete
* @param array $cookies The COOKIE parameters.
[130] Fix | Delete
* @param array $files The FILES parameters.
[131] Fix | Delete
* @param array $server The SERVER parameters.
[132] Fix | Delete
* @param null|string $content The raw request body data. If null, it will be lazy-loaded.
[133] Fix | Delete
*/
[134] Fix | Delete
public function __construct($query = array(), $request = array(), $attributes = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
[135] Fix | Delete
{
[136] Fix | Delete
$this->query = $query;
[137] Fix | Delete
$this->request = $request;
[138] Fix | Delete
$this->attributes = $attributes;
[139] Fix | Delete
$this->cookies = $cookies;
[140] Fix | Delete
$this->files = $files;
[141] Fix | Delete
$this->server = $server;
[142] Fix | Delete
$this->content = $content;
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
/**
[146] Fix | Delete
* Gets the request method.
[147] Fix | Delete
*
[148] Fix | Delete
* The method is always an uppercased string.
[149] Fix | Delete
*
[150] Fix | Delete
* @return string The request method
[151] Fix | Delete
*/
[152] Fix | Delete
public function getMethod()
[153] Fix | Delete
{
[154] Fix | Delete
if (null === $this->method) {
[155] Fix | Delete
$this->method = isset($this->server['REQUEST_METHOD']) ? strtoupper($this->server['REQUEST_METHOD']) : 'GET';
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
return $this->method;
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
/**
[162] Fix | Delete
* Sets the request method.
[163] Fix | Delete
*
[164] Fix | Delete
* @param string $method
[165] Fix | Delete
*/
[166] Fix | Delete
public function setMethod($method)
[167] Fix | Delete
{
[168] Fix | Delete
$this->method = null;
[169] Fix | Delete
$this->server['REQUEST_METHOD'] = $method;
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* MWP_Worker factory.
[174] Fix | Delete
*
[175] Fix | Delete
* @return MWP_Worker_Request
[176] Fix | Delete
*/
[177] Fix | Delete
public static function createFromGlobals()
[178] Fix | Delete
{
[179] Fix | Delete
$request = new self($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
[180] Fix | Delete
[181] Fix | Delete
return $request;
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
/**
[185] Fix | Delete
* @throws RuntimeException If the request is already initialized.
[186] Fix | Delete
*/
[187] Fix | Delete
public function initialize()
[188] Fix | Delete
{
[189] Fix | Delete
if ($this->initialized) {
[190] Fix | Delete
throw new RuntimeException('Request is already initialized.');
[191] Fix | Delete
}
[192] Fix | Delete
$this->initialized = true;
[193] Fix | Delete
[194] Fix | Delete
$this->attributes['action'] = $this->getHeader($this->actionHeaderName);
[195] Fix | Delete
$this->attributes['id'] = $this->getHeader($this->messageIdHeaderName);
[196] Fix | Delete
$this->attributes['signature'] = base64_decode($this->getHeader($this->signatureHeaderName));
[197] Fix | Delete
$this->attributes['key_name'] = $this->getHeader($this->keyNameHeaderName);
[198] Fix | Delete
$this->attributes['service_signature'] = $this->getHeader($this->serviceSignatureHeaderName);
[199] Fix | Delete
$this->attributes['no_host_signature'] = $this->getHeader($this->signatureNoHostHeaderName);
[200] Fix | Delete
$this->attributes['communication_key'] = $this->getHeader($this->communicationKeyHeaderName);
[201] Fix | Delete
$this->attributes['site_id'] = $this->getHeader($this->siteIdHeaderName);
[202] Fix | Delete
$this->attributes['data'] = null;
[203] Fix | Delete
$this->attributes['params'] = null;
[204] Fix | Delete
$this->attributes['setting'] = null;
[205] Fix | Delete
$this->attributes['user'] = null;
[206] Fix | Delete
$this->attributes['authenticated'] = false;
[207] Fix | Delete
$this->attributes['protocol'] = (int)$this->getHeader($this->protocolVersionHeaderName);
[208] Fix | Delete
[209] Fix | Delete
if (!empty($this->attributes['service_signature'])) {
[210] Fix | Delete
$this->attributes['service_signature'] = base64_decode($this->attributes['service_signature']);
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
if (!empty($this->attributes['no_host_signature'])) {
[214] Fix | Delete
$this->attributes['no_host_signature'] = base64_decode($this->attributes['no_host_signature']);
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
// Do we have {"params":{...}} inside of body?
[218] Fix | Delete
if ($this->isMasterRequest() && is_array($data = json_decode($this->getContent(), true)) && array_key_exists('params', $data)) {
[219] Fix | Delete
$this->attributes['data'] = $data;
[220] Fix | Delete
$this->attributes['params'] = $data['params'];
[221] Fix | Delete
$this->attributes['setting'] = array_key_exists('setting', $data) ? $data['setting'] : null;
[222] Fix | Delete
$this->attributes['user'] = (isset($data['params']['username']) && is_scalar($data['params']['username'])) ? $data['params']['username'] : null;
[223] Fix | Delete
}
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
/**
[227] Fix | Delete
* @return bool
[228] Fix | Delete
*/
[229] Fix | Delete
public function isInitialized()
[230] Fix | Delete
{
[231] Fix | Delete
return $this->initialized;
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
/**
[235] Fix | Delete
* @param string $actionHeaderName
[236] Fix | Delete
*/
[237] Fix | Delete
public function setActionHeaderName($actionHeaderName)
[238] Fix | Delete
{
[239] Fix | Delete
$this->actionHeaderName = $actionHeaderName;
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
/**
[243] Fix | Delete
* @param string $signatureHeaderName
[244] Fix | Delete
*/
[245] Fix | Delete
public function setSignatureHeaderName($signatureHeaderName)
[246] Fix | Delete
{
[247] Fix | Delete
$this->signatureHeaderName = $signatureHeaderName;
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
/**
[251] Fix | Delete
* @param bool $asResource If true, a resource will be returned
[252] Fix | Delete
*
[253] Fix | Delete
* @return resource|string The request body content or a resource to read the body stream.
[254] Fix | Delete
* @throws RuntimeException If attempting to call the method again after getting it as a resource previously.
[255] Fix | Delete
*/
[256] Fix | Delete
public function getContent($asResource = false)
[257] Fix | Delete
{
[258] Fix | Delete
if (false === $this->content || (true === $asResource && null !== $this->content)) {
[259] Fix | Delete
throw new RuntimeException('getContent() can only be called once when using the resource return type.');
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
if (true === $asResource) {
[263] Fix | Delete
$this->content = false;
[264] Fix | Delete
[265] Fix | Delete
return fopen('php://input', 'rb');
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
if (null === $this->content) {
[269] Fix | Delete
$this->content = file_get_contents('php://input');
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
return $this->content;
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
/**
[276] Fix | Delete
* @return bool Whether the current request is sent by the master script.
[277] Fix | Delete
*/
[278] Fix | Delete
public function isMasterRequest()
[279] Fix | Delete
{
[280] Fix | Delete
if ($this->getMethod() !== 'POST') {
[281] Fix | Delete
return false;
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
$masterHeader = $this->getHeader($this->actionHeaderName);
[285] Fix | Delete
if ($masterHeader === null) {
[286] Fix | Delete
return false;
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
return true;
[290] Fix | Delete
}
[291] Fix | Delete
[292] Fix | Delete
/**
[293] Fix | Delete
* @return null|string
[294] Fix | Delete
*/
[295] Fix | Delete
public function getAction()
[296] Fix | Delete
{
[297] Fix | Delete
return $this->attributes['action'];
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
/**
[301] Fix | Delete
* @return null|string
[302] Fix | Delete
*/
[303] Fix | Delete
public function getSignature()
[304] Fix | Delete
{
[305] Fix | Delete
return $this->attributes['signature'];
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
/**
[309] Fix | Delete
* @return null|string
[310] Fix | Delete
*/
[311] Fix | Delete
public function getKeyName()
[312] Fix | Delete
{
[313] Fix | Delete
return $this->attributes['key_name'];
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
/**
[317] Fix | Delete
* @return null|string
[318] Fix | Delete
*/
[319] Fix | Delete
public function getServiceSignature()
[320] Fix | Delete
{
[321] Fix | Delete
return $this->attributes['service_signature'];
[322] Fix | Delete
}
[323] Fix | Delete
[324] Fix | Delete
/**
[325] Fix | Delete
* @return null|string
[326] Fix | Delete
*/
[327] Fix | Delete
public function getNoHostSignature()
[328] Fix | Delete
{
[329] Fix | Delete
return $this->attributes['no_host_signature'];
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
/**
[333] Fix | Delete
* @return string
[334] Fix | Delete
*/
[335] Fix | Delete
public function getCommunicationKey()
[336] Fix | Delete
{
[337] Fix | Delete
return !empty($this->attributes['communication_key']) ? $this->attributes['communication_key'] : '';
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
/**
[341] Fix | Delete
* @return string
[342] Fix | Delete
*/
[343] Fix | Delete
public function getSiteId()
[344] Fix | Delete
{
[345] Fix | Delete
return !empty($this->attributes['site_id']) ? $this->attributes['site_id'] : '';
[346] Fix | Delete
}
[347] Fix | Delete
[348] Fix | Delete
/**
[349] Fix | Delete
* @return null|string
[350] Fix | Delete
*/
[351] Fix | Delete
public function getUsername()
[352] Fix | Delete
{
[353] Fix | Delete
return $this->attributes['user'];
[354] Fix | Delete
}
[355] Fix | Delete
[356] Fix | Delete
public function getNonce()
[357] Fix | Delete
{
[358] Fix | Delete
return $this->attributes['id'];
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
public function getParams()
[362] Fix | Delete
{
[363] Fix | Delete
return $this->attributes['params'];
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
public function getData()
[367] Fix | Delete
{
[368] Fix | Delete
return $this->attributes['data'];
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
public function getSetting()
[372] Fix | Delete
{
[373] Fix | Delete
return $this->attributes['setting'];
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
/**
[377] Fix | Delete
* @param string $header Header name.
[378] Fix | Delete
*
[379] Fix | Delete
* @return string|null Header content, or null if it doesn't exist.
[380] Fix | Delete
*/
[381] Fix | Delete
public function getHeader($header)
[382] Fix | Delete
{
[383] Fix | Delete
$header = 'HTTP_'.strtoupper(str_replace('-', '_', $header));
[384] Fix | Delete
if (isset($this->server[$header])) {
[385] Fix | Delete
return $this->server[$header];
[386] Fix | Delete
}
[387] Fix | Delete
[388] Fix | Delete
return null;
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
/**
[392] Fix | Delete
* @return bool
[393] Fix | Delete
*/
[394] Fix | Delete
public function isAuthenticated()
[395] Fix | Delete
{
[396] Fix | Delete
return $this->attributes['authenticated'];
[397] Fix | Delete
}
[398] Fix | Delete
[399] Fix | Delete
public function setAuthenticated($isAuthenticated)
[400] Fix | Delete
{
[401] Fix | Delete
$this->attributes['authenticated'] = $isAuthenticated;
[402] Fix | Delete
}
[403] Fix | Delete
[404] Fix | Delete
/**
[405] Fix | Delete
* @return int
[406] Fix | Delete
*/
[407] Fix | Delete
public function getProtocol()
[408] Fix | Delete
{
[409] Fix | Delete
return $this->attributes['protocol'];
[410] Fix | Delete
}
[411] Fix | Delete
}
[412] Fix | Delete
[413] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function