2012-05-05 35 views
3

有人可以指出我需要什麼來允許像這樣的錨標記通過htmlpurifier,不受影響嗎? :如何配置HTML淨化器以允許錨點href值中的「callto:123」?

<a href="callto:+1-800-555-1212" class="callUsBtn" style="">...</a> 

與我的配置。設置,callto:...值不被識別爲允許,因此整個href屬性被剝離。我想我需要覆蓋href值的類型(< - 或稱之爲?),從URICDATA(?),但不知道如何。我希望有一個比這裏找到的方向更快/更簡單的方法:http://htmlpurifier.org/docs/enduser-customize.html(如果這甚至會導致解決方案?)。

回答

4

感謝愛德華的回答,我得到了它的工作。

試試這個:

在你相當於在這裏:​​
/htmlpurifier-4_4_0/library/HTMLPurifier/URIScheme/

添加一個名爲文件:callto.php
......這些內容:

<?php 

// replicated this class/file from '../htmlpurifier-4_4_0/library/HTMLPurifier/URIScheme/mailto.php' 

// VERY RELAXED! Shouldn't cause problems, ... but be careful! 

/** 
* Validates 'callto:' phone number in URI to included only alphanumeric, hyphens, underscore, and optional leading "+" 
*/ 

class HTMLPurifier_URIScheme_callto extends HTMLPurifier_URIScheme { 

    public $browsable = false; 
    public $may_omit_host = true; 

    public function doValidate(&$uri, $config, $context) { 
     $uri->userinfo = null; 
     $uri->host  = null; 
     $uri->port  = null; 

     /* notes: 
      where is the actual phone # parked? Answer in $uri->path. See here: 

      echo '<pre style="color:pink;">'; 
      var_dump($uri); 
      echo '</pre>'; 

      object(HTMLPurifier_URI)#490 (7) { 
       ["scheme"]=> 
       string(6) "callto" 
       ["userinfo"]=> 
       NULL 
       ["host"]=> 
       NULL 
       ["port"]=> 
       NULL 
       ["path"]=> 
       string(15) "+1-800-555-1212" 
       ["query"]=> 
       NULL 
       ["fragment"]=> 
       NULL 
      } 
     */ 

     // are the characters in the submitted <a> href's (URI) value (callto:) from amongst a legal/allowed set? 
      // my legal phone # chars: alphanumeric, underscore, hyphen, optional "+" for the first character. That's it. But you can allow whatever you want. Just change this: 
      $validCalltoPhoneNumberPattern = '/^\+?[a-zA-Z0-9_-]+$/i'; // <---whatever pattern you want to force phone numbers to match 
      $proposedPhoneNumber = $uri->path; 
      if (preg_match($validCalltoPhoneNumberPattern, $proposedPhoneNumber) !== 1) { 
       // submitted phone # inside the href attribute value looks bad; reject the phone number, and let HTMLpurifier remove the whole href attribute on the submitted <a> tag. 
       return FALSE; 
      } else { 
       // submitted phone # inside the href attribute value looks OK; accept the phone number; HTMLpurifier should NOT strip the href attribute on the submitted <a> tag. 
       return TRUE; 
      } 
    } 

} 

...和不要忘記更新您的HTMLpurifier配置,從默認情況下,包括這樣的內容:

$config->set('URI.AllowedSchemes', array ('http' => true, 'https' => true, 'mailto' => true, 'ftp' => true, 'nntp' => true, 'news' => true, 'callto' => true,)); 
+0

看起來很合理。 –

+0

與其編輯庫內容,我該如何動態添加自己的URIScheme類?我正在嘗試這樣的事情:'$ urireg = $ config-> getDefinition('URISchemeRegistry'); $ urireg-> register(new MyApp_HTMLPurifier_URIScheme_callto,$ config);' – Synchro

+0

我想我找到了它:'HTMLPurifier_URISchemeRegistry :: instance() - > register(new MyApp_HTMLPurifier_URIScheme_callto,$ config);' – Synchro

2

您需要實施新的HTMLPurifier_URISchemelibrary/HTMLPurifier/URIScheme中有很多示例,並且使用正常配置很容易添加自定義URI方案。

+0

謝謝愛德華。我有點頭痛 - 主要來自OOP&HTMLpurifier新手。我查看'library/HTMLPurifier/URIScheme'並查看一個'抽象類HTMLPurifier_URIScheme'。你是說我需要破解那個課程嗎?在這裏我沒有看到你的意思是「大量的例子」。我更新了配置w /:'$ config-> set('URI.AllowedSchemes',array('http'=> true,...,'callto'=> true,));',但顯然仍然需要「實現一個新的'HTMLPurifier_URIScheme'」(這與「向註冊表註冊HTMLPurifier_URIScheme」相同?)。你能給我多一點方向嗎? – govinda

+1

有一個URIScheme文件夾,其中包含更多文件,如http.php和ftp.php;這些就是例子。抽象類定義了你需要定義的接口。 –

+0

啊..我多麼傻。是的,*文件夾*名爲'library/HTMLPurifier/URIScheme'。非常感謝。我相信我已經完成了必要的工作。 (似乎工作正常。)我會發佈一個完整的答案,以方便新手。如果我可以成爲你的技能/知識和比我更綠色的初學者之間的中間人,那麼這似乎是結束這篇文章/主題最有用的方法,呵呵?你如何評論或投票答覆我現在要發佈的答案......如果我後來看到你批准了,那麼我會接受這個答案,所以對未來的讀者來說很明顯。好? – govinda

相關問題