1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Filter
5 *
6 * @copyright Copyright (C) 2005 - 2017 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 use Joomla\Filter\OutputFilter;
13 use Joomla\String\StringHelper;
14
15 /**
16 * JFilterOutput
17 *
18 * @since 11.1
19 */
20 class JFilterOutput extends OutputFilter
21 {
22 /**
23 * This method processes a string and replaces all instances of & with & in links only.
24 *
25 * @param string $input String to process
26 *
27 * @return string Processed string
28 *
29 * @since 11.1
30 */
31 public static function linkXHTMLSafe($input)
32 {
33 $regex = 'href="([^"]*(&(amp;){0})[^"]*)*?"';
34
35 return preg_replace_callback("#$regex#i", array('JFilterOutput', '_ampReplaceCallback'), $input);
36 }
37
38 /**
39 * This method processes a string and escapes it for use in JavaScript
40 *
41 * @param string $string String to process
42 *
43 * @return string Processed text
44 */
45 public static function stringJSSafe($string)
46 {
47 for ($i = 0, $l = strlen($string), $new_str = ''; $i < $l; $i++)
48 {
49 $new_str .= (ord(substr($string, $i, 1)) < 16 ? '\\x0' : '\\x') . dechex(ord(substr($string, $i, 1)));
50 }
51
52 return $new_str;
53 }
54
55 /**
56 * This method processes a string and replaces all accented UTF-8 characters by unaccented
57 * ASCII-7 "equivalents", whitespaces are replaced by hyphens and the string is lowercase.
58 *
59 * @param string $string String to process
60 * @param string $language Language to transilterate to
61 *
62 * @return string Processed string
63 *
64 * @since 11.1
65 */
66 public static function stringURLSafe($string, $language = '')
67 {
68 // Remove any '-' from the string since they will be used as concatenaters
69 $str = str_replace('-', ' ', $string);
70
71 // Transliterate on the language requested (fallback to current language if not specified)
72 $lang = $language == '' || $language == '*' ? JFactory::getLanguage() : JLanguage::getInstance($language);
73 $str = $lang->transliterate($str);
74
75 // Trim white spaces at beginning and end of alias and make lowercase
76 $str = trim(StringHelper::strtolower($str));
77
78 // Remove any duplicate whitespace, and ensure all characters are alphanumeric
79 $str = preg_replace('/(\s|[^A-Za-z0-9\-])+/', '-', $str);
80
81 // Trim dashes at beginning and end of alias
82 $str = trim($str, '-');
83
84 return $str;
85 }
86
87 /**
88 * Callback method for replacing & with & in a string
89 *
90 * @param string $m String to process
91 *
92 * @return string Replaced string
93 *
94 * @since 3.5
95 */
96 public static function ampReplaceCallback($m)
97 {
98 $rx = '&(?!amp;)';
99
100 return preg_replace('#' . $rx . '#', '&', $m[0]);
101 }
102
103 /**
104 * Callback method for replacing & with & in a string
105 *
106 * @param string $m String to process
107 *
108 * @return string Replaced string
109 *
110 * @since 11.1
111 * @deprecated 4.0 Use JFilterOutput::ampReplaceCallback() instead
112 */
113 public static function _ampReplaceCallback($m)
114 {
115 return static::ampReplaceCallback($m);
116 }
117 }
118