fileManage.lib.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. <?php # $Id: fileManage.lib.php 14316 2008-02-19 15:38:24Z yannoo $
  2. /* vim: set expandtab tabstop=4 shiftwidth=4:
  3. ===============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. more copyrights held by individual contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ===============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * This is the file manage library for Dokeos.
  22. * Include/require it in your code to use its functionality.
  23. *
  24. * @package dokeos.library
  25. ==============================================================================
  26. */
  27. /**
  28. * Update the file or directory path in the document db document table
  29. *
  30. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  31. * @param - action (string) - action type require : 'delete' or 'update'
  32. * @param - oldPath (string) - old path info stored to change
  33. * @param - newPath (string) - new path info to substitute
  34. * @desc Update the file or directory path in the document db document table
  35. *
  36. */
  37. function update_db_info($action, $oldPath, $newPath="")
  38. {
  39. global $dbTable; // table 'document' // RH: see below
  40. /* DELETE */
  41. if ($action == "delete")
  42. {
  43. /* // RH: metadata, update 2004/08/23
  44. these two lines replaced by new code below:
  45. $query = "DELETE FROM `$dbTable`
  46. WHERE path='".$oldPath."' OR path LIKE '".$oldPath."/%'";
  47. */
  48. $to_delete = "WHERE path LIKE BINARY '".$oldPath."' OR path LIKE BINARY '".$oldPath."/%'";
  49. $query = "DELETE FROM $dbTable " . $to_delete;
  50. $result = api_sql_query("SELECT id FROM $dbTable " . $to_delete);
  51. if (mysql_num_rows($result))
  52. {
  53. require_once(api_get_path(INCLUDE_PATH) . "../metadata/md_funcs.php");
  54. $mdStore = new mdstore(TRUE); // create if needed
  55. $mdType = (substr($dbTable, -13) == 'scormdocument') ?
  56. 'Scorm' : 'Document';
  57. while ($row = mysql_fetch_array($result))
  58. {
  59. $eid = $mdType . '.' . $row['id'];
  60. $mdStore->mds_delete($eid);
  61. $mdStore->mds_delete_offspring($eid);
  62. }
  63. }
  64. }
  65. /* UPDATE */
  66. if ($action == "update")
  67. {
  68. //Display::display_normal_message("newPath = $newPath");
  69. if ($newPath[0] == ".") $newPath = substr($newPath,1);
  70. $newPath = str_replace('//','/',$newPath);
  71. //Display::display_normal_message("character 0 = " . $newPath[0] . " 1=" . $newPath[1]);
  72. //Display::display_normal_message("newPath = $newPath");
  73. //older broken version
  74. //$query = "UPDATE `$dbTable`
  75. //SET path = CONCAT('".$newPath."', SUBSTRING(path, LENGTH('".$oldPath."')+1) )
  76. //WHERE path = '".$oldPath."' OR path LIKE '".$oldPath."/%'";
  77. //attempt to update - tested & working for root dir
  78. $query = "UPDATE $dbTable
  79. SET path = CONCAT('".$newPath."', SUBSTRING(path, LENGTH('".$oldPath."')+1) )
  80. WHERE path LIKE BINARY '".$oldPath."' OR path LIKE BINARY '".$oldPath."/%'";
  81. }
  82. //echo $query;
  83. //error_log($query,0);
  84. api_sql_query($query,__FILE__,__LINE__);
  85. //Display::display_normal_message("query = $query");
  86. }
  87. //------------------------------------------------------------------------------
  88. /**
  89. * Cheks a file or a directory actually exist at this location
  90. *
  91. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  92. * @param - filePath (string) - path of the presume existing file or dir
  93. * @return - boolean TRUE if the file or the directory exists
  94. * boolean FALSE otherwise.
  95. */
  96. function check_name_exist($filePath)
  97. {
  98. clearstatcache();
  99. $save_dir = getcwd();
  100. chdir ( dirname($filePath) );
  101. $fileName = basename ($filePath);
  102. if (file_exists( $fileName ))
  103. {
  104. chdir($save_dir);
  105. return true;
  106. }
  107. else
  108. {
  109. chdir($save_dir);
  110. return false;
  111. }
  112. }
  113. /**
  114. * Delete a file or a directory
  115. *
  116. * @author - Hugues Peeters
  117. * @param - $file (String) - the path of file or directory to delete
  118. * @return - bolean - true if the delete succeed
  119. * bolean - false otherwise.
  120. * @see - delete() uses check_name_exist() and removeDir() functions
  121. */
  122. function my_delete($file)
  123. {
  124. if ( check_name_exist($file) )
  125. {
  126. if ( is_file($file) ) // FILE CASE
  127. {
  128. unlink($file);
  129. return true;
  130. }
  131. elseif ( is_dir($file) ) // DIRECTORY CASE
  132. {
  133. removeDir($file);
  134. return true;
  135. }
  136. }
  137. else
  138. {
  139. return false; // no file or directory to delete
  140. }
  141. }
  142. //------------------------------------------------------------------------------
  143. /**
  144. * removes a directory recursively
  145. *
  146. * @returns true if OK, otherwise false
  147. *
  148. * @author Amary <MasterNES@aol.com> (from Nexen.net)
  149. * @author Olivier Brouckaert <oli.brouckaert@skynet.be>
  150. *
  151. * @param string $dir directory to remove
  152. */
  153. function removeDir($dir)
  154. {
  155. if(!@$opendir = opendir($dir))
  156. {
  157. return false;
  158. }
  159. while($readdir = readdir($opendir))
  160. {
  161. if($readdir != '..' && $readdir != '.')
  162. {
  163. if(is_file($dir.'/'.$readdir))
  164. {
  165. if(!@unlink($dir.'/'.$readdir))
  166. {
  167. return false;
  168. }
  169. }
  170. elseif(is_dir($dir.'/'.$readdir))
  171. {
  172. if(!removeDir($dir.'/'.$readdir))
  173. {
  174. return false;
  175. }
  176. }
  177. }
  178. }
  179. closedir($opendir);
  180. if(!@rmdir($dir))
  181. {
  182. return false;
  183. }
  184. return true;
  185. }
  186. //------------------------------------------------------------------------------
  187. /**
  188. * Rename a file or a directory
  189. *
  190. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  191. * @param - $filePath (string) - complete path of the file or the directory
  192. * @param - $newFileName (string) - new name for the file or the directory
  193. * @return - boolean - true if succeed
  194. * - boolean - false otherwise
  195. * @see - rename() uses the check_name_exist() and php2phps() functions
  196. */
  197. function my_rename($filePath, $newFileName)
  198. {
  199. $save_dir = getcwd();
  200. $path = dirname($filePath);
  201. $oldFileName = basename($filePath);
  202. $newFileName = replace_dangerous_char($newFileName);
  203. // If no extension, take the old one
  204. if ((strpos($newFileName, '.') === FALSE)
  205. && ($dotpos = strrpos($oldFileName, '.')))
  206. {
  207. $newFileName .= substr($oldFileName, $dotpos);
  208. }
  209. // Note: still possible: 'xx.yy' -rename-> '.yy' -rename-> 'zz'
  210. // This is useful for folder names, where otherwise '.' would be sticky
  211. // Extension PHP is not allowed, change to PHPS
  212. $newFileName = php2phps($newFileName);
  213. if ($newFileName == $oldFileName) return $oldFileName;
  214. if (strtolower($newFileName) != strtolower($oldFileName) && check_name_exist($path."/".$newFileName)) return false;
  215. // On a Windows server, it would be better not to do the above check
  216. // because it succeeds for some new names resembling the old name.
  217. // But on Unix/Linux the check must be done because rename overwrites.
  218. chdir($path);
  219. $res = rename($oldFileName, $newFileName) ? $newFileName : false;
  220. chdir($save_dir);
  221. return $res;
  222. }
  223. //------------------------------------------------------------------------------
  224. /**
  225. * Move a file or a directory to an other area
  226. *
  227. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  228. * @param - $source (String) - the path of file or directory to move
  229. * @param - $target (String) - the path of the new area
  230. * @return - bolean - true if the move succeed
  231. * bolean - false otherwise.
  232. * @see - move() uses check_name_exist() and copyDirTo() functions
  233. */
  234. function move($source, $target)
  235. {
  236. if ( check_name_exist($source) )
  237. {
  238. $fileName = basename($source);
  239. if ( check_name_exist($target."/".$fileName) )
  240. {
  241. return false;
  242. }
  243. else
  244. { /* File case */
  245. if ( is_file($source) )
  246. {
  247. copy($source , $target."/".$fileName);
  248. unlink($source);
  249. return true;
  250. }
  251. /* Directory case */
  252. elseif (is_dir($source))
  253. {
  254. // check to not copy the directory inside itself
  255. if (ereg("^".$source."/", $target."/"))
  256. {
  257. return false;
  258. }
  259. else
  260. {
  261. copyDirTo($source, $target);
  262. return true;
  263. }
  264. }
  265. }
  266. }
  267. else
  268. {
  269. return false;
  270. }
  271. }
  272. //------------------------------------------------------------------------------
  273. /**
  274. * Move a directory and its content to an other area
  275. *
  276. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  277. * @param - $origDirPath (String) - the path of the directory to move
  278. * @param - $destination (String) - the path of the new area
  279. * @return - no return !!
  280. */
  281. function copyDirTo($origDirPath, $destination, $move=true)
  282. {
  283. $save_dir=getcwd();
  284. // extract directory name - create it at destination - update destination trail
  285. $dirName = basename($origDirPath);
  286. mkdir ($destination."/".$dirName, 0775);
  287. $destinationTrail = $destination."/".$dirName;
  288. chdir ($origDirPath) ;
  289. $handle = opendir($origDirPath);
  290. while ($element = readdir($handle) )
  291. {
  292. if ( $element == "." || $element == "..")
  293. {
  294. continue; // skip the current and parent directories
  295. }
  296. elseif ( is_file($element) )
  297. {
  298. copy($element, $destinationTrail."/".$element);
  299. if($move)
  300. {
  301. unlink($element) ;
  302. }
  303. }
  304. elseif ( is_dir($element) )
  305. {
  306. $dirToCopy[] = $origDirPath."/".$element;
  307. }
  308. }
  309. closedir($handle) ;
  310. if ( sizeof($dirToCopy) > 0)
  311. {
  312. foreach($dirToCopy as $thisDir)
  313. {
  314. copyDirTo($thisDir, $destinationTrail, $move); // recursivity
  315. }
  316. }
  317. if($move)
  318. {
  319. rmdir ($origDirPath) ;
  320. }
  321. chdir($save_dir);
  322. }
  323. //------------------------------------------------------------------------------
  324. /* NOTE: These functions batch is used to automatically build HTML forms
  325. * with a list of the directories contained on the course Directory.
  326. *
  327. * From a thechnical point of view, form_dir_lists calls sort_dir wich calls index_dir
  328. */
  329. /**
  330. * Indexes all the directories and subdirectories
  331. * contented in a given directory
  332. *
  333. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  334. * @param - path (string) - directory path of the one to index
  335. * @return - an array containing the path of all the subdirectories
  336. */
  337. function index_dir($path)
  338. {
  339. $save_dir = getcwd();
  340. chdir($path);
  341. $handle = opendir($path);
  342. // reads directory content end record subdirectoies names in $dir_array
  343. while ($element = readdir($handle) )
  344. {
  345. if ( $element == "." || $element == "..") continue; // skip the current and parent directories
  346. if ( is_dir($element) ) $dirArray[] = $path."/".$element;
  347. }
  348. closedir($handle) ;
  349. // recursive operation if subdirectories exist
  350. $dirNumber = sizeof($dirArray);
  351. if ( $dirNumber > 0 )
  352. {
  353. for ($i = 0 ; $i < $dirNumber ; $i++ )
  354. {
  355. $subDirArray = index_dir( $dirArray[$i] ) ; // function recursivity
  356. $dirArray = array_merge( (array)$dirArray , (array)$subDirArray ); // data merge
  357. }
  358. }
  359. chdir($save_dir) ;
  360. return $dirArray ;
  361. }
  362. /**
  363. * Indexes all the directories and subdirectories
  364. * contented in a given directory, and sort them alphabetically
  365. *
  366. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  367. * @param - path (string) - directory path of the one to index
  368. * @return - an array containing the path of all the subdirectories sorted
  369. * false, if there is no directory
  370. * @see - index_and_sort_dir uses the index_dir() function
  371. */
  372. function index_and_sort_dir($path)
  373. {
  374. $dir_list = index_dir($path);
  375. if ($dir_list)
  376. {
  377. sort($dir_list);
  378. return $dir_list;
  379. }
  380. else
  381. {
  382. return false;
  383. }
  384. }
  385. /**
  386. * build an html form listing all directories of a given directory
  387. *
  388. */
  389. function form_dir_list($sourceType, $sourceComponent, $command, $baseWorkDir)
  390. {
  391. $dirList = index_and_sort_dir($baseWorkDir);
  392. $dialogBox .= "<form action=\"".api_get_self()."\" method=\"post\">\n" ;
  393. $dialogBox .= "<input type=\"hidden\" name=\"".$sourceType."\" value=\"".$sourceComponent."\">\n" ;
  394. $dialogBox .= get_lang('Move').' '.$sourceComponent.' '.get_lang('To');
  395. $dialogBox .= "<select name=\"".$command."\">\n" ;
  396. $dialogBox .= "<option value=\"\" style=\"color:#999999\">".get_lang('Root')."\n";
  397. $bwdLen = strlen($baseWorkDir) ; // base directories lenght, used under
  398. /* build html form inputs */
  399. if ($dirList)
  400. {
  401. while (list( , $pathValue) = each($dirList) )
  402. {
  403. $pathValue = substr ( $pathValue , $bwdLen ); // truncate cunfidential informations confidentielles
  404. $dirname = basename ($pathValue); // extract $pathValue directory name du nom
  405. /* compute de the display tab */
  406. $tab = ""; // $tab reinitialisation
  407. $depth = substr_count($pathValue, "/"); // The number of nombre '/' indicates the directory deepness
  408. for ($h=0; $h<$depth; $h++)
  409. {
  410. $tab .= "&nbsp;&nbsp";
  411. }
  412. $dialogBox .= "<option value=\"$pathValue\">$tab>$dirname\n";
  413. }
  414. }
  415. $dialogBox .= "</select>\n";
  416. $dialogBox .= "<input type=\"submit\" value=\"".get_lang('Ok')."\">";
  417. $dialogBox .= "</form>\n";
  418. return $dialogBox;
  419. }
  420. //------------------------------------------------------------------------------
  421. /**
  422. * to create missing directory in a gived path
  423. *
  424. * @returns a resource identifier or FALSE if the query was not executed correctly.
  425. * @author KilerCris@Mail.com original function from php manual
  426. * @author Christophe Gesch� gesche@ipm.ucl.ac.be Claroline Team
  427. * @since 28-Aug-2001 09:12
  428. * @param sting $path wanted path
  429. * @param boolean $verbose fix if comments must be printed
  430. * @param string $mode fix if chmod is same of parent or default
  431. * @global string $langCreatedIn string to say "create in"
  432. */
  433. function mkpath($path, $verbose = false, $mode = "herit")
  434. {
  435. global $langCreatedIn, $_configuration;
  436. $path=str_replace("/","\\",$path);
  437. $dirs=explode("\\",$path);
  438. $path=$dirs[0];
  439. if($verbose)
  440. {
  441. echo "<UL>";
  442. }
  443. for($i=1;$i < sizeof($dirs);$i++)
  444. {
  445. $path.='/'.$dirs[$i];
  446. if(ereg('^'.$path,$_configuration['root_sys']) && strlen($path) < strlen($_configuration['root_sys']))
  447. {
  448. continue;
  449. }
  450. if(!is_dir($path))
  451. {
  452. $ret=mkdir($path,0770);
  453. if($ret)
  454. {
  455. if($verbose)
  456. {
  457. echo '<li><strong>'.basename($path).'</strong><br>'.$langCreatedIn.'<br><strong>'.realpath($path.'/..').'</strong></li>';
  458. }
  459. }
  460. else
  461. {
  462. if($verbose)
  463. {
  464. echo '</UL>error : '.$path.' not created';
  465. }
  466. $ret=false;
  467. break;
  468. }
  469. }
  470. }
  471. if($verbose)
  472. {
  473. echo '</UL>';
  474. }
  475. return $ret;
  476. }
  477. /**
  478. * to extract the extention of the filename
  479. *
  480. * @returns array
  481. * @param string $filename filename
  482. */
  483. function getextension($filename)
  484. {
  485. $bouts = explode(".", $filename);
  486. return array(array_pop($bouts), implode(".", $bouts));
  487. }
  488. /**
  489. * to compute the size of the directory
  490. *
  491. * @returns integer size
  492. * @param string $path path to size
  493. * @param boolean $recursive if true , include subdir in total
  494. */
  495. function dirsize($root,$recursive=true)
  496. {
  497. $dir=@opendir($root);
  498. $size=0;
  499. while($file=@readdir($dir))
  500. {
  501. if(!in_array($file,array('.','..')))
  502. {
  503. if(is_dir($root.'/'.$file))
  504. {
  505. $size+=$recursive?dirsize($root.'/'.$file):0;
  506. }
  507. else
  508. {
  509. $size+=@filesize($root.'/'.$file);
  510. }
  511. }
  512. }
  513. @closedir($dir);
  514. return $size;
  515. }
  516. /*
  517. ===============================================================
  518. CLASS FileManager
  519. ===============================================================
  520. */
  521. /**
  522. This class contains functions that you can access statically.
  523. FileManager::list_all_directories($path)
  524. FileManager::list_all_files($dir_array)
  525. FileManager::compat_load_file($file_name)
  526. FileManager::set_default_settings($upload_path, $filename, $filetype="file", $glued_table, $default_visibility='v')
  527. @author Roan Embrechts
  528. @version 1.1, July 2004
  529. */
  530. class FileManager
  531. {
  532. /**
  533. ---------------------------------------------------------------
  534. Returns a list of all directories, except the base dir,
  535. of the current course. This function uses recursion.
  536. Convention: the parameter $path does not end with a slash.
  537. @author Roan Embrechts
  538. @version 1.0.1
  539. ---------------------------------------------------------------
  540. */
  541. function list_all_directories($path)
  542. {
  543. $save_dir = getcwd();
  544. chdir($path);
  545. $handle = opendir($path);
  546. while ($element = readdir($handle) )
  547. {
  548. if ( $element == "." || $element == "..") continue; // skip the current and parent directories
  549. if ( is_dir($element) )
  550. {
  551. $dirArray[] = $path."/".$element;
  552. }
  553. }
  554. closedir($handle) ;
  555. // recursive operation if subdirectories exist
  556. $dirNumber = sizeof($dirArray);
  557. if ( $dirNumber > 0 )
  558. {
  559. for ($i = 0 ; $i < $dirNumber ; $i++ )
  560. {
  561. $subDirArray = FileManager::list_all_directories( $dirArray[$i] ) ; // function recursivity
  562. $dirArray = array_merge( $dirArray , $subDirArray ) ; // data merge
  563. }
  564. }
  565. $resultArray = $dirArray;
  566. chdir($save_dir) ;
  567. return $resultArray ;
  568. }
  569. /**
  570. ===============================================================
  571. This function receives a list of directories.
  572. It returns a list of all files in these directories
  573. @author Roan Embrechts
  574. @version 1.0
  575. ===============================================================
  576. */
  577. function list_all_files($dirArray)
  578. {
  579. $save_dir = getcwd();
  580. foreach ($dirArray as $directory)
  581. {
  582. chdir($directory);
  583. $handle = opendir($directory);
  584. while ($element = readdir($handle) )
  585. {
  586. if ( $element == "." || $element == ".." || $element == '.htaccess') continue; // skip the current and parent directories
  587. if ( ! is_dir($element) )
  588. {
  589. $elementArray[] = $directory."/".$element;
  590. }
  591. }
  592. closedir($handle) ;
  593. chdir("..") ;
  594. }
  595. chdir($save_dir);
  596. return $elementArray;
  597. }
  598. /**
  599. Load contents of file $filename into memory
  600. and return them as a string.
  601. Function kept for compatibility with older PHP versions.
  602. Function is binary safe (is needed on Windows)
  603. */
  604. function compat_load_file($file_name)
  605. {
  606. $fp = fopen($file_name, "rb");
  607. $buffer = fread ($fp, filesize ($file_name));
  608. fclose ($fp);
  609. //api_display_debug_info(htmlentities($buffer));
  610. return $buffer;
  611. }
  612. /**
  613. * Adds file/folder to document table in database
  614. * improvement from set_default_settings (see below):
  615. * take all info from function parameters
  616. * no global variables needed
  617. *
  618. * NOTE $glued_table should already have backticks around it
  619. * (get it from the database library, and it is done automatically)
  620. *
  621. * @param path, filename, filetype,
  622. $glued_table, default_visibility
  623. * @action Adds an entry to the document table with the default settings.
  624. * @author Olivier Cauberghe <olivier.cauberghe@ugent.be>
  625. * @author Roan Embrechts
  626. * @version 1.2
  627. */
  628. function set_default_settings($upload_path, $filename, $filetype="file", $glued_table, $default_visibility='v')
  629. {
  630. if (!$default_visibility) $default_visibility="v";
  631. //make sure path is not wrongly formed
  632. if( strlen($upload_path) ) $upload_path = "/$upload_path";
  633. else $upload_path = "";
  634. $endchar=substr($filename,strlen($filename)-1,1);
  635. if($endchar=="\\" || $endchar=="/")
  636. {
  637. $filename=substr($filename,0,strlen($filename)-1);
  638. }
  639. $full_file_name = $upload_path."/".$filename;
  640. //$upload_path = str_replace("//", "/", $upload_path);
  641. $full_file_name = str_replace("//", "/", $full_file_name);
  642. $sql_query = "SELECT count(*) as number_existing FROM $glued_table WHERE path='$full_file_name'";
  643. //api_display_debug_info($sql_query);
  644. $sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
  645. $result = mysql_fetch_array($sql_result);
  646. //determine which query to execute
  647. if( $result["number_existing"] > 0 )
  648. {
  649. //entry exists, update
  650. $query="UPDATE $glued_table SET path='$full_file_name',visibility='$default_visibility', filetype='$filetype' WHERE path='$full_file_name'";
  651. }
  652. else
  653. {
  654. //no entry exists, create new one
  655. $query="INSERT INTO $glued_table (path,visibility,filetype) VALUES('$full_file_name','$default_visibility','$filetype')";
  656. }
  657. api_sql_query($query,__FILE__,__LINE__);
  658. }
  659. /**
  660. * Like in Java, creates the directory named by this abstract pathname,
  661. * including any necessary but nonexistent parent directories.
  662. *
  663. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  664. * @author Christophe Gesche <gesche@ipm.ucl.ac.be>
  665. *
  666. * @param string $path - path to create
  667. * @param string $mode - directory permission (default is '770')
  668. *
  669. * @return boolean TRUE if succeeds FALSE otherwise
  670. */
  671. function mkdirs($path, $mode = '0770')
  672. {
  673. if (file_exists($path))
  674. {
  675. return false;
  676. }
  677. else
  678. {
  679. FileManager :: mkdirs(dirname($path), $mode);
  680. return mkdir($path, $mode);
  681. }
  682. }
  683. } //end class FileManager
  684. ?>