using OemrUI class in Fees > Fee Sheet, Billing, Payment, Checkout, Batch Payments...
[openemr.git] / library / sanitize.inc.php
blob7a083467e04e1c2f91fd0329459606a6af769ea9
1 <?php
2 /**
3 * Function to check and/or sanitize things for security such as
4 * directories names, file names, etc.
5 * Also including csrf token management functions.
7 * @package OpenEMR
8 * @link http://www.open-emr.org
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @author Roberto Vasquez <robertogagliotta@gmail.com>
11 * @author Shachar Zilbershlag <shaharzi@matrix.co.il>
12 * @copyright Copyright (c) 2012-2018 Brady Miller <brady.g.miller@gmail.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 // Function to collect ip address(es)
17 function collectIpAddresses()
19 $mainIp = $_SERVER['REMOTE_ADDR'];
20 $stringIp = $mainIp;
22 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
23 $forwardIp = $_SERVER['HTTP_X_FORWARDED_FOR'];
24 $stringIp .= " (" . $forwardIp . ")";
27 return array(
28 'ip_string' => $stringIp,
29 'ip' => $mainIp,
30 'forward_ip' => $forwardIp
34 // Function to create a random unique token
35 // Length is in bytes that the openssl_random_pseudo_bytes() function will create
36 function createUniqueToken($length = 32)
38 try {
39 $uniqueToken = random_bytes($length);
40 } catch (Error $e) {
41 error_log('OpenEMR Error : OpenEMR is not working because of random_bytes() Error: ' . $e->getMessage());
42 die("OpenEMR Error : OpenEMR is not working because because of random_bytes() Error.");
43 } catch (Exception $e) {
44 error_log('OpenEMR Error : OpenEMR is not working because because of random_bytes() Exception: ' . $e->getMessage());
45 die("OpenEMR Error : OpenEMR is not working because because of random_bytes() Exception.");
48 $uniqueToken = base64_encode($uniqueToken);
50 if (empty($uniqueToken)) {
51 error_log("OpenEMR Error : OpenEMR is not working because a random unique token is not being formed correctly.");
52 die("OpenEMR Error : OpenEMR is not working because a random unique token is not being formed correctly.");
55 return $uniqueToken;
58 // Function to create a csrf_token
59 function createCsrfToken()
61 return createUniqueToken(32);
64 // Function to collect the csrf token
65 function collectCsrfToken()
67 return $_SESSION['csrf_token'];
70 // Function to verify a csrf_token
71 function verifyCsrfToken($token)
73 if (empty(collectCsrfToken())) {
74 error_log("OpenEMR Error : OpenEMR is potentially not secure because CSRF token was not formed correctly.");
75 return false;
76 } elseif (empty($token)) {
77 return false;
78 } elseif (collectCsrfToken() == $token) {
79 return true;
80 } else {
81 return false;
85 function csrfNotVerified($toScreen = true, $toLog = true)
87 if ($toScreen) {
88 echo xlt('Authentication Error');
90 if ($toLog) {
91 error_log("OpenEMR CSRF token authentication error");
93 die;
96 // Sanitize a json encoded entry.
97 function json_sanitize($json)
99 if (json_decode($json)) {
100 return json_encode(json_decode($json, true));
101 } else {
102 error_log("OPENEMR ERROR: " . $json . " is not a valid json ");
103 return false;
107 // If the label contains any illegal characters, then the script will die.
108 function check_file_dir_name($label)
110 if (empty($label) || preg_match('/[^A-Za-z0-9_.-]/', $label)) {
111 error_log("ERROR: The following variable contains invalid characters:" . $label);
112 die(xlt("ERROR: The following variable contains invalid characters").": ". attr($label));
113 } else {
114 return $label;
118 // Convert all illegal characters to _
119 function convert_safe_file_dir_name($label)
121 return preg_replace('/[^A-Za-z0-9_.-]/', '_', $label);
124 // Convert all non A-Z a-z 0-9 characters to _
125 function convert_very_strict_label($label)
127 return preg_replace('/[^A-Za-z0-9]/', '_', $label);
130 //Basename functionality for nonenglish languages (without this, basename function omits nonenglish characters).
131 function basename_international($path)
133 $parts = preg_split('~[\\\\/]~', $path);
134 foreach ($parts as $key => $value) {
135 $encoded = urlencode($value);
136 $parts[$key] = $encoded;
139 $encoded_path = implode("/", $parts);
140 $encoded_file_name = basename($encoded_path);
141 $decoded_file_name = urldecode($encoded_file_name);
143 return $decoded_file_name;
148 * This function detects a MIME type for a file and check if it in the white list of the allowed mime types.
149 * @param string $file - file location.
150 * @param array|null $whiteList - array of mime types that allowed to upload.
152 // Regarding the variable below. In the case of multiple file upload the isWhiteList function will run multiple
153 // times, therefore, storing the white list in the variable below to prevent multiple requests from database.
154 $white_list = null;
155 function isWhiteFile($file)
157 global $white_list;
158 if (is_null($white_list)) {
159 $white_list = array();
160 $lres = sqlStatement("SELECT option_id FROM list_options WHERE list_id = 'files_white_list' AND activity = 1");
161 while ($lrow = sqlFetchArray($lres)) {
162 $white_list[] = $lrow['option_id'];
166 $mimetype = mime_content_type($file);
167 if (in_array($mimetype, $white_list)) {
168 return true;
169 } else {
170 $splitMimeType = explode('/', $mimetype);
171 $categoryType = $splitMimeType[0];
172 if (in_array($categoryType. '/*', $white_list)) {
173 return true;
177 return false;
180 // Sanitize a value to ensure it is a number.
181 function sanitizeNumber($number)
183 $clean_number = $number +0 ;
185 if ($clean_number==$number) {
186 return $clean_number;
187 } else {
188 error_log('Custom validation error: Parameter contains non-numeric value (A numeric value expected)');
189 return $clean_number;