PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/osCommerce/OM/Core/Site/Admin/Application/CoreUpdate/Model/applyPackage.php

http://github.com/osCommerce/oscommerce
PHP | 194 lines | 149 code | 35 blank | 10 comment | 42 complexity | 84338a26058ee3b855ba11c8faa137ab MD5 | raw file
  1. <?php
  2. /**
  3. * osCommerce Online Merchant
  4. *
  5. * @copyright Copyright (c) 2011 osCommerce; http://www.oscommerce.com
  6. * @license BSD License; http://www.oscommerce.com/bsdlicense.txt
  7. */
  8. namespace osCommerce\OM\Core\Site\Admin\Application\CoreUpdate\Model;
  9. use \Phar;
  10. use \RecursiveIteratorIterator;
  11. use osCommerce\OM\Core\DateTime;
  12. use osCommerce\OM\Core\DirectoryListing;
  13. use osCommerce\OM\Core\OSCOM;
  14. class applyPackage {
  15. protected static $_to_version;
  16. public static function execute() {
  17. $phar_can_open = true;
  18. $meta = array();
  19. $pro_hart = array();
  20. try {
  21. $phar = new Phar(OSCOM::BASE_DIRECTORY . 'Work/CoreUpdate/update.phar');
  22. $meta = $phar->getMetadata();
  23. self::$_to_version = $meta['version_to'];
  24. // reset the log
  25. if ( file_exists(OSCOM::BASE_DIRECTORY . 'Work/Logs/update-' . self::$_to_version . '.txt') && is_writable(OSCOM::BASE_DIRECTORY . 'Work/Logs/update-' . self::$_to_version . '.txt') ) {
  26. unlink(OSCOM::BASE_DIRECTORY . 'Work/Logs/update-' . self::$_to_version . '.txt');
  27. }
  28. self::log('##### UPDATE TO ' . self::$_to_version . ' STARTED');
  29. // first delete files before extracting new files
  30. if ( isset($meta['delete']) ) {
  31. foreach ( $meta['delete'] as $file ) {
  32. $directory = (substr($file, 0, 14) == 'osCommerce/OM/' ? realpath(OSCOM::BASE_DIRECTORY . '../../') : realpath(OSCOM::getConfig('dir_fs_public', 'OSCOM') . '../')) . '/';
  33. if ( file_exists($directory . $file) ) {
  34. if ( is_dir($directory . $file) ) {
  35. if ( rename($directory . $file, $directory . dirname($file) . '/.CU_' . basename($file)) ) {
  36. $pro_hart[] = array('type' => 'directory',
  37. 'where' => $directory,
  38. 'path' => dirname($file) . '/.CU_' . basename($file),
  39. 'log' => true);
  40. }
  41. } else {
  42. if ( rename($directory . $file, $directory . dirname($file) . '/.CU_' . basename($file)) ) {
  43. $pro_hart[] = array('type' => 'file',
  44. 'where' => $directory,
  45. 'path' => dirname($file) . '/.CU_' . basename($file),
  46. 'log' => true);
  47. }
  48. }
  49. }
  50. }
  51. }
  52. // loop through each file individually as extractTo() does not work with
  53. // directories (see http://bugs.php.net/bug.php?id=54289)
  54. foreach ( new RecursiveIteratorIterator($phar) as $iteration ) {
  55. if ( ($pos = strpos($iteration->getPathName(), 'update.phar')) !== false ) {
  56. $file = substr($iteration->getPathName(), $pos+12);
  57. $directory = (substr($file, 0, 14) == 'osCommerce/OM/' ? realpath(OSCOM::BASE_DIRECTORY . '../../') : realpath(OSCOM::getConfig('dir_fs_public', 'OSCOM') . '../')) . '/';
  58. if ( file_exists($directory . $file) ) {
  59. if ( rename($directory . $file, $directory . dirname($file) . '/.CU_' . basename($file)) ) {
  60. $pro_hart[] = array('type' => 'file',
  61. 'where' => $directory,
  62. 'path' => dirname($file) . '/.CU_' . basename($file),
  63. 'log' => false);
  64. }
  65. }
  66. if ( $phar->extractTo($directory, $file, true) ) {
  67. self::log('Extracted: ' . $file);
  68. } else {
  69. self::log('*** Could Not Extract: ' . $file);
  70. }
  71. }
  72. }
  73. self::log('##### CLEANUP');
  74. foreach ( array_reverse($pro_hart, true) as $mess ) {
  75. if ( $mess['type'] == 'directory' ) {
  76. if ( self::rmdir_r($mess['where'] . $mess['path']) ) {
  77. if ( $mess['log'] === true ) {
  78. self::log('Deleted: ' . str_replace('/.CU_', '/', $mess['path']));
  79. }
  80. } else {
  81. if ( $mess['log'] === true ) {
  82. self::log('*** Could Not Delete: ' . str_replace('/.CU_', '/', $mess['path']));
  83. }
  84. }
  85. } else {
  86. if ( unlink($mess['where'] . $mess['path']) ) {
  87. if ( $mess['log'] === true ) {
  88. self::log('Deleted: ' . str_replace('/.CU_', '/', $mess['path']));
  89. }
  90. } else {
  91. if ( $mess['log'] === true ) {
  92. self::log('*** Could Not Delete: ' . str_replace('/.CU_', '/', $mess['path']));
  93. }
  94. }
  95. }
  96. }
  97. } catch ( \Exception $e ) {
  98. $phar_can_open = false;
  99. self::log('##### ERROR: ' . $e->getMessage());
  100. self::log('##### REVERTING STARTED');
  101. foreach ( array_reverse($pro_hart, true) as $mess ) {
  102. if ( $mess['type'] == 'directory' ) {
  103. if ( file_exists($mess['where'] . str_replace('/.CU_', '/', $mess['path'])) ) {
  104. self::rmdir_r($mess['where'] . str_replace('/.CU_', '/', $mess['path']));
  105. }
  106. } else {
  107. if ( file_exists($mess['where'] . str_replace('/.CU_', '/', $mess['path'])) ) {
  108. unlink($mess['where'] . str_replace('/.CU_', '/', $mess['path']));
  109. }
  110. }
  111. if ( file_exists($mess['where'] . $mess['path']) ) {
  112. rename($mess['where'] . $mess['path'], $mess['where'] . str_replace('/.CU_', '/', $mess['path']));
  113. }
  114. self::log('Reverted: ' . str_replace('/.CU_', '/', $mess['path']));
  115. }
  116. self::log('##### REVERTING COMPLETE');
  117. self::log('##### UPDATE TO ' . self::$_to_version . ' FAILED');
  118. trigger_error($e->getMessage());
  119. trigger_error('Please review the update log at: ' . OSCOM::BASE_DIRECTORY . 'Work/Logs/update-' . self::$_to_version . '.txt');
  120. }
  121. if ( $phar_can_open === true ) {
  122. if ( isset($meta['run']) && method_exists('osCommerce\\OM\\Work\\CoreUpdate\\' . $meta['run'] . '\\Controller', 'runAfter') ) {
  123. $results = call_user_func(array('osCommerce\\OM\\Work\\CoreUpdate\\' . $meta['run'] . '\\Controller', 'runAfter'));
  124. if ( !empty($results) ) {
  125. self::log('##### RAN AFTER');
  126. foreach ( $results as $r ) {
  127. self::log($r);
  128. }
  129. }
  130. self::log('##### CLEANUP');
  131. if ( self::rmdir_r(OSCOM::BASE_DIRECTORY . 'Work/CoreUpdate/' . $meta['run']) ) {
  132. self::log('Deleted: osCommerce/OM/Work/CoreUpdate/' . $meta['run']);
  133. } else {
  134. self::log('*** Could Not Delete: osCommerce/OM/Work/CoreUpdate/' . $meta['run']);
  135. }
  136. }
  137. self::log('##### UPDATE TO ' . self::$_to_version . ' COMPLETE');
  138. }
  139. return $phar_can_open;
  140. }
  141. protected static function log($message) {
  142. if ( is_writable(OSCOM::BASE_DIRECTORY . 'Work/Logs') ) {
  143. file_put_contents(OSCOM::BASE_DIRECTORY . 'Work/Logs/update-' . self::$_to_version . '.txt', '[' . DateTime::getNow('d-M-Y H:i:s') . '] ' . $message . "\n", FILE_APPEND);
  144. }
  145. }
  146. protected static function rmdir_r($dir) {
  147. foreach ( scandir($dir) as $file ) {
  148. if ( !in_array($file, array('.', '..')) ) {
  149. if ( is_dir($dir . '/' . $file) ) {
  150. self::rmdir_r($dir . '/' . $file);
  151. } else {
  152. unlink($dir . '/' . $file);
  153. }
  154. }
  155. }
  156. return rmdir($dir);
  157. }
  158. }
  159. ?>