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

https://bitbucket.org/acidel/buykoala · PHP · 197 lines · 106 code · 24 blank · 67 comment · 35 complexity · ee392488bfff9d464ecf69ac2d5bc75a 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@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) 2011 Magento Inc. (http://www.magentocommerce.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. * Files at storage
  43. *
  44. * @var array
  45. */
  46. public function getMediaBaseDirectory()
  47. {
  48. if (is_null($this->_mediaBaseDirectory)) {
  49. $this->_mediaBaseDirectory = Mage::helper('core/file_storage_database')->getMediaBaseDir();
  50. }
  51. return $this->_mediaBaseDirectory;
  52. }
  53. /**
  54. * Collect files and directories recursively
  55. *
  56. * @param string$dir
  57. * @return array
  58. */
  59. public function getStorageData($dir = '')
  60. {
  61. $files = array();
  62. $directories = array();
  63. $currentDir = $this->getMediaBaseDirectory() . $dir;
  64. if (is_dir($currentDir)) {
  65. $dh = opendir($currentDir);
  66. if ($dh) {
  67. while (($file = readdir($dh)) !== false) {
  68. if ($file == '.' || $file == '..' || $file == '.svn' || $file == '.htaccess') {
  69. continue;
  70. }
  71. $fullPath = $currentDir . DS . $file;
  72. $relativePath = $dir . DS . $file;
  73. if (is_dir($fullPath)) {
  74. $directories[] = array(
  75. 'name' => $file,
  76. 'path' => str_replace(DS, '/', ltrim($dir, DS))
  77. );
  78. $data = $this->getStorageData($relativePath);
  79. $directories = array_merge($directories, $data['directories']);
  80. $files = array_merge($files, $data['files']);
  81. } else {
  82. $files[] = $relativePath;
  83. }
  84. }
  85. closedir($dh);
  86. }
  87. }
  88. return array('files' => $files, 'directories' => $directories);
  89. }
  90. /**
  91. * Clear files and directories in storage
  92. *
  93. * @param string $dir
  94. * @return Mage_Core_Model_Mysql4_File_Storage_File
  95. */
  96. public function clear($dir = '')
  97. {
  98. $currentDir = $this->getMediaBaseDirectory() . $dir;
  99. if (is_dir($currentDir)) {
  100. $dh = opendir($currentDir);
  101. if ($dh) {
  102. while (($file = readdir($dh)) !== false) {
  103. if ($file == '.' || $file == '..') {
  104. continue;
  105. }
  106. $fullPath = $currentDir . DS . $file;
  107. if (is_dir($fullPath)) {
  108. $this->clear($dir . DS . $file);
  109. } else {
  110. @unlink($fullPath);
  111. }
  112. }
  113. closedir($dh);
  114. @rmdir($currentDir);
  115. }
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Save directory to storage
  121. *
  122. * @param array $dir
  123. * @return bool
  124. */
  125. public function saveDir($dir)
  126. {
  127. if (!isset($dir['name']) || !strlen($dir['name'])
  128. || !isset($dir['path'])
  129. ) {
  130. return false;
  131. }
  132. $path = (strlen($dir['path']))
  133. ? $dir['path'] . DS . $dir['name']
  134. : $dir['name'];
  135. $path = Mage::helper('core/file_storage_database')->getMediaBaseDir() . DS . str_replace('/', DS, $path);
  136. if (!file_exists($path) || !is_dir($path)) {
  137. if (!@mkdir($path, 0777, true)) {
  138. Mage::throwException(Mage::helper('core')->__('Unable to create directory: %s', $path));
  139. }
  140. }
  141. return true;
  142. }
  143. /**
  144. * Save file to storage
  145. *
  146. * @param string $filePath
  147. * @param string $content
  148. * @param bool $overwrite
  149. * @return bool
  150. */
  151. public function saveFile($filePath, $content, $overwrite = false)
  152. {
  153. $filename = basename($filePath);
  154. $path = $this->getMediaBaseDirectory() . DS . str_replace('/', DS ,dirname($filePath));
  155. if (!file_exists($path) || !is_dir($path)) {
  156. @mkdir($path, 0777, true);
  157. }
  158. $ioFile = new Varien_Io_File();
  159. $ioFile->cd($path);
  160. if (!$ioFile->fileExists($filename) || ($overwrite && $ioFile->rm($filename))) {
  161. $ioFile->streamOpen($filename);
  162. $ioFile->streamLock(true);
  163. $result = $ioFile->streamWrite($content);
  164. $ioFile->streamUnlock();
  165. $ioFile->streamClose();
  166. if ($result) {
  167. return true;
  168. }
  169. Mage::throwException(Mage::helper('core')->__('Unable to save file: %s', $filePath));
  170. }
  171. return false;
  172. }
  173. }