Source for file document.lib.php
Documentation is available at document.lib.php
==============================================================================
Dokeos - elearning and course management software
Copyright (c) 2004-2008 Dokeos SPRL
Copyright (c) 2003 Ghent University (UGent)
Copyright (c) Roan Embrechts, Vrije Universiteit Brussel
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 address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
==============================================================================
==============================================================================
* This is the document library for Dokeos.
* It is / will be used to provide a service layer to all document-using tools.
* and eliminate code duplication fro group documents, scorm documents, main documents.
* Include/require it in your code to use its functionality.
* @version 1.1, January 2005
* @package dokeos.library
==============================================================================
==============================================================================
use the functions like this: DocumentManager::get_course_quota()
==============================================================================
==============================================================================
==============================================================================
define("DISK_QUOTA_FIELD", "disk_quota"); //name of the database field
/** default quota for the course documents folder */
define("DEFAULT_DOCUMENT_QUOTA", get_setting('default_document_quotum'));
==============================================================================
==============================================================================
$baseServUrl = $_configuration['url_append']. "/";
$baseWorkDir = $sys_course_path. (!empty($courseDir)? $courseDir: '');
==============================================================================
the class and its functions
==============================================================================
* @package dokeos.library
* @return the document folder quuta of the current course, in bytes
* @todo eliminate globals
global $_course, $maxFilledSpace;
$course_code = $_course['sysCode'];
$sql_query = "SELECT `". DISK_QUOTA_FIELD. "` FROM $course_table WHERE `code` = '$course_code'";
if ($course_quota == NULL)
//course table entry for quota was null
* Get the content type of a file by checking the extension
* We could use mime_content_type() with php-versions > 4.3,
* but this doesn't work as it should on Windows installations
* @param string $filename or boolean TRUE to return complete array
* @author ? first version
* @author Bert Vanderkimpen
//all mime types in an array (from 1.6, this is the authorative source)
//please keep this alphabetical if you add something to this list!!!
"ai" => "application/postscript",
"aifc" => "audio/x-aiff",
"aiff" => "audio/x-aiff",
"asf" => "video/x-ms-asf",
"avi" => "video/x-msvideo",
"bcpio" => "application/x-bcpio",
"bin" => "application/octet-stream",
"cdf" => "application/x-netcdf",
"class" => "application/octet-stream",
"cpio" => "application/x-cpio",
"cpt" => "application/mac-compactpro",
"csh" => "application/x-csh",
"dcr" => "application/x-director",
"dir" => "application/x-director",
"djv" => "image/vnd.djvu",
"djvu" => "image/vnd.djvu",
"dll" => "application/octet-stream",
"dmg" => "application/x-diskcopy",
"dms" => "application/octet-stream",
"doc" => "application/msword",
"dvi" => "application/x-dvi",
"dwg" => "application/vnd.dwg",
"dxf" => "application/vnd.dxf",
"dxr" => "application/x-director",
"eps" => "application/postscript",
"etx" => "text/x-setext",
"exe" => "application/octet-stream",
"ez" => "application/andrew-inset",
"gtar" => "application/x-gtar",
"gz" => "application/x-gzip",
"hdf" => "application/x-hdf",
"hqx" => "application/mac-binhex40",
"ice" => "x-conference-xcooltalk",
"jar" => "application/java-archiver",
"js" => "application/x-javascript",
"latex" => "application/x-latex",
"lha" => "application/octet-stream",
"lzh" => "application/octet-stream",
"m3u" => "audio/x-mpegurl",
"man" => "application/x-troff-man",
"me" => "application/x-troff-me",
"mov" => "video/quicktime",
"movie" => "video/x-sgi-movie",
"mp4" => "video/mpeg4-generic",
"ms" => "application/x-troff-ms",
"mxu" => "video/vnd.mpegurl",
"nc" => "application/x-netcdf",
"oda" => "application/oda",
"pbm" => "image/x-portable-bitmap",
"pdb" => "chemical/x-pdb",
"pdf" => "application/pdf",
"pgm" => "image/x-portable-graymap",
"pgn" => "application/x-chess-pgn",
"pnm" => "image/x-portable-anymap",
"ppm" => "image/x-portable-pixmap",
"ppt" => "application/vnd.ms-powerpoint",
"pps" => "application/vnd.ms-powerpoint",
"ps" => "application/postscript",
"qt" => "video/quicktime",
"ra" => "audio/x-realaudio",
"ram" => "audio/x-pn-realaudio",
"rar" => "image/x-rar-compressed",
"ras" => "image/x-cmu-raster",
"rm" => "audio/x-pn-realaudio",
"roff" => "application/x-troff",
"rpm" => "audio/x-pn-realaudio-plugin",
"rtx" => "text/richtext",
"sh" => "application/x-sh",
"shar" => "application/x-shar",
"sib" => "application/X-Sibelius-Score",
"sit" => "application/x-stuffit",
"skd" => "application/x-koan",
"skm" => "application/x-koan",
"skp" => "application/x-koan",
"skt" => "application/x-koan",
"smi" => "application/smil",
"smil" => "application/smil",
"so" => "application/octet-stream",
"spl" => "application/x-futuresplash",
"src" => "application/x-wais-source",
"sv4cpio" => "application/x-sv4cpio",
"sv4crc" => "application/x-sv4crc",
"svf" => "application/vnd.svf",
"swf" => "application/x-shockwave-flash",
"sxc" => "application/vnd.sun.xml.calc",
"sxi" => "application/vnd.sun.xml.impress",
"sxw" => "application/vnd.sun.xml.writer",
"t" => "application/x-troff",
"tar" => "application/x-tar",
"tcl" => "application/x-tcl",
"tex" => "application/x-tex",
"texi" => "application/x-texinfo",
"texinfo" => "application/x-texinfo",
"tga" => "image/x-targa",
"tr" => "application/x-troff",
"tsv" => "text/tab-seperated-values",
"ustar" => "application/x-ustar",
"vcd" => "application/x-cdlink",
"wbmp" => "image/vnd.wap.wbmp",
"wbxml" => "application/vnd.wap.wbxml",
"wml" => "text/vnd.wap.wml",
"wmlc" => "application/vnd.wap.wmlc",
"wmls" => "text/vnd.wap.wmlscript",
"wmlsc" => "application/vnd.wap.wmlscriptc",
"wma" => "video/x-ms-wma",
"wmv" => "audio/x-ms-wmv",
"xbm" => "image/x-xbitmap",
"xht" => "application/xhtml+xml",
"xhtml" => "application/xhtml+xml",
"xls" => "application/vnd.ms-excel",
"xpm" => "image/x-xpixmap",
"xwd" => "image/x-windowdump",
"xyz" => "chemical/x-xyz",
"zip" => "application/zip"
//get the extension of the file
$extension = explode('.', $filename);
//$filename will be an array if a . was found
//if the extension is found, return the content type
if (isset ($mime_types[$extension]))
return $mime_types[$extension];
//else return octet-stream
return "application/octet-stream";
* @return true if the user is allowed to see the document, false otherwise
* @author Sergio A Kessler, first version
* @author Roan Embrechts, bugfix
* @todo ??not only check if a file is visible, but also check if the user is allowed to see the file??
$tbl_item_property = $this_course. 'item_property';
//$doc_url = addslashes($doc_url);
$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'";
* This function streams a file to the client
* @param string $full_file_name
* @return false if file doesn't exist, true if stream succeeded
$filename = ($name == '') ? basename($full_file_name) : $name;
//force the browser to save the file instead of opening it
header('Content-type: application/octet-stream');
//header('Content-Type: application/force-download');
header('Content-length: '. $len);
if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT']))
header('Content-Disposition: filename= '. $filename);
header('Content-Disposition: attachment; filename= '. $filename);
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
header('Cache-Control: public'); // IE cannot download from sessions without a cache
header('Content-Description: '. $filename);
header('Content-transfer-encoding: binary');
$fp = fopen($full_file_name, 'r');
//no forced download, just let the browser decide what to do according to the mimetype
header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: '. $content_type);
header('Content-Length: '. $len);
$user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($user_agent, 'msie'))
header('Content-Disposition: ; filename= '. $filename);
header('Content-Disposition: inline; filename= '. $filename);
* This function streams a string to the client for download.
* You have to ensure that the calling script then stops processing (exit();)
* otherwise it may cause subsequent use of the page to want to download
* other pages in php rather than interpreting them.
* @param string The string contents
* @param boolean Whether "save" mode is forced (or opening directly authorized)
* @param string The name of the file in the end (including extension)
* @return false if file doesn't exist, true if stream succeeded
//force the browser to save the file instead of opening it
header('Content-type: application/octet-stream');
//header('Content-Type: application/force-download');
header('Content-length: '. $len);
if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT']))
header('Content-Disposition: filename= '. $filename);
header('Content-Disposition: attachment; filename= '. $filename);
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
header('Cache-Control: public'); // IE cannot download from sessions without a cache
header('Content-Description: '. $filename);
header('Content-transfer-encoding: binary');
//$fp = fopen($full_string, 'r');
//You have to ensure that the calling script then stops processing (exit();)
//otherwise it may cause subsequent use of the page to want to download
//other pages in php rather than interpreting them.
//no forced download, just let the browser decide what to do according to the mimetype
header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: '. $content_type);
header('Content-Length: '. $len);
$user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($user_agent, 'msie'))
header('Content-Disposition: ; filename= '. $filename);
header('Content-Disposition: inline; filename= '. $filename);
//You have to ensure that the calling script then stops processing (exit();)
//otherwise it may cause subsequent use of the page to want to download
//other pages in php rather than interpreting them.
* Fetches all document data for the given user/group
* @param int $to_group_id
* @param boolean $can_see_invisible
* @return array with all document data
function get_all_document_data($_course, $path = '/', $to_group_id = 0, $to_user_id = NULL, $can_see_invisible = false)
//if to_user_id = NULL -> change query (IS NULL)
//$to_user_id = (is_null($to_user_id))?'IS NULL':'= '.$to_user_id;
$to_field = 'last.to_user_id';
$to_field = 'last.to_group_id';
$to_value = $to_group_id;
//escape underscores in the path so they don't act as a wildcard
//if they can't see invisible files, they can only see files with visibility 1
$visibility_bit = ' = 1';
//if they can see invisible files, only deleted files (visibility 2) are filtered out
$visibility_bit = ' <> 2';
//the given path will not end with a slash, unless it's the root '/'
//so no root -> add slash
$added_slash = ($path == '/') ? '' : '/';
FROM ". $TABLE_ITEMPROPERTY. " AS last, ". $TABLE_DOCUMENT. " AS docs
AND docs.path LIKE '". $path. $added_slash. "%'
AND docs.path NOT LIKE '". $path. $added_slash. "%/%'
AND ". $to_field. " = ". $to_value. "
AND last.visibility". $visibility_bit;
//while ($row = Database::fetch_array($result,MYSQL_NUM))
if($row['filetype']== 'file' && pathinfo($row['path'],PATHINFO_EXTENSION)== 'html'){
$sql_is_template = "SELECT id FROM $table_template
WHERE course_code='". $_course['id']. "'
AND ref_doc='". $row['id']. "'";
$document_data[$row['id']] = $row;
//$document_data[] = $row;
//display_error("Error getting document info from database (".mysql_error().")!");
* Gets the paths of all folders in a course
* can show all folders (exept for the deleted ones) or only visible ones
* @param boolean $can_see_invisible
* @param int $to_group_id
* @return array with paths
if(empty($to_group_id)){$to_group_id = '0';} //avoid empty strings in $to_group_id
FROM ". $TABLE_ITEMPROPERTY. " AS last, ". $TABLE_DOCUMENT. " AS docs
AND docs.filetype = 'folder'
AND last.to_group_id = ". $to_group_id. "
AND last.visibility <> 2";
$document_folders[] = $row['path'];
sort($document_folders, SORT_ASC);
return $document_folders;
$visible_sql = "SELECT path
FROM ". $TABLE_ITEMPROPERTY. " AS last, ". $TABLE_DOCUMENT. " AS docs
AND docs.filetype = 'folder'
AND last.to_group_id = ". $to_group_id. "
AND last.visibility = 1";
$visibleresult = api_sql_query($visible_sql, __FILE__ , __LINE__ );
$visiblefolders[] = $all_visible_folders['path'];
//echo "visible folders: ".$all_visible_folders['path']."<br>";
$invisible_sql = "SELECT path
FROM ". $TABLE_ITEMPROPERTY. " AS last, ". $TABLE_DOCUMENT. " AS docs
AND docs.filetype = 'folder'
AND last.to_group_id = ". $to_group_id. "
AND last.visibility = 0";
$invisibleresult = api_sql_query($invisible_sql, __FILE__ , __LINE__ );
//get visible folders in the invisible ones -> they are invisible too
//echo "invisible folders: ".$invisible_folders['path']."<br>";
$folder_in_invisible_sql = "SELECT path
FROM ". $TABLE_ITEMPROPERTY. " AS last, ". $TABLE_DOCUMENT. " AS docs
AND docs.filetype = 'folder'
AND last.to_group_id = ". $to_group_id. "
AND last.visibility = 1";
$folder_in_invisible_result = api_sql_query($folder_in_invisible_sql, __FILE__ , __LINE__ );
while ($folders_in_invisible_folder = Database::fetch_array($folder_in_invisible_result,'ASSOC'))
$invisiblefolders[] = $folders_in_invisible_folder['path'];
//echo "<br>folders in invisible folders: ".$folders_in_invisible_folder['path']."<br><br><br>";
//if both results are arrays -> //calculate the difference between the 2 arrays -> only visible folders are left :)
$document_folders = array_diff($visiblefolders, $invisiblefolders);
sort($document_folders, SORT_ASC);
return $document_folders;
//only visible folders found
sort($visiblefolders, SORT_ASC);
//no visible folders found
* This deletes a document by changing visibility to 2, renaming it to filename_DELETED_#id
* Files/folders that are inside a deleted folder get visibility 2
* @param string $path, path stored in the database
* @param string ,$base_work_dir, path to the documents folder
* @return boolean true/false
* @todo now only files/folders in a folder get visibility 2, we should rename them too.
//first, delete the actual document...
$new_path = $path. '_DELETED_'. $document_id;
if (get_setting('permanently_remove_deleted_files') == 'true') //deleted files are *really* deleted
$what_to_delete_sql = "SELECT id FROM ". $TABLE_DOCUMENT. " WHERE path='". $path. "' OR path LIKE BINARY '". $path. "/%'";
//get all id's of documents that are deleted
$what_to_delete_result = api_sql_query($what_to_delete_sql, __FILE__ , __LINE__ );
//needed to deleted medadata
//delete all item_property entries
//query to delete from item_property table
$remove_from_item_property_sql = "DELETE FROM ". $TABLE_ITEMPROPERTY. " WHERE ref = ". $row['id']. " AND tool='". TOOL_DOCUMENT. "'";
//query to delete from document table
$remove_from_document_sql = "DELETE FROM ". $TABLE_DOCUMENT. " WHERE id = ". $row['id']. "";
//echo($remove_from_item_property_sql.'<br>');
api_sql_query($remove_from_item_property_sql, __FILE__ , __LINE__ );
//echo($remove_from_document_sql.'<br>');
$eid = 'Document'. '.'. $row['id'];
$mdStore->mds_delete($eid);
$mdStore->mds_delete_offspring($eid);
//delete documents, do it like this so metadata get's deleted too
//update_db_info('delete', $path);
else //set visibility to 2 and rename file/folder to qsdqsd_DELETED_#id
//echo('item_property_update OK');
if(rename($base_work_dir. $path, $base_work_dir. $new_path))
$sql = "UPDATE $TABLE_DOCUMENT set path='". $new_path. "' WHERE id='". $document_id. "'";
//if it is a folder it can contain files
$sql = "SELECT id,path FROM ". $TABLE_DOCUMENT. " WHERE path LIKE BINARY '". $path. "/%'";
//echo('to delete also: id '.$deleted_items['id']);
//Change path of subfolders and documents in database
$old_item_path = $deleted_items['path'];
$new_item_path = $new_path. substr($old_item_path, strlen($path));
$sql = "UPDATE $TABLE_DOCUMENT set path = '". $new_item_path. "' WHERE id = ". $deleted_items['id'];
//Couldn't rename - file permissions problem?
error_log(__FILE__. ' '.__LINE__. ': Error renaming '. $base_work_dir. $path. ' to '. $base_work_dir. $new_path. '. This is probably due to file permissions',0);
//The file or directory isn't there anymore (on the filesystem)
// This means it has been removed externally. To prevent a
// blocking error from happening, we drop the related items from the
// item_property and the document table.
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);
$sql = "SELECT id FROM $TABLE_DOCUMENT WHERE path='". $path. "' OR path LIKE BINARY '". $path. "/%'";
$sqlipd = "DELETE FROM $TABLE_ITEMPROPERTY WHERE ref = ". $row['id']. " AND tool='". TOOL_DOCUMENT. "'";
$sqldd = "DELETE FROM $TABLE_DOCUMENT WHERE id = ". $row['id'];
* Gets the id of a document with a given path
* @return int id of document / false if no doc found
$sql = "SELECT id FROM $TABLE_DOCUMENT WHERE path LIKE BINARY '$path'";
* Allow to set a specific document as a new template for FCKEditor for a particular user in a particular course
* @param string $description
* @param int $document_id_for_template the document id
* @param string $couse_code
$sql = 'INSERT INTO '. $table_template. '(title, description, course_code, user_id, ref_doc)
VALUES ("'. $title. '", "'. $description. '", "'. $couse_code. '", "'. $user_id. '", "'. $document_id_for_template. '")';
* Unset a document as template
* @param int $document_id
* @param string $couse_code
$sql = 'SELECT id FROM '. $table_template. ' WHERE course_code="'. $couse_code. '" AND user_id="'. $user_id. '" AND ref_doc="'. $document_id. '"';
$sql = 'DELETE FROM '. $table_template. ' WHERE course_code="'. $couse_code. '" AND user_id="'. $user_id. '" AND ref_doc="'. $document_id. '"';
//end class DocumentManager
|