Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 30da811

Browse files
author
Florian Krämer
committed
Working on the 3.0 version
1 parent 76160d1 commit 30da811

File tree

7 files changed

+177
-156
lines changed

7 files changed

+177
-156
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
._*
44
Icon
55

6-
Vendor/HtmlPurifier/library/HTMLPurifier/DefinitionCache/Serializer/
6+
Vendor/*

src/Lib/Purifier.php

Lines changed: 87 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -5,100 +5,105 @@
55
* Purifier
66
*
77
* @author Florian Krämer
8-
* @copyright 2012 Florian Krämer
8+
* @copyright 2012 - 2015 Florian Krämer
99
* @license MIT
1010
*/
11-
class Purifier extends Object {
12-
/**
13-
* Purifier configurations
14-
*
15-
* @var array
16-
*/
17-
protected $_configs = array();
11+
class Purifier {
1812

19-
/**
20-
* HTMLPurifier instances
21-
*
22-
* @var array
23-
*/
24-
protected $_instances = array();
13+
/**
14+
* Purifier configurations
15+
*
16+
* @var array
17+
*/
18+
protected $_configs = array();
2519

26-
/**
27-
* Return a singleton instance of the StorageManager.
28-
*
29-
* @return ClassRegistry instance
30-
*/
31-
public static function &getInstance() {
32-
static $instance = array();
33-
if (!$instance) {
34-
$instance[0] = new Purifier();
35-
}
36-
return $instance[0];
37-
}
20+
/**
21+
* HTMLPurifier instances
22+
*
23+
* @var array
24+
*/
25+
protected $_instances = array();
3826

39-
/**
40-
* Config
41-
*
42-
* @param string $markup
43-
* @param string $configName
44-
*/
45-
public static function config($configName, $config = null) {
46-
$_this = Purifier::getInstance();
27+
/**
28+
* Return a singleton instance of the StorageManager.
29+
*
30+
* @return ClassRegistry instance
31+
*/
32+
public static function &getInstance()
33+
{
34+
static $instance = array();
35+
if (!$instance) {
36+
$instance[0] = new Purifier();
37+
}
38+
return $instance[0];
39+
}
4740

48-
if (empty($config)) {
49-
if (!isset($_this->_configs[$configName])) {
50-
throw new InvalidArgumentException(__('Configuration %s does not exist!', $configName));
51-
}
52-
return $_this->_configs[$configName];
53-
}
41+
/**
42+
* Config
43+
*
44+
* @param string $configName
45+
* @param string $config
46+
* @throws \InvalidArgumentException
47+
*/
48+
public static function config($configName, $config = null)
49+
{
50+
$_this = Purifier::getInstance();
5451

55-
if (is_array($config)) {
56-
$purifierConfig = HTMLPurifier_Config::createDefault();
57-
foreach ($config as $key => $value) {
58-
$purifierConfig->set($key, $value);
59-
}
52+
if (empty($config)) {
53+
if (!isset($_this->_configs[$configName])) {
54+
throw new \InvalidArgumentException(sprintf('Purifier configuration `%s` does not exist!', $configName));
55+
}
56+
return $_this->_configs[$configName];
57+
}
6058

61-
return $_this->_configs[$configName] = $purifierConfig;
62-
} elseif (is_object($config) && is_a($config, 'HTMLPurifier_Config')) {
63-
return $_this->_configs[$configName] = $config;
64-
} else {
65-
throw new InvalidArgumentException('Invalid config passed');
66-
}
67-
}
59+
if (is_array($config)) {
60+
$purifierConfig = \HTMLPurifier_Config::createDefault();
61+
foreach ($config as $key => $value) {
62+
$purifierConfig->set($key, $value);
63+
}
6864

69-
/**
70-
* Gets an instance of the purifier lib only when needed, lazy loading it
71-
*
72-
* @param string $configName
73-
* @return HTMLPurifier
74-
*/
75-
public static function getPurifierInstance($configName = null) {
76-
$_this = Purifier::getInstance();
65+
return $_this->_configs[$configName] = $purifierConfig;
66+
} elseif (is_object($config) && is_a($config, 'HTMLPurifier_Config')) {
67+
return $_this->_configs[$configName] = $config;
68+
} else {
69+
throw new \InvalidArgumentException('Invalid purifier config passed!');
70+
}
71+
}
7772

78-
if (!isset($_this->_instances[$configName])) {
79-
if (!isset($_this->_configs[$configName])) {
80-
throw new InvalidArgumentException(__('Configuration and instance %s does not exist!', $name));
81-
}
82-
$_this->_instances[$configName] = new HTMLPurifier($_this->_configs[$configName]);
83-
}
73+
/**
74+
* Gets an instance of the purifier lib only when needed, lazy loading it
75+
*
76+
* @param string $configName
77+
* @return HTMLPurifier
78+
*/
79+
public static function getPurifierInstance($configName = null)
80+
{
81+
$_this = Purifier::getInstance();
8482

85-
return $_this->_instances[$configName];
86-
}
83+
if (!isset($_this->_instances[$configName])) {
84+
if (!isset($_this->_configs[$configName])) {
85+
throw new \InvalidArgumentException(sprintf('Configuration and instance `%s` does not exist!', $configName));
86+
}
87+
$_this->_instances[$configName] = new \HTMLPurifier($_this->_configs[$configName]);
88+
}
8789

88-
/**
89-
* Cleans Markup using a given config
90-
*
91-
* @param string $markup
92-
* @param string $configName
93-
*/
94-
public static function clean($markup, $configName = null) {
95-
$_this = Purifier::getInstance();
90+
return $_this->_instances[$configName];
91+
}
9692

97-
if (!isset($_this->_configs[$configName])) {
98-
throw new InvalidArgumentException(__('Invalid configuration %s!', $configName));
99-
}
93+
/**
94+
* Cleans Markup using a given config
95+
*
96+
* @param string $markup
97+
* @param string $configName
98+
*/
99+
public static function clean($markup, $configName = null)
100+
{
101+
$_this = Purifier::getInstance();
100102

101-
return $_this->getPurifierInstance($configName)->purify($markup);
102-
}
103+
if (!isset($_this->_configs[$configName])) {
104+
throw new \InvalidArgumentException(sprintf('Invalid configuration %s!', $configName));
105+
}
103106

104-
}
107+
return $_this->getPurifierInstance($configName)->purify($markup);
108+
}
109+
}

