/app/code/core/Mage/Core/Model/File/Storage.php

https://bitbucket.org/kdms/sh-magento · PHP · 232 lines · 120 code · 29 blank · 83 comment · 20 complexity · 9416e213ef7664b827b0452a627ef576 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * File storage model class
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Core_Model_File_Storage extends Mage_Core_Model_Abstract
  34. {
  35. /**
  36. * Storage systems ids
  37. */
  38. const STORAGE_MEDIA_FILE_SYSTEM = 0;
  39. const STORAGE_MEDIA_DATABASE = 1;
  40. /**
  41. * Config pathes for storing storage configuration
  42. */
  43. const XML_PATH_STORAGE_MEDIA = 'default/system/media_storage_configuration/media_storage';
  44. const XML_PATH_STORAGE_MEDIA_DATABASE = 'default/system/media_storage_configuration/media_database';
  45. const XML_PATH_MEDIA_RESOURCE_WHITELIST = 'default/system/media_storage_configuration/allowed_resources';
  46. const XML_PATH_MEDIA_UPDATE_TIME = 'system/media_storage_configuration/configuration_update_time';
  47. /**
  48. * Prefix of model events names
  49. *
  50. * @var string
  51. */
  52. protected $_eventPrefix = 'core_file_storage';
  53. /**
  54. * Show if there were errors while synchronize process
  55. *
  56. * @param Mage_Core_Model_Abstract $sourceModel
  57. * @param Mage_Core_Model_Abstract $destinationModel
  58. * @return bool
  59. */
  60. protected function _synchronizeHasErrors(Mage_Core_Model_Abstract $sourceModel,
  61. Mage_Core_Model_Abstract $destinationModel
  62. ) {
  63. if (!$sourceModel || !$destinationModel) {
  64. return true;
  65. }
  66. return $sourceModel->hasErrors() || $destinationModel->hasErrors();
  67. }
  68. /**
  69. * Return synchronize process status flag
  70. *
  71. * @return Mage_Core_Model_File_Storage_Flag
  72. */
  73. public function getSyncFlag()
  74. {
  75. return Mage::getSingleton('core/file_storage_flag')->loadSelf();
  76. }
  77. /**
  78. * Retrieve storage model
  79. * If storage not defined - retrieve current storage
  80. *
  81. * params = array(
  82. * connection => string, - define connection for model if needed
  83. * init => bool - force initialization process for storage model
  84. * )
  85. *
  86. * @param int|null $storage
  87. * @param array $params
  88. * @return Mage_Core_Model_Abstract|bool
  89. */
  90. public function getStorageModel($storage = null, $params = array())
  91. {
  92. if (is_null($storage)) {
  93. $storage = Mage::helper('core/file_storage')->getCurrentStorageCode();
  94. }
  95. switch ($storage) {
  96. case self::STORAGE_MEDIA_FILE_SYSTEM:
  97. $model = Mage::getModel('core/file_storage_file');
  98. break;
  99. case self::STORAGE_MEDIA_DATABASE:
  100. $connection = (isset($params['connection'])) ? $params['connection'] : null;
  101. $model = Mage::getModel('core/file_storage_database', array('connection' => $connection));
  102. break;
  103. default:
  104. return false;
  105. }
  106. if (isset($params['init']) && $params['init']) {
  107. $model->init();
  108. }
  109. return $model;
  110. }
  111. /**
  112. * Synchronize current media storage with defined
  113. * $storage = array(
  114. * type => int
  115. * connection => string
  116. * )
  117. *
  118. * @param array $storage
  119. * @return Mage_Core_Model_File_Storage
  120. */
  121. public function synchronize($storage)
  122. {
  123. if (is_array($storage) && isset($storage['type'])) {
  124. $storageDest = (int) $storage['type'];
  125. $connection = (isset($storage['connection'])) ? $storage['connection'] : null;
  126. $helper = Mage::helper('core/file_storage');
  127. // if unable to sync to internal storage from itself
  128. if ($storageDest == $helper->getCurrentStorageCode() && $helper->isInternalStorage()) {
  129. return $this;
  130. }
  131. $sourceModel = $this->getStorageModel();
  132. $destinationModel = $this->getStorageModel(
  133. $storageDest,
  134. array(
  135. 'connection' => $connection,
  136. 'init' => true
  137. )
  138. );
  139. if (!$sourceModel || !$destinationModel) {
  140. return $this;
  141. }
  142. $hasErrors = false;
  143. $flag = $this->getSyncFlag();
  144. $flagData = array(
  145. 'source' => $sourceModel->getStorageName(),
  146. 'destination' => $destinationModel->getStorageName(),
  147. 'destination_storage_type' => $storageDest,
  148. 'destination_connection_name' => (string) $destinationModel->getConfigConnectionName(),
  149. 'has_errors' => false,
  150. 'timeout_reached' => false
  151. );
  152. $flag->setFlagData($flagData);
  153. $destinationModel->clear();
  154. $offset = 0;
  155. while (($dirs = $sourceModel->exportDirectories($offset)) !== false) {
  156. $flagData['timeout_reached'] = false;
  157. if (!$hasErrors) {
  158. $hasErrors = $this->_synchronizeHasErrors($sourceModel, $destinationModel);
  159. if ($hasErrors) {
  160. $flagData['has_errors'] = true;
  161. }
  162. }
  163. $flag->setFlagData($flagData)
  164. ->save();
  165. $destinationModel->importDirectories($dirs);
  166. $offset += count($dirs);
  167. }
  168. unset($dirs);
  169. $offset = 0;
  170. while (($files = $sourceModel->exportFiles($offset, 1)) !== false) {
  171. $flagData['timeout_reached'] = false;
  172. if (!$hasErrors) {
  173. $hasErrors = $this->_synchronizeHasErrors($sourceModel, $destinationModel);
  174. if ($hasErrors) {
  175. $flagData['has_errors'] = true;
  176. }
  177. }
  178. $flag->setFlagData($flagData)
  179. ->save();
  180. $destinationModel->importFiles($files);
  181. $offset += count($files);
  182. }
  183. unset($files);
  184. }
  185. return $this;
  186. }
  187. /**
  188. * Return current media directory, allowed resources for get.php script, etc.
  189. *
  190. * @return array
  191. */
  192. public static function getScriptConfig()
  193. {
  194. $config = array();
  195. $config['media_directory'] = Mage::getBaseDir('media');
  196. $allowedResources = (array) Mage::app()->getConfig()->getNode(self::XML_PATH_MEDIA_RESOURCE_WHITELIST);
  197. foreach ($allowedResources as $key => $allowedResource) {
  198. $config['allowed_resources'][] = $allowedResource;
  199. }
  200. $config['update_time'] = Mage::getStoreConfig(self::XML_PATH_MEDIA_UPDATE_TIME);
  201. return $config;
  202. }
  203. }