| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
http://flourishlib.com/docs/UTF-8 |
|---|
| 7 |
|
|---|
| 8 |
@copyright |
|---|
| 9 |
@author will@flourishlib.com |
|---|
| 10 |
@license http://flourishlib.com/license |
|---|
| 11 |
|
|---|
| 12 |
@package |
|---|
| 13 |
@link http://flourishlib.com/fHTML |
|---|
| 14 |
|
|---|
| 15 |
@version |
|---|
| 16 |
@changes |
|---|
| 17 |
@changes |
|---|
| 18 |
@changes |
|---|
| 19 |
@changes |
|---|
| 20 |
@changes |
|---|
| 21 |
@changes |
|---|
| 22 |
@changes |
|---|
| 23 |
|
|---|
| 24 |
class fHTML |
|---|
| 25 |
{ |
|---|
| 26 |
|
|---|
| 27 |
const containsBlockLevelHTML = 'fHTML::containsBlockLevelHTML'; |
|---|
| 28 |
const convertNewlines = 'fHTML::convertNewlines'; |
|---|
| 29 |
const decode = 'fHTML::decode'; |
|---|
| 30 |
const encode = 'fHTML::encode'; |
|---|
| 31 |
const makeLinks = 'fHTML::makeLinks'; |
|---|
| 32 |
const prepare = 'fHTML::prepare'; |
|---|
| 33 |
const printOption = 'fHTML::printOption'; |
|---|
| 34 |
const sendHeader = 'fHTML::sendHeader'; |
|---|
| 35 |
const show = 'fHTML::show'; |
|---|
| 36 |
const showChecked = 'fHTML::showChecked'; |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
@param |
|---|
| 43 |
@return |
|---|
| 44 |
|
|---|
| 45 |
static public function containsBlockLevelHTML($content) |
|---|
| 46 |
{ |
|---|
| 47 |
static $inline_tags = '<a><abbr><acronym><b><big><br><button><cite><code><del><dfn><em><font><i><img><input><ins><kbd><label><q><s><samp><select><small><span><strike><strong><sub><sup><textarea><tt><u><var>'; |
|---|
| 48 |
return strip_tags($content, $inline_tags) != $content; |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
@param |
|---|
| 56 |
@return |
|---|
| 57 |
|
|---|
| 58 |
static public function convertNewlines($content) |
|---|
| 59 |
{ |
|---|
| 60 |
static $inline_tags_minus_br = '<a><abbr><acronym><b><big><button><cite><code><del><dfn><em><font><i><img><input><ins><kbd><label><q><s><samp><select><small><span><strike><strong><sub><sup><textarea><tt><u><var>'; |
|---|
| 61 |
return (strip_tags($content, $inline_tags_minus_br) != $content) ? $content : nl2br($content); |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
@param |
|---|
| 69 |
@return |
|---|
| 70 |
|
|---|
| 71 |
static public function decode($content) |
|---|
| 72 |
{ |
|---|
| 73 |
return html_entity_decode($content, ENT_QUOTES, 'UTF-8'); |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
@param |
|---|
| 81 |
@return |
|---|
| 82 |
|
|---|
| 83 |
static public function encode($content) |
|---|
| 84 |
{ |
|---|
| 85 |
return htmlentities($content, ENT_QUOTES, 'UTF-8'); |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
@param |
|---|
| 93 |
@param |
|---|
| 94 |
@return |
|---|
| 95 |
|
|---|
| 96 |
static public function makeLinks($content, $link_text_length=0) |
|---|
| 97 |
{ |
|---|
| 98 |
|
|---|
| 99 |
$reg_exp = "/<\s*a(?:\s+[\w:]+(?:\s*=\s*(?:\"[^\"]*?\"|'[^']*?'|[^'\">\s]+))?)*\s*>.*?<\s*\/\s*a\s*>|<\s*\/?\s*[\w:]+(?:\s+[\w:]+(?:\s*=\s*(?:\"[^\"]*?\"|'[^']*?'|[^'\">\s]+))?)*\s*\/?\s*>|<\!--.*?-->/s"; |
|---|
| 100 |
preg_match_all($reg_exp, $content, $html_matches, PREG_SET_ORDER); |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
$text_matches = preg_split($reg_exp, $content); |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
foreach($text_matches as $key => $text) { |
|---|
| 107 |
preg_match_all( |
|---|
| 108 |
'~ |
|---|
| 109 |
\b([a-z]{3,}://[a-z0-9%\$\-_.+!*;/?:@=&\'\#,]+[a-z0-9\$\-_+!*;/?:@=&\'\#,])\b | # Fully URLs |
|---|
| 110 |
\b(www\.(?:[a-z0-9\-]+\.)+[a-z]{2,}(?:/[a-z0-9%\$\-_.+!*;/?:@=&\'\#,]+[a-z0-9\$\-_+!*;/?:@=&\'\#,])?)\b | # www. domains |
|---|
| 111 |
\b([a-z0-9\\.+\'_\\-]+@(?:[a-z0-9\\-]+\.)+[a-z]{2,})\b # email addresses |
|---|
| 112 |
~ix', |
|---|
| 113 |
$text, |
|---|
| 114 |
$matches, |
|---|
| 115 |
PREG_SET_ORDER |
|---|
| 116 |
); |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
http://www.example.com |
|---|
| 121 |
www.example.com |
|---|
| 122 |
$last_pos = 0; |
|---|
| 123 |
foreach ($matches as $match) { |
|---|
| 124 |
$match_pos = strpos($text, $match[0], $last_pos); |
|---|
| 125 |
$length = strlen($match[0]); |
|---|
| 126 |
$prefix = ''; |
|---|
| 127 |
|
|---|
| 128 |
if (!empty($match[3])) { |
|---|
| 129 |
$prefix = 'mailto:'; |
|---|
| 130 |
} elseif (!empty($match[2])) { |
|---|
| 131 |
$prefix = 'http://'; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
$replacement = '<a href="' . $prefix . $match[0] . '">'; |
|---|
| 135 |
$replacement .= ($link_text_length && strlen($match[0]) > $link_text_length) ? substr($match[0], 0, $link_text_length) . "…" : $match[0]; |
|---|
| 136 |
$replacement .= '</a>'; |
|---|
| 137 |
|
|---|
| 138 |
$text = substr_replace( |
|---|
| 139 |
$text, |
|---|
| 140 |
$replacement, |
|---|
| 141 |
$match_pos, |
|---|
| 142 |
$length |
|---|
| 143 |
); |
|---|
| 144 |
|
|---|
| 145 |
$last_pos = $match_pos + strlen($replacement); |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
$text_matches[$key] = $text; |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
for ($i = 0; $i < sizeof($html_matches); $i++) { |
|---|
| 153 |
$text_matches[$i] .= $html_matches[$i][0]; |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
return implode($text_matches); |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
@param |
|---|
| 164 |
@return |
|---|
| 165 |
|
|---|
| 166 |
static public function prepare($content) |
|---|
| 167 |
{ |
|---|
| 168 |
|
|---|
| 169 |
$reg_exp = "/<\s*\/?\s*[\w:]+(?:\s+[\w:]+(?:\s*=\s*(?:\"[^\"]*?\"|'[^']*?'|[^'\">\s]+))?)*\s*\/?\s*>|&(?:#\d+|\w+);|<\!--.*?-->/s"; |
|---|
| 170 |
preg_match_all($reg_exp, $content, $html_matches, PREG_SET_ORDER); |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
$text_matches = preg_split($reg_exp, $content); |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
foreach($text_matches as $key => $value) { |
|---|
| 177 |
$text_matches[$key] = htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
for ($i = 0; $i < sizeof($html_matches); $i++) { |
|---|
| 182 |
$text_matches[$i] .= $html_matches[$i][0]; |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
return implode($text_matches); |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
@param |
|---|
| 193 |
@param |
|---|
| 194 |
@param |
|---|
| 195 |
@return |
|---|
| 196 |
|
|---|
| 197 |
static public function printOption($text, $value, $selected_value=NULL) |
|---|
| 198 |
{ |
|---|
| 199 |
$selected = FALSE; |
|---|
| 200 |
if ($value == $selected_value || (is_array($selected_value) && in_array($value, $selected_value))) { |
|---|
| 201 |
$selected = TRUE; |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
echo '<option value="' . fHTML::encode($value) . '"'; |
|---|
| 205 |
if ($selected) { |
|---|
| 206 |
echo ' selected="selected"'; |
|---|
| 207 |
} |
|---|
| 208 |
echo '>' . fHTML::prepare($text) . '</option>'; |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
@return |
|---|
| 216 |
|
|---|
| 217 |
static public function sendHeader() |
|---|
| 218 |
{ |
|---|
| 219 |
header('Content-Type: text/html; charset=utf-8'); |
|---|
| 220 |
} |
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
@param |
|---|
| 227 |
@param |
|---|
| 228 |
@return |
|---|
| 229 |
|
|---|
| 230 |
static public function show($content, $css_class='') |
|---|
| 231 |
{ |
|---|
| 232 |
if ((!is_string($content) && !is_object($content) && !is_numeric($content)) || !strlen(trim($content))) { |
|---|
| 233 |
return FALSE; |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
$class = ($css_class) ? ' class="' . $css_class . '"' : ''; |
|---|
| 237 |
if (self::containsBlockLevelHTML($content)) { |
|---|
| 238 |
echo '<div' . $class . '>' . self::prepare($content) . '</div>'; |
|---|
| 239 |
} else { |
|---|
| 240 |
echo '<p' . $class . '>' . self::prepare($content) . '</p>'; |
|---|
| 241 |
} |
|---|
| 242 |
|
|---|
| 243 |
return TRUE; |
|---|
| 244 |
} |
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
@param |
|---|
| 256 |
@param |
|---|
| 257 |
@return |
|---|
| 258 |
|
|---|
| 259 |
static public function showChecked($value, $checked_value) |
|---|
| 260 |
{ |
|---|
| 261 |
$checked = FALSE; |
|---|
| 262 |
|
|---|
| 263 |
$one_null = $value === NULL || $checked_value === NULL; |
|---|
| 264 |
$equal = ($one_null) ? $value === $checked_value : $value == $checked_value; |
|---|
| 265 |
$in_array = is_array($checked_value) && in_array($value, $checked_value, $one_null ? TRUE : FALSE); |
|---|
| 266 |
|
|---|
| 267 |
if ($equal || $in_array) { |
|---|
| 268 |
$checked = TRUE; |
|---|
| 269 |
} |
|---|
| 270 |
|
|---|
| 271 |
if ($checked) { |
|---|
| 272 |
echo ' checked="checked"'; |
|---|
| 273 |
return TRUE; |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
return FALSE; |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
@return |
|---|
| 284 |
|
|---|
| 285 |
private function __construct() { } |
|---|
| 286 |
} |
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
will@flourishlib.com |
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|