document.lib.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2005 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) Roan Embrechts, Vrije Universiteit Brussel
  8. For a full list of contributors, see "credits.txt".
  9. The full license can be read in "license.txt".
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. See the GNU General Public License for more details.
  15. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  16. Mail: info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * This is the document library for Dokeos.
  22. * It is / will be used to provide a service layer to all document-using tools.
  23. * and eliminate code duplication fro group documents, scorm documents, main documents.
  24. * Include/require it in your code to use its functionality.
  25. *
  26. * @version 1.1, January 2005
  27. * @package dokeos.library
  28. ==============================================================================
  29. */
  30. /*
  31. ==============================================================================
  32. DOCUMENTATION
  33. use the functions like this: DocumentManager::get_course_quota()
  34. ==============================================================================
  35. */
  36. /*
  37. ==============================================================================
  38. CONSTANTS
  39. ==============================================================================
  40. */
  41. define("DISK_QUOTA_FIELD", "disk_quota"); //name of the database field
  42. /** default quota for the course documents folder */
  43. define("DEFAULT_DOCUMENT_QUOTA", get_setting('default_document_quotum'));
  44. /*
  45. ==============================================================================
  46. VARIABLES
  47. ==============================================================================
  48. */
  49. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  50. $baseServDir = api_get_path(SYS_PATH);
  51. $baseServUrl = $_configuration['url_append']."/";
  52. $baseWorkDir = $sys_course_path.$courseDir;
  53. /*
  54. ==============================================================================
  55. DocumentManager CLASS
  56. the class and its functions
  57. ==============================================================================
  58. */
  59. /**
  60. * @package dokeos.library
  61. */
  62. class DocumentManager
  63. {
  64. /**
  65. * @return the document folder quuta of the current course, in bytes
  66. * @todo eliminate globals
  67. */
  68. function get_course_quota()
  69. {
  70. global $_course, $maxFilledSpace;
  71. $course_code = $_course['sysCode'];
  72. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  73. $sql_query = "SELECT `".DISK_QUOTA_FIELD."` FROM $course_table WHERE `code` = '$course_code'";
  74. $sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
  75. $result = mysql_fetch_array($sql_result);
  76. $course_quota = $result[DISK_QUOTA_FIELD];
  77. if ($course_quota == NULL)
  78. {
  79. //course table entry for quota was null
  80. //use default value
  81. $course_quota = DEFAULT_DOCUMENT_QUOTA;
  82. }
  83. return $course_quota;
  84. }
  85. /**
  86. * Get the content type of a file by checking the extension
  87. * We could use mime_content_type() with php-versions > 4.3,
  88. * but this doesn't work as it should on Windows installations
  89. *
  90. * @param string $filename or boolean TRUE to return complete array
  91. * @author ? first version
  92. * @author Bert Vanderkimpen
  93. *
  94. */
  95. function file_get_mime_type($filename)
  96. {
  97. //all mime types in an array (from 1.6, this is the authorative source)
  98. //please keep this alphabetical if you add something to this list!!!
  99. $mime_types=array(
  100. "ai" => "application/postscript",
  101. "aif" => "audio/x-aiff",
  102. "aifc" => "audio/x-aiff",
  103. "aiff" => "audio/x-aiff",
  104. "asf" => "video/x-ms-asf",
  105. "asc" => "text/plain",
  106. "au" => "audio/basic",
  107. "avi" => "video/x-msvideo",
  108. "bcpio" => "application/x-bcpio",
  109. "bin" => "application/octet-stream",
  110. "bmp" => "image/bmp",
  111. "cdf" => "application/x-netcdf",
  112. "class" => "application/octet-stream",
  113. "cpio" => "application/x-cpio",
  114. "cpt" => "application/mac-compactpro",
  115. "csh" => "application/x-csh",
  116. "css" => "text/css",
  117. "dcr" => "application/x-director",
  118. "dir" => "application/x-director",
  119. "djv" => "image/vnd.djvu",
  120. "djvu" => "image/vnd.djvu",
  121. "dll" => "application/octet-stream",
  122. "dmg" => "application/x-diskcopy",
  123. "dms" => "application/octet-stream",
  124. "doc" => "application/msword",
  125. "dvi" => "application/x-dvi",
  126. "dwg" => "application/vnd.dwg",
  127. "dxf" => "application/vnd.dxf",
  128. "dxr" => "application/x-director",
  129. "eps" => "application/postscript",
  130. "etx" => "text/x-setext",
  131. "exe" => "application/octet-stream",
  132. "ez" => "application/andrew-inset",
  133. "gif" => "image/gif",
  134. "gtar" => "application/x-gtar",
  135. "gz" => "application/x-gzip",
  136. "hdf" => "application/x-hdf",
  137. "hqx" => "application/mac-binhex40",
  138. "htm" => "text/html",
  139. "html" => "text/html",
  140. "ice" => "x-conference-xcooltalk",
  141. "ief" => "image/ief",
  142. "iges" => "model/iges",
  143. "igs" => "model/iges",
  144. "jar" => "application/java-archiver",
  145. "jpe" => "image/jpeg",
  146. "jpeg" => "image/jpeg",
  147. "jpg" => "image/jpeg",
  148. "js" => "application/x-javascript",
  149. "kar" => "audio/midi",
  150. "latex" => "application/x-latex",
  151. "lha" => "application/octet-stream",
  152. "lzh" => "application/octet-stream",
  153. "m1a" => "audio/mpeg",
  154. "m2a" => "audio/mpeg",
  155. "m3u" => "audio/x-mpegurl",
  156. "man" => "application/x-troff-man",
  157. "me" => "application/x-troff-me",
  158. "mesh" => "model/mesh",
  159. "mid" => "audio/midi",
  160. "midi" => "audio/midi",
  161. "mov" => "video/quicktime",
  162. "movie" => "video/x-sgi-movie",
  163. "mp2" => "audio/mpeg",
  164. "mp3" => "audio/mpeg",
  165. "mp4" => "video/mpeg4-generic",
  166. "mpa" => "audio/mpeg",
  167. "mpe" => "video/mpeg",
  168. "mpeg" => "video/mpeg",
  169. "mpg" => "video/mpeg",
  170. "mpga" => "audio/mpeg",
  171. "ms" => "application/x-troff-ms",
  172. "msh" => "model/mesh",
  173. "mxu" => "video/vnd.mpegurl",
  174. "nc" => "application/x-netcdf",
  175. "oda" => "application/oda",
  176. "pbm" => "image/x-portable-bitmap",
  177. "pct" => "image/pict",
  178. "pdb" => "chemical/x-pdb",
  179. "pdf" => "application/pdf",
  180. "pgm" => "image/x-portable-graymap",
  181. "pgn" => "application/x-chess-pgn",
  182. "pict" => "image/pict",
  183. "png" => "image/png",
  184. "pnm" => "image/x-portable-anymap",
  185. "ppm" => "image/x-portable-pixmap",
  186. "ppt" => "application/vnd.ms-powerpoint",
  187. "pps" => "application/vnd.ms-powerpoint",
  188. "ps" => "application/postscript",
  189. "qt" => "video/quicktime",
  190. "ra" => "audio/x-realaudio",
  191. "ram" => "audio/x-pn-realaudio",
  192. "rar" => "image/x-rar-compressed",
  193. "ras" => "image/x-cmu-raster",
  194. "rgb" => "image/x-rgb",
  195. "rm" => "audio/x-pn-realaudio",
  196. "roff" => "application/x-troff",
  197. "rpm" => "audio/x-pn-realaudio-plugin",
  198. "rtf" => "text/rtf",
  199. "rtx" => "text/richtext",
  200. "sgm" => "text/sgml",
  201. "sgml" => "text/sgml",
  202. "sh" => "application/x-sh",
  203. "shar" => "application/x-shar",
  204. "silo" => "model/mesh",
  205. "sib" => "application/X-Sibelius-Score",
  206. "sit" => "application/x-stuffit",
  207. "skd" => "application/x-koan",
  208. "skm" => "application/x-koan",
  209. "skp" => "application/x-koan",
  210. "skt" => "application/x-koan",
  211. "smi" => "application/smil",
  212. "smil" => "application/smil",
  213. "snd" => "audio/basic",
  214. "so" => "application/octet-stream",
  215. "spl" => "application/x-futuresplash",
  216. "src" => "application/x-wais-source",
  217. "sv4cpio" => "application/x-sv4cpio",
  218. "sv4crc" => "application/x-sv4crc",
  219. "svf" => "application/vnd.svf",
  220. "swf" => "application/x-shockwave-flash",
  221. "sxc" => "application/vnd.sun.xml.calc",
  222. "sxi" => "application/vnd.sun.xml.impress",
  223. "sxw" => "application/vnd.sun.xml.writer",
  224. "t" => "application/x-troff",
  225. "tar" => "application/x-tar",
  226. "tcl" => "application/x-tcl",
  227. "tex" => "application/x-tex",
  228. "texi" => "application/x-texinfo",
  229. "texinfo" => "application/x-texinfo",
  230. "tga" => "image/x-targa",
  231. "tif" => "image/tif",
  232. "tiff" => "image/tiff",
  233. "tr" => "application/x-troff",
  234. "tsv" => "text/tab-seperated-values",
  235. "txt" => "text/plain",
  236. "ustar" => "application/x-ustar",
  237. "vcd" => "application/x-cdlink",
  238. "vrml" => "model/vrml",
  239. "wav" => "audio/x-wav",
  240. "wbmp" => "image/vnd.wap.wbmp",
  241. "wbxml" => "application/vnd.wap.wbxml",
  242. "wml" => "text/vnd.wap.wml",
  243. "wmlc" => "application/vnd.wap.wmlc",
  244. "wmls" => "text/vnd.wap.wmlscript",
  245. "wmlsc" => "application/vnd.wap.wmlscriptc",
  246. "wma" => "video/x-ms-wma",
  247. "wmv" => "video/x-ms-wmv",
  248. "wrl" => "model/vrml",
  249. "xbm" => "image/x-xbitmap",
  250. "xht" => "application/xhtml+xml",
  251. "xhtml" => "application/xhtml+xml",
  252. "xls" => "application/vnd.ms-excel",
  253. "xml" => "text/xml",
  254. "xpm" => "image/x-xpixmap",
  255. "xsl" => "text/xml",
  256. "xwd" => "image/x-windowdump",
  257. "xyz" => "chemical/x-xyz",
  258. "zip" => "application/zip"
  259. );
  260. if ($filename === TRUE)
  261. {
  262. return $mime_types;
  263. }
  264. //get the extension of the file
  265. $extension = explode('.', $filename);
  266. //$filename will be an array if a . was found
  267. if (is_array($extension))
  268. {
  269. $extension = (strtolower($extension[sizeof($extension) - 1]));
  270. }
  271. //file without extension
  272. else
  273. {
  274. $extension = 'empty';
  275. }
  276. //if the extension is found, return the content type
  277. if (isset ($mime_types[$extension]))
  278. return $mime_types[$extension];
  279. //else return octet-stream
  280. return "application/octet-stream";
  281. }
  282. /**
  283. * @return true if the user is allowed to see the document, false otherwise
  284. * @author Sergio A Kessler, first version
  285. * @author Roan Embrechts, bugfix
  286. * @todo ??not only check if a file is visible, but also check if the user is allowed to see the file??
  287. */
  288. function file_visible_to_user($this_course, $doc_url)
  289. {
  290. if (api_is_allowed_to_edit())
  291. {
  292. return true;
  293. }
  294. else
  295. {
  296. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  297. $tbl_item_property = $this_course.'item_property';
  298. //$doc_url = addslashes($doc_url);
  299. $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'";
  300. //echo $query;
  301. $result = api_sql_query($query, __FILE__, __LINE__);
  302. return (mysql_num_rows($result) == 0);
  303. }
  304. }
  305. /**
  306. * This function streams a file to the client
  307. *
  308. * @param string $full_file_name
  309. * @param boolean $forced
  310. * @param string $name
  311. * @return false if file doesn't exist, true if stream succeeded
  312. */
  313. function file_send_for_download($full_file_name, $forced = false, $name = '')
  314. {
  315. if (!is_file($full_file_name))
  316. {
  317. return false;
  318. }
  319. $filename = ($name == '') ? basename($full_file_name) : $name;
  320. $len = filesize($full_file_name);
  321. if ($forced)
  322. {
  323. //force the browser to save the file instead of opening it
  324. header('Content-type: application/octet-stream');
  325. //header('Content-Type: application/force-download');
  326. header('Content-length: '.$len);
  327. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT']))
  328. {
  329. header('Content-Disposition: filename= '.$filename);
  330. }
  331. else
  332. {
  333. header('Content-Disposition: attachment; filename= '.$filename);
  334. }
  335. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
  336. {
  337. header('Pragma: ');
  338. header('Cache-Control: ');
  339. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  340. }
  341. header('Content-Description: '.$filename);
  342. header('Content-transfer-encoding: binary');
  343. $fp = fopen($full_file_name, 'r');
  344. fpassthru($fp);
  345. return true;
  346. }
  347. else
  348. {
  349. //no forced download, just let the browser decide what to do according to the mimetype
  350. $content_type = DocumentManager :: file_get_mime_type($filename);
  351. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  352. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  353. header('Cache-Control: no-cache, must-revalidate');
  354. header('Pragma: no-cache');
  355. header('Content-type: '.$content_type);
  356. header('Content-Length: '.$len);
  357. $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  358. if (strpos($user_agent, 'msie'))
  359. {
  360. header('Content-Disposition: ; filename= '.$filename);
  361. }
  362. else
  363. {
  364. header('Content-Disposition: inline; filename= '.$filename);
  365. }
  366. readfile($full_file_name);
  367. return true;
  368. }
  369. }
  370. /**
  371. * Fetches all document data for the given user/group
  372. *
  373. * @param array $_course
  374. * @param string $path
  375. * @param int $to_group_id
  376. * @param int $to_user_id
  377. * @param boolean $can_see_invisible
  378. * @return array with all document data
  379. */
  380. function get_all_document_data($_course, $path = '/', $to_group_id = 0, $to_user_id = NULL, $can_see_invisible = false)
  381. {
  382. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY, $_course['dbName']);
  383. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT, $_course['dbName']);
  384. //if to_user_id = NULL -> change query (IS NULL)
  385. //$to_user_id = (is_null($to_user_id))?'IS NULL':'= '.$to_user_id;
  386. if (!is_null($to_user_id))
  387. {
  388. $to_field = 'last.to_user_id';
  389. $to_value = $to_user_id;
  390. }
  391. else
  392. {
  393. $to_field = 'last.to_group_id';
  394. $to_value = $to_group_id;
  395. }
  396. //escape underscores in the path so they don't act as a wildcard
  397. $path = str_replace('_', '\_', $path);
  398. //if they can't see invisible files, they can only see files with visibility 1
  399. $visibility_bit = ' = 1';
  400. //if they can see invisible files, only deleted files (visibility 2) are filtered out
  401. if ($can_see_invisible)
  402. {
  403. $visibility_bit = ' <> 2';
  404. }
  405. //the given path will not end with a slash, unless it's the root '/'
  406. //so no root -> add slash
  407. $added_slash = ($path == '/') ? '' : '/';
  408. $sql = "SELECT *
  409. FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
  410. WHERE docs.id = last.ref
  411. AND docs.path LIKE '".$path.$added_slash."%'
  412. AND docs.path NOT LIKE '".$path.$added_slash."%/%'
  413. AND last.tool = '".TOOL_DOCUMENT."'
  414. AND ".$to_field." = ".$to_value."
  415. AND last.visibility".$visibility_bit;
  416. //echo $sql;
  417. $result = mysql_query($sql);
  418. if ($result && mysql_num_rows($result) != 0)
  419. {
  420. while ($row = mysql_fetch_assoc($result))
  421. //while ($row = mysql_fetch_array($result,MYSQL_NUM))
  422. {
  423. $document_data[$row['id']] = $row;
  424. //$document_data[] = $row;
  425. }
  426. return $document_data;
  427. }
  428. else
  429. {
  430. //display_error("Error getting document info from database (".mysql_error().")!");
  431. return false;
  432. }
  433. }
  434. /**
  435. * Gets the paths of all folders in a course
  436. * can show all folders (exept for the deleted ones) or only visible ones
  437. * @param array $_course
  438. * @param boolean $can_see_invisible
  439. * @param int $to_group_id
  440. * @return array with paths
  441. */
  442. function get_all_document_folders($_course, $to_group_id = '0', $can_see_invisible = false)
  443. {
  444. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY, $_course['dbName']);
  445. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT, $_course['dbName']);
  446. if(empty($to_group_id)){$to_group_id = '0';} //avoid empty strings in $to_group_id
  447. if ($can_see_invisible)
  448. {
  449. $sql = "SELECT path
  450. FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
  451. WHERE docs.id = last.ref
  452. AND docs.filetype = 'folder'
  453. AND last.tool = '".TOOL_DOCUMENT."'
  454. AND last.to_group_id = ".$to_group_id."
  455. AND last.visibility <> 2";
  456. $result = api_sql_query($sql, __FILE__, __LINE__);
  457. if ($result && mysql_num_rows($result) != 0)
  458. {
  459. while ($row = mysql_fetch_assoc($result))
  460. {
  461. $document_folders[] = $row['path'];
  462. }
  463. sort($document_folders, SORT_ASC);
  464. //return results
  465. return $document_folders;
  466. }
  467. else
  468. {
  469. return false;
  470. }
  471. }
  472. //no invisible folders
  473. else
  474. {
  475. //get visible folders
  476. $visible_sql = "SELECT path
  477. FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
  478. WHERE docs.id = last.ref
  479. AND docs.filetype = 'folder'
  480. AND last.tool = '".TOOL_DOCUMENT."'
  481. AND last.to_group_id = ".$to_group_id."
  482. AND last.visibility = 1";
  483. $visibleresult = api_sql_query($visible_sql, __FILE__, __LINE__);
  484. while ($all_visible_folders = mysql_fetch_assoc($visibleresult))
  485. {
  486. $visiblefolders[] = $all_visible_folders['path'];
  487. //echo "visible folders: ".$all_visible_folders['path']."<br>";
  488. }
  489. //get invisible folders
  490. $invisible_sql = "SELECT path
  491. FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
  492. WHERE docs.id = last.ref
  493. AND docs.filetype = 'folder'
  494. AND last.tool = '".TOOL_DOCUMENT."'
  495. AND last.to_group_id = ".$to_group_id."
  496. AND last.visibility = 0";
  497. $invisibleresult = api_sql_query($invisible_sql, __FILE__, __LINE__);
  498. while ($invisible_folders = mysql_fetch_assoc($invisibleresult))
  499. {
  500. //get visible folders in the invisible ones -> they are invisible too
  501. //echo "invisible folders: ".$invisible_folders['path']."<br>";
  502. $folder_in_invisible_sql = "SELECT path
  503. FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
  504. WHERE docs.id = last.ref
  505. AND docs.path LIKE '".mysql_real_escape_string($invisible_folders['path'])."/%'
  506. AND docs.filetype = 'folder'
  507. AND last.tool = '".TOOL_DOCUMENT."'
  508. AND last.to_group_id = ".$to_group_id."
  509. AND last.visibility = 1";
  510. $folder_in_invisible_result = api_sql_query($folder_in_invisible_sql, __FILE__, __LINE__);
  511. while ($folders_in_invisible_folder = mysql_fetch_assoc($folder_in_invisible_result))
  512. {
  513. $invisiblefolders[] = $folders_in_invisible_folder['path'];
  514. //echo "<br>folders in invisible folders: ".$folders_in_invisible_folder['path']."<br><br><br>";
  515. }
  516. }
  517. //if both results are arrays -> //calculate the difference between the 2 arrays -> only visible folders are left :)
  518. if (is_array($visiblefolders) && is_array($invisiblefolders))
  519. {
  520. $document_folders = array_diff($visiblefolders, $invisiblefolders);
  521. sort($document_folders, SORT_ASC);
  522. return $document_folders;
  523. }
  524. //only visible folders found
  525. elseif (is_array($visiblefolders))
  526. {
  527. sort($visiblefolders, SORT_ASC);
  528. return $visiblefolders;
  529. }
  530. //no visible folders found
  531. else
  532. {
  533. return false;
  534. }
  535. }
  536. }
  537. /**
  538. * This deletes a document by changing visibility to 2, renaming it to filename_DELETED_#id
  539. * Files/folders that are inside a deleted folder get visibility 2
  540. *
  541. * @param array $_course
  542. * @param string $path, path stored in the database
  543. * @param string ,$base_work_dir, path to the documents folder
  544. * @return boolean true/false
  545. * @todo now only files/folders in a folder get visibility 2, we should rename them too.
  546. */
  547. function delete_document($_course, $path, $base_work_dir)
  548. {
  549. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  550. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  551. //first, delete the actual document...
  552. $document_id = DocumentManager :: get_document_id($_course, $path);
  553. $new_path = $path.'_DELETED_'.$document_id;
  554. if ($document_id)
  555. {
  556. if (get_setting('permanently_remove_deleted_files') == 'true') //deleted files are *really* deleted
  557. {
  558. $what_to_delete_sql = "SELECT id FROM ".$TABLE_DOCUMENT." WHERE path='".$path."' OR path LIKE '".$path."/%'";
  559. //get all id's of documents that are deleted
  560. $what_to_delete_result = api_sql_query($what_to_delete_sql, __FILE__, __LINE__);
  561. if ($what_to_delete_result && mysql_num_rows($what_to_delete_result) != 0)
  562. {
  563. //needed to deleted medadata
  564. require_once (api_get_path(INCLUDE_PATH)."../metadata/md_funcs.php");
  565. $mdStore = new mdstore(TRUE);
  566. //delete all item_property entries
  567. while ($row = mysql_fetch_array($what_to_delete_result))
  568. {
  569. //query to delete from item_property table
  570. $remove_from_item_property_sql = "DELETE FROM ".$TABLE_ITEMPROPERTY." WHERE ref = ".$row['id']." AND tool='".TOOL_DOCUMENT."'";
  571. //query to delete from document table
  572. $remove_from_document_sql = "DELETE FROM ".$TABLE_DOCUMENT." WHERE id = ".$row['id']."";
  573. //echo($remove_from_item_property_sql.'<br>');
  574. api_sql_query($remove_from_item_property_sql, __FILE__, __LINE__);
  575. //echo($remove_from_document_sql.'<br>');
  576. api_sql_query($remove_from_document_sql, __FILE__, __LINE__);
  577. //delete metadata
  578. $eid = 'Document'.'.'.$row['id'];
  579. $mdStore->mds_delete($eid);
  580. $mdStore->mds_delete_offspring($eid);
  581. }
  582. //delete documents, do it like this so metadata get's deleted too
  583. //update_db_info('delete', $path);
  584. //throw it away
  585. my_delete($base_work_dir.$path);
  586. return true;
  587. }
  588. else
  589. {
  590. return false;
  591. }
  592. }
  593. else //set visibility to 2 and rename file/folder to qsdqsd_DELETED_#id
  594. {
  595. if (api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'delete', $user_id))
  596. {
  597. //echo('item_property_update OK');
  598. if (rename($base_work_dir.$path, $base_work_dir.$new_path))
  599. {
  600. //echo('rename OK');
  601. $sql = "UPDATE $TABLE_DOCUMENT set path='".$new_path."' WHERE id='".$document_id."'";
  602. if (api_sql_query($sql, __FILE__, __LINE__))
  603. {
  604. //if it is a folder it can contain files
  605. $sql = "SELECT id,path FROM ".$TABLE_DOCUMENT." WHERE path LIKE '".$path."/%'";
  606. $result = api_sql_query($sql, __FILE__, __LINE__);
  607. if ($result && mysql_num_rows($result) > 0)
  608. {
  609. while ($deleted_items = mysql_fetch_assoc($result))
  610. {
  611. //echo('to delete also: id '.$deleted_items['id']);
  612. api_item_property_update($_course, TOOL_DOCUMENT, $deleted_items['id'], 'delete', $user_id);
  613. //Change path of subfolders and documents in database
  614. $old_item_path = $deleted_items['path'];
  615. $new_item_path = $new_path.substr($old_item_path, strlen($path));
  616. $sql = "UPDATE $TABLE_DOCUMENT set path = '".$new_item_path."' WHERE id = ".$deleted_items['id'];
  617. api_sql_query($sql, __FILE__, __LINE__);
  618. }
  619. }
  620. //echo('dbase update OK');
  621. return true;
  622. }
  623. }
  624. }
  625. else
  626. {
  627. return false;
  628. }
  629. }
  630. }
  631. }
  632. /**
  633. * Gets the id of a document with a given path
  634. *
  635. * @param array $_course
  636. * @param string $path
  637. * @return int id of document / false if no doc found
  638. */
  639. function get_document_id($_course, $path)
  640. {
  641. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT, $_course['dbName']);
  642. $sql = "SELECT id FROM $TABLE_DOCUMENT WHERE path = '$path'";
  643. $result = api_sql_query($sql, __FILE__, __LINE__);
  644. if ($result && mysql_num_rows($result) == 1)
  645. {
  646. $row = mysql_fetch_row($result);
  647. return $row[0];
  648. }
  649. else
  650. {
  651. return false;
  652. }
  653. }
  654. }
  655. //end class DocumentManager
  656. ?>