@@ -4,12 +4,12 @@ discard block |
||
4 | 4 | |
5 | 5 | class BeforeStrip |
6 | 6 | { |
7 | - protected $multi = false; |
|
8 | - protected $is_html = false; |
|
9 | - protected $is_php = false; |
|
10 | - protected $html = []; |
|
11 | - protected $php = []; |
|
12 | - protected $content = ''; |
|
7 | + protected $multi=false; |
|
8 | + protected $is_html=false; |
|
9 | + protected $is_php=false; |
|
10 | + protected $html=[]; |
|
11 | + protected $php=[]; |
|
12 | + protected $content=''; |
|
13 | 13 | protected $doctype; |
14 | 14 | protected $body; |
15 | 15 | protected $head; |
@@ -20,58 +20,58 @@ discard block |
||
20 | 20 | if (!is_string($html)) { |
21 | 21 | throw new \InvalidArgumentException(sprintf('Waiting string, %s given', gettype($html))); |
22 | 22 | } |
23 | - $html = is_file($html) ? file_get_contents($html) : $html; |
|
23 | + $html=is_file($html) ? file_get_contents($html) : $html; |
|
24 | 24 | $this->setMainTags($html); |
25 | 25 | |
26 | - $html = '<div>' . str_replace( |
|
26 | + $html='<div>'.str_replace( |
|
27 | 27 | [$this->doctype, $this->html_tag, '</html>', $this->head, '</head>', $this->body, '</body>'], |
28 | - ['<doctypetag' . substr($this->doctype, 9), '<htmltag ' . substr($this->html_tag, 5), '</htmltag>', '<headtag ' . substr($this->head, 5), '</headtag>', '<bodytag ' . substr($this->body, 5), '</bodytag>'], |
|
28 | + ['<doctypetag'.substr($this->doctype, 9), '<htmltag '.substr($this->html_tag, 5), '</htmltag>', '<headtag '.substr($this->head, 5), '</headtag>', '<bodytag '.substr($this->body, 5), '</bodytag>'], |
|
29 | 29 | $html |
30 | - ) . '</doctypetag></div>'; |
|
31 | - $preprocessed = token_get_all($html); |
|
30 | + ).'</doctypetag></div>'; |
|
31 | + $preprocessed=token_get_all($html); |
|
32 | 32 | |
33 | - $HTML = array_filter($preprocessed, function ($v) { |
|
34 | - return is_array($v) && $v[0] === T_INLINE_HTML; |
|
33 | + $HTML=array_filter($preprocessed, function($v) { |
|
34 | + return is_array($v)&&$v[0]===T_INLINE_HTML; |
|
35 | 35 | }); |
36 | - $PHP = array_diff_key($preprocessed, $HTML); |
|
36 | + $PHP=array_diff_key($preprocessed, $HTML); |
|
37 | 37 | $this->init($HTML, $PHP, $html); |
38 | 38 | } |
39 | 39 | |
40 | 40 | protected function setMainTags(&$html) |
41 | 41 | { |
42 | - $doctypeOffset = stripos($html, '<!doctype'); |
|
43 | - $headOffset = stripos($html, '<head'); |
|
44 | - $htmlTagOffset = stripos($html, '<html'); |
|
45 | - $bodyOffset = stripos($html, '<body'); |
|
46 | - if (false !== $doctypeOffset) { |
|
47 | - $endDoctypeOffset = strpos($html, '>', $doctypeOffset); |
|
48 | - $this->doctype = substr($html, $doctypeOffset, $endDoctypeOffset - $doctypeOffset + 1); |
|
42 | + $doctypeOffset=stripos($html, '<!doctype'); |
|
43 | + $headOffset=stripos($html, '<head'); |
|
44 | + $htmlTagOffset=stripos($html, '<html'); |
|
45 | + $bodyOffset=stripos($html, '<body'); |
|
46 | + if (false!==$doctypeOffset) { |
|
47 | + $endDoctypeOffset=strpos($html, '>', $doctypeOffset); |
|
48 | + $this->doctype=substr($html, $doctypeOffset, $endDoctypeOffset-$doctypeOffset+1); |
|
49 | 49 | } |
50 | - if (false !== $headOffset) { |
|
51 | - $endHeadOffset = strpos($html, '>', $headOffset); |
|
52 | - $this->head = substr($html, $headOffset, $endHeadOffset - $headOffset + 1); |
|
50 | + if (false!==$headOffset) { |
|
51 | + $endHeadOffset=strpos($html, '>', $headOffset); |
|
52 | + $this->head=substr($html, $headOffset, $endHeadOffset-$headOffset+1); |
|
53 | 53 | } |
54 | - if (false !== $bodyOffset) { |
|
55 | - $endBodyOffset = strpos($html, '>', $bodyOffset); |
|
56 | - $this->body = substr($html, $bodyOffset, $endBodyOffset - $bodyOffset + 1); |
|
54 | + if (false!==$bodyOffset) { |
|
55 | + $endBodyOffset=strpos($html, '>', $bodyOffset); |
|
56 | + $this->body=substr($html, $bodyOffset, $endBodyOffset-$bodyOffset+1); |
|
57 | 57 | } |
58 | - if (false !== $htmlTagOffset) { |
|
59 | - $endHtmlTagOffset = strpos($html, '>', $htmlTagOffset); |
|
60 | - $this->html_tag = substr($html, $htmlTagOffset, $endHtmlTagOffset - $htmlTagOffset + 1); |
|
58 | + if (false!==$htmlTagOffset) { |
|
59 | + $endHtmlTagOffset=strpos($html, '>', $htmlTagOffset); |
|
60 | + $this->html_tag=substr($html, $htmlTagOffset, $endHtmlTagOffset-$htmlTagOffset+1); |
|
61 | 61 | } |
62 | 62 | } |
63 | 63 | |
64 | 64 | protected function init(&$HTML, &$PHP, &$html) |
65 | 65 | { |
66 | - $is_h = (bool)$HTML; |
|
67 | - $is_p = (bool)$PHP; |
|
68 | - |
|
69 | - $this->is_html = $is_h; |
|
70 | - $this->is_php = $is_p; |
|
71 | - $this->multi = $is_h && $is_p; |
|
72 | - $this->content = $html; |
|
73 | - $this->html = $is_p ? $HTML : []; |
|
74 | - $this->php = $is_p ? $PHP : []; |
|
66 | + $is_h=(bool)$HTML; |
|
67 | + $is_p=(bool)$PHP; |
|
68 | + |
|
69 | + $this->is_html=$is_h; |
|
70 | + $this->is_php=$is_p; |
|
71 | + $this->multi=$is_h&&$is_p; |
|
72 | + $this->content=$html; |
|
73 | + $this->html=$is_p ? $HTML : []; |
|
74 | + $this->php=$is_p ? $PHP : []; |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | public function mustLoadMulti() |
@@ -6,31 +6,31 @@ discard block |
||
6 | 6 | { |
7 | 7 | protected $type; |
8 | 8 | |
9 | - public function __construct($html, $type = 'replace', $notAllowedTags = ['', false], $notAllowedAttributes = ['', false]) |
|
9 | + public function __construct($html, $type='replace', $notAllowedTags=['', false], $notAllowedAttributes=['', false]) |
|
10 | 10 | { |
11 | - $callback = function ($v) { |
|
11 | + $callback=function($v) { |
|
12 | 12 | return trim(strtolower($v)); |
13 | 13 | }; |
14 | - if (!$this->handleTags($notAllowedTags, $callback, function ($v) { |
|
14 | + if (!$this->handleTags($notAllowedTags, $callback, function($v) { |
|
15 | 15 | return isset(self::$tags[$v]); |
16 | 16 | })) { |
17 | - $this->allowedTags = self::$tags; |
|
17 | + $this->allowedTags=self::$tags; |
|
18 | 18 | } |
19 | - if (!$this->handleAttributes($notAllowedAttributes, $callback, function ($v) { |
|
19 | + if (!$this->handleAttributes($notAllowedAttributes, $callback, function($v) { |
|
20 | 20 | return isset(self::$attributes[$v]); |
21 | 21 | })) { |
22 | - $this->allowedAttributes = self::$attributes; |
|
22 | + $this->allowedAttributes=self::$attributes; |
|
23 | 23 | } |
24 | - $this->type = !is_string($type) || !in_array($tmp = strtolower(trim($type)), ['remove', 'replace']) ? 'remove' : $tmp; |
|
24 | + $this->type=!is_string($type)||!in_array($tmp=strtolower(trim($type)), ['remove', 'replace']) ? 'remove' : $tmp; |
|
25 | 25 | |
26 | - $bs = new BeforeStrip($html); |
|
27 | - $this->is_html = $bs->isHtml(); |
|
28 | - $this->is_php = $bs->isPHP(); |
|
29 | - $bs = new PrepareStrip($bs); |
|
26 | + $bs=new BeforeStrip($html); |
|
27 | + $this->is_html=$bs->isHtml(); |
|
28 | + $this->is_php=$bs->isPHP(); |
|
29 | + $bs=new PrepareStrip($bs); |
|
30 | 30 | $this->loadHTML($bs->getPrepared()); |
31 | 31 | } |
32 | 32 | |
33 | - public function go($type = self::TAGS) |
|
33 | + public function go($type=self::TAGS) |
|
34 | 34 | { |
35 | 35 | switch ($type) { |
36 | 36 | case self::TAGS_AND_ATTRIBUTES: |
@@ -53,10 +53,10 @@ discard block |
||
53 | 53 | |
54 | 54 | protected static function strip($domDoc, $type, $allowed_tags, $allowed_attrs, $is_php) |
55 | 55 | { |
56 | - $firstDiv = 0; |
|
57 | - if (count(self::$tags) > count($allowed_tags) || count(self::$attributes) > count($allowed_attrs)) { |
|
56 | + $firstDiv=0; |
|
57 | + if (count(self::$tags)>count($allowed_tags)||count(self::$attributes)>count($allowed_attrs)) { |
|
58 | 58 | foreach (new DOMNodeRecursiveIterator($domDoc->getElementsByTagName('*'))as $tag) { |
59 | - if (!isset($allowed_tags['<' . $tag->tagName . '>'])) { |
|
59 | + if (!isset($allowed_tags['<'.$tag->tagName.'>'])) { |
|
60 | 60 | if (self::skipUsefull($tag, $firstDiv)) { |
61 | 61 | continue; |
62 | 62 | } |
@@ -74,25 +74,25 @@ discard block |
||
74 | 74 | |
75 | 75 | protected static function skipUsefull($tag, &$firstDiv) |
76 | 76 | { |
77 | - if ($tag->tagName === 'div' && !$firstDiv) { |
|
77 | + if ($tag->tagName==='div'&&!$firstDiv) { |
|
78 | 78 | $firstDiv++; |
79 | 79 | |
80 | 80 | return true; |
81 | 81 | } |
82 | - if ($tag->tagName === 'doctypetag') { |
|
82 | + if ($tag->tagName==='doctypetag') { |
|
83 | 83 | return true; |
84 | 84 | } |
85 | 85 | } |
86 | 86 | |
87 | 87 | private static function replaceOrRemove($tag, $type) |
88 | 88 | { |
89 | - if ($type === 'remove') { |
|
89 | + if ($type==='remove') { |
|
90 | 90 | $tag->parentNode->removeChild($tag); |
91 | 91 | } else { |
92 | 92 | if (!in_array(strtolower($tag->tagName), ['php', 'style', 'script'])) { |
93 | - $elem = $tag->parentNode; |
|
94 | - $childNodes = $tag->childNodes; |
|
95 | - while ($childNodes->length > 0) { |
|
93 | + $elem=$tag->parentNode; |
|
94 | + $childNodes=$tag->childNodes; |
|
95 | + while ($childNodes->length>0) { |
|
96 | 96 | $elem->insertBefore($childNodes->item(0), $tag); |
97 | 97 | } |
98 | 98 | $elem->removeChild($tag); |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | |
105 | 105 | protected static function stripTagsWithAttributes($domDoc, &$allowed_tags, &$allowed_attrs, $is_php) |
106 | 106 | { |
107 | - if (count(self::$attributes) > count($allowed_attrs)) { |
|
107 | + if (count(self::$attributes)>count($allowed_attrs)) { |
|
108 | 108 | foreach (new DOMNodeRecursiveIterator($domDoc->getElementsByTagName('*'))as $tag) { |
109 | 109 | self::stripAttributes($tag, $allowed_attrs, 2); |
110 | 110 | } |
@@ -4,21 +4,21 @@ discard block |
||
4 | 4 | |
5 | 5 | abstract class HtmlStripHelper |
6 | 6 | { |
7 | - const TAGS = 3; |
|
8 | - const ATTRIBUTES = 4; |
|
9 | - const TAGS_AND_ATTRIBUTES = 1; |
|
10 | - const TAGS_WITH_ATTRIBUTES = 2; |
|
7 | + const TAGS=3; |
|
8 | + const ATTRIBUTES=4; |
|
9 | + const TAGS_AND_ATTRIBUTES=1; |
|
10 | + const TAGS_WITH_ATTRIBUTES=2; |
|
11 | 11 | |
12 | - protected $is_php = false; |
|
13 | - protected $is_html = false; |
|
14 | - protected $allowedTags = []; |
|
15 | - protected $allowedAttributes = []; |
|
16 | - protected $html = ''; |
|
12 | + protected $is_php=false; |
|
13 | + protected $is_html=false; |
|
14 | + protected $allowedTags=[]; |
|
15 | + protected $allowedAttributes=[]; |
|
16 | + protected $html=''; |
|
17 | 17 | protected $doctype; |
18 | 18 | protected $body; |
19 | 19 | protected $head; |
20 | 20 | protected $html_tag; |
21 | - protected static $events_attributes = [ |
|
21 | + protected static $events_attributes=[ |
|
22 | 22 | 'onabort' => 1, |
23 | 23 | 'onafterprint' => 1, |
24 | 24 | 'onbeforeprint' => 1, |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | 'onwheel' => 1, |
89 | 89 | ]; |
90 | 90 | |
91 | - protected static $attributes = [ |
|
91 | + protected static $attributes=[ |
|
92 | 92 | 'accept' => 1, |
93 | 93 | 'accesskey' => 1, |
94 | 94 | 'action' => 1, |
@@ -249,8 +249,8 @@ discard block |
||
249 | 249 | 'width' => 1, |
250 | 250 | 'wrap' => 1, |
251 | 251 | ]; |
252 | - protected static $special_tags = ['<doctypetag>' => '<!doctype>', '<htmltag>' => '<html>', '<headtag>' => '<head>', '<htmltag>' => '<html>', '<bodytag>' => '<body>']; |
|
253 | - protected static $tags = [ |
|
252 | + protected static $special_tags=['<doctypetag>' => '<!doctype>', '<htmltag>' => '<html>', '<headtag>' => '<head>', '<htmltag>' => '<html>', '<bodytag>' => '<body>']; |
|
253 | + protected static $tags=[ |
|
254 | 254 | '<php>' => 1, |
255 | 255 | '<!-- -->' => 1, |
256 | 256 | '<doctypetag>' => 1, |
@@ -383,13 +383,13 @@ discard block |
||
383 | 383 | if (!strlen($html)) { |
384 | 384 | throw new \InvalidArgumentException('Empty string given'); |
385 | 385 | } |
386 | - $xml = new \DOMDocument(); |
|
386 | + $xml=new \DOMDocument(); |
|
387 | 387 | //Suppress warnings: proper error handling is beyond scope of example |
388 | 388 | libxml_use_internal_errors(true); |
389 | 389 | |
390 | - $true = $xml->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); |
|
390 | + $true=$xml->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); |
|
391 | 391 | if ($true) { |
392 | - $this->html = $xml; |
|
392 | + $this->html=$xml; |
|
393 | 393 | } |
394 | 394 | } |
395 | 395 | |
@@ -398,19 +398,19 @@ discard block |
||
398 | 398 | if (!is_array($notAllowedTags)) { |
399 | 399 | return false; |
400 | 400 | } |
401 | - if (count($notAllowedTags) !== 2) { |
|
401 | + if (count($notAllowedTags)!==2) { |
|
402 | 402 | return false; |
403 | 403 | } |
404 | - $notAllowedTags = array_values($notAllowedTags); |
|
405 | - $keep = (bool)$notAllowedTags[1]; |
|
406 | - $notAllowedTags = $notAllowedTags[0]; |
|
404 | + $notAllowedTags=array_values($notAllowedTags); |
|
405 | + $keep=(bool)$notAllowedTags[1]; |
|
406 | + $notAllowedTags=$notAllowedTags[0]; |
|
407 | 407 | if (is_string($notAllowedTags)) { |
408 | - $notAllowedTags = explode(',', $notAllowedTags); |
|
408 | + $notAllowedTags=explode(',', $notAllowedTags); |
|
409 | 409 | } |
410 | 410 | if (is_array($notAllowedTags)) { |
411 | 411 | self::checkSpecialTags($notAllowedTags); |
412 | - $notAllowedTags = array_filter(array_map($callback, $notAllowedTags), $callback1); |
|
413 | - $this->allowedTags = !$keep ? array_fill_keys($notAllowedTags, 1) : array_diff_key(self::$tags, array_flip($notAllowedTags)); |
|
412 | + $notAllowedTags=array_filter(array_map($callback, $notAllowedTags), $callback1); |
|
413 | + $this->allowedTags=!$keep ? array_fill_keys($notAllowedTags, 1) : array_diff_key(self::$tags, array_flip($notAllowedTags)); |
|
414 | 414 | } else { |
415 | 415 | return false; |
416 | 416 | } |
@@ -421,8 +421,8 @@ discard block |
||
421 | 421 | protected static function checkSpecialTags(&$notAllowedTags) |
422 | 422 | { |
423 | 423 | foreach (self::$special_tags as $fakeTag => $trueTag) { |
424 | - if (false !== $key = array_search($trueTag, $notAllowedTags, true)) { |
|
425 | - $notAllowedTags[$key] = $fakeTag; |
|
424 | + if (false!==$key=array_search($trueTag, $notAllowedTags, true)) { |
|
425 | + $notAllowedTags[$key]=$fakeTag; |
|
426 | 426 | } |
427 | 427 | } |
428 | 428 | } |
@@ -432,17 +432,17 @@ discard block |
||
432 | 432 | if (!is_array($notAllowedAttributes)) { |
433 | 433 | return false; |
434 | 434 | } |
435 | - if (count($notAllowedAttributes) !== 2) { |
|
435 | + if (count($notAllowedAttributes)!==2) { |
|
436 | 436 | return false; |
437 | 437 | } |
438 | - $keep = (bool)$notAllowedAttributes[1]; |
|
439 | - $notAllowedAttributes = $notAllowedAttributes[0]; |
|
438 | + $keep=(bool)$notAllowedAttributes[1]; |
|
439 | + $notAllowedAttributes=$notAllowedAttributes[0]; |
|
440 | 440 | if (is_string($notAllowedAttributes)) { |
441 | - $notAllowedAttributes = explode(',', $notAllowedAttributes); |
|
441 | + $notAllowedAttributes=explode(',', $notAllowedAttributes); |
|
442 | 442 | } |
443 | 443 | if (is_array($notAllowedAttributes)) { |
444 | - $notAllowedAttributes = array_filter(array_map($callback, $notAllowedAttributes), $callback2); |
|
445 | - $this->allowedAttributes = !$keep ? array_fill_keys($notAllowedAttributes, 1) : array_diff_key(self::$attributes, array_flip($notAllowedAttributes)); |
|
444 | + $notAllowedAttributes=array_filter(array_map($callback, $notAllowedAttributes), $callback2); |
|
445 | + $this->allowedAttributes=!$keep ? array_fill_keys($notAllowedAttributes, 1) : array_diff_key(self::$attributes, array_flip($notAllowedAttributes)); |
|
446 | 446 | } else { |
447 | 447 | return false; |
448 | 448 | } |
@@ -452,41 +452,41 @@ discard block |
||
452 | 452 | |
453 | 453 | protected static function handlePhp($is_php, $domDoc, &$allowed_tags) |
454 | 454 | { |
455 | - $result = $domDoc->saveHTML(); |
|
455 | + $result=$domDoc->saveHTML(); |
|
456 | 456 | self::handleMainHtmlTags($result, $allowed_tags); |
457 | 457 | |
458 | - return substr(($is_php && isset($allowed_tags['<php>'])) ? |
|
459 | - str_replace(['<php>', '</php>'], ['<?php ', '?>'], $result) : $result, stripos($result, '<div>') + 5, -7); |
|
458 | + return substr(($is_php&&isset($allowed_tags['<php>'])) ? |
|
459 | + str_replace(['<php>', '</php>'], ['<?php ', '?>'], $result) : $result, stripos($result, '<div>')+5, -7); |
|
460 | 460 | } |
461 | 461 | |
462 | 462 | protected static function handleMainHtmlTags(&$result, &$allowed_tags) |
463 | 463 | { |
464 | - $result = str_replace( |
|
464 | + $result=str_replace( |
|
465 | 465 | ['<doctypetag', '</doctypetag>', '<headtag', '</headtag', '<htmltag', '</htmltag', '<bodytag', '</bodytag'], |
466 | 466 | ['<!doctype', '', '<head', '</head', '<html', '</html', '<body', '</body'], |
467 | 467 | $result |
468 | 468 | ); |
469 | 469 | if (!isset($allowed_tags['<doctypetag>'])) { |
470 | - $doctypeOffset = stripos($result, '<!doctype'); |
|
471 | - $result = str_replace(substr($result, $doctypeOffset, strpos($result, '>', $doctypeOffset) + 1 - $doctypeOffset), '', $result); |
|
470 | + $doctypeOffset=stripos($result, '<!doctype'); |
|
471 | + $result=str_replace(substr($result, $doctypeOffset, strpos($result, '>', $doctypeOffset)+1-$doctypeOffset), '', $result); |
|
472 | 472 | } |
473 | 473 | } |
474 | 474 | |
475 | 475 | protected static function handleComments($domDoc, &$allowed_tags) |
476 | 476 | { |
477 | 477 | if (!isset($allowed_tags['<!-- -->'])) { |
478 | - $xpath = new \DOMXPath($domDoc); |
|
479 | - $DomComments = $xpath->query('//comment()'); |
|
478 | + $xpath=new \DOMXPath($domDoc); |
|
479 | + $DomComments=$xpath->query('//comment()'); |
|
480 | 480 | foreach ($DomComments as $DomComment) { |
481 | 481 | $DomComment->parentNode->removeChild($DomComment); |
482 | 482 | } |
483 | 483 | } |
484 | 484 | } |
485 | 485 | |
486 | - protected static function stripAttributes($tag, &$allowed_attrs, $type = 1) |
|
486 | + protected static function stripAttributes($tag, &$allowed_attrs, $type=1) |
|
487 | 487 | { |
488 | 488 | if ($tag instanceof \DOMElement) { |
489 | - if ($type === 2) { |
|
489 | + if ($type===2) { |
|
490 | 490 | self:: stripAttributesTypeTwo($tag, $allowed_attrs); |
491 | 491 | } else { |
492 | 492 | self::stripAttributesTypeOne($tag, $allowed_attrs); |