-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIolyInstallerCore.php
More file actions
655 lines (622 loc) · 24.6 KB
/
IolyInstallerCore.php
File metadata and controls
655 lines (622 loc) · 24.6 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
<?php
/**
* ioly installer class
* Installs and optionally activates modules in the shop via ioly module manager.
*
* @version 1.8.0
* @package ioly
* @author Stefan Moises <moises@shoptimax.de>
* @copyright shoptimax GmbH, 2016-2017
*/
namespace ioly;
if (file_exists(dirname(__FILE__) . '/../../../bootstrap.php')) {
include_once dirname(__FILE__) . '/../../../bootstrap.php';
} else if (file_exists(dirname(__FILE__) . '/../../../source/bootstrap.php')) {
// OXID 6
include_once dirname(__FILE__) . '/../../../source/bootstrap.php';
}
if (file_exists(dirname(__FILE__) . '/../../../IolyInstallerConfig.php')) {
include_once dirname(__FILE__) . '/../../../IolyInstallerConfig.php';
} else if (file_exists(dirname(__FILE__) . '/../../../source/IolyInstallerConfig.php')) {
// OXID 6
include_once dirname(__FILE__) . '/../../../source/IolyInstallerConfig.php';
}
/**
* Class IolyInstallerCore
* No need to change anything here, all settings are in IolyInstallerConfig.php!
*
* @package ioly
*/
class IolyInstallerCore
{
/**
* Ioly Core
* @var Ioly
*/
protected static $ioly;
/**
* Ioly Config
* @var \ioly\IolyInstallerConfig
*/
protected static $config;
/**
* @var array
*/
protected static $domainsLocal;
/**
* The http port to use
* @var int
*/
protected static $portLocal;
/**
* @var array
*/
protected static $shoptifindDataLocal;
/**
* @var oxmodulelist $oModuleList
*/
protected static $oModuleList;
/**
* All available modules
* @var array $aModules
*/
protected static $aModules;
/**
* @var int
*/
private static $_startTime = 0;
/**
* @var string
*/
private static $_shopBaseDir = null;
/**
* IolyInstaller constructor.
*/
public static function constructStatic()
{
self::$_startTime = microtime(true);
self::$config = new IolyInstallerConfig();
// problem with output buffer since oxUtilsObject::oxNew() - first called in generateViews() -
// triggers oxbase::getViewName() somehow calls
// oxUtilsServer::setOxCookie() which dies with an Exception if anything has been echo'd before ...
ob_start();
echo "\nIolyInstaller constructStatic ... \n";
self::$ioly = new \ioly\ioly();
// OXID 6?
if (file_exists(dirname(__FILE__) . '/../../../source/bootstrap.php')) {
self::$_shopBaseDir = dirname(__FILE__) . '/../../../source/';
} else {
self::$_shopBaseDir = dirname(__FILE__) . '/../../../';
}
echo "\nIolyInstaller setting base vars ... \n";
self::$ioly->setSystemBasePath(self::$_shopBaseDir);
self::$ioly->setSystemVersion(self::$config->getMainShopVersion());
// add custom smx cookbook!
if (self::$config->getCookbookPath() != '') {
// add new OXID Connector cookbook instead of old ioly cookbook!
self::$ioly->removeCookbook('ioly');
self::$ioly->removeCookbook('omc');
self::$ioly->addCookbook('omc', 'https://github.com/shoptimax/OXID-Module-Connector/archive/recipes.zip');
// delete first to update ...
self::$ioly->removeCookbook('smx');
self::$ioly->addCookbook('smx', self::$config->getCookbookPath());
}
// init OXID classes and vars?
if (getenv('IOLY_ONLY_INSTALL') != "true") {
$oConfig = \oxRegistry::getConfig();
// avoid problems if views are already broken
$oConfig->setConfigParam('blSkipViewUsage', true);
// call getBaseLanguage() to early set the cookie to prevent from
// headers already sent errors!
$sLang = \oxRegistry::getLang()->getBaseLanguage();
//self::$_shopBaseDir = $oConfig->getConfigParam('sShopDir');
echo "\nIolyInstaller init ... \n";
self::init();
// all domains for local
self::$domainsLocal = $oConfig->getConfigParam('domainsLocal') != null ? $oConfig->getConfigParam('domainsLocal') : array();
// local port
self::$portLocal = $oConfig->getConfigParam('portLocal') != null ? $oConfig->getConfigParam('portLocal') : "";
self::$shoptifindDataLocal = $oConfig->getConfigParam('shoptifindDataLocal') != null ? $oConfig->getConfigParam('shoptifindDataLocal') : array();
// set status marker
self::handleStatusFile(true);
}
echo "\nIolyInstaller constructStatic done ... \n";
}
/**
* Write a status file when ioly installation is running
* and delete if afterwards
*
* @param boolean $create
*/
public static function handleStatusFile($create = true)
{
$filePath = self::$_shopBaseDir . "/.ioly_install_running";
if ($create) {
$data = date("Y-m-d H:i:s");
file_put_contents($filePath, $data, LOCK_EX);
echo "\nIolyInstaller status file written!";
} else {
if (file_exists($filePath)) {
@unlink($filePath);
echo "\nIolyInstaller status file deleted!";
}
}
}
/**
* Main function
* Runs after all composer installs are finished
* @param string $vendorDir The path to the composer vendor dir.
* @param boolean $skipInstall Skip ioly module installation?
* @param boolean $skipActivation Skip module activation?
* @param boolean $skipClean Skip module oxconfig cleanup?
*/
public static function run($vendorDir, $skipInstall = false, $skipActivation = false, $skipClean = false)
{
include $vendorDir . '/autoload.php';
echo "\nIolyInstaller running - skipInstall: $skipInstall skipActivation: $skipActivation skipClean: $skipClean\n";
try {
// before we do anything, clean tmp and create views!
if (!$skipClean) {
self::cleanup();
}
if (!$skipInstall) {
self::doInstall();
}
if (!$skipActivation) {
if (!$skipClean) {
self::cleanConfig();
self::emptyTmp();
}
self::initModuleSettings();
if (self::$config->isActivateAllExceptBlacklist()) {
self::activateModulesExceptBlacklisted();
} else {
self::activateModulesWhitelisted();
}
self::updateConfig();
self::setupShoptifind();
self::finish();
}
} catch (Exception $ex) {
echo "\nException occurred! " . $ex->getMessage() . "\n" . $ex->getTraceAsString();
}
ob_end_flush();
}
/**
* Initial cleanup and preparation
*/
protected static function init()
{
echo "\nIolyInstaller rm tmp files ... \n";
// TODO - keep .htaccess and .gitignore files in tmp/**
exec('rm -Rf ' . self::$_shopBaseDir . "/tmp/*");
}
/**
* Clean module info from DB
*/
protected static function cleanConfig()
{
// remove module entries from DB
echo "\nIolyInstaller cleaning DB ... \n";
try {
$aShopIds = self::getShopIdsFromString(self::$config->getShopIds());
foreach ($aShopIds as $shopId) {
\oxDb::getDb()->execute("DELETE FROM oxconfig WHERE oxvarname LIKE '%Module%' AND OXSHOPID='$shopId'");
// delete template block settings, too
\oxDb::getDb()->execute("DELETE FROM oxtplblocks WHERE OXSHOPID='$shopId'");
}
} catch (\Exception $ex) {
echo "\nException cleaning DB: {$ex->getMessage()} \n";
}
echo "\nIolyInstaller DB clean ... \n";
}
/**
* Empty tmp, create views
*/
protected static function cleanup()
{
self::emptyTmp();
self::generateViews(self::$config->getShopIds());
}
/**
* If the oxconfig table is cleaned from module settings,
* we need to set the module paths for subshops here ...
*/
protected static function initModuleSettings()
{
echo "\n\n/**********************************************";
echo "\ninitModuleSettings, setting aModulePaths ...";
echo "\n/**********************************************\n";
$oConfig = \oxRegistry::getConfig();
echo "\ngetting modules list from dir ...";
self::$oModuleList = oxNew('oxModuleList');
$sModulesDir = $oConfig->getModulesDir();
echo "\ndir: $sModulesDir";
// call this, in case of the oxconfig table doesn't have any module info yet!
self::$aModules = self::$oModuleList->getModulesFromDir($sModulesDir);
echo "\ngetting shopids, setting aModulePaths ...";
$aShopIds = self::getShopIdsFromString(self::$config->getShopIds());
$aModulePaths = $oConfig->getShopConfVar('aModulePaths', $aShopIds[0]);
foreach ($aShopIds as $shopId) {
echo "\nsetting aModulePaths for shop $shopId...";
$oConfig->setShopId($shopId);
// OXID seems to have a bug in oxmodulelist.php and only saves the module paths for shop id 1, so
// we save it for every shop id manually here!
$oConfig->saveShopConfVar('aarr', 'aModulePaths', $aModulePaths, $shopId);
}
ob_flush();
}
/**
* Un-/install modules
*/
protected static function doInstall()
{
echo "\n\n/**********************************************";
echo "\nINSTALLING IOLY MODULES ...";
echo "\n/**********************************************\n";
ob_flush();
foreach (self::$config->getAPackages() as $package => $aData) {
$version = $aData['version'];
$forceReinstall = $aData['forcereinstall'];
if ($aData['uninstallfirst']) {
try {
if (self::$ioly->isInstalledInVersion($package, $version)) {
self::$ioly->uninstall($package, $version);
}
} catch (Exception $ex) {
echo "\nError un-installing package '$package': " . $ex->getMessage();
}
}
if ($forceReinstall || !self::$ioly->isInstalledInVersion($package, $version)) {
try {
self::$ioly->install($package, $version, $aData['preserveFiles']);
echo "\nPackage: $package installed in version: $version";
// patch .gitignore?
if (self::$config->isPatchGitIngore()) {
self::patchGitIgnore($package, $version, $aData['preserveFiles']);
}
} catch (Exception $ex) {
echo "\nError installing package '$package': " . $ex->getMessage();
}
} else {
echo "\nPackage $package already installed in version: $version";
}
ob_flush();
}
}
/**
* Add ignore entries to .gitignore
* @param string $package The module package
* @param string $version The module version
* @param bool $preserveFiles Preserve available files in dir?
*/
protected static function patchGitIgnore($package, $version, $preserveFiles)
{
// get all module files from ioly
$aModuleFiles = self::$ioly->getFileList($package, $version);
if (!$aModuleFiles || !is_array($aModuleFiles)) {
echo "\nWarning: no (additional) module files for package $package, version $version";
return;
}
// do not preserve files, so add complete module dir to .gitignore
if (!$preserveFiles) {
$modulePath = '';
foreach ($aModuleFiles as $file => $sha) {
if (($pos = strpos($file, "/metadata.php")) !== false) {
$modulePath = substr($file, 0, $pos+1);
}
}
if ($modulePath != '') {
// remove leading slash
if (substr($modulePath, 0, 1) == '/') {
$modulePath = substr($modulePath, 1);
}
echo "\nmodulePath: $modulePath";
$data = "\n# added by IolyInstaller for package {$package}, version {$version}\n" . $modulePath . "\n#end package {$package}\n";
// try folder above
$filePath = self::$_shopBaseDir . '/../.gitignore';
if (!file_exists($filePath)) {
$filePath = self::$_shopBaseDir . '/.gitignore';
}
$contents = "";
if (file_exists($filePath)) {
$contents = file_get_contents($filePath);
}
if (strpos($contents, $modulePath) === false) {
file_put_contents($filePath, $data, FILE_APPEND | LOCK_EX);
}
}
} else {
// preserve existing files and
// add every single file installed by ioly, so that only preserved files are visible in git!
$data = "\n# added by IolyInstaller for package {$package}, version {$version}\n";
// try folder above
$filePath = self::$_shopBaseDir . '/../.gitignore';
if (!file_exists($filePath)) {
$filePath = self::$_shopBaseDir . '/.gitignore';
}
$contents = "";
if (file_exists($filePath)) {
$contents = file_get_contents($filePath);
}
foreach ($aModuleFiles as $file => $sha) {
if (substr($file, 0, 1) == '/') {
$file = substr($file, 1);
}
if (strpos($contents, $file) === false) {
$data .= $file . "\n";
}
//echo "\nmoduleFile: $file";
}
$data .= "#end package {$package}\n";
if (strpos($contents, "package {$package}, version {$version}") === false) {
file_put_contents($filePath, $data, FILE_APPEND | LOCK_EX);
}
}
}
/**
* Activate ALL modules, except those blacklisted
*/
protected static function activateModulesExceptBlacklisted()
{
/**********************************************
* ACTIVATE MODULES IN DIR EXCEPT BLACKLISTED?
*********************************************/
echo "\n\n/**********************************************";
echo "\nACTIVATING MODULES ...";
echo "\n/**********************************************\n";
ob_flush();
foreach (self::$aModules as $sModuleId => $oModule) {
echo "\nChecking Module: $sModuleId";
$sFilteredIds = self::getFilteredShopIdsForModule($sModuleId);
if ($sFilteredIds != '') {
self::activateModule($sModuleId, $sFilteredIds);
echo "\nPackage: $sModuleId activated in subshops: $sFilteredIds ";
}
ob_flush();
}
}
/**
* Activate only whitelisted modules
*/
protected static function activateModulesWhitelisted()
{
echo "\n\n/**********************************************";
echo "\nACTIVATING MODULES ...";
echo "\n/**********************************************\n";
ob_flush();
$aShopIds = self::getShopIdsFromString(self::$config->getShopIds());
foreach ($aShopIds as $shopId) {
if (!self::$config->getAModuleWhiteList($shopId)) {
echo "\nNo whitelist for subshop: $shopId ";
continue;
}
foreach (self::$config->getAModuleWhiteList($shopId) as $sModuleId) {
self::activateModule($sModuleId, $shopId);
echo "\nPackage: $sModuleId activated in subshop: $shopId ";
ob_flush();
}
}
}
/**
* Activate module for deployment
* @param string $sModuleId
* @param string $sFilteredIds
* @param bool $deactivateFirst
* @return bool
*/
protected static function activateModule($sModuleId, $sFilteredIds, $deactivateFirst = false)
{
$activated = false;
$oConfig = \oxRegistry::getConfig();
$aShopIds = self::getShopIdsFromString($sFilteredIds);
foreach ($aShopIds as $shopId) {
try {
$oConfig->setShopId($shopId);
$oModule = oxNew('oxModule');
if ($oModule->load($sModuleId)) {
echo "\nLoaded $sModuleId module, trying to activate ...";
/** @var oxModuleCache $oModuleCache */
$oModuleCache = oxNew('oxModuleCache', $oModule);
/** @var \oxModuleInstaller $oModuleInstaller */
$oModuleInstaller = oxNew('oxModuleInstaller', $oModuleCache);
if ($deactivateFirst && $oModuleInstaller->deactivate($oModule)) {
echo "\nDeactivated $sModuleId in Shop $shopId";
}
if ($oModuleInstaller->activate($oModule)) {
echo "\nActivated $sModuleId in Shop $shopId";
$activated = true;
} else {
echo "\n***** PROBLEM ACTIVATING $sModuleId IN SHOP $shopId!!! *****";
$activated = false;
}
} else {
echo "\n***** PROBLEM LOADING $sModuleId IN SHOP $shopId!!! *****";
$activated = false;
}
ob_flush();
} catch (\Exception $ex) {
echo "\nError activating module '$sModuleId': " . $ex->getMessage() . "\n" . $ex->getTraceAsString();
}
}
return $activated;
}
/**
* change shop mall URLs etc. in DB!
*/
protected static function updateConfig()
{
if (self::$domainsLocal && is_array(self::$domainsLocal)) {
foreach (self::$domainsLocal as $shopId => $mallUrl) {
$oConfig = \oxRegistry::getConfig();
$oConfig->setShopId($shopId);
$sUrl = 'http://' . $mallUrl;
if (self::$portLocal != '80') {
$sUrl .= ":" . self::$portLocal;
}
echo "\nSetting mall url for shopid: $shopId to $sUrl";
$oConfig->saveShopConfVar('str', 'sMallShopURL', $sUrl, $shopId);
$oConfig->saveShopConfVar('str', 'sMallSSLShopURL', $sUrl, $shopId);
// no varnish!?
if (self::$config->isBlDisableVarnish()) {
$oConfig->saveShopConfVar('bool', 'blReverseProxyActive', 'false', $shopId);
}
if (self::$config->isBlSetLangUrls()) {
// lang URLs! use the same URL for all languages for now ...
$aLangIds = \oxRegistry::getLang()->getAllShopLanguageIds();
$aUrls = array();
foreach ($aLangIds as $idx => $sLangId) {
echo "\nLangId: $sLangId";
$aUrls[] = $sUrl;
}
echo "\nAll lang URLs: " . print_r($aUrls, true);
$oConfig->saveShopConfVar('arr', 'aLanguageURLs', $aUrls);
$oConfig->saveShopConfVar('arr', 'aLanguageSSLURLs', $aUrls);
}
ob_flush();
}
}
}
/**
* Set Shoptifind configuration
*/
protected static function setupShoptifind()
{
$oConfig = \oxRegistry::getConfig();
if (self::$shoptifindDataLocal && is_array(self::$shoptifindDataLocal)) {
foreach (self::$shoptifindDataLocal as $shopId => $aShoptifindData) {
$oConfig->setShopId($shopId);
echo "\nSetting Shoptifind vars for Shop $shopId - Host: " . $aShoptifindData['shoptifindHost'] . " Port: " . $aShoptifindData['shoptifindPort'] . " AppPath: " . print_r($aShoptifindData['shoptifindAppPath'], true);
$oConfig->saveShopConfVar('str', 'shoptifindHost', $aShoptifindData['shoptifindHost'], $shopId);
$oConfig->saveShopConfVar('str', 'shoptifindPort', $aShoptifindData['shoptifindPort'], $shopId);
$oConfig->saveShopConfVar('aarr', 'shoptifindAppPath', $aShoptifindData['shoptifindAppPath'], $shopId);
ob_flush();
}
}
}
/**
* Process LESS files
* special - copy less files from "less/" to "less_mirror/" folders ...
*/
protected static function updateLess()
{
if (file_exists(self::$_shopBaseDir . "/updateLessMirror.php")) {
echo "\nCopying less files ...";
include self::$_shopBaseDir . "/updateLessMirror.php";
ob_flush();
}
}
/**
* Final cleanup and preparation
*/
protected static function finish()
{
self::cleanup();
self::updateLess();
self::handleStatusFile(false);
$endTime = microtime(true) - self::$_startTime;
echo "\n\n/*************************************************************";
echo "\nIOLY SHOP SETUP DONE! Time taken: $endTime seconds";
echo "\n/*************************************************************\n";
}
/**
* Get shop id array from string
* @param string $sShopIds
*
* @return array
*/
protected static function getShopIdsFromString($sShopIds)
{
$aShopIds = array();
if ($sShopIds == "all") {
$aShopIds = \oxRegistry::getConfig()->getShopIds();
} elseif (strpos($sShopIds, ",") !== false) {
$aShopIds = explode(",", $sShopIds);
} else {
// single shopid
if (trim($sShopIds) != '') {
$aShopIds[] = $sShopIds;
}
}
return $aShopIds;
}
/**
* Filter modules per shopid based on module blacklist
* @param string $sModuleId
*
* @return string
*/
protected static function getFilteredShopIdsForModule($sModuleId)
{
$aShopIds = self::getShopIdsFromString(self::$config->getShopIds());
$sFilteredIds = array();
foreach ($aShopIds as $sShopId) {
// check if module is blacklisted for this shopid
if (!self::$config->getAModulesBlacklist($sShopId) || !in_array($sModuleId, self::$config->getAModulesBlacklist($sShopId))) {
$sFilteredIds[] = $sShopId;
}
}
return implode(",", $sFilteredIds);
}
/**
* Empty tmp dir
*/
protected static function emptyTmp()
{
echo "\nClearing tmp ... ";
$msg = "";
$tmpdir = \oxRegistry::getConfig()->getConfigParam('sCompileDir');
$d = opendir($tmpdir);
while (($filename = readdir($d)) !== false) {
// keep .htaccess and .gitignore files in tmp/**
if (strpos($filename, ".htaccess") !== false || strpos($filename, ".gitignore") !== false) {
continue;
}
$filepath = $tmpdir . $filename;
if (is_file($filepath)) {
$msg .= "\nDeleting $filepath ...";
@unlink($filepath);
}
}
$msg .= "\nTmp clean!!";
echo $msg;
}
/**
* Create views
* @param array $aShopIds
*/
protected static function generateViews($aShopIds)
{
$msg = "";
$msg .= "\nGenerating views now ... ";
//ob_flush();
$oConfig = \oxRegistry::getConfig();
// avoid problems if views are already broken
$oConfig->setConfigParam('blSkipViewUsage', true);
try {
// Admin (tools_list.php) still uses this:
$oMetaData = oxNew('oxDbMetaDataHandler');
$blViewSuccess = $oMetaData->updateViews();
$msg .= "\nGlobal views generated: $blViewSuccess";
} catch (\Exception $ex) {
$msg .= "\nError generating views: " . $ex->getMessage();
}
/*
echo "\nGenerating views for shopids: " . $aShopIds;
if (!is_array($aShopIds)) {
$aShopIds = self::getShopIdsFromString($aShopIds);
}
echo "\nshopid array: " . print_r($aShopIds, true);
$oShop = oxNew('oxShop');
$oShop->generateViews();
foreach ($aShopIds as $sShopId) {
$oShop->load($sShopId);
$msg .= "\nGenerating views for ShopID $sShopId ...";
$oShop->generateViews();
}
*/
echo $msg;
ob_flush();
}
}
IolyInstallerCore::constructStatic();