123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819 |
- <?php
- function update_db_info($action, $oldPath, $newPath="")
- {
- global $dbTable;
-
- if ($action == "delete")
- {
-
- $to_delete = "WHERE path='".$oldPath."' OR path LIKE '".$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);
- $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);
- }
- }
- }
-
- if ($action == "update")
- {
-
- if ($newPath[0] == ".") $newPath = substr($newPath,1);
- $newPath = str_replace('//','/',$newPath);
-
-
-
-
-
-
-
- $query = "UPDATE $dbTable
- SET path = CONCAT('".$newPath."', SUBSTRING(path, LENGTH('".$oldPath."')+1) )
- WHERE path = '".$oldPath."' OR path LIKE '".$oldPath."/%'";
- }
-
-
- api_sql_query($query,__FILE__,__LINE__);
-
- }
- function check_name_exist($filePath)
- {
- clearstatcache();
- $save_dir = getcwd();
- chdir ( dirname($filePath) );
- $fileName = basename ($filePath);
- if (file_exists( $fileName ))
- {
- 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($filePath, $newFileName)
- {
- $save_dir = getcwd();
- $path = dirname($filePath);
- $oldFileName = basename($filePath);
- $newFileName = replace_dangerous_char($newFileName);
-
- if ((strpos($newFileName, '.') === FALSE)
- && ($dotpos = strrpos($oldFileName, '.')))
- {
- $newFileName .= substr($oldFileName, $dotpos);
- }
-
-
-
- $newFileName = php2phps($newFileName);
- if ($newFileName == $oldFileName) return $oldFileName;
- if (strtolower($newFileName) != strtolower($oldFileName) && check_name_exist($path."/".$newFileName)) return false;
-
-
-
- chdir($path);
- $res = rename($oldFileName, $newFileName) ? $newFileName : false;
- chdir($save_dir);
- return $res;
- }
- function move($source, $target)
- {
- if ( check_name_exist($source) )
- {
- $fileName = basename($source);
- if ( check_name_exist($target."/".$fileName) )
- {
- return false;
- }
- else
- {
- if ( is_file($source) )
- {
- copy($source , $target."/".$fileName);
- 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($origDirPath, $destination, $move=true)
- {
- $save_dir=getcwd();
-
- $dirName = basename($origDirPath);
- mkdir ($destination."/".$dirName, 0775);
- $destinationTrail = $destination."/".$dirName;
- chdir ($origDirPath) ;
- $handle = opendir($origDirPath);
- while ($element = readdir($handle) )
- {
- if ( $element == "." || $element == "..")
- {
- continue;
- }
- 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);
- }
- }
- if($move)
- {
- rmdir ($origDirPath) ;
- }
- chdir($save_dir);
- }
- function index_dir($path)
- {
- $save_dir = getcwd();
- chdir($path);
- $handle = opendir($path);
-
- while ($element = readdir($handle) )
- {
- if ( $element == "." || $element == "..") continue;
- if ( is_dir($element) ) $dirArray[] = $path."/".$element;
- }
- closedir($handle) ;
-
- $dirNumber = sizeof($dirArray);
- if ( $dirNumber > 0 )
- {
- for ($i = 0 ; $i < $dirNumber ; $i++ )
- {
- $subDirArray = index_dir( $dirArray[$i] ) ;
- $dirArray = array_merge( (array)$dirArray , (array)$subDirArray );
- }
- }
- chdir($save_dir) ;
- return $dirArray ;
- }
- function index_and_sort_dir($path)
- {
- $dir_list = index_dir($path);
- if ($dir_list)
- {
- sort($dir_list);
- return $dir_list;
- }
- else
- {
- return false;
- }
- }
- function form_dir_list($sourceType, $sourceComponent, $command, $baseWorkDir)
- {
- $dirList = index_and_sort_dir($baseWorkDir);
- $dialogBox .= "<form action=\"".$_SERVER['PHP_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) ;
-
- if ($dirList)
- {
- while (list( , $pathValue) = each($dirList) )
- {
- $pathValue = substr ( $pathValue , $bwdLen );
- $dirname = basename ($pathValue);
-
- $tab = "";
- $depth = substr_count($pathValue, "/");
- 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;
- }
- function mkpath($path, $verbose = false, $mode = "herit")
- {
- global $langCreatedIn, $rootSys;
- $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,$rootSys) && strlen($path) < strlen($rootSys))
- {
- continue;
- }
- if(!is_dir($path))
- {
- $ret=mkdir($path,0770);
- 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;
- }
- 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)
- {
- $save_dir = getcwd();
- chdir($path);
- $handle = opendir($path);
- while ($element = readdir($handle) )
- {
- if ( $element == "." || $element == "..") continue;
- if ( is_dir($element) )
- {
- $dirArray[] = $path."/".$element;
- }
- }
- closedir($handle) ;
-
- $dirNumber = sizeof($dirArray);
- if ( $dirNumber > 0 )
- {
- for ($i = 0 ; $i < $dirNumber ; $i++ )
- {
- $subDirArray = FileManager::list_all_directories( $dirArray[$i] ) ;
- $dirArray = array_merge( $dirArray , $subDirArray ) ;
- }
- }
- $resultArray = $dirArray;
- chdir($save_dir) ;
- return $resultArray ;
- }
-
- function list_all_files($dirArray)
- {
- $save_dir = getcwd();
- foreach ($dirArray as $directory)
- {
- chdir($directory);
- $handle = opendir($directory);
- while ($element = readdir($handle) )
- {
- if ( $element == "." || $element == ".." || $element == '.htaccess') continue;
- if ( ! is_dir($element) )
- {
- $elementArray[] = $directory."/".$element;
- }
- }
- closedir($handle) ;
- chdir("..") ;
- }
- chdir($save_dir);
- return $elementArray;
- }
-
- function compat_load_file($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";
-
- if( strlen($upload_path) ) $upload_path = "/$upload_path";
- else $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 = api_sql_query($sql_query,__FILE__,__LINE__);
- $result = mysql_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')";
- }
- api_sql_query($query,__FILE__,__LINE__);
- }
-
- function mkdirs($path, $mode = '0777')
- {
- if (file_exists($path))
- {
- return false;
- }
- else
- {
- FileManager :: mkdirs(dirname($path), $mode);
- return mkdir($path, $mode);
- }
- }
- }
- ?>
|