Source for file output.php

Documentation is available at output.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Filter
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * JFilterOutput
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Filter
  17.  * @since       11.1
  18.  */
  19. {
  20.     /**
  21.      * Makes an object safe to display in forms
  22.      *
  23.      * Object parameters that are non-string, array, object or start with underscore
  24.      * will be converted
  25.      *
  26.      * @param   object   &$mixed        An object to be parsed
  27.      * @param   integer  $quote_style   The optional quote style for the htmlspecialchars function
  28.      * @param   mixed    $exclude_keys  An optional string single field name or array of field names not
  29.      *                                   to be parsed (eg, for a textarea)
  30.      *
  31.      * @return  void 
  32.      *
  33.      * @since   11.1
  34.      */
  35.     public static function objectHTMLSafe(&$mixed$quote_style ENT_QUOTES$exclude_keys '')
  36.     {
  37.         if (is_object($mixed))
  38.         {
  39.             foreach (get_object_vars($mixedas $k => $v)
  40.             {
  41.                 if (is_array($v|| is_object($v|| $v == null || substr($k11== '_')
  42.                 {
  43.                     continue;
  44.                 }
  45.  
  46.                 if (is_string($exclude_keys&& $k == $exclude_keys)
  47.                 {
  48.                     continue;
  49.                 }
  50.                 elseif (is_array($exclude_keys&& in_array($k$exclude_keys))
  51.                 {
  52.                     continue;
  53.                 }
  54.  
  55.                 $mixed->$k htmlspecialchars($v$quote_style'UTF-8');
  56.             }
  57.         }
  58.     }
  59.  
  60.     /**
  61.      * This method processes a string and replaces all instances of & with &amp; in links only.
  62.      *
  63.      * @param   string  $input  String to process
  64.      *
  65.      * @return  string  Processed string
  66.      *
  67.      * @since   11.1
  68.      */
  69.     public static function linkXHTMLSafe($input)
  70.     {
  71.         $regex 'href="([^"]*(&(amp;){0})[^"]*)*?"';
  72.  
  73.         return preg_replace_callback("#$regex#i"array('JFilterOutput''_ampReplaceCallback')$input);
  74.     }
  75.  
  76.     /**
  77.      * This method processes a string and replaces all accented UTF-8 characters by unaccented
  78.      * ASCII-7 "equivalents", whitespaces are replaced by hyphens and the string is lowercase.
  79.      *
  80.      * @param   string  $string  String to process
  81.      *
  82.      * @return  string  Processed string
  83.      *
  84.      * @since   11.1
  85.      */
  86.     public static function stringURLSafe($string)
  87.     {
  88.         // Remove any '-' from the string since they will be used as concatenaters
  89.         $str str_replace('-'' '$string);
  90.  
  91.         $lang JFactory::getLanguage();
  92.         $str $lang->transliterate($str);
  93.  
  94.         // Trim white spaces at beginning and end of alias and make lowercase
  95.         $str trim(JString::strtolower($str));
  96.  
  97.         // Remove any duplicate whitespace, and ensure all characters are alphanumeric
  98.         $str preg_replace('/(\s|[^A-Za-z0-9\-])+/''-'$str);
  99.  
  100.         // Trim dashes at beginning and end of alias
  101.         $str trim($str'-');
  102.  
  103.         return $str;
  104.     }
  105.  
  106.     /**
  107.      * This method implements unicode slugs instead of transliteration.
  108.      *
  109.      * @param   string  $string  String to process
  110.      *
  111.      * @return  string  Processed string
  112.      *
  113.      * @since   11.1
  114.      */
  115.     public static function stringURLUnicodeSlug($string)
  116.     {
  117.         // Replace double byte whitespaces by single byte (East Asian languages)
  118.         $str preg_replace('/\xE3\x80\x80/'' '$string);
  119.  
  120.         // Remove any '-' from the string as they will be used as concatenator.
  121.         // Would be great to let the spaces in but only Firefox is friendly with this
  122.  
  123.         $str str_replace('-'' '$str);
  124.  
  125.         // Replace forbidden characters by whitespaces
  126.         $str preg_replace('#[:\#\*"@+=;!><&\.%()\]\/\'\\\\|\[]#'"\x20"$str);
  127.  
  128.         // Delete all '?'
  129.         $str str_replace('?'''$str);
  130.  
  131.         // Trim white spaces at beginning and end of alias and make lowercase
  132.         $str trim(JString::strtolower($str));
  133.  
  134.         // Remove any duplicate whitespace and replace whitespaces by hyphens
  135.         $str preg_replace('#\x20+#''-'$str);
  136.  
  137.         return $str;
  138.     }
  139.  
  140.     /**
  141.      * Replaces &amp; with & for XHTML compliance
  142.      *
  143.      * @param   string  $text  Text to process
  144.      *
  145.      * @return  string  Processed string.
  146.      *
  147.      * @since   11.1
  148.      *
  149.      * @todo There must be a better way???
  150.      */
  151.     public static function ampReplace($text)
  152.     {
  153.         $text str_replace('&&''*--*'$text);
  154.         $text str_replace('&#''*-*'$text);
  155.         $text str_replace('&amp;''&'$text);
  156.         $text preg_replace('|&(?![\w]+;)|''&amp;'$text);
  157.         $text str_replace('*-*''&#'$text);
  158.         $text str_replace('*--*''&&'$text);
  159.  
  160.         return $text;
  161.     }
  162.  
  163.     /**
  164.      * Callback method for replacing & with &amp; in a string
  165.      *
  166.      * @param   string  $m  String to process
  167.      *
  168.      * @return  string  Replaced string
  169.      *
  170.      * @since   11.1
  171.      */
  172.     public static function _ampReplaceCallback($m)
  173.     {
  174.         $rx '&(?!amp;)';
  175.  
  176.         return preg_replace('#' $rx '#''&amp;'$m[0]);
  177.     }
  178.  
  179.     /**
  180.      * Cleans text of all formatting and scripting code
  181.      *
  182.      * @param   string  &$text  Text to clean
  183.      *
  184.      * @return  string  Cleaned text.
  185.      *
  186.      * @since   11.1
  187.      */
  188.     public static function cleanText(&$text)
  189.     {
  190.         $text preg_replace("'<script[^>]*>.*?</script>'si"''$text);
  191.         $text preg_replace('/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is''\2 (\1)'$text);
  192.         $text preg_replace('/<!--.+?-->/'''$text);
  193.         $text preg_replace('/{.+?}/'''$text);
  194.         $text preg_replace('/&nbsp;/'' '$text);
  195.         $text preg_replace('/&amp;/'' '$text);
  196.         $text preg_replace('/&quot;/'' '$text);
  197.         $text strip_tags($text);
  198.         $text htmlspecialchars($textENT_COMPAT'UTF-8');
  199.  
  200.         return $text;
  201.     }
  202.  
  203.     /**
  204.      * Strip img-tags from string
  205.      *
  206.      * @param   string  $string  Sting to be cleaned.
  207.      *
  208.      * @return  string  Cleaned string
  209.      *
  210.      * @since   11.1
  211.      */
  212.     public static function stripImages($string)
  213.     {
  214.         return preg_replace('#(<[/]?img.*>)#U'''$string);
  215.     }
  216.  
  217.     /**
  218.      * Strip iframe-tags from string
  219.      *
  220.      * @param   string  $string  Sting to be cleaned.
  221.      *
  222.      * @return  string  Cleaned string
  223.      *
  224.      * @since   12.2
  225.      */
  226.     public static function stripIframes($string)
  227.     {
  228.         return preg_replace('#(<[/]?iframe.*>)#U'''$string);
  229.     }
  230. }

Documentation generated on Tue, 19 Nov 2013 15:09:54 +0100 by phpDocumentor 1.4.3