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