+++ /dev/null
-<?php
-/**
- * FusionForge text sanitisation
- *
- * Copyright (C) 2005, Daniel Perez
- * Copyright (C) 2008-2009 Alcatel-Lucent
- *
- * This file is part of FusionForge.
- *
- * FusionForge is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published
- * by the Free Software Foundation; either version 2 of the License,
- * or (at your option) any later version.
- *
- * FusionForge is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with FusionForge; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
- */
-
-/*
- * Standard Alcatel-Lucent disclaimer for contributing to open source
- *
- * "The Style Sheet ("Contribution") has not been tested and/or
- * validated for release as or in products, combinations with products or
- * other commercial use. Any use of the Contribution is entirely made at
- * the user's own responsibility and the user can not rely on any features,
- * functionalities or performances Alcatel-Lucent has attributed to the
- * Contribution.
- *
- * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
- * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
- * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
- * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
- * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
- * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
- * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
- * ALONE BASIS."
- */
-
-require_once('HTMLPurifier.auto.php');
-
-Class TextSanitizer extends Error {
-
-
- /**
- * convertExtendedCharsForEmail - Grabs some text with html special characters and converts them to the corresponding character.
- *
- * @param string The input string
- * @return string The output string
- */
- function convertExtendedCharsForEmail($text) {
- $text = str_replace("´","'",$text); //it's better to see that char in the email than the html entity
- $text = str_replace("&","&",$text);
- $text = str_replace(""",'"',$text);
- $text = str_replace(" ",' ',$text);
- $text = str_replace("<",'<',$text);
- $text = str_replace(">",'>',$text);
- $text = str_replace("°",'°',$text);
- $text = str_replace("<br>","\n",$text);
- $text = str_replace("<br />","\n",$text);
-
- $text = str_replace("é","é",$text);
- $text = str_replace("è","è",$text);
- $text = str_replace("ê","ê",$text);
- $text = str_replace("ë","ë",$text);
- $text = str_replace("à","à",$text);
- $text = str_replace("â","â",$text);
- $text = str_replace("ç","ç",$text);
- $text = str_replace("ù","ù",$text);
- $text = str_replace("û","û",$text);
- $text = str_replace("ü","ü",$text);
- $text = str_replace("ô","ô",$text);
- $text = str_replace("ï","ï",$text);
-
- return $text;
- }
-
- /**
- * convertNeededTagsForEmail - Grabs some text with html tags and those which are important for display (<br>, <p>) convert accordingly
- *
- * @param string The input string
- * @return string The output string
- */
- function convertNeededTagsForEmail($text) {
- $text = str_replace("<br>","\n",$text);
- $text = str_replace("<br />","\n",$text);
- $text = str_replace("<br/>","\n",$text);
- $text = str_replace("<p>","\n",$text);
- $text = str_replace("</p>","\n",$text);
- $text = str_replace("<li>","\n - ",$text);
- $text = str_replace("</li>",'',$text);
- $text = str_replace("<ul>",'',$text);
- $text = str_replace("</ul>","\n",$text);
- $text = str_replace("\xc2\xa0",' ',$text);
- return $text;
- }
-
- function unhtmlentities ($string) {
- $trans_tbl = get_html_translation_table (HTML_SPECIALCHARS );
- $trans_tbl = array_flip ($trans_tbl );
- $res = strtr ($string ,$trans_tbl );
- $res = str_replace("&quot;",'"',$res);
- return $res;
- }
-
- /**
- * SanitizeHtml - Grabs some text with all kinds of html code and parses it to make it safe
- *
- * @param string The HTML Code
- * @return string The HTML output
- */
- function SanitizeHtml($input) {
-
- $input = htmlspecialchars($input); // first strip all chars
-
- $input=str_replace('&','&',$input);
- $input=str_replace('"','"',$input);
- $input=str_replace('/>','/>',$input);
- $input=str_replace('">','">',$input);
- $input=str_replace('</a>','</a>',$input);
- $input=str_replace('<strike>','<strike>',$input);
- $input=str_replace('</strike>','</strike>',$input);
- $input=str_replace('<sub>','<sub>',$input);
- $input=str_replace('</sub>','</sub>',$input);
- $input=str_replace('<span','<span',$input);
- $input=str_replace('</span>','</span>',$input);
- $input=str_replace('<font','<font',$input);
- $input=str_replace('</font>','</font>',$input);
- $input=str_replace('<hr>','<hr>',$input);
- $input=str_replace('<hr','<hr',$input);
- $input=str_replace('<br>','<br>',$input);
- $input=str_replace('<br />','<br />',$input);
- $input=str_replace('<tbody>','<tbody>',$input);
- $input=str_replace('</tbody>','</tbody>',$input);
- $input=str_replace('<tr>','<tr>',$input);
- $input=str_replace('</tr>','</tr>',$input);
- $input=str_replace('<td>','<td>',$input);
- $input=str_replace('</td>','</td>',$input);
- $input=str_replace('<td','<td',$input);
- $input=str_replace('<table>','<table>',$input);
- $input=str_replace('<table','<table',$input);
- $input=str_replace('</table>','</table>',$input);
- $input=str_replace('<div>','<div>',$input);
- $input=str_replace('<div','<div',$input);
- $input=str_replace('</div>','</div>',$input);
- $input=str_replace('<u>','<u>',$input);
- $input=str_replace('<u ','<u ',$input); // rg
- $input=str_replace('</u>','</u>',$input);
- $input=str_replace('<p>','<p>',$input);
- $input=str_replace('</p>','</p>',$input);
- $input=str_replace('<p ','<p ',$input);
- $input=str_replace('<li>','<li>',$input);
- $input=str_replace('</li>','</li>',$input);
- $input=str_replace('<ul>','<ul>',$input);
- $input=str_replace('</ul>','</ul>',$input);
- $input=str_replace('<ol>','<ol>',$input);
- $input=str_replace('</ol>','</ol>',$input);
- $input=str_replace('<blockquote>','<blockquote>',$input);
- $input=str_replace('<blockquote','<blockquote',$input);
- $input=str_replace('</blockquote>','</blockquote>',$input);
- $input=str_replace('<em>','<em>',$input);
- $input=str_replace('</em>','</em>',$input);
- $input=str_replace('<strong>','<strong>',$input);
- $input=str_replace('</strong>','</strong>',$input);
- $input=str_replace('<sup>','<sup>',$input);
- $input=str_replace('</sup>','</sup>',$input);
- $input=str_replace('<input ','<input ',$input);
- $input=str_replace('<img ','<img ',$input);
- $input=str_replace('<textarea ','<textarea ',$input);
- $input=str_replace('</textarea>','</textarea>',$input);
- $input=str_replace('<a ','<a ',$input);
- $input=str_replace('<h1>','<h1>',$input);
- $input=str_replace('</h1>','</h1>',$input);
- $input=str_replace('<h2>','<h2>',$input);
- $input=str_replace('</h2>','</h2>',$input);
- $input=str_replace('<h3>','<h3>',$input);
- $input=str_replace('</h3>','</h3>',$input);
- $input=str_replace('<h4>','<h4>',$input);
- $input=str_replace('</h4>','</h4>',$input);
- $input=str_replace('<h5>','<h5>',$input);
- $input=str_replace('</h5>','</h5>',$input);
- $input=str_replace('<h6>','<h6>',$input);
- $input=str_replace('</h6>','</h6>',$input);
- $input=str_replace('<pre>','<pre>',$input);
- $input=str_replace('</pre>','</pre>',$input);
- $input=str_replace('<address>','<address>',$input);
- $input=str_replace('</address>','</address>',$input);
- $input=str_replace('<h1 ','<h1 ',$input);
- $input=str_replace('<h2 ','<h2 ',$input);
- $input=str_replace('<h3 ','<h3 ',$input);
- $input=str_replace('<h4 ','<h4 ',$input);
- $input=str_replace('<h5 ','<h5 ',$input);
- $input=str_replace('<h6 ','<h6 ',$input);
- $input=str_replace('’','\\\'',$input);
- $input=str_replace('•','-',$input);
-
- // Allow embbeding video like youtube ones.
- $input=str_replace('<object ','<object ',$input);
- $input=str_replace('</object>','</object>',$input);
- $input=str_replace('<param ','<param ',$input);
- $input=str_replace('</param>','</param>',$input);
- $input=str_replace('<embed ','<embed ',$input);
- $input=str_replace('</embed>','</embed>',$input);
-
- return $input;
- }
-
- function stripTags ($text, $allowed='br,p,li,ul') {
- $config = HTMLPurifier_Config::createDefault();
- $config->set('Cache.DefinitionImpl', NULL);
- $config->set('HTML.Allowed', $allowed);
- $purifier = new HTMLPurifier($config);
- $text = $purifier->purify($text);
-
- return $text;
- }
-
- function purify ($text) {
- // Remove string like "<![if !supportLists]>" or "<![endif]>"
- $text = preg_replace('/<!\[.+?\]>/', '', $text);
- $config = HTMLPurifier_Config::createDefault();
- //$config->set('HTML.Allowed','a[href|title],strike,sub,span,font,hr,br,tbody,tr,td,table,div,u,p,ul,li,ol,blockquote,em,strong,sup,input,img,textarea,h1,h2,h3,h4,h5,h6,pre,address');
- $config->set('Cache.DefinitionImpl', NULL);
- $purifier = new HTMLPurifier($config);
- return $purifier->purify($text);
- }
-
- function summarize ($text, $nb_line=4, $truncate=true, $nb_char=145) {
- $text = $this->stripTags($text);
- $text = $this->convertNeededTagsForEmail($text);
- // Remove MS Windows extra char for CR
- $text = preg_replace('/\r/', '', $text);
- // Strip CR
- $text = preg_replace('/\n[\n\s]*/', "\n", $text);
- $text = trim($text);
- $arr = explode("\n", $text);
- $nb_max = count($arr);
- if ($nb_max > $nb_line) $nb_max = $nb_line;
- $summary = '';
- for ($l = 0; $l < $nb_max; $l++) {
- $summary .= '<br />';
- if ($truncate == true && $nb_max < $nb_line && $l == $nb_max - 1) {
- $nb_char = $nb_char * ($nb_line - $nb_max + 1);
- }
- $summary .= util_make_links((($truncate == true && strlen($arr[$l]) > $nb_char) ?
- preg_replace('/[^\s]*$/', ' <b>...</b>', substr($arr[$l], 0, $nb_char), 1) :
- $arr[$l]));
- }
-
- return $summary;
- }
-}
-
-// Local Variables:
-// mode: php
-// c-file-style: "bsd"
-// End:
-
-?>