📄 filemanage.lib.php
字号:
<?php # $Id: fileManage.lib.php 15278 2008-05-13 22:43:40Z yannoo $/* vim: set expandtab tabstop=4 shiftwidth=4:=============================================================================== Dokeos - elearning and course management software Copyright (c) 2004 Dokeos S.A. Copyright (c) 2003 Ghent University (UGent) Copyright (c) 2001 Universite catholique de Louvain (UCL) more copyrights held by individual contributors For a full list of contributors, see "credits.txt". The full license can be read in "license.txt". This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the GNU General Public License for more details. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com===============================================================================*//**==============================================================================* This is the file manage library for Dokeos.* Include/require it in your code to use its functionality.** @package dokeos.library==============================================================================*//** * Update the file or directory path in the document db document table * * @author - Hugues Peeters <peeters@ipm.ucl.ac.be> * @param - action (string) - action type require : 'delete' or 'update' * @param - oldPath (string) - old path info stored to change * @param - newPath (string) - new path info to substitute * @desc Update the file or directory path in the document db document table * */function update_db_info($action, $oldPath, $newPath=""){ global $dbTable; // table 'document' // RH: see below /* DELETE */ if ($action == "delete") { /* // RH: metadata, update 2004/08/23 these two lines replaced by new code below: $query = "DELETE FROM `$dbTable` WHERE path='".$oldPath."' OR path LIKE '".$oldPath."/%'"; */ $to_delete = "WHERE path LIKE BINARY '".$oldPath."' OR path LIKE BINARY '".$oldPath."/%'"; $query = "DELETE FROM $dbTable " . $to_delete; $result = api_sql_query("SELECT id FROM $dbTable " . $to_delete); if (mysql_num_rows($result)) { require_once(api_get_path(INCLUDE_PATH) . "../metadata/md_funcs.php"); $mdStore = new mdstore(TRUE); // create if needed $mdType = (substr($dbTable, -13) == 'scormdocument') ? 'Scorm' : 'Document'; while ($row = mysql_fetch_array($result)) { $eid = $mdType . '.' . $row['id']; $mdStore->mds_delete($eid); $mdStore->mds_delete_offspring($eid); } } } /* UPDATE */ if ($action == "update") { //Display::display_normal_message("newPath = $newPath"); if ($newPath[0] == ".") $newPath = substr($newPath,1); $newPath = str_replace('//','/',$newPath); //Display::display_normal_message("character 0 = " . $newPath[0] . " 1=" . $newPath[1]); //Display::display_normal_message("newPath = $newPath"); //older broken version //$query = "UPDATE `$dbTable` //SET path = CONCAT('".$newPath."', SUBSTRING(path, LENGTH('".$oldPath."')+1) ) //WHERE path = '".$oldPath."' OR path LIKE '".$oldPath."/%'"; //attempt to update - tested & working for root dir $query = "UPDATE $dbTable SET path = CONCAT('".$newPath."', SUBSTRING(path, LENGTH('".$oldPath."')+1) ) WHERE path LIKE BINARY '".$oldPath."' OR path LIKE BINARY '".$oldPath."/%'"; } //echo $query; //error_log($query,0); api_sql_query($query,__FILE__,__LINE__); //Display::display_normal_message("query = $query");}//------------------------------------------------------------------------------/** * Cheks a file or a directory actually exist at this location * * @author - Hugues Peeters <peeters@ipm.ucl.ac.be> * @param - filePath (string) - path of the presume existing file or dir * @return - boolean TRUE if the file or the directory exists * boolean FALSE otherwise. */function check_name_exist($filePath){ clearstatcache(); $save_dir = getcwd(); if(!is_dir(dirname($filePath))) { return false; } chdir ( dirname($filePath) ); $fileName = basename ($filePath); if (file_exists( $fileName )) { chdir($save_dir); return true; } else { chdir($save_dir); return false; }}/** * Delete a file or a directory * * @author - Hugues Peeters * @param - $file (String) - the path of file or directory to delete * @return - bolean - true if the delete succeed * bolean - false otherwise. * @see - delete() uses check_name_exist() and removeDir() functions */function my_delete($file){ if ( check_name_exist($file) ) { if ( is_file($file) ) // FILE CASE { unlink($file); return true; } elseif ( is_dir($file) ) // DIRECTORY CASE { removeDir($file); return true; } } else { return false; // no file or directory to delete }}//------------------------------------------------------------------------------/** * removes a directory recursively * * @returns true if OK, otherwise false * * @author Amary <MasterNES@aol.com> (from Nexen.net) * @author Olivier Brouckaert <oli.brouckaert@skynet.be> * * @param string $dir directory to remove */function removeDir($dir){ if(!@$opendir = opendir($dir)) { return false; } while($readdir = readdir($opendir)) { if($readdir != '..' && $readdir != '.') { if(is_file($dir.'/'.$readdir)) { if(!@unlink($dir.'/'.$readdir)) { return false; } } elseif(is_dir($dir.'/'.$readdir)) { if(!removeDir($dir.'/'.$readdir)) { return false; } } } } closedir($opendir); if(!@rmdir($dir)) { return false; } return true;}//------------------------------------------------------------------------------/** * Rename a file or a directory * * @author - Hugues Peeters <peeters@ipm.ucl.ac.be> * @param - $filePath (string) - complete path of the file or the directory * @param - $newFileName (string) - new name for the file or the directory * @return - boolean - true if succeed * - boolean - false otherwise * @see - rename() uses the check_name_exist() and php2phps() functions */function my_rename($filePath, $newFileName){ $save_dir = getcwd(); $path = dirname($filePath); $oldFileName = basename($filePath); $newFileName = replace_dangerous_char($newFileName); // If no extension, take the old one if ((strpos($newFileName, '.') === FALSE) && ($dotpos = strrpos($oldFileName, '.'))) { $newFileName .= substr($oldFileName, $dotpos); } // Note: still possible: 'xx.yy' -rename-> '.yy' -rename-> 'zz' // This is useful for folder names, where otherwise '.' would be sticky // Extension PHP is not allowed, change to PHPS $newFileName = php2phps($newFileName); if ($newFileName == $oldFileName) return $oldFileName; if (strtolower($newFileName) != strtolower($oldFileName) && check_name_exist($path."/".$newFileName)) return false; // On a Windows server, it would be better not to do the above check // because it succeeds for some new names resembling the old name. // But on Unix/Linux the check must be done because rename overwrites. chdir($path); $res = rename($oldFileName, $newFileName) ? $newFileName : false; chdir($save_dir); return $res;}//------------------------------------------------------------------------------/** * Move a file or a directory to an other area * * @author - Hugues Peeters <peeters@ipm.ucl.ac.be> * @param - $source (String) - the path of file or directory to move * @param - $target (String) - the path of the new area * @return - bolean - true if the move succeed * bolean - false otherwise. * @see - move() uses check_name_exist() and copyDirTo() functions */function move($source, $target){ if ( check_name_exist($source) ) { $fileName = basename($source); if ( check_name_exist($target."/".$fileName) ) { return false; } else { /* File case */ if ( is_file($source) ) { copy($source , $target."/".$fileName); unlink($source); return true; } /* Directory case */ elseif (is_dir($source)) { // check to not copy the directory inside itself if (ereg("^".$source."/", $target."/")) { return false; } else { copyDirTo($source, $target); return true; } } } } else { return false; }}//------------------------------------------------------------------------------/** * Move a directory and its content to an other area * * @author - Hugues Peeters <peeters@ipm.ucl.ac.be> * @param - $origDirPath (String) - the path of the directory to move * @param - $destination (String) - the path of the new area * @return - no return !! */function copyDirTo($origDirPath, $destination, $move=true){ $save_dir=getcwd(); // extract directory name - create it at destination - update destination trail $dirName = basename($origDirPath); mkdir ($destination."/".$dirName, 0775); $destinationTrail = $destination."/".$dirName; chdir ($origDirPath) ; $handle = opendir($origDirPath); while ($element = readdir($handle) ) { if ( $element == "." || $element == "..") { continue; // skip the current and parent directories } elseif ( is_file($element) ) { copy($element, $destinationTrail."/".$element); if($move) { unlink($element) ; } } elseif ( is_dir($element) ) { $dirToCopy[] = $origDirPath."/".$element; } } closedir($handle) ; if ( sizeof($dirToCopy) > 0) { foreach($dirToCopy as $thisDir) { copyDirTo($thisDir, $destinationTrail, $move); // recursivity } } if($move) { rmdir ($origDirPath) ; } chdir($save_dir);}//------------------------------------------------------------------------------/* NOTE: These functions batch is used to automatically build HTML forms * with a list of the directories contained on the course Directory. * * From a thechnical point of view, form_dir_lists calls sort_dir wich calls index_dir *//** * Indexes all the directories and subdirectories * contented in a given directory * * @author - Hugues Peeters <peeters@ipm.ucl.ac.be> * @param - path (string) - directory path of the one to index * @return - an array containing the path of all the subdirectories */function index_dir($path){ $save_dir = getcwd(); chdir($path); $handle = opendir($path); // reads directory content end record subdirectoies names in $dir_array while ($element = readdir($handle) ) { if ( $element == "." || $element == "..") continue; // skip the current and parent directories if ( is_dir($element) ) $dirArray[] = $path."/".$element; } closedir($handle) ; // recursive operation if subdirectories exist $dirNumber = sizeof($dirArray); if ( $dirNumber > 0 ) { for ($i = 0 ; $i < $dirNumber ; $i++ ) { $subDirArray = index_dir( $dirArray[$i] ) ; // function recursivity $dirArray = array_merge( (array)$dirArray , (array)$subDirArray ); // data merge } } chdir($save_dir) ; return $dirArray ;}/** * Indexes all the directories and subdirectories * contented in a given directory, and sort them alphabetically * * @author - Hugues Peeters <peeters@ipm.ucl.ac.be> * @param - path (string) - directory path of the one to index * @return - an array containing the path of all the subdirectories sorted * false, if there is no directory * @see - index_and_sort_dir uses the index_dir() function */function index_and_sort_dir($path){ $dir_list = index_dir($path); if ($dir_list) { sort($dir_list); return $dir_list; } else { return false; }}/** * build an html form listing all directories of a given directory * */function form_dir_list($sourceType, $sourceComponent, $command, $baseWorkDir){ $dirList = index_and_sort_dir($baseWorkDir); $dialogBox .= "<form action=\"".api_get_self()."\" method=\"post\">\n" ; $dialogBox .= "<input type=\"hidden\" name=\"".$sourceType."\" value=\"".$sourceComponent."\">\n" ; $dialogBox .= get_lang('Move').' '.$sourceComponent.' '.get_lang('To'); $dialogBox .= "<select name=\"".$command."\">\n" ; $dialogBox .= "<option value=\"\" style=\"color:#999999\">".get_lang('Root')."\n"; $bwdLen = strlen($baseWorkDir) ; // base directories lenght, used under /* build html form inputs */ if ($dirList) { while (list( , $pathValue) = each($dirList) ) { $pathValue = substr ( $pathValue , $bwdLen ); // truncate cunfidential informations confidentielles $dirname = basename ($pathValue); // extract $pathValue directory name du nom /* compute de the display tab */ $tab = ""; // $tab reinitialisation $depth = substr_count($pathValue, "/"); // The number of nombre '/' indicates the directory deepness for ($h=0; $h<$depth; $h++) { $tab .= "  "; } $dialogBox .= "<option value=\"$pathValue\">$tab>$dirname\n"; } } $dialogBox .= "</select>\n"; $dialogBox .= "<input type=\"submit\" value=\"".get_lang('Ok')."\">"; $dialogBox .= "</form>\n"; return $dialogBox;}//------------------------------------------------------------------------------/** * to create missing directory in a gived path * * @returns a resource identifier or FALSE if the query was not executed correctly. * @author KilerCris@Mail.com original function from php manual * @author Christophe Gesch锟
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -