1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Core_Model_Resource_File_Storage_File
36: {
37: 38: 39: 40: 41:
42: protected $_mediaBaseDirectory = null;
43:
44: 45: 46: 47: 48:
49: public function getMediaBaseDirectory()
50: {
51: if (is_null($this->_mediaBaseDirectory)) {
52: $this->_mediaBaseDirectory = Mage::helper('core/file_storage_database')->getMediaBaseDir();
53: }
54:
55: return $this->_mediaBaseDirectory;
56: }
57:
58: 59: 60: 61: 62: 63:
64: public function getStorageData($dir = '')
65: {
66: $files = array();
67: $directories = array();
68: $currentDir = $this->getMediaBaseDirectory() . $dir;
69:
70: if (is_dir($currentDir)) {
71: $dh = opendir($currentDir);
72: if ($dh) {
73: while (($file = readdir($dh)) !== false) {
74: if ($file == '.' || $file == '..' || $file == '.svn' || $file == '.htaccess') {
75: continue;
76: }
77:
78: $fullPath = $currentDir . DS . $file;
79: $relativePath = $dir . DS . $file;
80: if (is_dir($fullPath)) {
81: $directories[] = array(
82: 'name' => $file,
83: 'path' => str_replace(DS, '/', ltrim($dir, DS))
84: );
85:
86: $data = $this->getStorageData($relativePath);
87: $directories = array_merge($directories, $data['directories']);
88: $files = array_merge($files, $data['files']);
89: } else {
90: $files[] = $relativePath;
91: }
92: }
93: closedir($dh);
94: }
95: }
96:
97: return array('files' => $files, 'directories' => $directories);
98: }
99:
100: 101: 102: 103: 104: 105:
106: public function clear($dir = '')
107: {
108: $currentDir = $this->getMediaBaseDirectory() . $dir;
109:
110: if (is_dir($currentDir)) {
111: $dh = opendir($currentDir);
112: if ($dh) {
113: while (($file = readdir($dh)) !== false) {
114: if ($file == '.' || $file == '..') {
115: continue;
116: }
117:
118: $fullPath = $currentDir . DS . $file;
119: if (is_dir($fullPath)) {
120: $this->clear($dir . DS . $file);
121: } else {
122: @unlink($fullPath);
123: }
124: }
125: closedir($dh);
126: @rmdir($currentDir);
127: }
128: }
129:
130: return $this;
131: }
132:
133: 134: 135: 136: 137: 138:
139: public function saveDir($dir)
140: {
141: if (!isset($dir['name']) || !strlen($dir['name'])
142: || !isset($dir['path'])
143: ) {
144: return false;
145: }
146:
147: $path = (strlen($dir['path']))
148: ? $dir['path'] . DS . $dir['name']
149: : $dir['name'];
150: $path = Mage::helper('core/file_storage_database')->getMediaBaseDir() . DS . str_replace('/', DS, $path);
151:
152: if (!file_exists($path) || !is_dir($path)) {
153: if (!@mkdir($path, 0777, true)) {
154: Mage::throwException(Mage::helper('core')->__('Unable to create directory: %s', $path));
155: }
156: }
157:
158: return true;
159: }
160:
161: 162: 163: 164: 165: 166: 167: 168:
169: public function saveFile($filePath, $content, $overwrite = false)
170: {
171: $filename = basename($filePath);
172: $path = $this->getMediaBaseDirectory() . DS . str_replace('/', DS ,dirname($filePath));
173:
174: if (!file_exists($path) || !is_dir($path)) {
175: @mkdir($path, 0777, true);
176: }
177:
178: $ioFile = new Varien_Io_File();
179: $ioFile->cd($path);
180:
181: if (!$ioFile->fileExists($filename) || ($overwrite && $ioFile->rm($filename))) {
182: $ioFile->streamOpen($filename);
183: $ioFile->streamLock(true);
184: $result = $ioFile->streamWrite($content);
185: $ioFile->streamUnlock();
186: $ioFile->streamClose();
187:
188: if ($result) {
189: return true;
190: }
191:
192: Mage::throwException(Mage::helper('core')->__('Unable to save file: %s', $filePath));
193: }
194:
195: return false;
196: }
197: }
198: