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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* HTML helper class.
*
* $Id: html.php 4368 2009-05-27 21:58:51Z samsoir $
*
* @package Core
* @author Kohana Team
* @copyright (c) 2007-2008 Kohana Team
* @license http://kohanaphp.com/license.html
*/
class html_Core {
// Enable or disable automatic setting of target="_blank"
public static $windowed_urls = FALSE;
/**
* Convert special characters to HTML entities
*
* @param string string to convert
* @param boolean encode existing entities
* @return string
*/
public static function specialchars($str, $double_encode = TRUE)
{
// Force the string to be a string
$str = (string) $str;
// Do encode existing HTML entities (default)
if ($double_encode === TRUE)
{
$str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
}
else
{
// Do not encode existing HTML entities
// From PHP 5.2.3 this functionality is built-in, otherwise use a regex
if (version_compare(PHP_VERSION, '5.2.3', '>='))
{
$str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8', FALSE);
}
else
{
$str = preg_replace('/&(?!(?:#\d++|[a-z]++);)/ui', '&', $str);
$str = str_replace(array('<', '>', '\'', '"'), array('<', '>', ''', '"'), $str);
}
}
return $str;
}
/**
* Perform a html::specialchars() with additional URL specific encoding.
*
* @param string string to convert
* @param boolean encode existing entities
* @return string
*/
public static function specialurlencode($str, $double_encode = TRUE)
{
return str_replace(' ', '%20', html::specialchars($str, $double_encode));
}
/**
* Create HTML link anchors.
*
* @param string URL or URI string
* @param string link text
* @param array HTML anchor attributes
* @param string non-default protocol, eg: https
* @return string
*/
public static function anchor($uri, $title = NULL, $attributes = NULL, $protocol = NULL)
{
if ($uri === '')
{
$site_url = url::base(FALSE);
}
elseif (strpos($uri, '://') === FALSE AND strpos($uri, '#') !== 0)
{
$site_url = url::site($uri, $protocol);
}
else
{
if (html::$windowed_urls === TRUE AND empty($attributes['target']))
{
$attributes['target'] = '_blank';
}
$site_url = $uri;
}
return
// Parsed URL
'<a href="'.html::specialurlencode($site_url, FALSE).'"'
// Attributes empty? Use an empty string
.(is_array($attributes) ? html::attributes($attributes) : '').'>'
// Title empty? Use the parsed URL
.html::specialchars((($title === NULL) ? $site_url : $title), FALSE).'</a>';
}
/**
* Creates an HTML anchor to a file.
*
* @param string name of file to link to
* @param string link text
* @param array HTML anchor attributes
* @param string non-default protocol, eg: ftp
* @return string
*/
public static function file_anchor($file, $title = NULL, $attributes = NULL, $protocol = NULL)
{
return
// Base URL + URI = full URL
'<a href="'.html::specialurlencode(url::base(FALSE, $protocol).$file, FALSE).'"'
// Attributes empty? Use an empty string
.(is_array($attributes) ? html::attributes($attributes) : '').'>'
// Title empty? Use the filename part of the URI
.(($title === NULL) ? end(explode('/', $file)) : $title) .'</a>';
}
/**
* Similar to anchor, but with the protocol parameter first.
*
* @param string link protocol
* @param string URI or URL to link to
* @param string link text
* @param array HTML anchor attributes
* @return string
*/
public static function panchor($protocol, $uri, $title = NULL, $attributes = FALSE)
{
return html::anchor($uri, $title, $attributes, $protocol);
}
/**
* Create an array of anchors from an array of link/title pairs.
*
* @param array link/title pairs
* @return array
*/
public static function anchor_array(array $array)
{
$anchors = array();
foreach ($array as $link => $title)
{
// Create list of anchors
$anchors[] = html::anchor($link, $title);
}
return $anchors;
}
/**
* Generates an obfuscated version of an email address.
*
* @param string email address
* @return string
*/
public static function email($email)
{
$safe = '';
foreach (str_split($email) as $letter)
{
switch (($letter === '@') ? rand(1, 2) : rand(1, 3))
{
// HTML entity code
case 1: $safe .= '&#'.ord($letter).';'; break;
// Hex character code
case 2: $safe .= '&#x'.dechex(ord($letter)).';'; break;
// Raw (no) encoding
case 3: $safe .= $letter;
}
}
return $safe;
}
/**
* Creates an email anchor.
*
* @param string email address to send to
* @param string link text
* @param array HTML anchor attributes
* @return string
*/
public static function mailto($email, $title = NULL, $attributes = NULL)
{
if (empty($email))
return $title;
// Remove the subject or other parameters that do not need to be encoded
if (strpos($email, '?') !== FALSE)
{
// Extract the parameters from the email address
list ($email, $params) = explode('?', $email, 2);
// Make the params into a query string, replacing spaces
$params = '?'.str_replace(' ', '%20', $params);
}
else
{
// No parameters
$params = '';
}
// Obfuscate email address
$safe = html::email($email);
// Title defaults to the encoded email address
empty($title) and $title = $safe;
// Parse attributes
empty($attributes) or $attributes = html::attributes($attributes);
// Encoded start of the href="" is a static encoded version of 'mailto:'
return '<a href="mailto:'.$safe.$params.'"'.$attributes.'>'.$title.'</a>';
}
/**
* Generate a "breadcrumb" list of anchors representing the URI.
*
* @param array segments to use as breadcrumbs, defaults to using Router::$segments
* @return string
*/
public static function breadcrumb($segments = NULL)
{
empty($segments) and $segments = Router::$segments;
$array = array();
while ($segment = array_pop($segments))
{
$array[] = html::anchor
(
// Complete URI for the URL
implode('/', $segments).'/'.$segment,
// Title for the current segment
ucwords(inflector::humanize($segment))
);
}
// Retrun the array of all the segments
return array_reverse($array);
}
/**
* Creates a meta tag.
*
* @param string|array tag name, or an array of tags
* @param string tag "content" value
* @return string
*/
public static function meta($tag, $value = NULL)
{
if (is_array($tag))
{
$tags = array();
foreach ($tag as $t => $v)
{
// Build each tag and add it to the array
$tags[] = html::meta($t, $v);
}
// Return all of the tags as a string
return implode("\n", $tags);
}
// Set the meta attribute value
$attr = in_array(strtolower($tag), Kohana::config('http.meta_equiv')) ? 'http-equiv' : 'name';
return '<meta '.$attr.'="'.$tag.'" content="'.$value.'" />';
}
/**
* Creates a stylesheet link.
*
* @param string|array filename, or array of filenames to match to array of medias
* @param string|array media type of stylesheet, or array to match filenames
* @param boolean include the index_page in the link
* @return string
*/
public static function stylesheet($style, $media = FALSE, $index = FALSE)
{
return html::link($style, 'stylesheet', 'text/css', '.css', $media, $index);
}
/**
* Creates a link tag.
*
* @param string|array filename
* @param string|array relationship
* @param string|array mimetype
* @param string specifies suffix of the file
* @param string|array specifies on what device the document will be displayed
* @param boolean include the index_page in the link
* @return string
*/
public static function link($href, $rel, $type, $suffix = FALSE, $media = FALSE, $index = FALSE)
{
$compiled = '';
if (is_array($href))
{
foreach ($href as $_href)
{
$_rel = is_array($rel) ? array_shift($rel) : $rel;
$_type = is_array($type) ? array_shift($type) : $type;
$_media = is_array($media) ? array_shift($media) : $media;
$compiled .= html::link($_href, $_rel, $_type, $suffix, $_media, $index);
}
}
else
{
if (strpos($href, '://') === FALSE)
{
// Make the URL absolute
$href = url::base($index).$href;
}
$length = strlen($suffix);
if ( $length > 0 AND substr_compare($href, $suffix, -$length, $length, FALSE) !== 0)
{
// Add the defined suffix
$href .= $suffix;
}
$attr = array
(
'rel' => $rel,
'type' => $type,
'href' => $href,
);
if ( ! empty($media))
{
// Add the media type to the attributes
$attr['media'] = $media;
}
$compiled = '<link'.html::attributes($attr).' />';
}
return $compiled."\n";
}
/**
* Creates a script link.
*
* @param string|array filename
* @param boolean include the index_page in the link
* @return string
*/
public static function script($script, $index = FALSE)
{
$compiled = '';
if (is_array($script))
{
foreach ($script as $name)
{
$compiled .= html::script($name, $index);
}
}
else
{
if (strpos($script, '://') === FALSE)
{
// Add the suffix only when it's not already present
$script = url::base((bool) $index).$script;
}
if (substr_compare($script, '.js', -3, 3, FALSE) !== 0)
{
// Add the javascript suffix
$script .= '.js';
}
$compiled = '<script type="text/javascript" src="'.$script.'"></script>';
}
return $compiled."\n";
}
/**
* Creates a image link.
*
* @param string image source, or an array of attributes
* @param string|array image alt attribute, or an array of attributes
* @param boolean include the index_page in the link
* @return string
*/
public static function image($src = NULL, $alt = NULL, $index = FALSE)
{
// Create attribute list
$attributes = is_array($src) ? $src : array('src' => $src);
if (is_array($alt))
{
$attributes += $alt;
}
elseif ( ! empty($alt))
{
// Add alt to attributes
$attributes['alt'] = $alt;
}
if (strpos($attributes['src'], '://') === FALSE)
{
// Make the src attribute into an absolute URL
$attributes['src'] = url::base($index).$attributes['src'];
}
return '<img'.html::attributes($attributes).' />';
}
/**
* Compiles an array of HTML attributes into an attribute string.
*
* @param string|array array of attributes
* @return string
*/
public static function attributes($attrs)
{
if (empty($attrs))
return '';
if (is_string($attrs))
return ' '.$attrs;
$compiled = '';
foreach ($attrs as $key => $val)
{
$compiled .= ' '.$key.'="'.html::specialchars($val).'"';
}
return $compiled;
}
} // End html
|