diff --git a/main/inc/lib/fileUpload.lib.php b/main/inc/lib/fileUpload.lib.php index 90f45b8d2e..ebc35ca2f8 100755 --- a/main/inc/lib/fileUpload.lib.php +++ b/main/inc/lib/fileUpload.lib.php @@ -1749,209 +1749,6 @@ function create_link_file($file_path, $url) } } -/** - * Opens html file $full_file_name; - * Parses the hyperlinks; and - * Writes the result back in the html file. - * - * @author Roan Embrechts - * @version 0.1 - */ -function api_replace_links_in_html($upload_path, $full_file_name) -{ - // Open the file - if (file_exists($full_file_name)) { - $fp = fopen($full_file_name, 'r'); - $buffer = fread ($fp, filesize ($full_file_name)); - - // Parse the contents - $new_html_content = api_replace_links_in_string($upload_path, $buffer); - - // Write the result - $fp = fopen($full_file_name, 'w'); - fwrite($fp, $new_html_content); - } -} - -/** -deprecated: use api_replace_parameter instead - -Parse the buffer string provided as parameter -Replace the a href tags so they are displayed correctly. -- works for files in root and subdirectories -- replace relative hyperlinks to use showinframes.php?file= ... -- add target="_self" to all absolute hyperlinks -- leave local anchors untouched (e.g. #CHAPTER1) -- leave links with download.php and showinframes.php untouched - -@author Roan Embrechts -@version 0.6 - */ -function api_replace_links_in_string($upload_path, $buffer) { - // Search for hyperlinks - $matches = array(); - if (preg_match_all('//i', $buffer, $matches)) { - $tag_list = $matches[0]; - } - - // Search the filepath of all detected tags - if (sizeof($tag_list) > 0) { - $file_path_list = array(); - $href_list = array(); - - foreach ($tag_list as & $this_tag) { - /* Match case insensitive, the stuff between the two ~ : - a href = - e.g. a href="www.google.be", A HREF = "info.html" - to match ["] escape the " or else PHP interprets it - [\"]{1} --> matches exactly one " - + 1 or more (like * is 0 or more) - [\s]* matches whitespace - $matches contains captured subpatterns - the only one here is ([^\"]+) --> matches[1] - */ - if (preg_match("~a href[\s]*=[\s]*[\"]{1}([^\"]+)[\"]{1}~i", $this_tag, $matches)) { - $file_path_list[] = $matches[1]; // older - $href_list[] = $matches[0]; // to also add target="_self" - } - } - } - - // Replace the original hyperlinks by the correct ones - for ($count = 0; $count < sizeof($href_list); $count++) { - - $replace_what[$count] = $href_list[$count]; - - $is_absolute_hyperlink = strpos($replace_what[$count], 'http'); - $is_local_anchor = strpos($replace_what[$count], "#"); - if (!$is_absolute_hyperlink && !$is_local_anchor) { - // This is a relative hyperlink - if ((strpos($replace_what[$count], 'showinframes.php') === false) && - (strpos($replace_what[$count], 'download.php') === false) - ) { - // Fix the link to use showinframes.php - $replace_by[$count] = 'a href = "showinframes.php?file='.$upload_path.'/'.$file_path_list[$count].'" target="_self"'; - } else { - // URL has been already fixed, leave it as is - $replace_by[$count] = $replace_what[$count]; - } - } elseif ($is_absolute_hyperlink) { - $replace_by[$count] = 'a href="'.$file_path_list[$count].'" target ="_self"'; - } else { - // Don't change anything - $replace_by[$count] = $replace_what[$count]; - } - //Display::display_normal_message('Link replaced by ' . $replace_by[$count]); // debug - } - - $buffer = str_replace($replace_what, $replace_by, $buffer); - return $buffer; -} - -/** -EXPERIMENTAL - function seems to work, needs more testing - -@param $upload_path is the path where the document is stored, like "/archive/" -if it is the root level, the function expects "/" -otherwise "/path/" - -This function parses all tags with $param_name parameters. -so the tags are displayed correctly. - --------------- -Algorithm v1.0 --------------- -given a string and a parameter, - * OK find all tags in that string with the specified parameter (like href or src) - * OK for every one of these tags, find the src|href|... part to edit it - * OK change the src|href|... part to use download.php (or showinframes.php) - * OK do some special stuff for hyperlinks - -Exceptions - * OK if download.php or showinframes.php is already in the tag, leave it alone - * OK if mailto is in the tag, leave it alone - * OK if the src|href param contains http://, it's absolute --> leave it alone - -Special for hyperlinks (a href...) - * OK add target="_self" - * OK use showinframes.php instead of download.php - -@author Roan Embrechts -@version 1.1 - */ -function api_replace_parameter($upload_path, $buffer, $param_name = 'src') -{ - // Search for tags with $param_name as a parameter - - /* - // [\s]* matches whitespace - // [\"=a-z] matches ", = and a-z - // ([\s]*[a-z]*)* matches all whitespace and normal alphabet - // characters a-z combinations but seems too slow - // perhaps ([\s]*[a-z]*) a maximum number of times ? - // [\s]*[a-z]*[\s]* matches many tags - // the ending "i" means to match case insensitive (a matches a and A) - */ - $matches = array(); - if (preg_match_all('/<[a-z]+[^<]*'.$param_name.'[^<]*>/i', $buffer, $matches)) { - $tag_list = $matches[0]; - } - - // Search the filepath of parameter $param_name in all detected tags - - if (sizeof($tag_list) > 0) { - $file_path_list = array(); - $href_list = array(); - - foreach ($tag_list as & $this_tag) { - //Display::display_normal_message(htmlentities($this_tag)); //debug - if ( preg_match("~".$param_name."[\s]*=[\s]*[\"]{1}([^\"]+)[\"]{1}~i", $this_tag, $matches)) { - $file_path_list[] = $matches[1]; // older - $href_list[] = $matches[0]; // to also add target="_self" - } - } - } - - // Replace the original tags by the correct ones - - for ($count = 0; $count < sizeof($href_list); $count++) { - $replace_what[$count] = $href_list[$count]; - - $is_absolute_hyperlink = strpos($replace_what[$count], 'http'); - $is_local_anchor = strpos($replace_what[$count], '#'); - if (!$is_absolute_hyperlink && !$is_local_anchor) { - if ((strpos($replace_what[$count], 'showinframes.php') === false) - && (strpos($replace_what[$count], 'download.php') === false) - && (strpos($replace_what[$count], 'mailto') === false)) { - - // Fix the link to use download.php or showinframes.php - if (preg_match("//i", $tag_list[$count])) { - $replace_by[$count] = " $param_name =\"showinframes.php?file=" . $upload_path.$file_path_list[$count]."\" target=\"_self\" "; - } else { - $replace_by[$count] = " $param_name =\"download.php?doc_url=" . $upload_path.$file_path_list[$count]."\" "; - } - } else { - // "mailto" or url already fixed, leave as is - //$message .= "Already fixed or contains mailto: "; - $replace_by[$count] = $replace_what[$count]; - } - } elseif ($is_absolute_hyperlink) { - //$message .= "Absolute hyperlink, don't change, add target=_self: "; - $replace_by[$count] = " $param_name=\"" . $file_path_list[$count] . "\" target =\"_self\""; - } else { - // Don't change anything - //$message .= "Local anchor, don't change: "; - $replace_by[$count] = $replace_what[$count]; - } - //$message .= "In tag $count, " . htmlentities($tag_list[$count]) - // . ", parameter " . $replace_what[$count] . " replaced by " . $replace_by[$count] . "
"; //debug - } - //if ($message) api_display_debug_info($message); //debug - $buffer = str_replace($replace_what, $replace_by, $buffer); - - return $buffer; -} - /** * Checks the extension of a file, if it's .htm or .html * we use search_img_from_html to get all image paths in the file diff --git a/tests/main/inc/lib/fileUpload.lib.test.php b/tests/main/inc/lib/fileUpload.lib.test.php index d156b1991e..4bc0a3ab1a 100755 --- a/tests/main/inc/lib/fileUpload.lib.test.php +++ b/tests/main/inc/lib/fileUpload.lib.test.php @@ -343,50 +343,6 @@ class TestFileUpload extends UnitTestCase //var_dump($res); } - function testApiReplaceLinksInHtml() - { - $base_work_dir = api_get_path(SYS_COURSE_PATH); - $upload_path = $base_work_dir.'upload/blog'; - $full_file_name = 'doc.php'; - $res = api_replace_links_in_html($upload_path, $full_file_name); - $this->assertTrue(is_null($res)); - //var_dump($res); - } - - function testApiReplaceLinksInString() - { - $base_work_dir = api_get_path(SYS_COURSE_PATH); - $upload_path = $base_work_dir.'upload/blog'; - $buffer = ob_get_contents(); - $res = api_replace_links_in_string($upload_path, $buffer); - $this->assertTrue(is_string($res)); - //var_dump($res); - } - - function testApiReplaceParameter() - { - $count = 0; - $matches = array(); - $href_list = array(); - $file_path_list[] = $matches[1]; - $base_work_dir = api_get_path(SYS_COURSE_PATH); - $upload_path = $base_work_dir.'upload/blog'; - $replaceWhat[$count] = $href_list[$count]; - /** To can test this function you need to comment "die ('can not create file')" - * $res return void - */ - $replaceBy[$count] = " $param_name=\"".$file_path_list[$count]."\" target =\"_top\""; - $replaceBy[$count] = $replaceWhat[$count]; - $buffer = str_replace($replaceWhat, $replaceBy, $buffer); - $param_name = "src"; - $res = api_replace_parameter($upload_path, $buffer, $param_name = "src"); - $this->assertTrue(is_string($res)); - //var_dump($res); - } - - //clenaning - - function testCleanUpFilesInZip() { $p_event = '';