dokeos-document
[ class tree: dokeos-document ] [ index: dokeos-document ] [ all elements ]

Source for file downloadfolder.inc.php

Documentation is available at downloadfolder.inc.php

  1. <?php // $Id: downloadfolder.inc.php 10195 2006-11-25 15:26:00Z pcool $
  2. /**
  3. ==============================================================================
  4. *    Functions and main code for the download folder feature
  5. *
  6. *    @package dokeos.document
  7. ==============================================================================
  8. */
  9.  
  10. $path $_GET['path'];
  11.  
  12. //prevent some stuff
  13. if(empty($path))
  14. {
  15.     $path='/';
  16. }
  17.  
  18. //check to see if they want to download an existing folder
  19. if(($path!='/'&& (!DocumentManager::get_document_id($_course,$path)))
  20. {
  21.     $path='/';
  22. }
  23.  
  24. //zip library for creation of the zipfile
  25. include(api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
  26.  
  27. //we need this path to clean it out of the zip file
  28. //I'm not using dirname as it gives too much problems (cfr. \)
  29. $remove_dir ($path!='/'substr($path,0,strlen($pathstrlen(basename($path))) '/';
  30.  
  31. //place to temporarily stash the zipfiles
  32. $temp_zip_dir $sys_course_path.$_course['path']."/temp";
  33. //create the temp dir if it doesn't exist
  34. //or do a cleanup befor creating the zipfile
  35.  
  36. if(!is_dir($temp_zip_dir))
  37. {
  38.     mkdir($temp_zip_dir);
  39. }
  40. //cleanup: check the temp dir for old files and delete them
  41. else
  42. {
  43.     $handle=opendir($temp_zip_dir);
  44.     while (false!==($file readdir($handle)))
  45.     {
  46.         if ($file != "." && $file != "..")
  47.         {
  48.         //the "age" of the file in hours
  49.         $Diff (time(filemtime("$temp_zip_dir/$file"))/60/60;
  50.         //delete files older than 4 hours
  51.         if ($Diff 4unlink("$temp_zip_dir/$file");
  52.         }
  53.     }
  54.     closedir($handle);
  55. }
  56.  
  57. //create zipfile of given directory
  58. $temp_zip_file $temp_zip_dir."/".md5(time()).".zip";
  59. $zip_folder=new PclZip($temp_zip_file);
  60. //Put the files in the zip
  61. //2 possibilities: admins get all files and folders in the selected folder (except for the deleted ones)
  62. //normal users get only visible files that are in visible folders
  63.  
  64. //admins are allowed to download invisible files
  65. {
  66.     //folder we want to zip --> no longer used, deleted files are included too like this
  67.      //$what_to_zip = $sys_course_path.$_course['path']."/document".$path;
  68.      //creation of the zipped folder
  69.     //$zip_folder->create($what_to_zip ,PCLZIP_OPT_REMOVE_PATH, $sys_course_path.$_course['path']."/document".$remove_dir );
  70.     //set the path that will be used in the query
  71.     if($path=='/')
  72.     {
  73.         $querypath=''// to prevent ...path LIKE '//%'... in query
  74.     }
  75.     else
  76.     {
  77.         $querypath=$path;
  78.     }
  79.     //search for all files that are not deleted => visibility != 2
  80.     $query api_sql_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."",__FILE__,__LINE__);
  81.     //add tem to the zip file
  82.     while($not_deleted_file mysql_fetch_assoc($query))
  83.     {
  84.         $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);
  85.     }
  86. }
  87.  
  88. //for other users, we need to create a zipfile with only visible files and folders
  89. else
  90. {
  91.     if($path=='/')
  92.     {
  93.         $querypath=''// to prevent ...path LIKE '//%'... in query
  94.     }
  95.     else
  96.     {
  97.         $querypath=$path;
  98.     }
  99.     //big problem: visible files that are in a hidden folder are included when we do a query for visiblity='v'!!!
  100.     //so... I do it in a couple of steps:
  101.     //1st: get all files that are visible in the given path
  102.     $query api_sql_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,__FILE__,__LINE__);
  103.     //add them to an array
  104.     while($all_visible_files mysql_fetch_assoc($query))
  105.     {
  106.         $all_visible_files_path[$all_visible_files['path'];
  107.         //echo "visible files: ".$sys_course_path.$_course['path']."/document".$all_visible_files['path']."<br>";
  108.     }
  109.     //echo('<pre>');
  110.     //print_r($all_visible_files_path);
  111.     //echo('</pre>');
  112.     //2nd: get all folders that are invisible in the given path
  113.     $query2 api_sql_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'",__FILE__,__LINE__);
  114.     //if we get invisible folders, we have to filter out these results from all visible files we found
  115.     if(mysql_num_rows($query2)>0)
  116.     {
  117.         //add tem to an array
  118.         while($invisible_folders mysql_fetch_assoc($query2))
  119.         {
  120.         //3rd: get all files that are in the found invisible folder (these are "invisible" too)
  121.         //echo "<br><br>invisible folders: ".$sys_course_path.$_course['path']."/document".$invisible_folders['path']."<br>";
  122.             $query3 api_sql_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'",__FILE__,__LINE__);
  123.             //add tem to an array
  124.             while($files_in_invisible_folder mysql_fetch_assoc($query3))
  125.             {
  126.                 $files_in_invisible_folder_path[$files_in_invisible_folder['path'];
  127.                 //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>";
  128.             }
  129.         }
  130.         //compare the array with visible files and the array with files in invisible folders
  131.         //and keep the difference (= all visible files that are not in an invisible folder)
  132.         $files_for_zipfile diff((array) $all_visible_files_path,(array) $files_in_invisible_folder_path);
  133.  
  134.     }
  135.     //no invisible folders found, so all visible files can be added to the zipfile
  136.     else
  137.     {
  138.         $files_for_zipfile $all_visible_files_path;
  139.     }
  140.     //add all files in our final array to the zipfile
  141.     //echo("path to remove from file ".$sys_course_path.$_course['path']."/document".$remove_dir.'<br>');
  142.     //echo('<b>FILES FOR ZIP</b><br>');
  143.     //print_r($files_for_zipfile);
  144.     for($i=0;$i<count($files_for_zipfile);$i++)
  145.     {
  146.         $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);
  147.         //echo $sys_course_path.$_course['path']."/document".$files_for_zipfile[$i]."<br>";
  148.     }
  149. }//end for other users
  150. //exit;
  151. //logging
  152. // launch event
  153. event_download(($path=='/')?'documents.zip (folder)':basename($path).'.zip (folder)');
  154.  
  155. //start download of created file
  156. //send_file_to_client($temp_zip_file, basename(empty($_GET['id'])?"documents":$_GET['id']).".zip");
  157. $name ($path=='/')?'documents.zip':basename($path).'.zip';
  158. DocumentManager::file_send_for_download($temp_zip_file,true,$name);
  159. exit;
  160.  
  161. /**
  162. ==============================================================================
  163. *    Extra function (only used here)
  164. ==============================================================================
  165. */
  166.  
  167. /**
  168.  * Return the difference between two arrays, as an array of those key/values
  169.  * Use this as array_diff doesn't give the
  170.  *
  171.  * @param array $arr1 first array
  172.  * @param array $arr2 second array
  173.  * @return difference between the two arrays
  174.  */
  175. function diff($arr1,$arr2{
  176.     $res array()$r=0;
  177.     foreach ($arr1 as $av{
  178.         if (!in_array($av,$arr2)){
  179.             $res[$r]=$av$r++;
  180.         }
  181.     }
  182.     return $res;
  183. }
  184. ?>

Documentation generated on Thu, 12 Jun 2008 13:20:43 -0500 by phpDocumentor 1.4.1