PageRenderTime 63ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/local/Aitoc/Aitsys/Model/Platform.php

https://bitbucket.org/shopworksnl/dusseldorpafvalcontainer
PHP | 914 lines | 696 code | 83 blank | 135 comment | 91 complexity | bd6bde138bd04dd115eda44053e2ba9d MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-2.1, WTFPL
  1. <?php
  2. final class Aitoc_Aitsys_Model_Platform extends Aitoc_Aitsys_Abstract_Model
  3. {
  4. const PLATFORMFILE_SUFFIX = '.platform.xml';
  5. const INSTALLATION_DIR = 'ait_install';
  6. /**
  7. *
  8. * @var Aitoc_Aitsys_Model_Platform
  9. */
  10. static protected $_instance;
  11. /**
  12. *
  13. * @return Aitoc_Aitsys_Model_Platform
  14. */
  15. static public function getInstance()
  16. {
  17. if (!self::$_instance)
  18. {
  19. self::$_instance = new self();
  20. try
  21. {
  22. try
  23. {
  24. self::$_instance->init();
  25. }
  26. catch (Exception $exc)
  27. {
  28. self::$_instance->block();
  29. throw $exc;
  30. }
  31. }
  32. catch (Aitoc_Aitsys_Model_Aitfilesystem_Exception $exc)
  33. {
  34. $admin = Mage::getSingleton('admin/session');
  35. if ($admin->isLoggedIn())
  36. {
  37. $msg = "Error in the file: %s. Probably it does not have write permissions.";
  38. $session = Mage::getSingleton('adminhtml/session');
  39. /* @var $session Mage_Adminhtml_Model_Session */
  40. $session->addError(Mage::helper('aitsys')->__($msg,$exc->getMessage()));
  41. }
  42. }
  43. }
  44. return self::$_instance;
  45. }
  46. protected $_block = false;
  47. protected $_modules = array();
  48. protected $_version;
  49. protected $_installDir;
  50. protected $_licenseDir; // rastorguev fix
  51. protected $_copiedPlatformFiles = array();
  52. /**
  53. *
  54. * @var Aitoc_Aitsys_Model_License_Service
  55. */
  56. protected $_service = array();
  57. protected $_moduleIgnoreList = array('Aitoc_Aitinstall', 'Aitoc_Aitsys', 'Aitoc_Aitprepare');
  58. protected $_aitocPrefixList = array('Aitoc_','AdjustWare_');
  59. protected $_moduleDirs = array( 'Aitoc' , 'AdjustWare' );
  60. protected $_reloaded = false;
  61. /**
  62. *
  63. * @return Aitoc_Aitsys_Model_Platform
  64. */
  65. public function block()
  66. {
  67. $this->_block = true;
  68. return $this;
  69. }
  70. public function getModuleDirs()
  71. {
  72. return $this->_moduleDirs;
  73. }
  74. public function isAitocNamespace( $namespace , $compare = false )
  75. {
  76. if ($compare)
  77. {
  78. return in_array($namespace,$this->_moduleDirs);
  79. }
  80. foreach ($this->_moduleDirs as $dir)
  81. {
  82. if (false !== strstr($namespace,$dir))
  83. {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. public function isBlocked()
  90. {
  91. return $this->_block;
  92. }
  93. public function getModules()
  94. {
  95. if (!$this->_modules)
  96. {
  97. $this->_generateModuleList();
  98. }
  99. return $this->_modules;
  100. }
  101. public function getModuleKeysForced()
  102. {
  103. $modules = array();
  104. $path = Mage::getBaseDir('code').DS.'local'.DS;
  105. foreach ($this->_moduleDirs as $dir)
  106. {
  107. $aModuleDirs = glob($path.$dir.DS.'*');
  108. if (!$aModuleDirs)
  109. continue;
  110. foreach ($aModuleDirs as $tmpPath)
  111. {
  112. $name = pathinfo($tmpPath,PATHINFO_FILENAME);
  113. $key = $dir.'_'.$name;
  114. if ('Aitoc_Aitsys' == $key)
  115. {
  116. continue;
  117. }
  118. $modules[$key] = 'true' == (string)Mage::getConfig()->getNode('modules/'.$key.'/active');
  119. }
  120. }
  121. return $modules;
  122. }
  123. /**
  124. *
  125. * @param $key
  126. * @return Aitoc_Aitsys_Model_Module
  127. */
  128. public function getModule( $key )
  129. {
  130. if (!$this->_modules)
  131. {
  132. $this->_generateModuleList();
  133. }
  134. return isset($this->_modules[$key]) ? $this->_modules[$key] : null;
  135. }
  136. /**
  137. *
  138. * @return Aitoc_Aitsys_Model_License_Service
  139. */
  140. public function getService( $for = 'default' )
  141. {
  142. if (!isset($this->_service[$for]))
  143. {
  144. $notExcluded = (!$this->hasDebugExclude() || !in_array($for,$this->debug_exclude));
  145. if ($this->isDebug() && $notExcluded)
  146. {
  147. $this->tool()->testMsg('Use debug service');
  148. $this->_service[$for] = new Aitoc_Aitsys_Model_License_Service_Debug();
  149. }
  150. else
  151. {
  152. $this->tool()->testMsg('Use real service');
  153. $this->_service[$for] = new Aitoc_Aitsys_Model_License_Service();
  154. }
  155. $this->_service[$for]->setServiceUrl($this->getServiceUrl());
  156. }
  157. return $this->_service[$for];
  158. }
  159. public function isDebug()
  160. {
  161. return $this->getData('debug');
  162. }
  163. public function isDebugingAllowed()
  164. {
  165. return $this->getData('debuging_allowed') ? true : false;
  166. }
  167. public function getServiceUrl()
  168. {
  169. if ($url = $this->tool()->getApiUrl())
  170. {
  171. return $url;
  172. }
  173. if ($url = $this->getData('_service_url'))
  174. {
  175. return $url;
  176. }
  177. $url = $this->getData('service_url');
  178. return $url ? $url : Mage::getStoreConfig('aitsys/service/url');
  179. }
  180. public function getVersion()
  181. {
  182. if (!$this->_version)
  183. {
  184. $this->_version = (string)Mage::app()->getConfig()->getNode('modules/Aitoc_Aitsys/version');
  185. }
  186. return $this->_version;
  187. }
  188. /**
  189. *
  190. * @param $mode
  191. * @return Aitoc_Aitsys_Model_Platform
  192. */
  193. public function setTestMode( $mode = true )
  194. {
  195. if (!$this->isModePresetted())
  196. {
  197. $this->setData('mode',$mode ? 'test' : 'live');
  198. }
  199. return $this;
  200. }
  201. public function isModePresetted()
  202. {
  203. return $this->hasData('mode');
  204. }
  205. public function isTestMode()
  206. {
  207. return $this->getData('mode') == 'test';
  208. }
  209. public function getInstallDir( $base = false )
  210. {
  211. if (!$this->_installDir)
  212. {
  213. $this->_installDir = dirname(dirname(__FILE__)).'/install/';
  214. /**
  215. if (!$this->tool()->filesystem()->isWriteable($this->_installDir))
  216. {
  217. throw new Aitoc_Aitsys_Model_Aitfilesystem_Exception($this->_installDir." should be writeable.");
  218. }
  219. **/
  220. }
  221. if ($base)
  222. {
  223. return $this->_installDir;
  224. }
  225. return rtrim($this->_installDir,'/').'/';
  226. }
  227. // rastorguev fix
  228. public function getLicenseDir( $base = false )
  229. {
  230. if (!$this->_licenseDir)
  231. {
  232. $this->_licenseDir = dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))).'/var/'.self::INSTALLATION_DIR.'/';
  233. if (!$this->tool()->filesystem()->isWriteable($this->_licenseDir))
  234. {
  235. throw new Aitoc_Aitsys_Model_Aitfilesystem_Exception($this->_licenseDir." should be writeable.");
  236. }
  237. if (!file_exists($this->_licenseDir))
  238. {
  239. $this->tool()->filesystem()->mkDir($this->_licenseDir);
  240. }
  241. }
  242. if ($base)
  243. {
  244. return $this->_licenseDir;
  245. }
  246. return rtrim($this->_licenseDir.$this->getPlatformId(),'/').'/';
  247. }
  248. public function getPlatformId()
  249. {
  250. return $this->getData('platform_id');
  251. }
  252. /**
  253. *
  254. * @param $platformId
  255. * @return Aitoc_Aitsys_Model_Platform
  256. */
  257. public function setPlatformId( $platformId )
  258. {
  259. return $this->setData('platform_id',$platformId);
  260. }
  261. public function init()
  262. {
  263. $this->_fixOldPlatform();
  264. if (!$this->_loadConfigFile()->_loadPlatformData()->getPlatformId())
  265. {
  266. $license = $this->getAnyLicense();
  267. $service = $license ? $license->getService() : $this->getService();
  268. if ($this->tool()->cleanDomain($service->getServiceUrl())
  269. == $this->tool()->cleanDomain(Mage::getBaseUrl()))
  270. {
  271. $this->reset();
  272. return;
  273. }
  274. $this->tool()->testMsg('begin register platform');
  275. try
  276. {
  277. $service->connect();
  278. $data = array(
  279. 'purchaseid' => $license ? $license->getPurchaseId() : '' ,
  280. 'initial_module_list' => $this->getModulePurchaseIdQuickList()
  281. );
  282. $platformId = $service->registerPlatform($data);
  283. $service->disconnect();
  284. $this->tool()->testMsg('Generated platform id: '.$platformId);
  285. $this->setPlatformId($platformId);
  286. $this->setServiceUrl($service->getServiceUrl());
  287. $this->_savePlatformData();
  288. $this->_copyToPlatform($platformId);
  289. $this->unsPlatformId();
  290. $this->_loadPlatformData();
  291. }
  292. catch (Exception $exc)
  293. {
  294. $this->tool()->testMsg($exc);
  295. }
  296. }
  297. $this->reset();
  298. }
  299. protected function _fixOldPlatform()
  300. {
  301. $installDir = $this->getInstallDir(true);
  302. if ($platforms = glob($installDir.'*.platform.xml'))
  303. {
  304. foreach ($platforms as $platformFile)
  305. {
  306. $platformId = $this->_castPlatformId($platformFile);
  307. $platformDir = $this->getLicenseDir().$platformId;
  308. $this->tool()->filesystem()->makeDirStructure($platformDir);
  309. $oldPlatformDir = $this->getInstallDir().$platformId;
  310. if ($pathes = glob($oldPlatformDir.'/*'))
  311. {
  312. foreach ($pathes as $path)
  313. {
  314. $fileinfo = pathinfo($path);
  315. if ('xml' == $fileinfo['extension'])
  316. {
  317. $to = $this->getInstallDir().$fileinfo['basename'];
  318. }
  319. else
  320. {
  321. $to = $this->getLicenseDir().$platformId."/".$fileinfo['basename'];
  322. }
  323. $this->tool()->filesystem()->moveFile($path,$to);
  324. }
  325. }
  326. $this->tool()->filesystem()->moveFile($platformFile,$platformDir.'.platform.xml');
  327. $this->tool()->filesystem()->rmFile($oldPlatformDir);
  328. }
  329. }
  330. }
  331. /**
  332. *
  333. * @return Aitoc_Aitsys_Model_Platform
  334. */
  335. public function save()
  336. {
  337. return $this->_savePlatformData();
  338. }
  339. /**
  340. *
  341. * @return Aitoc_Aitsys_Model_Platform
  342. */
  343. public function reset()
  344. {
  345. $this->_generateModuleList();
  346. foreach ($this->_modules as $module)
  347. {
  348. $this->tool()->testMsg('Update module '.$module->getLabel().' status after generating');
  349. $module->updateStatuses();
  350. }
  351. return $this;
  352. }
  353. /**
  354. *
  355. * @return Aitoc_Aitsys_Model_Platform
  356. */
  357. public function reload()
  358. {
  359. if($this->_reloaded) {
  360. return $this;
  361. }
  362. foreach ($this->_modules as $module)
  363. {
  364. $license = $module->getLicense();
  365. if($license && !$license->isLight()) {
  366. continue;
  367. }
  368. if(!$license || $license->checkStatus()->isInstalled())
  369. {
  370. $module->setAvailable(true);
  371. }
  372. else
  373. {
  374. $module->setAvailable(false);
  375. }
  376. }
  377. $this->_reloaded = true;
  378. return $this;
  379. }
  380. public function isAitocModule( $module )
  381. {
  382. foreach ($this->_aitocPrefixList as $prefix)
  383. {
  384. if (0 === strpos($module,$prefix))
  385. {
  386. return true;
  387. }
  388. }
  389. }
  390. public function isIgnoredModule( $module )
  391. {
  392. foreach ($this->_moduleIgnoreList as $ignoredModule)
  393. {
  394. if (false !== strstr($module,$ignoredModule))
  395. {
  396. return true;
  397. }
  398. }
  399. }
  400. public function isPlatformFileName( $filename )
  401. {
  402. return preg_match('/'.preg_quote(self::PLATFORMFILE_SUFFIX).'$/',$filename);
  403. }
  404. /**
  405. *
  406. * @return Aitoc_Aitsys_Model_Module_License | null
  407. */
  408. public function getAnyLicense()
  409. {
  410. $path = $this->getInstallDir().'*.xml';
  411. if ($pathes = glob($path))
  412. {
  413. foreach ($pathes as $path)
  414. {
  415. if (!$this->isPlatformFileName($path))
  416. {
  417. $module = $this->_makeModuleByInstallFile($path);
  418. return $module->getLicense();
  419. }
  420. }
  421. }
  422. }
  423. public function getModulePurchaseIdQuickList()
  424. {
  425. $list = array();
  426. $path = $this->getInstallDir().'*.xml';
  427. if ($pathes = glob($path))
  428. {
  429. foreach ($pathes as $path)
  430. {
  431. if (!$this->isPlatformFileName($path))
  432. {
  433. $module = $this->_makeModuleByInstallFile($path);
  434. $list[$module->getKey()] = $module->getLicense()->getPurchaseId();
  435. }
  436. }
  437. }
  438. return $list;
  439. }
  440. /**
  441. *
  442. * @return Aitoc_Aitsys_Model_Platform
  443. */
  444. protected function _loadConfigFile()
  445. {
  446. $path = dirname($this->getInstallDir(true)).'/config.php';
  447. $this->tool()->testMsg('check config path: '.$path);
  448. if (file_exists($path))
  449. {
  450. include $path;
  451. if (isset($config) && is_array($config))
  452. {
  453. $this->tool()->testMsg('loaded config:');
  454. $this->tool()->testMsg($config);
  455. $this->setData($config);
  456. }
  457. }
  458. return $this;
  459. }
  460. /**
  461. *
  462. * @return Aitoc_Aitsys_Model_Platform
  463. */
  464. protected function _generateModuleList()
  465. {
  466. $this->tool()->testMsg('Try to generate module list!');
  467. $this->_loadLicensedModules()->_loadAllModules();
  468. $this->tool()->event('aitsys_generate_module_list_after');
  469. $this->tool()->testMsg('Module list generated');
  470. return $this;
  471. }
  472. /**
  473. *
  474. * @return Aitoc_Aitsys_Model_Platform
  475. */
  476. protected function _loadLicensedModules()
  477. {
  478. if (!file_exists($this->getInstallDir()))
  479. {
  480. return $this;
  481. }
  482. $dir = new DirectoryIterator($this->getInstallDir());
  483. foreach ($dir as $item)
  484. {
  485. /* @var $item DirectoryIterator */
  486. if ($item->isFile())
  487. {
  488. $filename = $item->getFilename();
  489. if (false !== strstr($filename,'.xml'))
  490. {
  491. if ($this->isPlatformFileName($filename))
  492. {
  493. continue;
  494. }
  495. if ($this->isUpgradeFilename($filename))
  496. {
  497. continue;
  498. }
  499. $this->tool()->testMsg("Try load licensed module");
  500. $module = $this->_makeModuleByInstallFile($item->getPathname());
  501. if ((!$this->_addEntHash() && $module->getLicense()->getEntHash()) || ($this->_addEntHash() && !$module->getLicense()->getEntHash()))
  502. {
  503. $this->_moduleIgnoreList[] = $module->getKey();
  504. continue;
  505. }
  506. $key = $module->getKey();
  507. $this->tool()->testMsg("Try load licensed module finished: ".$key);
  508. if (!isset($this->_modules[$key]))
  509. {
  510. $this->tool()->testMsg("Add new module");
  511. $this->_modules[$key] = $module;
  512. }
  513. else
  514. {
  515. $this->tool()->testMsg("Reset existed module");
  516. $this->_modules[$key]->reset();
  517. }
  518. }
  519. }
  520. }
  521. return $this;
  522. }
  523. protected function _addEntHash()
  524. {
  525. $val = Mage::getConfig()->getNode('modules/Enterprise_Enterprise/active');
  526. return ((string)$val == 'true');
  527. }
  528. public function isUpgradeFilename( $filename )
  529. {
  530. return false !== strstr($filename,'.upgrade-license.xml');
  531. }
  532. /**
  533. *
  534. * @param $path
  535. * @return Aitoc_Aitsys_Model_Module
  536. */
  537. protected function _makeModuleByInstallFile( $path )
  538. {
  539. $module = new Aitoc_Aitsys_Model_Module();
  540. $module->loadByInstallFile(str_replace('.php','.xml',$path));
  541. $this->tool()->testMsg(get_class($module->getLicense()));
  542. $this->tool()->event('aitsys_create_module_after',array('module' => $module));
  543. return $module;
  544. }
  545. /**
  546. *
  547. * @return Aitoc_Aitsys_Model_Platform
  548. */
  549. protected function _loadAllModules()
  550. {
  551. $filesystem = $this->tool()->filesystem();
  552. $dir = new DirectoryIterator($filesystem->getEtcDir());
  553. foreach ($dir as $item)
  554. {
  555. /* @var $item DirectoryIterator */
  556. if ($this->isIgnoredModule($item->getFilename()))
  557. {
  558. continue;
  559. }
  560. if ($item->isFile() && $this->isAitocModule($item->getFilename()))
  561. {
  562. $this->_makeModuleByModuleFile($item->getPathname());
  563. }
  564. }
  565. return $this;
  566. }
  567. /**
  568. *
  569. * @param $path
  570. * @return Aitoc_Aitsys_Model_Module
  571. */
  572. protected function _makeModuleByModuleFile( $path )
  573. {
  574. $moduleFile = new SplFileInfo($path);
  575. $file = $moduleFile->getFilename();
  576. list($key) = explode('.',$file);
  577. $this->tool()->testMsg('Check: '.$key.' -- '.$file);
  578. if ($module = (isset($this->_modules[$key]) ? $this->_modules[$key] : null))
  579. {
  580. return $module;
  581. }
  582. $this->tool()->testMsg('Create: '.$key);
  583. $module = new Aitoc_Aitsys_Model_Module();
  584. $module->loadByModuleFile($path,$key);
  585. return $this->_modules[$key] = $module;
  586. }
  587. protected function _castPlatformId( $file )
  588. {
  589. if ($file instanceof SplFileInfo)
  590. {
  591. $file = $file->getFilename();
  592. }
  593. $fileinfo = pathinfo($file);
  594. list($platformId) = explode('.',$fileinfo['basename'],2);
  595. return $platformId;
  596. }
  597. /**
  598. *
  599. * @return Aitoc_Aitsys_Model_Platform
  600. */
  601. protected function _loadPlatformData()
  602. {
  603. $this->_copiedPlatformFiles = array();
  604. // $dir = new DirectoryIterator($this->getInstallDir());
  605. $dir = new DirectoryIterator($this->getLicenseDir()); // rastorguev fix
  606. foreach ($this->getPlatforms() as $item)
  607. {
  608. /* @var $item DirectoryIterator */
  609. $platformId = $this->_castPlatformId($item);
  610. // start rastorguev fix
  611. if (!file_exists($item->getPathname()))
  612. {
  613. $this->tool()->testMsg("Platform id broken or superfluous: ".$platformId);
  614. continue;
  615. }
  616. // finish rastorguev fix
  617. if ($this->getPlatformId() || !$this->_checkPlatformId($platformId,$item->getPathname()))
  618. {
  619. $this->tool()->testMsg("Platform id broken or superfluous: ".$platformId);
  620. $this->_removePlatform($platformId);
  621. continue;
  622. }
  623. $dom = new DOMDocument('1.0');
  624. $dom->load($item->getPathname());
  625. $platform = $dom->getElementsByTagName('platform');
  626. if ($platform->length)
  627. {
  628. $platform = $platform->item(0);
  629. foreach ($platform->childNodes as $item)
  630. {
  631. if ('location' == $item->nodeName)
  632. {
  633. continue;
  634. }
  635. #$this->tool()->debug($item->nodeName.': '.$item->nodeValue);
  636. if (!$this->hasData($item->nodeName))
  637. {
  638. $this->setData($item->nodeName,$item->nodeValue);
  639. }
  640. }
  641. }
  642. $this->setPlatformId($platformId);
  643. $this->tool()->testMsg("Platform id:".$platformId);
  644. }
  645. if ($platformId = $this->getPlatformId())
  646. {
  647. $this->_copyToPlatform($platformId);
  648. }
  649. return $this;
  650. }
  651. /**
  652. *
  653. * @return SplFileInfo[]
  654. */
  655. public function getPlatforms()
  656. {
  657. $result = array();
  658. // $dir = new DirectoryIterator($this->getInstallDir(true));
  659. $dir = new DirectoryIterator($this->getLicenseDir(true)); // rastorguev fix
  660. foreach ($dir as $item)
  661. {
  662. /* @var $item DirectoryIterator */
  663. if ($item->isFile() && $this->isPlatformFileName($item->getFilename()))
  664. {
  665. $result[] = $item->getFileInfo();
  666. }
  667. }
  668. return $result;
  669. }
  670. public function getPlatformPathes()
  671. {
  672. $pathes = array();
  673. foreach ($this->getPlatforms() as $item)
  674. {
  675. $platformId = $this->_castPlatformId($item);
  676. $pathes[] = dirname($item->getPathname()).'/'.$platformId.'/';
  677. }
  678. return $pathes;
  679. }
  680. protected function _checkPlatformId( $platformId , $path )
  681. {
  682. $dom = new DOMDocument('1.0');
  683. $dom->load($path);
  684. if ($location = $dom->getElementsByTagName('location')->item(0))
  685. {
  686. /* @var $location DOMElement */
  687. if ($location->getAttribute('domain') == $this->tool()->getRealBaseUrl()
  688. && $location->getAttribute('path') == $this->_getLocationPath())
  689. {
  690. return true;
  691. }
  692. }
  693. return false;
  694. }
  695. /**
  696. *
  697. * @return Aitoc_Aitsys_Model_Platform
  698. */
  699. protected function _savePlatformData()
  700. {
  701. if ($platformId = $this->getPlatformId())
  702. {
  703. // $defaultInstallDir = $this->getInstallDir(true);
  704. $defaultInstallDir = $this->getLicenseDir(true); // rastorguev fix
  705. $path = $defaultInstallDir.$platformId.self::PLATFORMFILE_SUFFIX;
  706. $this->tool()->testMsg("Save platform path: ".$path);
  707. $dom = $this->getPlatformDom();
  708. $dom->save($path);
  709. if (!file_exists($path))
  710. {
  711. $msg = 'Write permissions required for: '.$defaultInstallDir.' and all files included.';
  712. throw new Aitoc_Aitsys_Model_Aitfilesystem_Exception($msg);
  713. }
  714. }
  715. return $this;
  716. }
  717. /**
  718. *
  719. * Genarate platform DOM structure
  720. * @param $configData custom configuration data
  721. * @return DOMDocument
  722. */
  723. public function getPlatformDom($configData = array())
  724. {
  725. $data = array(
  726. 'domain' => $this->tool()->getRealBaseUrl(),
  727. 'path' => $this->_getLocationPath(),
  728. );
  729. if ($configData) {
  730. $data = array_merge($data, $configData);
  731. }
  732. $dom = new DOMDocument('1.0');
  733. $platform = $dom->createElement('platform');
  734. $dom->appendChild($platform);
  735. $this->tool()->testMsg(array('try to save',$this->getData()));
  736. foreach ($this->getData() as $key => $value)
  737. {
  738. if (is_array($value))
  739. {
  740. continue;
  741. }
  742. $platform->appendChild($dom->createElement($key,$value));
  743. }
  744. $location = $dom->createElement('location');
  745. /* @var $location DOMElement */
  746. $location->setAttribute('domain',$data['domain']);
  747. $location->setAttribute('path',$data['path']);
  748. $platform->appendChild($location);
  749. return $dom;
  750. }
  751. private function _getLocationPath()
  752. {
  753. return $this->getInstallDir(true);
  754. //return $this->getLicenseDir(true); // rastorguev fix
  755. }
  756. /**
  757. *
  758. * @param $platformId
  759. * @return Aitoc_Aitsys_Model_Platform
  760. */
  761. protected function _copyToPlatform($platformId)
  762. {
  763. // $path = $this->getInstallDir(true);
  764. $path = $this->getLicenseDir(true); // rastorguev fix
  765. $platformPath = $path.$platformId.DS;
  766. if (!file_exists($platformPath))
  767. {
  768. $this->tool()->filesystem()->makeDirStructure($platformPath);
  769. }
  770. $dir = new DirectoryIterator($path);
  771. foreach ($dir as $item)
  772. {
  773. /* @var $item DirectoryIterator */
  774. $filename = $item->getFilename();
  775. if ($item->isFile() && !$this->isPlatformFileName($filename)
  776. && $item->getFilename() != $this->tool()->getUrlFileName())
  777. {
  778. if (!$this->tool()->filesystem()->isWriteable($item->getPathname()))
  779. {
  780. throw new Aitoc_Aitsys_Model_Aitfilesystem_Exception("File does not have write permissions: ".$item->getPathname());
  781. }
  782. $to = $platformPath.$filename;
  783. if (file_exists($to) && $this->_isCopiedPlatformFile($item->getFilename()))
  784. {
  785. $this->tool()->filesystem()->rmFile($item->getPathname());
  786. }
  787. else
  788. {
  789. $this->tool()->filesystem()->moveFile($item->getPathname(),$to);
  790. }
  791. }
  792. }
  793. return $this;
  794. }
  795. private function _isCopiedPlatformFile( $file )
  796. {
  797. return in_array($file,$this->_copiedPlatformFiles);
  798. }
  799. /**
  800. *
  801. * @param $platformId
  802. * @return Aitoc_Aitsys_Model_Platform
  803. */
  804. protected function _removePlatform( $platformId )
  805. {
  806. $this->_modules = array();
  807. return $this->_copyFromPlatform($platformId);
  808. }
  809. /**
  810. *
  811. * @param $platformId
  812. * @return Aitoc_Aitsys_Model_Platform
  813. */
  814. protected function _copyFromPlatform( $platformId )
  815. {
  816. // $path = $this->getInstallDir(true);
  817. $path = $this->getLicenseDir(true); //rastorguev fix
  818. $dir = new DirectoryIterator($path.$platformId);
  819. foreach ($dir as $item)
  820. {
  821. /* @var $item DirectoryIterator */
  822. $filename = $item->getFilename();
  823. if ($item->isFile() && '.php' !== substr($filename, -4))
  824. {
  825. $this->tool()->filesystem()->cpFile($item->getPathname(),$path.$filename);
  826. $this->_copiedPlatformFiles[] = $filename;
  827. }
  828. }
  829. return $this;
  830. }
  831. }