| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
/** |
| 4 |
* Sanitizer class |
| 5 |
* |
| 6 |
* @package opitn/inludes/uitls |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace OPTN\Includes\Utils; |
| 10 |
|
| 11 |
/** |
| 12 |
* Optin HTML Content Sanitizer |
| 13 |
*/ |
| 14 |
class Sanitizer { |
| 15 |
|
| 16 |
/** |
| 17 |
* Common attributes |
| 18 |
* |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
private static $common_attributes = array( |
| 22 |
'class' => true, |
| 23 |
'id' => true, |
| 24 |
'style' => array( |
| 25 |
'transform' => array(), |
| 26 |
), |
| 27 |
'title' => true, |
| 28 |
'role' => true, |
| 29 |
'tabindex' => true, |
| 30 |
'aria-hidden' => true, |
| 31 |
'data-*' => true, |
| 32 |
'data-optn' => true, |
| 33 |
'data-optn-post-id' => true, |
| 34 |
'data-optn-post-type' => true, |
| 35 |
'data-optn-updated-at' => true, |
| 36 |
'data-optn-block-index' => true, |
| 37 |
'data-optn-autoplay' => true, |
| 38 |
'data-step-id' => true, |
| 39 |
'aria-description' => true, |
| 40 |
); |
| 41 |
|
| 42 |
/** |
| 43 |
* Allow list |
| 44 |
* |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
private static function get_allowed_html() { |
| 48 |
return array( |
| 49 |
|
| 50 |
// HTML. |
| 51 |
'a' => array_merge( |
| 52 |
self::$common_attributes, |
| 53 |
array( |
| 54 |
'href' => true, |
| 55 |
'title' => true, |
| 56 |
'rel' => true, |
| 57 |
'target' => true, |
| 58 |
'noreferrer' => true, |
| 59 |
'download' => true, |
| 60 |
) |
| 61 |
), |
| 62 |
'b' => self::$common_attributes, |
| 63 |
'strong' => self::$common_attributes, |
| 64 |
'i' => self::$common_attributes, |
| 65 |
'em' => self::$common_attributes, |
| 66 |
'del' => self::$common_attributes, |
| 67 |
'sub' => self::$common_attributes, |
| 68 |
'sup' => self::$common_attributes, |
| 69 |
'u' => self::$common_attributes, |
| 70 |
'br' => self::$common_attributes, |
| 71 |
'p' => self::$common_attributes, |
| 72 |
'div' => self::$common_attributes, |
| 73 |
'span' => self::$common_attributes, |
| 74 |
'h1' => self::$common_attributes, |
| 75 |
'h2' => self::$common_attributes, |
| 76 |
'h3' => self::$common_attributes, |
| 77 |
'h4' => self::$common_attributes, |
| 78 |
'h5' => self::$common_attributes, |
| 79 |
'h6' => self::$common_attributes, |
| 80 |
'img' => array_merge( |
| 81 |
self::$common_attributes, |
| 82 |
array( |
| 83 |
'src' => true, |
| 84 |
'alt' => true, |
| 85 |
'width' => true, |
| 86 |
'height' => true, |
| 87 |
'loading' => true, |
| 88 |
) |
| 89 |
), |
| 90 |
'video' => array_merge( |
| 91 |
self::$common_attributes, |
| 92 |
array( |
| 93 |
'src' => true, |
| 94 |
'poster' => true, |
| 95 |
'preload' => true, |
| 96 |
'controls' => true, |
| 97 |
'loading' => true, |
| 98 |
'muted' => true, |
| 99 |
'autoplay' => true, |
| 100 |
'height' => true, |
| 101 |
'loop' => true, |
| 102 |
'width' => true, |
| 103 |
'playsinline' => true, |
| 104 |
|
| 105 |
) |
| 106 |
), |
| 107 |
'ul' => self::$common_attributes, |
| 108 |
'ol' => self::$common_attributes, |
| 109 |
'li' => self::$common_attributes, |
| 110 |
'table' => array_merge( |
| 111 |
self::$common_attributes, |
| 112 |
array( |
| 113 |
'border' => true, |
| 114 |
'cellpadding' => true, |
| 115 |
'cellspacing' => true, |
| 116 |
) |
| 117 |
), |
| 118 |
'thead' => self::$common_attributes, |
| 119 |
'tbody' => self::$common_attributes, |
| 120 |
'tfoot' => self::$common_attributes, |
| 121 |
'tr' => self::$common_attributes, |
| 122 |
'th' => array_merge( |
| 123 |
self::$common_attributes, |
| 124 |
array( |
| 125 |
'colspan' => true, |
| 126 |
'rowspan' => true, |
| 127 |
) |
| 128 |
), |
| 129 |
'td' => array_merge( |
| 130 |
self::$common_attributes, |
| 131 |
array( |
| 132 |
'colspan' => true, |
| 133 |
'rowspan' => true, |
| 134 |
) |
| 135 |
), |
| 136 |
'form' => array_merge( |
| 137 |
self::$common_attributes, |
| 138 |
array( |
| 139 |
'action' => true, |
| 140 |
'method' => true, |
| 141 |
'enctype' => true, |
| 142 |
) |
| 143 |
), |
| 144 |
'input' => array_merge( |
| 145 |
self::$common_attributes, |
| 146 |
array( |
| 147 |
'type' => true, |
| 148 |
'name' => true, |
| 149 |
'value' => true, |
| 150 |
'placeholder' => true, |
| 151 |
'checked' => true, |
| 152 |
'disabled' => true, |
| 153 |
'readonly' => true, |
| 154 |
'required' => true, |
| 155 |
'min' => true, |
| 156 |
'max' => true, |
| 157 |
'step' => true, |
| 158 |
'maxlength' => true, |
| 159 |
'size' => true, |
| 160 |
) |
| 161 |
), |
| 162 |
'textarea' => array_merge( |
| 163 |
self::$common_attributes, |
| 164 |
array( |
| 165 |
'name' => true, |
| 166 |
'rows' => true, |
| 167 |
'cols' => true, |
| 168 |
'placeholder' => true, |
| 169 |
'maxlength' => true, |
| 170 |
'readonly' => true, |
| 171 |
'required' => true, |
| 172 |
) |
| 173 |
), |
| 174 |
'button' => array_merge( |
| 175 |
self::$common_attributes, |
| 176 |
array( |
| 177 |
'type' => true, |
| 178 |
'name' => true, |
| 179 |
'value' => true, |
| 180 |
'disabled' => true, |
| 181 |
) |
| 182 |
), |
| 183 |
'select' => array_merge( |
| 184 |
self::$common_attributes, |
| 185 |
array( |
| 186 |
'name' => true, |
| 187 |
'multiple' => true, |
| 188 |
'size' => true, |
| 189 |
'disabled' => true, |
| 190 |
'required' => true, |
| 191 |
) |
| 192 |
), |
| 193 |
'option' => array( |
| 194 |
'value' => true, |
| 195 |
'selected' => true, |
| 196 |
), |
| 197 |
'label' => array_merge( |
| 198 |
self::$common_attributes, |
| 199 |
array( |
| 200 |
'for' => true, |
| 201 |
) |
| 202 |
), |
| 203 |
'fieldset' => self::$common_attributes, |
| 204 |
'figure' => self::$common_attributes, |
| 205 |
'figcaption' => self::$common_attributes, |
| 206 |
'blockquote' => array_merge( |
| 207 |
self::$common_attributes, |
| 208 |
array( |
| 209 |
'cite' => true, |
| 210 |
) |
| 211 |
), |
| 212 |
'pre' => self::$common_attributes, |
| 213 |
'code' => self::$common_attributes, |
| 214 |
'hr' => self::$common_attributes, |
| 215 |
'kbd' => self::$common_attributes, |
| 216 |
'iframe' => array_merge( |
| 217 |
self::$common_attributes, |
| 218 |
array( |
| 219 |
'src' => true, |
| 220 |
'srcdoc' => true, |
| 221 |
'name' => true, |
| 222 |
'width' => true, |
| 223 |
'height' => true, |
| 224 |
'frameborder' => true, |
| 225 |
'allow' => true, |
| 226 |
'allowfullscreen' => true, |
| 227 |
'loading' => true, |
| 228 |
'referrerpolicy' => true, |
| 229 |
'sandbox' => true, |
| 230 |
'scrolling' => true, |
| 231 |
'style' => true, |
| 232 |
'class' => true, |
| 233 |
'id' => true, |
| 234 |
'title' => true, |
| 235 |
'aria-hidden' => true, |
| 236 |
'allowtransparency' => true, |
| 237 |
) |
| 238 |
), |
| 239 |
|
| 240 |
// SVG. |
| 241 |
'svg' => array( |
| 242 |
'xmlns' => true, |
| 243 |
'xmlns:xlink' => true, |
| 244 |
'version' => true, |
| 245 |
'id' => true, |
| 246 |
'viewBox' => true, |
| 247 |
'viewbox' => true, // Must be lowercase B otherwise wp's stupid regex will remove it. |
| 248 |
'width' => true, |
| 249 |
'height' => true, |
| 250 |
'class' => true, |
| 251 |
'style' => true, |
| 252 |
'fill' => true, |
| 253 |
'stroke' => true, |
| 254 |
'stroke-width' => true, |
| 255 |
), |
| 256 |
'clippath' => array( // Must be in lowercase. |
| 257 |
'id' => true, |
| 258 |
), |
| 259 |
'g' => array( |
| 260 |
'id' => true, |
| 261 |
'class' => true, |
| 262 |
'transform' => true, |
| 263 |
'clip-path' => true, |
| 264 |
), |
| 265 |
'path' => array( |
| 266 |
'id' => true, |
| 267 |
'd' => true, |
| 268 |
'fill' => true, |
| 269 |
'stroke' => true, |
| 270 |
'stroke-width' => true, |
| 271 |
'class' => true, |
| 272 |
), |
| 273 |
'rect' => array( |
| 274 |
'x' => true, |
| 275 |
'y' => true, |
| 276 |
'width' => true, |
| 277 |
'height' => true, |
| 278 |
'fill' => true, |
| 279 |
'class' => true, |
| 280 |
), |
| 281 |
'circle' => array( |
| 282 |
'cx' => true, |
| 283 |
'cy' => true, |
| 284 |
'r' => true, |
| 285 |
'fill' => true, |
| 286 |
'class' => true, |
| 287 |
), |
| 288 |
'ellipse' => array( |
| 289 |
'cx' => true, |
| 290 |
'cy' => true, |
| 291 |
'rx' => true, |
| 292 |
'ry' => true, |
| 293 |
'fill' => true, |
| 294 |
'class' => true, |
| 295 |
), |
| 296 |
'line' => array( |
| 297 |
'x1' => true, |
| 298 |
'y1' => true, |
| 299 |
'x2' => true, |
| 300 |
'y2' => true, |
| 301 |
'stroke' => true, |
| 302 |
'class' => true, |
| 303 |
), |
| 304 |
'polyline' => array( |
| 305 |
'points' => true, |
| 306 |
'fill' => true, |
| 307 |
'stroke' => true, |
| 308 |
'class' => true, |
| 309 |
), |
| 310 |
'polygon' => array( |
| 311 |
'points' => true, |
| 312 |
'fill' => true, |
| 313 |
'stroke' => true, |
| 314 |
'class' => true, |
| 315 |
), |
| 316 |
'text' => array( |
| 317 |
'x' => true, |
| 318 |
'y' => true, |
| 319 |
'fill' => true, |
| 320 |
'font-family' => true, |
| 321 |
'font-size' => true, |
| 322 |
), |
| 323 |
'textpath' => array( |
| 324 |
'href' => true, |
| 325 |
'class' => true, |
| 326 |
'xlink:href' => true, |
| 327 |
'text-anchor' => true, |
| 328 |
'startoffset' => true, |
| 329 |
'dominant-baseline' => true, |
| 330 |
), |
| 331 |
'tspan' => array( |
| 332 |
'x' => true, |
| 333 |
'y' => true, |
| 334 |
'fill' => true, |
| 335 |
'font-family' => true, |
| 336 |
'font-size' => true, |
| 337 |
), |
| 338 |
'use' => array( |
| 339 |
'xlink:href' => true, |
| 340 |
'x' => true, |
| 341 |
'y' => true, |
| 342 |
), |
| 343 |
'defs' => true, |
| 344 |
'symbol' => array( 'id' => true ), |
| 345 |
'linearGradient' => array( |
| 346 |
'id' => true, |
| 347 |
'x1' => true, |
| 348 |
'y1' => true, |
| 349 |
'x2' => true, |
| 350 |
'y2' => true, |
| 351 |
), |
| 352 |
'stop' => array( |
| 353 |
'offset' => true, |
| 354 |
'stop-color' => true, |
| 355 |
), |
| 356 |
'title' => array(), |
| 357 |
'desc' => array(), |
| 358 |
'script' => array(), |
| 359 |
'style' => array(), |
| 360 |
); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
/** |
| 365 |
* Allowed protocols |
| 366 |
* |
| 367 |
* @var array |
| 368 |
*/ |
| 369 |
private static $allowed_protocols = array( |
| 370 |
'http', |
| 371 |
'https', |
| 372 |
'ftp', |
| 373 |
'ftps', |
| 374 |
'mailto', |
| 375 |
'tel', |
| 376 |
'fax', |
| 377 |
); |
| 378 |
|
| 379 |
/** |
| 380 |
* Allowed styles |
| 381 |
* |
| 382 |
* @var array |
| 383 |
*/ |
| 384 |
private static $allowed_styles = array( |
| 385 |
'stroke-dashoffset', |
| 386 |
'stroke-linejoin', |
| 387 |
'display', |
| 388 |
'background-clip', |
| 389 |
'color', |
| 390 |
'transform', |
| 391 |
); |
| 392 |
|
| 393 |
/** |
| 394 |
* Sanitizes html content |
| 395 |
* |
| 396 |
* @param string $content html content. |
| 397 |
* @return string |
| 398 |
*/ |
| 399 |
public static function sanitize( $content ) { |
| 400 |
|
| 401 |
add_filter( 'optn_is_sanitizing_content', '__return_true' ); |
| 402 |
|
| 403 |
$sanitized_content = wp_kses( $content, self::get_allowed_html(), self::$allowed_protocols ); |
| 404 |
|
| 405 |
add_filter( 'optn_is_sanitizing_content', '__return_false' ); |
| 406 |
|
| 407 |
return $sanitized_content; |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Sanitizes and echos html content |
| 412 |
* |
| 413 |
* @param string $content html content. |
| 414 |
* @return void |
| 415 |
*/ |
| 416 |
public static function e_sanitize( $content ) { |
| 417 |
echo self::sanitize( $content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Adds styles to allowlist |
| 422 |
* |
| 423 |
* @param array $styles style array. |
| 424 |
* @return array |
| 425 |
*/ |
| 426 |
public static function add_to_style_allowlist( $styles ) { |
| 427 |
return array_merge( $styles, self::$allowed_styles ); |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* Allow Style attribute value |
| 432 |
* |
| 433 |
* @param boolean $allow_css allow css. |
| 434 |
* @param string $css_str css string. |
| 435 |
* @return boolean |
| 436 |
*/ |
| 437 |
public static function allow_style_attrs( $allow_css, $css_str ) { |
| 438 |
|
| 439 |
// Check if we are sanitizing optin content. |
| 440 |
$is_sanitizing_optin_content = apply_filters( 'optn_is_sanitizing_content', false ); |
| 441 |
|
| 442 |
if ( $is_sanitizing_optin_content ) { |
| 443 |
$allowed = array( 'rgb', 'skew' ); |
| 444 |
|
| 445 |
foreach ( $allowed as $str ) { |
| 446 |
if ( strpos( $css_str, $str ) !== false ) { |
| 447 |
return true; |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
return $allow_css; |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* Adds tags to allowlist |
| 457 |
* |
| 458 |
* @param array $tags tags array. |
| 459 |
* @param string $context context. |
| 460 |
* @return array |
| 461 |
*/ |
| 462 |
public static function add_to_tags_allowlist( $tags, $context ) { |
| 463 |
if ( 'post' === $context ) { |
| 464 |
$tags['video'] = array( |
| 465 |
'autoplay' => true, |
| 466 |
'controls' => true, |
| 467 |
'height' => true, |
| 468 |
'loop' => true, |
| 469 |
'muted' => true, |
| 470 |
'poster' => true, |
| 471 |
'preload' => true, |
| 472 |
'src' => true, |
| 473 |
'width' => true, |
| 474 |
); |
| 475 |
$tags['source'] = array( |
| 476 |
'src' => true, |
| 477 |
'type' => true, |
| 478 |
); |
| 479 |
} |
| 480 |
return $tags; |
| 481 |
} |
| 482 |
} |
| 483 |
|