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

Commit

Permalink
Merge pull request #36 from burzum/4.0
Browse files Browse the repository at this point in the history
4.0
  • Loading branch information
burzum authored Jan 24, 2021
2 parents 2960837 + 9aaa4bf commit a289e9a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "~4.2.0",
"phpunit/phpunit": "~8.5.0"
},
"autoload": {
Expand Down
15 changes: 11 additions & 4 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
]
);
```

Expand Down Expand Up @@ -48,16 +53,18 @@ 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 ###

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',
)
]
);
```

Expand Down
7 changes: 7 additions & 0 deletions docs/Installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Installation

## Installation with composer (CakePHP 4)

```bash
composer require burzum/cakephp-html-purifier:4.0.x-dev
```
10 changes: 5 additions & 5 deletions docs/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
)
]
]
);
```

Expand Down
13 changes: 8 additions & 5 deletions src/Shell/PurifierShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
{
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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(
[
Expand Down Expand Up @@ -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)) {
Expand All @@ -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) {
Expand All @@ -162,7 +165,7 @@ protected function _process(Table $table, $chunkCount, $chunkSize, $fields)
/**
* {@inheritDoc}
*/
public function getOptionParser()
public function getOptionParser(): ConsoleOptionParser
{
$parser = parent::getOptionParser();

Expand Down

0 comments on commit a289e9a

Please sign in to comment.