src/Lib/PurifierTrait.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
trait PurifierTrait {
55

6-
/**
7-
* Cleans markup
8-
*
9-
* @param string $markup
10-
* @param string $config
11-
*/
12-
public function purifyHtml($markup, $config) {
13-
return Purifier::clean($markup, $config);
14-
}
15-
}
6+
/**
7+
* Cleans markup
8+
*
9+
* @param string $markup
10+
* @param string $config
11+
*/
12+
public function purifyHtml($markup, $config = '')
13+
{
14+
return Purifier::clean($markup, $config);
15+
}
16+
}
Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,67 @@
11
<?php
2-
namespace Burzum\HtmlPurifier\ModelBehavior;
2+
namespace App\Model\Behavior;
33

4-
use Cake\ORM\Behavior;
4+
use ArrayObject;
55
use Burzum\HtmlPurifier\Lib\PurifierTrait;
6+
use Cake\Core\Configure;
7+
use Cake\Event\Event;
8+
use Cake\ORM\Behavior;
9+
use HTMLPurifier;
10+
use HTMLPurifier_Config;
611

7-
/**
8-
* HtmlPurifierBehavior
9-
*
10-
* @author Florian Krämer
11-
* @copyright 2012 Florian Krämer
12-
* @license MIT
13-
*/
1412
class HtmlPurifierBehavior extends Behavior {
1513

16-
use PurifierTrait;
14+
use PurifierTrait;
15+
16+
/**
17+
* Default config
18+
*
19+
* @var array
20+
*/
21+
protected $_defaultConfig = [
22+
'fields' => [],
23+
'purifierConfig' => 'default',
24+
'implementedEvents' => [
25+
'Model.beforeMarshal' => 'beforeMarshal',
26+
],
27+
'implementedMethods' => [
28+
'purifyHtml' => 'purifyHtml'
29+
]
30+
];
31+
32+
protected $_purifier;
33+
34+
/**
35+
* Before marshal callaback
36+
*
37+
* @param \Cake\Event\Event $event The Model.beforeMarshal event.
38+
* @param \ArrayObject $data Data.
39+
* @param \ArrayObject $options Options.
40+
* @return void
41+
*/
42+
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
43+
{
44+
foreach ($this->config('fields') as $field) {
45+
if (isset($data[$field])) {
46+
$data[$field] = $this->purifyHtml($data[$field], $this->config('purifierConfig'));
47+
}
48+
}
49+
}
1750

18-
/**
19-
* Setup
20-
*
21-
* @param Model $Model
22-
* @param array $settings
23-
*/
24-
public function setup(Model $Model, $settings = array()) {
25-
$this->settings[$Model->alias] = (array)$settings;
26-
}
27-
/**
28-
* beforeSave
29-
*
30-
* @param Model $Model
31-
* @param array $options
32-
* @return boolean
33-
*/
34-
public function beforeSave(Event $event) {
35-
foreach($fields as $field) {
36-
if (isset($Model->data[$Model->alias][$field])) {
37-
$Model->data[$Model->alias][$field] = $this->purifyHtml($Model, $Model->data[$Model->alias][$field], $config);
38-
}
39-
}
40-
}
51+
public function purifier($config = null)
52+
{
53+
// if (is_string($config)) {
54+
// $config = (array)Configure::read('HtmlPurifier.' . $config);
55+
// }
56+
// if (!is_array($config)) {
57+
// throw new \InvalidArgumentException('Invalid purifier config value passed!');
58+
// }
59+
// $hash = md5(serialize($config));
60+
// if (!empty($this->_purifier[$hash])) {
61+
// return $this->_purifier[$hash];
62+
// }
63+
// $config = \HTMLPurifier_Config::create($config);
64+
// $this->_purifier[$hash] = new \HTMLPurifier($config);
65+
// return $this->_purifier[$hash];
66+
}
4167
}

src/View/Helper/HtmlPurifierHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22
namespace Burzum\HtmlPurifier\View\Helper;
33

4+
use Burzum\HtmlPurifier\Lib\Purifier;
45
use Cake\View\Helper;
56

67
/**
78
* HtmlPurifierHelper
89
*
910
* @author Florian Krämer
10-
* @copyright 2012 Florian Krämer
11+
* @copyright 2012 - 2015 Florian Krämer
1112
* @license MIT
1213
*/
1314
class HtmlPurifierHelper extends Helper {

tests/TestCase/Case/AllHtmlPurifierTest.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)