/** * Moves a directory and its content to an other area * * @author - Hugues Peeters <*****@*****.**> * @param - $orig_dir_path (string) - the path of the directory to move * @param - $destination (string) - the path of the new area * @return - no return */ function copyDirTo($orig_dir_path, $destination, $move = true) { if ($orig_dir_path == $destination) { return false; } $save_dir = getcwd(); // Extract directory name - create it at destination - update destination trail $dir_name = basename($orig_dir_path); $dir_to_copy = array(); if (is_dir($orig_dir_path)) { if (!is_dir($destination . '/' . $dir_name)) { 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; // Skip the current and parent directories } 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); // Recursivity } } if ($move) { rmdir($orig_dir_path); } chdir($save_dir); } } }
// end of process // copy chamilo data dirs and protect against copy recursion. /* echo "<pre>"; echo "copyDirTo($vcoursepath, $absolute_datadir, false); copyDirTo($varchivepath, $absolute_datadir.'/archive, false); copyDirTo($vhomepath, $absolute_datadir.'/home', false);"; echo "</pre>"; */ echo "<pre>"; echo "Copying from {$vcoursepath} to {$absolute_datadir} \n"; copyDirTo($vcoursepath, $absolute_datadir, false); echo "Copying from {$varchivepath} to {$absolute_datadir}/archive \n"; copyDirTo($varchivepath, $absolute_datadir . '/archive', false); echo "Copying from {$vhomepath} to {$absolute_datadir}/home \n"; copyDirTo($vhomepath, $absolute_datadir . '/home', false); echo "</pre>"; // Store original hostname and some config info for further database or filestore replacements. $FILE = fopen($absolute_sqldir . $separator . 'manifest.php', 'w'); fwrite($FILE, '<' . '?php '); fwrite($FILE, "\$templatewwwroot = '" . $wwwroot . "';\n"); fwrite($FILE, "\$templatevdbprefix = '" . $vhost->table_prefix . "';\n "); fwrite($FILE, "\$coursefolder = '" . $vhost->course_folder . "';\n "); fwrite($FILE, '?' . '>'); fclose($FILE); // Every step was SUCCESS. if (empty($fullautomation)) { $message_object->message = $plugininstance->get_lang('successfinishedcapture'); $message_object->style = 'notifysuccess'; // Save confirm message before redirection. $_SESSION['confirm_message'] = $message_object;
function copyDirTo($origDirPath, $destination) { // extract directory name - create it at destination - update destination trail $dirName = my_basename($origDirPath); mkdir($destination . "/" . $dirName, 0775); $destinationTrail = $destination . "/" . $dirName; $cwd = getcwd(); 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); unlink($element); } elseif (is_dir($element)) { $dirToCopy[] = $origDirPath . "/" . $element; } } closedir($handle); if (isset($dirToCopy) and sizeof($dirToCopy) > 0) { foreach ($dirToCopy as $thisDir) { copyDirTo($thisDir, $destinationTrail); // recursivity } } rmdir($origDirPath); chdir($cwd); }
/** * Restore scorm documents * TODO @TODO check that the restore function with renaming doesn't break the scorm structure! * see #7029 */ public function restore_scorm_documents() { $perm = api_get_permissions_for_new_directories(); if ($this->course->has_resources(RESOURCE_SCORM)) { $resources = $this->course->resources; foreach ($resources[RESOURCE_SCORM] as $document) { $path = api_get_path(SYS_COURSE_PATH) . $this->course->destination_path . '/'; @mkdir(dirname($path . $document->path), $perm, true); if (file_exists($path . $document->path)) { switch ($this->file_option) { case FILE_OVERWRITE: rmdirr($path . $document->path); copyDirTo($this->course->backup_path . '/' . $document->path, $path . dirname($document->path), false); break; case FILE_SKIP: break; case FILE_RENAME: $i = 1; $ext = explode('.', basename($document->path)); if (count($ext) > 1) { $ext = array_pop($ext); $file_name_no_ext = substr($document->path, 0, -(strlen($ext) + 1)); $ext = '.' . $ext; } else { $ext = ''; $file_name_no_ext = $document->path; } $new_file_name = $file_name_no_ext . '_' . $i . $ext; $file_exists = file_exists($path . $new_file_name); while ($file_exists) { $i++; $new_file_name = $file_name_no_ext . '_' . $i . $ext; $file_exists = file_exists($path . $new_file_name); } rename($this->course->backup_path . '/' . $document->path, $this->course->backup_path . '/' . $new_file_name); copyDirTo($this->course->backup_path . '/' . $new_file_name, $path . dirname($new_file_name), false); rename($this->course->backup_path . '/' . $new_file_name, $this->course->backup_path . '/' . $document->path); break; } // end switch } else { // end if file exists copyDirTo($this->course->backup_path . '/' . $document->path, $path . dirname($document->path), false); } } // end for each } }
/** * Write a course and all its resources to a zip-file. * @return string A pointer to the zip-file */ public static function write_course($course) { $perm_dirs = api_get_permissions_for_new_directories(); CourseArchiver::clean_backup_dir(); // Create a temp directory $tmp_dir_name = 'CourseArchiver_' . api_get_unique_id(); $backup_dir = api_get_path(SYS_ARCHIVE_PATH) . $tmp_dir_name . '/'; // All course-information will be stored in course_info.dat $course_info_file = $backup_dir . 'course_info.dat'; $zip_dir = api_get_path(SYS_ARCHIVE_PATH); $user = api_get_user_info(); $date = new DateTime(api_get_local_time()); $zip_file = $user['user_id'] . '_' . $course->code . '_' . $date->format('Ymd-His') . '.zip'; $php_errormsg = ''; $res = @mkdir($backup_dir, $perm_dirs); if ($res === false) { //TODO set and handle an error message telling the user to review the permissions on the archive directory error_log(__FILE__ . ' line ' . __LINE__ . ': ' . (ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini') . ' - This error, occuring because your archive directory will not let this script write data into it, will prevent courses backups to be created', 0); } // Write the course-object to the file $fp = @fopen($course_info_file, 'w'); if ($fp === false) { error_log(__FILE__ . ' line ' . __LINE__ . ': ' . (ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0); } $res = @fwrite($fp, base64_encode(serialize($course))); if ($res === false) { error_log(__FILE__ . ' line ' . __LINE__ . ': ' . (ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0); } $res = @fclose($fp); if ($res === false) { error_log(__FILE__ . ' line ' . __LINE__ . ': ' . (ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0); } // Copy all documents to the temp-dir if (isset($course->resources[RESOURCE_DOCUMENT]) && is_array($course->resources[RESOURCE_DOCUMENT])) { foreach ($course->resources[RESOURCE_DOCUMENT] as $document) { if ($document->file_type == DOCUMENT) { $doc_dir = $backup_dir . $document->path; @mkdir(dirname($doc_dir), $perm_dirs, true); if (file_exists($course->path . $document->path)) { copy($course->path . $document->path, $doc_dir); } } else { @mkdir($backup_dir . $document->path, $perm_dirs, true); } } } // Copy all scorm documents to the temp-dir if (isset($course->resources[RESOURCE_SCORM]) && is_array($course->resources[RESOURCE_SCORM])) { foreach ($course->resources[RESOURCE_SCORM] as $document) { $doc_dir = dirname($backup_dir . $document->path); @mkdir($doc_dir, $perm_dirs, true); copyDirTo($course->path . $document->path, $doc_dir, false); } } // Copy calendar attachments. if (isset($course->resources[RESOURCE_EVENT]) && is_array($course->resources[RESOURCE_EVENT])) { $doc_dir = dirname($backup_dir . '/upload/calendar/'); @mkdir($doc_dir, $perm_dirs, true); copyDirTo($course->path . 'upload/calendar/', $doc_dir, false); } // Copy Learning path author image. if (isset($course->resources[RESOURCE_LEARNPATH]) && is_array($course->resources[RESOURCE_LEARNPATH])) { $doc_dir = dirname($backup_dir . '/upload/learning_path/'); @mkdir($doc_dir, $perm_dirs, true); copyDirTo($course->path . 'upload/learning_path/', $doc_dir, false); } // Copy announcements attachments. if (isset($course->resources[RESOURCE_ANNOUNCEMENT]) && is_array($course->resources[RESOURCE_ANNOUNCEMENT])) { $doc_dir = dirname($backup_dir . '/upload/announcements/'); @mkdir($doc_dir, $perm_dirs, true); copyDirTo($course->path . 'upload/announcements/', $doc_dir, false); } // Copy work folders (only folders) if (isset($course->resources[RESOURCE_WORK]) && is_array($course->resources[RESOURCE_WORK])) { $doc_dir = dirname($backup_dir . '/upload/work/'); @mkdir($doc_dir, $perm_dirs, true); // @todo: adjust to only create subdirs, but not copy files copyDirTo($course->path . 'upload/work/', $doc_dir, false); } // Zip the course-contents $zip = new PclZip($zip_dir . $zip_file); $zip->create($zip_dir . $tmp_dir_name, PCLZIP_OPT_REMOVE_PATH, $zip_dir . $tmp_dir_name . '/'); //$zip->deleteByIndex(0); // Remove the temp-dir. rmdirr($backup_dir); return '' . $zip_file; }