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

Source for file document.lib.php

Documentation is available at document.lib.php

  1. <?php
  2. /*
  3. ==============================================================================
  4.     Dokeos - elearning and course management software
  5.     
  6.     Copyright (c) 2004-2008 Dokeos SPRL
  7.     Copyright (c) 2003 Ghent University (UGent)
  8.     Copyright (c) Roan Embrechts, Vrije Universiteit Brussel
  9.     
  10.     For a full list of contributors, see "credits.txt".
  11.     The full license can be read in "license.txt".
  12.     
  13.     This program is free software; you can redistribute it and/or
  14.     modify it under the terms of the GNU General Public License
  15.     as published by the Free Software Foundation; either version 2
  16.     of the License, or (at your option) any later version.
  17.     
  18.     See the GNU General Public License for more details.
  19.     
  20.     Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  21.     Mail: info@dokeos.com
  22. ==============================================================================
  23. */
  24. /**
  25. ============================================================================== 
  26. *    This is the document library for Dokeos.
  27. *    It is / will be used to provide a service layer to all document-using tools.
  28. *    and eliminate code duplication fro group documents, scorm documents, main documents.
  29. *    Include/require it in your code to use its functionality.
  30. *
  31. *    @version 1.1, January 2005
  32. *    @package dokeos.library
  33. ============================================================================== 
  34. */
  35.  
  36. /*
  37. ============================================================================== 
  38.     DOCUMENTATION
  39.     use the functions like this: DocumentManager::get_course_quota()
  40. ============================================================================== 
  41. */
  42.  
  43. /*
  44. ============================================================================== 
  45.         CONSTANTS
  46. ============================================================================== 
  47. */
  48.  
  49. define("DISK_QUOTA_FIELD""disk_quota")//name of the database field
  50. /** default quota for the course documents folder */
  51. define("DEFAULT_DOCUMENT_QUOTA"get_setting('default_document_quotum'));
  52.  
  53. /*
  54. ============================================================================== 
  55.         VARIABLES
  56. ============================================================================== 
  57. */
  58.  
  59. $sys_course_path api_get_path(SYS_COURSE_PATH);
  60. $baseServDir api_get_path(SYS_PATH);
  61. $baseServUrl $_configuration['url_append']."/";
  62. $baseWorkDir $sys_course_path.(!empty($courseDir)?$courseDir:'');
  63.  
  64. /*
  65. ============================================================================== 
  66.         DocumentManager CLASS
  67.         the class and its functions
  68. ============================================================================== 
  69. */
  70.  
  71. /**
  72.  *    @package dokeos.library
  73.  */
  74. {
  75.     /**
  76.     * @return the document folder quuta of the current course, in bytes
  77.     * @todo eliminate globals
  78.     */
  79.     function get_course_quota()
  80.     {
  81.         global $_course$maxFilledSpace;
  82.         $course_code $_course['sysCode'];
  83.         $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  84.  
  85.         $sql_query "SELECT `".DISK_QUOTA_FIELD."` FROM $course_table WHERE `code` = '$course_code'";
  86.         $sql_result api_sql_query($sql_query__FILE____LINE__);
  87.         $result Database::fetch_array($sql_result);
  88.         $course_quota $result[DISK_QUOTA_FIELD];
  89.  
  90.         if ($course_quota == NULL)
  91.         {
  92.             //course table entry for quota was null
  93.             //use default value
  94.             $course_quota DEFAULT_DOCUMENT_QUOTA;
  95.         }
  96.  
  97.         return $course_quota;
  98.     }
  99.  
  100.     /**
  101.     *    Get the content type of a file by checking the extension
  102.     *    We could use mime_content_type() with php-versions > 4.3,
  103.     *    but this doesn't work as it should on Windows installations
  104.     *
  105.     *    @param string $filename or boolean TRUE to return complete array
  106.     *    @author ? first version
  107.     *    @author Bert Vanderkimpen
  108.     *
  109.     */
  110.     function file_get_mime_type($filename)
  111.     {
  112.         //all mime types in an array (from 1.6, this is the authorative source)
  113.         //please keep this alphabetical if you add something to this list!!!
  114.         $mime_types=array(
  115.             "ai" => "application/postscript",
  116.             "aif" => "audio/x-aiff",
  117.             "aifc" => "audio/x-aiff",
  118.                "aiff" => "audio/x-aiff",
  119.             "asf" => "video/x-ms-asf",
  120.             "asc" => "text/plain",
  121.             "au" => "audio/basic",
  122.             "avi" => "video/x-msvideo",
  123.             "bcpio" => "application/x-bcpio",
  124.             "bin" => "application/octet-stream",
  125.             "bmp" => "image/bmp",
  126.             "cdf" => "application/x-netcdf",
  127.             "class" => "application/octet-stream",
  128.             "cpio" => "application/x-cpio",
  129.             "cpt" => "application/mac-compactpro",
  130.             "csh" => "application/x-csh",
  131.             "css" => "text/css",
  132.             "dcr" => "application/x-director",
  133.             "dir" => "application/x-director",
  134.             "djv" => "image/vnd.djvu",
  135.             "djvu" => "image/vnd.djvu",
  136.             "dll" => "application/octet-stream",
  137.             "dmg" => "application/x-diskcopy",
  138.             "dms" => "application/octet-stream",
  139.             "doc" => "application/msword",
  140.             "dvi" => "application/x-dvi",
  141.             "dwg" => "application/vnd.dwg",
  142.             "dxf" => "application/vnd.dxf",
  143.             "dxr" => "application/x-director",
  144.             "eps" => "application/postscript",
  145.             "etx" => "text/x-setext",
  146.             "exe" => "application/octet-stream",
  147.             "ez" => "application/andrew-inset",
  148.             "gif" => "image/gif",
  149.             "gtar" => "application/x-gtar",
  150.             "gz" => "application/x-gzip",    
  151.             "hdf" => "application/x-hdf",
  152.             "hqx" => "application/mac-binhex40",
  153.             "htm" => "text/html",
  154.             "html" => "text/html",
  155.             "ice" => "x-conference-xcooltalk",
  156.             "ief" => "image/ief",
  157.             "iges" => "model/iges",
  158.             "igs" => "model/iges",
  159.             "jar" => "application/java-archiver",
  160.             "jpe" => "image/jpeg",
  161.             "jpeg" => "image/jpeg",
  162.             "jpg" => "image/jpeg",
  163.             "js" => "application/x-javascript",
  164.             "kar" => "audio/midi",
  165.             "latex" => "application/x-latex",
  166.             "lha" => "application/octet-stream",
  167.             "lzh" => "application/octet-stream",
  168.             "m1a" => "audio/mpeg",
  169.             "m2a" => "audio/mpeg",
  170.             "m3u" => "audio/x-mpegurl",
  171.             "man" => "application/x-troff-man",
  172.             "me" => "application/x-troff-me",
  173.             "mesh" => "model/mesh",
  174.             "mid" => "audio/midi",
  175.             "midi" => "audio/midi",
  176.             "mov" => "video/quicktime",
  177.             "movie" => "video/x-sgi-movie",
  178.             "mp2" => "audio/mpeg",
  179.             "mp3" => "audio/mpeg",
  180.             "mp4" => "video/mpeg4-generic",
  181.             "mpa" => "audio/mpeg",
  182.             "mpe" => "video/mpeg",
  183.             "mpeg" => "video/mpeg",
  184.             "mpg" => "video/mpeg",
  185.             "mpga" => "audio/mpeg",
  186.             "ms" => "application/x-troff-ms",
  187.             "msh" => "model/mesh",
  188.             "mxu" => "video/vnd.mpegurl",
  189.             "nc" => "application/x-netcdf",
  190.             "oda" => "application/oda",
  191.             "pbm" => "image/x-portable-bitmap",
  192.             "pct" => "image/pict",
  193.             "pdb" => "chemical/x-pdb",
  194.             "pdf" => "application/pdf",
  195.             "pgm" => "image/x-portable-graymap",
  196.             "pgn" => "application/x-chess-pgn",
  197.             "pict" => "image/pict",
  198.             "png" => "image/png",
  199.             "pnm" => "image/x-portable-anymap",
  200.             "ppm" => "image/x-portable-pixmap",
  201.             "ppt" => "application/vnd.ms-powerpoint",
  202.             "pps" => "application/vnd.ms-powerpoint",
  203.             "ps" => "application/postscript",
  204.             "qt" => "video/quicktime",
  205.             "ra" => "audio/x-realaudio",
  206.             "ram" => "audio/x-pn-realaudio",
  207.             "rar" => "image/x-rar-compressed",
  208.             "ras" => "image/x-cmu-raster",
  209.             "rgb" => "image/x-rgb",
  210.             "rm" => "audio/x-pn-realaudio",
  211.             "roff" => "application/x-troff",
  212.             "rpm" => "audio/x-pn-realaudio-plugin",
  213.             "rtf" => "text/rtf",
  214.             "rtx" => "text/richtext",
  215.             "sgm" => "text/sgml",
  216.             "sgml" => "text/sgml",
  217.             "sh" => "application/x-sh",
  218.             "shar" => "application/x-shar",
  219.             "silo" => "model/mesh",
  220.             "sib" => "application/X-Sibelius-Score",
  221.             "sit" => "application/x-stuffit",
  222.             "skd" => "application/x-koan",
  223.             "skm" => "application/x-koan",
  224.             "skp" => "application/x-koan",
  225.             "skt" => "application/x-koan",
  226.             "smi" => "application/smil",
  227.             "smil" => "application/smil",
  228.             "snd" => "audio/basic",
  229.             "so" => "application/octet-stream",
  230.             "spl" => "application/x-futuresplash",
  231.             "src" => "application/x-wais-source",
  232.             "sv4cpio" => "application/x-sv4cpio",
  233.             "sv4crc" => "application/x-sv4crc",
  234.             "svf" => "application/vnd.svf",
  235.             "swf" => "application/x-shockwave-flash",
  236.             "sxc" => "application/vnd.sun.xml.calc",
  237.             "sxi" => "application/vnd.sun.xml.impress",
  238.             "sxw" => "application/vnd.sun.xml.writer",
  239.             "t" => "application/x-troff",
  240.             "tar" => "application/x-tar",
  241.             "tcl" => "application/x-tcl",
  242.             "tex" => "application/x-tex",
  243.             "texi" => "application/x-texinfo",
  244.             "texinfo" => "application/x-texinfo",
  245.             "tga" => "image/x-targa",
  246.             "tif" => "image/tif",
  247.             "tiff" => "image/tiff",
  248.             "tr" => "application/x-troff",
  249.             "tsv" => "text/tab-seperated-values",
  250.             "txt" => "text/plain",
  251.             "ustar" => "application/x-ustar",
  252.             "vcd" => "application/x-cdlink",
  253.             "vrml" => "model/vrml",
  254.             "wav" => "audio/x-wav",
  255.             "wbmp" => "image/vnd.wap.wbmp",
  256.             "wbxml" => "application/vnd.wap.wbxml",
  257.             "wml" => "text/vnd.wap.wml",
  258.             "wmlc" => "application/vnd.wap.wmlc",
  259.             "wmls" => "text/vnd.wap.wmlscript",
  260.             "wmlsc" => "application/vnd.wap.wmlscriptc",
  261.             "wma" => "video/x-ms-wma",   
  262.             "wmv" => "audio/x-ms-wmv",    
  263.             "wrl" => "model/vrml",
  264.             "xbm" => "image/x-xbitmap",
  265.             "xht" => "application/xhtml+xml",
  266.             "xhtml" => "application/xhtml+xml",
  267.             "xls" => "application/vnd.ms-excel",
  268.             "xml" => "text/xml",
  269.             "xpm" => "image/x-xpixmap",
  270.             "xsl" => "text/xml",
  271.             "xwd" => "image/x-windowdump",
  272.             "xyz" => "chemical/x-xyz",
  273.             "zip" => "application/zip"
  274.         );
  275.         
  276.         if ($filename === TRUE
  277.         {
  278.             return $mime_types;
  279.         }
  280.         
  281.         //get the extension of the file
  282.         $extension explode('.'$filename);
  283.  
  284.         //$filename will be an array if a . was found
  285.         if (is_array($extension))
  286.         {
  287.             $extension (strtolower($extension[sizeof($extension1]));
  288.         }
  289.         //file without extension
  290.         else
  291.         {
  292.             $extension 'empty';
  293.         }
  294.  
  295.         //if the extension is found, return the content type
  296.         if (isset ($mime_types[$extension]))
  297.             return $mime_types[$extension];
  298.         //else return octet-stream
  299.         return "application/octet-stream";
  300.     }
  301.  
  302.     /**
  303.     *    @return true if the user is allowed to see the document, false otherwise
  304.     *    @author Sergio A Kessler, first version
  305.     *    @author Roan Embrechts, bugfix
  306.     *   @todo ??not only check if a file is visible, but also check if the user is allowed to see the file??
  307.     */
  308.     function file_visible_to_user($this_course$doc_url)
  309.     {
  310.         if (api_is_allowed_to_edit())
  311.         {
  312.             return true;
  313.         }
  314.         else
  315.         {
  316.             $tbl_document Database::get_course_table(TABLE_DOCUMENT);
  317.             $tbl_item_property $this_course.'item_property';
  318.             //$doc_url = addslashes($doc_url);
  319.             $query "SELECT 1 FROM `$tbl_document` AS docs,`$tbl_item_property` AS props WHERE props.tool = 'document' AND docs.id=props.ref AND props.visibility <> '1' AND docs.path = '$doc_url'";
  320.             //echo $query;
  321.             $result api_sql_query($query__FILE____LINE__);
  322.  
  323.             return (Database::num_rows($result== 0);
  324.         }
  325.     }
  326.  
  327.     /**
  328.     * This function streams a file to the client
  329.     *
  330.     * @param string $full_file_name 
  331.     * @param boolean $forced 
  332.     * @param string $name 
  333.     * @return false if file doesn't exist, true if stream succeeded
  334.     */
  335.     function file_send_for_download($full_file_name$forced false$name '')
  336.     {
  337.         if (!is_file($full_file_name))
  338.         {
  339.             return false;
  340.         }
  341.         $filename ($name == ''basename($full_file_name$name;
  342.         $len filesize($full_file_name);
  343.  
  344.         if ($forced)
  345.         {
  346.             //force the browser to save the file instead of opening it
  347.  
  348.             header('Content-type: application/octet-stream');
  349.             //header('Content-Type: application/force-download');
  350.             header('Content-length: '.$len);
  351.             if (preg_match("/MSIE 5.5/"$_SERVER['HTTP_USER_AGENT']))
  352.             {
  353.                 header('Content-Disposition: filename= '.$filename);
  354.             }
  355.             else
  356.             {
  357.                 header('Content-Disposition: attachment; filename= '.$filename);
  358.             }
  359.             if (strpos($_SERVER['HTTP_USER_AGENT']'MSIE'))
  360.             {
  361.                 header('Pragma: ');
  362.                 header('Cache-Control: ');
  363.                 header('Cache-Control: public')// IE cannot download from sessions without a cache
  364.             }
  365.             header('Content-Description: '.$filename);
  366.             header('Content-transfer-encoding: binary');
  367.  
  368.             $fp fopen($full_file_name'r');
  369.             fpassthru($fp);
  370.             return true;
  371.         }
  372.         else
  373.         {
  374.             //no forced download, just let the browser decide what to do according to the mimetype
  375.  
  376.             $content_type DocumentManager :: file_get_mime_type($filename);
  377.             header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  378.             header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  379.             header('Cache-Control: no-cache, must-revalidate');
  380.             header('Pragma: no-cache');
  381.             header('Content-type: '.$content_type);
  382.             header('Content-Length: '.$len);
  383.             $user_agent strtolower($_SERVER['HTTP_USER_AGENT']);
  384.             if (strpos($user_agent'msie'))
  385.             {
  386.                 header('Content-Disposition: ; filename= '.$filename);
  387.             }
  388.             else
  389.             {
  390.                 header('Content-Disposition: inline; filename= '.$filename);
  391.             }
  392.             readfile($full_file_name);
  393.             return true;
  394.         }
  395.     }
  396.  
  397.     /**
  398.     * This function streams a string to the client for download.
  399.     * You have to ensure that the calling script then stops processing (exit();)
  400.     * otherwise it may cause subsequent use of the page to want to download
  401.     * other pages in php rather than interpreting them.
  402.     *
  403.     * @param string The string contents
  404.     * @param boolean Whether "save" mode is forced (or opening directly authorized)
  405.     * @param string The name of the file in the end (including extension)
  406.     * @return false if file doesn't exist, true if stream succeeded
  407.     */
  408.     function string_send_for_download($full_string$forced false$name '')
  409.     {
  410.         $filename $name;
  411.         $len strlen($full_string);
  412.  
  413.         if ($forced)
  414.         {
  415.             //force the browser to save the file instead of opening it
  416.  
  417.             header('Content-type: application/octet-stream');
  418.             //header('Content-Type: application/force-download');
  419.             header('Content-length: '.$len);
  420.             if (preg_match("/MSIE 5.5/"$_SERVER['HTTP_USER_AGENT']))
  421.             {
  422.                 header('Content-Disposition: filename= '.$filename);
  423.             }
  424.             else
  425.             {
  426.                 header('Content-Disposition: attachment; filename= '.$filename);
  427.             }
  428.             if (strpos($_SERVER['HTTP_USER_AGENT']'MSIE'))
  429.             {
  430.                 header('Pragma: ');
  431.                 header('Cache-Control: ');
  432.                 header('Cache-Control: public')// IE cannot download from sessions without a cache
  433.             }
  434.             header('Content-Description: '.$filename);
  435.             header('Content-transfer-encoding: binary');
  436.  
  437.             //$fp = fopen($full_string, 'r');
  438.             //fpassthru($fp);
  439.             echo $full_string;
  440.             return true;
  441.             //You have to ensure that the calling script then stops processing (exit();)
  442.             //otherwise it may cause subsequent use of the page to want to download
  443.             //other pages in php rather than interpreting them.
  444.         }
  445.         else
  446.         {
  447.             //no forced download, just let the browser decide what to do according to the mimetype
  448.  
  449.             $content_type DocumentManager :: file_get_mime_type($filename);
  450.             header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  451.             header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  452.             header('Cache-Control: no-cache, must-revalidate');
  453.             header('Pragma: no-cache');
  454.             header('Content-type: '.$content_type);
  455.             header('Content-Length: '.$len);
  456.             $user_agent strtolower($_SERVER['HTTP_USER_AGENT']);
  457.             if (strpos($user_agent'msie'))
  458.             {
  459.                 header('Content-Disposition: ; filename= '.$filename);
  460.             }
  461.             else
  462.             {
  463.                 header('Content-Disposition: inline; filename= '.$filename);
  464.             }
  465.             echo($full_string);
  466.             //You have to ensure that the calling script then stops processing (exit();)
  467.             //otherwise it may cause subsequent use of the page to want to download
  468.             //other pages in php rather than interpreting them.
  469.             return true;
  470.         }
  471.     }
  472.  
  473.     /**
  474.     * Fetches all document data for the given user/group
  475.     *
  476.     * @param array $_course 
  477.     * @param string $path 
  478.     * @param int $to_group_id 
  479.     * @param int $to_user_id 
  480.     * @param boolean $can_see_invisible 
  481.     * @return array with all document data
  482.     */
  483.     function get_all_document_data($_course$path '/'$to_group_id 0$to_user_id NULL$can_see_invisible false)
  484.     {
  485.         $TABLE_ITEMPROPERTY Database :: get_course_table(TABLE_ITEM_PROPERTY$_course['dbName']);
  486.         $TABLE_DOCUMENT Database :: get_course_table(TABLE_DOCUMENT$_course['dbName']);
  487.  
  488.         //if to_user_id = NULL -> change query (IS NULL)
  489.         //$to_user_id = (is_null($to_user_id))?'IS NULL':'= '.$to_user_id;
  490.         if (!is_null($to_user_id))
  491.         {
  492.             $to_field 'last.to_user_id';
  493.             $to_value $to_user_id;
  494.         }
  495.         else
  496.         {
  497.             $to_field 'last.to_group_id';
  498.             $to_value $to_group_id;
  499.         }
  500.  
  501.         //escape underscores in the path so they don't act as a wildcard
  502.         $path         Database::escape_string(str_replace('_''\_'$path));
  503.         $to_user_id Database::escape_string($to_user_id);
  504.         $to_value    Database::escape_string($to_value);
  505.  
  506.         //if they can't see invisible files, they can only see files with visibility 1
  507.         $visibility_bit ' = 1';
  508.         //if they can see invisible files, only deleted files (visibility 2) are filtered out
  509.         if ($can_see_invisible)
  510.         {
  511.             $visibility_bit ' <> 2';
  512.         }
  513.  
  514.         //the given path will not end with a slash, unless it's the root '/'
  515.         //so no root -> add slash
  516.         $added_slash ($path == '/''' '/';
  517.  
  518.         $sql "SELECT *
  519.                         FROM  ".$TABLE_ITEMPROPERTY."  AS last, ".$TABLE_DOCUMENT."  AS docs
  520.                         WHERE docs.id = last.ref
  521.                         AND docs.path LIKE '".$path.$added_slash."%'
  522.                         AND docs.path NOT LIKE '".$path.$added_slash."%/%'
  523.                         AND last.tool = '".TOOL_DOCUMENT."' 
  524.                         AND ".$to_field." = ".$to_value."
  525.                         AND last.visibility".$visibility_bit;
  526.  
  527.         //echo $sql;
  528.  
  529.         $result api_sql_query($sql);
  530.  
  531.         if ($result && Database::num_rows($result!= 0)
  532.         {
  533.             while ($row Database::fetch_array($result,'ASSOC'))
  534.                 //while ($row = Database::fetch_array($result,MYSQL_NUM))
  535.             {
  536.                 
  537.                 if($row['filetype']=='file' && pathinfo($row['path'],PATHINFO_EXTENSION)=='html'){
  538.                     
  539.                     //Templates management
  540.                     $table_template Database::get_main_table(TABLE_MAIN_TEMPLATES);
  541.                     $sql_is_template "SELECT id FROM $table_template 
  542.                                         WHERE course_code='".$_course['id']."' 
  543.                                         AND user_id='".api_get_user_id()."'
  544.                                         AND ref_doc='".$row['id']."'";
  545.                     $template_result api_sql_query($sql_is_template);
  546.                     if(Database::num_rows($template_result)>0){
  547.                         $row['is_template'1;
  548.                     }
  549.                     else{
  550.                         $row['is_template'0;
  551.                     }
  552.                 }
  553.                 
  554.                 $document_data[$row['id']] $row;
  555.                 //$document_data[] = $row;
  556.             }
  557.             return $document_data;
  558.         }
  559.         else
  560.         {
  561.             //display_error("Error getting document info from database (".mysql_error().")!");
  562.             return false;
  563.         }
  564.     }
  565.  
  566.     /**
  567.      * Gets the paths of all folders in a course
  568.      * can show all folders (exept for the deleted ones) or only visible ones
  569.      * @param array $_course 
  570.      * @param boolean $can_see_invisible 
  571.      * @param int $to_group_id 
  572.      * @return array with paths
  573.      */
  574.     function get_all_document_folders($_course$to_group_id '0'$can_see_invisible false)
  575.     {
  576.         $TABLE_ITEMPROPERTY Database :: get_course_table(TABLE_ITEM_PROPERTY$_course['dbName']);
  577.         $TABLE_DOCUMENT Database :: get_course_table(TABLE_DOCUMENT$_course['dbName']);
  578.         if(empty($to_group_id)){$to_group_id '0';//avoid empty strings in $to_group_id
  579.         if ($can_see_invisible)
  580.         {
  581.             $sql "SELECT path
  582.                                 FROM  ".$TABLE_ITEMPROPERTY."  AS last, ".$TABLE_DOCUMENT."  AS docs
  583.                                 WHERE docs.id = last.ref
  584.                                 AND docs.filetype = 'folder' 
  585.                                 AND last.tool = '".TOOL_DOCUMENT."' 
  586.                                 AND last.to_group_id = ".$to_group_id.
  587.                                 AND last.visibility <> 2";
  588.  
  589.             $result api_sql_query($sql__FILE____LINE__);
  590.  
  591.             if ($result && Database::num_rows($result!= 0)
  592.             {
  593.                 while ($row Database::fetch_array($result,'ASSOC'))
  594.                 {
  595.                     $document_folders[$row['path'];
  596.                 }
  597.                 sort($document_foldersSORT_ASC);
  598.                 //return results
  599.                 return $document_folders;
  600.             }
  601.             else
  602.             {
  603.                 return false;
  604.             }
  605.         }
  606.         //no invisible folders
  607.         else
  608.         {
  609.             //get visible folders
  610.             $visible_sql "SELECT path
  611.                         FROM  ".$TABLE_ITEMPROPERTY."  AS last, ".$TABLE_DOCUMENT."  AS docs
  612.                         WHERE docs.id = last.ref
  613.                         AND docs.filetype = 'folder' 
  614.                         AND last.tool = '".TOOL_DOCUMENT."' 
  615.                         AND last.to_group_id = ".$to_group_id.
  616.                         AND last.visibility = 1";
  617.             $visibleresult api_sql_query($visible_sql__FILE____LINE__);
  618.             while ($all_visible_folders Database::fetch_array($visibleresult,'ASSOC'))
  619.             {
  620.                 $visiblefolders[$all_visible_folders['path'];
  621.                 //echo "visible folders: ".$all_visible_folders['path']."<br>";
  622.             }
  623.             //get invisible folders
  624.             $invisible_sql "SELECT path 
  625.                         FROM  ".$TABLE_ITEMPROPERTY."  AS last, ".$TABLE_DOCUMENT."  AS docs
  626.                         WHERE docs.id = last.ref
  627.                         AND docs.filetype = 'folder' 
  628.                         AND last.tool = '".TOOL_DOCUMENT."' 
  629.                         AND last.to_group_id = ".$to_group_id.
  630.                         AND last.visibility = 0";
  631.             $invisibleresult api_sql_query($invisible_sql__FILE____LINE__);
  632.             while ($invisible_folders Database::fetch_array($invisibleresult,'ASSOC'))
  633.             {
  634.                 //get visible folders in the invisible ones -> they are invisible too
  635.                 //echo "invisible folders: ".$invisible_folders['path']."<br>";
  636.                 $folder_in_invisible_sql "SELECT path 
  637.                                 FROM  ".$TABLE_ITEMPROPERTY."  AS last, ".$TABLE_DOCUMENT."  AS docs
  638.                                 WHERE docs.id = last.ref
  639.                                 AND docs.path LIKE '".Database::escape_string($invisible_folders['path'])."/%' 
  640.                                 AND docs.filetype = 'folder' 
  641.                                 AND last.tool = '".TOOL_DOCUMENT."' 
  642.                                 AND last.to_group_id = ".$to_group_id.
  643.                                 AND last.visibility = 1";
  644.                 $folder_in_invisible_result api_sql_query($folder_in_invisible_sql__FILE____LINE__);
  645.                 while ($folders_in_invisible_folder Database::fetch_array($folder_in_invisible_result,'ASSOC'))
  646.                 {
  647.                     $invisiblefolders[$folders_in_invisible_folder['path'];
  648.                     //echo "<br>folders in invisible folders: ".$folders_in_invisible_folder['path']."<br><br><br>";
  649.                 }
  650.             }
  651.             //if both results are arrays -> //calculate the difference between the 2 arrays -> only visible folders are left :)
  652.             if (is_array($visiblefolders&& is_array($invisiblefolders))
  653.             {
  654.                 $document_folders array_diff($visiblefolders$invisiblefolders);
  655.                 sort($document_foldersSORT_ASC);
  656.                 return $document_folders;
  657.             }
  658.             //only visible folders found
  659.             elseif (is_array($visiblefolders))
  660.             {
  661.                 sort($visiblefoldersSORT_ASC);
  662.                 return $visiblefolders;
  663.             }
  664.             //no visible folders found
  665.             else
  666.             {
  667.                 return false;
  668.             }
  669.         }
  670.     }
  671.  
  672.     /**
  673.      * This deletes a document by changing visibility to 2, renaming it to filename_DELETED_#id
  674.      * Files/folders that are inside a deleted folder get visibility 2
  675.      *
  676.      * @param array $_course 
  677.      * @param string $path, path stored in the database
  678.      * @param string ,$base_work_dir, path to the documents folder
  679.      * @return boolean true/false
  680.      * @todo now only files/folders in a folder get visibility 2, we should rename them too.
  681.      */
  682.     function delete_document($_course$path$base_work_dir)
  683.     {
  684.         $TABLE_DOCUMENT Database :: get_course_table(TABLE_DOCUMENT$_course['dbName']);
  685.         $TABLE_ITEMPROPERTY Database :: get_course_table(TABLE_ITEM_PROPERTY$_course['dbName']);
  686.         //first, delete the actual document...
  687.         $document_id DocumentManager :: get_document_id($_course$path);
  688.         $new_path $path.'_DELETED_'.$document_id;
  689.  
  690.         if ($document_id)
  691.         {
  692.             if (get_setting('permanently_remove_deleted_files'== 'true'//deleted files are *really* deleted
  693.             {
  694.                 $what_to_delete_sql "SELECT id FROM ".$TABLE_DOCUMENT." WHERE path='".$path."' OR path LIKE BINARY '".$path."/%'";
  695.                 //get all id's of documents that are deleted
  696.                 $what_to_delete_result api_sql_query($what_to_delete_sql__FILE____LINE__);
  697.  
  698.                 if ($what_to_delete_result && Database::num_rows($what_to_delete_result!= 0)
  699.                 {
  700.                     //needed to deleted medadata
  701.                     require_once (api_get_path(SYS_CODE_PATH).'metadata/md_funcs.php');
  702.                     require_once(api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  703.                     $mdStore new mdstore(TRUE);
  704.  
  705.                     //delete all item_property entries
  706.                     while ($row Database::fetch_array($what_to_delete_result))
  707.                     {
  708.                         //query to delete from item_property table
  709.                         $remove_from_item_property_sql "DELETE FROM ".$TABLE_ITEMPROPERTY." WHERE ref = ".$row['id']." AND tool='".TOOL_DOCUMENT."'";
  710.                         //query to delete from document table
  711.                         $remove_from_document_sql "DELETE FROM ".$TABLE_DOCUMENT." WHERE id = ".$row['id']."";
  712.  
  713.                         //echo($remove_from_item_property_sql.'<br>');
  714.                         api_sql_query($remove_from_item_property_sql__FILE____LINE__);
  715.                         //echo($remove_from_document_sql.'<br>');
  716.                         api_sql_query($remove_from_document_sql__FILE____LINE__);
  717.  
  718.                         //delete metadata
  719.                         $eid 'Document'.'.'.$row['id'];
  720.                         $mdStore->mds_delete($eid);
  721.                         $mdStore->mds_delete_offspring($eid);
  722.  
  723.                     }
  724.                     //delete documents, do it like this so metadata get's deleted too
  725.                     //update_db_info('delete', $path);
  726.                     //throw it away
  727.                     my_delete($base_work_dir.$path);
  728.  
  729.                     return true;
  730.                 }
  731.                 else
  732.                 {
  733.                     return false;
  734.                 }
  735.             }
  736.             else //set visibility to 2 and rename file/folder to qsdqsd_DELETED_#id
  737.             {
  738.                 if (api_item_property_update($_courseTOOL_DOCUMENT$document_id'delete'api_get_user_id()))
  739.                 {
  740.                     //echo('item_property_update OK');
  741.                     if (is_file($base_work_dir.$path))
  742.                     {
  743.                         if(rename($base_work_dir.$path$base_work_dir.$new_path))
  744.                         {
  745.                             $sql "UPDATE $TABLE_DOCUMENT set path='".$new_path."' WHERE id='".$document_id."'";
  746.                             if (api_sql_query($sql__FILE____LINE__))
  747.                             {
  748.                                 //if it is a folder it can contain files
  749.                                 $sql "SELECT id,path FROM ".$TABLE_DOCUMENT." WHERE path LIKE BINARY '".$path."/%'";
  750.                                 $result api_sql_query($sql__FILE____LINE__);
  751.                                 if ($result && Database::num_rows($result0)
  752.                                 {
  753.                                     while ($deleted_items Database::fetch_array($result,'ASSOC'))
  754.                                     {
  755.                                         //echo('to delete also: id '.$deleted_items['id']);
  756.                                         api_item_property_update($_courseTOOL_DOCUMENT$deleted_items['id']'delete'api_get_user_id());
  757.                                         //Change path of subfolders and documents in database
  758.                                         $old_item_path $deleted_items['path'];
  759.                                         $new_item_path $new_path.substr($old_item_pathstrlen($path));
  760.                                         $sql "UPDATE $TABLE_DOCUMENT set path = '".$new_item_path."' WHERE id = ".$deleted_items['id'];
  761.                                         api_sql_query($sql__FILE____LINE__);
  762.                                     }
  763.                                 }
  764.                                 return true;
  765.                             }
  766.                         }
  767.                         else
  768.                         {
  769.                             //Couldn't rename - file permissions problem?
  770.                             error_log(__FILE__.' '.__LINE__.': Error renaming '.$base_work_dir.$path.' to '.$base_work_dir.$new_path.'. This is probably due to file permissions',0);
  771.                         }
  772.                     }
  773.                     else
  774.                     {
  775.                         //The file or directory isn't there anymore (on the filesystem)
  776.                         // This means it has been removed externally. To prevent a
  777.                         // blocking error from happening, we drop the related items from the
  778.                         // item_property and the document table.
  779.                         error_log(__FILE__.' '.__LINE__.': System inconsistency detected. The file or directory '.$base_work_dir.$path.' seems to have been removed from the filesystem independently from the web platform. To restore consistency, the elements using the same path will be removed from the database',0);
  780.                         $sql "SELECT id FROM $TABLE_DOCUMENT WHERE path='".$path."' OR path LIKE BINARY '".$path."/%'";
  781.                         $res Database::query($sql,__FILE__,__LINE__);
  782.                         while $row Database::fetch_array($res) ) {
  783.                             $sqlipd "DELETE FROM $TABLE_ITEMPROPERTY WHERE ref = ".$row['id']." AND tool='".TOOL_DOCUMENT."'";
  784.                             $resipd Database::query($sqlipd,__FILE__,__LINE__);
  785.                             $sqldd "DELETE FROM $TABLE_DOCUMENT WHERE id = ".$row['id'];
  786.                             $resdd Database::query($sqldd,__FILE__,__LINE__)
  787.                         }
  788.                     }
  789.                 }
  790.             }
  791.         }
  792.         return false;
  793.     }
  794.  
  795.     /**
  796.      * Gets the id of a document with a given path
  797.      *
  798.      * @param array $_course 
  799.      * @param string $path 
  800.      * @return int id of document / false if no doc found
  801.      */
  802.     function get_document_id($_course$path)
  803.     {
  804.         $TABLE_DOCUMENT Database :: get_course_table(TABLE_DOCUMENT$_course['dbName']);
  805.  
  806.         $sql "SELECT id FROM $TABLE_DOCUMENT WHERE path LIKE BINARY '$path'";
  807.  
  808.         $result api_sql_query($sql__FILE____LINE__);
  809.  
  810.         if ($result && Database::num_rows($result== 1)
  811.         {
  812.             $row Database::fetch_array($result);
  813.             return $row[0];
  814.         }
  815.         else
  816.         {
  817.             return false;
  818.         }
  819.     }
  820.     
  821.     
  822.     /**
  823.      * Allow to set a specific document as a new template for FCKEditor for a particular user in a particular course
  824.      *
  825.      * @param string $title 
  826.      * @param string $description 
  827.      * @param int $document_id_for_template the document id
  828.      * @param string $couse_code 
  829.      * @param int $user_id 
  830.      */
  831.     function set_document_as_template($title$description$document_id_for_template$couse_code$user_id){
  832.         
  833.         $table_template Database::get_main_table(TABLE_MAIN_TEMPLATES);
  834.         
  835.         $title Database::escape_string($title);
  836.         $description Database::escape_string($description);
  837.         $document_id_for_template Database::escape_string($document_id_for_template);
  838.         $couse_code Database::escape_string($couse_code);
  839.         $user_id Database::escape_string($user_id);
  840.         
  841.         $sql 'INSERT INTO '.$table_template.'(title, description, course_code, user_id, ref_doc)
  842.                 VALUES ("'.$title.'", "'.$description.'", "'.$couse_code.'", "'.$user_id.'", "'.$document_id_for_template.'")';
  843.         
  844.         api_sql_query($sql);
  845.         
  846.     }
  847.     
  848.     
  849.     /**
  850.      * Unset a document as template
  851.      *
  852.      * @param int $document_id 
  853.      * @param string $couse_code 
  854.      * @param int $user_id 
  855.      */
  856.     function unset_document_as_template($document_id$couse_code$user_id){
  857.         
  858.         $table_template Database::get_main_table(TABLE_MAIN_TEMPLATES);
  859.         
  860.         $sql 'SELECT id FROM '.$table_template.' WHERE course_code="'.$couse_code.'" AND user_id="'.$user_id.'" AND ref_doc="'.$document_id.'"';
  861.         $result api_sql_query($sql);
  862.         $template_id Database::result($result,0,0);
  863.         
  864.         include_once(api_get_path(LIBRARY_PATH'fileManage.lib.php');
  865.         my_delete(api_get_path(SYS_CODE_PATH).'upload/template_thumbnails/'.$template_id.'.jpg');
  866.         
  867.         $sql 'DELETE FROM '.$table_template.' WHERE course_code="'.$couse_code.'" AND user_id="'.$user_id.'" AND ref_doc="'.$document_id.'"';
  868.         
  869.         api_sql_query($sql);
  870.         
  871.     }
  872.     
  873.  
  874. }
  875. //end class DocumentManager
  876. ?>

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