Fully responsive globals.php with vertical menu (#2460)
[openemr.git] / library / sanitize.inc.php
blobea6092840c53b816035d31270dcd8dcfc7222434
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 https://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 use OpenEMR\Common\Utils\RandomGenUtils;
18 // Function to collect ip address(es)
19 function collectIpAddresses()
21 $mainIp = $_SERVER['REMOTE_ADDR'];
22 $stringIp = $mainIp;
24 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
25 $forwardIp = $_SERVER['HTTP_X_FORWARDED_FOR'];
26 $stringIp .= " (" . $forwardIp . ")";
29 return array(
30 'ip_string' => $stringIp,
31 'ip' => $mainIp,
32 'forward_ip' => $forwardIp
36 // Function to create a csrf_token
37 function createCsrfToken()
39 return RandomGenUtils::createUniqueToken();
42 // Function to collect the csrf token
43 function collectCsrfToken()
45 return $_SESSION['csrf_token'];
48 // Function to verify a csrf_token
49 function verifyCsrfToken($token)
51 if (empty(collectCsrfToken())) {
52 error_log("OpenEMR Error : OpenEMR is potentially not secure because CSRF token was not formed correctly.");
53 return false;
54 } elseif (empty($token)) {
55 return false;
56 } elseif (collectCsrfToken() == $token) {
57 return true;
58 } else {
59 return false;
63 function csrfNotVerified($toScreen = true, $toLog = true)
65 if ($toScreen) {
66 echo xlt('Authentication Error');
68 if ($toLog) {
69 error_log("OpenEMR CSRF token authentication error");
71 die;
74 // Sanitize a json encoded entry.
75 function json_sanitize($json)
77 if (json_decode($json)) {
78 return json_encode(json_decode($json, true));
79 } else {
80 error_log("OPENEMR ERROR: " . $json . " is not a valid json ");
81 return false;
85 // If the label contains any illegal characters, then the script will die.
86 function check_file_dir_name($label)
88 if (empty($label) || preg_match('/[^A-Za-z0-9_.-]/', $label)) {
89 error_log("ERROR: The following variable contains invalid characters:" . $label);
90 die(xlt("ERROR: The following variable contains invalid characters").": ". attr($label));
91 } else {
92 return $label;
96 // Convert all illegal characters to _
97 function convert_safe_file_dir_name($label)
99 return preg_replace('/[^A-Za-z0-9_.-]/', '_', $label);
102 // Convert all non A-Z a-z 0-9 characters to _
103 function convert_very_strict_label($label)
105 return preg_replace('/[^A-Za-z0-9]/', '_', $label);
108 //Basename functionality for nonenglish languages (without this, basename function omits nonenglish characters).
109 function basename_international($path)
111 $parts = preg_split('~[\\\\/]~', $path);
112 foreach ($parts as $key => $value) {
113 $encoded = urlencode($value);
114 $parts[$key] = $encoded;
117 $encoded_path = implode("/", $parts);
118 $encoded_file_name = basename($encoded_path);
119 $decoded_file_name = urldecode($encoded_file_name);
121 return $decoded_file_name;
126 * This function detects a MIME type for a file and check if it in the white list of the allowed mime types.
127 * @param string $file - file location.
128 * @param array|null $whiteList - array of mime types that allowed to upload.
130 // Regarding the variable below. In the case of multiple file upload the isWhiteList function will run multiple
131 // times, therefore, storing the white list in the variable below to prevent multiple requests from database.
132 $white_list = null;
133 function isWhiteFile($file)
135 global $white_list;
136 if (is_null($white_list)) {
137 $white_list = array();
138 $lres = sqlStatement("SELECT option_id FROM list_options WHERE list_id = 'files_white_list' AND activity = 1");
139 while ($lrow = sqlFetchArray($lres)) {
140 $white_list[] = $lrow['option_id'];
144 $mimetype = mime_content_type($file);
145 if (in_array($mimetype, $white_list)) {
146 return true;
147 } else {
148 $splitMimeType = explode('/', $mimetype);
149 $categoryType = $splitMimeType[0];
150 if (in_array($categoryType. '/*', $white_list)) {
151 return true;
155 return false;
158 // Sanitize a value to ensure it is a number.
159 function sanitizeNumber($number)
161 $clean_number = $number +0 ;
163 if ($clean_number==$number) {
164 return $clean_number;
165 } else {
166 error_log('Custom validation error: Parameter contains non-numeric value (A numeric value expected)');
167 return $clean_number;