123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124 |
- <?php
- class FileManager
- {
-
- static function update_db_info($action, $old_path, $new_path = '')
- {
- $dbTable = Database::get_course_table(TABLE_DOCUMENT);
- $course_id = api_get_course_int_id();
-
- if ($action == 'delete') {
- $old_path = Database::escape_string($old_path);
- $to_delete = "WHERE c_id = $course_id AND 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 c_id = $course_id AND path LIKE BINARY '".$old_path."' OR path LIKE BINARY '".$old_path."/%'";
- }
- Database::query($query);
- }
-
- static 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;
- }
- }
-
- static function my_delete($file)
- {
- if (self::check_name_exist($file)) {
- if (is_file($file)) {
- unlink($file);
- return true;
- } elseif (is_dir($file)) {
- self::removeDir($file);
- return true;
- }
- } else {
- return false;
- }
- }
-
- static 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 (!self::removeDir($dir.'/'.$readdir)) {
- return false;
- }
- }
- }
- }
- closedir($opendir);
- if (!@rmdir($dir)) {
- return false;
- }
- return true;
- }
-
- static function folder_is_empty($in_folder)
- {
- $tab_folder_content = scandir($in_folder);
- $folder_is_empty = 0;
- if ((count($tab_folder_content) == 2 && in_array(".", $tab_folder_content) && in_array(
- "..",
- $tab_folder_content
- )) || (count($tab_folder_content) < 2)
- ) {
- $folder_is_empty = 1;
- }
- return $folder_is_empty;
- }
-
- static 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 = self::php2phps($new_file_name);
- if ($new_file_name == $old_file_name) {
- return $old_file_name;
- }
- if (strtolower($new_file_name) != strtolower($old_file_name) && self::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;
- }
-
- static function move($source, $target)
- {
- if (self::check_name_exist($source)) {
- $file_name = basename($source);
- if (self::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 {
- self::copyDirTo($source, $target);
- return true;
- }
- }
- }
- } else {
- return false;
- }
- }
-
- static 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) {
- self::copyDirTo($this_dir, $destination_trail, $move);
- }
- }
- if ($move) {
- rmdir($orig_dir_path);
- }
- chdir($save_dir);
- }
- }
- }
-
-
- static 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 = self::index_dir($dir_array[$i]);
- $dir_array = array_merge((array)$dir_array, (array)$sub_dir_array);
- }
- }
- }
- chdir($save_dir);
- return $dir_array;
- }
-
- static function index_and_sort_dir($path)
- {
- $dir_list = self::index_dir($path);
- if ($dir_list) {
- natsort($dir_list);
- return $dir_list;
- }
- return false;
- }
-
- static function getextension($filename)
- {
- $bouts = explode('.', $filename);
- return array(array_pop($bouts), implode('.', $bouts));
- }
-
- static 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;
- }
-
- static 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 = self::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;
- }
-
- static 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;
- }
- static function choose_image($file_name)
- {
- static $type, $image;
-
- if (!$type || !$image) {
- $type['word'] = array('doc', 'dot', 'rtf', 'mcw', 'wps', 'psw', 'docm', 'docx', 'dotm', 'dotx');
- $type['web'] = array('htm', 'html', 'htx', 'xml', 'xsl', 'php', 'xhtml');
- $type['image'] = array('gif', 'jpg', 'png', 'bmp', 'jpeg', 'tif', 'tiff');
- $type['image_vect'] = array('svg', 'svgz');
- $type['audio'] = array('wav', 'mid', 'mp2', 'mp3', 'midi', 'sib', 'amr', 'kar', 'oga', 'au', 'wma');
- $type['video'] = array(
- 'mp4',
- 'mov',
- 'rm',
- 'pls',
- 'mpg',
- 'mpeg',
- 'm2v',
- 'm4v',
- 'flv',
- 'f4v',
- 'avi',
- 'wmv',
- 'asf',
- '3gp',
- 'ogv',
- 'ogg',
- 'ogx',
- 'webm'
- );
- $type['excel'] = array('xls', 'xlt', 'xls', 'xlt', 'pxl', 'xlsx', 'xlsm', 'xlam', 'xlsb', 'xltm', 'xltx');
- $type['compressed'] = array('zip', 'tar', 'rar', 'gz');
- $type['code'] = array('js', 'cpp', 'c', 'java', 'phps', 'jsp', 'asp', 'aspx', 'cfm');
- $type['acrobat'] = array('pdf');
- $type['powerpoint'] = array('ppt', 'pps', 'pptm', 'pptx', 'potm', 'potx', 'ppam', 'ppsm', 'ppsx');
- $type['flash'] = array('fla', 'swf');
- $type['text'] = array('txt', 'log');
- $type['oo_writer'] = array('odt', 'ott', 'sxw', 'stw');
- $type['oo_calc'] = array('ods', 'ots', 'sxc', 'stc');
- $type['oo_impress'] = array('odp', 'otp', 'sxi', 'sti');
- $type['oo_draw'] = array('odg', 'otg', 'sxd', 'std');
- $type['epub'] = array('epub');
- $type['java'] = array('class', 'jar');
- $type['freemind'] = array('mm');
- $image['word'] = 'word.gif';
- $image['web'] = 'file_html.gif';
- $image['image'] = 'file_image.gif';
- $image['image_vect'] = 'file_svg.png';
- $image['audio'] = 'file_sound.gif';
- $image['video'] = 'film.gif';
- $image['excel'] = 'excel.gif';
- $image['compressed'] = 'file_zip.gif';
- $image['code'] = 'icons/22/mime_code.png';
- $image['acrobat'] = 'file_pdf.gif';
- $image['powerpoint'] = 'powerpoint.gif';
- $image['flash'] = 'file_flash.gif';
- $image['text'] = 'icons/22/mime_text.png';
- $image['oo_writer'] = 'file_oo_writer.gif';
- $image['oo_calc'] = 'file_oo_calc.gif';
- $image['oo_impress'] = 'file_oo_impress.gif';
- $image['oo_draw'] = 'file_oo_draw.gif';
- $image['epub'] = 'file_epub.gif';
- $image['java'] = 'file_java.png';
- $image['freemind'] = 'file_freemind.png';
- }
-
- $extension = array();
- if (!is_array($file_name)) {
- if (preg_match('/\.([[:alnum:]]+)(\?|$)/', $file_name, $extension)) {
- $extension[1] = strtolower($extension[1]);
- foreach ($type as $generic_type => $extension_list) {
- if (in_array($extension[1], $extension_list)) {
- return $image[$generic_type];
- }
- }
- }
- }
- return 'defaut.gif';
- }
-
- static function format_url($file_path)
- {
- $path_component = explode('/', $file_path);
- $path_component = array_map('rawurlencode', $path_component);
- return implode('/', $path_component);
- }
-
- static function get_total_folder_size($path, $can_see_invisible = false)
- {
- $table_itemproperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $table_document = Database::get_course_table(TABLE_DOCUMENT);
- $tool_document = TOOL_DOCUMENT;
- $course_id = api_get_course_int_id();
- $session_id = api_get_session_id();
- $session_condition = api_get_session_condition($session_id, true, true, 'props.id_session');
- $visibility_rule = ' props.visibility '.($can_see_invisible ? '<> 2' : '= 1');
- $sql = "SELECT SUM(table1.size) FROM (
- SELECT size FROM $table_itemproperty AS props, $table_document AS docs
- WHERE docs.c_id = $course_id AND
- docs.id = props.ref AND
- docs.path LIKE '$path/%' AND
- props.c_id = $course_id AND
- props.tool = '$tool_document' AND
- $visibility_rule
- $session_condition
- GROUP BY ref
- ) as table1";
- $result = Database::query($sql);
- if ($result && Database::num_rows($result) != 0) {
- $row = Database::fetch_row($result);
- return $row[0] == null ? 0 : $row[0];
- } else {
- return 0;
- }
- }
-
- static function php2phps($file_name)
- {
- return preg_replace('/\.(php.?|phtml.?)(\.){0,1}.*$/i', '.phps', $file_name);
- }
-
- static function htaccess2txt($filename)
- {
- return str_replace(array('.htaccess', '.HTACCESS'), array('htaccess.txt', 'htaccess.txt'), $filename);
- }
-
- static function disable_dangerous_file($filename)
- {
- return self::htaccess2txt(self::php2phps($filename));
- }
-
- static function unique_name($path, $name)
- {
- $ext = substr(strrchr($name, '.'), 0);
- $name_no_ext = substr($name, 0, strlen($name) - strlen(strstr($name, $ext)));
- $n = 0;
- $unique = '';
- while (file_exists($path.$name_no_ext.$unique.$ext)) {
- $unique = '_'.++$n;
- }
- return $name_no_ext.$unique.$ext;
- }
-
- static function get_document_title($name)
- {
-
- $name = self::disable_dangerous_file($name);
- $ext = substr(strrchr($name, '.'), 0);
- return addslashes(substr($name, 0, strlen($name) - strlen(strstr($name, $ext))));
- }
-
- static function process_uploaded_file($uploaded_file, $show_output = true)
- {
-
- switch ($uploaded_file['error']) {
- case 1:
-
- if ($show_output) {
- Display::display_error_message(
- get_lang('UplExceedMaxServerUpload').ini_get('upload_max_filesize')
- );
- }
- return false;
- case 2:
-
-
- $max_file_size = intval($_POST['MAX_FILE_SIZE']);
- if ($show_output) {
- Display::display_error_message(get_lang('UplExceedMaxPostSize').Text::format_file_size($max_file_size));
- }
- return false;
- case 3:
-
- if ($show_output) {
- Display::display_error_message(get_lang('UplPartialUpload').' '.get_lang('PleaseTryAgain'));
- }
- return false;
- case 4:
-
- if ($show_output) {
- Display::display_error_message(get_lang('UplNoFileUploaded').' '.get_lang('UplSelectFileFirst'));
- }
- return false;
- }
- if (!file_exists($uploaded_file['tmp_name'])) {
-
- if ($show_output) {
- Display::display_error_message(get_lang('UplUploadFailed'));
- }
- return false;
- }
- if (file_exists($uploaded_file['tmp_name'])) {
- $filesize = filesize($uploaded_file['tmp_name']);
- if (empty($filesize)) {
-
- if ($show_output) {
- Display::display_error_message(get_lang('UplUploadFailedSizeIsZero'));
- }
- return false;
- }
- }
- $course_id = api_get_course_id();
-
- if (!empty($course_id)) {
- $max_filled_space = DocumentManager::get_course_quota();
-
- if (!DocumentManager::enough_space($uploaded_file['size'], $max_filled_space)) {
- if ($show_output) {
- Display::display_error_message(get_lang('UplNotEnoughSpace'));
- }
- return false;
- }
- }
-
- return true;
- }
-
- static function handle_uploaded_document(
- $_course,
- $uploaded_file,
- $base_work_dir,
- $upload_path,
- $user_id,
- $to_group_id = 0,
- $to_user_id = null,
- $unzip = 0,
- $what_if_file_exists = '',
- $output = true
- ) {
- if (!$user_id) {
- die('Not a valid user.');
- }
-
- $uploaded_file['name'] = stripslashes($uploaded_file['name']);
-
- $uploaded_file['name'] = self::add_ext_on_mime($uploaded_file['name'], $uploaded_file['type']);
- $current_session_id = api_get_session_id();
-
- $max_filled_space = DocumentManager::get_course_quota();
-
- if (!DocumentManager::enough_space($uploaded_file['size'], $max_filled_space)) {
- if ($output) {
- Display::display_error_message(get_lang('UplNotEnoughSpace'));
- }
- return false;
- }
-
- if ($unzip == 1 && preg_match('/.zip$/', strtolower($uploaded_file['name']))) {
- return self::unzip_uploaded_document(
- $uploaded_file,
- $upload_path,
- $base_work_dir,
- $max_filled_space,
- $output,
- $to_group_id
- );
-
- } elseif ($unzip == 1 && !preg_match(
- '/.zip$/',
- strtolower($uploaded_file['name'])
- )
- ) {
- if ($output) {
- Display::display_error_message(get_lang('UplNotAZip')." ".get_lang('PleaseTryAgain'));
- }
- return false;
- } else {
-
- $clean_name = replace_dangerous_char($uploaded_file['name'], 'strict');
-
- $clean_name = self::disable_dangerous_file($clean_name);
- if (!self::filter_extension($clean_name)) {
- if ($output) {
- Display::display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
- }
- return false;
- } else {
-
-
-
-
- if ($upload_path != '/') {
- $upload_path = $upload_path.'/';
- }
-
- $file_path = $upload_path.$clean_name;
-
-
- $where_to_save = $base_work_dir.$upload_path;
-
- if (!is_dir($where_to_save)) {
- if ($output) {
- Display::display_error_message(get_lang('DestDirectoryDoesntExist').' ('.$upload_path.')');
- }
- return false;
- }
-
-
- $store_path = $where_to_save.$clean_name;
-
-
- $document_name = self::get_document_title($uploaded_file['name']);
-
- $file_size = $uploaded_file['size'];
- $files_perm = api_get_permissions_for_new_files();
-
- switch ($what_if_file_exists) {
-
- case 'overwrite':
-
- $file_exists = file_exists($store_path);
- if (@move_uploaded_file($uploaded_file['tmp_name'], $store_path)) {
- chmod($store_path, $files_perm);
- if ($file_exists) {
-
- $document_id = DocumentManager::get_document_id($_course, $file_path);
- if (is_numeric($document_id)) {
-
- self::update_existing_document($_course, $document_id, $uploaded_file['size']);
-
- api_item_property_update(
- $_course,
- TOOL_DOCUMENT,
- $document_id,
- 'DocumentUpdated',
- $user_id,
- $to_group_id,
- $to_user_id,
- null,
- null,
- $current_session_id
- );
-
- api_set_default_visibility(TOOL_DOCUMENT, $document_id);
- }
-
- self::item_property_update_on_folder($_course, $upload_path, $user_id);
-
- if ($output) {
- Display::display_confirmation_message(
- get_lang('UplUploadSucceeded').'<br />'.$file_path.' '.get_lang(
- 'UplFileOverwritten'
- ),
- false
- );
- }
- return $file_path;
- } else {
-
- $document_id = self::add_document(
- $_course,
- $file_path,
- 'file',
- $file_size,
- $document_name,
- null,
- 0,
- true
- );
- if ($document_id) {
-
- api_item_property_update(
- $_course,
- TOOL_DOCUMENT,
- $document_id,
- 'DocumentAdded',
- $user_id,
- $to_group_id,
- $to_user_id,
- null,
- null,
- $current_session_id
- );
- }
-
- self::item_property_update_on_folder($_course, $upload_path, $user_id);
-
- if ($output) {
- Display::display_confirmation_message(
- get_lang('UplUploadSucceeded').'<br />'.$file_path,
- false
- );
- }
- return $file_path;
- }
- } else {
- if ($output) {
- Display::display_error_message(get_lang('UplUnableToSaveFile'));
- }
- return false;
- }
- break;
-
- case 'rename':
- $new_name = unique_name($where_to_save, $clean_name);
- $store_path = $where_to_save.$new_name;
- $new_file_path = $upload_path.$new_name;
- if (@move_uploaded_file($uploaded_file['tmp_name'], $store_path)) {
- chmod($store_path, $files_perm);
-
- $document_id = self::add_document(
- $_course,
- $new_file_path,
- 'file',
- $file_size,
- $document_name,
- null,
- 0,
- true
- );
- if ($document_id) {
-
- api_item_property_update(
- $_course,
- TOOL_DOCUMENT,
- $document_id,
- 'DocumentAdded',
- $user_id,
- $to_group_id,
- $to_user_id,
- null,
- null,
- $current_session_id
- );
- }
-
- self::item_property_update_on_folder($_course, $upload_path, $user_id);
-
- if ($output) {
- Display::display_confirmation_message(
- get_lang('UplUploadSucceeded').'<br />'.get_lang('UplFileSavedAs').$new_file_path,
- false
- );
- }
- return $new_file_path;
- } else {
- if ($output) {
- Display::display_error_message(get_lang('UplUnableToSaveFile'));
- }
- return false;
- }
- break;
-
- default:
- if (file_exists($store_path)) {
- if ($output) {
- Display::display_error_message($clean_name.' '.get_lang('UplAlreadyExists'));
- }
- } else {
- if (@move_uploaded_file($uploaded_file['tmp_name'], $store_path)) {
- chmod($store_path, $files_perm);
-
- $document_id = self::add_document(
- $_course,
- $file_path,
- 'file',
- $file_size,
- $document_name,
- null,
- 0,
- true
- );
- if ($document_id) {
-
- api_item_property_update(
- $_course,
- TOOL_DOCUMENT,
- $document_id,
- 'DocumentAdded',
- $user_id,
- $to_group_id,
- $to_user_id,
- null,
- null,
- $current_session_id
- );
- }
-
- self::item_property_update_on_folder($_course, $upload_path, $user_id);
-
- if ($output) {
- Display::display_confirmation_message(
- get_lang('UplUploadSucceeded').'<br />'.$file_path,
- false
- );
- }
- return $file_path;
- } else {
- if ($output) {
- Display::display_error_message(get_lang('UplUnableToSaveFile'));
- }
- return false;
- }
- }
- break;
- }
- }
- }
- }
-
- static function enough_size($file_size, $dir, $max_dir_space)
- {
- if ($max_dir_space) {
- $already_filled_space = self::dir_total_space($dir);
- if (($file_size + $already_filled_space) > $max_dir_space) {
- return false;
- }
- }
- return true;
- }
-
- static function dir_total_space($dir_path)
- {
- $save_dir = getcwd();
- chdir($dir_path);
- $handle = opendir($dir_path);
- while ($element = readdir($handle)) {
- if ($element == '.' || $element == '..') {
- continue;
- }
- if (is_file($element)) {
- $sumSize += filesize($element);
- }
- if (is_dir($element)) {
- $dirList[] = $dir_path.'/'.$element;
- }
- }
- closedir($handle);
- if (sizeof($dirList) > 0) {
- foreach ($dirList as $j) {
- $sizeDir = self::dir_total_space($j);
- $sumSize += $sizeDir;
- }
- }
- chdir($save_dir);
- return $sumSize;
- }
-
- static function add_ext_on_mime($file_name, $file_type)
- {
-
- if (!preg_match('/^.*\.[a-zA-Z_0-9]+$/', $file_name) && $file_type) {
-
- static $mime_type = array();
- $mime_type[] = 'application/msword';
- $extension[] = '.doc';
- $mime_type[] = 'application/rtf';
- $extension[] = '.rtf';
- $mime_type[] = 'application/vnd.ms-powerpoint';
- $extension[] = '.ppt';
- $mime_type[] = 'application/vnd.ms-excel';
- $extension[] = '.xls';
- $mime_type[] = 'application/pdf';
- $extension[] = '.pdf';
- $mime_type[] = 'application/postscript';
- $extension[] = '.ps';
- $mime_type[] = 'application/mac-binhex40';
- $extension[] = '.hqx';
- $mime_type[] = 'application/x-gzip';
- $extension[] = 'tar.gz';
- $mime_type[] = 'application/x-shockwave-flash';
- $extension[] = '.swf';
- $mime_type[] = 'application/x-stuffit';
- $extension[] = '.sit';
- $mime_type[] = 'application/x-tar';
- $extension[] = '.tar';
- $mime_type[] = 'application/zip';
- $extension[] = '.zip';
- $mime_type[] = 'application/x-tar';
- $extension[] = '.tar';
- $mime_type[] = 'text/html';
- $extension[] = '.html';
- $mime_type[] = 'text/plain';
- $extension[] = '.txt';
- $mime_type[] = 'text/rtf';
- $extension[] = '.rtf';
- $mime_type[] = 'img/gif';
- $extension[] = '.gif';
- $mime_type[] = 'img/jpeg';
- $extension[] = '.jpg';
- $mime_type[] = 'img/png';
- $extension[] = '.png';
- $mime_type[] = 'audio/midi';
- $extension[] = '.mid';
- $mime_type[] = 'audio/mpeg';
- $extension[] = '.mp3';
- $mime_type[] = 'audio/x-aiff';
- $extension[] = '.aif';
- $mime_type[] = 'audio/x-pn-realaudio';
- $extension[] = '.rm';
- $mime_type[] = 'audio/x-pn-realaudio-plugin';
- $extension[] = '.rpm';
- $mime_type[] = 'audio/x-wav';
- $extension[] = '.wav';
- $mime_type[] = 'video/mpeg';
- $extension[] = '.mpg';
- $mime_type[] = 'video/mpeg4-generic';
- $extension[] = '.mp4';
- $mime_type[] = 'video/quicktime';
- $extension[] = '.mov';
- $mime_type[] = 'video/x-msvideo';
- $extension[] = '.avi';
- $mime_type[] = 'video/x-ms-wmv';
- $extension[] = '.wmv';
- $mime_type[] = 'video/x-flv';
- $extension[] = '.flv';
- $mime_type[] = 'image/svg+xml';
- $extension[] = '.svg';
- $mime_type[] = 'image/svg+xml';
- $extension[] = '.svgz';
- $mime_type[] = 'video/ogg';
- $extension[] = '.ogv';
- $mime_type[] = 'audio/ogg';
- $extension[] = '.oga';
- $mime_type[] = 'application/ogg';
- $extension[] = '.ogg';
- $mime_type[] = 'application/ogg';
- $extension[] = '.ogx';
- $mime_type[] = 'application/x-freemind';
- $extension[] = '.mm';
- $mime_type[] = 'application/vnd.ms-word.document.macroEnabled.12';
- $extension[] = '.docm';
- $mime_type[] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
- $extension[] = '.docx';
- $mime_type[] = 'application/vnd.ms-word.template.macroEnabled.12';
- $extension[] = '.dotm';
- $mime_type[] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template';
- $extension[] = '.dotx';
- $mime_type[] = 'application/vnd.ms-powerpoint.template.macroEnabled.12';
- $extension[] = '.potm';
- $mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.template';
- $extension[] = '.potx';
- $mime_type[] = 'application/vnd.ms-powerpoint.addin.macroEnabled.12';
- $extension[] = '.ppam';
- $mime_type[] = 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12';
- $extension[] = '.ppsm';
- $mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow';
- $extension[] = '.ppsx';
- $mime_type[] = 'application/vnd.ms-powerpoint.presentation.macroEnabled.12';
- $extension[] = '.pptm';
- $mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
- $extension[] = '.pptx';
- $mime_type[] = 'application/vnd.ms-excel.addin.macroEnabled.12';
- $extension[] = '.xlam';
- $mime_type[] = 'application/vnd.ms-excel.sheet.binary.macroEnabled.12';
- $extension[] = '.xlsb';
- $mime_type[] = 'application/vnd.ms-excel.sheet.macroEnabled.12';
- $extension[] = '.xlsm';
- $mime_type[] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
- $extension[] = '.xlsx';
- $mime_type[] = 'application/vnd.ms-excel.template.macroEnabled.12';
- $extension[] = '.xltm';
- $mime_type[] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.template';
- $extension[] = '.xltx';
-
-
-
- foreach ($mime_type as $key => & $type) {
- if ($type == $file_type) {
- $file_name .= $extension[$key];
- break;
- }
- }
- unset($mime_type, $extension, $type, $key);
- }
- return $file_name;
- }
-
- static function unzip_uploaded_document(
- $uploaded_file,
- $upload_path,
- $base_work_dir,
- $max_filled_space,
- $output = true,
- $to_group_id = 0
- ) {
- global $_course;
- global $_user;
- global $to_user_id;
- global $to_group_id;
- $zip_file = new PclZip($uploaded_file['tmp_name']);
-
- $zip_content_array = (array)$zip_file->listContent();
- $real_filesize = 0;
- foreach ($zip_content_array as & $this_content) {
- $real_filesize += $this_content['size'];
- }
- if (!DocumentManager::enough_space($real_filesize, $max_filled_space)) {
- Display::display_error_message(get_lang('UplNotEnoughSpace'));
- return false;
- }
-
- if ($upload_path[0] != '/') {
- $upload_path = '/'.$upload_path;
- }
-
-
- $save_dir = getcwd();
- chdir($base_work_dir.$upload_path);
-
-
- $unzipping_state = $zip_file->extract(
- PCLZIP_CB_PRE_EXTRACT,
- 'FileManager::clean_up_files_in_zip',
- PCLZIP_OPT_REPLACE_NEWER
- );
-
- self::add_all_documents_in_folder_to_database(
- $_course,
- $_user['user_id'],
- $base_work_dir,
- $upload_path == '/' ? '' : $upload_path,
- $to_group_id
- );
-
- return true;
- }
-
- function clean_up_files_in_zip($p_event, &$p_header)
- {
- $res = self::clean_up_path($p_header['filename']);
- return $res;
- }
-
- static function clean_up_path(&$path)
- {
-
- $path_array = explode('/', $path);
-
- foreach ($path_array as $key => & $val) {
-
- if ($val != '.') {
- $val = self::disable_dangerous_file(replace_dangerous_char($val));
- }
- }
-
- $path = implode('/', $path_array);
- $res = self::filter_extension($path);
- return $res;
- }
-
- static function filter_extension(&$filename)
- {
- if (substr($filename, -1) == '/') {
- return 1;
- }
- $blacklist = api_get_setting('upload_extensions_list_type');
- if ($blacklist != 'whitelist') {
- $extensions = explode(';', strtolower(api_get_setting('upload_extensions_blacklist')));
- $skip = api_get_setting('upload_extensions_skip');
- $ext = strrchr($filename, '.');
- $ext = substr($ext, 1);
- if (empty($ext)) {
- return 1;
- }
- if (in_array(strtolower($ext), $extensions)) {
- if ($skip == 'true') {
- return 0;
- } else {
- $new_ext = api_get_setting('upload_extensions_replace_by');
- $filename = str_replace('.'.$ext, '.'.$new_ext, $filename);
- return 1;
- }
- } else {
- return 1;
- }
- } else {
- $extensions = split(';', strtolower(api_get_setting('upload_extensions_whitelist')));
- $skip = api_get_setting('upload_extensions_skip');
- $ext = strrchr($filename, '.');
- $ext = substr($ext, 1);
- if (empty($ext)) {
- return 1;
- }
- if (!in_array(strtolower($ext), $extensions)) {
- if ($skip == 'true') {
- return 0;
- } else {
- $new_ext = api_get_setting('upload_extensions_replace_by');
- $filename = str_replace('.'.$ext, '.'.$new_ext, $filename);
- return 1;
- }
- } else {
- return 1;
- }
- }
- }
-
- static function add_document(
- $_course,
- $path,
- $filetype,
- $filesize,
- $title,
- $comment = null,
- $readonly = 0,
- $save_visibility = true,
- $group_id = null
- ) {
- $session_id = api_get_session_id();
- $readonly = intval($readonly);
- $comment = Database::escape_string($comment);
- $path = Database::escape_string($path);
- $filetype = Database::escape_string($filetype);
- $filesize = intval($filesize);
- $title = Database::escape_string(htmlspecialchars($title));
- $c_id = $_course['real_id'];
- $table_document = Database::get_course_table(TABLE_DOCUMENT);
- $sql = "INSERT INTO $table_document (c_id, path, filetype, size, title, comment, readonly, session_id)
- VALUES ($c_id, '$path','$filetype','$filesize','$title', '$comment', $readonly, $session_id)";
- if (Database::query($sql)) {
- $document_id = Database::insert_id();
- if ($document_id) {
- if ($save_visibility) {
- api_set_default_visibility($document_id, TOOL_DOCUMENT, $group_id);
- }
- }
- return $document_id;
- } else {
- return false;
- }
- }
-
- static function update_existing_document($_course, $document_id, $filesize, $readonly = 0)
- {
- $document_table = Database::get_course_table(TABLE_DOCUMENT);
- $document_id = intval($document_id);
- $filesize = intval($filesize);
- $readonly = intval($readonly);
- $course_id = $_course['real_id'];
- $sql = "UPDATE $document_table SET size = '$filesize' , readonly = '$readonly'
- WHERE c_id = $course_id AND id = $document_id";
- if (Database::query($sql)) {
- return true;
- } else {
- return false;
- }
- }
-
- static function item_property_update_on_folder($_course, $path, $user_id)
- {
-
-
- if ($path == '/') {
- return;
- }
- $user_id = intval($user_id);
-
- $endchar = substr($path, strlen($path) - 1, 1);
- if ($endchar == '/') {
- $path = substr($path, 0, strlen($path) - 1);
- }
- $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
-
- $time = date('Y-m-d H:i:s', time());
-
-
-
- $exploded_path = explode('/', $path);
- $course_id = api_get_course_int_id();
- foreach ($exploded_path as $key => & $value) {
-
- if ($key != 0) {
- $newpath .= '/'.$value;
-
-
- $folder_id = DocumentManager::get_document_id($_course, $newpath);
- if ($folder_id) {
- $sql = "UPDATE $TABLE_ITEMPROPERTY SET lastedit_date='$time',lastedit_type='DocumentInFolderUpdated', lastedit_user_id='$user_id'
- WHERE c_id = $course_id AND tool='".TOOL_DOCUMENT."' AND ref='$folder_id'";
- Database::query($sql);
- }
- }
- }
- }
-
- static function get_levels($filename)
- {
- $levels = explode('/', $filename);
- if (empty($levels[count($levels) - 1])) {
- unset($levels[count($levels) - 1]);
- }
- return count($levels);
- }
-
- static function search_img_from_html($html_file)
- {
- $img_path_list = array();
- if (!$fp = fopen($html_file, 'r')) {
- return;
- }
-
- $size_file = (filesize($html_file) === 0) ? 1 : filesize($html_file);
- if (isset($fp) && $fp !== false) {
- $buffer = fread($fp, $size_file);
- if (strlen($buffer) >= 0 && $buffer !== false) {
-
- } else {
- die('<center>Can not read file.</center>');
- }
- } else {
- die('<center>Can not read file.</center>');
- }
- $matches = array();
- if (preg_match_all('~<[[:space:]]*img[^>]*>~i', $buffer, $matches)) {
- $img_tag_list = $matches[0];
- }
- fclose($fp);
- unset($buffer);
-
- if (sizeof($img_tag_list) > 0) {
- foreach ($img_tag_list as & $this_img_tag) {
- if (preg_match('~src[[:space:]]*=[[:space:]]*[\"]{1}([^\"]+)[\"]{1}~i', $this_img_tag, $matches)) {
- $img_path_list[] = $matches[1];
- }
- }
- $img_path_list = array_unique($img_path_list);
- }
- return $img_path_list;
- }
-
- static function create_unexisting_directory(
- $_course,
- $user_id,
- $session_id,
- $to_group_id,
- $to_user_id,
- $base_work_dir,
- $desired_dir_name,
- $title = null,
- $visibility = ''
- ) {
- $nb = '';
-
- while (file_exists($base_work_dir.$desired_dir_name.$nb)) {
- $nb += 1;
- }
- if ($title == null) {
- $title = basename($desired_dir_name);
- }
- $course_id = $_course['real_id'];
- if (mkdir($base_work_dir.$desired_dir_name.$nb, api_get_permissions_for_new_directories(), true)) {
-
- $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
- $sql = "SELECT path FROM $tbl_document WHERE c_id = $course_id AND path='".$desired_dir_name.$nb."'";
- $rs = Database::query($sql);
- if (Database::num_rows($rs) == 0) {
- $document_id = self::add_document(
- $_course,
- $desired_dir_name.$nb,
- 'folder',
- 0,
- $title,
- null,
- 0,
- true,
- $to_group_id
- );
- if ($document_id) {
-
- if ($visibility !== '') {
- $visibilities = array(0 => 'invisible', 1 => 'visible', 2 => 'delete');
- api_item_property_update(
- $_course,
- TOOL_DOCUMENT,
- $document_id,
- $visibilities[$visibility],
- $user_id,
- $to_group_id,
- $to_user_id,
- null,
- null,
- $session_id
- );
- } else {
- api_item_property_update(
- $_course,
- TOOL_DOCUMENT,
- $document_id,
- 'FolderCreated',
- $user_id,
- $to_group_id,
- $to_user_id,
- null,
- null,
- $session_id
- );
- }
- return $desired_dir_name.$nb;
- }
- } else {
-
- return $desired_dir_name.$nb;
- }
- } else {
- return false;
- }
- }
-
- static function move_uploaded_file_collection_into_directory(
- $_course,
- $uploaded_file_collection,
- $base_work_dir,
- $missing_files_dir,
- $user_id,
- $to_group_id,
- $to_user_id,
- $max_filled_space
- ) {
- $number_of_uploaded_images = count($uploaded_file_collection['name']);
- for ($i = 0; $i < $number_of_uploaded_images; $i++) {
- $missing_file['name'] = $uploaded_file_collection['name'][$i];
- $missing_file['type'] = $uploaded_file_collection['type'][$i];
- $missing_file['tmp_name'] = $uploaded_file_collection['tmp_name'][$i];
- $missing_file['error'] = $uploaded_file_collection['error'][$i];
- $missing_file['size'] = $uploaded_file_collection['size'][$i];
- $upload_ok = self::process_uploaded_file($missing_file);
- if ($upload_ok) {
- $new_file_list[] = self::handle_uploaded_document(
- $_course,
- $missing_file,
- $base_work_dir,
- $missing_files_dir,
- $user_id,
- $to_group_id,
- $to_user_id,
- $max_filled_space,
- 0,
- 'overwrite'
- );
- }
- unset($missing_file);
- }
- return $new_file_list;
- }
-
- static function replace_img_path_in_html_file($original_img_path, $new_img_path, $html_file)
- {
-
- $fp = fopen($html_file, 'r');
- $buffer = fread($fp, filesize($html_file));
- $new_html_content = null;
-
- for ($i = 0, $fileNb = count($original_img_path); $i < $fileNb; $i++) {
- $replace_what = $original_img_path[$i];
-
- $exploded_file_path = explode('/', $new_img_path[$i]);
- $replace_by = $exploded_file_path[count($exploded_file_path) - 2].'/'.$exploded_file_path[count(
- $exploded_file_path
- ) - 1];
- $buffer = str_replace($replace_what, $replace_by, $buffer);
- }
- $new_html_content .= $buffer;
- @fclose($fp);
-
- if (!$fp = fopen($html_file, 'w')) {
- return;
- }
- if (!fwrite($fp, $new_html_content)) {
- return;
- }
- }
-
- static function check_for_missing_files($file)
- {
- if (strrchr($file, '.') == '.htm' || strrchr($file, '.') == '.html') {
- $img_file_path = self::search_img_from_html($file);
- return $img_file_path;
- }
- return false;
- }
-
- static function build_missing_files_form($missing_files, $upload_path, $file_name)
- {
-
- $added_slash = ($upload_path == '/') ? '' : '/';
- $folder_id = DocumentManager::get_document_id(api_get_course_info(), $upload_path);
-
- $form = "<p><strong>".get_lang('MissingImagesDetected')."</strong></p>"
- ."<form method=\"post\" action=\"".api_get_self()."\" enctype=\"multipart/form-data\">"
-
- ."<input type=\"hidden\" name=\"related_file\" value=\"".$upload_path.$added_slash.$file_name."\" />"
- ."<input type=\"hidden\" name=\"upload_path\" value=\"".$upload_path."\" />"
- ."<input type=\"hidden\" name=\"id\" value=\"".$folder_id."\" />"
- ."<table border=\"0\">";
- foreach ($missing_files as & $this_img_file_path) {
- $form .= "<tr>"
- ."<td>".basename($this_img_file_path)." : </td>"
- ."<td>"
- ."<input type=\"file\" name=\"img_file[]\"/>"
- ."<input type=\"hidden\" name=\"img_file_path[]\" value=\"".$this_img_file_path."\" />"
- ."</td>"
- ."</tr>";
- }
- $form .= "</table>"
- ."<button type='submit' name=\"cancel_submit_image\" value=\"".get_lang(
- 'Cancel'
- )."\" class=\"cancel\">".get_lang('Cancel')."</button>"
- ."<button type='submit' name=\"submit_image\" value=\"".get_lang('Ok')."\" class=\"save\">".get_lang(
- 'Ok'
- )."</button>"
- ."</form>";
- return $form;
- }
-
- static function add_all_documents_in_folder_to_database(
- $_course,
- $user_id,
- $base_work_dir,
- $current_path = '',
- $to_group_id = 0
- ) {
- $current_session_id = api_get_session_id();
- $path = $base_work_dir.$current_path;
-
- $handle = opendir($path);
- if (is_dir($path)) {
-
- while ($file = readdir($handle)) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- $completepath = "$path/$file";
-
- if (is_dir($completepath)) {
- $title = self::get_document_title($file);
- $safe_file = replace_dangerous_char($file);
- @rename($path.'/'.$file, $path.'/'.$safe_file);
-
- if (!DocumentManager::get_document_id($_course, $current_path.'/'.$safe_file)) {
- $document_id = self::add_document($_course, $current_path.'/'.$safe_file, 'folder', 0, $title);
- api_item_property_update(
- $_course,
- TOOL_DOCUMENT,
- $document_id,
- 'DocumentAdded',
- $user_id,
- $to_group_id,
- null,
- null,
- null,
- $current_session_id
- );
-
- }
-
- self::add_all_documents_in_folder_to_database(
- $_course,
- $user_id,
- $base_work_dir,
- $current_path.'/'.$safe_file,
- $to_group_id
- );
- } else {
-
- $safe_file = self::disable_dangerous_file(replace_dangerous_char($file, 'strict'));
- @rename($base_work_dir.$current_path.'/'.$file, $base_work_dir.$current_path.'/'.$safe_file);
- $document_id = DocumentManager::get_document_id($_course, $current_path.'/'.$safe_file);
- if (!$document_id) {
- $title = self::get_document_title($file);
- $size = filesize($base_work_dir.$current_path.'/'.$safe_file);
- $document_id = self::add_document(
- $_course,
- $current_path.'/'.$safe_file,
- 'file',
- $size,
- $title
- );
- api_item_property_update(
- $_course,
- TOOL_DOCUMENT,
- $document_id,
- 'DocumentAdded',
- $user_id,
- $to_group_id,
- null,
- null,
- null,
- $current_session_id
- );
-
- } else {
- api_item_property_update(
- $_course,
- TOOL_DOCUMENT,
- $document_id,
- 'DocumentUpdated',
- $user_id,
- $to_group_id,
- null,
- null,
- null,
- $current_session_id
- );
- }
- }
- }
- }
- }
- }
|