fileManager.lib.php 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is the file manage library for Chamilo.
  5. * Include/require it in your code to use its functionality.
  6. * @package chamilo.library
  7. */
  8. /**
  9. This class contains functions that you can access statically.
  10. FileManager::list_all_directories($path)
  11. FileManager::list_all_files($dir_array)
  12. FileManager::compat_load_file($file_name)
  13. FileManager::set_default_settings($upload_path, $filename, $filetype="file", $glued_table, $default_visibility='v')
  14. @author Roan Embrechts
  15. @version 1.1, July 2004
  16. * @package chamilo.library
  17. */
  18. use Symfony\Component\Finder\Finder;
  19. use Symfony\Component\Filesystem\Filesystem;
  20. class FileManager
  21. {
  22. /**
  23. * Update the file or directory path in the document db document table
  24. *
  25. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  26. * @param - action (string) - action type require : 'delete' or 'update'
  27. * @param - old_path (string) - old path info stored to change
  28. * @param - new_path (string) - new path info to substitute
  29. * @desc Update the file or directory path in the document db document table
  30. *
  31. */
  32. static function update_db_info($action, $old_path, $new_path = '')
  33. {
  34. $dbTable = Database::get_course_table(TABLE_DOCUMENT);
  35. $course_id = api_get_course_int_id();
  36. /* DELETE */
  37. if ($action == 'delete') {
  38. $old_path = Database::escape_string($old_path);
  39. $to_delete = "WHERE c_id = $course_id AND path LIKE BINARY '".$old_path."' OR path LIKE BINARY '".$old_path."/%'";
  40. $query = "DELETE FROM $dbTable ".$to_delete;
  41. $result = Database::query("SELECT id FROM $dbTable ".$to_delete);
  42. if (Database::num_rows($result)) {
  43. require_once api_get_path(INCLUDE_PATH).'../metadata/md_funcs.php';
  44. $mdStore = new mdstore(true); // create if needed
  45. $md_type = (substr($dbTable, -13) == 'scormdocument') ? 'Scorm' : 'Document';
  46. while ($row = Database::fetch_array($result)) {
  47. $eid = $md_type.'.'.$row['id'];
  48. $mdStore->mds_delete($eid);
  49. $mdStore->mds_delete_offspring($eid);
  50. }
  51. }
  52. }
  53. /* UPDATE */
  54. if ($action == 'update') {
  55. if ($new_path[0] == '.') {
  56. $new_path = substr($new_path, 1);
  57. }
  58. $new_path = str_replace('//', '/', $new_path);
  59. // Attempt to update - tested & working for root dir
  60. $new_path = Database::escape_string($new_path);
  61. $query = "UPDATE $dbTable
  62. SET path = CONCAT('".$new_path."', SUBSTRING(path, LENGTH('".$old_path."')+1) )
  63. WHERE c_id = $course_id AND path LIKE BINARY '".$old_path."' OR path LIKE BINARY '".$old_path."/%'";
  64. }
  65. Database::query($query);
  66. }
  67. /**
  68. * Cheks a file or a directory actually exist at this location
  69. *
  70. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  71. * @param - file_path (string) - path of the presume existing file or dir
  72. * @return - boolean TRUE if the file or the directory exists
  73. * boolean FALSE otherwise.
  74. */
  75. static function check_name_exist($file_path)
  76. {
  77. clearstatcache();
  78. $save_dir = getcwd();
  79. if (!is_dir(dirname($file_path))) {
  80. return false;
  81. }
  82. chdir(dirname($file_path));
  83. $file_name = basename($file_path);
  84. if (file_exists($file_name)) {
  85. chdir($save_dir);
  86. return true;
  87. } else {
  88. chdir($save_dir);
  89. return false;
  90. }
  91. }
  92. /**
  93. * Deletes a file or a directory
  94. *
  95. * @author - Hugues Peeters
  96. * @param - $file (String) - the path of file or directory to delete
  97. * @return - bolean - true if the delete succeed
  98. * bolean - false otherwise.
  99. * @see - delete() uses check_name_exist() and removeDir() functions
  100. */
  101. static function my_delete($file)
  102. {
  103. if (self::check_name_exist($file)) {
  104. if (is_file($file)) { // FILE CASE
  105. unlink($file);
  106. return true;
  107. } elseif (is_dir($file)) { // DIRECTORY CASE
  108. self::removeDir($file);
  109. return true;
  110. }
  111. } else {
  112. return false; // no file or directory to delete
  113. }
  114. }
  115. /**
  116. * Removes a directory recursively
  117. *
  118. * @returns true if OK, otherwise false
  119. *
  120. * @author Amary <MasterNES@aol.com> (from Nexen.net)
  121. * @author Olivier Brouckaert <oli.brouckaert@skynet.be>
  122. *
  123. * @param string $dir directory to remove
  124. */
  125. static function removeDir($dir)
  126. {
  127. if (!@$opendir = opendir($dir)) {
  128. return false;
  129. }
  130. while ($readdir = readdir($opendir)) {
  131. if ($readdir != '..' && $readdir != '.') {
  132. if (is_file($dir.'/'.$readdir)) {
  133. if (!@unlink($dir.'/'.$readdir)) {
  134. return false;
  135. }
  136. } elseif (is_dir($dir.'/'.$readdir)) {
  137. if (!self::removeDir($dir.'/'.$readdir)) {
  138. return false;
  139. }
  140. }
  141. }
  142. }
  143. closedir($opendir);
  144. if (!@rmdir($dir)) {
  145. return false;
  146. }
  147. return true;
  148. }
  149. /**
  150. * Return true if folder is empty
  151. * @author : hubert.borderiou@grenet.fr
  152. * @param string $in_folder : folder path on disk
  153. * @return 1 if folder is empty, 0 otherwise
  154. */
  155. static function folder_is_empty($in_folder)
  156. {
  157. $tab_folder_content = scandir($in_folder);
  158. $folder_is_empty = 0;
  159. if ((count($tab_folder_content) == 2 && in_array(".", $tab_folder_content) && in_array(
  160. "..",
  161. $tab_folder_content
  162. )) || (count($tab_folder_content) < 2)
  163. ) {
  164. $folder_is_empty = 1;
  165. }
  166. return $folder_is_empty;
  167. }
  168. /**
  169. * Renames a file or a directory
  170. *
  171. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  172. * @param - $file_path (string) - complete path of the file or the directory
  173. * @param - $new_file_name (string) - new name for the file or the directory
  174. * @return - boolean - true if succeed
  175. * - boolean - false otherwise
  176. * @see - rename() uses the check_name_exist() and php2phps() functions
  177. */
  178. static function my_rename($file_path, $new_file_name)
  179. {
  180. $save_dir = getcwd();
  181. $path = dirname($file_path);
  182. $old_file_name = basename($file_path);
  183. $new_file_name = api_replace_dangerous_char($new_file_name);
  184. // If no extension, take the old one
  185. if ((strpos($new_file_name, '.') === false) && ($dotpos = strrpos($old_file_name, '.'))) {
  186. $new_file_name .= substr($old_file_name, $dotpos);
  187. }
  188. // Note: still possible: 'xx.yy' -rename-> '.yy' -rename-> 'zz'
  189. // This is useful for folder names, where otherwise '.' would be sticky
  190. // Extension PHP is not allowed, change to PHPS
  191. $new_file_name = self::php2phps($new_file_name);
  192. if ($new_file_name == $old_file_name) {
  193. return $old_file_name;
  194. }
  195. if (strtolower($new_file_name) != strtolower($old_file_name) && self::check_name_exist(
  196. $path.'/'.$new_file_name
  197. )
  198. ) {
  199. return false;
  200. }
  201. // On a Windows server, it would be better not to do the above check
  202. // because it succeeds for some new names resembling the old name.
  203. // But on Unix/Linux the check must be done because rename overwrites.
  204. chdir($path);
  205. $res = rename($old_file_name, $new_file_name) ? $new_file_name : false;
  206. chdir($save_dir);
  207. return $res;
  208. }
  209. /**
  210. * Moves a file or a directory to an other area
  211. *
  212. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  213. * @param - $source (String) - the path of file or directory to move
  214. * @param - $target (String) - the path of the new area
  215. * @return - bolean - true if the move succeed
  216. * bolean - false otherwise.
  217. * @see - move() uses check_name_exist() and copyDirTo() functions
  218. */
  219. static function move($source, $target)
  220. {
  221. if (self::check_name_exist($source)) {
  222. $file_name = basename($source);
  223. if (self::check_name_exist($target.'/'.$file_name)) {
  224. return false;
  225. } else {
  226. /* File case */
  227. if (is_file($source)) {
  228. copy($source, $target.'/'.$file_name);
  229. unlink($source);
  230. return true;
  231. } elseif (is_dir($source)) {
  232. /* Directory case */
  233. // Check to not copy the directory inside itself
  234. if (ereg('^'.$source.'/', $target.'/')) { // TODO: ereg() function is deprecated in PHP 5.3
  235. return false;
  236. } else {
  237. self::copyDirTo($source, $target);
  238. return true;
  239. }
  240. }
  241. }
  242. } else {
  243. return false;
  244. }
  245. }
  246. /**
  247. * Moves a directory and its content to an other area
  248. *
  249. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  250. * @param - $orig_dir_path (string) - the path of the directory to move
  251. * @param - $destination (string) - the path of the new area
  252. * @return - no return
  253. */
  254. static function copyDirTo($orig_dir_path, $destination, $move = true)
  255. {
  256. $save_dir = getcwd();
  257. // Extract directory name - create it at destination - update destination trail
  258. $dir_name = basename($orig_dir_path);
  259. if (is_dir($orig_dir_path)) {
  260. mkdir($destination.'/'.$dir_name, api_get_permissions_for_new_directories());
  261. $destination_trail = $destination.'/'.$dir_name;
  262. if (is_dir($destination)) {
  263. chdir($orig_dir_path);
  264. $handle = opendir($orig_dir_path);
  265. while ($element = readdir($handle)) {
  266. if ($element == '.' || $element == '..') {
  267. continue; // Skip the current and parent directories
  268. } elseif (is_file($element)) {
  269. copy($element, $destination_trail.'/'.$element);
  270. if ($move) {
  271. unlink($element);
  272. }
  273. } elseif (is_dir($element)) {
  274. $dir_to_copy[] = $orig_dir_path.'/'.$element;
  275. }
  276. }
  277. closedir($handle);
  278. if (sizeof($dir_to_copy) > 0) {
  279. foreach ($dir_to_copy as $this_dir) {
  280. self::copyDirTo($this_dir, $destination_trail, $move); // Recursivity
  281. }
  282. }
  283. if ($move) {
  284. rmdir($orig_dir_path);
  285. }
  286. chdir($save_dir);
  287. }
  288. }
  289. }
  290. /* NOTE: These functions batch is used to automatically build HTML forms
  291. * with a list of the directories contained on the course Directory.
  292. *
  293. * From a thechnical point of view, form_dir_lists calls sort_dir wich calls index_dir
  294. */
  295. /**
  296. * Gets all the directories and subdirectories
  297. * contented in a given directory
  298. *
  299. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  300. * @param - path (string) - directory path of the one to index
  301. * @return - an array containing the path of all the subdirectories
  302. */
  303. static function index_dir($path)
  304. {
  305. $dir_array = array();
  306. $save_dir = getcwd();
  307. if (is_dir($path)) {
  308. chdir($path);
  309. $handle = opendir($path);
  310. // Reads directory content end record subdirectoies names in $dir_array
  311. if ($handle !== false) {
  312. while ($element = readdir($handle)) {
  313. if ($element == '.' || $element == '..') {
  314. continue;
  315. } // Skip the current and parent directories
  316. if (is_dir($element)) {
  317. $dir_array[] = $path.'/'.$element;
  318. }
  319. }
  320. closedir($handle);
  321. }
  322. // Recursive operation if subdirectories exist
  323. $dir_number = sizeof($dir_array);
  324. if ($dir_number > 0) {
  325. for ($i = 0; $i < $dir_number; $i++) {
  326. $sub_dir_array = self::index_dir($dir_array[$i]); // Function recursivity
  327. $dir_array = array_merge((array)$dir_array, (array)$sub_dir_array); // Data merge
  328. }
  329. }
  330. }
  331. chdir($save_dir);
  332. return $dir_array;
  333. }
  334. /**
  335. * Indexes all the directories and subdirectories
  336. * contented in a given directory, and sort them alphabetically
  337. *
  338. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  339. * @param - path (string) - directory path of the one to index
  340. * @return - an array containing the path of all the subdirectories sorted
  341. * false, if there is no directory
  342. * @see - index_and_sort_dir uses the index_dir() function
  343. */
  344. static function index_and_sort_dir($path)
  345. {
  346. $dir_list = self::index_dir($path);
  347. if ($dir_list) {
  348. natsort($dir_list);
  349. return $dir_list;
  350. }
  351. return false;
  352. }
  353. /**
  354. * Extracting extention of a filename
  355. *
  356. * @returns array
  357. * @param string $filename filename
  358. */
  359. static function getextension($filename)
  360. {
  361. $bouts = explode('.', $filename);
  362. return array(array_pop($bouts), implode('.', $bouts));
  363. }
  364. /**
  365. * Calculation size of a directory
  366. *
  367. * @returns integer size
  368. * @param string $path path to size
  369. * @param boolean $recursive if true , include subdir in total
  370. */
  371. static function dirsize($root, $recursive = true)
  372. {
  373. $dir = @opendir($root);
  374. $size = 0;
  375. while ($file = @readdir($dir)) {
  376. if (!in_array($file, array('.', '..'))) {
  377. if (is_dir($root.'/'.$file)) {
  378. $size += $recursive ? dirsize($root.'/'.$file) : 0;
  379. } else {
  380. $size += @filesize($root.'/'.$file);
  381. }
  382. }
  383. }
  384. @closedir($dir);
  385. return $size;
  386. }
  387. /**
  388. Returns a list of all directories, except the base dir,
  389. of the current course. This function uses recursion.
  390. Convention: the parameter $path does not end with a slash.
  391. @author Roan Embrechts
  392. @version 1.0.1
  393. */
  394. static function list_all_directories($path)
  395. {
  396. $result_array = array();
  397. if (is_dir($path)) {
  398. $save_dir = getcwd();
  399. chdir($path);
  400. $handle = opendir($path);
  401. while ($element = readdir($handle)) {
  402. if ($element == '.' || $element == '..') {
  403. continue;
  404. } // Skip the current and parent directories
  405. if (is_dir($element)) {
  406. $dir_array[] = $path.'/'.$element;
  407. }
  408. }
  409. closedir($handle);
  410. // Recursive operation if subdirectories exist
  411. $dir_number = sizeof($dir_array);
  412. if ($dir_number > 0) {
  413. for ($i = 0; $i < $dir_number; $i++) {
  414. $sub_dir_array = self::list_all_directories($dir_array[$i]); // Function recursivity
  415. if (is_array($dir_array) && is_array($sub_dir_array)) {
  416. $dir_array = array_merge($dir_array, $sub_dir_array); // Data merge
  417. }
  418. }
  419. }
  420. $result_array = $dir_array;
  421. chdir($save_dir);
  422. }
  423. return $result_array;
  424. }
  425. /**
  426. This function receives a list of directories.
  427. It returns a list of all files in these directories
  428. @author Roan Embrechts
  429. @version 1.0
  430. */
  431. function list_all_files($dir_array)
  432. {
  433. $element_array = array();
  434. if (is_dir($dir_array)) {
  435. $save_dir = getcwd();
  436. foreach ($dir_array as $directory) {
  437. chdir($directory);
  438. $handle = opendir($directory);
  439. while ($element = readdir($handle)) {
  440. if ($element == '.' || $element == '..' || $element == '.htaccess') {
  441. continue;
  442. } // Skip the current and parent directories
  443. if (!is_dir($element)) {
  444. $element_array[] = $directory.'/'.$element;
  445. }
  446. }
  447. closedir($handle);
  448. chdir('..');
  449. chdir($save_dir);
  450. }
  451. }
  452. return $element_array;
  453. }
  454. /**
  455. Loads contents of file $filename into memory and returns them as a string.
  456. Function kept for compatibility with older PHP versions.
  457. Function is binary safe (is needed on Windows)
  458. */
  459. static function compat_load_file($file_name)
  460. {
  461. $buffer = '';
  462. if (file_exists($file_name)) {
  463. $fp = fopen($file_name, 'rb');
  464. $buffer = fread($fp, filesize($file_name));
  465. fclose($fp);
  466. }
  467. return $buffer;
  468. }
  469. static function choose_image($file_name)
  470. {
  471. static $type, $image;
  472. /* TABLES INITIALISATION */
  473. if (!$type || !$image) {
  474. $type['word'] = array('doc', 'dot', 'rtf', 'mcw', 'wps', 'psw', 'docm', 'docx', 'dotm', 'dotx');
  475. $type['web'] = array('htm', 'html', 'htx', 'xml', 'xsl', 'php', 'xhtml');
  476. $type['image'] = array('gif', 'jpg', 'png', 'bmp', 'jpeg', 'tif', 'tiff');
  477. $type['image_vect'] = array('svg', 'svgz');
  478. $type['audio'] = array('wav', 'mid', 'mp2', 'mp3', 'midi', 'sib', 'amr', 'kar', 'oga', 'au', 'wma');
  479. $type['video'] = array(
  480. 'mp4',
  481. 'mov',
  482. 'rm',
  483. 'pls',
  484. 'mpg',
  485. 'mpeg',
  486. 'm2v',
  487. 'm4v',
  488. 'flv',
  489. 'f4v',
  490. 'avi',
  491. 'wmv',
  492. 'asf',
  493. '3gp',
  494. 'ogv',
  495. 'ogg',
  496. 'ogx',
  497. 'webm'
  498. );
  499. $type['excel'] = array('xls', 'xlt', 'xls', 'xlt', 'pxl', 'xlsx', 'xlsm', 'xlam', 'xlsb', 'xltm', 'xltx');
  500. $type['compressed'] = array('zip', 'tar', 'rar', 'gz');
  501. $type['code'] = array('js', 'cpp', 'c', 'java', 'phps', 'jsp', 'asp', 'aspx', 'cfm');
  502. $type['acrobat'] = array('pdf');
  503. $type['powerpoint'] = array('ppt', 'pps', 'pptm', 'pptx', 'potm', 'potx', 'ppam', 'ppsm', 'ppsx');
  504. $type['flash'] = array('fla', 'swf');
  505. $type['text'] = array('txt', 'log');
  506. $type['oo_writer'] = array('odt', 'ott', 'sxw', 'stw');
  507. $type['oo_calc'] = array('ods', 'ots', 'sxc', 'stc');
  508. $type['oo_impress'] = array('odp', 'otp', 'sxi', 'sti');
  509. $type['oo_draw'] = array('odg', 'otg', 'sxd', 'std');
  510. $type['epub'] = array('epub');
  511. $type['java'] = array('class', 'jar');
  512. $type['freemind'] = array('mm');
  513. $image['word'] = 'word.gif';
  514. $image['web'] = 'file_html.gif';
  515. $image['image'] = 'file_image.gif';
  516. $image['image_vect'] = 'file_svg.png';
  517. $image['audio'] = 'file_sound.gif';
  518. $image['video'] = 'film.gif';
  519. $image['excel'] = 'excel.gif';
  520. $image['compressed'] = 'file_zip.gif';
  521. $image['code'] = 'icons/22/mime_code.png';
  522. $image['acrobat'] = 'file_pdf.gif';
  523. $image['powerpoint'] = 'powerpoint.gif';
  524. $image['flash'] = 'file_flash.gif';
  525. $image['text'] = 'icons/22/mime_text.png';
  526. $image['oo_writer'] = 'file_oo_writer.gif';
  527. $image['oo_calc'] = 'file_oo_calc.gif';
  528. $image['oo_impress'] = 'file_oo_impress.gif';
  529. $image['oo_draw'] = 'file_oo_draw.gif';
  530. $image['epub'] = 'file_epub.gif';
  531. $image['java'] = 'file_java.png';
  532. $image['freemind'] = 'file_freemind.png';
  533. }
  534. /* FUNCTION CORE */
  535. $extension = array();
  536. if (!is_array($file_name)) {
  537. if (preg_match('/\.([[:alnum:]]+)(\?|$)/', $file_name, $extension)) {
  538. $extension[1] = strtolower($extension[1]);
  539. foreach ($type as $generic_type => $extension_list) {
  540. if (in_array($extension[1], $extension_list)) {
  541. return $image[$generic_type];
  542. }
  543. }
  544. }
  545. }
  546. return 'defaut.gif';
  547. }
  548. /**
  549. * Transform the file path to a URL.
  550. *
  551. * @param - $file_path (string) - Relative local path of the file on the hard disk
  552. * @return - Relative url
  553. */
  554. static function format_url($file_path)
  555. {
  556. $path_component = explode('/', $file_path);
  557. $path_component = array_map('rawurlencode', $path_component);
  558. return implode('/', $path_component);
  559. }
  560. /**
  561. * Calculates the total size of a directory by adding the sizes (that
  562. * are stored in the database) of all files & folders in this directory.
  563. *
  564. * @param string $path
  565. * @param boolean $can_see_invisible
  566. * @return Total size
  567. */
  568. static function get_total_folder_size($path, $can_see_invisible = false)
  569. {
  570. $table_itemproperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  571. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  572. $tool_document = TOOL_DOCUMENT;
  573. $course_id = api_get_course_int_id();
  574. $session_id = api_get_session_id();
  575. $session_condition = api_get_session_condition($session_id, true, true, 'props.id_session');
  576. $visibility_rule = ' props.visibility '.($can_see_invisible ? '<> 2' : '= 1');
  577. $sql = "SELECT SUM(table1.size) FROM (
  578. SELECT size FROM $table_itemproperty AS props, $table_document AS docs
  579. WHERE docs.c_id = $course_id AND
  580. docs.id = props.ref AND
  581. docs.path LIKE '$path/%' AND
  582. props.c_id = $course_id AND
  583. props.tool = '$tool_document' AND
  584. $visibility_rule
  585. $session_condition
  586. GROUP BY ref
  587. ) as table1";
  588. $result = Database::query($sql);
  589. if ($result && Database::num_rows($result) != 0) {
  590. $row = Database::fetch_row($result);
  591. return $row[0] == null ? 0 : $row[0];
  592. } else {
  593. return 0;
  594. }
  595. }
  596. /**
  597. * Changes the file name extension from .php to .phps
  598. * Useful for securing a site.
  599. *
  600. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  601. * @param - file_name (string) name of a file
  602. *
  603. * @return string the filename phps'ized
  604. */
  605. static function php2phps($file_name)
  606. {
  607. return preg_replace('/\.(php.?|phtml.?)(\.){0,1}.*$/i', '.phps', $file_name);
  608. }
  609. /**
  610. * Renames .htaccess & .HTACCESS to htaccess.txt
  611. *
  612. * @param string $filename
  613. * @return string
  614. */
  615. static function htaccess2txt($filename)
  616. {
  617. return str_replace(array('.htaccess', '.HTACCESS'), array('htaccess.txt', 'htaccess.txt'), $filename);
  618. }
  619. /**
  620. * This function executes our safety precautions
  621. * more functions can be added
  622. *
  623. * @param string $filename
  624. * @return string
  625. * @see FileManager::php2phps()
  626. * @see htaccess2txt()
  627. */
  628. static function disable_dangerous_file($filename)
  629. {
  630. return self::htaccess2txt(self::php2phps($filename));
  631. }
  632. /**
  633. * This function generates a unique name for a file on a given location. Filenames are changed to name_#.ext
  634. *
  635. * @param string $path
  636. * @param string $name
  637. * @return new unique name
  638. */
  639. static function unique_name($path, $name)
  640. {
  641. $ext = substr(strrchr($name, '.'), 0);
  642. $name_no_ext = substr($name, 0, strlen($name) - strlen(strstr($name, $ext)));
  643. $n = 0;
  644. $unique = '';
  645. while (file_exists($path.$name_no_ext.$unique.$ext)) {
  646. $unique = '_'.++$n;
  647. }
  648. return $name_no_ext.$unique.$ext;
  649. }
  650. /**
  651. * Returns the name without extension, used for the title
  652. *
  653. * @param string $name
  654. * @return name without the extension
  655. */
  656. static function get_document_title($name)
  657. {
  658. // If they upload .htaccess...
  659. $name = self::disable_dangerous_file($name);
  660. $ext = substr(strrchr($name, '.'), 0);
  661. return addslashes(substr($name, 0, strlen($name) - strlen(strstr($name, $ext))));
  662. }
  663. /**
  664. * This function checks if the upload succeeded
  665. *
  666. * @param array $uploaded_file ($_FILES)
  667. * @param bool $show_output
  668. *
  669. * @return true if upload succeeded
  670. */
  671. static function process_uploaded_file($uploaded_file, $show_output = true)
  672. {
  673. // Checking the error code sent with the file upload.
  674. switch ($uploaded_file['error']) {
  675. case 1:
  676. // The uploaded file exceeds the upload_max_filesize directive in php.ini.
  677. if ($show_output) {
  678. Display::display_error_message(
  679. get_lang('UplExceedMaxServerUpload').ini_get('upload_max_filesize')
  680. );
  681. }
  682. return false;
  683. case 2:
  684. // The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
  685. // Not used at the moment, but could be handy if we want to limit the size of an upload (e.g. image upload in html editor).
  686. $max_file_size = intval($_POST['MAX_FILE_SIZE']);
  687. if ($show_output) {
  688. Display::display_error_message(get_lang('UplExceedMaxPostSize').Text::format_file_size($max_file_size));
  689. }
  690. return false;
  691. case 3:
  692. // The uploaded file was only partially uploaded.
  693. if ($show_output) {
  694. Display::display_error_message(get_lang('UplPartialUpload').' '.get_lang('PleaseTryAgain'));
  695. }
  696. return false;
  697. case 4:
  698. // No file was uploaded.
  699. if ($show_output) {
  700. Display::display_error_message(get_lang('UplNoFileUploaded').' '.get_lang('UplSelectFileFirst'));
  701. }
  702. return false;
  703. }
  704. if (!file_exists($uploaded_file['tmp_name'])) {
  705. // No file was uploaded.
  706. if ($show_output) {
  707. Display::display_error_message(get_lang('UplUploadFailed'));
  708. }
  709. return false;
  710. }
  711. if (file_exists($uploaded_file['tmp_name'])) {
  712. $filesize = filesize($uploaded_file['tmp_name']);
  713. if (empty($filesize)) {
  714. // No file was uploaded.
  715. if ($show_output) {
  716. Display::display_error_message(get_lang('UplUploadFailedSizeIsZero'));
  717. }
  718. return false;
  719. }
  720. }
  721. $course_id = api_get_course_id();
  722. //Checking course quota if we are in a course
  723. if (!empty($course_id)) {
  724. $max_filled_space = DocumentManager::get_course_quota();
  725. // Check if there is enough space to save the file
  726. if (!DocumentManager::enough_space($uploaded_file['size'], $max_filled_space)) {
  727. if ($show_output) {
  728. Display::display_error_message(get_lang('UplNotEnoughSpace'));
  729. }
  730. return false;
  731. }
  732. }
  733. // case 0: default: We assume there is no error, the file uploaded with success.
  734. return true;
  735. }
  736. /**
  737. * This function does the save-work for the documents.
  738. * It handles the uploaded file and adds the properties to the database
  739. * If unzip=1 and the file is a zipfile, it is extracted
  740. * If we decide to save ALL kinds of documents in one database,
  741. * we could extend this with a $type='document', 'scormdocument',...
  742. *
  743. * @param array $_course
  744. * @param array $uploaded_file ($_FILES)
  745. * @param string $base_work_dir
  746. * @param string $upload_path
  747. * @param int $user_id
  748. * @param int $to_group_id, 0 for everybody
  749. * @param int $to_user_id, NULL for everybody
  750. * @param int $unzip 1/0
  751. * @param string $what_if_file_exists overwrite, rename or warn if exists (default)
  752. * @param boolean Optional output parameter. So far only use for unzip_uploaded_document function. If no output wanted on success, set to false.
  753. * @return path of the saved file
  754. */
  755. static function handle_uploaded_document(
  756. $_course,
  757. $uploaded_file,
  758. $base_work_dir,
  759. $upload_path,
  760. $user_id,
  761. $to_group_id = 0,
  762. $to_user_id = null,
  763. $unzip = 0,
  764. $what_if_file_exists = '',
  765. $output = true
  766. ) {
  767. if (!$user_id) {
  768. die('Not a valid user.');
  769. }
  770. // Strip slashes
  771. $uploaded_file['name'] = stripslashes($uploaded_file['name']);
  772. // Add extension to files without one (if possible)
  773. $uploaded_file['name'] = self::add_ext_on_mime($uploaded_file['name'], $uploaded_file['type']);
  774. $current_session_id = api_get_session_id();
  775. //Just in case process_uploaded_file is not called
  776. $max_filled_space = DocumentManager::get_course_quota();
  777. // Check if there is enough space to save the file
  778. if (!DocumentManager::enough_space($uploaded_file['size'], $max_filled_space)) {
  779. if ($output) {
  780. Display::display_error_message(get_lang('UplNotEnoughSpace'));
  781. }
  782. return false;
  783. }
  784. // If the want to unzip, check if the file has a .zip (or ZIP,Zip,ZiP,...) extension
  785. if ($unzip == 1 && preg_match('/.zip$/', strtolower($uploaded_file['name']))) {
  786. return self::unzip_uploaded_document(
  787. $uploaded_file,
  788. $upload_path,
  789. $base_work_dir,
  790. $max_filled_space,
  791. $output,
  792. $to_group_id
  793. );
  794. //display_message('Unzipping file');
  795. } elseif ($unzip == 1 && !preg_match(
  796. '/.zip$/',
  797. strtolower($uploaded_file['name'])
  798. )
  799. ) { // We can only unzip ZIP files (no gz, tar,...)
  800. if ($output) {
  801. Display::display_error_message(get_lang('UplNotAZip')." ".get_lang('PleaseTryAgain'));
  802. }
  803. return false;
  804. } else {
  805. // Clean up the name, only ASCII characters should stay. (and strict)
  806. $clean_name = api_replace_dangerous_char($uploaded_file['name'], 'strict');
  807. // No "dangerous" files
  808. $clean_name = self::disable_dangerous_file($clean_name);
  809. if (!self::filter_extension($clean_name)) {
  810. if ($output) {
  811. Display::display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  812. }
  813. return false;
  814. } else {
  815. // Extension is good
  816. //echo '<br />clean name = '.$clean_name;
  817. //echo '<br />upload_path = '.$upload_path;
  818. // If the upload path differs from / (= root) it will need a slash at the end
  819. if ($upload_path != '/') {
  820. $upload_path = $upload_path.'/';
  821. }
  822. //echo '<br />upload_path = '.$upload_path;
  823. $file_path = $upload_path.$clean_name;
  824. //echo '<br />file path = '.$file_path;
  825. // Full path to where we want to store the file with trailing slash
  826. $where_to_save = $base_work_dir.$upload_path;
  827. // At least if the directory doesn't exist, tell so
  828. if (!is_dir($where_to_save)) {
  829. if ($output) {
  830. Display::display_error_message(get_lang('DestDirectoryDoesntExist').' ('.$upload_path.')');
  831. }
  832. return false;
  833. }
  834. //echo '<br />where to save = '.$where_to_save;
  835. // Full path of the destination
  836. $store_path = $where_to_save.$clean_name;
  837. //echo '<br />store path = '.$store_path;
  838. // Name of the document without the extension (for the title)
  839. $document_name = self::get_document_title($uploaded_file['name']);
  840. // Size of the uploaded file (in bytes)
  841. $file_size = $uploaded_file['size'];
  842. $files_perm = api_get_permissions_for_new_files();
  843. // What to do if the target file exists
  844. switch ($what_if_file_exists) {
  845. // Overwrite the file if it exists
  846. case 'overwrite':
  847. // Check if the target file exists, so we can give another message
  848. $file_exists = file_exists($store_path);
  849. if (@move_uploaded_file($uploaded_file['tmp_name'], $store_path)) {
  850. chmod($store_path, $files_perm);
  851. if ($file_exists) {
  852. // UPDATE DATABASE
  853. $document_id = DocumentManager::get_document_id($_course, $file_path);
  854. if (is_numeric($document_id)) {
  855. // Update filesize
  856. self::update_existing_document($_course, $document_id, $uploaded_file['size']);
  857. // Update document item_property
  858. api_item_property_update(
  859. $_course,
  860. TOOL_DOCUMENT,
  861. $document_id,
  862. 'DocumentUpdated',
  863. $user_id,
  864. $to_group_id,
  865. $to_user_id,
  866. null,
  867. null,
  868. $current_session_id
  869. );
  870. //Redo visibility
  871. api_set_default_visibility($_course, TOOL_DOCUMENT, $document_id);
  872. }
  873. // If the file is in a folder, we need to update all parent folders
  874. self::item_property_update_on_folder($_course, $upload_path, $user_id);
  875. // Display success message with extra info to user
  876. if ($output) {
  877. Display::display_confirmation_message(
  878. get_lang('UplUploadSucceeded').'<br />'.$file_path.' '.get_lang(
  879. 'UplFileOverwritten'
  880. ),
  881. false
  882. );
  883. }
  884. return $file_path;
  885. } else {
  886. // Put the document data in the database
  887. $document_id = self::add_document(
  888. $_course,
  889. $file_path,
  890. 'file',
  891. $file_size,
  892. $document_name,
  893. null,
  894. 0,
  895. true
  896. );
  897. if ($document_id) {
  898. // Put the document in item_property update
  899. api_item_property_update(
  900. $_course,
  901. TOOL_DOCUMENT,
  902. $document_id,
  903. 'DocumentAdded',
  904. $user_id,
  905. $to_group_id,
  906. $to_user_id,
  907. null,
  908. null,
  909. $current_session_id
  910. );
  911. }
  912. // If the file is in a folder, we need to update all parent folders
  913. self::item_property_update_on_folder($_course, $upload_path, $user_id);
  914. // Display success message to user
  915. if ($output) {
  916. Display::display_confirmation_message(
  917. get_lang('UplUploadSucceeded').'<br />'.$file_path,
  918. false
  919. );
  920. }
  921. return $file_path;
  922. }
  923. } else {
  924. if ($output) {
  925. Display::display_error_message(get_lang('UplUnableToSaveFile'));
  926. }
  927. return false;
  928. }
  929. break;
  930. // Rename the file if it exists
  931. case 'rename':
  932. $new_name = unique_name($where_to_save, $clean_name);
  933. $store_path = $where_to_save.$new_name;
  934. $new_file_path = $upload_path.$new_name;
  935. if (@move_uploaded_file($uploaded_file['tmp_name'], $store_path)) {
  936. chmod($store_path, $files_perm);
  937. // Put the document data in the database
  938. $document_id = self::add_document(
  939. $_course,
  940. $new_file_path,
  941. 'file',
  942. $file_size,
  943. $document_name,
  944. null,
  945. 0,
  946. true
  947. );
  948. if ($document_id) {
  949. // Update document item_property
  950. api_item_property_update(
  951. $_course,
  952. TOOL_DOCUMENT,
  953. $document_id,
  954. 'DocumentAdded',
  955. $user_id,
  956. $to_group_id,
  957. $to_user_id,
  958. null,
  959. null,
  960. $current_session_id
  961. );
  962. }
  963. // If the file is in a folder, we need to update all parent folders
  964. self::item_property_update_on_folder($_course, $upload_path, $user_id);
  965. // Display success message to user
  966. if ($output) {
  967. Display::display_confirmation_message(
  968. get_lang('UplUploadSucceeded').'<br />'.get_lang('UplFileSavedAs').$new_file_path,
  969. false
  970. );
  971. }
  972. return $new_file_path;
  973. } else {
  974. if ($output) {
  975. Display::display_error_message(get_lang('UplUnableToSaveFile'));
  976. }
  977. return false;
  978. }
  979. break;
  980. // Only save the file if it doesn't exist or warn user if it does exist
  981. default:
  982. if (file_exists($store_path)) {
  983. if ($output) {
  984. Display::display_error_message($clean_name.' '.get_lang('UplAlreadyExists'));
  985. }
  986. } else {
  987. if (@move_uploaded_file($uploaded_file['tmp_name'], $store_path)) {
  988. chmod($store_path, $files_perm);
  989. // Put the document data in the database
  990. $document_id = self::add_document(
  991. $_course,
  992. $file_path,
  993. 'file',
  994. $file_size,
  995. $document_name,
  996. null,
  997. 0,
  998. true
  999. );
  1000. if ($document_id) {
  1001. // Update document item_property
  1002. api_item_property_update(
  1003. $_course,
  1004. TOOL_DOCUMENT,
  1005. $document_id,
  1006. 'DocumentAdded',
  1007. $user_id,
  1008. $to_group_id,
  1009. $to_user_id,
  1010. null,
  1011. null,
  1012. $current_session_id
  1013. );
  1014. }
  1015. // If the file is in a folder, we need to update all parent folders
  1016. self::item_property_update_on_folder($_course, $upload_path, $user_id);
  1017. // Display success message to user
  1018. if ($output) {
  1019. Display::display_confirmation_message(
  1020. get_lang('UplUploadSucceeded').'<br />'.$file_path,
  1021. false
  1022. );
  1023. }
  1024. return $file_path;
  1025. } else {
  1026. if ($output) {
  1027. Display::display_error_message(get_lang('UplUnableToSaveFile'));
  1028. }
  1029. return false;
  1030. }
  1031. }
  1032. break;
  1033. }
  1034. }
  1035. }
  1036. }
  1037. /**
  1038. * Checks if there is enough place to add a file on a directory
  1039. * on the base of a maximum directory size allowed
  1040. * deprecated: use enough_space instead!
  1041. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  1042. * @param - file_size (int) - size of the file in byte
  1043. * @param - dir (string) - Path of the directory
  1044. * whe the file should be added
  1045. * @param - max_dir_space (int) - maximum size of the diretory in byte
  1046. * @return - boolean true if there is enough space,
  1047. * boolean false otherwise
  1048. *
  1049. * @see - enough_size() uses dir_total_space() function
  1050. */
  1051. static function enough_size($file_size, $dir, $max_dir_space)
  1052. {
  1053. if ($max_dir_space) {
  1054. $already_filled_space = self::dir_total_space($dir);
  1055. if (($file_size + $already_filled_space) > $max_dir_space) {
  1056. return false;
  1057. }
  1058. }
  1059. return true;
  1060. }
  1061. /**
  1062. * Computes the size already occupied by a directory and is subdirectories
  1063. *
  1064. * @param string dir_path (string) - size of the file in byte
  1065. * @return int - return the directory size in bytes
  1066. */
  1067. static function dir_total_space($dir_path)
  1068. {
  1069. $fs = new Filesystem();
  1070. if (!empty($dir_path) && $fs->exists($dir_path)) {
  1071. $finder = new Finder();
  1072. $finder->files()->in($dir_path);
  1073. $sumSize = 0;
  1074. foreach ($finder as $file) {
  1075. // Print the absolute path
  1076. //var_dump($file->getRelativePathname());
  1077. $sumSize += $file->getSize();
  1078. }
  1079. return $sumSize;
  1080. }
  1081. return 0;
  1082. /*
  1083. // author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  1084. $save_dir = getcwd();
  1085. chdir($dir_path);
  1086. $handle = opendir($dir_path);
  1087. $sumSize = 0;
  1088. $dirList = array();
  1089. while ($element = readdir($handle)) {
  1090. if ($element == '.' || $element == '..') {
  1091. continue; // Skip the current and parent directories
  1092. }
  1093. if (is_file($element)) {
  1094. $sumSize += filesize($element);
  1095. }
  1096. if (is_dir($element)) {
  1097. $dirList[] = $dir_path.'/'.$element;
  1098. }
  1099. }
  1100. closedir($handle);
  1101. if (sizeof($dirList) > 0) {
  1102. foreach ($dirList as $j) {
  1103. $sizeDir = self::dir_total_space($j, 0); // Recursivity
  1104. $sumSize += $sizeDir;
  1105. }
  1106. }
  1107. chdir($save_dir); // Return to initial position
  1108. return $sumSize;*/
  1109. }
  1110. /**
  1111. * Tries to add an extension to files without extension
  1112. * Some applications on Macintosh computers don't add an extension to the files.
  1113. * This subroutine try to fix this on the basis of the MIME type sent
  1114. * by the browser.
  1115. *
  1116. * Note : some browsers don't send the MIME Type (e.g. Netscape 4).
  1117. * We don't have solution for this kind of situation
  1118. *
  1119. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  1120. * @author - Bert Vanderkimpen
  1121. * @param - file_name (string) - Name of the file
  1122. * @param - file_type (string) - Type of the file
  1123. * @return - file_name (string)
  1124. */
  1125. static function add_ext_on_mime($file_name, $file_type)
  1126. {
  1127. // Check whether the file has an extension AND whether the browser has sent a MIME Type
  1128. if (!preg_match('/^.*\.[a-zA-Z_0-9]+$/', $file_name) && $file_type) {
  1129. // Build a "MIME-types / extensions" connection table
  1130. static $mime_type = array();
  1131. $mime_type[] = 'application/msword';
  1132. $extension[] = '.doc';
  1133. $mime_type[] = 'application/rtf';
  1134. $extension[] = '.rtf';
  1135. $mime_type[] = 'application/vnd.ms-powerpoint';
  1136. $extension[] = '.ppt';
  1137. $mime_type[] = 'application/vnd.ms-excel';
  1138. $extension[] = '.xls';
  1139. $mime_type[] = 'application/pdf';
  1140. $extension[] = '.pdf';
  1141. $mime_type[] = 'application/postscript';
  1142. $extension[] = '.ps';
  1143. $mime_type[] = 'application/mac-binhex40';
  1144. $extension[] = '.hqx';
  1145. $mime_type[] = 'application/x-gzip';
  1146. $extension[] = 'tar.gz';
  1147. $mime_type[] = 'application/x-shockwave-flash';
  1148. $extension[] = '.swf';
  1149. $mime_type[] = 'application/x-stuffit';
  1150. $extension[] = '.sit';
  1151. $mime_type[] = 'application/x-tar';
  1152. $extension[] = '.tar';
  1153. $mime_type[] = 'application/zip';
  1154. $extension[] = '.zip';
  1155. $mime_type[] = 'application/x-tar';
  1156. $extension[] = '.tar';
  1157. $mime_type[] = 'text/html';
  1158. $extension[] = '.html';
  1159. $mime_type[] = 'text/plain';
  1160. $extension[] = '.txt';
  1161. $mime_type[] = 'text/rtf';
  1162. $extension[] = '.rtf';
  1163. $mime_type[] = 'img/gif';
  1164. $extension[] = '.gif';
  1165. $mime_type[] = 'img/jpeg';
  1166. $extension[] = '.jpg';
  1167. $mime_type[] = 'img/png';
  1168. $extension[] = '.png';
  1169. $mime_type[] = 'audio/midi';
  1170. $extension[] = '.mid';
  1171. $mime_type[] = 'audio/mpeg';
  1172. $extension[] = '.mp3';
  1173. $mime_type[] = 'audio/x-aiff';
  1174. $extension[] = '.aif';
  1175. $mime_type[] = 'audio/x-pn-realaudio';
  1176. $extension[] = '.rm';
  1177. $mime_type[] = 'audio/x-pn-realaudio-plugin';
  1178. $extension[] = '.rpm';
  1179. $mime_type[] = 'audio/x-wav';
  1180. $extension[] = '.wav';
  1181. $mime_type[] = 'video/mpeg';
  1182. $extension[] = '.mpg';
  1183. $mime_type[] = 'video/mpeg4-generic';
  1184. $extension[] = '.mp4';
  1185. $mime_type[] = 'video/quicktime';
  1186. $extension[] = '.mov';
  1187. $mime_type[] = 'video/x-msvideo';
  1188. $extension[] = '.avi';
  1189. $mime_type[] = 'video/x-ms-wmv';
  1190. $extension[] = '.wmv';
  1191. $mime_type[] = 'video/x-flv';
  1192. $extension[] = '.flv';
  1193. $mime_type[] = 'image/svg+xml';
  1194. $extension[] = '.svg';
  1195. $mime_type[] = 'image/svg+xml';
  1196. $extension[] = '.svgz';
  1197. $mime_type[] = 'video/ogg';
  1198. $extension[] = '.ogv';
  1199. $mime_type[] = 'audio/ogg';
  1200. $extension[] = '.oga';
  1201. $mime_type[] = 'application/ogg';
  1202. $extension[] = '.ogg';
  1203. $mime_type[] = 'application/ogg';
  1204. $extension[] = '.ogx';
  1205. $mime_type[] = 'application/x-freemind';
  1206. $extension[] = '.mm';
  1207. $mime_type[] = 'application/vnd.ms-word.document.macroEnabled.12';
  1208. $extension[] = '.docm';
  1209. $mime_type[] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
  1210. $extension[] = '.docx';
  1211. $mime_type[] = 'application/vnd.ms-word.template.macroEnabled.12';
  1212. $extension[] = '.dotm';
  1213. $mime_type[] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template';
  1214. $extension[] = '.dotx';
  1215. $mime_type[] = 'application/vnd.ms-powerpoint.template.macroEnabled.12';
  1216. $extension[] = '.potm';
  1217. $mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.template';
  1218. $extension[] = '.potx';
  1219. $mime_type[] = 'application/vnd.ms-powerpoint.addin.macroEnabled.12';
  1220. $extension[] = '.ppam';
  1221. $mime_type[] = 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12';
  1222. $extension[] = '.ppsm';
  1223. $mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow';
  1224. $extension[] = '.ppsx';
  1225. $mime_type[] = 'application/vnd.ms-powerpoint.presentation.macroEnabled.12';
  1226. $extension[] = '.pptm';
  1227. $mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
  1228. $extension[] = '.pptx';
  1229. $mime_type[] = 'application/vnd.ms-excel.addin.macroEnabled.12';
  1230. $extension[] = '.xlam';
  1231. $mime_type[] = 'application/vnd.ms-excel.sheet.binary.macroEnabled.12';
  1232. $extension[] = '.xlsb';
  1233. $mime_type[] = 'application/vnd.ms-excel.sheet.macroEnabled.12';
  1234. $extension[] = '.xlsm';
  1235. $mime_type[] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  1236. $extension[] = '.xlsx';
  1237. $mime_type[] = 'application/vnd.ms-excel.template.macroEnabled.12';
  1238. $extension[] = '.xltm';
  1239. $mime_type[] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.template';
  1240. $extension[] = '.xltx';
  1241. // Test on PC (files with no extension get application/octet-stream)
  1242. //$mime_type[] = 'application/octet-stream'; $extension[] = '.ext';
  1243. // Check whether the MIME type sent by the browser is within the table
  1244. foreach ($mime_type as $key => & $type) {
  1245. if ($type == $file_type) {
  1246. $file_name .= $extension[$key];
  1247. break;
  1248. }
  1249. }
  1250. unset($mime_type, $extension, $type, $key); // Delete to eschew possible collisions
  1251. }
  1252. return $file_name;
  1253. }
  1254. /**
  1255. * Manages all the unzipping process of an uploaded document
  1256. * This uses the item_property table for properties of documents
  1257. *
  1258. * @author Hugues Peeters <hugues.peeters@claroline.net>
  1259. * @author Bert Vanderkimpen
  1260. *
  1261. * @param array $uploaded_file - follows the $_FILES Structure
  1262. * @param string $upload_path - destination of the upload.
  1263. * This path is to append to $base_work_dir
  1264. * @param string $base_work_dir - base working directory of the module
  1265. * @param int $max_filled_space - amount of bytes to not exceed in the base
  1266. * working directory
  1267. * @param boolean Output switch. Optional. If no output not wanted on success, set to false.
  1268. *
  1269. * @return boolean true if it succeeds false otherwise
  1270. */
  1271. static function unzip_uploaded_document(
  1272. $uploaded_file,
  1273. $upload_path,
  1274. $base_work_dir,
  1275. $max_filled_space,
  1276. $output = true,
  1277. $to_group_id = 0
  1278. ) {
  1279. $_course = api_get_course_info();
  1280. global $_user;
  1281. global $to_user_id;
  1282. global $to_group_id;
  1283. $zip_file = new PclZip($uploaded_file['tmp_name']);
  1284. // Check the zip content (real size and file extension)
  1285. $zip_content_array = (array)$zip_file->listContent();
  1286. $real_filesize = 0;
  1287. foreach ($zip_content_array as & $this_content) {
  1288. $real_filesize += $this_content['size'];
  1289. }
  1290. if (!DocumentManager::enough_space($real_filesize, $max_filled_space)) {
  1291. Display::display_error_message(get_lang('UplNotEnoughSpace'));
  1292. return false;
  1293. }
  1294. // It happens on Linux that $upload_path sometimes doesn't start with '/'
  1295. if ($upload_path[0] != '/') {
  1296. $upload_path = '/'.$upload_path;
  1297. }
  1298. /* Uncompressing phase */
  1299. // Get into the right directory
  1300. $save_dir = getcwd();
  1301. chdir($base_work_dir.$upload_path);
  1302. // We extract using a callback function that "cleans" the path
  1303. //@todo check if this works
  1304. $unzipping_state = $zip_file->extract(
  1305. PCLZIP_CB_PRE_EXTRACT,
  1306. 'FileManager::clean_up_files_in_zip',
  1307. PCLZIP_OPT_REPLACE_NEWER
  1308. );
  1309. // Add all documents in the unzipped folder to the database
  1310. self::add_all_documents_in_folder_to_database(
  1311. $_course,
  1312. $_user['user_id'],
  1313. $base_work_dir,
  1314. $upload_path == '/' ? '' : $upload_path,
  1315. $to_group_id
  1316. );
  1317. //Display::display_normal_message(get_lang('UplZipExtractSuccess'));
  1318. return true;
  1319. }
  1320. /**
  1321. * This function is a callback function that is used while extracting a zipfile
  1322. * http://www.phpconcept.net/pclzip/man/en/index.php?options-pclzip_cb_pre_extract
  1323. *
  1324. * @param $p_event
  1325. * @param $p_header
  1326. * @return 1 (If the function returns 1, then the extraction is resumed)
  1327. */
  1328. function clean_up_files_in_zip($p_event, &$p_header)
  1329. {
  1330. $res = self::clean_up_path($p_header['filename']);
  1331. return $res;
  1332. }
  1333. /**
  1334. * This function cleans up a given path
  1335. * by eliminating dangerous file names and cleaning them
  1336. *
  1337. * @param string $path
  1338. * @return $path
  1339. * @see FileManager::disable_dangerous_file()
  1340. * @see replace_dangerous_char()
  1341. */
  1342. static function clean_up_path(&$path)
  1343. {
  1344. // Split the path in folders and files
  1345. $path_array = explode('/', $path);
  1346. // Clean up every foler and filename in the path
  1347. foreach ($path_array as $key => & $val) {
  1348. // We don't want to lose the dots in ././folder/file (cfr. zipfile)
  1349. if ($val != '.') {
  1350. $val = self::disable_dangerous_file(api_replace_dangerous_char($val));
  1351. }
  1352. }
  1353. // Join the "cleaned" path (modified in-place as passed by reference)
  1354. $path = implode('/', $path_array);
  1355. $res = self::filter_extension($path);
  1356. return $res;
  1357. }
  1358. /**
  1359. * Checks if the file is dangerous, based on extension and/or mimetype.
  1360. * The list of extensions accepted/rejected can be found from
  1361. * api_get_setting('upload_extensions_exclude') and api_get_setting('upload_extensions_include')
  1362. * @param string filename passed by reference. The filename will be modified if filter rules say so! (you can include path but the filename should look like 'abc.html')
  1363. * @return int 0 to skip file, 1 to keep file
  1364. */
  1365. static function filter_extension(&$filename)
  1366. {
  1367. if (substr($filename, -1) == '/') {
  1368. return 1; // Authorize directories
  1369. }
  1370. $blacklist = api_get_setting('upload_extensions_list_type');
  1371. if ($blacklist != 'whitelist') { // if = blacklist
  1372. $extensions = explode(';', strtolower(api_get_setting('upload_extensions_blacklist')));
  1373. $skip = api_get_setting('upload_extensions_skip');
  1374. $ext = strrchr($filename, '.');
  1375. $ext = substr($ext, 1);
  1376. if (empty($ext)) {
  1377. return 1; // We're in blacklist mode, so accept empty extensions
  1378. }
  1379. if (in_array(strtolower($ext), $extensions)) {
  1380. if ($skip == 'true') {
  1381. return 0;
  1382. } else {
  1383. $new_ext = api_get_setting('upload_extensions_replace_by');
  1384. $filename = str_replace('.'.$ext, '.'.$new_ext, $filename);
  1385. return 1;
  1386. }
  1387. } else {
  1388. return 1;
  1389. }
  1390. } else {
  1391. $extensions = split(';', strtolower(api_get_setting('upload_extensions_whitelist')));
  1392. $skip = api_get_setting('upload_extensions_skip');
  1393. $ext = strrchr($filename, '.');
  1394. $ext = substr($ext, 1);
  1395. if (empty($ext)) {
  1396. return 1; // Accept empty extensions
  1397. }
  1398. if (!in_array(strtolower($ext), $extensions)) {
  1399. if ($skip == 'true') {
  1400. return 0;
  1401. } else {
  1402. $new_ext = api_get_setting('upload_extensions_replace_by');
  1403. $filename = str_replace('.'.$ext, '.'.$new_ext, $filename);
  1404. return 1;
  1405. }
  1406. } else {
  1407. return 1;
  1408. }
  1409. }
  1410. }
  1411. /**
  1412. * Adds a new document to the database
  1413. *
  1414. * @param array $_course
  1415. * @param string $path
  1416. * @param string $filetype
  1417. * @param int $filesize
  1418. * @param string $title
  1419. *
  1420. * @return id if inserted document
  1421. */
  1422. static function add_document(
  1423. $_course,
  1424. $path,
  1425. $filetype,
  1426. $filesize,
  1427. $title,
  1428. $comment = null,
  1429. $readonly = 0,
  1430. $save_visibility = true,
  1431. $group_id = null
  1432. ) {
  1433. $session_id = api_get_session_id();
  1434. $readonly = intval($readonly);
  1435. $comment = Database::escape_string($comment);
  1436. $path = Database::escape_string($path);
  1437. $filetype = Database::escape_string($filetype);
  1438. $filesize = intval($filesize);
  1439. $title = Database::escape_string(htmlspecialchars($title));
  1440. $c_id = $_course['real_id'];
  1441. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  1442. $sql = "INSERT INTO $table_document (c_id, path, filetype, size, title, comment, readonly, session_id)
  1443. VALUES ($c_id, '$path','$filetype','$filesize','$title', '$comment', $readonly, $session_id)";
  1444. if (Database::query($sql)) {
  1445. $document_id = Database::insert_id();
  1446. if ($document_id) {
  1447. if ($save_visibility) {
  1448. api_set_default_visibility($_course, $document_id, TOOL_DOCUMENT, $group_id);
  1449. }
  1450. }
  1451. return $document_id;
  1452. } else {
  1453. return false;
  1454. }
  1455. }
  1456. /**
  1457. * Updates an existing document in the database
  1458. * as the file exists, we only need to change the size
  1459. *
  1460. * @param array $_course
  1461. * @param int $document_id
  1462. * @param int $filesize
  1463. * @param int $readonly
  1464. *
  1465. * @return boolean true /false
  1466. */
  1467. static function update_existing_document($_course, $document_id, $filesize, $readonly = 0)
  1468. {
  1469. $document_table = Database::get_course_table(TABLE_DOCUMENT);
  1470. $document_id = intval($document_id);
  1471. $filesize = intval($filesize);
  1472. $readonly = intval($readonly);
  1473. $course_id = $_course['real_id'];
  1474. $sql = "UPDATE $document_table SET size = '$filesize' , readonly = '$readonly'
  1475. WHERE c_id = $course_id AND id = $document_id";
  1476. if (Database::query($sql)) {
  1477. return true;
  1478. } else {
  1479. return false;
  1480. }
  1481. }
  1482. /**
  1483. * This function updates the last_edit_date, last edit user id on all folders in a given path
  1484. *
  1485. * @param array $_course
  1486. * @param string $path
  1487. * @param int $user_id
  1488. */
  1489. static function item_property_update_on_folder($_course, $path, $user_id)
  1490. {
  1491. //display_message("Start update_lastedit_on_folder");
  1492. // If we are in the root, just return... no need to update anything
  1493. if ($path == '/') {
  1494. return;
  1495. }
  1496. $user_id = intval($user_id);
  1497. // If the given path ends with a / we remove it
  1498. $endchar = substr($path, strlen($path) - 1, 1);
  1499. if ($endchar == '/') {
  1500. $path = substr($path, 0, strlen($path) - 1);
  1501. }
  1502. $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1503. // Get the time
  1504. $time = date('Y-m-d H:i:s', time());
  1505. // Det all paths in the given path
  1506. // /folder/subfolder/subsubfolder/file
  1507. // if file is updated, subsubfolder, subfolder and folder are updated
  1508. $exploded_path = explode('/', $path);
  1509. $course_id = api_get_course_int_id();
  1510. foreach ($exploded_path as $key => & $value) {
  1511. // We don't want a slash before our first slash
  1512. if ($key != 0) {
  1513. $newpath .= '/'.$value;
  1514. //echo 'path= '.$newpath.'<br />';
  1515. // Select ID of given folder
  1516. $folder_id = DocumentManager::get_document_id($_course, $newpath);
  1517. if ($folder_id) {
  1518. $sql = "UPDATE $TABLE_ITEMPROPERTY SET lastedit_date='$time',lastedit_type='DocumentInFolderUpdated', lastedit_user_id='$user_id'
  1519. WHERE c_id = $course_id AND tool='".TOOL_DOCUMENT."' AND ref='$folder_id'";
  1520. Database::query($sql);
  1521. }
  1522. }
  1523. }
  1524. }
  1525. /**
  1526. * Returns the directory depth of the file.
  1527. *
  1528. * @author Olivier Cauberghe <olivier.cauberghe@ugent.be>
  1529. * @param path+filename eg: /main/document/document.php
  1530. * @return The directory depth
  1531. */
  1532. static function get_levels($filename)
  1533. {
  1534. $levels = explode('/', $filename);
  1535. if (empty($levels[count($levels) - 1])) {
  1536. unset($levels[count($levels) - 1]);
  1537. }
  1538. return count($levels);
  1539. }
  1540. /**
  1541. * Retrieves the image path list in a html file
  1542. *
  1543. * @author Hugues Peeters <hugues.peeters@claroline.net>
  1544. * @param string $html_file
  1545. * @return array - images path list
  1546. */
  1547. static function search_img_from_html($html_file)
  1548. {
  1549. $img_path_list = array();
  1550. if (!$fp = fopen($html_file, 'r')) {
  1551. return;
  1552. }
  1553. // Aearch and store occurences of the <img> tag in an array
  1554. $size_file = (filesize($html_file) === 0) ? 1 : filesize($html_file);
  1555. if (isset($fp) && $fp !== false) {
  1556. $buffer = fread($fp, $size_file);
  1557. if (strlen($buffer) >= 0 && $buffer !== false) {
  1558. //
  1559. } else {
  1560. die('<center>Can not read file.</center>');
  1561. }
  1562. } else {
  1563. die('<center>Can not read file.</center>');
  1564. }
  1565. $matches = array();
  1566. if (preg_match_all('~<[[:space:]]*img[^>]*>~i', $buffer, $matches)) {
  1567. $img_tag_list = $matches[0];
  1568. }
  1569. fclose($fp);
  1570. unset($buffer);
  1571. // Search the image file path from all the <IMG> tag detected
  1572. if (sizeof($img_tag_list) > 0) {
  1573. foreach ($img_tag_list as & $this_img_tag) {
  1574. if (preg_match('~src[[:space:]]*=[[:space:]]*[\"]{1}([^\"]+)[\"]{1}~i', $this_img_tag, $matches)) {
  1575. $img_path_list[] = $matches[1];
  1576. }
  1577. }
  1578. $img_path_list = array_unique($img_path_list); // Remove duplicate entries
  1579. }
  1580. return $img_path_list;
  1581. }
  1582. /**
  1583. * Creates a new directory trying to find a directory name
  1584. * that doesn't already exist
  1585. * (we could use unique_name() here...)
  1586. *
  1587. * @author Hugues Peeters <hugues.peeters@claroline.net>
  1588. * @author Bert Vanderkimpen
  1589. * @param array $_course current course information
  1590. * @param int $user_id current user id
  1591. * @param string $desiredDirName complete path of the desired name
  1592. * @param string The visible name of the directory
  1593. * @param int Visibility (0 for invisible, 1 for visible, 2 for deleted)
  1594. * @return string actual directory name if it succeeds,
  1595. * boolean false otherwise
  1596. */
  1597. static function create_unexisting_directory(
  1598. $_course,
  1599. $user_id,
  1600. $session_id,
  1601. $to_group_id,
  1602. $to_user_id,
  1603. $base_work_dir,
  1604. $desired_dir_name,
  1605. $title = null,
  1606. $visibility = ''
  1607. ) {
  1608. $nb = '';
  1609. // add numerical suffix to directory if another one of the same number already exists
  1610. while (file_exists($base_work_dir.$desired_dir_name.$nb)) {
  1611. $nb += 1;
  1612. }
  1613. if ($title == null) {
  1614. $title = basename($desired_dir_name);
  1615. }
  1616. $course_id = $_course['real_id'];
  1617. if (mkdir($base_work_dir.$desired_dir_name.$nb, api_get_permissions_for_new_directories(), true)) {
  1618. // Check if pathname already exists inside document table
  1619. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1620. $sql = "SELECT path FROM $tbl_document WHERE c_id = $course_id AND path='".$desired_dir_name.$nb."'";
  1621. $rs = Database::query($sql);
  1622. if (Database::num_rows($rs) == 0) {
  1623. $document_id = self::add_document(
  1624. $_course,
  1625. $desired_dir_name.$nb,
  1626. 'folder',
  1627. 0,
  1628. $title,
  1629. null,
  1630. 0,
  1631. true,
  1632. $to_group_id
  1633. );
  1634. if ($document_id) {
  1635. // Update document item_property
  1636. if ($visibility !== '') {
  1637. $visibilities = array(0 => 'invisible', 1 => 'visible', 2 => 'delete');
  1638. api_item_property_update(
  1639. $_course,
  1640. TOOL_DOCUMENT,
  1641. $document_id,
  1642. $visibilities[$visibility],
  1643. $user_id,
  1644. $to_group_id,
  1645. $to_user_id,
  1646. null,
  1647. null,
  1648. $session_id
  1649. );
  1650. } else {
  1651. api_item_property_update(
  1652. $_course,
  1653. TOOL_DOCUMENT,
  1654. $document_id,
  1655. 'FolderCreated',
  1656. $user_id,
  1657. $to_group_id,
  1658. $to_user_id,
  1659. null,
  1660. null,
  1661. $session_id
  1662. );
  1663. }
  1664. return $desired_dir_name.$nb;
  1665. }
  1666. } else {
  1667. //This means the folder NOT exist in the filesystem (now this was created) but there is a record in the Database
  1668. return $desired_dir_name.$nb;
  1669. }
  1670. } else {
  1671. return false;
  1672. }
  1673. }
  1674. /**
  1675. * Handles uploaded missing images
  1676. *
  1677. * @author Hugues Peeters <hugues.peeters@claroline.net>
  1678. * @author Bert Vanderkimpen
  1679. * @param array $_course
  1680. * @param array $uploaded_file_collection - follows the $_FILES Structure
  1681. * @param string $base_work_dir
  1682. * @param string $missing_files_dir
  1683. * @param int $user_id
  1684. * @param int $max_filled_space
  1685. */
  1686. static function move_uploaded_file_collection_into_directory(
  1687. $_course,
  1688. $uploaded_file_collection,
  1689. $base_work_dir,
  1690. $missing_files_dir,
  1691. $user_id,
  1692. $to_group_id,
  1693. $to_user_id,
  1694. $max_filled_space
  1695. ) {
  1696. $number_of_uploaded_images = count($uploaded_file_collection['name']);
  1697. for ($i = 0; $i < $number_of_uploaded_images; $i++) {
  1698. $missing_file['name'] = $uploaded_file_collection['name'][$i];
  1699. $missing_file['type'] = $uploaded_file_collection['type'][$i];
  1700. $missing_file['tmp_name'] = $uploaded_file_collection['tmp_name'][$i];
  1701. $missing_file['error'] = $uploaded_file_collection['error'][$i];
  1702. $missing_file['size'] = $uploaded_file_collection['size'][$i];
  1703. $upload_ok = self::process_uploaded_file($missing_file);
  1704. if ($upload_ok) {
  1705. $new_file_list[] = self::handle_uploaded_document(
  1706. $_course,
  1707. $missing_file,
  1708. $base_work_dir,
  1709. $missing_files_dir,
  1710. $user_id,
  1711. $to_group_id,
  1712. $to_user_id,
  1713. $max_filled_space,
  1714. 0,
  1715. 'overwrite'
  1716. );
  1717. }
  1718. unset($missing_file);
  1719. }
  1720. return $new_file_list;
  1721. }
  1722. /**
  1723. * Opens the old html file and replace the src path into the img tag
  1724. * This also works for files in subdirectories.
  1725. * @param $original_img_path is an array
  1726. * @param $new_img_path is an array
  1727. */
  1728. static function replace_img_path_in_html_file($original_img_path, $new_img_path, $html_file)
  1729. {
  1730. // Open the file
  1731. $fp = fopen($html_file, 'r');
  1732. $buffer = fread($fp, filesize($html_file));
  1733. $new_html_content = null;
  1734. // Fix the image tags
  1735. for ($i = 0, $fileNb = count($original_img_path); $i < $fileNb; $i++) {
  1736. $replace_what = $original_img_path[$i];
  1737. // We only need the directory and the filename /path/to/file_html_files/missing_file.gif -> file_html_files/missing_file.gif
  1738. $exploded_file_path = explode('/', $new_img_path[$i]);
  1739. $replace_by = $exploded_file_path[count($exploded_file_path) - 2].'/'.$exploded_file_path[count(
  1740. $exploded_file_path
  1741. ) - 1];
  1742. $buffer = str_replace($replace_what, $replace_by, $buffer);
  1743. }
  1744. $new_html_content .= $buffer;
  1745. @fclose($fp);
  1746. // Write the resulted new file
  1747. if (!$fp = fopen($html_file, 'w')) {
  1748. return;
  1749. }
  1750. if (!fwrite($fp, $new_html_content)) {
  1751. return;
  1752. }
  1753. }
  1754. /**
  1755. * Checks the extension of a file, if it's .htm or .html
  1756. * we use search_img_from_html to get all image paths in the file
  1757. *
  1758. * @param string $file
  1759. * @return array paths
  1760. * @see check_for_missing_files() uses search_img_from_html()
  1761. */
  1762. static function check_for_missing_files($file)
  1763. {
  1764. if (strrchr($file, '.') == '.htm' || strrchr($file, '.') == '.html') {
  1765. $img_file_path = self::search_img_from_html($file);
  1766. return $img_file_path;
  1767. }
  1768. return false;
  1769. }
  1770. /**
  1771. * This function builds a form that asks for the missing images in a html file
  1772. * maybe we should do this another way?
  1773. *
  1774. * @param array $missing_files
  1775. * @param string $upload_path
  1776. * @param string $file_name
  1777. * @return string the form
  1778. */
  1779. static function build_missing_files_form($missing_files, $upload_path, $file_name)
  1780. {
  1781. // Do we need a / or not?
  1782. $added_slash = ($upload_path == '/') ? '' : '/';
  1783. $folder_id = DocumentManager::get_document_id(api_get_course_info(), $upload_path);
  1784. // Build the form
  1785. $form = "<p><strong>".get_lang('MissingImagesDetected')."</strong></p>"
  1786. ."<form method=\"post\" action=\"".api_get_self()."\" enctype=\"multipart/form-data\">"
  1787. // Related_file is the path to the file that has missing images
  1788. ."<input type=\"hidden\" name=\"related_file\" value=\"".$upload_path.$added_slash.$file_name."\" />"
  1789. ."<input type=\"hidden\" name=\"upload_path\" value=\"".$upload_path."\" />"
  1790. ."<input type=\"hidden\" name=\"id\" value=\"".$folder_id."\" />"
  1791. ."<table border=\"0\">";
  1792. foreach ($missing_files as & $this_img_file_path) {
  1793. $form .= "<tr>"
  1794. ."<td>".basename($this_img_file_path)." : </td>"
  1795. ."<td>"
  1796. ."<input type=\"file\" name=\"img_file[]\"/>"
  1797. ."<input type=\"hidden\" name=\"img_file_path[]\" value=\"".$this_img_file_path."\" />"
  1798. ."</td>"
  1799. ."</tr>";
  1800. }
  1801. $form .= "</table>"
  1802. ."<button type='submit' name=\"cancel_submit_image\" value=\"".get_lang(
  1803. 'Cancel'
  1804. )."\" class=\"cancel\">".get_lang('Cancel')."</button>"
  1805. ."<button type='submit' name=\"submit_image\" value=\"".get_lang('Ok')."\" class=\"save\">".get_lang(
  1806. 'Ok'
  1807. )."</button>"
  1808. ."</form>";
  1809. return $form;
  1810. }
  1811. /**
  1812. * This recursive function can be used during the upgrade process form older versions of Chamilo
  1813. * It crawls the given directory, checks if the file is in the DB and adds it if it's not
  1814. *
  1815. * @param string $base_work_dir
  1816. * @param string $current_path, needed for recursivity
  1817. */
  1818. static function add_all_documents_in_folder_to_database(
  1819. $_course,
  1820. $user_id,
  1821. $base_work_dir,
  1822. $current_path = '',
  1823. $to_group_id = 0
  1824. ) {
  1825. $current_session_id = api_get_session_id();
  1826. $path = $base_work_dir.$current_path;
  1827. // Open dir
  1828. $handle = opendir($path);
  1829. if (is_dir($path)) {
  1830. // Run trough
  1831. while ($file = readdir($handle)) {
  1832. if ($file == '.' || $file == '..') {
  1833. continue;
  1834. }
  1835. $completepath = "$path/$file";
  1836. // Directory?
  1837. if (is_dir($completepath)) {
  1838. $title = self::get_document_title($file);
  1839. $safe_file = api_replace_dangerous_char($file);
  1840. @rename($path.'/'.$file, $path.'/'.$safe_file);
  1841. // If we can't find the file, add it
  1842. if (!DocumentManager::get_document_id($_course, $current_path.'/'.$safe_file)) {
  1843. $document_id = self::add_document($_course, $current_path.'/'.$safe_file, 'folder', 0, $title);
  1844. api_item_property_update(
  1845. $_course,
  1846. TOOL_DOCUMENT,
  1847. $document_id,
  1848. 'DocumentAdded',
  1849. $user_id,
  1850. $to_group_id,
  1851. null,
  1852. null,
  1853. null,
  1854. $current_session_id
  1855. );
  1856. //echo $current_path.'/'.$safe_file.' added!<br />';
  1857. }
  1858. // Recursive
  1859. self::add_all_documents_in_folder_to_database(
  1860. $_course,
  1861. $user_id,
  1862. $base_work_dir,
  1863. $current_path.'/'.$safe_file,
  1864. $to_group_id
  1865. );
  1866. } else {
  1867. //Rename
  1868. $safe_file = self::disable_dangerous_file(api_replace_dangerous_char($file, 'strict'));
  1869. @rename($base_work_dir.$current_path.'/'.$file, $base_work_dir.$current_path.'/'.$safe_file);
  1870. $document_id = DocumentManager::get_document_id($_course, $current_path.'/'.$safe_file);
  1871. if (!$document_id) {
  1872. $title = self::get_document_title($file);
  1873. $size = filesize($base_work_dir.$current_path.'/'.$safe_file);
  1874. $document_id = self::add_document(
  1875. $_course,
  1876. $current_path.'/'.$safe_file,
  1877. 'file',
  1878. $size,
  1879. $title
  1880. );
  1881. api_item_property_update(
  1882. $_course,
  1883. TOOL_DOCUMENT,
  1884. $document_id,
  1885. 'DocumentAdded',
  1886. $user_id,
  1887. $to_group_id,
  1888. null,
  1889. null,
  1890. null,
  1891. $current_session_id
  1892. );
  1893. //echo $current_path.'/'.$safe_file.' added!<br />';
  1894. } else {
  1895. api_item_property_update(
  1896. $_course,
  1897. TOOL_DOCUMENT,
  1898. $document_id,
  1899. 'DocumentUpdated',
  1900. $user_id,
  1901. $to_group_id,
  1902. null,
  1903. null,
  1904. null,
  1905. $current_session_id
  1906. );
  1907. }
  1908. }
  1909. }
  1910. }
  1911. }
  1912. }