Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mewebstudio/Purifier
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3.1.1
Choose a base ref
...
head repository: mewebstudio/Purifier
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
Showing with 469 additions and 144 deletions.
  1. +3 −0 .gitignore
  2. +0 −23 .scrutinizer.yml
  3. +11 −3 .travis.yml
  4. +94 −49 README.md
  5. +52 −45 composer.json
  6. +5 −4 config/purifier.php
  7. +28 −0 phpunit.xml
  8. +38 −0 src/Casts/CleanHtml.php
  9. +38 −0 src/Casts/CleanHtmlInput.php
  10. +38 −0 src/Casts/CleanHtmlOutput.php
  11. +16 −0 src/Casts/WithConfig.php
  12. +1 −0 src/Facades/Purifier.php
  13. +45 −9 src/Purifier.php
  14. +1 −1 tests/AbstractTestCase.php
  15. +99 −10 tests/PurifierTest.php
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -12,3 +12,6 @@ Vagrantfile
Homestead.yaml
after.sh
aliases

# Tests
.phpunit.result.cache
23 changes: 0 additions & 23 deletions .scrutinizer.yml

This file was deleted.

14 changes: 11 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -3,6 +3,14 @@ language: php
php:
- 7.2
- 7.3
- 7.4
- 8.0
- nightly

matrix:
fast_finish: true
allow_failures:
- php: nightly

# This triggers builds to run on the new TravisCI infrastructure.
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
@@ -15,10 +23,10 @@ cache:

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev
- COMPOSER_MEMORY_LIMIT=-1 travis_retry composer install --prefer-source --no-interaction --dev

script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
- vendor/bin/phpunit --coverage-clover=coverage.xml

