1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Viewer; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
trait HelperTrait{ |
8
|
|
|
use CheckTrait, JScriptTrait; |
9
|
|
|
|
10
|
|
|
protected function getOB(string $require, array $data = []): string |
11
|
|
|
{ |
12
|
|
|
foreach($data as $variable => $_){ |
13
|
|
|
$$variable = $_; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
$_ = (array_key_exists('_',$data)) ? $data['_'] : null; |
17
|
|
|
|
18
|
|
|
if(!file_exists($require)){ |
19
|
|
|
$require = basename($require); |
20
|
|
|
throw new Exception("Importation file does not exist: {$require} ."); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->initData(); |
24
|
|
|
|
25
|
|
|
$_SESSION['data'] = (!empty($data)) ? array_merge($data,$_SESSION['data']) : $_SESSION['data']; |
26
|
|
|
|
27
|
|
|
ob_start(); |
28
|
|
|
require($require); |
29
|
|
|
$response = ob_get_contents(); |
30
|
|
|
ob_end_clean(); |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
return $this->treatHTML($response); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function treatHTML(string $html): string |
38
|
|
|
{ |
39
|
|
|
$arrayHtml = explode(PHP_EOL,$html); |
40
|
|
|
$html = []; |
41
|
|
|
|
42
|
|
|
$inScript = false; |
43
|
|
|
$inComment = false; |
44
|
|
|
|
45
|
|
|
foreach($arrayHtml as $index => $value){ |
46
|
|
|
$inScript = $this->checkInScript($inScript, $value); |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
if($inScript){ |
50
|
|
|
$inComment = $this->checkCommentInScript($inComment, $value); |
51
|
|
|
|
52
|
|
|
if(!$this->checkScriptNeed($inComment, $value)){ |
53
|
|
|
continue; |
54
|
|
|
}else{ |
55
|
|
|
$value = $this->treatScript($value); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$html[$index] = ltrim($value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return implode('',$html); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function initData() |
66
|
|
|
{ |
67
|
|
|
$_SESSION['data'] = (empty($_SESSION['data'])) ? null : $_SESSION['data']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function getVars(string $buffer,string $prefix = null, ?array $values = null): string |
71
|
|
|
{ |
72
|
|
|
$this->initData(); |
73
|
|
|
|
74
|
|
|
$vars = (is_null($values)) ? $_SESSION['data'] : $values; |
75
|
|
|
|
76
|
|
|
return (is_null($vars)) ? $buffer : $this->replace_vars($buffer, $vars, $prefix); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function replace_vars($buffer, $vars, $prefix): string |
80
|
|
|
{ |
81
|
|
|
foreach ($vars as $key => $value) { |
82
|
|
|
switch(gettype($value)){ |
83
|
|
|
case 'array': |
84
|
|
|
$buffer = $this->replace_Array($buffer, $value, $prefix, $key); |
85
|
|
|
break; |
86
|
|
|
case 'object': |
87
|
|
|
$buffer = $this->replace_Object($buffer, $value, $prefix, $key); |
88
|
|
|
break; |
89
|
|
|
default: |
90
|
|
|
$buffer = $this->replace_value($buffer, $value, $prefix, $key); |
91
|
|
|
break; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $buffer; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function replace_value(string $buffer, $value, ?string $prefix, string $key): string |
99
|
|
|
{ |
100
|
|
|
if(gettype($value)!=='array' && gettype($value)!=='object'){ |
101
|
|
|
while(strstr($buffer,'{{ $'.$prefix.$key.' }}')){ |
102
|
|
|
$buffer = str_replace('{{ $'.$prefix.$key.' }}', htmlspecialchars($value) ,$buffer); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
return $buffer; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function replace_Object(string $buffer, object $obj, string $prefix, string $key): string |
109
|
|
|
{ |
110
|
|
|
foreach($obj->get_object_vars() as $field => $val){ |
111
|
|
|
|
112
|
|
|
$buffer = $this->replace_value($buffer, $val, $key.'.'.$field.'.' , $field); |
113
|
|
|
|
114
|
|
|
while(strstr($buffer,'{{ $'.$prefix.$key.'.'.$field.' }}')){ |
115
|
|
|
$buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val) ,$buffer); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
return $buffer; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function replace_Array(string $buffer, array $array, ?string $prefix = '', ?string $key = ''): string |
122
|
|
|
{ |
123
|
|
|
foreach($array as $field => $val){ |
124
|
|
|
$buffer = $this->replace_value($buffer, $val, $key.'.'.$field.'.' , $field); |
125
|
|
|
|
126
|
|
|
while(strstr($buffer,'{{ $'.$prefix.$key.'.'.$field.' }}')){ |
127
|
|
|
$buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val) ,$buffer); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
return $buffer; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function removeComments(string $buffer): string |
134
|
|
|
{ |
135
|
|
|
while(strstr($buffer,'<!--')){ |
136
|
|
|
$comment = substr( |
137
|
|
|
$buffer, |
138
|
|
|
strpos($buffer,'<!--'), |
139
|
|
|
strpos(strstr($buffer,'<!--'),'-->')+3 |
140
|
|
|
); |
141
|
|
|
$buffer = str_replace($comment,'',$buffer); |
142
|
|
|
} |
143
|
|
|
return $buffer; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
protected function saveData(): bool |
147
|
|
|
{ |
148
|
|
|
if(session_status() !== PHP_SESSION_ACTIVE){ |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
unset($_SESSION['data']); |
152
|
|
|
|
153
|
|
|
if(!empty($_SESSION['save'])){ |
154
|
|
|
foreach ($_SESSION['save'] as $key => $value) { |
155
|
|
|
$_SESSION['data'][$key] = $value; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
return true; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |