public function __construct() { parent::__construct(); $this->useSSL = AKFactory::get('kickstart.ftp.ssl', false); $this->passive = AKFactory::get('kickstart.ftp.passive', true); $this->host = AKFactory::get('kickstart.ftp.host', ''); $this->port = AKFactory::get('kickstart.ftp.port', 21); if (trim($this->port) == '') { $this->port = 21; } $this->user = AKFactory::get('kickstart.ftp.user', ''); $this->pass = AKFactory::get('kickstart.ftp.pass', ''); $this->dir = AKFactory::get('kickstart.ftp.dir', ''); $this->tempDir = AKFactory::get('kickstart.ftp.tempdir', ''); $connected = $this->connect(); if ($connected) { if (!empty($this->tempDir)) { $tempDir = rtrim($this->tempDir, '/\\') . '/'; $writable = $this->isDirWritable($tempDir); } else { $tempDir = ''; $writable = false; } if (!$writable) { // Default temporary directory is the current root $tempDir = function_exists('getcwd') ? getcwd() : dirname(__FILE__); if (empty($tempDir)) { // Oh, we have no directory reported! $tempDir = '.'; } $absoluteDirToHere = $tempDir; $tempDir = rtrim(str_replace('\\', '/', $tempDir), '/'); if (!empty($tempDir)) { $tempDir .= '/'; } $this->tempDir = $tempDir; // Is this directory writable? $writable = $this->isDirWritable($tempDir); } if (!$writable) { // Nope. Let's try creating a temporary directory in the site's root. $tempDir = $absoluteDirToHere . '/kicktemp'; $this->createDirRecursive($tempDir, 0777); // Try making it writable... $this->fixPermissions($tempDir); $writable = $this->isDirWritable($tempDir); } // Was the new directory writable? if (!$writable) { // Let's see if the user has specified one $userdir = AKFactory::get('kickstart.ftp.tempdir', ''); if (!empty($userdir)) { // Is it an absolute or a relative directory? $absolute = false; $absolute = $absolute || substr($userdir, 0, 1) == '/'; $absolute = $absolute || substr($userdir, 1, 1) == ':'; $absolute = $absolute || substr($userdir, 2, 1) == ':'; if (!$absolute) { // Make absolute $tempDir = $absoluteDirToHere . $userdir; } else { // it's already absolute $tempDir = $userdir; } // Does the directory exist? if (is_dir($tempDir)) { // Yeah. Is it writable? $writable = $this->isDirWritable($tempDir); } } } $this->tempDir = $tempDir; if (!$writable) { // No writable directory found!!! $this->setError(AKText::_('FTP_TEMPDIR_NOT_WRITABLE')); } else { AKFactory::set('kickstart.ftp.tempdir', $tempDir); $this->tempDir = $tempDir; } } }
/** * Public constructor. Tries to connect to the FTP server. */ public function __construct() { parent::__construct(); $this->useFTP = true; $this->useSSL = AKFactory::get('kickstart.ftp.ssl', false); $this->passive = AKFactory::get('kickstart.ftp.passive', true); $this->host = AKFactory::get('kickstart.ftp.host', ''); $this->port = AKFactory::get('kickstart.ftp.port', 21); $this->user = AKFactory::get('kickstart.ftp.user', ''); $this->pass = AKFactory::get('kickstart.ftp.pass', ''); $this->dir = AKFactory::get('kickstart.ftp.dir', ''); $this->tempDir = AKFactory::get('kickstart.ftp.tempdir', ''); if (trim($this->port) == '') { $this->port = 21; } // If FTP is not configured, skip it altogether if (empty($this->host) || empty($this->user) || empty($this->pass)) { $this->useFTP = false; } // Try to connect to the FTP server $connected = $this->connect(); // If the connection fails, skip FTP altogether if (!$connected) { $this->useFTP = false; } if ($connected) { if (!empty($this->tempDir)) { $tempDir = rtrim($this->tempDir, '/\\') . '/'; $writable = $this->isDirWritable($tempDir); } else { $tempDir = ''; $writable = false; } if (!$writable) { // Default temporary directory is the current root $tempDir = KSROOTDIR; if (empty($tempDir)) { // Oh, we have no directory reported! $tempDir = '.'; } $absoluteDirToHere = $tempDir; $tempDir = rtrim(str_replace('\\', '/', $tempDir), '/'); if (!empty($tempDir)) { $tempDir .= '/'; } $this->tempDir = $tempDir; // Is this directory writable? $writable = $this->isDirWritable($tempDir); } if (!$writable) { // Nope. Let's try creating a temporary directory in the site's root. $tempDir = $absoluteDirToHere . '/kicktemp'; $trustMeIKnowWhatImDoing = 500 + 10 + 1; // working around overzealous scanners written by bozos $this->createDirRecursive($tempDir, $trustMeIKnowWhatImDoing); // Try making it writable... $this->fixPermissions($tempDir); $writable = $this->isDirWritable($tempDir); } // Was the new directory writable? if (!$writable) { // Let's see if the user has specified one $userdir = AKFactory::get('kickstart.ftp.tempdir', ''); if (!empty($userdir)) { // Is it an absolute or a relative directory? $absolute = false; $absolute = $absolute || substr($userdir, 0, 1) == '/'; $absolute = $absolute || substr($userdir, 1, 1) == ':'; $absolute = $absolute || substr($userdir, 2, 1) == ':'; if (!$absolute) { // Make absolute $tempDir = $absoluteDirToHere . $userdir; } else { // it's already absolute $tempDir = $userdir; } // Does the directory exist? if (is_dir($tempDir)) { // Yeah. Is it writable? $writable = $this->isDirWritable($tempDir); } } } $this->tempDir = $tempDir; if (!$writable) { // No writable directory found!!! $this->setError(AKText::_('FTP_TEMPDIR_NOT_WRITABLE')); } else { AKFactory::set('kickstart.ftp.tempdir', $tempDir); $this->tempDir = $tempDir; } } }
/** * 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; }