-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerSideRender.php
294 lines (278 loc) · 10 KB
/
ServerSideRender.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
class ServerSideElementBase{
protected array $attributes = array();
function setId($id){
$this->attributes['id'] = $id;
return $this;
}
function setAttribute(array $attribute){
$this->attributes = array_merge($this->attributes,$attribute);
return $this;
}
}
class ServerSideElement extends ServerSideElementBase{
protected string $HTMLTag;
function HTMLTag(string $tag):ServerSideElement{
$this->HTMLTag = $tag;
return $this;
}
function build():void{
echo "<{$this->HTMLTag}";
foreach($this->attributes as $attribute => $value){
echo htmlspecialchars($attribute).'="'.htmlspecialchars($value).'" ';
}
echo "></{$this->HTMLTag}";
}
}
class ServerSideInputType{
static public string $TypeText = "Text";
static public string $TypePassword = "Password";
static public string $TypeEmail = "Email";
static public string $TypeSubmit = "Submit";
}
class ServerSideInputText extends ServerSideElementBase{
private string $type = "Text";
private bool $isRequired = false;
private bool $isReadonly = false;
function setName(string $name):ServerSideInputText{
$this->attributes['name'] = $name;
return $this;
}
function setType(string $type):ServerSideInputText{
$this->type = $type;
return $this;
}
function isRequired():ServerSideInputText{
$this->isRequired = true;
return $this;
}
function readonly(bool $isReadonly = true):ServerSideInputText{
$this->isReadonly = $isReadonly;
return $this;
}
function getName():string{
return $this->attributes['name'];
}
function build():void{
echo '<input type="'.$this->type.'" ';
if(isset($this->attributes['name'])){
if(isset($_POST[$this->attributes['name']]) && !isset($this->attributes['value'])){
$this->attributes['value'] = $_POST[$this->attributes['name']];
}
}
foreach($this->attributes as $attribute => $value){
echo htmlspecialchars($attribute).'="'.htmlspecialchars($value).'" ';
}
if($this->isRequired) echo "required ";
if($this->isReadonly) echo "readonly ";
echo '>';
}
}
enum EventConst{
case onChange;
case onSubmit;
case onInit;
}
class EventTargetPair{
public EventConst $event;
public string $target;
}
class ServerSideEventHandler{
private array $eventTargetHandler = array();
private array $previousTargetValues = array();
private array $session;
public function __construct(array &$session) {
$this->session = &$session;
return $this;
}
/**
* Bind event handler
*/
public function bind(ServerSideInputText $target, EventConst $eventConst, $handler):ServerSideEventHandler{
$eventTargetPair = new EventTargetPair();
$eventTargetPair->target = $target->getName();
$eventTargetPair->event = $eventConst;
$this->eventTargetHandler[serialize($eventTargetPair)] = $handler;
switch ($eventConst) {
case EventConst::onChange:
$target->setAttribute(["onChange" => "this.form.submit()"]);
break;
default:
break;
}
return $this;
}
private function onInit(EventTargetPair $eventTargetPair,string $target) : void {
if($eventTargetPair->event != EventConst::onInit){
return;
}
$eventTargetPair = new EventTargetPair();
$eventTargetPair->target = $target;
$eventTargetPair->event = EventConst::onInit;
if(!isset($this->eventTargetHandler[serialize($eventTargetPair)])){
return;
}
$this->eventTargetHandler[serialize($eventTargetPair)]();
unset($this->previousTargetValues[$target]);
}
private function onChange(string $target,string $value,array $previous):void{
if(!isset($previous[$target])){
return;
}
if($previous[$target] == $value){
return;
}
$eventTargetPair = new EventTargetPair();
$eventTargetPair->target = $target;
$eventTargetPair->event = EventConst::onChange;
if(!isset($this->eventTargetHandler[serialize($eventTargetPair)])){
return;
}
$this->eventTargetHandler[serialize($eventTargetPair)]();
}
private function onSubmit(string $target,string $value) : void {
$eventTargetPair = new EventTargetPair();
$eventTargetPair->target = $target;
$eventTargetPair->event = EventConst::onSubmit;
if(!isset($this->eventTargetHandler[serialize($eventTargetPair)])){
return;
}
$this->eventTargetHandler[serialize($eventTargetPair)]($value);
}
/**
* Start event handler
*/
public function start():void{
$current = $_POST;
if(isset($this->session['ServerSideEventHandler'])){
$this->previousTargetValues = json_decode($this->session['ServerSideEventHandler'],true);
foreach($current as $target=>$value){
$this->onChange($target,$value,$this->previousTargetValues);
$this->onSubmit($target,$value);
$this->previousTargetValues[$target] = $value;
}
}else{
foreach($this->eventTargetHandler as $eventTargetPair=>$handler){
$eventTargetPair = unserialize($eventTargetPair,["allowed_classes"=>["EventTargetPair"]]);
$target = $eventTargetPair->target;
$this->previousTargetValues[$target] = "";
$this->onInit($eventTargetPair,$target);
}
}
$this->session['ServerSideEventHandler'] = json_encode($this->previousTargetValues);
}
/**
* Clear all value form current form
*/
public static function clearValue():void{
$previous = json_decode($_SESSION['ServerSideEventHandler'],true);
foreach($previous as $key){
unset($_POST[$key]);
}
}
}
enum PageTypeConst{
case Page;
case Service;
}
class PageTypePair{
public string $page;
public PageTypeConst $type;
public $restrictionFunction;
public function __construct(string $page, PageTypeConst $type, $restrictionFunction) {
$this->page = $page;
$this->type = $type;
$this->restrictionFunction = $restrictionFunction;
}
}
interface PageRenderExceptionBase{};
class PageRenderRestrictionException extends Exception implements PageRenderExceptionBase{};
class PageRender{
public string $pageFolder;
private array $endpointTargetPair;
public $body;
public $header;
public string $title = "Kamijaga Account";
public function __construct(string $pageFolder = "/page/") {
$this->header = function(){?>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<?php };
$this->body = function(){?>
<body>
</body>
<?php };
$this->pageFolder = $pageFolder;
$this->endpointTargetPair = array();
return $this;
}
public function bind(array $endpoints,string $target, PageTypeConst $type,$restrictionFunction){
$pageControllerPair = new PageTypePair($target,$type,$restrictionFunction);
foreach($endpoints as $endpoint){
$this->endpointTargetPair[$endpoint] = $pageControllerPair;
}
}
public function start(){
$endpoint = strtolower(explode("?",$_SERVER["REQUEST_URI"])[0]);
if(isset($this->endpointTargetPair[$endpoint])){
$pageTypePair = $this->endpointTargetPair[$endpoint];
if($pageTypePair->type == PageTypeConst::Page){
try{
if($pageTypePair->restrictionFunction != null){
if(!($pageTypePair->restrictionFunction)()) throw new PageRenderRestrictionException("Restricted");
}
$body = function()use($endpoint){
require_once $_SERVER["DOCUMENT_ROOT"].$this->pageFolder.$this->endpointTargetPair[$endpoint]->page;
};
$this->html($body,$this->header);
}
catch(PageRenderRestrictionException $pex){
http_response_code(401);
}
catch(Throwable $ex){
http_response_code(500);
throw new LogException("Internal Error",LogException::$MODE_LOG_ERROR,$ex);
}
}else if($pageTypePair->type == PageTypeConst::Service){
try{
if($pageTypePair->restrictionFunction != null){
if(!($pageTypePair->restrictionFunction)()) throw new PageRenderRestrictionException("Restricted");
}
$body = function()use($endpoint){
require_once $_SERVER["DOCUMENT_ROOT"].$this->pageFolder.$this->endpointTargetPair[$endpoint]->page;
};
$this->service($body);
}
catch(PageRenderRestrictionException $pex){
http_response_code(401);
}
catch(Throwable $ex){
http_response_code(500);
throw new LogException("Internal Error",LogException::$MODE_LOG_ERROR,$ex);
}
}
}else{
http_response_code(404);
}
}
private function html($body,$header){?>
<!DOCTYPE html>
<html lang="en">
<?php $header()?>
<?php $body()?>
</html>
<?php }
private function service($body){
$body();
}
static public function Redirect($url){
?>
<script>
window.location.href = '<?php echo $url;?>';
</script>
<?php
}
}
?>