1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Ajax model |
7
|
|
|
* |
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
19
|
|
|
* |
20
|
|
|
* @author Glynn Quelch <[email protected]> |
21
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
22
|
|
|
* @package PinkCrab\Ajax |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace PinkCrab\Ajax; |
26
|
|
|
|
27
|
|
|
use Psr\Http\Message\ResponseInterface; |
28
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
29
|
|
|
use PinkCrab\Ajax\Dispatcher\Response_Factory; |
30
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
31
|
|
|
|
32
|
|
|
abstract class Ajax { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Define the action to call. |
36
|
|
|
* |
37
|
|
|
* @var string|null |
38
|
|
|
* @required |
39
|
|
|
*/ |
40
|
|
|
protected ?string $action = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The ajax calls nonce handle. |
44
|
|
|
* |
45
|
|
|
* @var string|null |
46
|
|
|
*/ |
47
|
|
|
protected ?string $nonce_handle = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The field name/id for the nonce field. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected string $nonce_field = 'nonce'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Should the ajax call be registered if the user is logged in. |
58
|
|
|
* |
59
|
|
|
* @var boolean |
60
|
|
|
*/ |
61
|
|
|
protected bool $logged_in = true; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Should the ajax call be registered if the user is not logged in |
65
|
|
|
* non_priv |
66
|
|
|
* |
67
|
|
|
* @var boolean |
68
|
|
|
*/ |
69
|
|
|
protected $logged_out = true; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The callback |
73
|
|
|
* |
74
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
75
|
|
|
* @param \PinkCrab\Ajax\Dispatcher\Response_Factory $response_factory |
76
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
77
|
|
|
*/ |
78
|
|
|
abstract public function callback( |
79
|
|
|
ServerRequestInterface $request, |
80
|
|
|
Response_Factory $response_factory |
81
|
|
|
): ResponseInterface; |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get define the action to call. |
86
|
|
|
* |
87
|
|
|
* @return string|null |
88
|
|
|
*/ |
89
|
|
|
public function get_action(): ?string { |
90
|
|
|
return $this->action; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the ajax calls nonce handle. |
95
|
|
|
* |
96
|
|
|
* @return string|null |
97
|
|
|
*/ |
98
|
|
|
public function get_nonce_handle(): ?string { |
99
|
|
|
return $this->nonce_handle; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the field name/id for the nonce field. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function get_nonce_field(): string { |
108
|
|
|
return $this->nonce_field; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get should the ajax call be registered if the user is logged in. |
113
|
|
|
* |
114
|
|
|
* @return boolean |
115
|
|
|
*/ |
116
|
|
|
public function get_logged_in(): bool { |
117
|
|
|
return $this->logged_in; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get non_priv |
122
|
|
|
* |
123
|
|
|
* @return boolean |
124
|
|
|
*/ |
125
|
|
|
public function get_logged_out(): bool { |
126
|
|
|
return $this->logged_out; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Checks if the action is defined. |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function has_valid_action(): bool { |
135
|
|
|
return is_string( $this->get_action() ) |
136
|
|
|
&& \mb_strlen( $this->get_action() ) > 0; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Checks if this Ajax call uses a nonce. |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function has_nonce(): bool { |
145
|
|
|
return ! is_null( $this->get_nonce_handle() ); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|