PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/magiczoom/magictoolbox.installer.magento.class.php

http://yurishop.googlecode.com/
PHP | 265 lines | 209 code | 41 blank | 15 comment | 45 complexity | c105c107a1d20c923896c8fd6e60d3ec MD5 | raw file
  1. <?php
  2. /**
  3. Magento module installer class
  4. */
  5. require_once(dirname(__FILE__) . '/magictoolbox.installer.core.class.php');
  6. class MagicToolboxMagentoModuleInstallerClass extends MagicToolboxCoreInstallerClass {
  7. private $design = 'base';
  8. private $isCompilerDisabled = true;
  9. private $mageVersion;
  10. private $moduleDisabled = false;
  11. private $foundCopies = array();
  12. function MagicToolboxMagentoModuleInstallerClass() {
  13. $this->dir = dirname(dirname(__FILE__));
  14. $this->modDir = dirname(__FILE__) . '/module';
  15. //for fix url's in css files
  16. $this->runMage();
  17. }
  18. function runMage() {
  19. if(file_exists($this->dir . '/app/etc/modules/MagicToolbox_MagicZoom.xml')) {
  20. $this->moduleDisabled = @rename($this->dir . '/app/etc/modules/MagicToolbox_MagicZoom.xml', $this->dir . '/app/etc/modules/MagicToolbox_MagicZoom.xml.backup');
  21. }
  22. // go to magento core folder
  23. chdir($this->dir);
  24. ob_start();
  25. // include core magento file (load front page)
  26. include('index.php');
  27. ob_end_clean();
  28. //check Magento version
  29. $mageVersion = Mage::getVersion();
  30. $pattern = "/([0-9]+\.[0-9]+\.[0-9]+)(?:\.(?:[0-9]+))*/";
  31. $matches = array();
  32. if(preg_match($pattern, $mageVersion, $matches)) {
  33. $this->mageVersion = $matches[1];
  34. if(version_compare($this->mageVersion, '1.4.0', '<')) {
  35. $this->design = 'default';
  36. }
  37. }
  38. $this->resDir = "/" . preg_replace('/https?:\/\/[^\/]+\//is','',str_replace('/magiczoom.css', '', Mage::getSingleton('core/design_package')->getSkinUrl('css/magiczoom.css')));
  39. //TODO: find better way to get web path
  40. $this->resDir = preg_replace('/(skin\/frontend\/)([^\/]+\/[^\/]+)/is', '$1base/default', $this->resDir);
  41. if($this->design == 'default') {
  42. $this->resDir = str_replace('/base/', '/default/', $this->resDir);
  43. }
  44. //this hack need if Web Base URL contains {{base_url}}
  45. $this->resDir = str_replace('/magiczoom/', '/', $this->resDir);
  46. if(defined('COMPILER_INCLUDE_PATH')) {
  47. $this->setError('Please, disable Magento Compiler before continue.');
  48. $this->isCompilerDisabled = false;
  49. }
  50. if($this->moduleDisabled) {
  51. $resource = Mage::getSingleton('core/resource');
  52. $connection = $resource->getConnection('core_write');
  53. $table = $resource->getTableName('core/resource');
  54. $result = $connection->query("SELECT * FROM {$table} WHERE code = 'magiczoom_setup'");
  55. if($result) {
  56. $rows = $result->fetch(PDO::FETCH_ASSOC);
  57. if($rows) {
  58. $connection->query("DELETE FROM {$table} WHERE code = 'magiczoom_setup'");
  59. }
  60. }
  61. //delete old options
  62. $result = $connection->query("DROP TABLE IF EXISTS magiczoom");
  63. } else {
  64. @rename($this->dir . '/app/etc/modules/MagicToolbox_MagicZoom.xml.backup', $this->dir . '/app/etc/modules/MagicToolbox_MagicZoom.xml');
  65. }
  66. $availableDesigns = Mage::getSingleton('core/design_source_design')->getAllOptions();
  67. foreach($availableDesigns as $pKey => $package) {
  68. if(is_array($package['value'])) {
  69. foreach($package['value'] as $tKey => $theme) {
  70. if($package['label'] == $this->design && $theme['label'] == 'default') continue;
  71. if(file_exists($this->dir . '/app/design/frontend/'.$package['label'].'/'.$theme['label'].'/template/magiczoom')) {
  72. $this->foundCopies[] = '/app/design/frontend/'.$package['label'].'/'.$theme['label'].'/template/magiczoom';
  73. }
  74. if(file_exists($this->dir . '/app/design/frontend/'.$package['label'].'/'.$theme['label'].'/layout/magiczoom.xml')) {
  75. $this->foundCopies[] = '/app/design/frontend/'.$package['label'].'/'.$theme['label'].'/layout/magiczoom.xml';
  76. }
  77. }
  78. }
  79. }
  80. // return to installer folder
  81. chdir(dirname(__FILE__));
  82. return true;
  83. }
  84. function checkPlace() {
  85. $this->setStatus('check', 'place');
  86. if(!is_dir($this->dir . '/app') && !file_exists($this->dir . '/index.php')) {
  87. $this->setError('Wrong location: please upload the files from the ZIP archive to the Magento store directory.');
  88. return false;
  89. }
  90. return $this->isCompilerDisabled;
  91. }
  92. function checkPerm() {
  93. $this->setStatus('check', 'perm');
  94. $files = array(
  95. // directory
  96. '/app/code/local',
  97. '/app/design/adminhtml/default/default/layout',
  98. '/app/design/adminhtml/default/default/template',
  99. '/app/etc/modules',
  100. '/js',
  101. '/app/design/frontend/'.$this->design.'/default/layout',
  102. '/app/design/frontend/'.$this->design.'/default/template',
  103. '/skin/adminhtml/default/default',
  104. '/skin/frontend/'.$this->design.'/default/css',
  105. '/skin/frontend/'.$this->design.'/default/images',
  106. '/skin/frontend/'.$this->design.'/default/js',
  107. );
  108. $excludeDesign = ($this->design == 'base')?'default':'base';
  109. foreach($this->getFilesRecursive($this->modDir) as $file) {
  110. if(strpos($file, '/app/design/frontend/'.$excludeDesign) === 0) continue;
  111. if(strpos($file, '/skin/frontend/'.$excludeDesign) === 0) continue;
  112. if(file_exists($this->dir . $file)) {
  113. $files[] = $file;
  114. }
  115. }
  116. if(file_exists($this->dir . '/app/etc/modules/MagicToolbox_MagicZoom.xml')) {
  117. $files[] = '/app/etc/modules/MagicToolbox_MagicZoom.xml';
  118. }
  119. list($result, $wrang) = $this->checkFilesPerm($files);
  120. if(!$result) {
  121. $this->setError('This installer need to modify some Magento store files.');
  122. $this->setError('Please check write access for following files and/or dirrectories of your Magento store:');
  123. $this->setError(array_unique($wrang), '&nbsp;&nbsp;&nbsp;-&nbsp;');
  124. return false;
  125. }
  126. return true;
  127. }
  128. function getFilesRecursive($path, $firstCall = true) {
  129. $result = array();
  130. $files = glob($path . '/*');
  131. if($files !== false) {
  132. foreach($files as $file) {
  133. if(is_dir($file)) {
  134. $result = array_merge($result, $this->getFilesRecursive($file, false));
  135. } else {
  136. $result[] = $file;
  137. }
  138. }
  139. }
  140. if($firstCall) {
  141. $result = str_replace($path, '', $result);
  142. }
  143. return $result;
  144. }
  145. function installFiles() {
  146. $this->setStatus('install', 'files');
  147. //copy app, js, skin folders
  148. $this->copyDir($this->modDir . '/app/code', $this->dir . '/app/code');
  149. $this->copyDir($this->modDir . '/app/design/adminhtml', $this->dir . '/app/design/adminhtml');
  150. $this->copyDir($this->modDir . '/app/design/frontend/'.$this->design, $this->dir . '/app/design/frontend/'.$this->design);
  151. $this->copyDir($this->modDir . '/js', $this->dir . '/js');
  152. $this->copyDir($this->modDir . '/skin/adminhtml', $this->dir . '/skin/adminhtml');
  153. $this->copyDir($this->modDir . '/skin/frontend/'.$this->design, $this->dir . '/skin/frontend/'.$this->design);
  154. //modify config.xml
  155. if(isset($this->mageVersion) && version_compare($this->mageVersion, '1.4.1', '<')) {
  156. $fileContents = file_get_contents($this->dir . '/app/code/local/MagicToolbox/MagicZoom/etc/config.xml');
  157. $rCount = 0;
  158. $fileContents = preg_replace('/<!--(<page>.*?MagicToolbox_MagicZoom_Block_Html_Head.*?<\/page>)-->/is', '$1', $fileContents, 1, $rCount);
  159. if($rCount) {
  160. file_put_contents($this->dir . '/app/code/local/MagicToolbox/MagicZoom/etc/config.xml', $fileContents);
  161. }
  162. }
  163. //this must be last
  164. $this->copyDir($this->modDir . '/app/etc', $this->dir . '/app/etc');
  165. if(count($this->foundCopies)) {
  166. $this->setError('The following layout and/or template files was detected in your Magento directory:');
  167. $this->setError($this->foundCopies, '&nbsp;&nbsp;&nbsp;-&nbsp;');
  168. $this->setError('Make sure to update these files from \''.$this->design.'/default\' design if needed!');
  169. }
  170. if($this->moduleDisabled) {
  171. @unlink($this->dir . '/app/etc/modules/MagicToolbox_MagicZoom.xml.backup');
  172. }
  173. return true;
  174. }
  175. function restoreStep_installFiles() {
  176. $this->removeDir($this->dir . '/app/code/local/MagicToolbox/MagicZoom');
  177. if($this->is_empty_dir($this->dir . '/app/code/local/MagicToolbox')) {
  178. $this->removeDir($this->dir . '/app/code/local/MagicToolbox');
  179. $removeAll = true;
  180. } else $removeAll = false;
  181. unlink($this->dir . '/app/etc/modules/MagicToolbox_MagicZoom.xml');
  182. unlink($this->dir . '/app/design/adminhtml/default/default/layout/magiczoom.xml');
  183. $this->removeDir($this->dir . '/app/design/adminhtml/default/default/template/magiczoom');
  184. unlink($this->dir . '/app/design/frontend/'.$this->design.'/default/layout/magiczoom.xml');
  185. $this->removeDir($this->dir . '/app/design/frontend/'.$this->design.'/default/template/magiczoom');
  186. unlink($this->dir . '/skin/frontend/'.$this->design.'/default/css/magiczoom.css');
  187. unlink($this->dir . '/skin/frontend/'.$this->design.'/default/js/magiczoom.js');
  188. $this->removeDir($this->dir . '/js/magiczoom');
  189. $this->removeDir($this->dir . '/skin/adminhtml/default/default/magiczoom');
  190. if($removeAll) {
  191. unlink($this->dir . '/skin/frontend/'.$this->design.'/default/js/magictoolbox_utils.js');
  192. unlink($this->dir . '/skin/frontend/'.$this->design.'/default/js/magicscroll.js');
  193. unlink($this->dir . '/skin/frontend/'.$this->design.'/default/css/magicscroll.css');
  194. if(file_exists($this->dir . '/skin/frontend/'.$this->design.'/default/images/loader.gif'))
  195. unlink($this->dir . '/skin/frontend/'.$this->design.'/default/images/loader.gif');
  196. }
  197. return true;
  198. }
  199. function is_empty_dir($dir) {
  200. if($dh = @opendir($dir)) {
  201. while($file = readdir($dh)) {
  202. if($file != '.' && $file != '..') {
  203. closedir($dh);
  204. return false;
  205. }
  206. }
  207. closedir($dh);
  208. return true;
  209. }
  210. else return false; //no such dir, not a dir, not readable
  211. }
  212. function upgrade($files) {
  213. $path = $this->dir . '/skin/frontend/'.$this->design.'/default/js/';
  214. foreach($files as $name => $file) {
  215. if(file_exists($path . $name)) {
  216. unlink($path . $name);
  217. }
  218. file_put_contents($path . $name, $file);
  219. chmod($path . $name, 0755);
  220. }
  221. return true;
  222. }
  223. }
  224. ?>