Feature #272 - The "Documents" tool, part 2: Revision, code conventions and cleaning.

skala
Ivan Tcholakov 16 years ago
parent 6ae24bc4a8
commit 3412f6e28d
  1. 132
      main/document/download.php
  2. 225
      main/document/downloadfolder.inc.php

@ -1,126 +1,84 @@
<?php // $Id: download.php 22201 2009-07-17 19:57:03Z cfasanando $ <?php
/* /* For licensing terms, see /license.txt */
==============================================================================
Dokeos - elearning and course management software
Copyright (c) 2004 Dokeos S.A.
Copyright (c) 2003 Ghent University (UGent)
Copyright (c) 2001 Universite catholique de Louvain (UCL)
Copyright (c) Olivier Brouckaert
Copyright (c) Roan Embrechts
Copyright (c) Sergio A. Kessler aka "sak"
For a full list of contributors, see "credits.txt".
The full license can be read in "license.txt".
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
See the GNU General Public License for more details.
Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
==============================================================================
*/
/** /**
============================================================================== * This file is responsible for passing requested documents to the browser.
* This file is responsible for passing requested documents to the browser. *
* Html files are parsed to fix a few problems with URLs, * @package chamilo.document
* but this code will hopefully be replaced soon by an Apache URL */
* rewrite mechanism.
* /* FUNCTIONS */
* @package dokeos.document
==============================================================================
*/
/*
==============================================================================
FUNCTIONS
==============================================================================
*/
/* file_html_dynamic_parsing removed */ /* file_html_dynamic_parsing removed */
/* other functions updated and moved to lib/document.lib.php */
/* /* Other functions updated and moved to lib/document.lib.php */
==============================================================================
MAIN CODE /* MAIN CODE */
==============================================================================
*/
session_cache_limiter('none'); session_cache_limiter('none');
require_once '../inc/global.inc.php'; require_once '../inc/global.inc.php';
$this_section=SECTION_COURSES; $this_section = SECTION_COURSES;
require_once api_get_path(LIBRARY_PATH).'document.lib.php'; require_once api_get_path(LIBRARY_PATH).'document.lib.php';
$doc_url = $_GET['doc_url']; $doc_url = $_GET['doc_url'];
// Change the '&' that got rewritten to '///' by mod_rewrite back to '&'
//change the '&' that got rewritten to '///' by mod_rewrite back to '&'
$doc_url = str_replace('///', '&', $doc_url); $doc_url = str_replace('///', '&', $doc_url);
//still a space present? it must be a '+' (that got replaced by mod_rewrite) // Still a space present? it must be a '+' (that got replaced by mod_rewrite)
$doc_url = str_replace(' ', '+', $doc_url); $doc_url = str_replace(' ', '+', $doc_url);
$doc_url = str_replace(array('../','\\..','\\0','..\\'),array('','','',''), $doc_url); //echo $doc_url; $doc_url = str_replace(array('../', '\\..', '\\0', '..\\'), array('', '', '', ''), $doc_url); //echo $doc_url;
// dealing with image included into survey: when users receive a link towards a // Dealing with image included into survey: when users receive a link towards a
// survey while not being authenticated on the plateform. // survey while not being authenticated on the plateform.
// the administrator should probably be able to disable this code through admin // The administrator should probably be able to disable this code through admin
// inteface // inteface.
$refer_script = strrchr($_SERVER["HTTP_REFERER"],'/'); $refer_script = strrchr($_SERVER["HTTP_REFERER"], '/');
if (substr($refer_script,0,15) == "/fillsurvey.php") { if (substr($refer_script, 0, 15) == '/fillsurvey.php') {
$invitation = substr(strstr($refer_script, 'invitationcode='),15); $invitation = substr(strstr($refer_script, 'invitationcode='), 15);
$course = strstr($refer_script, 'course='); $course = strstr($refer_script, 'course=');
$course = substr($course, 7, strpos($course, '&')-7); $course = substr($course, 7, strpos($course, '&') - 7);
include ("../survey/survey.download.inc.php"); include '../survey/survey.download.inc.php';
$_course = check_download_survey($course, $invitation, $doc_url); $_course = check_download_survey($course, $invitation, $doc_url);
$_course['path']=$_course['directory']; $_course['path'] = $_course['directory'];
} else { } else {
//protection // Protection
api_protect_course_script(); api_protect_course_script();
if (! isset($_course)) if (!isset($_course)) {
{
api_not_allowed(true); api_not_allowed(true);
} }
// If the rewrite rule asks for a directory, we redirect to the document explorer
//if the rewrite rule asks for a directory, we redirect to the document explorer if (is_dir(api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$doc_url)) {
if(is_dir(api_get_path(SYS_COURSE_PATH).$_course['path']."/document".$doc_url)) // Remove last slash if present
{ // mod_rewrite can change /some/path/ to /some/path// in some cases, so clean them all off (René)
//remove last slash if present while ($doc_url{$dul = strlen($doc_url) - 1} == '/') {
//$doc_url = ($doc_url{strlen($doc_url)-1}=='/')?substr($doc_url,0,strlen($doc_url)-1):$doc_url; $doc_url = substr($doc_url, 0, $dul);
//mod_rewrite can change /some/path/ to /some/path// in some cases, so clean them all off (René) }
while ($doc_url{$dul = strlen($doc_url)-1}=='/') $doc_url = substr($doc_url,0,$dul); // Group folder?
//group folder? $gid_req = ($_GET['gidReq']) ? '&gidReq='.Security::remove_XSS($_GET['gidReq']) : '';
$gid_req = ($_GET['gidReq'])?'&gidReq='.Security::remove_XSS($_GET['gidReq']):''; // Create the path
//create the path
$document_explorer = api_get_path(WEB_CODE_PATH).'document/document.php?curdirpath='.urlencode($doc_url).'&cidReq='.Security::remove_XSS($_GET['cidReq']).$gid_req; $document_explorer = api_get_path(WEB_CODE_PATH).'document/document.php?curdirpath='.urlencode($doc_url).'&cidReq='.Security::remove_XSS($_GET['cidReq']).$gid_req;
//redirect // Redirect
header('Location: '.$document_explorer); header('Location: '.$document_explorer);
} }
// launch event // Launch event
event_download($doc_url); event_download($doc_url);
} }
$sys_course_path = api_get_path(SYS_COURSE_PATH); $sys_course_path = api_get_path(SYS_COURSE_PATH);
//$full_file_name = $sys_course_path.$_course['path'].'/document'.$doc_url; $full_file_name = $sys_course_path.$_course['path'].'/document'.str_replace('+', ' ', $doc_url);
$full_file_name = $sys_course_path.$_course['path'].'/document'.str_replace('+',' ',$doc_url);
// check visibility of document and paths // Check visibility of document and paths
$is_allowed_to_edit = api_is_allowed_to_edit(); $is_allowed_to_edit = api_is_allowed_to_edit();
if (!$is_allowed_to_edit && if (!$is_allowed_to_edit && !DocumentManager::is_visible($doc_url, $_course)) {
!DocumentManager::is_visible($doc_url, $_course)){ echo 'document not visible'; //api_not_allowed backbutton won't work.
echo "document not visible"; //api_not_allowed backbutton won't work exit; // You shouldn't be here anyway.
exit; // you shouldn't be here anyway
} }
DocumentManager::file_send_for_download($full_file_name); DocumentManager::file_send_for_download($full_file_name);
exit; exit;
?>

