PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Dataflow/Model/Convert/Adapter/Io.php

https://github.com/rgranadino/magento-mirror
PHP | 172 lines | 105 code | 21 blank | 46 comment | 19 complexity | 3411f0855af99d5cab27ead01b1902a7 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_Dataflow
  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. * Convert IO adapter
  28. *
  29. * @category Mage
  30. * @package Mage_Dataflow
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Dataflow_Model_Convert_Adapter_Io extends Mage_Dataflow_Model_Convert_Adapter_Abstract
  34. {
  35. const XML_PATH_EXPORT_LOCAL_VALID_PATH = 'general/file/importexport_local_valid_paths';
  36. /**
  37. * @return Varien_Io_Abstract
  38. */
  39. public function getResource($forWrite = false)
  40. {
  41. if (!$this->_resource) {
  42. $type = $this->getVar('type', 'file');
  43. $className = 'Varien_Io_'.ucwords($type);
  44. $this->_resource = new $className();
  45. $isError = false;
  46. $ioConfig = $this->getVars();
  47. switch ($this->getVar('type', 'file')) {
  48. case 'file':
  49. //validate export/import path
  50. $path = rtrim($ioConfig['path'], '\\/')
  51. . DS . $ioConfig['filename'];
  52. /** @var $validator Mage_Core_Model_File_Validator_AvailablePath */
  53. $validator = Mage::getModel('core/file_validator_availablePath');
  54. $validator->setPaths( Mage::getStoreConfig(self::XML_PATH_EXPORT_LOCAL_VALID_PATH) );
  55. if (!$validator->isValid($path)) {
  56. foreach ($validator->getMessages() as $message) {
  57. Mage::throwException($message);
  58. return false;
  59. }
  60. }
  61. if (preg_match('#^' . preg_quote(DS, '#').'#', $this->getVar('path')) ||
  62. preg_match('#^[a-z]:' . preg_quote(DS, '#') . '#i', $this->getVar('path'))) {
  63. $path = $this->_resource->getCleanPath($this->getVar('path'));
  64. } else {
  65. $baseDir = Mage::getBaseDir();
  66. $path = $this->_resource->getCleanPath($baseDir . DS . trim($this->getVar('path'), DS));
  67. }
  68. $this->_resource->checkAndCreateFolder($path);
  69. $realPath = realpath($path);
  70. if (!$isError && $realPath === false) {
  71. $message = Mage::helper('dataflow')->__('The destination folder "%s" does not exist or there is no access to create it.', $ioConfig['path']);
  72. Mage::throwException($message);
  73. } elseif (!$isError && !is_dir($realPath)) {
  74. $message = Mage::helper('dataflow')->__('Destination folder "%s" is not a directory.', $realPath);
  75. Mage::throwException($message);
  76. } elseif (!$isError) {
  77. if ($forWrite && !is_writeable($realPath)) {
  78. $message = Mage::helper('dataflow')->__('Destination folder "%s" is not writable.', $realPath);
  79. Mage::throwException($message);
  80. } else {
  81. $ioConfig['path'] = rtrim($realPath, DS);
  82. }
  83. }
  84. break;
  85. default:
  86. $ioConfig['path'] = rtrim($this->getVar('path'), '/');
  87. break;
  88. }
  89. if ($isError) {
  90. return false;
  91. }
  92. try {
  93. $this->_resource->open($ioConfig);
  94. } catch (Exception $e) {
  95. $message = Mage::helper('dataflow')->__('An error occurred while opening file: "%s".', $e->getMessage());
  96. Mage::throwException($message);
  97. }
  98. }
  99. return $this->_resource;
  100. }
  101. /**
  102. * Load data
  103. *
  104. * @return Mage_Dataflow_Model_Convert_Adapter_Io
  105. */
  106. public function load()
  107. {
  108. if (!$this->getResource()) {
  109. return $this;
  110. }
  111. $batchModel = Mage::getSingleton('dataflow/batch');
  112. $destFile = $batchModel->getIoAdapter()->getFile(true);
  113. $result = $this->getResource()->read($this->getVar('filename'), $destFile);
  114. $filename = $this->getResource()->pwd() . '/' . $this->getVar('filename');
  115. if (false === $result) {
  116. $message = Mage::helper('dataflow')->__('Could not load file: "%s".', $filename);
  117. Mage::throwException($message);
  118. } else {
  119. $message = Mage::helper('dataflow')->__('Loaded successfully: "%s".', $filename);
  120. $this->addException($message);
  121. }
  122. $this->setData($result);
  123. return $this;
  124. }
  125. /**
  126. * Save result to destination file from temporary
  127. *
  128. * @return Mage_Dataflow_Model_Convert_Adapter_Io
  129. */
  130. public function save()
  131. {
  132. if (!$this->getResource(true)) {
  133. return $this;
  134. }
  135. $batchModel = Mage::getSingleton('dataflow/batch');
  136. $dataFile = $batchModel->getIoAdapter()->getFile(true);
  137. $filename = $this->getVar('filename');
  138. $result = $this->getResource()->write($filename, $dataFile, 0777);
  139. if (false === $result) {
  140. $message = Mage::helper('dataflow')->__('Could not save file: %s.', $filename);
  141. Mage::throwException($message);
  142. } else {
  143. $message = Mage::helper('dataflow')->__('Saved successfully: "%s" [%d byte(s)].', $filename, $batchModel->getIoAdapter()->getFileSize());
  144. if ($this->getVar('link')) {
  145. $message .= Mage::helper('dataflow')->__('<a href="%s" target="_blank">Link</a>', $this->getVar('link'));
  146. }
  147. $this->addException($message);
  148. }
  149. return $this;
  150. }
  151. }