1
|
|
|
<?php namespace Anomaly\Streams\Platform\Support; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class Purifier |
5
|
|
|
* |
6
|
|
|
* @link http://pyrocms.com/ |
7
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
8
|
|
|
* @author Ryan Thompson <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class Purifier extends \HTMLPurifier |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Create a new Purifier instance. |
15
|
|
|
* |
16
|
|
|
* @param null $config |
17
|
|
|
*/ |
18
|
|
|
public function __construct($config = null) |
19
|
|
|
{ |
20
|
|
|
parent::__construct($config); |
21
|
|
|
|
22
|
|
|
$cache = app_storage_path('support/purifier'); |
23
|
|
|
|
24
|
|
|
if (!is_dir($cache)) { |
25
|
|
|
mkdir($cache, 0777, true); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$this->config->set('Cache.SerializerPath', $cache); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Return purified HTML. |
33
|
|
|
* |
34
|
|
|
* @param string $html |
35
|
|
|
* @param null $config |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function purify($html, $config = null) |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* Replace <pre> and <code> blocks |
42
|
|
|
* that are complete with placeholders. |
43
|
|
|
*/ |
44
|
|
|
preg_match_all("/\<pre\>(.+?)\<\/pre\>/s", $html, $pres, PREG_PATTERN_ORDER); |
45
|
|
|
preg_match_all("/\<code\>(.+?)\<\/code\>/s", $html, $codes, PREG_PATTERN_ORDER); |
46
|
|
|
|
47
|
|
|
$html = preg_replace("/\<pre\>(.+?)\<\/pre\>/s", "PRE_PLACEHOLDER", $html); |
48
|
|
|
$html = preg_replace("/\<code\>(.+?)\<\/code\>/s", "CODE_PLACEHOLDER", $html); |
49
|
|
|
|
50
|
|
|
// Purify! |
51
|
|
|
$html = parent::purify($html, $config); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Replace the placeholders with the |
55
|
|
|
* complete <pre> and <code> blocks. |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
if (isset($pres[0])) { |
|
|
|
|
58
|
|
|
foreach ($pres[0] as $pre) { |
59
|
|
|
$html = preg_replace('/PRE_PLACEHOLDER/', $pre, $html, 1); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
if (isset($codes[0])) { |
|
|
|
|
64
|
|
|
foreach ($codes[0] as $code) { |
65
|
|
|
$html = preg_replace('/CODE_PLACEHOLDER/', $code, $html, 1); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $html; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.