Permalink
Browse files

Fixed php basename function to work with nonenglish filenames.

  • Loading branch information...
1 parent 68b9668 commit 0f235648e8c38403b443dcfe465682069310e88e @shachar058 shachar058 committed with bradymiller Nov 1, 2016
Showing with 33 additions and 18 deletions.
  1. +16 −16 controllers/C_Document.class.php
  2. +2 −2 library/classes/Document.class.php
  3. +15 −0 library/sanitize.inc.php
@@ -503,7 +503,7 @@ function retrieve_action($patient_id="",$document_id,$as_file=true,$original_fil
readfile( $tmpfilepath.$tmpfilename );
unlink( $tmpfilepath.$tmpfilename );
} else {
- header("Content-Disposition: " . ($as_file ? "attachment" : "inline") . "; filename=\"" . basename($d->get_url()) . "\"");
+ header("Content-Disposition: " . ($as_file ? "attachment" : "inline") . "; filename=\"" . basename_international($d->get_url()) . "\"");
header("Content-Type: " . $d->get_mimetype());
header("Content-Length: " . filesize($tmpcouchpath));
fpassthru($f);
@@ -587,7 +587,7 @@ function retrieve_action($patient_id="",$document_id,$as_file=true,$original_fil
readfile( $tmpfilepath.$tmpfilename );
unlink( $tmpfilepath.$tmpfilename );
} else {
- header("Content-Disposition: " . ($as_file ? "attachment" : "inline") . "; filename=\"" . basename($d->get_url()) . "\"");
+ header("Content-Disposition: " . ($as_file ? "attachment" : "inline") . "; filename=\"" . basename_international($d->get_url()) . "\"");
header("Content-Type: " . $d->get_mimetype());
header("Content-Length: " . filesize($url));
fpassthru($f);
@@ -596,7 +596,7 @@ function retrieve_action($patient_id="",$document_id,$as_file=true,$original_fil
}
else {
//special case when retrieving a document that has been converted to a jpg and not directly referenced in database
- $convertedFile = substr(basename($url), 0, strrpos(basename($url), '.')) . '_converted.jpg';
+ $convertedFile = substr(basename_international($url), 0, strrpos(basename_international($url), '.')) . '_converted.jpg';
if($couch_docid && $couch_revid){
$url = $GLOBALS['OE_SITE_DIR'] . '/documents/temp/' . $convertedFile;
}
@@ -609,7 +609,7 @@ function retrieve_action($patient_id="",$document_id,$as_file=true,$original_fil
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
- header("Content-Disposition: " . ($as_file ? "attachment" : "inline") . "; filename=\"" . basename($url) . "\"");
+ header("Content-Disposition: " . ($as_file ? "attachment" : "inline") . "; filename=\"" . basename_international($url) . "\"");
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($url));
$f = fopen($url,"r");
@@ -638,11 +638,11 @@ function queue_action($patient_id="") {
$file = $this->_config['repository'] .$file;
$file_info = array();
//if the filename is a file get its info and put into a tmp array
- if (is_file($file) && strpos(basename($file),".") !== 0) {
- $file_info['filename'] = basename($file);
+ if (is_file($file) && strpos(basename_international($file),".") !== 0) {
+ $file_info['filename'] = basename_international($file);
$file_info['mtime'] = date("m/d/Y H:i:s",filemtime($file));
$d = $this->Document->document_factory_url("file://" . $file);
- preg_match("/^([0-9]+)_/",basename($file),$patient_match);
+ preg_match("/^([0-9]+)_/",basename_international($file),$patient_match);
$file_info['patient_id'] = $patient_match[1];
$file_info['document_id'] = $d->get_id();
$file_info['web_path'] = $this->_link("retrieve",true) . "document_id=" . $d->get_id() . "&";
@@ -731,7 +731,7 @@ function queue_action_process() {
$fname = $file['name'];
//see if patient autonumbering is used in this filename, if so strip out the autonumber part
- preg_match("/^([0-9]+)_/",basename($fname),$patient_match);
+ preg_match("/^([0-9]+)_/",basename_international($fname),$patient_match);
if ($patient_match[1] == $file['patient_id']) {
$fname = preg_replace("/^([0-9]+)_/","",$fname);
}
@@ -742,7 +742,7 @@ function queue_action_process() {
//see if there is an existing file with the same name and rename as necessary
if (file_exists($new_path.$file['name'])) {
$messages .= "File with same name already exists at location: " . $new_path . "\n";
- $fname = basename($this->_rename_file($new_path.$file['name']));
+ $fname = basename_international($this->_rename_file($new_path.$file['name']));
$messages .= "Current file name was changed to " . $fname ."\n";
}
@@ -819,7 +819,7 @@ function move_action_process($patient_id="",$document_id) {
//see if there is an existing file with the same name and rename as necessary
if (file_exists($new_path.$d->get_url_file())) {
$messages .= "File with same name already exists in the queue.\n";
- $fname = basename($this->_rename_file($new_path.$d->get_url_file()));
+ $fname = basename_international($this->_rename_file($new_path.$d->get_url_file()));
$messages .= "Current file name was changed to " . $fname ."\n";
}
@@ -947,7 +947,7 @@ function update_action_process($patient_id="", $document_id) {
if ( rename( $d->get_url(), $new_url ) ) {
// check the "converted" file, and delete it if it exists. It will be regenerated when report is run
$url = preg_replace("|^(.*)://|","",$d->get_url());
- $convertedFile = substr(basename($url), 0, strrpos(basename($url), '.')) . '_converted.jpg';
+ $convertedFile = substr(basename_international($url), 0, strrpos(basename_international($url), '.')) . '_converted.jpg';
$url = $GLOBALS['OE_SITE_DIR'] . '/documents/' . $patient_id . '/' . $convertedFile;
if ( file_exists( $url ) ) {
unlink( $url );
@@ -1002,7 +1002,7 @@ function list_action($patient_id = "") {
$menu->addItem($rnode);
$treeMenu = new HTML_TreeMenu_DHTML($menu, array('images' => 'images', 'defaultClass' => 'treeMenuDefault'));
$treeMenu_listbox = new HTML_TreeMenu_Listbox($menu, array('linkTarget' => '_self'));
-
+
$this->assign("tree_html",$treeMenu->toHTML());
return $this->fetch($GLOBALS['template_dir'] . "documents/" . $this->template_mod . "_list.html");
@@ -1019,7 +1019,7 @@ function list_action($patient_id = "") {
*/
function _rename_file($fname) {
$path = dirname($fname);
- $file = basename($fname);
+ $file = basename_international($fname);
$fparts = explode("\.",$file);
@@ -1100,23 +1100,23 @@ function &_array_recurse($array,$categories = array()) {
foreach ($categories[$id] as $doc) {
if($this->tree->get_node_name($id) == "CCR"){
$current_node->addItem(new HTML_TreeNode(array(
- 'text' => $doc['docdate'] . ' ' . basename($doc['url']),
+ 'text' => $doc['docdate'] . ' ' . basename_international($doc['url']),
'link' => $this->_link("view") . "doc_id=" . $doc['document_id'] . "&",
'icon' => $icon,
'expandedIcon' => $expandedIcon,
'events' => array('Onclick' => "javascript:newwindow=window.open('ccr/display.php?type=CCR&doc_id=" . $doc['document_id'] . "','CCR');")
)));
}elseif($this->tree->get_node_name($id) == "CCD"){
$current_node->addItem(new HTML_TreeNode(array(
- 'text' => $doc['docdate'] . ' ' . basename($doc['url']),
+ 'text' => $doc['docdate'] . ' ' . basename_international($doc['url']),
'link' => $this->_link("view") . "doc_id=" . $doc['document_id'] . "&",
'icon' => $icon,
'expandedIcon' => $expandedIcon,
'events' => array('Onclick' => "javascript:newwindow=window.open('ccr/display.php?type=CCD&doc_id=" . $doc['document_id'] . "','CCD');")
)));
}else{
$current_node->addItem(new HTML_TreeNode(array(
- 'text' => $doc['docdate'] . ' ' . basename($doc['url']),
+ 'text' => $doc['docdate'] . ' ' . basename_international($doc['url']),
'link' => $this->_link("view") . "doc_id=" . $doc['document_id'] . "&",
'icon' => $icon,
'expandedIcon' => $expandedIcon
@@ -310,7 +310,7 @@ function get_thumb_url() {
* this returns the url stripped down to basename
*/
function get_url_web() {
- return basename($this->url);
+ return basename_international($this->url);
}
/**
* get the url without the protocol handler
@@ -322,7 +322,7 @@ function get_url_filepath() {
* get the url filename only
*/
function get_url_file() {
- return basename(preg_replace("|^(.*)://|","",$this->url));
+ return basename_international(preg_replace("|^(.*)://|","",$this->url));
}
/**
* get the url path only
@@ -19,6 +19,7 @@
* @package OpenEMR
* @author Brady Miller <brady@sparmy.com>
* @author Roberto Vasquez <robertogagliotta@gmail.com>
+* @author Shachar Zilbershlag <shaharzi@matrix.co.il>
* @link http://www.open-emr.org
*/
// If the label contains any illegal characters, then the script will die.
@@ -32,4 +33,18 @@ function convert_safe_file_dir_name($label) {
return preg_replace('/[^A-Za-z0-9_.-]/','_',$label);
}
+//Basename functionality for nonenglish languages (without this, basename function ommits nonenglish characters).
+function basename_international($path){
+ $parts = preg_split('~[\\\\/]~', $path);
+ foreach ($parts as $key => $value){
+ $encoded = urlencode($value);
+ $parts[$key] = $encoded;
+ }
+ $encoded_path = implode("/", $parts);
+ $encoded_file_name = basename($encoded_path);
+ $decoded_file_name = urldecode($encoded_file_name);
+
+ return $decoded_file_name;
+}
+
?>

0 comments on commit 0f23564

Please sign in to comment.