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

https://github.com/speedupmate/Magento-CE-Mirror · PHP · 219 lines · 118 code · 26 blank · 75 comment · 26 complexity · 467aa228d1c33a5882a1f6a758da366a MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Model for synchronization from DB to filesystem
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Core_Model_Resource_File_Storage_File
  34. {
  35. /**
  36. * Prefix of model events names
  37. *
  38. * @var string
  39. */
  40. protected $_mediaBaseDirectory = null;
  41. /**
  42. * List of files/directories that should be ignored when cleaning and reading files from the filesystem
  43. * @var array
  44. */
  45. protected $_ignoredFiles;
  46. /**
  47. * Files at storage
  48. *
  49. * @var array
  50. */
  51. public function getMediaBaseDirectory()
  52. {
  53. if (is_null($this->_mediaBaseDirectory)) {
  54. $this->_mediaBaseDirectory = Mage::helper('core/file_storage_database')->getMediaBaseDir();
  55. }
  56. return $this->_mediaBaseDirectory;
  57. }
  58. /**
  59. * Collect files and directories recursively
  60. *
  61. * @param string$dir
  62. * @return array
  63. */
  64. public function getStorageData($dir = '')
  65. {
  66. $files = array();
  67. $directories = array();
  68. $currentDir = $this->getMediaBaseDirectory() . $dir;
  69. $ignoredFiles = array_merge(array('.', '..'), $this->_getIgnoredFiles());
  70. if (is_dir($currentDir)) {
  71. $dh = opendir($currentDir);
  72. if ($dh) {
  73. while (($file = readdir($dh)) !== false) {
  74. if (in_array($file, $ignoredFiles)) {
  75. continue;
  76. }
  77. $fullPath = $currentDir . DS . $file;
  78. $relativePath = $dir . DS . $file;
  79. if (is_dir($fullPath)) {
  80. $directories[] = array(
  81. 'name' => $file,
  82. 'path' => str_replace(DS, '/', ltrim($dir, DS))
  83. );
  84. $data = $this->getStorageData($relativePath);
  85. $directories = array_merge($directories, $data['directories']);
  86. $files = array_merge($files, $data['files']);
  87. } else {
  88. $files[] = $relativePath;
  89. }
  90. }
  91. closedir($dh);
  92. }
  93. }
  94. return array('files' => $files, 'directories' => $directories);
  95. }
  96. /**
  97. * Clear files and directories in storage
  98. *
  99. * @param string $dir
  100. * @return Mage_Core_Model_Mysql4_File_Storage_File
  101. */
  102. public function clear($dir = '')
  103. {
  104. $currentDir = $this->getMediaBaseDirectory() . $dir;
  105. $ignoredFiles = array_merge(array('.', '..'), $this->_getIgnoredFiles());
  106. if (is_dir($currentDir)) {
  107. $dh = opendir($currentDir);
  108. if ($dh) {
  109. while (($file = readdir($dh)) !== false) {
  110. if (in_array($file, $ignoredFiles)) {
  111. continue;
  112. }
  113. $fullPath = $currentDir . DS . $file;
  114. if (is_dir($fullPath)) {
  115. $this->clear($dir . DS . $file);
  116. } else {
  117. @unlink($fullPath);
  118. }
  119. }
  120. closedir($dh);
  121. @rmdir($currentDir);
  122. }
  123. }
  124. return $this;
  125. }
  126. /**
  127. * Returns list of files/directories that should be ignored when cleaning and reading files from the filesystem
  128. * @return array
  129. */
  130. protected function _getIgnoredFiles()
  131. {
  132. if (null === $this->_ignoredFiles) {
  133. $ignored = (string)Mage::app()->getConfig()
  134. ->getNode(Mage_Core_Model_File_Storage::XML_PATH_MEDIA_RESOURCE_IGNORED);
  135. $this->_ignoredFiles = $ignored ? explode(',', $ignored) : array();
  136. }
  137. return $this->_ignoredFiles;
  138. }
  139. /**
  140. * Save directory to storage
  141. *
  142. * @param array $dir
  143. * @return bool
  144. */
  145. public function saveDir($dir)
  146. {
  147. if (!isset($dir['name']) || !strlen($dir['name'])
  148. || !isset($dir['path'])
  149. ) {
  150. return false;
  151. }
  152. $path = (strlen($dir['path']))
  153. ? $dir['path'] . DS . $dir['name']
  154. : $dir['name'];
  155. $path = Mage::helper('core/file_storage_database')->getMediaBaseDir() . DS . str_replace('/', DS, $path);
  156. if (!file_exists($path) || !is_dir($path)) {
  157. if (!@mkdir($path, 0777, true)) {
  158. Mage::throwException(Mage::helper('core')->__('Unable to create directory: %s', $path));
  159. }
  160. }
  161. return true;
  162. }
  163. /**
  164. * Save file to storage
  165. *
  166. * @param string $filePath
  167. * @param string $content
  168. * @param bool $overwrite
  169. * @return bool
  170. */
  171. public function saveFile($filePath, $content, $overwrite = false)
  172. {
  173. $filename = basename($filePath);
  174. $path = $this->getMediaBaseDirectory() . DS . str_replace('/', DS ,dirname($filePath));
  175. if (!file_exists($path) || !is_dir($path)) {
  176. @mkdir($path, 0777, true);
  177. }
  178. $ioFile = new Varien_Io_File();
  179. $ioFile->cd($path);
  180. if (!$ioFile->fileExists($filename) || ($overwrite && $ioFile->rm($filename))) {
  181. $ioFile->streamOpen($filename);
  182. $ioFile->streamLock(true);
  183. $result = $ioFile->streamWrite($content);
  184. $ioFile->streamUnlock();
  185. $ioFile->streamClose();
  186. if ($result !== false) {
  187. return true;
  188. }
  189. Mage::throwException(Mage::helper('core')->__('Unable to save file: %s', $filePath));
  190. }
  191. return false;
  192. }
  193. }