/** * @package AdminTools * * @copyright Copyright (c)2010-2011 Nicholas K. Dionysopoulos * @license GNU General Public License version 3, or later * * @param string $file * @param string $buffer * * @return boolean */ static function _write($file, $buffer) { // Initialize variables jimport('joomla.client.helper'); jimport('joomla.filesystem.folder'); jimport('joomla.filesystem.file'); jimport('joomla.filesystem.path'); $FTPOptions = JClientHelper::getCredentials('ftp'); // If the destination directory doesn't exist we need to create it if (!file_exists(dirname($file))) { JFolder::create(dirname($file)); } if ($FTPOptions['enabled'] == 1) { // Connect the FTP client jimport('joomla.client.ftp'); if (version_compare(JVERSION, '3.0', 'ge')) { $ftp = JClientFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], NULL, $FTPOptions['user'], $FTPOptions['pass']); } else { $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], NULL, $FTPOptions['user'], $FTPOptions['pass']); } // Translate path for the FTP account and use FTP write buffer to // file $file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/'); $ret = $ftp->write($file, $buffer); } else { if (!is_writable($file)) { chmod($file, 0755); } if (!is_writable($file)) { chmod($file, 0777); } $ret = @file_put_contents($file, $buffer); chmod($file, 0644); } if (!$ret) { jimport('joomla.filesystem.file'); JFile::write($file, $buffer); } return $ret; }
/** * Changes the permissions of a file or directory using direct file access or * Joomla!'s FTP layer, whichever works * @param $path string Absolute path to the file/dir to chmod * @param $mode The permissions mode to apply * @return bool True on success */ private function chmod($path, $mode) { if (is_string($mode)) { $mode = octdec($mode); } // Initialize variables JLoader::import('joomla.client.helper'); $ftpOptions = JClientHelper::getCredentials('ftp'); // Check to make sure the path valid and clean $path = JPath::clean($path); if ($ftpOptions['enabled'] == 1) { // Connect the FTP client JLoader::import('joomla.client.ftp'); if (version_compare(JVERSION, '3.0', 'ge')) { $ftp = JClientFTP::getInstance($ftpOptions['host'], $ftpOptions['port'], array(), $ftpOptions['user'], $ftpOptions['pass']); } else { $ftp = JFTP::getInstance($ftpOptions['host'], $ftpOptions['port'], array(), $ftpOptions['user'], $ftpOptions['pass']); } } if (@chmod($path, $mode)) { $ret = true; } elseif ($ftpOptions['enabled'] == 1) { // Translate path and delete JLoader::import('joomla.client.ftp'); $path = JPath::clean(str_replace(JPATH_ROOT, $ftpOptions['root'], $path), '/'); // FTP connector throws an error $ret = $ftp->chmod($path, $mode); } else { return false; } }
private function deletePath($path) { // Initialize variables JLoader::import('joomla.client.helper'); $ftpOptions = JClientHelper::getCredentials('ftp'); // Check to make sure the path valid and clean $n_path = @realpath($path); $path = empty($n_path) ? $path : $n_path; if ($ftpOptions['enabled'] == 1) { // Connect the FTP client if (version_compare(JVERSION, '3.0', 'ge')) { $ftp = JClientFTP::getInstance($ftpOptions['host'], $ftpOptions['port'], array(), $ftpOptions['user'], $ftpOptions['pass']); } else { $ftp = JFTP::getInstance($ftpOptions['host'], $ftpOptions['port'], array(), $ftpOptions['user'], $ftpOptions['pass']); } } if (@unlink($path)) { $ret = true; } elseif (@rmdir($path)) { $ret = true; } elseif ($ftpOptions['enabled'] == 1) { if (substr($path, 0, strlen(JPATH_ROOT)) !== JPATH_ROOT) { return false; } // Translate path and delete $path = JPath::clean(str_replace(JPATH_ROOT, $ftpOptions['root'], $path), '/'); // FTP connector throws an error $ret = $ftp->delete($path); } else { return false; } return $ret; }
/** * Creates the restoration.ini file which is used during the update * package's extraction. This file tells Akeeba Restore which package to * read and where and how to extract it. * * @return boolean True on success */ public function createRestorationINI() { // Get a password $password = $this->getRandomString(64); $this->setState('update_password', $password); // Get the absolute path to site's root $siteroot = JPATH_SITE; $siteroot = str_replace('\\', '/', $siteroot); $jreg = JFactory::getConfig(); $tempdir = dirname(__FILE__) . '/tmp'; $file = dirname(__FILE__) . '/tmp/myjoomla-upgradefile.zip'; $data = "<?php\ndefined('_AKEEBA_RESTORATION') or die();\n"; $data .= '$restoration_setup = array(' . "\n"; $ftpOptions = $this->getFTPOptions(); $engine = $ftpOptions['enable'] ? 'hybrid' : 'direct'; $data .= <<<ENDDATA \t'kickstart.security.password' => '{$password}', \t'kickstart.tuning.max_exec_time' => '5', \t'kickstart.tuning.run_time_bias' => '75', \t'kickstart.tuning.min_exec_time' => '0', \t'kickstart.procengine' => '{$engine}', \t'kickstart.setup.sourcefile' => '{$tempdir}/myjoomla-upgradefile.zip', \t'kickstart.setup.destdir' => '{$siteroot}', \t'kickstart.setup.restoreperms' => '0', \t'kickstart.setup.filetype' => 'zip', \t'kickstart.setup.dryrun' => '0' ENDDATA; if ($ftpOptions['enable']) { // Get an instance of the FTP client JLoader::import('joomla.client.ftp'); if (version_compare(JVERSION, '3.0', 'ge')) { $ftp = JClientFTP::getInstance($ftpOptions['host'], $ftpOptions['port'], array('type' => FTP_BINARY), $ftpOptions['user'], $ftpOptions['pass']); } else { $ftp = JFTP::getInstance($ftpOptions['host'], $ftpOptions['port'], array('type' => FTP_BINARY), $ftpOptions['user'], $ftpOptions['pass']); } // Is the tempdir really writable? $writable = @is_writeable($tempdir); if ($writable) { // Let's be REALLY sure $fp = @fopen($tempdir . '/test.txt', 'w'); if ($fp === FALSE) { $writable = FALSE; } else { fclose($fp); unlink($tempdir . '/test.txt'); } } // If the tempdir is not writable, create a new writable subdirectory if (!$writable) { JLoader::import('joomla.filesystem.folder'); $dest = JPath::clean(str_replace(JPATH_ROOT, $ftpOptions['root'], $tempdir . '/cmsupdate'), '/'); if (!@mkdir($tempdir . '/cmsupdate')) { $ftp->mkdir($dest); } if (!@chmod($tempdir . '/cmsupdate', 511)) { $ftp->chmod($dest, 511); } $tempdir .= '/cmsupdate'; } // Just in case the temp-directory was off-root, try using the default tmp directory $writable = @is_writeable($tempdir); if (!$writable) { $tempdir = JPATH_ROOT . '/tmp'; // Does the JPATH_ROOT/tmp directory exist? if (!is_dir($tempdir)) { JLoader::import('joomla.filesystem.file'); JFolder::create($tempdir, 511); $htAccessContents = "order deny,allow\ndeny from all\nallow from none\n"; JFile::write($tempdir . '/.htaccess', $htAccessContents); } // If it exists and it is unwritable, try creating a writable cmsupdate subdirectory if (!is_writable($tempdir)) { JLoader::import('joomla.filesystem.folder'); $dest = JPath::clean(str_replace(JPATH_ROOT, $ftpOptions['root'], $tempdir . '/cmsupdate'), '/'); if (!@mkdir($tempdir . '/cmsupdate')) { $ftp->mkdir($dest); } if (!@chmod($tempdir . '/cmsupdate', 511)) { $ftp->chmod($dest, 511); } $tempdir .= '/cmsupdate'; } } // If we still have no writable directory, we'll try /tmp and the system's temp-directory $writable = @is_writeable($tempdir); if (!$writable) { if (@is_dir('/tmp') && @is_writable('/tmp')) { $tempdir = '/tmp'; } else { // Try to find the system temp path $tmpfile = @tempnam("dummy", ""); $systemp = @dirname($tmpfile); @unlink($tmpfile); if (!empty($systemp)) { if (@is_dir($systemp) && @is_writable($systemp)) { $tempdir = $systemp; } } } } $data .= <<<ENDDATA \t, \t'kickstart.ftp.ssl' => '0', \t'kickstart.ftp.passive' => '1', \t'kickstart.ftp.host' => '{$ftpOptions['host']}', \t'kickstart.ftp.port' => '{$ftpOptions['port']}', \t'kickstart.ftp.user' => '{$ftpOptions['user']}', \t'kickstart.ftp.pass' => '{$ftpOptions['pass']}', \t'kickstart.ftp.dir' => '{$ftpOptions['root']}', \t'kickstart.ftp.tempdir' => '{$tempdir}' ENDDATA; } $data .= ');'; // Remove the old file, if it's there... JLoader::import('joomla.filesystem.file'); $componentPaths = array(); $componentPaths['admin'] = dirname(__FILE__) . '/tmp'; $configpath = $componentPaths['admin'] . '/restoration.php'; if (file_exists($configpath)) { if (!@unlink($configpath)) { JFile::delete($configpath); } } // Write the new file. First try directly. if (function_exists('file_put_contents')) { $result = @file_put_contents($configpath, $data); if ($result !== FALSE) { $result = TRUE; } } else { $fp = @fopen($configpath, 'wt'); if ($fp !== FALSE) { $result = @fwrite($fp, $data); if ($result !== FALSE) { $result = TRUE; } @fclose($fp); } } if ($result === FALSE) { $result = JFile::write($configpath, $data); } return $password; }