7 * @link https://www.open-emr.org
8 * @author Boyd Stephen Smith Jr.
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2011 Boyd Stephen Smith Jr.
11 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 * Escape a javascript literal.
18 function js_escape($text)
20 return json_encode($text);
24 * Escape a javascript literal within html onclick attribute.
26 function attr_js($text)
28 return attr(json_encode($text));
32 * Escape html and url encode a url item.
34 function attr_url($text)
36 return attr(urlencode($text ??
''));
40 * Escape js and url encode a url item.
42 function js_url($text)
44 return js_escape(urlencode($text));
48 * Escape variables that are outputted into the php error log.
50 function errorLogEscape($text)
56 * Escape variables that are outputted into csv and spreadsheet files.
57 * See here: https://www.owasp.org/index.php/CSV_Injection
58 * Based mitigation strategy on this report: https://asecurityz.blogspot.com/2017/12/csv-injection-mitigations.html
59 * 1. Remove all the following characters: = + " |
60 * 2. Only remove leading - characters (since need in dates)
61 * 3. Only remove leading @ characters (since need in email addresses)
62 * 4. Surround with double quotes (no reference link, but seems very reasonable, which will prevent commas from breaking things).
63 * If needed in future, will add a second parameter called 'options' which will be an array of option tokens that will allow
64 * less stringent (or more stringent) mechanisms to escape for csv.
66 function csvEscape($text)
68 // 1. Remove all the following characters: = + " |
69 $text = preg_replace('/[=+"|]/', '', $text);
71 // 2. Only remove leading - characters (since need in dates)
72 // 3. Only remove leading @ characters (since need in email addresses)
73 $text = preg_replace('/^[\-@]+/', '', $text);
75 // 4. Surround with double quotes (no reference link, but seems very reasonable, which will prevent commas from breaking things).
76 return '"' . $text . '"';
81 * references: https://stackoverflow.com/questions/3426090/how-do-you-make-strings-xml-safe
82 * https://www.php.net/htmlspecialchars
83 * https://www.php.net/XMLWriter
87 * TODO: not sure if need to escape ' and ", which are escaping for now (via the ENT_QUOTES flag)
89 function xmlEscape($text)
91 return htmlspecialchars(($text ??
''), ENT_XML1 | ENT_QUOTES
);
95 * Special function to remove the 'javascript' string (case insensitive) for when including a variable within a html link
97 function javascriptStringRemove($text)
99 return str_ireplace('javascript', '', $text ??
'');
103 * Escape a PHP string for use as (part of) an HTML / XML text node.
105 * It only escapes a few special chars: the ampersand (&) and both the left-
106 * pointing angle bracket (<) and the right-pointing angle bracket (>), since
107 * these are the only characters that are special in a text node. Minimal
108 * quoting is preferred because it produces smaller and more easily human-
111 * Some characters simply cannot appear in valid XML documents, even
112 * as entities but, this function does not attempt to handle them.
114 * NOTE: Attribute values are NOT text nodes, and require additional escaping.
116 * @param string $text The string to escape, possibly including "&", "<",
118 * @return string The string, with "&", "<", and ">" escaped.
122 return htmlspecialchars(($text ??
''), ENT_NOQUOTES
);
126 * Given an array of properties run through both the keys and values of the array and
127 * escape each PHP string for use as (part of) an HTML / XML text node.
129 * It only escapes a few special chars: the ampersand (&) and both the left-
130 * pointing angle bracket (<) and the right-pointing angle bracket (>), since
131 * these are the only characters that are special in a text node. Minimal
132 * quoting is preferred because it produces smaller and more easily human-
135 * Some characters simply cannot appear in valid XML documents, even
136 * as entities but, this function does not attempt to handle them.
138 * NOTE: Attribute values are NOT text nodes, and require additional escaping.
140 * @param string $arr The array of strings to escape, possibly including "&", "<",
142 * @param int $depth The current recursive depth of the escaping function. Defaults to 0 for initial call
143 * @return array The array that has each key and property escaped.
145 function textArray(array $arr, $depth = 0)
148 throw new \
InvalidArgumentException("array was nested too deep for escaping. Max limit reached");
152 foreach ($arr as $key => $value) {
153 if (is_array($value)) {
154 $newArray[text($key)] = textArray($value, $depth +
1);
156 $newArray[text($key)] = text($value);
163 * Escape a PHP string for use as (part of) an HTML / XML attribute value.
165 * It escapes several special chars: the ampersand (&), the double quote
166 * ("), the singlequote ('), and both the left-pointing angle bracket (<)
167 * and the right-pointing angle bracket (>), since these are the characters
168 * that are special in an attribute value.
170 * Some characters simply cannot appear in valid XML documents, even
171 * as entities but, this function does not attempt to handle them.
173 * NOTE: This can be used as a "generic" HTML escape since it does maximal
174 * quoting. However, some HTML and XML contexts (CDATA) don't provide
175 * escape mechanisms. Also, further pre- or post-escaping might need to
176 * be done when embdedded other languages (like JavaScript) inside HTML /
179 * @param string $text The string to escape, possibly including (&), (<),
181 * @return string The string, with (&), (<), (>), ("), and (') escaped.
185 return htmlspecialchars(($text ??
''), ENT_QUOTES
);
189 * Don't call this function. You don't see this function. This function
192 * TODO: Hide this function so it can be called from this file but not from
193 * PHP that includes / requires this file. Either that, or write reasonable
194 * documentation and clean up the name.
196 function hsc_private_xl_or_warn($key)
198 if (function_exists('xl')) {
202 'Translation via xl() was requested, but the xl()'
203 . ' function is not defined, yet.',
211 * Translate via xl() and then escape via text().
213 * @param string $key The string to escape, possibly including "&", "<",
215 * @return string The string, with "&", "<", and ">" escaped.
219 return text(hsc_private_xl_or_warn($key));
223 * Translate via xl() and then escape via attr().
225 * @param string $key The string to escape, possibly including (&), (<),
227 * @return string The string, with (&), (<), (>), ("), and (') escaped.
231 return attr(hsc_private_xl_or_warn($key));
235 * Translate via xl() and then escape via js_escape for use with javascript literals
239 return js_escape(hsc_private_xl_or_warn($key));
244 *Translate via xl() and then escape via addslashes for use with javascript literals
248 return addslashes(hsc_private_xl_or_warn($key));