好吧,我成功地定製注射器添加到HTMLPurifier,那就是:
首先,在創建 「DisplayRemoteLinkURI.php」「包括\ htmlpurifier \庫\ HTMLPurifier \注射器「寫這在它
<?php
class HTMLPurifier_Injector_DisplayRemoteLinkURI extends HTMLPurifier_Injector
{
public $name = 'DisplayRemoteLinkURI';
public $needed = array('a');
public function handleElement(&$token) {
}
public function handleEnd(&$token) {
if (isset($token->start->attr['href'])){
$url = $token->start->attr['href'];
if($this->is_remote($url)){
unset($token->start->attr['href']);
$token = array($token, new HTMLPurifier_Token_Text(" ($url)"));
}
} else {
// nothing to display
}
}
public function is_remote($path){
$urlvar = parse_url($path);
$remote_schemes = array("mailto");
$local_schemes = array("javascript");
if(in_array($urlvar["scheme"],$remote_schemes)){
return true;
}else if(in_array($urlvar["scheme"],$local_schemes)){
return false;
}else{
if(empty($urlvar["host"]) || $urlvar["host"]==$_SERVER["HTTP_HOST"]){
return false;
}else{
return true;
}
}
}
}
?>
然後創建一個名爲另一個文件」 AUT oFormat.DisplayRemoteLinkURI.txt 「中的」包括\ htmlpurifier \庫\ HTMLPurifier \ ConfigSchema \模式「,並補充一點:
AutoFormat.DisplayRemoteLinkURI
TYPE: bool
VERSION: 3.2.0
DEFAULT: false
--DESCRIPTION--
<p>
This directive turns on the in-text display of Remote URIs in <a> tags, and disables
those links. For example, <a href="http://example.com">example</a> becomes
example (<a>http://example.com</a>).
</p>
--# vim: et sw=4 sts=4
之後,添加此行
require 'HTMLPurifier/Injector/DisplayRemoteLinkURI.php';
下
require 'HTMLPurifier/Injector/DisplayLinkURI.php';
in include \ htmlpurifier \ library \ HTMLPurifier.includes.php
然後,添加這一行
require_once $__dir . '/HTMLPurifier/Injector/DisplayRemoteLinkURI.php';
下
require_once $__dir . '/HTMLPurifier/Injector/DisplayLinkURI.php';
在包括\ htmlpurifier \庫\ HTMLPurifier.safe-包括。php
經過這些編輯,如果你的文件在本地,運行cmd.exe並進入你的php目錄。然後從php.exe運行「include/HTMLPurifier/maintenance/generate-schema-cache.php」。
或者如果你想通過瀏覽器做到這一點,重命名你的.htaccess文件內的「include/HTMLPurifier/maintenance /」一段時間,然後將此行添加到「generate-schema-cache.php」 <?php
標籤後的第一行;
php_set_env("PHP_IS_CLI",true);
然後從瀏覽器運行此文件。在看到「保存模式完成!」後,重命名您的.htaccess文件。
然後在您的腳本中,使用「AutoFormat.DisplayRemoteLinkURI」作爲配置,瞧!
請注意,我在這裏給出的第一個文件中的is_remote()函數可能不太好,而且我找不到檢查鏈接是遠程還是本地鏈接的腳本,因此如果您以後可能會更改該鏈接需要。
嗨,謝謝你的努力。我創建了該文件,添加了上述代碼,並從命令行運行了php文件。文件庫/ HTMLPurifier/ConfigSchema/schema.ser被創建。我添加了AutoFormat.DisplayRemoteLinkURI到我的配置,但我得到了這個錯誤「警告:無法設置未定義的指令AutoFormat.DisplayRemoteLinkURI值......」。我在schema.ser中搜索了DisplayRemoteLinkURI,但是我沒有找到它。我的文件是本地的,我運行(php htmlpurifier/maintenance/generate-schema-cache.php)。我收到了消息「Saving schema ... done!' – 2012-03-21 13:02:00
好吧,我沒有做任何其他更改。讓我再看一次。 – 2012-03-21 13:08:08
還有一個我忘記提到的txt文件。我更新了代碼。添加文本文件並重建模式。 – 2012-03-21 13:15:09