fileManage.lib.php 21 KB

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