Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file textsanitizer.php

Documentation is available at textsanitizer.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright (c) 2001, Zikula Development Team
  6.  * @link http://www.zikula.org
  7.  * @version $Id: textsanitizer.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  */
  10.  
  11. /**
  12.  * Text encoding style
  13.  */
  14. define("TS_DEFAULT"0);
  15. define("TS_ZIKULA"1);
  16. define("TS_WIKI"2);
  17. define("TS_BBCODE"3);
  18.  
  19. /**
  20.  * @package Zikula_Core
  21.  * @subpackage Zikula_legacy
  22.  */
  23. class TextSanitizer {
  24.     // added by sebastien, for the treatment of wiki ...
  25.     var $typeCoding;
  26.     // this one is use to fake the view of the text if unfortunately there are some
  27.     // _TP, _TW ... in it.
  28.     function de_typocode($text)
  29.     {
  30.         $retour str_replace('_TW'''$text);
  31.         $retour str_replace('_TB'''$retour);
  32.         $retour str_replace('_TP'''$retour);
  33.         return ($retour);
  34.     }
  35.     // this one is supposed to return a 0 to 3 when reveiving "", "postnuke","wiki, "bbcode"
  36.     function typocode($cType)
  37.     {
  38.         switch ($cType{
  39.             case ('postnuke':
  40.                 $nRetour TS_ZIKULA;
  41.                 break;
  42.             case ('wiki':
  43.                 $nRetour TS_WIKI;
  44.                 break;
  45.             case ('bbcode':
  46.                 $nRetour TS_BBCODE;
  47.                 break;
  48.             default :
  49.                 $nRetour TS_DEFAULT;
  50.         }
  51.         return($nRetour);
  52.     }
  53.     // thisone to fill $this->typeCoding with a 0, 1, 2, 3
  54.     // this is where I am stocking the value of Format_type coming from the
  55.     // table stories.
  56.     function fillFormat_type($nType)
  57.     {
  58.         $this->typeCoding = $nType;
  59.     }
  60.     // end of added by sebastien, for the treatment of wiki ...
  61.     function makeClickable($text)
  62.     {
  63.         // Modified by Nathan Codding - July 20, 2000.
  64.         // Made it only work on URLs and e-mail addresses preceeded by a space, in order to stop
  65.         // mangling HTML code.
  66.         // The Following function was taken from the Scriplets area of http://www.phpwizard.net, and was written by Tobias Ratschiller.
  67.         // Visit phpwizard.net today, its an excellent site!
  68.         // original make_clickable
  69.         $ret eregi_replace(" ([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])"" <a href='\\1://\\2\\3' target='_blank' target='_new'>\\1://\\2\\3</a>"$text);
  70.         $ret eregi_replace(" (([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))"" <a href='mailto:\\1' target='_new'>\\1</a>"$ret);
  71.         return $ret;
  72.     }
  73.  
  74.     function undoHtmlSpecialChars($input)
  75.     {
  76.         // Simon Birtwistle
  77.         // Takes a string, and does the reverse of the PHP standard function htmlspecialchars().
  78.         // Note htmlspecialchars_decode is only available in PHP >=5.2
  79.         if !function_exists('htmlspecialchars_decode') ) {
  80.            function htmlspecialchars_decode($text{
  81.                return strtr($textarray_flip(get_html_translation_table(HTML_SPECIALCHARS)));
  82.            }
  83.         }
  84.         return htmlspecialchars_decode($input);
  85.     }
  86.  
  87.     function oopsNl2Br($string)
  88.     {
  89.         $string preg_replace("/(\015\012)|(\015)|(\012)/""<br>"$string);
  90.         $string str_replace("<br><br><br>""<br>"$string);
  91.         return $string;
  92.     }
  93.  
  94.     function oopsAddSlashes($text)
  95.     {
  96.         if (!get_magic_quotes_gpc()) {
  97.             $text addslashes($text);
  98.         }
  99.         return $text;
  100.     }
  101.  
  102.     function oopsStripSlashes($text)
  103.     {
  104.         $text stripslashes($text);
  105.         return $text;
  106.     }
  107.  
  108.     function sanitizeIntoDB($text)
  109.     {
  110.         // dbescape checks the database type and escapes appropriately.
  111.         $text dbescape($text);
  112.         return $text;
  113.     }
  114.  
  115.     function sanitizeToTextarea($text)
  116.     {
  117.         $text $this->oopsStripSlashes($text);
  118.         return $text;
  119.     }
  120.  
  121.     function sanitizeForDisplay($text$allowhtml 0)
  122.     {
  123.         $numargs func_num_args();
  124.         if (get_magic_quotes_runtime()) {
  125.             $text $this->oopsStripSlashes($text);
  126.         }
  127.         if ($numargs == 4{
  128.             $text $this->makeClickable($text);
  129.             if ($allowhtml == 0{
  130.                 $text htmlspecialchars($text);
  131.             }
  132.         }
  133.  
  134.         return $text;
  135.     }
  136.  
  137.     function sanitizeForPreview($text$allowhtml 0)
  138.     {
  139.         $numargs func_num_args();
  140.         if (get_magic_quotes_gpc()) {
  141.             $text $this->oopsStripSlashes($text);
  142.         }
  143.         if ($numargs == 4{
  144.             $text $this->makeClickable($text);
  145.             if ($allowhtml == 0{
  146.                 $text htmlspecialchars($text);
  147.             }
  148.             $text transform($text$this->typeCoding);
  149.             $text $this->de_typocode($text);
  150.             $text $this->oopsNl2Br($text);
  151.         }
  152.  
  153.         return $text;
  154.     }
  155. }
  156.  
  157. /**
  158.  * @package Zikula_Core
  159.  * @subpackage Zikula_legacy
  160.  */
  161. class MyTextSanitizer extends TextSanitizer {
  162.     // Allow no html tags for textbox data
  163.     // Smiley can be enabled/disabled for both textbox and textarea data
  164.     // Allow only the following html tags for textarea data
  165.     // <br> is not allowed since nl2br will be used when storing data
  166.     var $allowed = "<a>,<b>,<blockquote>,<img>,<code>,<div>,<em>,<i>,<li>,<ol>,<p>,<pre>,<strike>,<strong>,<sub>,<sup>,<tt>,<u>,<ul>,<image>,<hr>,%%%";
  167.     // called before saving textbox form data
  168.     function makeTboxData4Save($text)
  169.     {
  170.         $text $this->undoHtmlSpecialChars($text);
  171.         $text strip_tags($text'')// strip all html tags SF bug #457478
  172.         // we are preparing for a save so send to DB sanitize method
  173.         $text $this->sanitizeIntoDB($text);
  174.         return $text;
  175.     }
  176.     // called before displaying textbox form data
  177.     // smiley can be used if you want
  178.     function makeTboxData4Show($text$smiley 0)
  179.     {
  180.         $text $this->sanitizeForDisplay($text0$smiley0)//do htmlspecialchars
  181.         return $text;
  182.     }
  183.     // called before editting textbox form data
  184.     function makeTboxData4Edit($text)
  185.     {
  186.         $text $this->sanitizeForDisplay($text000)//do htmlspecialchars
  187.         return $text;
  188.     }
  189.     // called before preview of textbox form data
  190.     // smiley can be used if you want
  191.     // use makeTboxData4PreviewInForm when you want textbox data to be previewed in textbox again
  192.     function makeTboxData4Preview($text$smiley 0)
  193.     {
  194.         $text $this->sanitizeForPreview($text0$smiley0)//do htmlspecialchars
  195.         return $text;
  196.     }
  197.  
  198.     function makeTboxData4PreviewInForm($text)
  199.     {
  200.         $text $this->sanitizeForPreview($text000)//do htmlspecialchars
  201.         return $text;
  202.     }
  203.     // functions for filtering textarea form data
  204.     function sanitizeTotextarea4Edit($text)
  205.     {
  206.         if (get_magic_quotes_runtime()) {
  207.             $text stripslashes($text);
  208.         }
  209.         return $text;
  210.     }
  211.  
  212.     function sanitizeTotextarea4Preview($text)
  213.     {
  214.         if (get_magic_quotes_gpc()) {
  215.             $text stripslashes($text);
  216.         }
  217.         $text strip_tags($text$this->allowed)// strip unallowed html tags
  218.         return $text;
  219.     }
  220.     // called before saving first time data or editted textarea data
  221.     function makeTareaData4Save($text)
  222.     {
  223.         $text strip_tags($text$this->allowed)// strip unallowed html tags
  224.         // we are preparing for a save so send to DB sanitize method
  225.         $text $this->sanitizeIntoDB($text);
  226.         return $text;
  227.     }
  228.     // called before displaying textarea form data
  229.     function makeTareaData4Show($text$allowhtml 1$smiley 0$bbcode 0)
  230.     {
  231.         $text $this->sanitizeForDisplay($text$allowhtml$smiley$bbcode);
  232.         return $text;
  233.     }
  234.     // called before editting textarea form data
  235.     function makeTareaData4Edit($text)
  236.     {
  237.         // if magic_quotes_runtime is on, do stipslashes
  238.         $text $this->sanitizeTotextarea4Edit($text);
  239.         return $text;
  240.     }
  241.     // called before previewing textarea form data
  242.     function makeTareaData4Preview($text$allowhtml 1$smiley 0$bbcode 0)
  243.     {
  244.         $text strip_tags($text$this->allowed)// strip unallowed html tags
  245.         $text $this->sanitizeForPreview($text$allowhtml$smiley$bbcode);
  246.         return $text;
  247.     }
  248.     // called before previewing textarea form data
  249.     // this time, text area data is inserted into textarea again
  250.     function makeTareaData4PreviewInForm($text)
  251.     {
  252.         // if magic_quotes_gpc is on, do stipslashes
  253.         $text $this->sanitizeTotextarea4Preview($text);
  254.         return $text;
  255.     }
  256. }

Documentation generated on Fri, 18 Jul 2008 21:58:25 +0200 by phpDocumentor 1.4.1