-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.php
386 lines (386 loc) · 11.7 KB
/
class.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
define( 'DEFAULT_INDENT' , ' ' );
define( 'CHARSET' , 'UTF-8' );
define( 'UNCLOSING_TAG', '我会对啊我的互加块地方回答户外的会的全会带我去回答会饿疯会饿我会エrhギオフフィエフイフェウイfヘイウfジオフェジオfwelcome to yo-kosoジャパリパックpapapapd ');
define( 'SELF_CLOSING_TAG', 'bdS-5Lb-xHJ-s7G <-');
class htmltagger {
//declare
private $buffer = FALSE;
private $head = [];
private $body = [];
private $cli;
//disable redis as default
protected $redis = ['enable' => FALSE];
//setup redis
public function redis( $addr = 'localhost' , $port = 6379 , $exp = 300 ) {
$this->redis = ['enable' => TRUE , 'addr' => $addr , 'port' => $port , 'expire_time' => $exp];
return $this;
}
//set html <title>
public function setTitle( $title ) {
$this->head = array_merge_recursive( $this->head , array( 'title' => htmlspecialchars( $title )));
return $this;
}
//set html <head>
public function setHead( $head ) {
if ( is_array( $head )) {
$this->head = array_merge_recursive( $this->head , $head );
} elseif ( is_string( $head )) {
$this->head['__str'] = $head;
}
return $this;
}
//set html <body>
public function setBody() {
$body = func_get_args();
if ( is_array( $body )) {
$this->body = array_merge_recursive( $this->body , $body );
} elseif ( is_string( $body )) {
$this->body['__str'] = $body;
}
return $this;
}
//get htmlized <head>
public function getHeadHtml() {
$head = new head( $this->head );
return $head->__rtn();
}
//get htmlized <body>
public function getBodyHtml() {
$body = new body( $this->body );
return $body->__rtn();
}
//print html tags
public function prn() {
ob_start();
$this->buffer = TRUE;
$head_html = FALSE;
$body_html = FALSE;
$redis_enable = $this->redis['enable'];
if ( $redis_enable ) {
$redis = new Redis();
if ( !$redis->connect( $this->redis['addr'] , $this->redis['port'] )) {
exit( 'unable to connect to Redis.' );
}
$head_md5 = md5( serialize( $this->head ));
$body_md5 = md5( serialize( $this->body ));
$head_html = $redis->get( "htmltagger:${head_md5}" );
$body_html = $redis->get( "htmltagger:${body_md5}" );
}
echo "<!DOCTYPE html>" , PHP_EOL , "<html>" , PHP_EOL;
if ( !$head_html ) {
$head = new head( $this->head );
if ( $redis_enable ) {
$head_html = $head->__rtn();
$redis->set( "htmltagger:${head_md5}" , $head_html );
$redis->setTimeout( "htmltagger:${head_md5}" , $this->redis['expire_time'] );
}
$head->__prn();
} else {
echo $head_html;
}
//ob_flush();
if ( !$body_html ) {
$body = new body( $this->body );
if ( $redis_enable ) {
$body_html = $body->__rtn();
$redis->set( "htmltagger:${body_md5}" , $body_html );
$redis->setTimeout( "htmltagger:${body_md5}" , $this->redis['expire_time'] );
}
$body->__prn();
} else {
echo $body_html;
}
echo '</html>';
ob_flush();
$this->buffer = FALSE;
return;
}
//shrink the unused tags
protected function shrink( $member ) {
$member = $this->$member();
if ( !isset( $member )) {
return FALSE;
}
$content = $member['content'];
foreach ( $content as $head_name => $data ) {
if ( $data == '' ) {
unset( $content[$head_name] );
}
}
}
public static function errHandler( $errlevel , $errmsg , $errfile , $errline ) {
if ( $errlevel < 256 || $errlevel > 1024 && $errlevel != 16384 ) return FALSE;
$invisable = ( $errlevel > 256 );
$cli = ( php_sapi_name() == 'cli' );
if ( !$cli ) {
echo '<phpEZhtmltaggerErr';
if ( $invisable ) echo ' style="visibility:hidden;"';
echo '>';
}
$type = ( $invisable ) ? 'Error: ' : 'Fat Error: ';
echo PHP_EOL , $type , $errmsg , PHP_EOL;
if ( !$cli ) echo '</phpEZhtmltaggerErr>';
if ( $need_to_die = ( $errlevel = 256 )) {
ob_flush();
die();
}
}
public function __call( $type , $arguments ) {
$data = $arguments['0'];
$offset = $arguments['1'];
$dataType = is_string( $data ) ? 's' : 'a';
$inside = isset( $data['__in'] ) ? $data['__in'] : SELF_CLOSING_TAG;
//formating html tag
echo str_repeat( DEFAULT_INDENT , $offset );
//open a html tag
echo "<${type}";
//convert array setting-arguments to html tag's parameters @ADD 2017-04-27 if it's array
if ( $dataType == 'a' && $data != [] ) {
$return = $this->__exchange( $data );
//print all parameters
foreach ( $return as $config ) {
echo ' ' , $config;
}
}
//close prefix
if ( $inside == SELF_CLOSING_TAG ) {
echo '/>';
return;
} else {
//for <h1></h1> ( <**></**> ) and <br> ( <**> )
echo '>';
}
if ( $data == UNCLOSING_TAG ) {
return;
} else if ( $dataType == 's' ) {
if ( $data == UNCLOSING_TAG || $data == SELF_CLOSING_TAG ) return;
echo "${data}</${type}>";
return;
} else {
if ( is_array( $inside )) {
//we admire if there's only on array in the $inside. we use another array inside there to avoid
//if you want two tag that's uses one name.
if ( !isset( $inside[0] )) {
foreach ( $inside as $k => $s ) {
echo PHP_EOL;
$this->$k( $s , $offset + 1 );
}
} else { //if it comes' with more array.
foreach ( $inside as $i ) {
if ( is_string( $i )) {
echo $i;
} else {
$this->__do( $i , $offset + 1 );
}
}
}
echo PHP_EOL , str_repeat( DEFAULT_INDENT , $offset ) , "</${type}>";
return;
}
else if ( is_string( $inside )) {
echo "${inside}</${type}>";
return;
}
else if ( !is_null( $inside )) {
$nl = ( $this->cli ) ? '<br>' : PHP_EOL;
$msg = 'I don\'t know how to do with the data. Please check.' . $nl;
if ( $this->cli ) $msg .= '<phpEZhtmltaggerErrdata>' . PHP_EOL;
$msg .= var_dump( $inside );
if ( $this->cli ) $msg .= '</phpEZhtmltaggerErrdata>' . PHP_EOL;
trigger_error( $msg , E_USER_WARNING );
return;
}
}
}
public function __destruct() {
if ( $this->buffer ) ob_end_clean();
}
}
class head extends htmltagger {
private $buffer = FALSE;
protected function __exchange( $data ) {
//we don't need toch anything in the '__in' so just unset it if it's exist
if ( isset( $data['__in'] )) {
unset( $data['__in'] );
}
//prepare the array to return
$convert = array();
//Converting is just fucking easy. Cause html tag's just fucking easy to do.
if ( !$data == NULL ) {
foreach ( $data as $k => $s ) {
if ( !$s == '' ) {
//converting's like this:
// ['content'=>'what'] >> content="what"
// and we add spage before it or it'll not be able to understand by browser.
// then we return an array contains all we converted
$convert[$k] = "${k}=\"${s}\"";
}
}
}
//kick converted parameters back
return $convert;
}
public function __construct( $head ) {
if ( $head == [] ) {
echo '<--!empty head-->' , PHP_EOL;
return;
}
ob_start();
$this->buffer = TRUE;
echo DEFAULT_INDENT , '<head>' , PHP_EOL , DEFAULT_INDENT , DEFAULT_INDENT , '<meta charset="' , CHARSET , '">' , PHP_EOL;
foreach ( $head as $k => $s ) {
echo DEFAULT_INDENT , DEFAULT_INDENT;
$this->$k( $s );
echo PHP_EOL;
}
echo DEFAULT_INDENT , '</head>' , PHP_EOL;
}
public function __str($str) {
echo $str , DEFAULT_INDENT;
}
public function title( $title ) {
echo "<title>${title}</title>";
}
public function icon( $icon ) {
echo '<link rel="icon" href="' , $icon , '">';
}
public function script( $script ) {
$length = count( $script );
foreach ( $script as $k => $s ) {
echo '<script type="' , $s['type'] , '" src="' , $s['location'] , '"></script>';
if ( $k < ( $length - 1 )) {
echo PHP_EOL , DEFAULT_INDENT , DEFAULT_INDENT;
}
}
}
public function css( $css ) {
$length = count( $css );
foreach ( $css as $k => $s ) {
if ( is_string( $s )) {
echo '<link rel="stylesheet" href="' , $s , '">';
}
elseif ( is_array( $s )) {
echo '<link rel="stylesheet" ';
$return = $this->__exchange( $css[$k] );
foreach ( $return as $config ) {
echo ' ' , $config;
}
echo '>';
}
if ( $k < ( $length - 1 )) {
echo PHP_EOL , DEFAULT_INDENT , DEFAULT_INDENT;
}
}
}
public function style( $style ) {
echo '<style>' , PHP_EOL;
if ( is_array( $style )) {
foreach ( $style as $s ) {
echo DEFAULT_INDENT , DEFAULT_INDENT , DEFAULT_INDENT , $s , PHP_EOL;
}
} else if ( is_string( $style )) {
echo DEFAULT_INDENT , DEFAULT_INDENT , $style , PHP_EOL;
}
echo DEFAULT_INDENT , DEFAULT_INDENT , '</style>';
}
public function __prn() {
if ( $this->buffer ) ob_end_flush();
$this->buffer = FALSE;
return;
}
public function __rtn() {
$return = ob_get_contents();
return $return;
}
public function __call( $method , $parameters ){}
public function __destruct() {
if ( $this->buffer ) ob_end_clean();
}
}
class body extends htmltagger {
public $cli;
private $buffer = FALSE;
//when a new body object's cerating
public function __construct( $body ) {
//set custom error handler for user error
set_error_handler( array( 'htmltagger' , 'errHandler' ));
if ( $body == [] ) {
echo '<--!empty body-->' , PHP_EOL;
return;
}
$this->cli = ( is_null( $this->cli )) ? ( php_sapi_name() == 'cli' ) : $this->cli;
//if threr's ver0.0.2 array we reject tag it although it's just easy to make a another foreach.
// if you see this , you , you might be a friend that writes php code.
// you can change this code ( v ) to {foreach ( $body as $inner ) {...}} but anyway it will just not work.
if ( isset( $body[0][0] ))
trigger_error( 'You are feeding phpEZhtmltagger ver 0.0.1-0.0.2 arrays to phpEZhtmltagger 0.0.3. ' . PHP_EOL . 'This\' need some easy-array-changing-operation. ' , E_USER_ERROR );
ob_start();
//all tags need to be placed in <body> so we print it at FUCKING FIRST without anything above it.
$this->buffer = TRUE;
echo DEFAULT_INDENT , '<body>';
//open every array
foreach ( $body as $s ) {
$this->__do( $s , 2 );
}
echo PHP_EOL , DEFAULT_INDENT , '</body>' , PHP_EOL;
}
function __do( $s , $offset ) {
if ( is_array ( $s ) ) {
foreach ( $s as $k => $s ) {
echo PHP_EOL;
//every thing in <body> are tabing use offset just below
//and the first offset is 2 instead of 1 because <body> uses offset 1 and <html> uses 0
$this->$k( $s , $offset );
}
} elseif ( is_string( $s ) || is_int( $s ) ) {
echo $s;
} elseif( is_null($s) || $s === FALSE ) {
htmltagger::errHandler(512 , 'htmltagger::body::__do(): undefined variable:'.var_dump($s) , __FILE__ , __LINE__);
} else {
var_dump($s);
}
}
public function __prn() {
if ( $this->buffer ) ob_end_flush();
$this->buffer = FALSE;
return;
}
public function __rtn() {
$return = ob_get_contents();
return $return;
}
// __exchange htmltagging array to html tag's parameters
protected function __exchange( $data ) {
//we don't need toch anything in the '__in' so just unset it.
if ( isset( $data['__in'] )) {
unset( $data['__in'] );
}
//prepare the array to return
$convert = array();
//Converting is just fucking easy. html tag's just fucking easy.
if ( !$data == NULL ) {
foreach ( $data as $k => $s ) {
if ( !$s == '' ) {
//converting is like this:
// ['content'=>'what'] -> content="what"
// and we add spage before it or it'll not be able to understand by browser.
// then we put it in an array to return
$convert[$k] = "${k}=\"${s}\"";
}
}
}
return $convert;
}
public function __destruct() {
if ( $this->buffer ) ob_end_clean();
}
}
class toolBox {
public static function humanityMath( $size , $withdetail = FALSE ) {
if (is_int($size)) return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.['Bytes','KB','MB','GB','TB','PB'][$i]. (($withdetail) ? "(${size})" : '');
else return FALSE;
}
public static function phpEZhtmltagger_arrConverter( $arr ){
}
}