Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUPEE-3941 - Addresses extension-related issues #3

Merged
merged 1 commit into from
Nov 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion downloader/Maged/Model/Connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ public function saveConfigPost($p)
*/
public function checkExtensionKey($id, &$match)
{
return preg_match('#^([^ ]+)\/([^-]+)(-.+)?$#', $id, $match);
if (preg_match('#^(.+)\/(.+)-([\.\d]+)$#', $id, $match)) {
return $match;
}
return preg_match('#^(.+)\/(.+)$#', $id, $match);
}
}
169 changes: 169 additions & 0 deletions downloader/lib/Mage/Connect/Backup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Connect
* @copyright Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Class to backup files before extension installation
*
* @category Mage
* @package Mage_Connect
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Connect_Backup
{
/**
* Prefix for backuped files
*
* @var string
*/
protected $_prefix = '_backup_';

/**
* Array of available to overwrite type of files
*
* @var array
*/
protected $_fileTypes = array();

/**
* List of files to backup files
*
* @var array
*/
protected $_fileList = array();

/**
* Get available file types for backup
*
* @return array
*/
public function getFileTypes()
{
return $this->_fileTypes;
}

/**
* Set available file types for backup
*
* @param array $types
*/
public function setFileTypes(array $types)
{
foreach ($types as $type) {
$this->_fileTypes[] = $type;
}
}

/**
* Add file to files list for backup
*
* @param string $file
* @param string $rootPath
* @return void
*/
public function addFile($file, $rootPath)
{
$dest = $rootPath . DS . $file;
$type = $this->getFileType($file);
if (file_exists($dest) && in_array($type, $this->getFileTypes())) {
$this->_fileList[] = $file;
}
}

/**
* Get count of files
*
* @return int
*/
public function getFilesCount()
{
return count($this->_fileList);
}

/**
* Clear list of files
*
* @return void
*/
public function unsetAllFiles()
{
$this->_fileList = array();
}

/**
* Get list of files
*
* @return array
*/
public function getAllFiles()
{
return $this->_fileList;
}

/**
* Run backup process
*
* @param boolean $cleanUpQueue
* @return void
*/
public function run($cleanUpQueue = false)
{
if ($this->getFilesCount() > 0) {
$fileList = $this->getAllFiles();
foreach($fileList as $file) {
$this->_backupFile($file);
}
if ($cleanUpQueue) {
$this->unsetAllFiles();
}
}
}

/**
* Get File type
*
* @param string $file
* @return string
*/
public function getFileType($file)
{
return pathinfo($file, PATHINFO_EXTENSION);
}

/**
* Backup file
*
* @param string $file
* @return void
*/
private function _backupFile($file)
{
$type = $this->getFileType($file);
if ($type && $type != '') {
$newName = $this->_prefix . time() . '.' . $type;
@rename($file, str_replace('.' . $type, $newName, $file));
}
}
}
24 changes: 22 additions & 2 deletions downloader/lib/Mage/Connect/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class Mage_Connect_Command
*/
protected static $_validator = null;

/**
* Backup instance
*
* @var Mage_Connect_Backup
*/
protected static $_backup = null;

/**
* Rest instance
*
Expand Down Expand Up @@ -248,6 +255,19 @@ public function validator()
return self::$_validator;
}

/**
* Get backup object
*
* @return Mage_Connect_Backup
*/
public function backup()
{
if(is_null(self::$_backup)) {
self::$_backup = new Mage_Connect_Backup();
}
return self::$_backup;
}

/**
* Get rest object
*
Expand Down Expand Up @@ -422,8 +442,8 @@ public function splitPackageArgs(array & $params)
return;
}
if(preg_match("@([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)@ims", $params[0], $subs)) {
$params[0] = $subs[2];
array_unshift($params, $subs[1]);
$params[0] = $subs[2];
array_unshift($params, $subs[1]);
}
}

Expand Down
38 changes: 30 additions & 8 deletions downloader/lib/Mage/Connect/Command/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ public function doInstall($command, $options, $params, $objects = array())
@mkdir($config->magento_root . $dirTmp,0777,true);
@mkdir($config->magento_root . $dirMedia,0777,true);
$isWritable = is_writable($config->magento_root)
&& is_writable($config->magento_root . DIRECTORY_SEPARATOR . $config->downloader_path)
&& is_writable($config->magento_root . $dirCache)
&& is_writable($config->magento_root . $dirTmp)
&& is_writable($config->magento_root . $dirMedia);
&& is_writable($config->magento_root . DIRECTORY_SEPARATOR . $config->downloader_path)
&& is_writable($config->magento_root . $dirCache)
&& is_writable($config->magento_root . $dirTmp)
&& is_writable($config->magento_root . $dirMedia);
$err = "Please check for sufficient write file permissions.";
}
$isWritable = $isWritable && is_writable($config->magento_root . $dirMedia)
&& is_writable($config->magento_root . $dirCache)
&& is_writable($config->magento_root . $dirTmp);
&& is_writable($config->magento_root . $dirCache)
&& is_writable($config->magento_root . $dirTmp);
if (!$isWritable) {
$this->doError($command, $err);
throw new Exception(
Expand Down Expand Up @@ -316,7 +316,7 @@ public function doInstall($command, $options, $params, $objects = array())
if ($ftp) {
$cwd=$ftpObj->getcwd();
$dir=$cwd . DIRECTORY_SEPARATOR .$config->downloader_path . DIRECTORY_SEPARATOR
. Mage_Connect_Config::DEFAULT_CACHE_PATH . DIRECTORY_SEPARATOR . trim( $pChan, "\\/");
. Mage_Connect_Config::DEFAULT_CACHE_PATH . DIRECTORY_SEPARATOR . trim( $pChan, "\\/");
$ftpObj->mkdirRecursive($dir,0777);
$ftpObj->chdir($cwd);
} else {
Expand Down Expand Up @@ -346,11 +346,33 @@ public function doInstall($command, $options, $params, $objects = array())

$package = new Mage_Connect_Package($file);
if ($clearInstallMode && $pInstallState != 'upgrade' && !$installAll) {
$this->validator()->validateContents($package->getContents(), $config);
$contents = $package->getContents();
$this->backup()->setFileTypes(array('csv', 'html'));
$typesToBackup = $this->backup()->getFileTypes();
$this->validator()->validateContents($contents, $config, $typesToBackup);
$errors = $this->validator()->getErrors();
if (count($errors)) {
throw new Exception("Package '{$pName}' is invalid\n" . implode("\n", $errors));
}

$targetPath = rtrim($config->magento_root, "\\/");
foreach ($contents as $filePath) {
$this->backup()->addFile($filePath, $targetPath);
}

if ($this->backup()->getFilesCount() > 0) {
$this->ui()->output('<br/>');
$this->ui()->output('Backup of following files will be created :');
$this->ui()->output('<br/>');
$this->backup()->run();
$this->ui()->output(implode('<br/>', $this->backup()->getAllFiles()));
$this->ui()->output('<br/>');
$this->ui()->output(
$this->backup()->getFilesCount() . ' files was overwritten by installed extension.'
);
$this->ui()->output('<br/>');
$this->backup()->unsetAllFiles();
}
}

$conflicts = $package->checkPhpDependencies();
Expand Down
Loading