* @copyright 2015 PrestaShow.pl
* @license http://PrestaShow.pl/license
*/
require_once dirname(__FILE__) . "/../../config.php";
class PShowImporterMainController extends PShowAdminController
{
public $default_action = 'index';
public $select_menu_tab = 'subtab-PShowImporterMain';
public function getlogAction()
{
$filepath = _IMPORT_LOG_PATH_ . urldecode(Tools::getValue('filename')) . '.log';
die(PShow_File::getLogFile($filepath));
}
public function __construct()
{
parent::__construct();
$this->controller_displayName = $this->l('Sample Controller');
$modulePath = getModulePath(__FILE__);
$req = array(
$modulePath . 'import-status',
$modulePath . 'import-log',
$modulePath . 'files-data',
$modulePath . 'upload',
$modulePath . 'update',
$modulePath . 'update/backups',
$modulePath . 'update/tmp',
);
// create required directories
foreach ($req as $path) {
if (!is_dir($path)) {
mkdir($path);
chmod($path, 0777);
}
}
// remove old log files
PShow_Log::removeOldFiles();
global $smarty;
$smarty->assign('category_matching', 2);
$smarty->assign('lang_iso', $this->context->language->iso_code);
$smarty->assign('pshowimporter_logs', glob(getModulePath(__FILE__) . "log/*.log"));
}
public function updateChmod($path = null)
{
if ($path === null)
$path = getModulePath(__FILE__) . '*';
$files = glob($path);
foreach ($files as $file) {
if (is_dir($file))
$this->updateChmod($file . "/*");
@chmod($file, 0777);
}
}
/**
* Overrides reinstallation
*/
public function reinstallOverridesAction()
{
if (Tools::getValue('start')) {
$module = Module::getInstanceByName('pshowimporter');
$test = true;
try {
$module->uninstallOverrides();
$this->alerts[] = array('success', $this->l('Override removed'));
} catch (Exception $e) {
$test = false;
$this->alerts[] = array('danger', sprintf(Tools::displayError('Unable to uninstall override: %s'), $e->getMessage()));
}
try {
$module->installOverrides();
$this->alerts[] = array('success', $this->l('Override installed'));
} catch (Exception $e) {
$test = false;
$this->alerts[] = array('danger', sprintf(Tools::displayError('Unable to install override: %s'), $e->getMessage()));
$msg = $this->l("Unfortunately, we failed to install new overrides.") . '
';
$matches = array();
preg_match_all('~The method ([a-zA-Z0-9\-\_]+) in the class ([a-zA-Z0-9\-\_]+) is already overridden~', $e->getMessage(), $matches);
if (isset($matches[1]) && isset($matches[2])) {
$reflector = new ReflectionClass($matches[2][0]);
$filepath = str_replace(_PS_ROOT_DIR_, '', $reflector->getFileName());
$msg .= sprintf($this->l("Function `%s` in the file %s` has been previously overwritten manually or by another module. "
. "You need to remove the `%s` function from the `%s` file or manually install the overwrite in the store."), $matches[1][0], $filepath, $matches[1][0], $filepath) . '
';
$msg .= $this->l("If you need help, write to us and send the FTP access data.") . '
';
$msg .= 'https://helpdesk.prestashow.pl';
$this->alerts[] = array('danger', $msg);
}
$module->uninstallOverrides();
}
if ($test) {
$this->alerts[] = array('success', $this->l('Module is now ready to use!'));
}
}
$this->action_displayName = $this->l('Reinstall overrides');
}
public function sshAction()
{
$this->action_displayName = $this->l('How to run import via SSH');
}
public function cloneAction()
{
$this->action_displayName = $this->l('Clone file');
$filename = Tools::getValue('file');
$filepath = false;
$pathes = glob(_MODULE_UPLOAD_PATH_ . $filename . '.*');
foreach ($pathes as $path) {
if (!in_array(pathinfo($path, PATHINFO_EXTENSION), explode(',', _IMPORT_FILE_EXTENSIONS_))) {
continue;
}
$filepath = $path;
break;
}
if ($filepath) {
$clonepath = str_replace($filename, time() . '-' . PShow_File::findFreeNameToUpload(Tools::substr($filename, (stripos($filename, '-') + 1))), $filepath);
file_put_contents($clonepath, PShow_File::file_get_contents($filepath));
if (file_exists(_MODULE_UPLOAD_PATH_ . $filename . '.txt')) {
file_put_contents(
str_replace(pathinfo($path, PATHINFO_EXTENSION), 'txt', $clonepath), PShow_File::file_get_contents(_MODULE_UPLOAD_PATH_ . $filename . '.txt')
);
}
PShow_Log::add(
_IMPORT_LOG_PATH_ . $filename . ".log", PShow_Log::FILE_CLONE, str_replace(_MODULE_UPLOAD_PATH_, '', $clonepath)
);
$this->alerts[] = array('success', $this->l('File cloned successfully'));
}
Cache::getInstance()->delete('pshowimporter_files_list');
$this->action = 'index';
$this->indexAction();
}
public function redownloadAction()
{
$this->action_displayName = $this->l('Update this file from URL');
$filename = Tools::getValue('file');
$filepath = false;
$pathes = glob(_MODULE_UPLOAD_PATH_ . $filename . '.*');
foreach ($pathes as $path) {
if (!in_array(pathinfo($path, PATHINFO_EXTENSION), explode(',', _IMPORT_FILE_EXTENSIONS_))) {
continue;
}
$filepath = $path;
break;
}
$url = PShow_File::file_get_contents(str_replace('.' . pathinfo($filepath, PATHINFO_EXTENSION), ".txt", $filepath));
if (!$url) {
$this->alerts[] = array('error', $this->l('URL not found'));
$this->action = 'index';
$this->indexAction();
return;
}
if (stripos($url, 'http://') === false && stripos($url, 'https://') === false) {
$url = 'http://' . $url;
}
if (PShow_File::updateFileFromURL($filepath, $url)) {
$this->alerts[] = array('success', $this->l('File updated successfully'));
} else {
$this->alerts[] = array('warning', $this->l('Something went wrong! Check file log.'));
}
$this->action = 'index';
$this->indexAction();
return;
}
public function indexAction()
{
$this->addJS(__PS_BASE_URI__ . 'modules/pshowimporter/views/js/files_list.js');
$this->addCSS(__PS_BASE_URI__ . 'modules/pshowimporter/views/css/full-screen-loader.css');
if (version_compare(_PS_VERSION_, '1.6.0', '<')) {
$this->addCSS(__PS_BASE_URI__ . 'modules/pshowimporter/views/css/font-awesome.min.css');
$this->addCSS(__PS_BASE_URI__ . 'modules/pshowimporter/views/css/import_ps15.css');
$this->addJS(__PS_BASE_URI__ . 'modules/pshowimporter/views/js/import_ps15.js');
}
$this->action_displayName = $this->l('Informations');
$smarty = $this->context->smarty;
if ((int) ini_get("upload_max_filesize") < 100) {
$smarty->assign('upload_max_filesize_error', true);
}
if ((int) ini_get("post_max_size") < 150) {
$smarty->assign('post_max_size_error', true);
}
if ((int) ini_get("memory_limit") < 200) {
$smarty->assign('memory_limit_error', true);
}
if (Tools::isSubmit('submit_sendfile')) {
$this->getFile();
}
if (Tools::getValue('removefile')) {
$this->removeFile();
}
if (Tools::getValue('removesamplefiles')) {
$this->removeSampleFiles();
}
if (Tools::getValue('removeconfig')) {
$this->removeConfig();
}
$files_list = PShow_File::getFilesList();
$sortby = Tools::getValue('sortby');
$sortorder = Tools::getValue('sortorder');
if ($sortby && $sortorder) {
$files_list = PShow_File::sortFiles($files_list, $sortby, $sortorder);
}
$smarty->assign('files', $files_list);
$smarty->assign('modulePath', getModulePath(__FILE__));
$smarty->assign('writeableImportConfig', is_writeable(getModulePath(__FILE__) . 'import-config'));
$smarty->assign('writeableImportLog', is_writeable(getModulePath(__FILE__) . 'import-log'));
$smarty->assign('writeableImportStatus', is_writeable(getModulePath(__FILE__) . 'import-status'));
$smarty->assign('writeableUpload', is_writeable(getModulePath(__FILE__) . 'upload'));
$smarty->assign('writeableUpdate', is_writeable(getModulePath(__FILE__) . 'update'));
$smarty->assign('MODULE_PATH', getModulePath(__FILE__));
$isDuringImport = self::isDuringImport();
$smarty->assign('isDuringImport', $isDuringImport);
if ($isDuringImport) {
$smarty->assign('importStartDate', self::getImportStartDate());
$smarty->assign('importUpdateDate', self::getImportUpdateDate());
$smarty->assign('imported', self::getImported());
$smarty->assign('isDuringCronImport', file_exists(getModulePath(__FILE__) . 'direct_import_now'));
}
$this->context->smarty->assign('is_multishop', class_exists('PShow_Import_Object_Multistore'));
if (version_compare(LIBXML_DOTTED_VERSION, '2.9.3') == 0) {
$this->alerts[] = array(
'danger',
$this->l('You are using the php extension `libxml` in version 2.9.3.')
. ' ' . $this->l('Many users report a problem with the module\'s functionality with this version of the extension.') . ' '
. $this->l('Contact your hosting provider to change the version of this extension.')
);
}
if (Shop::isFeatureActive() && !class_exists('PShow_Import_Object_Multistore')) {
$this->alerts[] = array(
'danger',
$this->l('You are using multistore.')
. ' ' . $this->l('The module may operate incorrectly.') . ' '
. $this->l('For a special addition to the multistore options, go to this address')
. ': http://prestashow.pl/pl//26-.html'
);
}
/**
* Multistore support
*/
if (class_exists('PShow_Import_Object_Multistore') &&
(!property_exists('Shop', 'mod_from_pshowimporter_v') || version_compare('2.193', Shop::$mod_from_pshowimporter_v) != 0)) {
$this->alerts[] = array('danger', $this->l('Overrides reinstallation required!')
. ' click here');
}
/**
* Queue support
*/
$queue = PShow_Queue::getInstance()->getQueue();
$queue_list = array();
foreach ($queue as $filename) {
foreach ($files_list as $file) {
if ($file['time'] . '-' . $file['filename'] == $filename) {
array_push($queue_list, $file);
break;
}
}
}
$this->context->smarty->assign('import_queue', $queue);
$this->context->smarty->assign('queue_list', $queue_list);
/**
* pshowHook below side menu
*/
$pshowHook_below_side_menu = '
' . $this->l('Multistore support') . ':
'; if (PShow_Addon::exists('Multistore')) { $pshowHook_below_side_menu .= '' . $this->l('Installed and ready to use') . '
'; } else { $pshowHook_below_side_menu .= '' . '' . $this->l('Not available, click to install') . '
'; } $pshowHook_below_side_menu .= '' . $this->l('Advanced stock management') . ':
'; if (PShow_Addon::exists('ASM')) { $pshowHook_below_side_menu .= '' . $this->l('Installed and ready to use') . '
'; } else { $pshowHook_below_side_menu .= ''; } $pshowHook_below_side_menu .= '