@ -1,189 +1,160 @@
<?php // $Id: downloadfolder.inc.php 19385 2009-03-27 20:48:57Z iflorespaz $ <?php
/* For licensing terms, see /license.txt */
/** /**
============================================================================== * Functions and main code for the download folder feature
* Functions and main code for the download folder feature *
* * @package chamilo.document
* @package dokeos.document */
==============================================================================
*/
$path = $_GET['path']; $path = $_GET['path'];
//prevent some stuff // Prevent some stuff
if(empty($path)) if (empty($path)) {
{ $path = '/';
$path='/';
} }
//check to see if they want to download an existing folder // Check to see if they want to download an existing folder
if(($path!='/') && (!DocumentManager::get_document_id($_course,$path))) if (($path != '/') && (!DocumentManager::get_document_id($_course, $path))) {
{ $path = '/';
$path='/';
} }
//zip library for creation of the zipfile //zip library for creation of the zipfile
include(api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php'); include api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
//we need this path to clean it out of the zip file // We need this path to clean it out of the zip file
//I'm not using dirname as it gives too much problems (cfr. \) // I'm not using dirname as it gives too much problems (cfr.)
$remove_dir = ($path!='/') ? substr($path,0,strlen($path) - strlen(basename($path))) : '/'; $remove_dir = ($path != '/') ? substr($path, 0, strlen($path) - strlen(basename($path))) : '/';
//place to temporarily stash the zipfiles // Place to temporarily stash the zipfiles
$temp_zip_dir = $sys_course_path.$_course['path']."/temp"; $temp_zip_dir = $sys_course_path.$_course['path']."/temp";
//create the temp dir if it doesn't exist // Create the temp dir if it doesn't exist or do a cleanup befor creating the zipfile
//or do a cleanup befor creating the zipfile
if(!is_dir($temp_zip_dir)) if (!is_dir($temp_zip_dir)) {
{
mkdir($temp_zip_dir, api_get_permissions_for_new_directories()); mkdir($temp_zip_dir, api_get_permissions_for_new_directories());
} }
//cleanup: check the temp dir for old files and delete them // Cleanup: check the temp dir for old files and delete them
else else {
{ $handle = opendir($temp_zip_dir);
$handle=opendir($temp_zip_dir); while (false !== ($file = readdir($handle))) {
while (false!==($file = readdir($handle))) if ($file != '.' && $file != '..') {
{ // The "age" of the file in hours
if ($file != "." && $file != "..") $diff = (time() - filemtime("$temp_zip_dir/$file"))/60/60;
{ // Delete files older than 4 hours
//the "age" of the file in hours if ($diff > 4) unlink("$temp_zip_dir/$file");
$Diff = (time() - filemtime("$temp_zip_dir/$file"))/60/60;
//delete files older than 4 hours
if ($Diff > 4) unlink("$temp_zip_dir/$file");
} }
} }
closedir($handle); closedir($handle);
} }
//create zipfile of given directory // Create zipfile of given directory
$temp_zip_file = $temp_zip_dir."/".md5(time()).".zip"; $temp_zip_file = $temp_zip_dir.'/'.md5(time()).'.zip';
$zip_folder=new PclZip($temp_zip_file); $zip_folder = new PclZip($temp_zip_file);
$doc_table = Database::get_course_table(TABLE_DOCUMENT); $doc_table = Database::get_course_table(TABLE_DOCUMENT);
$prop_table = Database::get_course_table(TABLE_ITEM_PROPERTY); $prop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
//Put the files in the zip // Put the files in the zip
//2 possibilities: admins get all files and folders in the selected folder (except for the deleted ones) // 2 possibilities: Admins get all files and folders in the selected folder (except for the deleted ones)
//normal users get only visible files that are in visible folders // Normal users get only visible files that are in visible folders
//admins are allowed to download invisible files // Admins are allowed to download invisible files
if (is_allowed_to_edit()) if (is_allowed_to_edit()) {
{ // Folder we want to zip --> no longer used, deleted files are included too like this
//folder we want to zip --> no longer used, deleted files are included too like this //$what_to_zip = $sys_course_path.$_course['path'].'/document'.$path;
//$what_to_zip = $sys_course_path.$_course['path']."/document".$path; // Creation of the zipped folder
//creation of the zipped folder //$zip_folder->create($what_to_zip ,PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path'].'/document'.$remove_dir );
//$zip_folder->create($what_to_zip ,PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path']."/document".$remove_dir ); // Set the path that will be used in the query
//set the path that will be used in the query if ($path == '/') {
if($path=='/') $querypath = ''; // To prevent ...path LIKE '//%'... in query
{ } else {
$querypath=''; // to prevent ...path LIKE '//%'... in query $querypath = $path;
} }
else // Search for all files that are not deleted => visibility != 2
{
$querypath=$path;
}
//search for all files that are not deleted => visibility != 2
$query = Database::query("SELECT path FROM $doc_table AS docs,$prop_table AS props WHERE `props`.`tool`='".TOOL_DOCUMENT."' AND `docs`.`id`=`props`.`ref` AND `docs`.`path` LIKE '".$querypath."/%' AND `docs`.`filetype`='file' AND `props`.`visibility`<>'2' AND `props`.`to_group_id`=".$to_group_id.""); $query = Database::query("SELECT path FROM $doc_table AS docs,$prop_table AS props WHERE `props`.`tool`='".TOOL_DOCUMENT."' AND `docs`.`id`=`props`.`ref` AND `docs`.`path` LIKE '".$querypath."/%' AND `docs`.`filetype`='file' AND `props`.`visibility`<>'2' AND `props`.`to_group_id`=".$to_group_id."");
//add tem to the zip file // Add tem to the zip file
while ($not_deleted_file = Database::fetch_assoc($query)) while ($not_deleted_file = Database::fetch_assoc($query)) {
{ $zip_folder->add($sys_course_path.$_course['path'].'/document'.$not_deleted_file['path'], PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path'].'/document'.$remove_dir);
$zip_folder->add($sys_course_path.$_course['path']."/document".$not_deleted_file['path'],PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path']."/document".$remove_dir);
} }
} }
// For other users, we need to create a zipfile with only visible files and folders
//for other users, we need to create a zipfile with only visible files and folders else {
else if ($path == '/') {
{ $querypath = ''; // To prevent ...path LIKE '//%'... in query
if($path=='/') } else {
{ $querypath = $path;
$querypath=''; // to prevent ...path LIKE '//%'... in query
} }
else // A big problem: Visible files that are in a hidden folder are included when we do a query for visiblity='v'
{ // So... I do it in a couple of steps:
$querypath=$path; // 1st: Get all files that are visible in the given path
}
//big problem: visible files that are in a hidden folder are included when we do a query for visiblity='v'!!!
//so... I do it in a couple of steps:
//1st: get all files that are visible in the given path
$query = Database::query("SELECT path FROM $doc_table AS docs,$prop_table AS props WHERE `props`.`tool`='".TOOL_DOCUMENT."' AND `docs`.`id`=`props`.`ref` AND `docs`.`path` LIKE '".$querypath."/%' AND `props`.`visibility`='1' AND `docs`.`filetype`='file' AND `props`.`to_group_id`=".$to_group_id); $query = Database::query("SELECT path FROM $doc_table AS docs,$prop_table AS props WHERE `props`.`tool`='".TOOL_DOCUMENT."' AND `docs`.`id`=`props`.`ref` AND `docs`.`path` LIKE '".$querypath."/%' AND `props`.`visibility`='1' AND `docs`.`filetype`='file' AND `props`.`to_group_id`=".$to_group_id);
//add them to an array // Add them to an array
while ($all_visible_files = Database::fetch_assoc($query)) while ($all_visible_files = Database::fetch_assoc($query)) {
{
$all_visible_files_path[] = $all_visible_files['path']; $all_visible_files_path[] = $all_visible_files['path'];
//echo "visible files: ".$sys_course_path.$_course['path']."/document".$all_visible_files['path']."<br>"; //echo "visible files: ".$sys_course_path.$_course['path'].'/document'.$all_visible_files['path']."<br>";
} }
//echo('<pre>'); //echo('<pre>');
//print_r($all_visible_files_path); //print_r($all_visible_files_path);
//echo('</pre>'); //echo('</pre>');
//2nd: get all folders that are invisible in the given path // 2nd: Get all folders that are invisible in the given path
$query2 = Database::query("SELECT path FROM $doc_table AS docs,$prop_table AS props WHERE `props`.`tool`='".TOOL_DOCUMENT."' AND `docs`.`id`=`props`.`ref` AND `docs`.`path` LIKE '".$querypath."/%' AND `props`.`visibility`<>'1' AND `docs`.`filetype`='folder'"); $query2 = Database::query("SELECT path FROM $doc_table AS docs,$prop_table AS props WHERE `props`.`tool`='".TOOL_DOCUMENT."' AND `docs`.`id`=`props`.`ref` AND `docs`.`path` LIKE '".$querypath."/%' AND `props`.`visibility`<>'1' AND `docs`.`filetype`='folder'");
//if we get invisible folders, we have to filter out these results from all visible files we found // If we get invisible folders, we have to filter out these results from all visible files we found
if(Database::num_rows($query2)>0) if (Database::num_rows($query2) > 0) {
{ // Add tem to an array
//add tem to an array while ($invisible_folders = Database::fetch_assoc($query2)) {
while ($invisible_folders = Database::fetch_assoc($query2)) //3rd: Get all files that are in the found invisible folder (these are "invisible" too)
{ //echo "<br /><br />invisible folders: ".$sys_course_path.$_course['path'].'/document'.$invisible_folders['path'].'<br />';
//3rd: get all files that are in the found invisible folder (these are "invisible" too)
//echo "<br><br>invisible folders: ".$sys_course_path.$_course['path']."/document".$invisible_folders['path']."<br>";
$query3 = Database::query("SELECT path FROM $doc_table AS docs,$prop_table AS props WHERE `props`.`tool`='".TOOL_DOCUMENT."' AND `docs`.`id`=`props`.`ref` AND `docs`.`path` LIKE '".$invisible_folders['path']."/%' AND `docs`.`filetype`='file' AND `props`.`visibility`='1'"); $query3 = Database::query("SELECT path FROM $doc_table AS docs,$prop_table AS props WHERE `props`.`tool`='".TOOL_DOCUMENT."' AND `docs`.`id`=`props`.`ref` AND `docs`.`path` LIKE '".$invisible_folders['path']."/%' AND `docs`.`filetype`='file' AND `props`.`visibility`='1'");
//add tem to an array // Add tem to an array
while ($files_in_invisible_folder = Database::fetch_assoc($query3)) while ($files_in_invisible_folder = Database::fetch_assoc($query3)) {
{
$files_in_invisible_folder_path[] = $files_in_invisible_folder['path']; $files_in_invisible_folder_path[] = $files_in_invisible_folder['path'];
//echo "<br><br>files in invisible folders: ".$sys_course_path.$_course['path']."/document".$files_in_invisible_folder['path']." <b>id ".$files_in_invisible_folder['id']."</b><br>"; //echo '<br /><br />files in invisible folders: '.$sys_course_path.$_course['path'].'/document'.$files_in_invisible_folder['path'].' <b>id '.$files_in_invisible_folder['id'].'</b><br />';
} }
} }
//compare the array with visible files and the array with files in invisible folders // Compare the array with visible files and the array with files in invisible folders
//and keep the difference (= all visible files that are not in an invisible folder) // and keep the difference (= all visible files that are not in an invisible folder)
$files_for_zipfile = diff((array) $all_visible_files_path,(array) $files_in_invisible_folder_path); $files_for_zipfile = diff((array)$all_visible_files_path, (array)$files_in_invisible_folder_path);
} }
//no invisible folders found, so all visible files can be added to the zipfile // No invisible folders found, so all visible files can be added to the zipfile
else else {
{
$files_for_zipfile = $all_visible_files_path; $files_for_zipfile = $all_visible_files_path;
} }
//add all files in our final array to the zipfile // Add all files in our final array to the zipfile
//echo("path to remove from file ".$sys_course_path.$_course['path']."/document".$remove_dir.'<br>'); //echo("path to remove from file ".$sys_course_path.$_course['path']."/document".$remove_dir.'<br>');
//echo('<b>FILES FOR ZIP</b><br>'); //echo('<b>FILES FOR ZIP</b><br />');
//print_r($files_for_zipfile); //print_r($files_for_zipfile);
for($i=0;$i<count($files_for_zipfile);$i++) for ($i = 0; $i < count($files_for_zipfile); $i++) {
{ $zip_folder->add($sys_course_path.$_course['path'].'/document'.$files_for_zipfile[$i], PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path'].'/document'.$remove_dir);
$zip_folder->add($sys_course_path.$_course['path']."/document".$files_for_zipfile[$i],PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path']."/document".$remove_dir); //echo $sys_course_path.$_course['path'].'/document'.$files_for_zipfile[$i].'<br />';
//echo $sys_course_path.$_course['path']."/document".$files_for_zipfile[$i]."<br>";
} }
}//end for other users } // end for other users
//exit; //exit;
//logging
// launch event
event_download(($path=='/')?'documents.zip (folder)':basename($path).'.zip (folder)');
//start download of created file // Launch event
//send_file_to_client($temp_zip_file, basename(empty($_GET['id'])?"documents":$_GET['id']).".zip"); event_download(($path == '/') ? 'documents.zip (folder)' : basename($path).'.zip (folder)');
$name = ($path=='/')?'documents.zip':basename($path).'.zip';
// Start download of created file
//send_file_to_client($temp_zip_file, basename(empty($_GET['id']) ? 'documents' : $_GET['id']).'.zip');
$name = ($path=='/') ? 'documents.zip' : basename($path).'.zip';
DocumentManager::file_send_for_download($temp_zip_file,true,$name); DocumentManager::file_send_for_download($temp_zip_file, true, $name);
@unlink($temp_zip_file); @unlink($temp_zip_file);
exit; exit;
/** /**
============================================================================== * Returns the difference between two arrays, as an array of those key/values
* Extra function (only used here)
==============================================================================
*/
/**
* Return the difference between two arrays, as an array of those key/values
* Use this as array_diff doesn't give the * Use this as array_diff doesn't give the
* *
* @param array $arr1 first array * @param array $arr1 first array
* @param array $arr2 second array * @param array $arr2 second array
* @return difference between the two arrays * @return difference between the two arrays
*/ */
function diff($arr1,$arr2) { function diff($arr1, $arr2) {
$res = array(); $r=0; $res = array();
foreach ($arr1 as $av) { $r = 0;
if (!in_array($av,$arr2)){ foreach ($arr1 as & $av) {
$res[$r]=$av; $r++; if (!in_array($av, $arr2)) {
$res[$r] = $av;
$r++;
} }
} }
return $res; return $res;
} }
?>
Loading…
Cancel
Save