make sure pt payments show on statement as pt paid (#4008)
[openemr.git] / library / htmlspecialchars.inc.php
bloba9bfd33a5abe1e6e23b6055c5ae83562055552cc
1 <?php
3 /**
4 * Escaping Functions
6 * @package OpenEMR
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
15 /**
16 * Escape a javascript literal.
18 function js_escape($text)
20 return json_encode($text);
23 /**
24 * Escape a javascript literal within html onclick attribute.
26 function attr_js($text)
28 return attr(json_encode($text));
31 /**
32 * Escape html and url encode a url item.
34 function attr_url($text)
36 return attr(urlencode($text));
39 /**
40 * Escape js and url encode a url item.
42 function js_url($text)
44 return js_escape(urlencode($text));
47 /**
48 * Escape variables that are outputted into the php error log.
50 function errorLogEscape($text)
52 return attr($text);
55 /**
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 . '"';
79 /**
80 * Escape a PHP string for use as (part of) an HTML / XML text node.
82 * It only escapes a few special chars: the ampersand (&) and both the left-
83 * pointing angle bracket (<) and the right-pointing angle bracket (>), since
84 * these are the only characters that are special in a text node. Minimal
85 * quoting is preferred because it produces smaller and more easily human-
86 * readable output.
88 * Some characters simply cannot appear in valid XML documents, even
89 * as entities but, this function does not attempt to handle them.
91 * NOTE: Attribute values are NOT text nodes, and require additional escaping.
93 * @param string $text The string to escape, possibly including "&", "<",
94 * or ">".
95 * @return string The string, with "&", "<", and ">" escaped.
97 function text($text)
99 return htmlspecialchars($text, ENT_NOQUOTES);
103 * Escape a PHP string for use as (part of) an HTML / XML attribute value.
105 * It escapes several special chars: the ampersand (&), the double quote
106 * ("), the singlequote ('), and both the left-pointing angle bracket (<)
107 * and the right-pointing angle bracket (>), since these are the characters
108 * that are special in an attribute value.
110 * Some characters simply cannot appear in valid XML documents, even
111 * as entities but, this function does not attempt to handle them.
113 * NOTE: This can be used as a "generic" HTML escape since it does maximal
114 * quoting. However, some HTML and XML contexts (CDATA) don't provide
115 * escape mechanisms. Also, further pre- or post-escaping might need to
116 * be done when embdedded other languages (like JavaScript) inside HTML /
117 * XML documents.
119 * @param string $text The string to escape, possibly including (&), (<),
120 * (>), ('), and (").
121 * @return string The string, with (&), (<), (>), ("), and (') escaped.
123 function attr($text)
125 return htmlspecialchars($text, ENT_QUOTES);
129 * Don't call this function. You don't see this function. This function
130 * doesn't exist.
132 * TODO: Hide this function so it can be called from this file but not from
133 * PHP that includes / requires this file. Either that, or write reasonable
134 * documentation and clean up the name.
136 function hsc_private_xl_or_warn($key)
138 if (function_exists('xl')) {
139 return xl($key);
140 } else {
141 trigger_error(
142 'Translation via xl() was requested, but the xl()'
143 . ' function is not defined, yet.',
144 E_USER_WARNING
146 return $key;
151 * Translate via xl() and then escape via text().
153 * @param string $key The string to escape, possibly including "&", "<",
154 * or ">".
155 * @return string The string, with "&", "<", and ">" escaped.
157 function xlt($key)
159 return text(hsc_private_xl_or_warn($key));
163 * Translate via xl() and then escape via attr().
165 * @param string $key The string to escape, possibly including (&), (<),
166 * (>), ('), and (").
167 * @return string The string, with (&), (<), (>), ("), and (') escaped.
169 function xla($key)
171 return attr(hsc_private_xl_or_warn($key));
175 * Translate via xl() and then escape via js_escape for use with javascript literals
177 function xlj($key)
179 return js_escape(hsc_private_xl_or_warn($key));
183 * Deprecated
184 *Translate via xl() and then escape via addslashes for use with javascript literals
186 function xls($key)
188 return addslashes(hsc_private_xl_or_warn($key));