Skip to content

Commit

Permalink
linkify config
Browse files Browse the repository at this point in the history
  • Loading branch information
伯诚 committed Jul 30, 2013
1 parent cf8ed38 commit 0f9251a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 100 deletions.
5 changes: 5 additions & 0 deletions configdoc/usage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,11 @@
<line>277</line>
</file>
</directive>
<directive id="Linkify.Hostlist">
<file name="HTMLPurifier/Filter/Linkify.php">
<line>26</line>
</file>
</directive>
<directive id="HTML.SafeIframe">
<file name="HTMLPurifier/HTMLModule/Iframe.php">
<line>17</line>
Expand Down
Binary file modified library/HTMLPurifier/ConfigSchema/schema.ser
Binary file not shown.
8 changes: 8 additions & 0 deletions library/HTMLPurifier/ConfigSchema/schema/Linkify.Hostlist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Linkify.Hostlist
TYPE: list
VERSION: 1.3.0
DEFAULT: array()
--DESCRIPTION--
List of strings that will auto transform to a link with
so <tt>http://www.taobao.com</tt> will transform to <tt><a href="http://www.taobao.com">http://www.taobao.com</a></tt>.
--# vim: et sw=4 sts=4
123 changes: 24 additions & 99 deletions library/HTMLPurifier/Filter/Linkify.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,121 +4,46 @@ class HTMLPurifier_Filter_Linkify extends HTMLPurifier_Filter
{

public $name = 'Linkify';


protected $host_list = array();
protected $config;


protected function is_safe($uri){
$rel = false;
foreach($this->host_list as $host){
if (fnmatch($host,$uri)) {
$rel = true;
break;
}
}

return $rel;
}

public function postFilter($html, $config, $context) {

$this->host_list = $config->get('Linkify.Hostlist');
$this->config = $config;
$doc = new DOMDocument();
$doc->loadHTML($html);
$doc->strictErrorChecking = false;
libxml_use_internal_errors(true);
$xpath = new DOMXPath($doc);
$textnodes = $xpath->query('//text()[not(ancestor::a) and normalize-space()]');
foreach($textnodes as $node){
$parent = $node->parentNode;
$v = $node->nodeValue;
$a = $doc->createElement('a');
$a->nodeValue = $v;
$a->setAttribute('href',$v);
$parent->replaceChild($a,$node);
if($this->is_safe($v)){
$parent = $node->parentNode;
$a = $doc->createElement('a');
$a->nodeValue = $v;
$a->setAttribute('href',$v);
$parent->replaceChild($a,$node);
}
}
$b = $doc->getElementsByTagName('body')->item(0);
//print_r($token_list); */
return $doc->saveHtml($b->firstChild);
}

protected function textCb($matches){
$text = $matches[1];

return preg_replace_callback('#((?:https?|ftp)://[^\s\'"<>()]+)#S', array($this,'linkCb'),$text);
}

protected function linkCb($matches){
print_r($matches);
return "xx";
$obj_regex = '#(<object[^>]+>)(.*?)(</object>)#s';
$obj_html = $matches[0];

//check allowScriptAccess
$ret = preg_match($this->allowScriptAccess_regex,$matches[0]);
if(!$ret){
//add allowScriptAccess
$obj_html = preg_replace_callback($obj_regex,array($this, 'addScriptAccess'),$obj_html);
}

//check allowNetworking
$ret = preg_match($this->allowNetworking_regex,$matches[0]);
if(!$ret){
//add allowNetworking
$obj_html = preg_replace_callback($obj_regex,array($this, 'addNetworking'),$obj_html);
}

//check allowFullScreen
$ret = preg_match($this->allowFullScreen_regex,$matches[0]);
if(!$ret){
//add allowFullScreen
$obj_html = preg_replace_callback($obj_regex,array($this, 'addFullScreen'),$obj_html);
}

$param_regex = '#<param[^>]+/>#s';

return preg_replace_callback($param_regex,array($this, 'paramCb'),$obj_html);
}

protected function addScriptAccess($matches){
$to_add = '<param name="allowScriptAccess" value="never" />';
return $matches[1].$to_add.$matches[2].$matches[3];

}

protected function addNetworking($matches){
$to_add = '<param name="allowNetworking" value="internal" />';
return $matches[1].$to_add.$matches[2].$matches[3];

}


protected function addFullScreen($matches){
$allow = $this->config->get('HTML.FlashAllowFullScreen');
if($allow){
$to_add = '<param name="allowFullScreen" value="true" />';
return $matches[1].$to_add.$matches[2].$matches[3];
}
else {
return $matches[0];
}

}

protected function paramCb($matches){
$ret = preg_match($this->allowFullScreen_regex,$matches[0]);
$html = $matches[0];
if($ret){
$allow = $this->config->get('HTML.FlashAllowFullScreen');
if($allow){
$html = '<param name="allowFullScreen" value="true" />';
}
else{
$html = '';
}
}

$ret = preg_match($this->allowScriptAccess_regex,$matches[0]);
if($ret){
$html = '<param name="allowScriptAccess" value="never" />';
}

$ret = preg_match($this->allowNetworking_regex,$matches[0]);
if($ret){
$html = '<param name="allowNetworking" value="internal" />';
}

return $html;
}
protected function armorUrl($url) {
return str_replace('--', '-&#45;', $url);
}

}

Expand Down
3 changes: 2 additions & 1 deletion tests/HTMLPurifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ function test_linkify(){
// $this->config->set('Filter.YouTube', true);

/* use the custom FlashObject Filter to instead of the SafeObject Inector */
$this->config->set('Linkify.Hostlist',array('http://*.taobao.com'));
$this->config->set('Filter.Custom',array(new HTMLPurifier_Filter_Linkify(),new HTMLPurifier_Filter_FlashObject()));

/* use custom Whitelist URIFilter to Filter the unsafe URL */
Expand All @@ -130,7 +131,7 @@ function test_linkify(){
$hm->manager->addModule(new HTMLPurifier_HTMLModule_SafeFlashObject());


$content = "<a><span>http://www.taobao2.com</span></a>";
$content = "<span>http://www.taobao.com</span>";

$after = $this->purifier->purify($content,$this->config);

Expand Down

0 comments on commit 0f9251a

Please sign in to comment.