PageRenderTime 96ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/Ecart/Admin/controllers/Catalog/ImageController.php

https://code.google.com/p/ecartcommerce/
PHP | 264 lines | 213 code | 6 blank | 45 comment | 8 complexity | 74a3478812377682145d9b7dfd8ba4e6 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Ecart
  4. *
  5. * This file is part of Ecart.
  6. *
  7. * Ecart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Ecart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Ecart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Ecart
  21. * @package Ecart_Admin
  22. * @copyright Copyright 2008-2009 E-Cart LLC
  23. * @license GNU Public License V3.0
  24. */
  25. /**
  26. *
  27. * @category Ecart
  28. * @package Ecart_Admin
  29. * @subpackage Controller
  30. * @author Ecart Core Team <core@ecartcommerce.com>
  31. */
  32. class Ecart_Admin_Catalog_ImageController extends Ecart_Admin_Controller_Back
  33. {
  34. /**
  35. * @param string $path
  36. * @return string|false
  37. */
  38. private function _getPath($path)
  39. {
  40. $root = Ecart::config()->system->path . '/media';
  41. $path = trim($path, '/\\');
  42. $path = Ecart::config()->system->path . '/' . $path;
  43. $path = str_replace('\\', '/', realpath($path));
  44. if (file_exists($path) && strpos($path, $root) === 0) {
  45. return $path;
  46. }
  47. return false;
  48. }
  49. /**
  50. * @param string $path
  51. * @return string
  52. */
  53. private function _getIcon($path)
  54. {
  55. if (is_dir($path)) {
  56. return 'folder';
  57. } else {
  58. if (false !== strrpos($path, '.')) {
  59. return 'file-' . substr($path, strrpos($path, '.') + 1);
  60. }
  61. }
  62. return 'file';
  63. }
  64. /**
  65. * @param string $path
  66. * @param string $mode ['all', 'file', 'folder']
  67. * @param bool $recursive
  68. * @param array $items [optional]
  69. * @return array
  70. */
  71. private function _scanFolder($path, $mode, $recursive, $items = array())
  72. {
  73. if (!is_dir($path)) {
  74. return $items;
  75. }
  76. try {
  77. $di = new DirectoryIterator($path);
  78. } catch (Exception $e) {
  79. throw new Ecart_Exception(
  80. Ecart::translate('core')->__(
  81. "Directory %s not readable", $path
  82. )
  83. );
  84. }
  85. foreach ($di as $f) {
  86. $s = $f->getFilename();
  87. if ($s[0] == '.') {
  88. continue;
  89. }
  90. $path = str_replace('\\', '/', $f->getPathname());
  91. $isDir = $f->isDir();
  92. if (($isDir && $mode != 'file') || (!$isDir && $mode != 'folder')) {
  93. $item = array(
  94. 'text' => $f->getBasename(),
  95. 'absolute_path' => $path,
  96. 'path' => str_replace(Ecart::config('system/path') . '/', '', $path),
  97. 'absolute_url' => $this->view->href(
  98. str_replace(Ecart::config('system/path'), '', $path),
  99. false
  100. ),
  101. 'iconCls' => $this->_getIcon($path),
  102. 'leaf' => !$isDir,
  103. 'is_dir' => $isDir
  104. );
  105. if (!$isDir) {
  106. $item['qtip'] = 'Size: ' . $f->getSize();
  107. }
  108. $items[] = $item;
  109. }
  110. if ($isDir && $recursive) {
  111. $items += $this->_scanFolder($path, $mode, $recursive, $items);
  112. }
  113. }
  114. return $items;
  115. }
  116. /**
  117. * @param string $path
  118. * @return bool
  119. */
  120. private function _delete($path)
  121. {
  122. /* check are we in 'ROOT/media' */
  123. if (false === strpos($path, Ecart::config()->system->path . '/media')) {
  124. return false;
  125. }
  126. if (is_dir($path)) {
  127. $path = rtrim($path, '/');
  128. $dir = dir($path);
  129. while (false !== ($file = $dir->read())) {
  130. if ($file != '.' && $file != '..') {
  131. (!is_link("$path/$file") && is_dir("$path/$file")) ?
  132. $this->_delete("$path/$file") : unlink("$path/$file");
  133. }
  134. }
  135. $dir->close();
  136. rmdir($path);
  137. return true;
  138. } else {
  139. unlink($path);
  140. }
  141. return false;
  142. }
  143. private function _getAction()
  144. {
  145. $result = array();
  146. if ($path = $this->_getPath($this->_getParam('path'))) {
  147. $result = $this->_scanFolder(
  148. $path,
  149. $this->_getParam('mode', 'all'),
  150. (bool) $this->_getParam('recursive', false)
  151. );
  152. }
  153. return $this->_helper->json->sendJson($result, false, false);
  154. }
  155. private function _uploadAction()
  156. {
  157. $this->_helper->layout->disableLayout();
  158. $result = array();
  159. foreach ($_FILES as $key => $values) {
  160. if (strpos($key, 'ext-gen') !== 0) {
  161. continue;
  162. }
  163. try {
  164. $uploader = new Ecart_File_Uploader($key);
  165. $file = $uploader
  166. ->setUseDispersion(false)
  167. ->save(Ecart::config()->system->path . '/' . $this->_getParam('path'));
  168. $result = array(
  169. 'success' => true,
  170. 'data' => array(
  171. 'path' => $file['path'],
  172. 'file' => $file['file']
  173. )
  174. );
  175. } catch (Ecart_Exception $e) {
  176. $result = array(
  177. 'success' => false,
  178. 'messages' => array(
  179. 'error' => $e->getMessage()
  180. )
  181. );
  182. }
  183. }
  184. return $this->getResponse()->appendBody(Zend_Json_Encoder::encode($result));
  185. }
  186. public function _deleteAction()
  187. {
  188. if ($this->_getParam('batch', 0)) {
  189. foreach (Zend_Json::decode($this->_getParam('file')) as $path) {
  190. $this->_delete(Ecart::config()->system->path . '/' . $path);
  191. }
  192. return $this->_helper->json->sendSuccess();
  193. }
  194. if (!$this->_delete(Ecart::config()->system->path . '/' . $this->_getParam('file'))) {
  195. return $this->_helper->json->sendFailure();
  196. }
  197. return $this->_helper->json->sendSuccess();
  198. }
  199. public function _newdirAction()
  200. {
  201. if (!@mkdir(Ecart::config()->system->path . '/' . $this->_getParam('dir'), 0777, true)) {
  202. return $this->_helper->json->sendFailure();
  203. }
  204. return $this->_helper->json->sendSuccess();
  205. }
  206. public function _renameAction()
  207. {
  208. if (!@rename(
  209. Ecart::config()->system->path . '/' . $this->_getParam('oldname'),
  210. Ecart::config()->system->path . '/' . $this->_getParam('newname')
  211. )) {
  212. return $this->_helper->json->sendFailure();
  213. }
  214. return $this->_helper->json->sendSuccess();
  215. }
  216. public function treePanelAction()
  217. {
  218. $this->_helper->layout->disableLayout();
  219. $cmd = $this->_getParam('cmd');
  220. $method = '_' . $cmd . 'Action';
  221. if (!method_exists($this, $method)) {
  222. Ecart::message()->addError(Ecart::translate('catalog')->__(
  223. 'Method %s not exist', $method
  224. ));
  225. return $this->getResponse()->appendBody(Zend_Json_Encoder::encode(array(
  226. 'success' => false,
  227. 'messages' => Ecart::message()->get()
  228. )));
  229. }
  230. return $this->$method();
  231. }
  232. }