[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Filter/ -> OutputFilter.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Filter;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Language\Language;
  14  use Joomla\Filter\OutputFilter as BaseOutputFilter;
  15  use Joomla\String\StringHelper;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * OutputFilter
  23   *
  24   * @since  1.7.0
  25   */
  26  class OutputFilter extends BaseOutputFilter
  27  {
  28      /**
  29       * This method processes a string and replaces all instances of & with &amp; in links only.
  30       *
  31       * @param   string  $input  String to process
  32       *
  33       * @return  string  Processed string
  34       *
  35       * @since   1.7.0
  36       */
  37      public static function linkXHTMLSafe($input)
  38      {
  39          $regex = 'href="([^"]*(&(amp;){0})[^"]*)*?"';
  40  
  41          return preg_replace_callback("#$regex#i", array('\\Joomla\\CMS\\Filter\\OutputFilter', 'ampReplaceCallback'), $input);
  42      }
  43  
  44      /**
  45       * This method processes a string and escapes it for use in JavaScript
  46       *
  47       * @param   string  $string  String to process
  48       *
  49       * @return  string  Processed text
  50       */
  51      public static function stringJSSafe($string)
  52      {
  53          $chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
  54          $new_str = '';
  55  
  56          foreach ($chars as $chr) {
  57              $code = str_pad(dechex(StringHelper::ord($chr)), 4, '0', STR_PAD_LEFT);
  58  
  59              if (strlen($code) < 5) {
  60                  $new_str .= '\\u' . $code;
  61              } else {
  62                  $new_str .= '\\u{' . $code . '}';
  63              }
  64          }
  65  
  66          return $new_str;
  67      }
  68  
  69      /**
  70       * This method processes a string and replaces all accented UTF-8 characters by unaccented
  71       * ASCII-7 "equivalents", whitespaces are replaced by hyphens and the string is lowercase.
  72       *
  73       * @param   string  $string    String to process
  74       * @param   string  $language  Language to transliterate to
  75       *
  76       * @return  string  Processed string
  77       *
  78       * @since   1.7.0
  79       */
  80      public static function stringURLSafe($string, $language = '')
  81      {
  82          // Remove any '-' from the string since they will be used as concatenaters
  83          $str = str_replace('-', ' ', $string);
  84  
  85          // Transliterate on the language requested (fallback to current language if not specified)
  86          $lang = $language == '' || $language == '*' ? Factory::getLanguage() : Language::getInstance($language);
  87          $str = $lang->transliterate($str);
  88  
  89          // Trim white spaces at beginning and end of alias and make lowercase
  90          $str = trim(StringHelper::strtolower($str));
  91  
  92          // Remove any apostrophe. We do it here to ensure it is not replaced by a '-'
  93          $str = str_replace("'", '', $str);
  94  
  95          // Remove any duplicate whitespace, and ensure all characters are alphanumeric
  96          $str = preg_replace('/(\s|[^A-Za-z0-9\-])+/', '-', $str);
  97  
  98          // Trim dashes at beginning and end of alias
  99          $str = trim($str, '-');
 100  
 101          return $str;
 102      }
 103  
 104      /**
 105       * Callback method for replacing & with &amp; in a string
 106       *
 107       * @param   string  $m  String to process
 108       *
 109       * @return  string  Replaced string
 110       *
 111       * @since   3.5
 112       */
 113      public static function ampReplaceCallback($m)
 114      {
 115          $rx = '&(?!amp;)';
 116  
 117          return preg_replace('#' . $rx . '#', '&amp;', $m[0]);
 118      }
 119  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer