/** * Creates the directory this file points to */ protected function createDirectory() { if (AKFactory::get('kickstart.setup.dryrun', '0')) { return true; } // Do we need to create a directory? $lastSlash = strrpos($this->fileHeader->realFile, '/'); $dirName = substr($this->fileHeader->realFile, 0, $lastSlash); $perms = $this->flagRestorePermissions ? $retArray['permissions'] : 0755; $ignore = AKFactory::get('kickstart.setup.ignoreerrors', false); if ($this->postProcEngine->createDirRecursive($dirName, $perms) == false && !$ignore) { $this->setError(AKText::sprintf('COULDNT_CREATE_DIR', $dirName)); return false; } else { return true; } }
/** * Proxies deleting a file to the restore.php version * * @param string $fileName The path to the file to be deleted * * @return bool * * @since 3.5.1 */ public static function delete($fileName) { $postproc = AKFactory::getPostProc(); $postproc->unlink($fileName); }
/** * Enforce the minimum execution time */ public function enforce_min_exec_time() { // Try to get a sane value for PHP's maximum_execution_time INI parameter if (@function_exists('ini_get')) { $php_max_exec = @ini_get("maximum_execution_time"); } else { $php_max_exec = 10; } if ($php_max_exec == "" || $php_max_exec == 0) { $php_max_exec = 10; } // Decrease $php_max_exec time by 500 msec we need (approx.) to tear down // the application, as well as another 500msec added for rounding // error purposes. Also make sure this is never gonna be less than 0. $php_max_exec = max($php_max_exec * 1000 - 1000, 0); // Get the "minimum execution time per step" Akeeba Backup configuration variable $minexectime = AKFactory::get('kickstart.tuning.min_exec_time', 0); if (!is_numeric($minexectime)) { $minexectime = 0; } // Make sure we are not over PHP's time limit! if ($minexectime > $php_max_exec) { $minexectime = $php_max_exec; } // Get current running time $elapsed_time = $this->getRunningTime() * 1000; // Only run a sleep delay if we haven't reached the minexectime execution time if ($minexectime > $elapsed_time && $elapsed_time > 0) { $sleep_msec = $minexectime - $elapsed_time; if (function_exists('usleep')) { usleep(1000 * $sleep_msec); } elseif (function_exists('time_nanosleep')) { $sleep_sec = floor($sleep_msec / 1000); $sleep_nsec = 1000000 * ($sleep_msec - $sleep_sec * 1000); time_nanosleep($sleep_sec, $sleep_nsec); } elseif (function_exists('time_sleep_until')) { $until_timestamp = time() + $sleep_msec / 1000; time_sleep_until($until_timestamp); } elseif (function_exists('sleep')) { $sleep_sec = ceil($sleep_msec / 1000); sleep($sleep_sec); } } elseif ($elapsed_time > 0) { // No sleep required, even if user configured us to be able to do so. } }
/** * Set up the Akeeba Restore engine for the current archive */ private function setUpAkeebaRestore() { $config = Factory::getConfiguration(); $maxTime = Factory::getTimer()->getTimeLeft(); $maxTime = floor($maxTime); $maxTime = max(2, $maxTime); $statistics = Factory::getStatistics(); $stat = $statistics->getRecord(); $backup_parts = Factory::getStatistics()->get_all_filenames($stat, false); $filePath = array_shift($backup_parts); $specialDirs = Platform::getInstance()->get_stock_directories(); $tmpPath = $specialDirs['[SITETMP]']; $archiver = Factory::getArchiverEngine(); $extension = $archiver->getExtension(); $extension = strtoupper($extension); $extension = ltrim($extension, '.'); $ksOptions = array('kickstart.tuning.max_exec_time' => $maxTime, 'kickstart.tuning.run_time_bias' => $config->get('akeeba.tuning.run_time_bias', 75), 'kickstart.tuning.min_exec_time' => '0', 'kickstart.procengine' => 'direct', 'kickstart.setup.sourcefile' => $filePath, 'kickstart.setup.destdir' => $tmpPath, 'kickstart.setup.restoreperms' => '0', 'kickstart.setup.filetype' => $extension, 'kickstart.setup.dryrun' => '1', 'kickstart.jps.password' => $config->get('engine.archiver.jps.key', '', false)); \AKFactory::nuke(); foreach ($ksOptions as $k => $v) { \AKFactory::set($k, $v); } \AKFactory::set('kickstart.enabled', true); }
public function urlimport($params) { $this->params = $params; // Fetch data $filename = $this->getParam('file'); $frag = $this->getParam('frag', -1); $totalSize = $this->getParam('totalSize', -1); $doneSize = $this->getParam('doneSize', -1); debugMsg('Importing from URL'); debugMsg(' file : ' . $filename); debugMsg(' frag : ' . $frag); debugMsg(' totalSize : ' . $totalSize); debugMsg(' doneSize : ' . $doneSize); // Init retArray $retArray = array("status" => true, "error" => '', "frag" => $frag, "totalSize" => $totalSize, "doneSize" => $doneSize, "percent" => 0); try { AKFactory::set('kickstart.tuning.max_exec_time', '5'); AKFactory::set('kickstart.tuning.run_time_bias', '75'); $timer = new AKCoreTimer(); $start = $timer->getRunningTime(); // Mark the start of this download $break = false; // Don't break the step while ($timer->getTimeLeft() > 0 && !$break) { // Figure out where on Earth to put that file $local_file = KSROOTDIR . '/' . basename($filename); debugMsg("- Importing from {$filename}"); // Do we have to initialize the file? if ($frag == -1) { debugMsg("-- First frag, killing local file"); // Currently downloaded size $doneSize = 0; // Delete and touch the output file @unlink($local_file); $fp = @fopen($local_file, 'wb'); if ($fp !== false) { @fclose($fp); } // Init $frag = 0; } // Calculate from and length $length = 1048576; $from = $frag * $length; $to = $length + $from - 1; //if($from == 0) $from = 1; // Try to download the first frag $temp_file = $local_file . '.tmp'; @unlink($temp_file); $required_time = 1.0; debugMsg("-- Importing frag {$frag}, byte position from/to: {$from} / {$to}"); $filesize = 0; try { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $filename); curl_setopt($ch, CURLOPT_RANGE, "{$from}-{$to}"); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $result = curl_exec($ch); $errno = curl_errno($ch); $errmsg = curl_error($ch); $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($result === false) { $error = "cURL error {$errno}: {$errmsg}"; } elseif ($http_status > 299) { $result = false; $error = "HTTP status {$http_status}"; } else { $result = file_put_contents($temp_file, $result); if ($result === false) { $error = "Could not open temporary file {$temp_file} for writing"; } } curl_close($ch); } catch (Exception $e) { $error = $e->getMessage(); } if (!$result) { @unlink($temp_file); // Failed download if ($frag == 0) { // Failure to download first frag = failure to download. Period. $retArray['status'] = false; $retArray['error'] = $error; debugMsg("-- Download FAILED"); return $retArray; } else { // Since this is a staggered download, consider this normal and finish $frag = -1; debugMsg("-- Import complete"); $doneSize = $totalSize; $break = true; continue; } } // Add the currently downloaded frag to the total size of downloaded files if ($result) { clearstatcache(); $filesize = (int) @filesize($temp_file); debugMsg("-- Successful download of {$filesize} bytes"); $doneSize += $filesize; // Append the file $fp = @fopen($local_file, 'ab'); if ($fp === false) { debugMsg("-- Can't open local file for writing"); // Can't open the file for writing @unlink($temp_file); $retArray['status'] = false; $retArray['error'] = 'Can\'t write to the local file'; return false; } $tf = fopen($temp_file, 'rb'); while (!feof($tf)) { $data = fread($tf, 262144); fwrite($fp, $data); } fclose($tf); fclose($fp); @unlink($temp_file); debugMsg("-- Temporary file merged and removed"); if ($filesize > $length) { debugMsg("-- Read more data than the requested length. I assume this file is complete."); $frag = -1; } else { $frag++; debugMsg("-- Proceeding to next fragment, frag {$frag}"); } } // Advance the frag pointer and mark the end $end = $timer->getRunningTime(); // Do we predict that we have enough time? $required_time = max(1.1 * ($end - $start), $required_time); if ($required_time > 10 - $end + $start) { $break = true; } $start = $end; } if ($frag == -1) { $percent = 100; } elseif ($doneSize <= 0) { $percent = 0; } else { if ($totalSize > 0) { $percent = 100 * ($doneSize / $totalSize); } else { $percent = 0; } } // Update $retArray $retArray = array("status" => true, "error" => '', "frag" => $frag, "totalSize" => $totalSize, "doneSize" => $doneSize, "percent" => $percent); } catch (Exception $e) { debugMsg("EXCEPTION RAISED:"); debugMsg($e->getMessage()); $retArray['status'] = false; $retArray['error'] = $e->getMessage(); } return $retArray; }
function doAjax() { $ajax = $this->getState('ajax'); switch ($ajax) { // FTP Connection test for DirectFTP case 'testftp': // Grab request parameters $config = array('host' => $this->input->get('host', '', 'none', 2), 'port' => $this->input->get('port', 21, 'int'), 'user' => $this->input->get('user', '', 'none', 2), 'pass' => $this->input->get('pass', '', 'none', 2), 'initdir' => $this->input->get('initdir', '', 'none', 2), 'usessl' => $this->input->get('usessl', 'cmd') == 'true', 'passive' => $this->input->get('passive', 'cmd') == 'true'); // Perform the FTP connection test $test = new AEArchiverDirectftp(); $test->initialize('', $config); $errors = $test->getError(); if (empty($errors)) { $result = true; } else { $result = $errors; } break; case 'restoreFilesPing': case 'restoreFilesStart': case 'restoreFilesStep': case 'restoreFilesFinalize': global $restoration_setup; $restoration_setup = $this->getRestorationParameters(); define('KICKSTART', 1); include_once JPATH_COMPONENT_ADMINISTRATOR . '/restore.php'; akeebaTinyHackForRestorationObserver(); masterSetup(); $retArray = array('status' => true, 'message' => null); switch ($ajax) { case 'restoreFilesPing': // ping task - realy does nothing! $timer = AKFactory::getTimer(); $timer->enforce_min_exec_time(); break; case 'restoreFilesStart': AKFactory::nuke(); // Reset the factory // Reset the factory case 'restoreFilesStep': $config = JFactory::getConfig(); if (version_compare(JVERSION, '3.0', 'ge')) { $tmp_path = $config->get('tmp_path', ''); } else { $tmp_path = $config->getValue('tmp_path', ''); } $override = array('rename_dirs' => array('sql' => rtrim($tmp_path, '/\\') . '/sql')); $engine = AKFactory::getUnarchiver($override); // Get the engine $observer = new RestorationObserver(); // Create a new observer $engine->attach($observer); // Attach the observer $engine->tick(); $ret = $engine->getStatusArray(); if ($ret['Error'] != '') { $retArray['status'] = false; $retArray['done'] = true; $retArray['message'] = $ret['Error']; } elseif (!$ret['HasRun']) { $retArray['files'] = $observer->filesProcessed; $retArray['bytesIn'] = $observer->compressedTotal; $retArray['bytesOut'] = $observer->uncompressedTotal; $retArray['status'] = true; $retArray['done'] = true; } else { $retArray['files'] = $observer->filesProcessed; $retArray['bytesIn'] = $observer->compressedTotal; $retArray['bytesOut'] = $observer->uncompressedTotal; $retArray['status'] = true; $retArray['done'] = false; $retArray['factory'] = AKFactory::serialize(); } break; case 'restoreFilesFinalize': $root = AKFactory::get('kickstart.setup.destdir'); // Remove the sql dump directory $config = JFactory::getConfig(); if (version_compare(JVERSION, '3.0', 'ge')) { $tmp_path = $config->get('tmp_path', ''); } else { $tmp_path = $config->getValue('tmp_path', ''); } $sqldir = rtrim($tmp_path, '/\\') . '/sql'; recursive_remove_directory($sqldir); break; } return $retArray; break; case 'dbRestoreStart': $this->dbRestorationInit(); case 'dbRestore': $result = $this->dbRestore(); break; // Unrecognized AJAX task // Unrecognized AJAX task default: $result = false; break; } return $result; }