-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HTML purifier filtering and filter traversal
- Loading branch information
1 parent
e07b6e2
commit 125c151
Showing
5 changed files
with
89 additions
and
14 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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace TgUtils; | ||
|
||
/** | ||
* Abstract string filter that traverses objects and arrays. | ||
*/ | ||
abstract class AbstractStringFilter implements StringFilter { | ||
|
||
public function __construct() { | ||
} | ||
|
||
/** | ||
* Filters the given string and returns sanitized value. | ||
* @param string $s - string to sanitize (can be null) | ||
* @return the sanitized string. | ||
*/ | ||
public function filter($s) { | ||
if ($s == NULL) return $s; | ||
if (is_string($s)) { | ||
return $this->filterString($s); | ||
} else if (is_array($s)) { | ||
foreach ($s AS $key => $value) { | ||
$s[$key] = $this->filter($value); | ||
} | ||
} else if (is_object($s)) { | ||
foreach (get_object_vars($s) AS $name => $value) { | ||
$s->$name = $this->filter($value); | ||
} | ||
} | ||
return $s; | ||
} | ||
|
||
protected function filterString($s) { | ||
return $s; | ||
} | ||
} | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace TgUtils; | ||
|
||
class PurifierStringFilter extends AbstractStringFilter { | ||
|
||
public static $INSTANCE; | ||
|
||
protected $purifier; | ||
|
||
public function __construct() { | ||
parent::__construct(); | ||
$config = $this->getConfig(); | ||
$this->purifier = new \HTMLPurifier($config); | ||
} | ||
|
||
public function filterString($s) { | ||
return $this->purifier->purify($s); | ||
} | ||
|
||
protected function getConfig() { | ||
$config = \HTMLPurifier_Config::createDefault(); | ||
$config->set('HTML.DefinitionID', 'simple'); | ||
$config->set('HTML.DefinitionRev', 1); | ||
$config->set('HTML.AllowedElements', array('br', 'p', 'div', 'li', 'ol', 'ul', 'i', 'b', 'strong', 'a', 'h4', 'h5','table','tr','td','th')); | ||
$config->set('HTML.AllowedAttributes', array( | ||
'a.href', 'a.class', 'a.style', | ||
'p.style', 'div.style', | ||
'li.style', 'ol.style', 'ul.style', | ||
'i.style', 'b.style', 'strong.style', | ||
'h4.style', 'h5.style', | ||
'table.style','table.class','tr.style','td.colspan','td.rowspan','td.style','th.colspan','th.rowspan','th.style','tr.class','td.class', | ||
)); | ||
return $config; | ||
} | ||
} | ||
PurifierStringFilter::$INSTANCE = new PurifierStringFilter(); | ||
|
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,14 +1,18 @@ | ||
<?php | ||
|
||
package TgUtils; | ||
namespace TgUtils; | ||
|
||
/** | ||
* Provides default string filters. | ||
*/ | ||
public class StringFilters { | ||
class StringFilters { | ||
|
||
public static $DUMMY = DummyStringFilter::$INSTANCE; | ||
public static $NO_HTML = NoHtmlStringFilter::$INSTANCE; | ||
public static $DUMMY; | ||
public static $NO_HTML; | ||
public static $TEXTBOX; | ||
|
||
} | ||
StringFilters::$DUMMY = DummyStringFilter::$INSTANCE; | ||
StringFilters::$NO_HTML = NoHtmlStringFilter::$INSTANCE; | ||
StringFilters::$TEXTBOX = PurifierStringFilter::$INSTANCE; | ||
|