| 1 |
<?php |
| 2 |
|
| 3 |
namespace GeminiLabs\SiteReviews; |
| 4 |
|
| 5 |
use GeminiLabs\SiteReviews\Helpers\Arr; |
| 6 |
use GeminiLabs\SiteReviews\Helpers\Cast; |
| 7 |
use GeminiLabs\SiteReviews\Helpers\Str; |
| 8 |
use GeminiLabs\SiteReviews\Modules\Captcha; |
| 9 |
use GeminiLabs\SiteReviews\Modules\Encryption; |
| 10 |
|
| 11 |
class Request extends Arguments |
| 12 |
{ |
| 13 |
public function decrypt(string $key): string |
| 14 |
{ |
| 15 |
$value = $this->cast($key, 'string'); |
| 16 |
$decrypted = glsr(Encryption::class)->decrypt($value); |
| 17 |
return Cast::toString($decrypted); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* @param mixed $key |
| 22 |
* @param mixed $fallback |
| 23 |
* |
| 24 |
* @return mixed |
| 25 |
*/ |
| 26 |
public function get($key, $fallback = null) |
| 27 |
{ |
| 28 |
$value = Arr::get($this->getArrayCopy(), $key, null); |
| 29 |
if (is_null($fallback)) { |
| 30 |
return $value; |
| 31 |
} |
| 32 |
if (!Helper::isEmpty($value)) { |
| 33 |
return $value; |
| 34 |
} |
| 35 |
return Helper::runClosure($fallback); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @return static |
| 40 |
* |
| 41 |
* @todo support array values |
| 42 |
*/ |
| 43 |
public static function inputGet(): Request |
| 44 |
{ |
| 45 |
$values = []; |
| 46 |
if ($token = filter_input(\INPUT_GET, glsr()->prefix)) { |
| 47 |
$token = sanitize_text_field($token); |
| 48 |
$values = glsr(Encryption::class)->decryptRequest($token); |
| 49 |
} |
| 50 |
return new static($values); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @return static |
| 55 |
*/ |
| 56 |
public static function inputPost(): Request |
| 57 |
{ |
| 58 |
$action = Helper::filterInput('action'); |
| 59 |
$values = Helper::filterInputArray(glsr()->id); |
| 60 |
if (in_array($action, [glsr()->prefix.'admin_action', glsr()->prefix.'public_action'])) { |
| 61 |
$values['_ajax_request'] = true; |
| 62 |
} |
| 63 |
$requestAction = Helper::filterInput('_action', $values); |
| 64 |
if (in_array($requestAction, glsr(Captcha::class)->actions())) { |
| 65 |
$values['_captcha'] = glsr(Captcha::class)->token(); |
| 66 |
} |
| 67 |
return new static($values); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @param mixed $value |
| 72 |
*/ |
| 73 |
public function set(string $path, $value): void |
| 74 |
{ |
| 75 |
$storage = Arr::set($this->getArrayCopy(), $path, $value); |
| 76 |
$this->exchangeArray($storage); |
| 77 |
if (!$this->exists('form_signature') || 'form_signature' === $path) { |
| 78 |
return; |
| 79 |
} |
| 80 |
$values = $this->signedValues(); |
| 81 |
if (array_key_exists($path, $values)) { |
| 82 |
$values[$path] = $value; |
| 83 |
$storage['form_signature'] = glsr(Encryption::class)->encrypt(maybe_serialize($values)); |
| 84 |
$this->exchangeArray($storage); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* The signed values a form was rendered with. |
| 90 |
*/ |
| 91 |
public function signedValues(array $defaults = []): array |
| 92 |
{ |
| 93 |
$decrypted = $this->decrypt('form_signature'); |
| 94 |
$values = is_serialized($decrypted) |
| 95 |
? unserialize($decrypted, ['allowed_classes' => false]) |
| 96 |
: null; |
| 97 |
return wp_parse_args(is_array($values) ? $values : [], $defaults); |
| 98 |
} |
| 99 |
} |
| 100 |
|