-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from nguyenanhung/v4.x
Update security v4.0.2
- Loading branch information
Showing
3 changed files
with
153 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,67 @@ | ||
{ | ||
"name": "nguyenanhung/security", | ||
"type": "library", | ||
"description": "Wrapper for Security, Encrypt, Encode, Purifier", | ||
"keywords": [ | ||
"security", | ||
"encrypt", | ||
"encode", | ||
"decode" | ||
], | ||
"homepage": "https://github.com/nguyenanhung/security", | ||
"license": "GPL-3.0", | ||
"authors": [ | ||
{ | ||
"name": "Nguyen An Hung", | ||
"email": "[email protected]", | ||
"homepage": "https://nguyenanhung.com", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.0 || ^7.0", | ||
"ext-openssl": "*", | ||
"paragonie/sodium_compat": "^1.20", | ||
"ircmaxell/password-compat": "^1.0", | ||
"symfony/security-csrf": "^7.0 || ^6.4 || ^5.4 || ^4.4 || ^3.4", | ||
"ezyang/htmlpurifier": "^4.13", | ||
"phpseclib/phpseclib": "^3.0", | ||
"phpseclib/mcrypt_compat": "^2.0", | ||
"voku/anti-xss": "^4.0 || ^3.0", | ||
"nguyenanhung/escape-helper": "^2.0", | ||
"nguyenanhung/math-helper": "^2.0", | ||
"nguyenanhung/htmlawed": "^1.2" | ||
}, | ||
"require-dev": { | ||
"kint-php/kint": ">=3.0" | ||
}, | ||
"suggest": { | ||
"ext-json": "Needed to support JSON", | ||
"ext-mcrypt": "Needed to support mcrypt", | ||
"ext-iconv": "Needed to support iconv", | ||
"ext-openssl": "Needed to support openssl", | ||
"ext-mbstring": "Needed to support mb_string" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"nguyenanhung\\MySecurity\\": "src/" | ||
}, | ||
"files": [ | ||
"helpers/helpers.php" | ||
] | ||
} | ||
"name": "nguyenanhung/security", | ||
"type": "library", | ||
"description": "Wrapper for Security, Encrypt, Encode, Purifier", | ||
"keywords": [ | ||
"security", | ||
"encrypt", | ||
"encode", | ||
"decode" | ||
], | ||
"homepage": "https://github.com/nguyenanhung/security", | ||
"license": "GPL-3.0", | ||
"authors": [ | ||
{ | ||
"name": "Nguyen An Hung", | ||
"email": "[email protected]", | ||
"homepage": "https://nguyenanhung.com", | ||
"role": "Developer" | ||
} | ||
], | ||
"repositories": [ | ||
{ | ||
"type": "package", | ||
"package": { | ||
"name": "voku/portable-utf8", | ||
"version": "6.0.14", | ||
"source": { | ||
"type": "git", | ||
"url": "https://github.com/joomla-backports/portable-utf8.git", | ||
"reference": "joomla-5.3" | ||
} | ||
} | ||
} | ||
], | ||
"require": { | ||
"php": "^8.0 || ^7.1", | ||
"ext-openssl": "*", | ||
"paragonie/sodium_compat": "^1.20", | ||
"ircmaxell/password-compat": "^1.0", | ||
"symfony/security-csrf": "^7.0 || ^6.4 || ^5.4 || ^4.4 || ^3.4", | ||
"ezyang/htmlpurifier": "^4.13", | ||
"phpseclib/phpseclib": "^3.0", | ||
"phpseclib/mcrypt_compat": "^2.0", | ||
"voku/anti-xss": "^4.0 || ^3.0", | ||
"nguyenanhung/escape-helper": "^3.0 || ^2.0", | ||
"nguyenanhung/math-helper": "^2.0", | ||
"nguyenanhung/htmlawed": "^1.2" | ||
}, | ||
"require-dev": { | ||
"kint-php/kint": ">=3.0" | ||
}, | ||
"suggest": { | ||
"ext-json": "Needed to support JSON", | ||
"ext-mcrypt": "Needed to support mcrypt", | ||
"ext-iconv": "Needed to support iconv", | ||
"ext-openssl": "Needed to support openssl", | ||
"ext-mbstring": "Needed to support mb_string" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"nguyenanhung\\MySecurity\\": "src/" | ||
}, | ||
"files": [ | ||
"helpers/helpers.php" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,8 @@ | |
|
||
namespace nguyenanhung\MySecurity; | ||
|
||
use HTMLPurifier_Config; | ||
use HTMLPurifier; | ||
use HTMLPurifier_Config; | ||
|
||
/** | ||
* Class HtmlSecurity | ||
|
@@ -25,100 +25,100 @@ | |
*/ | ||
class HtmlSecurity implements ProjectInterface | ||
{ | ||
use VersionTrait; | ||
use VersionTrait; | ||
|
||
/** @var null|string Thư mục cache cho HTML Purifier */ | ||
protected $cachePath; | ||
/** @var null|string Thư mục cache cho HTML Purifier */ | ||
protected $cachePath; | ||
|
||
/** @var null|array Mảng dữ liệu cấu hình cho HTML Purifier */ | ||
protected $config; | ||
/** @var null|array Mảng dữ liệu cấu hình cho HTML Purifier */ | ||
protected $config; | ||
|
||
/** | ||
* HtmlSecurity constructor. | ||
* | ||
* @param string|null $cachePath | ||
* @param array|null $config | ||
* | ||
* @author : 713uk13m <[email protected]> | ||
* @copyright: 713uk13m <[email protected]> | ||
*/ | ||
public function __construct(string $cachePath = '', array $config = array()) | ||
{ | ||
if (!empty($cachePath)) { | ||
$this->cachePath = $cachePath; | ||
} | ||
if (!empty($config)) { | ||
$this->config = $config; | ||
} | ||
} | ||
/** | ||
* HtmlSecurity constructor. | ||
* | ||
* @param string|null $cachePath | ||
* @param array|null $config | ||
* | ||
* @author : 713uk13m <[email protected]> | ||
* @copyright: 713uk13m <[email protected]> | ||
*/ | ||
public function __construct(string $cachePath = '', array $config = array()) | ||
{ | ||
if (!empty($cachePath)) { | ||
$this->cachePath = $cachePath; | ||
} | ||
if (!empty($config)) { | ||
$this->config = $config; | ||
} | ||
} | ||
|
||
/** | ||
* Hàm cấu hình thư mục cache cho HTML Purifier | ||
* | ||
* @param string|null $cachePath | ||
* | ||
* @return $this | ||
* @author: 713uk13m <[email protected]> | ||
* @time : 10/18/18 09:44 | ||
* | ||
*/ | ||
public function setCachePath(string $cachePath = null): HtmlSecurity | ||
{ | ||
$this->cachePath = $cachePath; | ||
return $this; | ||
} | ||
/** | ||
* Hàm cấu hình thư mục cache cho HTML Purifier | ||
* | ||
* @param string|null $cachePath | ||
* | ||
* @return $this | ||
* @author: 713uk13m <[email protected]> | ||
* @time : 10/18/18 09:44 | ||
* | ||
*/ | ||
public function setCachePath(?string $cachePath = ''): HtmlSecurity | ||
{ | ||
$this->cachePath = $cachePath; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Hàm set cấu hình Config cho HTML Purifier | ||
* | ||
* Mảng dữ liệu với key và value | ||
* | ||
* @param array $config Mảng dữ liệu cấu hình | ||
* | ||
* @return $this | ||
* | ||
* @author: 713uk13m <[email protected]> | ||
* @time : 10/18/18 09:51 | ||
* | ||
* @see http://htmlpurifier.org/live/configdoc/plain.html | ||
*/ | ||
public function setConfig(array $config = array()): HtmlSecurity | ||
{ | ||
$this->config = $config; | ||
return $this; | ||
} | ||
/** | ||
* Hàm set cấu hình Config cho HTML Purifier | ||
* | ||
* Mảng dữ liệu với key và value | ||
* | ||
* @param array $config Mảng dữ liệu cấu hình | ||
* | ||
* @return $this | ||
* | ||
* @author: 713uk13m <[email protected]> | ||
* @time : 10/18/18 09:51 | ||
* | ||
* @see http://htmlpurifier.org/live/configdoc/plain.html | ||
*/ | ||
public function setConfig(array $config = array()): HtmlSecurity | ||
{ | ||
$this->config = $config; | ||
return $this; | ||
} | ||
|
||
/** | ||
* HTML Escape | ||
* | ||
* Hàm clean mã html, loại bỏ mã độc, mã bẩn sử dụng HTML Purifier | ||
* | ||
* @param string $dirtyHtml Chuỗi đầu vào | ||
* | ||
* @return string Nội dung đầu ra sau khi đã lọc | ||
* @author: 713uk13m <[email protected]> | ||
* @time : 10/18/18 09:45 | ||
* | ||
*/ | ||
public function escape(string $dirtyHtml = ''): string | ||
{ | ||
// Create Config | ||
$config = HTMLPurifier_Config::createDefault(); | ||
/** | ||
* HTML Escape | ||
* | ||
* Hàm clean mã html, loại bỏ mã độc, mã bẩn sử dụng HTML Purifier | ||
* | ||
* @param string $dirtyHtml Chuỗi đầu vào | ||
* | ||
* @return string Nội dung đầu ra sau khi đã lọc | ||
* @author: 713uk13m <[email protected]> | ||
* @time : 10/18/18 09:45 | ||
* | ||
*/ | ||
public function escape(string $dirtyHtml = ''): string | ||
{ | ||
// Create Config | ||
$config = HTMLPurifier_Config::createDefault(); | ||
|
||
// Setup Cache.SerializerPath | ||
$config->set('Cache.SerializerPath', $this->cachePath); | ||
// Setup Cache.SerializerPath | ||
$config->set('Cache.SerializerPath', $this->cachePath); | ||
|
||
// Setup External Config | ||
if (!empty($this->config) && is_array($this->config) && count($this->config) > 0) { | ||
foreach ($this->config as $key => $value) { | ||
$config->set($key, $value); | ||
} | ||
} | ||
// Setup External Config | ||
if (!empty($this->config) && is_array($this->config) && count($this->config) > 0) { | ||
foreach ($this->config as $key => $value) { | ||
$config->set($key, $value); | ||
} | ||
} | ||
|
||
// Init HTMLPurifier | ||
$purifier = new HTMLPurifier($config); | ||
$cleanHtml = $purifier->purify($dirtyHtml); | ||
// Init HTMLPurifier | ||
$purifier = new HTMLPurifier($config); | ||
$cleanHtml = $purifier->purify($dirtyHtml); | ||
|
||
return trim($cleanHtml); | ||
} | ||
return trim($cleanHtml); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters