PluginProbe
Site Reviews / trunk
Site Reviews vtrunk
8.3.1 8.3.0 8.2.2 8.2.1 8.2.0 8.1.0 8.0.13 8.0.12 8.0.11 trunk 1.2.2 2.17.1 3.5.4 4.7.0 5.25.1 6.11.8 7.0.10 7.0.11 7.0.12 7.0.13 7.0.14 7.0.15 7.0.16 7.0.17 7.0.18 All 54 releases
site-reviews / plugin / Request.php

Request.php in Site Reviews trunk, at plugin/Request.php

100 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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