forked from thirtybees/psonesixmigrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsonesixmigrator.php
222 lines (199 loc) · 8.5 KB
/
psonesixmigrator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* 2007-2016 PrestaShop
*
* Thirty Bees is an extension to the PrestaShop e-commerce software developed by PrestaShop SA
* Copyright (C) 2017 Thirty Bees
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 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/afl-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 [email protected] so we can send you a copy immediately.
*
* @author Thirty Bees <[email protected]>
* @author PrestaShop SA <[email protected]>
* @copyright 2017 Thirty Bees
* @copyright 2007-2016 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* PrestaShop is an internationally registered trademark & property of PrestaShop SA
*
*
* Don't forget: It should be possible to parse this file on PHP 5.2+, the other files are allowed to be 5.5+
*/
/**
* Class PsOneSixMigrator
*
* @since 1.0.0
*/
class PsOneSixMigrator extends Module
{
/**
* PsOneSixMigrator constructor.
*
* @since 1.0.0
*/
public function __construct()
{
$this->name = 'psonesixmigrator';
$this->tab = 'administration';
$this->author = 'thirty bees';
$this->version = '1.0.0';
$this->need_instance = 1;
$this->bootstrap = true;
parent::__construct();
$this->multishop_context = Shop::CONTEXT_ALL;
$this->displayName = $this->l('1-Click PrestaShop 1.6 => thirty bees migration');
$this->description = $this->l('Provides an automated way to fully migrate your PrestaShop 1.6 store to thirty bees.');
$this->ps_versions_compliancy = array('min' => '1.6.0.0', 'max' => '1.6.999.999');
}
/**
* @return bool
*
* @since 1.0.0
*/
public function install()
{
/* If the "AdminThirtyBeesMigrate" tab does not exist yet, create it */
if (!$idTab = Tab::getIdFromClassName('AdminThirtyBeesMigrate')) {
$tab = new Tab();
$tab->class_name = 'AdminThirtyBeesMigrate';
$tab->module = $this->name;
$tab->id_parent = -1;
foreach (Language::getLanguages(false) as $lang) {
$tab->name[(int) $lang['id_lang']] = 'thirty bees migration';
}
if (!$tab->save()) {
return $this->abortInstall($this->l('Unable to create the "AdminThirtyBeesMigrate" tab'));
}
} else {
$tab = new Tab((int) $idTab);
}
/* Update the "AdminThirtyBeesMigrate" tab id in database or exit */
if (Validate::isLoadedObject($tab)) {
Configuration::updateValue('PS_AUTOUPDATE_MODULE_IDTAB', (int) $tab->id);
} else {
return $this->abortInstall($this->l('Unable to load the "AdminThirtyBeesMigrate" tab'));
}
/* Check that the 1-click upgrade working directory is existing or create it */
$autoupgradeDir = _PS_ADMIN_DIR_.DIRECTORY_SEPARATOR.'autoupgrade';
if (!file_exists($autoupgradeDir) && !@mkdir($autoupgradeDir)) {
return $this->abortInstall(sprintf($this->l('Unable to create the directory "%s"'), $autoupgradeDir));
}
/* Make sure that the 1-click upgrade working directory is writeable */
if (!is_writable($autoupgradeDir)) {
return $this->abortInstall(sprintf($this->l('Unable to write in the directory "%s"'), $autoupgradeDir));
}
/* If a previous version of ajax-upgradetab.php exists, delete it */
if (file_exists($autoupgradeDir.DIRECTORY_SEPARATOR.'ajax-upgradetab.php')) {
@unlink($autoupgradeDir.DIRECTORY_SEPARATOR.'ajax-upgradetab.php');
}
/* Then, try to copy the newest version from the module's directory */
if (!@copy(dirname(__FILE__).DIRECTORY_SEPARATOR.'ajax-upgradetab.php', $autoupgradeDir.DIRECTORY_SEPARATOR.'ajax-upgradetab.php')) {
return $this->abortInstall(sprintf($this->l('Unable to copy ajax-upgradetab.php in %s'), $autoupgradeDir));
}
/* Make sure that the XML config directory exists */
if (!file_exists(_PS_ROOT_DIR_.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'xml') &&
!@mkdir(_PS_ROOT_DIR_.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'xml')
) {
return $this->abortInstall(sprintf($this->l('Unable to create the directory "%s"'), _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'xml'));
} else {
@chmod(_PS_ROOT_DIR_.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'xml', 0777);
}
/* Create a dummy index.php file in the XML config directory to avoid directory listing */
if (!file_exists(_PS_ROOT_DIR_.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'xml'.DIRECTORY_SEPARATOR.'index.php') &&
(file_exists(_PS_ROOT_DIR_.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'index.php') &&
!@copy(_PS_ROOT_DIR_.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'index.php', _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'xml'.DIRECTORY_SEPARATOR.'index.php'))
) {
return $this->abortInstall(sprintf($this->l('Unable to create the directory "%s"'), _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'xml'));
}
return parent::install();
}
/**
* @return bool
*
* @since 1.0.0
*/
public function uninstall()
{
/* Delete the 1-click upgrade Back-office tab */
if ($idTab = Tab::getIdFromClassName('AdminThirtyBeesMigrate')) {
$tab = new Tab((int) $idTab);
$tab->delete();
}
/* Remove the 1-click upgrade working directory */
static::removeDirectory(_PS_ADMIN_DIR_.DIRECTORY_SEPARATOR.'autoupgrade');
return parent::uninstall();
}
/**
* Get content
*
* @return void
*
* @since 1.0.0
*/
public function getContent()
{
if (version_compare(PHP_VERSION, '5.5.0', '<')) {
$this->context->controller->errors[] = $this->l('In order to migrate to thirty bees you need at least PHP version 5.5');
}
if (!extension_loaded('bcmath')) {
$this->context->controller->errors[] = sprintf($this->l('The `%s` PHP extension needs to be installed and available in order to migrate to thirty bees'), 'bcmath');
}
if (!class_exists('ZipArchive')) {
sprintf($this->l('The `%s` PHP extension needs to be installed and available in order to migrate to thirty bees'), 'zip');
}
if (!class_exists('PDO')) {
$this->context->controller->errors[] = $this->l('The MySQL PDO extension needs to be installed and available in order to migrate to thirty bees');
}
if (is_a(Cache::getInstance(), 'CacheApc') || is_a(Cache::getInstance(), 'CacheXcache')) {
// Disable Cache in these cases
$settings = file_get_contents(_PS_ROOT_DIR_.'/config/settings.inc.php');
$settings = preg_replace('/define\(\'_PS_CACHE_ENABLED_\', \'([01]?)\'\);/Ui', 'define(\'_PS_CACHE_ENABLED_\', \'0\');', $settings);
file_put_contents(_PS_ROOT_DIR_.'/config/settings.inc.php', $settings);
}
if (empty($this->context->controller->errors)) {
header('Location: '.$this->context->link->getAdminLink('AdminThirtyBeesMigrate', true));
exit;
}
}
/**
* Set installation errors and return false
*
* @param string $error Installation abortion reason
*
* @return boolean Always false
*
* @since 1.0.0
*/
protected function abortInstall($error)
{
$this->_errors[] = $error;
return false;
}
/**
* @param string $dir
*
* @since 1.00
*/
protected static function removeDirectory($dir)
{
if ($handle = @opendir($dir)) {
while (false !== ($entry = @readdir($handle))) {
if ($entry != '.' && $entry != '..') {
if (is_dir($dir.DIRECTORY_SEPARATOR.$entry) === true) {
static::removeDirectory($dir.DIRECTORY_SEPARATOR.$entry);
} else {
@unlink($dir.DIRECTORY_SEPARATOR.$entry);
}
}
}
@closedir($handle);
@rmdir($dir);
}
}
}