123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672 |
- <?php
- function update_db_info($action, $old_path, $new_path = '') {
- global $dbTable;
-
- if ($action == 'delete') {
-
- $old_path = Database::escape_string($old_path);
- $to_delete = "WHERE path LIKE BINARY '".$old_path."' OR path LIKE BINARY '".$old_path."/%'";
- $query = "DELETE FROM $dbTable " . $to_delete;
- $result = Database::query("SELECT id FROM $dbTable " . $to_delete);
- if (Database::num_rows($result)) {
- require_once api_get_path(INCLUDE_PATH).'../metadata/md_funcs.php';
- $mdStore = new mdstore(TRUE);
- $md_type = (substr($dbTable, -13) == 'scormdocument') ? 'Scorm' : 'Document';
- while ($row = Database::fetch_array($result)) {
- $eid = $md_type . '.' . $row['id'];
- $mdStore->mds_delete($eid);
- $mdStore->mds_delete_offspring($eid);
- }
- }
- }
-
- if ($action == 'update') {
-
- if ($new_path[0] == '.') $new_path = substr($new_path, 1);
- $new_path = str_replace('//', '/', $new_path);
-
-
-
-
-
-
-
- $new_path = Database::escape_string($new_path);
- $query = "UPDATE $dbTable
- SET path = CONCAT('".$new_path."', SUBSTRING(path, LENGTH('".$old_path."')+1) )
- WHERE path LIKE BINARY '".$old_path."' OR path LIKE BINARY '".$old_path."/%'";
- }
-
-
- Database::query($query);
-
- }
- function check_name_exist($file_path) {
- clearstatcache();
- $save_dir = getcwd();
- if (!is_dir(dirname($file_path))) {
- return false;
- }
- chdir(dirname($file_path));
- $file_name = basename($file_path);
- if (file_exists($file_name)) {
- chdir($save_dir);
- return true;
- } else {
- chdir($save_dir);
- return false;
- }
- }
- function my_delete($file) {
- if (check_name_exist($file)) {
- if (is_file($file)) {
- unlink($file);
- return true;
- } elseif (is_dir($file)) {
- removeDir($file);
- return true;
- }
- } else {
- return false;
- }
- }
- 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;
- }
- function my_rename($file_path, $new_file_name) {
- $save_dir = getcwd();
- $path = dirname($file_path);
- $old_file_name = basename($file_path);
- $new_file_name = replace_dangerous_char($new_file_name);
-
- if ((strpos($new_file_name, '.') === false) && ($dotpos = strrpos($old_file_name, '.'))) {
- $new_file_name .= substr($old_file_name, $dotpos);
- }
-
-
-
- $new_file_name = php2phps($new_file_name);
- if ($new_file_name == $old_file_name) {
- return $old_file_name;
- }
- if (strtolower($new_file_name) != strtolower($old_file_name) && check_name_exist($path.'/'.$new_file_name)) {
- return false;
- }
-
-
-
- chdir($path);
- $res = rename($old_file_name, $new_file_name) ? $new_file_name : false;
- chdir($save_dir);
- return $res;
- }
- function move($source, $target) {
- if (check_name_exist($source)) {
- $file_name = basename($source);
- if (check_name_exist($target.'/'.$file_name)) {
- return false;
- } else {
-
- if (is_file($source)) {
- copy($source , $target.'/'.$file_name);
- unlink($source);
- return true;
- }
-
- elseif (is_dir($source)) {
-
- if (ereg('^'.$source.'/', $target.'/')) {
- return false;
- } else {
- copyDirTo($source, $target);
- return true;
- }
- }
- }
- } else {
- return false;
- }
- }
- function copyDirTo($orig_dir_path, $destination, $move = true) {
- $save_dir = getcwd();
-
- $dir_name = basename($orig_dir_path);
- if (is_dir($orig_dir_path)) {
- mkdir($destination.'/'.$dir_name, api_get_permissions_for_new_directories());
- $destination_trail = $destination.'/'.$dir_name;
- if (is_dir($destination)) {
- chdir ($orig_dir_path) ;
- $handle = opendir($orig_dir_path);
- while ($element = readdir($handle)) {
- if ($element == '.' || $element == '..') {
- continue;
- } elseif (is_file($element)) {
- copy($element, $destination_trail.'/'.$element);
- if ($move) {
- unlink($element) ;
- }
- } elseif (is_dir($element)) {
- $dir_to_copy[] = $orig_dir_path.'/'.$element;
- }
- }
- closedir($handle) ;
- if (sizeof($dir_to_copy) > 0) {
- foreach ($dir_to_copy as $this_dir) {
- copyDirTo($this_dir, $destination_trail, $move);
- }
- }
- if ($move) {
- rmdir($orig_dir_path) ;
- }
- chdir($save_dir);
- }
- }
- }
- function index_dir($path) {
- $dir_array = array();
- $save_dir = getcwd();
- if (is_dir($path)){
- chdir($path);
- $handle = opendir($path);
-
- if ($handle !== false) {
- while ($element = readdir($handle)) {
- if ($element == '.' || $element == '..') continue;
- if (is_dir($element)) $dir_array[] = $path.'/'.$element;
- }
- closedir($handle) ;
- }
-
- $dir_number = sizeof($dir_array);
- if ($dir_number > 0) {
- for ($i = 0 ; $i < $dir_number ; $i++) {
- $sub_dir_array = index_dir($dir_array[$i]);
- $dir_array = array_merge((array)$dir_array, (array)$sub_dir_array);
- }
- }
- }
- chdir($save_dir) ;
- return $dir_array ;
- }
- function index_and_sort_dir($path) {
- $dir_list = index_dir($path);
- if ($dir_list) {
- natsort($dir_list);
- return $dir_list;
- }
- return false;
- }
- function form_dir_list($source_type, $source_component, $command, $base_work_dir) {
- $dir_list = index_and_sort_dir($base_work_dir);
- $dialog_box .= "<form action=\"".api_get_self()."\" method=\"post\">\n" ;
- $dialog_box .= "<input type=\"hidden\" name=\"".$source_type."\" value=\"".$source_component."\">\n" ;
- $dialog_box .= get_lang('Move').' '.$source_component.' '.get_lang('To');
- $dialog_box .= "<select name=\"".$command."\">\n" ;
- $dialog_box .= "<option value=\"\" style=\"color:#999999\">".get_lang('Root')."\n";
- $bwdLen = strlen($base_work_dir) ;
-
- if ($dir_list) {
- while (list( , $path_value) = each($dir_list) ) {
- $path_value = substr ( $path_value , $bwdLen );
- $dirname = basename ($path_value);
-
- $tab = "";
- $depth = substr_count($path_value, '/');
- for ($h = 0; $h < $depth; $h++) {
- $tab .= " ";
- }
- $dialog_box .= "<option value=\"$path_value\">$tab>$dirname\n";
- }
- }
- $dialog_box .= "</select>\n";
- $dialog_box .= "<input type=\"submit\" value=\"".get_lang('Ok')."\">";
- $dialog_box .= "</form>\n";
- return $dialog_box;
- }
- function getextension($filename) {
- $bouts = explode('.', $filename);
- return array(array_pop($bouts), implode('.', $bouts));
- }
- function dirsize($root, $recursive = true) {
- $dir = @opendir($root);
- $size = 0;
- while ($file = @readdir($dir)) {
- if (!in_array($file, array('.', '..'))) {
- if (is_dir($root.'/'.$file)) {
- $size += $recursive ? dirsize($root.'/'.$file) : 0;
- } else {
- $size += @filesize($root.'/'.$file);
- }
- }
- }
- @closedir($dir);
- return $size;
- }
- class FileManager
- {
-
- function list_all_directories($path) {
- $result_array = array();
- if (is_dir($path)) {
- $save_dir = getcwd();
- chdir($path);
- $handle = opendir($path);
- while ($element = readdir($handle)) {
- if ($element == '.' || $element == '..') continue;
- if (is_dir($element)) {
- $dir_array[] = $path.'/'.$element;
- }
- }
- closedir($handle);
-
- $dir_number = sizeof($dir_array);
- if ($dir_number > 0) {
- for ($i = 0 ; $i < $dir_number ; $i++) {
- $sub_dir_array = FileManager::list_all_directories($dir_array[$i]);
- if (is_array($dir_array) && is_array($sub_dir_array)) {
- $dir_array = array_merge($dir_array, $sub_dir_array);
- }
- }
- }
- $result_array = $dir_array;
- chdir($save_dir) ;
- }
- return $result_array ;
- }
-
- function list_all_files($dir_array) {
- $element_array = array();
- if (is_dir($dir_array)) {
- $save_dir = getcwd();
- foreach ($dir_array as $directory) {
- chdir($directory);
- $handle = opendir($directory);
- while ($element = readdir($handle)) {
- if ($element == '.' || $element == '..' || $element == '.htaccess') continue;
- if (!is_dir($element)) {
- $element_array[] = $directory.'/'.$element;
- }
- }
- closedir($handle);
- chdir('..');
- chdir($save_dir);
- }
- }
- return $element_array;
- }
-
- function compat_load_file($file_name) {
- $buffer = '';
- if (file_exists($file_name)) {
- $fp = fopen($file_name, 'rb');
- $buffer = fread ($fp, filesize($file_name));
- fclose ($fp);
- }
- return $buffer;
- }
-
- function set_default_settings($upload_path, $filename, $filetype = 'file', $glued_table, $default_visibility = 'v') {
- if (!$default_visibility) $default_visibility = 'v';
-
- $upload_path = !empty($upload_path) ? "/$upload_path" : '';
- $endchar = substr($filename, strlen($filename) - 1, 1);
- if ($endchar == "\\" || $endchar == '/') {
- $filename = substr($filename, 0, strlen($filename) - 1);
- }
- $full_file_name = $upload_path.'/'.$filename;
-
- $full_file_name = str_replace("//", '/', $full_file_name);
- $sql_query = "SELECT count(*) as number_existing FROM $glued_table WHERE path='$full_file_name'";
- $sql_result = Database::query($sql_query);
- $result = Database::fetch_array($sql_result);
-
- if ($result['number_existing'] > 0) {
-
- $query = "UPDATE $glued_table SET path='$full_file_name',visibility='$default_visibility', filetype='$filetype' WHERE path='$full_file_name'";
- } else {
-
- $query = "INSERT INTO $glued_table (path,visibility,filetype) VALUES('$full_file_name','$default_visibility','$filetype')";
- }
- Database::query($query);
- }
- }
- function mkdirs($path, $mode = '0770') {
- if (file_exists($path)) {
- return false;
- } else {
- FileManager :: mkdirs(dirname($path), $mode);
-
- return true;
- }
- }
- function mkpath($path, $verbose = false, $mode = 'herit') {
- global $langCreatedIn, $_configuration;
- $path = str_replace('/', "\\", $path);
- $dirs = explode("\\", $path);
- $path = $dirs[0];
- if ($verbose) {
- echo '<ul>';
- }
- for ($i = 1; $i < sizeof($dirs); $i++) {
- $path .= '/'.$dirs[$i];
- if (ereg('^'.$path, $_configuration['root_sys']) && strlen($path) < strlen($_configuration['root_sys'])) {
- continue;
- }
- if (!is_dir($path)) {
- $ret = mkdir($path, api_get_permissions_for_new_directories());
- if ($ret) {
- if ($verbose) {
- echo '<li><strong>'.basename($path).'</strong><br />'.$langCreatedIn.'<br /><strong>'.realpath($path.'/..').'</strong></li>';
- }
- } else {
- if ($verbose) {
- echo '</ul>error : '.$path.' not created';
- }
- $ret = false;
- break;
- }
- }
- }
- if ($verbose) {
- echo '</ul>';
- }
- return $ret;
- }
|