From 96909d1be1c3dc5737363c2d09e4f718a0feec85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Mon, 2 Nov 2020 20:19:21 +0100 Subject: [PATCH 1/3] Update composer.json --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4fa51f7..c14daad 100644 --- a/composer.json +++ b/composer.json @@ -15,10 +15,10 @@ "require": { "php": "^7.2", "cakephp/cakephp": "^4.0", - "ezyang/htmlpurifier": "*", - "cakephp/cakephp-codesniffer": "^3.0" + "ezyang/htmlpurifier": "*" }, "require-dev": { + "cakephp/cakephp-codesniffer": "^3.0", "phpunit/phpunit": "~8.5.0" }, "autoload": { From af6ac5fa89a3a42be8d2cc739a4bf5e4f2708c62 Mon Sep 17 00:00:00 2001 From: Hache_raw Date: Sun, 17 Jan 2021 03:43:46 +0000 Subject: [PATCH 2/3] CakePHP 4.x fixes Shell wasn't working because of deprecations. After making it work again, I had to add a `patchEntity()`method because beforeMarshall Events wasn't firing (It is fired before request data is converted into entities) Note that `Shells` are deprecated since 3.6 and will be removed on 5.x in favor of `Commands` Edited a typo in the docs, added some lines and created Installation.md since there was a link to it but the file did not exist. --- composer.json | 2 +- docs/Configuration.md | 15 +++++++++++---- docs/Installation.md | 7 +++++++ docs/Usage.md | 10 +++++----- src/Shell/PurifierShell.php | 13 ++++++++----- 5 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 docs/Installation.md diff --git a/composer.json b/composer.json index c14daad..4b6a022 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "ezyang/htmlpurifier": "*" }, "require-dev": { - "cakephp/cakephp-codesniffer": "^3.0", + "cakephp/cakephp-codesniffer": "~4.2.0", "phpunit/phpunit": "~8.5.0" }, "autoload": { diff --git a/docs/Configuration.md b/docs/Configuration.md index 3c414b0..a8ad8a3 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -4,15 +4,20 @@ Important: Before you start declaring a configuration you should lookup how HTML Purifier can be configured. http://htmlpurifier.org/docs +In `src/Application.php` add: +```php +$this->addPlugin('Burzum/HtmlPurifier'); +``` + In `config/boostrap.php` you can either set the purifier config as an array or pass a native config object. The array style would look like this: ```php -Purifier::config('ConfigName', array( +Purifier::config('ConfigName', [ 'HTML.AllowedElements' => 'a, em, blockquote, p, strong, pre, code, span,ul,ol,li,img', 'HTML.AllowedAttributes' => 'a.href, a.title, img.src, img.alt' - ) + ] ); ``` @@ -48,6 +53,8 @@ or clean some dirty HTML directly by calling Purifier::clean($markup, 'ConfigName'); ``` +*Remember to add `use Burzum\HtmlPurifier\Lib\Purifier;` when ussing `Purifier` class* + For some automatization you can also use the Behavior or Helper. ## Caching ### @@ -55,9 +62,9 @@ For some automatization you can also use the Behavior or Helper. It is recommended to change the path of the purifier libs cache to your `tmp` folder. For example: ```php -Purifier::config('ConfigName', array( +Purifier::config('ConfigName', [ 'Cache.SerializerPath' => ROOT . DS . 'tmp' . DS . 'purifier', - ) + ] ); ``` diff --git a/docs/Installation.md b/docs/Installation.md new file mode 100644 index 0000000..582a8e7 --- /dev/null +++ b/docs/Installation.md @@ -0,0 +1,7 @@ +# Installation + +## Installation with composer (CakePHP 4) + +```bash +composer require burzum/cakephp-html-purifier:4.0.x-dev +``` diff --git a/docs/Usage.md b/docs/Usage.md index 426aec9..c39d81c 100644 --- a/docs/Usage.md +++ b/docs/Usage.md @@ -6,12 +6,12 @@ Set a config you want to use and the fields you want to sanitize. ```php public $actsAs = array( - 'Burzum/HtmlPurifier.HtmlPurifier' => array( - 'config' => 'ConfigName', - 'fields' => array( + 'Burzum/HtmlPurifier.HtmlPurifier' => [ + 'purifierConfig' => 'ConfigName', + 'fields' => [ 'body', 'excerpt' - ) - ) + ] + ] ); ``` diff --git a/src/Shell/PurifierShell.php b/src/Shell/PurifierShell.php index 1a5456c..55068cd 100644 --- a/src/Shell/PurifierShell.php +++ b/src/Shell/PurifierShell.php @@ -8,9 +8,11 @@ */ namespace Burzum\HtmlPurifier\Shell; +use Cake\Console\ConsoleOptionParser; use Cake\Console\Shell; use Cake\ORM\Locator\TableLocator; use Cake\ORM\Table; +use Cake\Datasource\EntityInterface; /** * PurifierShell @@ -31,7 +33,7 @@ public function main() /** * Gets the table from the shell args. * - * @return \Cake\ORM\Table; + * @return \Cake\ORM\Table */ protected function _getTable() { @@ -67,7 +69,7 @@ protected function _getFields(Table $table) /** * Loads the purifier behavior for the given table if not already attached. * - * @param \Cake\ORM\Table $table Table object. + * @param \Cake\ORM\Table $table Table object. * @param array Set of fields to sanitize * @return void */ @@ -100,7 +102,7 @@ public function purify() } $total = $query->all()->count(); - $this->info(__d('Burzum/HtmlPurifier', 'Sanitizing fields `{0}` in table `{1}`', implode(',', $fields), $table->table())); + $this->info(__d('Burzum/HtmlPurifier', 'Sanitizing fields `{0}` in table `{1}`', implode(',', $fields), $table->getTable())); $this->helper('progress')->output( [ @@ -142,7 +144,7 @@ protected function _process(Table $table, $chunkCount, $chunkSize, $fields) ->select($fields) ->offset($chunkCount) ->limit($chunkSize) - ->orderDesc($table->aliasField($table->primarygetPrimaryKey())) + ->orderDesc($table->aliasField($table->getPrimaryKey())) ->all(); if (empty($results)) { @@ -151,6 +153,7 @@ protected function _process(Table $table, $chunkCount, $chunkSize, $fields) foreach ($results as $result) { try { + $table->patchEntity($result, $result->toArray()); $table->save($result); $chunkCount++; } catch (\Exception $e) { @@ -162,7 +165,7 @@ protected function _process(Table $table, $chunkCount, $chunkSize, $fields) /** * {@inheritDoc} */ - public function getOptionParser() + public function getOptionParser(): ConsoleOptionParser { $parser = parent::getOptionParser(); From 0400127c8b9e47e701bf71d0c294136b5ad155b7 Mon Sep 17 00:00:00 2001 From: Hache_raw Date: Sun, 24 Jan 2021 14:20:51 +0000 Subject: [PATCH 3/3] identation fix --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 4b6a022..8bb7e01 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "ezyang/htmlpurifier": "*" }, "require-dev": { - "cakephp/cakephp-codesniffer": "~4.2.0", + "cakephp/cakephp-codesniffer": "~4.2.0", "phpunit/phpunit": "~8.5.0" }, "autoload": {