after_script:
- php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover
- bash <(curl -s https://codecov.io/bash)
143 changes: 94 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
# HTMLPurifier for Laravel 5 & 6
# HTMLPurifier for Laravel 5/6/7/8/9/10/11

[![Build Status](https://scrutinizer-ci.com/g/mewebstudio/Purifier/badges/build.png?b=master)](https://scrutinizer-ci.com/g/mewebstudio/Purifier/build-status/master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mewebstudio/Purifier/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mewebstudio/Purifier/?branch=master)
[![Build Status](https://travis-ci.org/mewebstudio/Purifier.svg?branch=master)](https://travis-ci.org/github/mewebstudio/Purifier)
[![codecov](https://codecov.io/gh/mewebstudio/Purifier/branch/master/graph/badge.svg)](https://codecov.io/gh/mewebstudio/Purifier)
[![Latest Stable Version](https://poser.pugx.org/mews/Purifier/v/stable.svg)](https://packagist.org/packages/mews/Purifier)
[![Latest Unstable Version](https://poser.pugx.org/mews/Purifier/v/unstable.svg)](https://packagist.org/packages/mews/Purifier)
[![License](https://poser.pugx.org/mews/Purifier/license.svg)](https://packagist.org/packages/mews/Purifier)
[![Total Downloads](https://poser.pugx.org/mews/Purifier/downloads.svg)](https://packagist.org/packages/mews/Purifier)

A simple [Laravel 5 & 6](http://www.laravel.com/) service provider for including the [HTMLPurifier for Laravel](https://github.com/mewebstudio/purifier).
A simple [Laravel](http://www.laravel.com/) service provider for easily using [HTMLPurifier](http://htmlpurifier.org/) inside Laravel. From their website:

for Laravel 4 [HTMLPurifier for Laravel 4](https://github.com/mewebstudio/Purifier/tree/master-l4)
> HTML Purifier is a standards-compliant HTML filter library written in PHP. HTML Purifier will not only remove all malicious code (better known as XSS) with a thoroughly audited, secure yet permissive whitelist, it will also make sure your documents are standards compliant, something only achievable with a comprehensive knowledge of W3C's specifications. Tired of using BBCode due to the current landscape of deficient or insecure HTML filters? Have a WYSIWYG editor but never been able to use it? Looking for high-quality, standards-compliant, open-source components for that application you're building? HTML Purifier is for you!
This package can be installed via [Composer](http://getcomposer.org) by
requiring the `mews/purifier` package in your project's `composer.json`:
## Installation

```json
{
"require": {
"laravel/framework": "~5.0",
"mews/purifier": "~2.0",
}
}
```

or
### For Laravel 5.5+

Require this package with composer:
```
composer require mews/purifier
```

Update your packages with `composer update` or install with `composer install`.
The service provider will be auto-discovered. You do not need to add the provider anywhere.

## Usage
### For Laravel 5.0 to 5.4

To use the HTMLPurifier Service Provider, you must register the provider when bootstrapping your Laravel application. There are
essentially two ways to do this.
Require this package with composer:
```
composer require mews/purifier
```

# For Laravel 5.0 to 5.4:
Find the `providers` key in `config/app.php` and register the HTMLPurifier Service Provider.

```php
@@ -46,7 +38,7 @@ Find the `providers` key in `config/app.php` and register the HTMLPurifier Servi
]
```

Find the `aliases` key in `app/config/app.php`.
Find the `aliases` key in `config/app.php` and register the Purifier alias.

```php
'aliases' => [
@@ -55,24 +47,86 @@ Find the `aliases` key in `app/config/app.php`.
]
```

# For Laravel 5.5+:
The service provider will be auto-discovered. You do not need to add the provider anywhere.
### For Laravel 4

Check out [HTMLPurifier for Laravel 4](https://github.com/mewebstudio/Purifier/tree/master-l4)


## Usage


Use these methods inside your requests or middleware, wherever you need the HTML cleaned up:

```php
clean(Input::get('inputname'));
```
or

```php
Purifier::clean(Input::get('inputname'));
```

dynamic config
```php
clean('This is my H1 title', 'titles');
clean('This is my H1 title', array('Attr.EnableID' => true));
```
or

```php
Purifier::clean('This is my H1 title', 'titles');
Purifier::clean('This is my H1 title', array('Attr.EnableID' => true));
```

use [URI filter](http://htmlpurifier.org/docs/enduser-uri-filter.html)

```php
Purifier::clean('This is my H1 title', 'titles', function (HTMLPurifier_Config $config) {
$uri = $config->getDefinition('URI');
$uri->addFilter(new HTMLPurifier_URIFilter_NameOfFilter(), $config);
});
```

Alternatively, in Laravel 7+, if you're looking to clean your HTML inside your Eloquent models, you can use our custom casts:

```php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Mews\Purifier\Casts\CleanHtml;
use Mews\Purifier\Casts\CleanHtmlInput;
use Mews\Purifier\Casts\CleanHtmlOutput;

class Monster extends Model
{
protected $casts = [
'bio' => CleanHtml::class, // cleans both when getting and setting the value
'description' => CleanHtmlInput::class, // cleans when setting the value
'history' => CleanHtmlOutput::class, // cleans when getting the value
];
}
```

## Configuration

To use your own settings, publish config.

```$ php artisan vendor:publish --provider="Mews\Purifier\PurifierServiceProvider"```
```
php artisan vendor:publish --provider="Mews\Purifier\PurifierServiceProvider"
```

Config file `config/purifier.php` should like this

```php

return [
'encoding' => 'UTF-8',
'finalize' => true,
'cachePath' => storage_path('app/purifier'),
'cacheFileMode' => 0755,
'encoding' => 'UTF-8',
'finalize' => true,
'ignoreNonStrings' => false,
'cachePath' => storage_path('app/purifier'),
'cacheFileMode' => 0755,
'settings' => [
'default' => [
'HTML.Doctype' => 'HTML 4.01 Transitional',
@@ -157,29 +211,20 @@ return [
];
```

## Change log

## Example
Please see the [Github Releases Tab](https://github.com/mewebstudio/Purifier/releases) for more information on what has changed recently.

default
```php
clean(Input::get('inputname'));
```
or
## Security

```php
Purifier::clean(Input::get('inputname'));
```
If you discover any security related issues, please email [the author](mailto:me@mewebstudio.com) instead of using the issue tracker.

dynamic config
```php
clean('This is my H1 title', 'titles');
clean('This is my H1 title', array('Attr.EnableID' => true));
```
or
## Credits

```php
Purifier::clean('This is my H1 title', 'titles');
Purifier::clean('This is my H1 title', array('Attr.EnableID' => true));
```
- [HTMLPurifier.org](http://htmlpurifier.org/) - created the actual HTMLPurifier this package uses;
- [Muharrem ERİN](https://github.com/mewebstudio) - package author and maintainer;
- [All Contributors](https://github.com/mewebstudio/Purifier/graphs/contributors)

## License

for Laravel 4 [HTMLPurifier for Laravel 4](https://github.com/mewebstudio/Purifier/tree/master-l4)
MIT. Please see the [license file](https://github.com/mewebstudio/Purifier/blob/master/LICENSE) for more information.
97 changes: 52 additions & 45 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,55 @@
{
"name": "mews/purifier",
"type": "package",
"description": "Laravel 5/6 HtmlPurifier Package",
"keywords": ["laravel6 HtmlPurifier", "laravel6 Purifier", "laravel6 Security", "laravel5 HtmlPurifier", "laravel5 Purifier", "laravel5 Security", "HtmlPurifier", "Purifier", "security", "xss"],
"homepage": "https://github.com/mewebstudio/purifier",
"license": "MIT",
"authors": [
{
"name": "Muharrem ERİN",
"email": "me@mewebstudio.com",
"homepage": "https://github.com/mewebstudio",
"role": "Developer"
}
],
"require": {
"php": "^7.2",
"illuminate/config": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^6.1",
"illuminate/support": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^6.1",
"illuminate/filesystem": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^6.1",
"ezyang/htmlpurifier": "4.11.*"
},
"require-dev": {
"phpunit/phpunit": "^4.8|^5.0",
"mockery/mockery": "0.9.*",
"graham-campbell/testbench": "^3.2",
"scrutinizer/ocular": "^1.3"
},
"suggest": {
"laravel/framework": "To test the Laravel bindings",
"laravel/lumen-framework": "To test the Lumen bindings"
},
"autoload": {
"psr-4": {
"Mews\\Purifier\\": "src/"
"name": "mews/purifier",
"type": "package",
"description": "Laravel 5/6/7/8/9/10 HtmlPurifier Package",
"keywords": [
"Laravel HtmlPurifier",
"Laravel Purifier",
"Laravel Security",
"HtmlPurifier",
"Purifier",
"security",
"xss"
],
"homepage": "https://github.com/mewebstudio/purifier",
"license": "MIT",
"authors": [
{
"name": "Muharrem ERİN",
"email": "me@mewebstudio.com",
"homepage": "https://github.com/mewebstudio",
"role": "Developer"
}
],
"require": {
"php": "^7.2|^8.0",
"illuminate/config": "^5.8|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0",
"illuminate/support": "^5.8|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0",
"illuminate/filesystem": "^5.8|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0",
"ezyang/htmlpurifier": "^4.16.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0|^9.0|^10.0",
"mockery/mockery": "^1.3.3",
"graham-campbell/testbench": "^3.2|^5.5.1|^6.1"
},
"suggest": {
"laravel/framework": "To test the Laravel bindings",
"laravel/lumen-framework": "To test the Lumen bindings"
},
"autoload": {
"psr-4": {
"Mews\\Purifier\\": "src/"
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Mews\\Tests\\Purifier\\": "tests/"
}
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Mews\\Tests\\Purifier\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
@@ -53,6 +60,6 @@
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
"minimum-stability": "dev",
"prefer-stable": true
}
9 changes: 5 additions & 4 deletions config/purifier.php
Original file line number Diff line number Diff line change
@@ -17,10 +17,11 @@
*/

return [
'encoding' => 'UTF-8',
'finalize' => true,
'cachePath' => storage_path('app/purifier'),
'cacheFileMode' => 0755,
'encoding' => 'UTF-8',
'finalize' => true,
'ignoreNonStrings' => false,
'cachePath' => storage_path('app/purifier'),
'cacheFileMode' => 0755,
'settings' => [
'default' => [
'HTML.Doctype' => 'HTML 4.01 Transitional',
Loading