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_Dataflow_Model_Convert_Adapter_Io extends Mage_Dataflow_Model_Convert_Adapter_Abstract
36: {
37: const XML_PATH_EXPORT_LOCAL_VALID_PATH = 'general/file/importexport_local_valid_paths';
38:
39: 40: 41:
42: public function getResource($forWrite = false)
43: {
44: if (!$this->_resource) {
45: $type = $this->getVar('type', 'file');
46: $className = 'Varien_Io_' . ucwords($type);
47: $this->_resource = new $className();
48:
49: $isError = false;
50:
51: $ioConfig = $this->getVars();
52: switch ($this->getVar('type', 'file')) {
53: case 'file':
54:
55: $path = rtrim($ioConfig['path'], '\\/')
56: . DS . $ioConfig['filename'];
57:
58: $validator = Mage::getModel('core/file_validator_availablePath');
59: $validator->setPaths( Mage::getStoreConfig(self::XML_PATH_EXPORT_LOCAL_VALID_PATH) );
60: if (!$validator->isValid($path)) {
61: foreach ($validator->getMessages() as $message) {
62: Mage::throwException($message);
63: return false;
64: }
65: }
66:
67: if (preg_match('#^' . preg_quote(DS, '#').'#', $this->getVar('path')) ||
68: preg_match('#^[a-z]:' . preg_quote(DS, '#') . '#i', $this->getVar('path'))) {
69:
70: $path = $this->_resource->getCleanPath($this->getVar('path'));
71: } else {
72: $baseDir = Mage::getBaseDir();
73: $path = $this->_resource->getCleanPath($baseDir . DS . trim($this->getVar('path'), DS));
74: }
75:
76: $this->_resource->checkAndCreateFolder($path);
77:
78: $realPath = realpath($path);
79:
80: if (!$isError && $realPath === false) {
81: $message = Mage::helper('dataflow')->__('The destination folder "%s" does not exist or there is no access to create it.', $ioConfig['path']);
82: Mage::throwException($message);
83: } elseif (!$isError && !is_dir($realPath)) {
84: $message = Mage::helper('dataflow')->__('Destination folder "%s" is not a directory.', $realPath);
85: Mage::throwException($message);
86: } elseif (!$isError) {
87: if ($forWrite && !is_writeable($realPath)) {
88: $message = Mage::helper('dataflow')->__('Destination folder "%s" is not writable.', $realPath);
89: Mage::throwException($message);
90: } else {
91: $ioConfig['path'] = rtrim($realPath, DS);
92: }
93: }
94: break;
95: default:
96: $ioConfig['path'] = rtrim($this->getVar('path'), '/');
97: break;
98: }
99:
100: if ($isError) {
101: return false;
102: }
103: try {
104: $this->_resource->open($ioConfig);
105: } catch (Exception $e) {
106: $message = Mage::helper('dataflow')->__('An error occurred while opening file: "%s".', $e->getMessage());
107: Mage::throwException($message);
108: }
109: }
110: return $this->_resource;
111: }
112:
113: 114: 115: 116: 117:
118: public function load()
119: {
120: if (!$this->getResource()) {
121: return $this;
122: }
123:
124: $batchModel = Mage::getSingleton('dataflow/batch');
125: $destFile = $batchModel->getIoAdapter()->getFile(true);
126:
127: $result = $this->getResource()->read($this->getVar('filename'), $destFile);
128: $filename = $this->getResource()->pwd() . '/' . $this->getVar('filename');
129: if (false === $result) {
130: $message = Mage::helper('dataflow')->__('Could not load file: "%s".', $filename);
131: Mage::throwException($message);
132: } else {
133: $message = Mage::helper('dataflow')->__('Loaded successfully: "%s".', $filename);
134: $this->addException($message);
135: }
136:
137: $this->setData($result);
138: return $this;
139: }
140:
141: 142: 143: 144: 145:
146: public function save()
147: {
148: if (!$this->getResource(true)) {
149: return $this;
150: }
151:
152: $batchModel = Mage::getSingleton('dataflow/batch');
153:
154: $dataFile = $batchModel->getIoAdapter()->getFile(true);
155:
156: $filename = $this->getVar('filename');
157:
158: $result = $this->getResource()->write($filename, $dataFile, 0777);
159:
160: if (false === $result) {
161: $message = Mage::helper('dataflow')->__('Could not save file: %s.', $filename);
162: Mage::throwException($message);
163: } else {
164: $message = Mage::helper('dataflow')->__('Saved successfully: "%s" [%d byte(s)].', $filename, $batchModel->getIoAdapter()->getFileSize());
165: if ($this->getVar('link')) {
166: $message .= Mage::helper('dataflow')->__('<a href="%s" target="_blank">Link</a>', $this->getVar('link'));
167: }
168: $this->addException($message);
169: }
170: return $this;
171: }
172: }
173: