document.lib.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. * This function streams a string to the client for download.
  372. * You have to ensure that the calling script then stops processing (exit();)
  373. * otherwise it may cause subsequent use of the page to want to download
  374. * other pages in php rather than interpreting them.
  375. *
  376. * @param string The string contents
  377. * @param boolean Whether "save" mode is forced (or opening directly authorized)
  378. * @param string The name of the file in the end (including extension)
  379. * @return false if file doesn't exist, true if stream succeeded
  380. */
  381. function string_send_for_download($full_string, $forced = false, $name = '')
  382. {
  383. $filename = $name;
  384. $len = strlen($full_string);
  385. if ($forced)
  386. {
  387. //force the browser to save the file instead of opening it
  388. header('Content-type: application/octet-stream');
  389. //header('Content-Type: application/force-download');
  390. header('Content-length: '.$len);
  391. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT']))
  392. {
  393. header('Content-Disposition: filename= '.$filename);
  394. }
  395. else
  396. {
  397. header('Content-Disposition: attachment; filename= '.$filename);
  398. }
  399. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
  400. {
  401. header('Pragma: ');
  402. header('Cache-Control: ');
  403. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  404. }
  405. header('Content-Description: '.$filename);
  406. header('Content-transfer-encoding: binary');
  407. //$fp = fopen($full_string, 'r');
  408. //fpassthru($fp);
  409. echo $full_string;
  410. return true;
  411. //You have to ensure that the calling script then stops processing (exit();)
  412. //otherwise it may cause subsequent use of the page to want to download
  413. //other pages in php rather than interpreting them.
  414. }
  415. else
  416. {
  417. //no forced download, just let the browser decide what to do according to the mimetype
  418. $content_type = DocumentManager :: file_get_mime_type($filename);
  419. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  420. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  421. header('Cache-Control: no-cache, must-revalidate');
  422. header('Pragma: no-cache');
  423. header('Content-type: '.$content_type);
  424. header('Content-Length: '.$len);
  425. $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  426. if (strpos($user_agent, 'msie'))
  427. {
  428. header('Content-Disposition: ; filename= '.$filename);
  429. }
  430. else
  431. {
  432. header('Content-Disposition: inline; filename= '.$filename);
  433. }
  434. echo($full_string);
  435. //You have to ensure that the calling script then stops processing (exit();)
  436. //otherwise it may cause subsequent use of the page to want to download
  437. //other pages in php rather than interpreting them.
  438. return true;
  439. }
  440. }
  441. /**
  442. * Fetches all document data for the given user/group
  443. *
  444. * @param array $_course
  445. * @param string $path
  446. * @param int $to_group_id
  447. * @param int $to_user_id
  448. * @param boolean $can_see_invisible
  449. * @return array with all document data
  450. */
  451. function get_all_document_data($_course, $path = '/', $to_group_id = 0, $to_user_id = NULL, $can_see_invisible = false)
  452. {
  453. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY, $_course['dbName']);
  454. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT, $_course['dbName']);
  455. //if to_user_id = NULL -> change query (IS NULL)
  456. //$to_user_id = (is_null($to_user_id))?'IS NULL':'= '.$to_user_id;
  457. if (!is_null($to_user_id))
  458. {
  459. $to_field = 'last.to_user_id';
  460. $to_value = $to_user_id;
  461. }
  462. else
  463. {
  464. $to_field = 'last.to_group_id';
  465. $to_value = $to_group_id;
  466. }
  467. //escape underscores in the path so they don't act as a wildcard
  468. $path = str_replace('_', '\_', $path);
  469. //if they can't see invisible files, they can only see files with visibility 1
  470. $visibility_bit = ' = 1';
  471. //if they can see invisible files, only deleted files (visibility 2) are filtered out
  472. if ($can_see_invisible)
  473. {
  474. $visibility_bit = ' <> 2';
  475. }
  476. //the given path will not end with a slash, unless it's the root '/'
  477. //so no root -> add slash
  478. $added_slash = ($path == '/') ? '' : '/';
  479. $sql = "SELECT *
  480. FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
  481. WHERE docs.id = last.ref
  482. AND docs.path LIKE '".$path.$added_slash."%'
  483. AND docs.path NOT LIKE '".$path.$added_slash."%/%'
  484. AND last.tool = '".TOOL_DOCUMENT."'
  485. AND ".$to_field." = ".$to_value."
  486. AND last.visibility".$visibility_bit;
  487. //echo $sql;
  488. $result = mysql_query($sql);
  489. if ($result && mysql_num_rows($result) != 0)
  490. {
  491. while ($row = mysql_fetch_assoc($result))
  492. //while ($row = mysql_fetch_array($result,MYSQL_NUM))
  493. {
  494. if($row['filetype']=='file' && pathinfo($row['path'],PATHINFO_EXTENSION)=='html'){
  495. //Templates management
  496. $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  497. $sql_is_template = 'SELECT id FROM '.$table_template.'
  498. WHERE course_code="'.$_course['id'].'"
  499. AND user_id="'.api_get_user_id().'"
  500. AND ref_doc="'.$row['id'].'"
  501. ';
  502. $template_result = api_sql_query($sql_is_template);
  503. if(mysql_numrows($template_result)>0){
  504. $row['is_template'] = 1;
  505. }
  506. else{
  507. $row['is_template'] = 0;
  508. }
  509. }
  510. $document_data[$row['id']] = $row;
  511. //$document_data[] = $row;
  512. }
  513. return $document_data;
  514. }
  515. else
  516. {
  517. //display_error("Error getting document info from database (".mysql_error().")!");
  518. return false;
  519. }
  520. }
  521. /**
  522. * Gets the paths of all folders in a course
  523. * can show all folders (exept for the deleted ones) or only visible ones
  524. * @param array $_course
  525. * @param boolean $can_see_invisible
  526. * @param int $to_group_id
  527. * @return array with paths
  528. */
  529. function get_all_document_folders($_course, $to_group_id = '0', $can_see_invisible = false)
  530. {
  531. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY, $_course['dbName']);
  532. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT, $_course['dbName']);
  533. if(empty($to_group_id)){$to_group_id = '0';} //avoid empty strings in $to_group_id
  534. if ($can_see_invisible)
  535. {
  536. $sql = "SELECT path
  537. FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
  538. WHERE docs.id = last.ref
  539. AND docs.filetype = 'folder'
  540. AND last.tool = '".TOOL_DOCUMENT."'
  541. AND last.to_group_id = ".$to_group_id."
  542. AND last.visibility <> 2";
  543. $result = api_sql_query($sql, __FILE__, __LINE__);
  544. if ($result && mysql_num_rows($result) != 0)
  545. {
  546. while ($row = mysql_fetch_assoc($result))
  547. {
  548. $document_folders[] = $row['path'];
  549. }
  550. sort($document_folders, SORT_ASC);
  551. //return results
  552. return $document_folders;
  553. }
  554. else
  555. {
  556. return false;
  557. }
  558. }
  559. //no invisible folders
  560. else
  561. {
  562. //get visible folders
  563. $visible_sql = "SELECT path
  564. FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
  565. WHERE docs.id = last.ref
  566. AND docs.filetype = 'folder'
  567. AND last.tool = '".TOOL_DOCUMENT."'
  568. AND last.to_group_id = ".$to_group_id."
  569. AND last.visibility = 1";
  570. $visibleresult = api_sql_query($visible_sql, __FILE__, __LINE__);
  571. while ($all_visible_folders = mysql_fetch_assoc($visibleresult))
  572. {
  573. $visiblefolders[] = $all_visible_folders['path'];
  574. //echo "visible folders: ".$all_visible_folders['path']."<br>";
  575. }
  576. //get invisible folders
  577. $invisible_sql = "SELECT path
  578. FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
  579. WHERE docs.id = last.ref
  580. AND docs.filetype = 'folder'
  581. AND last.tool = '".TOOL_DOCUMENT."'
  582. AND last.to_group_id = ".$to_group_id."
  583. AND last.visibility = 0";
  584. $invisibleresult = api_sql_query($invisible_sql, __FILE__, __LINE__);
  585. while ($invisible_folders = mysql_fetch_assoc($invisibleresult))
  586. {
  587. //get visible folders in the invisible ones -> they are invisible too
  588. //echo "invisible folders: ".$invisible_folders['path']."<br>";
  589. $folder_in_invisible_sql = "SELECT path
  590. FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
  591. WHERE docs.id = last.ref
  592. AND docs.path LIKE '".mysql_real_escape_string($invisible_folders['path'])."/%'
  593. AND docs.filetype = 'folder'
  594. AND last.tool = '".TOOL_DOCUMENT."'
  595. AND last.to_group_id = ".$to_group_id."
  596. AND last.visibility = 1";
  597. $folder_in_invisible_result = api_sql_query($folder_in_invisible_sql, __FILE__, __LINE__);
  598. while ($folders_in_invisible_folder = mysql_fetch_assoc($folder_in_invisible_result))
  599. {
  600. $invisiblefolders[] = $folders_in_invisible_folder['path'];
  601. //echo "<br>folders in invisible folders: ".$folders_in_invisible_folder['path']."<br><br><br>";
  602. }
  603. }
  604. //if both results are arrays -> //calculate the difference between the 2 arrays -> only visible folders are left :)
  605. if (is_array($visiblefolders) && is_array($invisiblefolders))
  606. {
  607. $document_folders = array_diff($visiblefolders, $invisiblefolders);
  608. sort($document_folders, SORT_ASC);
  609. return $document_folders;
  610. }
  611. //only visible folders found
  612. elseif (is_array($visiblefolders))
  613. {
  614. sort($visiblefolders, SORT_ASC);
  615. return $visiblefolders;
  616. }
  617. //no visible folders found
  618. else
  619. {
  620. return false;
  621. }
  622. }
  623. }
  624. /**
  625. * This deletes a document by changing visibility to 2, renaming it to filename_DELETED_#id
  626. * Files/folders that are inside a deleted folder get visibility 2
  627. *
  628. * @param array $_course
  629. * @param string $path, path stored in the database
  630. * @param string ,$base_work_dir, path to the documents folder
  631. * @return boolean true/false
  632. * @todo now only files/folders in a folder get visibility 2, we should rename them too.
  633. */
  634. function delete_document($_course, $path, $base_work_dir)
  635. {
  636. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT, $_course['dbName']);
  637. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY, $_course['dbName']);
  638. //first, delete the actual document...
  639. $document_id = DocumentManager :: get_document_id($_course, $path);
  640. $new_path = $path.'_DELETED_'.$document_id;
  641. if ($document_id)
  642. {
  643. if (get_setting('permanently_remove_deleted_files') == 'true') //deleted files are *really* deleted
  644. {
  645. $what_to_delete_sql = "SELECT id FROM ".$TABLE_DOCUMENT." WHERE path='".$path."' OR path LIKE '".$path."/%'";
  646. //get all id's of documents that are deleted
  647. $what_to_delete_result = api_sql_query($what_to_delete_sql, __FILE__, __LINE__);
  648. if ($what_to_delete_result && mysql_num_rows($what_to_delete_result) != 0)
  649. {
  650. //needed to deleted medadata
  651. require_once (api_get_path(SYS_CODE_PATH).'metadata/md_funcs.php');
  652. require_once(api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  653. $mdStore = new mdstore(TRUE);
  654. //delete all item_property entries
  655. while ($row = mysql_fetch_array($what_to_delete_result))
  656. {
  657. //query to delete from item_property table
  658. $remove_from_item_property_sql = "DELETE FROM ".$TABLE_ITEMPROPERTY." WHERE ref = ".$row['id']." AND tool='".TOOL_DOCUMENT."'";
  659. //query to delete from document table
  660. $remove_from_document_sql = "DELETE FROM ".$TABLE_DOCUMENT." WHERE id = ".$row['id']."";
  661. //echo($remove_from_item_property_sql.'<br>');
  662. api_sql_query($remove_from_item_property_sql, __FILE__, __LINE__);
  663. //echo($remove_from_document_sql.'<br>');
  664. api_sql_query($remove_from_document_sql, __FILE__, __LINE__);
  665. //delete metadata
  666. $eid = 'Document'.'.'.$row['id'];
  667. $mdStore->mds_delete($eid);
  668. $mdStore->mds_delete_offspring($eid);
  669. }
  670. //delete documents, do it like this so metadata get's deleted too
  671. //update_db_info('delete', $path);
  672. //throw it away
  673. my_delete($base_work_dir.$path);
  674. return true;
  675. }
  676. else
  677. {
  678. return false;
  679. }
  680. }
  681. else //set visibility to 2 and rename file/folder to qsdqsd_DELETED_#id
  682. {
  683. if (api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'delete', $user_id))
  684. {
  685. //echo('item_property_update OK');
  686. if (rename($base_work_dir.$path, $base_work_dir.$new_path))
  687. {
  688. //echo('rename OK');
  689. $sql = "UPDATE $TABLE_DOCUMENT set path='".$new_path."' WHERE id='".$document_id."'";
  690. if (api_sql_query($sql, __FILE__, __LINE__))
  691. {
  692. //if it is a folder it can contain files
  693. $sql = "SELECT id,path FROM ".$TABLE_DOCUMENT." WHERE path LIKE '".$path."/%'";
  694. $result = api_sql_query($sql, __FILE__, __LINE__);
  695. if ($result && mysql_num_rows($result) > 0)
  696. {
  697. while ($deleted_items = mysql_fetch_assoc($result))
  698. {
  699. //echo('to delete also: id '.$deleted_items['id']);
  700. api_item_property_update($_course, TOOL_DOCUMENT, $deleted_items['id'], 'delete', $user_id);
  701. //Change path of subfolders and documents in database
  702. $old_item_path = $deleted_items['path'];
  703. $new_item_path = $new_path.substr($old_item_path, strlen($path));
  704. $sql = "UPDATE $TABLE_DOCUMENT set path = '".$new_item_path."' WHERE id = ".$deleted_items['id'];
  705. api_sql_query($sql, __FILE__, __LINE__);
  706. }
  707. }
  708. //echo('dbase update OK');
  709. return true;
  710. }
  711. }
  712. }
  713. else
  714. {
  715. return false;
  716. }
  717. }
  718. }
  719. }
  720. /**
  721. * Gets the id of a document with a given path
  722. *
  723. * @param array $_course
  724. * @param string $path
  725. * @return int id of document / false if no doc found
  726. */
  727. function get_document_id($_course, $path)
  728. {
  729. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT, $_course['dbName']);
  730. $sql = "SELECT id FROM $TABLE_DOCUMENT WHERE path = '$path'";
  731. $result = api_sql_query($sql, __FILE__, __LINE__);
  732. if ($result && mysql_num_rows($result) == 1)
  733. {
  734. $row = mysql_fetch_row($result);
  735. return $row[0];
  736. }
  737. else
  738. {
  739. return false;
  740. }
  741. }
  742. /**
  743. * Allow to set a specific document as a new template for FCKEditor for a particular user in a particular course
  744. *
  745. * @param string $title
  746. * @param string $description
  747. * @param int $document_id_for_template the document id
  748. * @param string $couse_code
  749. * @param int $user_id
  750. */
  751. function set_document_as_template($title, $description, $document_id_for_template, $couse_code, $user_id){
  752. $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  753. $title = Database::escape_string($title);
  754. $description = Database::escape_string($description);
  755. $document_id_for_template = Database::escape_string($document_id_for_template);
  756. $couse_code = Database::escape_string($couse_code);
  757. $user_id = Database::escape_string($user_id);
  758. $sql = 'INSERT INTO '.$table_template.'(title, description, course_code, user_id, ref_doc)
  759. VALUES ("'.$title.'", "'.$description.'", "'.$couse_code.'", "'.$user_id.'", "'.$document_id_for_template.'")';
  760. api_sql_query($sql);
  761. }
  762. /**
  763. * Unset a document as template
  764. *
  765. * @param int $document_id
  766. * @param string $couse_code
  767. * @param int $user_id
  768. */
  769. function unset_document_as_template($document_id, $couse_code, $user_id){
  770. $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  771. $sql = 'SELECT id FROM '.$table_template.' WHERE course_code="'.$couse_code.'" AND user_id="'.$user_id.'" AND ref_doc="'.$document_id.'"';
  772. $result = api_sql_query($sql);
  773. $template_id = mysql_result($result,0,0);
  774. include_once(api_get_path(LIBRARY_PATH) . 'fileManage.lib.php');
  775. my_delete(api_get_path(SYS_CODE_PATH).'upload/template_thumbnails/'.$template_id.'.jpg');
  776. $sql = 'DELETE FROM '.$table_template.' WHERE course_code="'.$couse_code.'" AND user_id="'.$user_id.'" AND ref_doc="'.$document_id.'"';
  777. api_sql_query($sql);
  778. }
  779. }
  780. //end class DocumentManager
  781. ?>