|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mews\Purifier; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Laravel 5 HTMLPurifier package |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) 2015 MeWebStudio |
|
9
|
|
|
* @version 2.0.0 |
|
10
|
|
|
* @author Muharrem ERİN |
|
11
|
|
|
* @contact [email protected] |
|
12
|
|
|
* @web http://www.mewebstudio.com |
|
13
|
|
|
* @date 2014-04-02 |
|
14
|
|
|
* @license MIT |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
use Exception; |
|
18
|
|
|
use HTMLPurifier; |
|
19
|
|
|
use HTMLPurifier_Config; |
|
20
|
|
|
use Illuminate\Config\Repository; |
|
21
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
22
|
|
|
|
|
23
|
|
|
class Purifier |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Filesystem |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $files; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Repository |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $config; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var HTMLPurifier |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $purifier; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructor |
|
43
|
|
|
* |
|
44
|
|
|
* @param Filesystem $files |
|
45
|
|
|
* @param Repository $config |
|
46
|
|
|
* @throws Exception |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(Filesystem $files, Repository $config) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->files = $files; |
|
51
|
|
|
$this->config = $config; |
|
52
|
|
|
|
|
53
|
|
|
$this->setUp(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Setup |
|
58
|
|
|
* |
|
59
|
|
|
* @throws Exception |
|
60
|
|
|
*/ |
|
61
|
|
|
private function setUp() |
|
62
|
|
|
{ |
|
63
|
|
|
if (!$this->config->has('purifier')) { |
|
64
|
|
|
throw new Exception('Configuration parameters not loaded!'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->checkCacheDirectory(); |
|
68
|
|
|
|
|
69
|
|
|
// Create a new configuration object |
|
70
|
|
|
$config = HTMLPurifier_Config::createDefault(); |
|
71
|
|
|
|
|
72
|
|
|
// Allow configuration to be modified |
|
73
|
|
|
if (!$this->config->get('purifier.finalize')) { |
|
74
|
|
|
$config->autoFinalize = false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$config->loadArray($this->getConfig()); |
|
78
|
|
|
|
|
79
|
|
|
// Create HTMLPurifier object |
|
80
|
|
|
$this->purifier = new HTMLPurifier($this->configure($config)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Check/Create cache directory |
|
85
|
|
|
*/ |
|
86
|
|
|
private function checkCacheDirectory() |
|
87
|
|
|
{ |
|
88
|
|
|
$cachePath = $this->config->get('purifier.cachePath'); |
|
89
|
|
|
|
|
90
|
|
|
if ($cachePath) { |
|
91
|
|
|
if (!$this->files->isDirectory($cachePath)) { |
|
92
|
|
|
$this->files->makeDirectory($cachePath, $this->config->get('purifier.cacheFileMode', 0755)); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param HTMLPurifier_Config $config |
|
99
|
|
|
* |
|
100
|
|
|
* @return HTMLPurifier_Config |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function configure(HTMLPurifier_Config $config) |
|
103
|
|
|
{ |
|
104
|
|
|
return HTMLPurifier_Config::inherit($config); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param null $config |
|
109
|
|
|
* |
|
110
|
|
|
* @return mixed|null |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function getConfig($config = null) |
|
113
|
|
|
{ |
|
114
|
|
|
$default_config = []; |
|
115
|
|
|
$default_config['Core.Encoding'] = $this->config->get('purifier.encoding'); |
|
116
|
|
|
$default_config['Cache.SerializerPath'] = $this->config->get('purifier.cachePath'); |
|
117
|
|
|
$default_config['Cache.SerializerPermissions'] = $this->config->get('purifier.cacheFileMode', 0755); |
|
118
|
|
|
|
|
119
|
|
|
if (!$config) { |
|
120
|
|
|
$config = $this->config->get('purifier.settings.default'); |
|
121
|
|
|
} elseif (is_string($config)) { |
|
122
|
|
|
$config = $this->config->get('purifier.settings.'.$config); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (!is_array($config)) { |
|
126
|
|
|
$config = []; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$config = $default_config + $config; |
|
130
|
|
|
|
|
131
|
|
|
return $config; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param $dirty |
|
136
|
|
|
* @param null $config |
|
137
|
|
|
* |
|
138
|
|
|
* @return mixed |
|
139
|
|
|
*/ |
|
140
|
|
|
public function clean($dirty, $config = null) |
|
141
|
|
|
{ |
|
142
|
|
|
if (is_array($dirty)) { |
|
143
|
|
|
return array_map(function ($item) use ($config) { |
|
144
|
|
|
return $this->clean($item, $config); |
|
145
|
|
|
}, $dirty); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $this->purifier->purify($dirty, $this->getConfig($config)); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get HTMLPurifier instance. |
|
153
|
|
|
* |
|
154
|
|
|
* @return \HTMLPurifier |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getInstance() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->purifier; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: