3 * Function to check and/or sanitize things for security such as
4 * directories names, file names, etc.
7 * @link http://www.open-emr.org
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @author Roberto Vasquez <robertogagliotta@gmail.com>
10 * @author Shachar Zilbershlag <shaharzi@matrix.co.il>
11 * @copyright Copyright (c) 2012-2017 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 // If the label contains any illegal characters, then the script will die.
17 function check_file_dir_name($label)
19 if (empty($label) ||
preg_match('/[^A-Za-z0-9_.-]/', $label)) {
20 error_log("ERROR: The following variable contains invalid characters:" . $label);
21 die(xlt("ERROR: The following variable contains invalid characters").": ". attr($label));
25 // Convert all illegal characters to _
26 function convert_safe_file_dir_name($label)
28 return preg_replace('/[^A-Za-z0-9_.-]/','_',$label);
31 //Basename functionality for nonenglish languages (without this, basename function ommits nonenglish characters).
32 function basename_international($path)
34 $parts = preg_split('~[\\\\/]~', $path);
35 foreach ($parts as $key => $value){
36 $encoded = urlencode($value);
37 $parts[$key] = $encoded;
39 $encoded_path = implode("/", $parts);
40 $encoded_file_name = basename($encoded_path);
41 $decoded_file_name = urldecode($encoded_file_name);
43 return $decoded_file_name;
48 * This function detects a MIME type for a file and check if it in the white list of the allowed mime types.
49 * @param string $file - file location.
50 * @param array|null $whiteList - array of mime types that allowed to upload.
52 // Regarding the variable below. In the case of multiple file upload the isWhiteList function will run multiple
53 // times, therefore, storing the white list in the variable below to prevent multiple requests from database.
55 function isWhiteFile($file)
58 if(is_null($white_list)){
59 $white_list = array();
60 $lres = sqlStatement("SELECT option_id FROM list_options WHERE list_id = 'files_white_list' AND activity = 1");
61 while ($lrow = sqlFetchArray($lres)) {
62 $white_list[] = $lrow['option_id'];
66 $mimetype = mime_content_type($file);
67 if(in_array($mimetype, $white_list)){
70 $splitMimeType = explode('/', $mimetype);
71 $categoryType = $splitMimeType[0];
72 if(in_array($categoryType. '/*', $white_list))return true;