-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass.html.php
More file actions
367 lines (330 loc) · 10.9 KB
/
Copy pathclass.html.php
File metadata and controls
367 lines (330 loc) · 10.9 KB
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
<?php
/**
* @abstract
* @author Miranda <[email protected]> & Gustavo <[email protected]>
* @version 1.0.7
* @link https://github.com/over9k/HTML-Helper
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @package HTML Helper
*/
abstract class HTML {
/**
* $tag Tell what is the current open tag for close it later.
*
* @static
* @access private
* @var string What is the current open tag?
*/
private static $tag = '';
/**
* What is the current class version?
*
* @const string The current script version
*/
const VERSION = '1.0.7';
/**
* ONLY FOR THIS CLASS (self)
* self::parse_attr($attributes) -> Parse out the attributes
*
* @static
* @access private
* @param mixed - An array or string for parse the specified attributes
* @return string The parsed attribute (attribute="value")
*/
private static function parse_attr($attributes) {
if (is_string($attributes)) {
return (!empty($attributes)) ? ' ' . trim($attributes) : '';
}
if (is_array($attributes)) {
$attr = '';
foreach ($attributes as $key => $val) {
$attr .= ' ' . $key . '="' . $val . '"';
}
return $attr;
}
}
/**
* ONLY FOR THIS CLASS (self)
* HTML::parse_fields($fields) -> Parse the $fields array and transform into a valid HTML input
*
* @static
* @access private
* @param array $fields An array with the following structure -> 'Type' => array($attributes)
* @return string The parsed input HTML
*/
private static function parse_fields($fields) {
if (is_array($fields)) {
$field = '';
foreach ($fields as $key => $val) {
$attributes = self::parse_attr($val);
$field .= '<input type="' . $key .'"' . $attributes . ' />' . PHP_EOL;
}
return $field;
}
}
/**
* ONLY FOR THIS CLASS (self)
* self::list_item($items) -> Returns a <li></li> tag parsed with the value in the array ($items = array)
*
* @static
* @access private
* @param array $items The array with a list to transform into a <li></li> tag
* @param string $class A class for the items
* @return string The complete <li></li> tag
*/
private static function list_item($items, $class = null) {
if (is_array($items)) {
$class = (isset($class) && !empty($class)) ? ' class="' . $class . '"': null;
$li = '';
$i = 0;
foreach ($items as $key => $val) {
$i++;
$li .= '<li id="' . $i . '"' . $class . '>' . PHP_EOL . $val . PHP_EOL . '</li>' . PHP_EOL;
}
return $li;
}
}
/**
* ONLY FOR THIS CLASS (self)
* self::filter description
*
* @static
* @access private
* @param string $str The input string to filter
* @param string $mode The filter mode
* @return mixed May return the filtered string or may return null if the $mode variable isn't set
*/
private static function filter($str, $mode) {
switch($mode) {
case 'strip':
/* HTML tags are stripped from the string
before it is used. */
return strip_tags($str);
case 'escapeAll':
/* HTML and special characters are escaped from the string
before it is used. */
return htmlentities($str, ENT_QUOTES, 'UTF-8');
case 'escape':
/* Only HTML tags are escaped from the string. Special characters
is kept as is. */
return htmlspecialchars($str, ENT_NOQUOTES, 'UTF-8');
case 'url':
/* Encode a string according to RFC 3986 for use in a URL. */
return rawurlencode($str);
case 'filename':
/* Escape a string so it's safe to be used as filename. */
return str_replace('/', '-', $str);
default:
return null;
}
}
/**
* Generates a HTML document type
*
* @static
* @access public
* @param string $type Type of the document
* @return string
*/
public static function Doctype($type = 'html5') {
$doctypes = array(
'html5' => '<!DOCTYPE html>',
'xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
'xhtml1-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
'xhtml1-trans' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
'xhtml1-frame' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
'html4-strict' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
'html4-trans' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
'html4-frame' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
);
if (isset($doctypes[strtolower($type)])) {
return $doctypes[$type] . "\n";
}
else {
return '';
}
}
/**
* Creates the <img /> tag
*
* @static
* @access public
* @param string $src Where is the image?
* @param mixed $attributes Custom attributes (must be a valid attribute for the <img /> tag)
* @return string The formated <img /> tag
*/
public static function Image($src, $attributes = '') {
if (isset($attributes) && !empty($attributes)) {
$attributes = self::parse_attr($attributes);
}
$border = (isset($attributes['border']) && !empty($attributes['border'])) ? $attributes['border'] . ' ' : 'border="0" ';
$alt = (isset($attributes['alt']) && !empty($attributes['alt'])) ? $attributes['alt'] . ' ' : 'alt="" ';
return '<img src="' . $src . '"' . $attributes . ' ' . $border . $alt . '/>';
}
/**
* Creates a HTML Anchor link
*
* @static
* @access public
* @param string $url the URL
* @param string $label the link value
* @param mixed $attributes Custom attributes (must be a valid attribute for the <a></a> tag)
* @return string The formated <a></a> tag
*/
public static function Anchor($url, $label = null, $attributes = null) {
$label = (!empty($label)) ? $label : $url;
if (isset($attributes) && !empty($attributes)) {
$attributes = self::parse_attr($attributes);
}
return '<a href="' . $uri . '"' . $attributes . '>' . $label . '</a>';
}
/**
* Generates a "mailto" link
*
* @static
* @access public
* @param $email
* @param string $label The anchor value.
* @param mixed $attributes Custom attributes (must be a valid attribute for the <a></a> tag)
* @return string The formated <a></a> tag with the 'href' attribute set for: mailto:$email
*/
public static function Email($email, $label = null, $attributes = null) {
$label = (!empty($label)) ? $label : $email;
if (isset($attributes) && !empty($attributes)) {
$attributes = self::parse_attr($attributes);
}
$html = '<a href="mailto:' . $email . '"' . $attributes . '>' . $label . '</a>';
return $html;
}
/**
* HTML <br /> tag
*
* @static
* @access public
* @param int $count How many line breaks?
* @return string
*/
public static function LineBreak($count = 1) {
return str_repeat('<br />', $count) . PHP_EOL;
}
/**
* Returns non-breaking space entities
*
* @static
* @access public
* @param int $count How many spaces?
* @return string
*/
public static function Space($count = 1) {
return str_repeat(' ', $count);
}
/**
* HTML::Form() -> Creates the <form> tag with the specified variables.
*
* @static
* @access public
* @param string $action The action attribute value.
* @param array $fields What is the form fields?
* @param string $name The form name
* @param string $method The form method (post or get)
* @param string $enctype The form enctype
*/
public static function Form($action, $fields, $name = null, $method = 'post', $enctype = 'multipart/form-data') {
$name = (isset($name) && !empty($name)) ? ' name="' . $name . '"' : null;
$method = (isset($method)) ? ' method="' . $method . '"': null;
$enctype = (isset($enctype)) ? ' enctype="' . $enctype . '"': null;
$html = '<form action="' . $action . '"' . $name . $method . $enctype . '>' . PHP_EOL;
$html .= self::parse_fields($fields);
$html .= '</form>' . PHP_EOL;
return $html;
}
/**
* HTML::Open('tag') -> Opens a HTML tag
*
* @static
* @access private
* @param string $tag Which tag we're gonna open?
* @param mixed $attributes Custom attributes (must be a valid attribute for the specified tag)
* @param array $li_items Some array with items for <ul> or <ol> tags
* @return string Return the opened tag (<$tag>)
*/
public static function Open($tag, $attributes = null, $li_items = array()) {
self::$tag = strtolower($tag);
if (isset($attributes) && !empty($attributes)) {
$attributes = self::parse_attr($attributes);
}
if ($tag == 'ul' || $tag == 'ol') {
if (!empty($attributes['li_class'])) {
$list = self::list_item($li_items, $attributes['li_class']);
return '<' . self::$tag . $attributes . '>' . PHP_EOL . $li_items;
} else {
$list = self::list_item($li_items);
return '<' . self::$tag . $attributes . '>' . PHP_EOL . $li_items;
}
}
return '<' . self::$tag . $attributes . '>' . PHP_EOL;
}
/**
* HTML::Close() -> Close the current open tag
*
* @static
* @access public
*/
public static function Close() {
return PHP_EOL . '</' . self::$tag . '>' . PHP_EOL;
}
/**
* HTML::Filter_XSS($str, $args) -> Filter some string with the params into $args
*
* @static
* @access public
* @param string $str String to clean the possible XSS attack.
* @param array $args The array with the parameters
* @return string The safe string.
*/
public static function Filter_XSS($str, $args) {
/* Loop trough the args and apply the filters. */
while(list($name, $data) = each($args)) {
$safe = false;
$type = mb_substr($name, 0, 1);
switch($type) {
case '%':
/* %variables: HTML tags are stripped of from the string
before it's inserted. */
$safe = self::filter($data, 'strip');
break;
case '!':
/* !variables: HTML and special characters are escaped from the string
before it is used. */
$safe = self::filter($data, 'escapeAll');
break;
case '@':
/* @variables: Only HTML is escaped from the string. Special characters
is kept as it is. */
$safe = self::filter($data, 'escape');
break;
case '&':
/* Encode a string according to RFC 3986 for use in a URL. */
$safe = self::filter($data, 'url');
break;
default:
return null;
break;
}
if ($safe !== false) {
$str = str_replace($name, $safe, $str);
}
}
return $str;
}
/**
* HTML::Version() -> Return the script version
*
* @static
* @access public
*/
public static function Version() {
return self::VERSION;
}
}
?>