Skip to content

Commit

Permalink
Merge pull request #32 from nguyenanhung/v4.x
Browse files Browse the repository at this point in the history
Update security v4.0.2
  • Loading branch information
nguyenanhung authored Jan 19, 2025
2 parents aa54bda + 182377a commit 6f1735c
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 139 deletions.
116 changes: 65 additions & 51 deletions composer.json
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"
]
}
}
174 changes: 87 additions & 87 deletions src/HtmlSecurity.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace nguyenanhung\MySecurity;

use HTMLPurifier_Config;
use HTMLPurifier;
use HTMLPurifier_Config;

/**
* Class HtmlSecurity
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/ProjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
interface ProjectInterface
{
const VERSION = '4.0.1';
const VERSION = '4.0.2';
const USE_BENCHMARK = true;

/**
Expand Down

0 comments on commit 6f1735c

Please sign in to comment.