XOOPS Docs - kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file module.textsanitizer.php

Documentation is available at module.textsanitizer.php

  1. <?php
  2. // $Id: module.textsanitizer.php 957 2007-08-06 23:40:36Z malanciault $
  3. //  ------------------------------------------------------------------------ //
  4. //                XOOPS - PHP Content Management System                      //
  5. //                    Copyright (c) 2000 XOOPS.org                           //
  6. //                       <http://www.xoops.org/>                             //
  7. //  ------------------------------------------------------------------------ //
  8. //  This program is free software; you can redistribute it and/or modify     //
  9. //  it under the terms of the GNU General Public License as published by     //
  10. //  the Free Software Foundation; either version 2 of the License, or        //
  11. //  (at your option) any later version.                                      //
  12. //                                                                           //
  13. //  You may not change or alter any portion of this comment or credits       //
  14. //  of supporting developers from this source code or any supporting         //
  15. //  source code which is considered copyrighted (c) material of the          //
  16. //  original comment or credit authors.                                      //
  17. //                                                                           //
  18. //  This program is distributed in the hope that it will be useful,          //
  19. //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
  20. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
  21. //  GNU General Public License for more details.                             //
  22. //                                                                           //
  23. //  You should have received a copy of the GNU General Public License        //
  24. //  along with this program; if not, write to the Free Software              //
  25. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
  26. //  ------------------------------------------------------------------------ //
  27. // Author: Kazumi Ono (http://www.myweb.ne.jp/, http://jp.xoops.org/)        //
  28. //         Goghs Cheng (http://www.eqiao.com, http://www.devbeez.com/)       //
  29. // Project: The XOOPS Project (http://www.xoops.org/)                        //
  30. // ------------------------------------------------------------------------- //
  31.  
  32. /**
  33.  * Class to "clean up" text for various uses
  34.  *
  35.  * <b>Singleton</b>
  36.  *
  37.  * @package        kernel
  38.  * @subpackage    core
  39.  *
  40.  * @author        Kazumi Ono     <onokazu@xoops.org>
  41.  * @author      Goghs Cheng
  42.  * @copyright    (c) 2000-2003 The Xoops Project - www.xoops.org
  43.  */
  44. {
  45.     /**
  46.      * @var    array 
  47.      */
  48.     var $smileys = array();
  49.  
  50.     /**
  51.      *
  52.      */
  53.     var $censorConf;
  54.  
  55.     /*
  56.     * Constructor of this class
  57.     *
  58.     * Gets allowed html tags from admin config settings
  59.     * <br> should not be allowed since nl2br will be used
  60.     * when storing data.
  61.     *
  62.     * @access    private
  63.     *
  64.     * @todo Sofar, this does nuttin' ;-)
  65.     */
  66.     function MyTextSanitizer()
  67.     {
  68.  
  69.     }
  70.  
  71.     /**
  72.      * Access the only instance of this class
  73.      *
  74.      * @return    object 
  75.      *
  76.      * @static
  77.      * @staticvar   object 
  78.      */
  79.     function &getInstance()
  80.     {
  81.         static $instance;
  82.         if (!isset($instance)) {
  83.             $instance new MyTextSanitizer();
  84.         }
  85.         return $instance;
  86.     }
  87.  
  88.     /**
  89.      * Get the smileys
  90.      *
  91.      * @return    array 
  92.      */
  93.     function getSmileys()
  94.     {
  95.         return $this->smileys;
  96.     }
  97.  
  98.     /**
  99.      * Replace emoticons in the message with smiley images
  100.      *
  101.      * @param    string  $message 
  102.      *
  103.      * @return    string 
  104.      */
  105.     function smiley($message)
  106.     {
  107.         $db =Database::getInstance();
  108.         if (count($this->smileys== 0{
  109.             if ($getsmiles $db->query("SELECT * FROM ".$db->prefix("smiles"))){
  110.                 while ($smiles $db->fetchArray($getsmiles)) {
  111.                     $message str_replace($smiles['code']'<img src="'.XOOPS_UPLOAD_URL.'/'.htmlspecialchars($smiles['smile_url']).'" alt="" />'$message);
  112.                     array_push($this->smileys$smiles);
  113.                 }
  114.             }
  115.         }
  116.         elseif (is_array($this->smileys)) {
  117.             foreach ($this->smileys as $smile{
  118.                 $message str_replace($smile['code']'<img src="'.XOOPS_UPLOAD_URL.'/'.htmlspecialchars($smile['smile_url']).'" alt="" />'$message);
  119.             }
  120.         }
  121.         return $message;
  122.     }
  123.  
  124.     /**
  125.      * Make links in the text clickable
  126.      *
  127.      * @param   string  $text 
  128.      * @return  string 
  129.      ***/
  130.     function makeClickable(&$text)
  131.     {
  132.         $patterns array("/(^|[^]_a-z0-9-=\"'\/])([a-z]+?):\/\/([^, \r\n\"\(\)'<>]+)/i""/(^|[^]_a-z0-9-=\"'\/])www\.([a-z0-9\-]+)\.([^, \r\n\"\(\)'<>]+)/i""/(^|[^]_a-z0-9-=\"'\/])ftp\.([a-z0-9\-]+)\.([^, \r\n\"\(\)'<>]+)/i""/(^|[^]_a-z0-9-=\"'\/:\.])([a-z0-9\-_\.]+?)@([^, \r\n\"\(\)'<>\[\]]+)/i");
  133.         $replacements array("\\1<a href=\"\\2://\\3\" target=\"_blank\">\\2://\\3</a>""\\1<a href=\"http://www.\\2.\\3\" target=\"_blank\">www.\\2.\\3</a>""\\1<a href=\"ftp://ftp.\\2.\\3\" target=\"_blank\">ftp.\\2.\\3</a>""\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>");
  134.         return preg_replace($patterns$replacements$text);
  135.     }
  136.  
  137.     /**
  138.      * Replace XoopsCodes with their equivalent HTML formatting
  139.      *
  140.      * @param   string  $text 
  141.      * @param   bool    $allowimage Allow images in the text?
  142.      *                               On FALSE, uses links to images.
  143.      * @return  string 
  144.      ***/
  145.     function &xoopsCodeDecode(&$text$allowimage 1)
  146.     {
  147.         $patterns array();
  148.         $replacements array();
  149.         //$patterns[] = "/\[code](.*)\[\/code\]/esU";
  150.         //$replacements[] = "'<div class=\"xoopsCode\"><code><pre>'.wordwrap(MyTextSanitizer::htmlSpecialChars('\\1'), 100).'</pre></code></div>'";
  151.         // RMV: added new markup for intrasite url (allows easier site moves)
  152.         // TODO: automatically convert other URLs to this format if XOOPS_URL matches??
  153.         $patterns["/\[siteurl=(['\"]?)([^\"'<>]*)\\1](.*)\[\/siteurl\]/sU";
  154.         $replacements['<a href="'.XOOPS_URL.'/\\2">\\3</a>';
  155.         $patterns["/\[url=(['\"]?)(http[s]?:\/\/[^\"'<>]*)\\1](.*)\[\/url\]/sU";
  156.         $replacements['<a href="\\2" target="_blank">\\3</a>';
  157.         $patterns["/\[url=(['\"]?)(ftp?:\/\/[^\"'<>]*)\\1](.*)\[\/url\]/sU";
  158.         $replacements['<a href="\\2" target="_blank">\\3</a>';
  159.         $patterns["/\[url=(['\"]?)([^\"'<>]*)\\1](.*)\[\/url\]/sU";
  160.         $replacements['<a href="http://\\2" target="_blank">\\3</a>';
  161.         $patterns["/\[color=(['\"]?)([a-zA-Z0-9]*)\\1](.*)\[\/color\]/sU";
  162.         $replacements['<span style="color: #\\2;">\\3</span>';
  163.         $patterns["/\[size=(['\"]?)([a-z0-9-]*)\\1](.*)\[\/size\]/sU";
  164.         $replacements['<span style="font-size: \\2;">\\3</span>';
  165.         $patterns["/\[font=(['\"]?)([^;<>\*\(\)\"']*)\\1](.*)\[\/font\]/sU";
  166.         $replacements['<span style="font-family: \\2;">\\3</span>';
  167.         $patterns["/\[email]([^;<>\*\(\)\"']*)\[\/email\]/sU";
  168.         $replacements['<a href="mailto:\\1">\\1</a>';
  169.         $patterns["/\[b](.*)\[\/b\]/sU";
  170.         $replacements['<b>\\1</b>';
  171.         $patterns["/\[i](.*)\[\/i\]/sU";
  172.         $replacements['<i>\\1</i>';
  173.         $patterns["/\[u](.*)\[\/u\]/sU";
  174.         $replacements['<u>\\1</u>';
  175.         $patterns["/\[d](.*)\[\/d\]/sU";
  176.         $replacements['<del>\\1</del>';
  177.         //$patterns[] = "/\[li](.*)\[\/li\]/sU";
  178.         //$replacements[] = '<li>\\1</li>';
  179.         $patterns["/\[img align=(['\"]?)(left|center|right)\\1]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
  180.         $patterns["/\[img]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
  181.         $patterns["/\[img align=(['\"]?)(left|center|right)\\1 id=(['\"]?)([0-9]*)\\3]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
  182.         $patterns["/\[img id=(['\"]?)([0-9]*)\\1]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
  183.         if ($allowimage != 1{
  184.             $replacements['<a href="\\3" target="_blank">\\3</a>';
  185.             $replacements['<a href="\\1" target="_blank">\\1</a>';
  186.             $replacements['<a href="'.XOOPS_URL.'/image.php?id=\\4" target="_blank">\\5</a>';
  187.             $replacements['<a href="'.XOOPS_URL.'/image.php?id=\\2" target="_blank">\\3</a>';
  188.         else {
  189.             $replacements['<img src="\\3" align="\\2" alt="" />';
  190.             $replacements['<img src="\\1" alt="" />';
  191.             $replacements['<img src="'.XOOPS_URL.'/image.php?id=\\4" align="\\2" alt="\\5" />';
  192.             $replacements['<img src="'.XOOPS_URL.'/image.php?id=\\2" alt="\\3" />';
  193.         }
  194.         $patterns["/\[quote]/sU";
  195.         $replacements[_QUOTEC.'<div class="xoopsQuote"><blockquote>';
  196.         //$replacements[] = 'Quote: <div class="xoopsQuote"><blockquote>';
  197.         $patterns["/\[\/quote]/sU";
  198.         $replacements['</blockquote></div>';
  199.         $text str_replace"\x00"""$text );
  200.         $c "[\x01-\x1f]*";
  201.         $patterns["/j{$c}a{$c}v{$c}a{$c}s{$c}c{$c}r{$c}i{$c}p{$c}t{$c}:/si";
  202.         $replacements["(script removed)";
  203.         $patterns["/a{$c}b{$c}o{$c}u{$c}t{$c}:/si";
  204.         $replacements["about :";
  205.         $text preg_replace($patterns$replacements$text);
  206.         return $text;
  207.     }
  208.  
  209.     /**
  210.      * Convert linebreaks to <br /> tags
  211.      *
  212.      * @param    string  $text 
  213.      *
  214.      * @return    string 
  215.      */
  216.     function nl2Br($text)
  217.     {
  218.         return preg_replace("/(\015\012)|(\015)|(\012)/","<br />",$text);
  219.     }
  220.  
  221.     /**
  222.      * Add slashes to the text if magic_quotes_gpc is turned off.
  223.      *
  224.      * @param   string  $text 
  225.      * @return  string 
  226.      ***/
  227.     function addSlashes($text)
  228.     {
  229.         if (!get_magic_quotes_gpc()) {
  230.             $text addslashes($text);
  231.         }
  232.         return $text;
  233.     }
  234.     /*
  235.     * if magic_quotes_gpc is on, stirip back slashes
  236.     *
  237.     * @param    string  $text
  238.     *
  239.     * @return    string
  240.     */
  241.     function stripSlashesGPC($text)
  242.     {
  243.         if (get_magic_quotes_gpc()) {
  244.             $text stripslashes($text);
  245.         }
  246.         return $text;
  247.     }
  248.  
  249.     /*
  250.     *  for displaying data in html textbox forms
  251.     *
  252.     * @param    string  $text
  253.     *
  254.     * @return    string
  255.     */
  256.     function htmlSpecialChars($text)
  257.     {
  258.         //return preg_replace("/&amp;/i", '&', htmlspecialchars($text, ENT_QUOTES));
  259.         return preg_replace(array("/&amp;/i""/&nbsp;/i")array('&''&amp;nbsp;')htmlspecialchars($textENT_QUOTES));
  260.     }
  261.  
  262.     /**
  263.      * Reverses {@link htmlSpecialChars()}
  264.      *
  265.      * @param   string  $text 
  266.      * @return  string 
  267.      ***/
  268.     function undoHtmlSpecialChars$text )
  269.     {
  270.         return preg_replace(array("/&gt;/i""/&lt;/i""/&quot;/i""/'/i"'/&amp;nbsp;/i')array(">""<""\"""'""&nbsp;")$text);
  271.     }
  272.  
  273.     /**
  274.      * Filters textarea form data in DB for display
  275.      *
  276.      * @param   string  $text 
  277.      * @param   bool    $html   allow html?
  278.      * @param   bool    $smiley allow smileys?
  279.      * @param   bool    $xcode  allow xoopscode?
  280.      * @param   bool    $image  allow inline images?
  281.      * @param   bool    $br     convert linebreaks?
  282.      * @return  string 
  283.      ***/
  284.     function &displayTarea$text$html 0$smiley 1$xcode 1$image 1$br 1)
  285.     {
  286.         if ($html != 1{
  287.             // html not allowed
  288.             $text $this->htmlSpecialChars($text);
  289.         }
  290.         $text $this->codePreConv($text$xcode)// Ryuji_edit(2003-11-18)
  291.         $text $this->makeClickable($text);
  292.         if ($smiley != 0{
  293.             // process smiley
  294.             $text $this->smiley($text);
  295.         }
  296.         if ($xcode != 0{
  297.             // decode xcode
  298.             if ($image != 0{
  299.                 // image allowed
  300.                 $text $this->xoopsCodeDecode($text);
  301.             else {
  302.                 // image not allowed
  303.                 $text $this->xoopsCodeDecode($text0);
  304.             }
  305.         }
  306.         if ($br != 0{
  307.             $text $this->nl2Br($text);
  308.         }
  309.         $text $this->codeConv($text$xcode$image);    // Ryuji_edit(2003-11-18)
  310.         return $text;
  311.     }
  312.  
  313.     /**
  314.      * Filters textarea form data submitted for preview
  315.      *
  316.      * @param   string  $text 
  317.      * @param   bool    $html   allow html?
  318.      * @param   bool    $smiley allow smileys?
  319.      * @param   bool    $xcode  allow xoopscode?
  320.      * @param   bool    $image  allow inline images?
  321.      * @param   bool    $br     convert linebreaks?
  322.      * @return  string 
  323.      ***/
  324.     function &previewTarea$text$html 0$smiley 1$xcode 1$image 1$br 1)
  325.     {
  326.         $text $this->stripSlashesGPC($text);
  327.         if ($html != 1{
  328.             // html not allowed
  329.             $text $this->htmlSpecialChars($text);
  330.         }
  331.         $text $this->codePreConv($text$xcode)// Ryuji_edit(2003-11-18)
  332.         $text $this->makeClickable($text);
  333.         if ($smiley != 0{
  334.             // process smiley
  335.             $text $this->smiley($text);
  336.         }
  337.         if ($xcode != 0{
  338.             // decode xcode
  339.             if ($image != 0{
  340.                 // image allowed
  341.                 $text $this->xoopsCodeDecode($text);
  342.             else {
  343.                 // image not allowed
  344.                 $text $this->xoopsCodeDecode($text0);
  345.             }
  346.         }
  347.         if ($br != 0{
  348.             $text $this->nl2Br($text);
  349.         }
  350.         $text $this->codeConv($text$xcode$image);    // Ryuji_edit(2003-11-18)
  351.         return $text;
  352.     }
  353.  
  354.     /**
  355.      * Replaces banned words in a string with their replacements
  356.      *
  357.      * @param   string $text 
  358.      * @return  string 
  359.      *
  360.      * @deprecated
  361.      ***/
  362.     function &censorString(&$text)
  363.     {
  364.         if (!isset($this->censorConf)) {
  365.             $config_handler =xoops_gethandler('config');
  366.             $this->censorConf =$config_handler->getConfigsByCat(XOOPS_CONF_CENSOR);
  367.         }
  368.         if ($this->censorConf['censor_enable'== 1{
  369.             $replacement $this->censorConf['censor_replace'];
  370.             foreach ($this->censorConf['censor_words'as $bad{
  371.                 if !empty($bad) ) {
  372.                      $bad quotemeta($bad);
  373.                     $patterns["/(\s)".$bad."/siU";
  374.                     $replacements["\\1".$replacement;
  375.                     $patterns["/^".$bad."/siU";
  376.                     $replacements[$replacement;
  377.                     $patterns["/(\n)".$bad."/siU";
  378.                     $replacements["\\1".$replacement;
  379.                     $patterns["/]".$bad."/siU";
  380.                     $replacements["]".$replacement;
  381.                     $text preg_replace($patterns$replacements$text);
  382.                 }
  383.                }
  384.         }
  385.            return $text;
  386.     }
  387.  
  388.  
  389.     /**#@+
  390.      * Sanitizing of [code] tag
  391.      */
  392.     function codePreConv($text$xcode 1{
  393.         if($xcode != 0){
  394.             $patterns "/\[code](.*)\[\/code\]/esU";
  395.             $replacements "'[code]'.base64_encode('$1').'[/code]'";
  396.             $text =  preg_replace($patterns$replacements$text);
  397.         }
  398.         return $text;
  399.     }
  400.  
  401.     function codeConv($text$xcode 1$image 1){
  402.         if($xcode != 0){
  403.             $patterns "/\[code](.*)\[\/code\]/esU";
  404.             if ($image != 0{
  405.                 // image allowed
  406.                 $replacements "'<div class=\"xoopsCode\"><code><pre>'.MyTextSanitizer::codeSanitizer('$1').'</pre></code></div>'";
  407.                 //$text =& $this->xoopsCodeDecode($text);
  408.             else {
  409.                 // image not allowed
  410.                 $replacements "'<div class=\"xoopsCode\"><code><pre>'.MyTextSanitizer::codeSanitizer('$1', 0).'</pre></code></div>'";
  411.                 //$text =& $this->xoopsCodeDecode($text, 0);
  412.             }
  413.             $text =  preg_replace($patterns$replacements$text);
  414.         }
  415.         return $text;
  416.     }
  417.  
  418.     function codeSanitizer($str$image 1){
  419.         if($image != 0){
  420.             $str $this->xoopsCodeDecode(
  421.                 $this->htmlSpecialChars(str_replace('\"''"'base64_decode($str)))
  422.                 );
  423.         }else{
  424.             $str $this->xoopsCodeDecode(
  425.                 $this->htmlSpecialChars(str_replace('\"''"'base64_decode($str))),0
  426.                 );
  427.         }
  428.         return $str;
  429.     }
  430.  
  431.  
  432.     /**#@-*/
  433.  
  434.  
  435. ##################### Deprecated Methods ######################
  436.  
  437.     /**#@+
  438.      * @deprecated
  439.      */
  440.     function sanitizeForDisplay($text$allowhtml 0$smiley 1$bbcode 1)
  441.     {
  442.         if $allowhtml == {
  443.             $text $this->htmlSpecialChars($text);
  444.         else {
  445.             //$config =& $GLOBALS['xoopsConfig'];
  446.             //$allowed = $config['allowed_html'];
  447.             //$text = strip_tags($text, $allowed);
  448.             $text $this->makeClickable($text);
  449.         }
  450.         if $smiley == {
  451.             $text $this->smiley($text);
  452.         }
  453.         if $bbcode == {
  454.             $text $this->xoopsCodeDecode($text);
  455.         }
  456.         $text $this->nl2Br($text);
  457.         return $text;
  458.     }
  459.  
  460.     function sanitizeForPreview($text$allowhtml 0$smiley 1$bbcode 1)
  461.     {
  462.         $text $this->oopsStripSlashesGPC($text);
  463.         if $allowhtml == {
  464.             $text $this->htmlSpecialChars($text);
  465.         else {
  466.             //$config =& $GLOBALS['xoopsConfig'];
  467.             //$allowed = $config['allowed_html'];
  468.             //$text = strip_tags($text, $allowed);
  469.             $text $this->makeClickable($text);
  470.         }
  471.         if $smiley == {
  472.             $text $this->smiley($text);
  473.         }
  474.         if $bbcode == {
  475.             $text $this->xoopsCodeDecode($text);
  476.         }
  477.         $text $this->nl2Br($text);
  478.         return $text;
  479.     }
  480.  
  481.     function makeTboxData4Save($text)
  482.     {
  483.         //$text = $this->undoHtmlSpecialChars($text);
  484.         return $this->addSlashes($text);
  485.     }
  486.  
  487.     function makeTboxData4Show($text$smiley=0)
  488.     {
  489.         $text $this->htmlSpecialChars($text);
  490.         return $text;
  491.     }
  492.  
  493.     function makeTboxData4Edit($text)
  494.     {
  495.         return $this->htmlSpecialChars($text);
  496.     }
  497.  
  498.     function makeTboxData4Preview($text$smiley=0)
  499.     {
  500.         $text $this->stripSlashesGPC($text);
  501.         $text $this->htmlSpecialChars($text);
  502.         return $text;
  503.     }
  504.  
  505.     function makeTboxData4PreviewInForm($text)
  506.     {
  507.         $text $this->stripSlashesGPC($text);
  508.         return $this->htmlSpecialChars($text);
  509.     }
  510.  
  511.     function makeTareaData4Save($text)
  512.     {
  513.         return $this->addSlashes($text);
  514.     }
  515.  
  516.     function &makeTareaData4Show(&$text$html=1$smiley=1$xcode=1)
  517.     {
  518.         $text $this->displayTarea($text$html$smiley$xcode);
  519.         return $text;
  520.     }
  521.  
  522.     function makeTareaData4Edit($text)
  523.     {
  524.         return $this->htmlSpecialChars($text);
  525.     }
  526.  
  527.     function &makeTareaData4Preview(&$text$html=1$smiley=1$xcode=1)
  528.     {
  529.         $text $this->previewTarea($text$html$smiley$xcode);
  530.         return $text;
  531.     }
  532.  
  533.     function makeTareaData4PreviewInForm($text)
  534.     {
  535.         //if magic_quotes_gpc is on, do stipslashes
  536.         $text $this->stripSlashesGPC($text);
  537.         return $this->htmlSpecialChars($text);
  538.     }
  539.  
  540.     function makeTareaData4InsideQuotes($text)
  541.     {
  542.         return $this->htmlSpecialChars($text);
  543.     }
  544.  
  545.     function oopsStripSlashesGPC($text)
  546.     {
  547.         return $this->stripSlashesGPC($text);
  548.     }
  549.  
  550.     function oopsStripSlashesRT($text)
  551.     {
  552.         if (get_magic_quotes_runtime()) {
  553.             $text stripslashes($text);
  554.         }
  555.         return $text;
  556.     }
  557.  
  558.     function oopsAddSlashes($text)
  559.     {
  560.         return $this->addSlashes($text);
  561.     }
  562.  
  563.     function oopsHtmlSpecialChars($text)
  564.     {
  565.         return $this->htmlSpecialChars($text);
  566.     }
  567.  
  568.     function oopsNl2Br($text)
  569.     {
  570.         return $this->nl2br($text);
  571.     }
  572.     /**#@-*/
  573. }
  574. ?>

XOOPS Docs generated by phpDocumentor