/app/code/Ecart/Admin/controllers/Catalog/ImageController.php
PHP | 264 lines | 213 code | 6 blank | 45 comment | 8 complexity | 74a3478812377682145d9b7dfd8ba4e6 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
- <?php
- /**
- * Ecart
- *
- * This file is part of Ecart.
- *
- * Ecart is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Ecart is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Ecart. If not, see <http://www.gnu.org/licenses/>.
- *
- * @category Ecart
- * @package Ecart_Admin
- * @copyright Copyright 2008-2009 E-Cart LLC
- * @license GNU Public License V3.0
- */
- /**
- *
- * @category Ecart
- * @package Ecart_Admin
- * @subpackage Controller
- * @author Ecart Core Team <core@ecartcommerce.com>
- */
- class Ecart_Admin_Catalog_ImageController extends Ecart_Admin_Controller_Back
- {
- /**
- * @param string $path
- * @return string|false
- */
- private function _getPath($path)
- {
- $root = Ecart::config()->system->path . '/media';
-
- $path = trim($path, '/\\');
- $path = Ecart::config()->system->path . '/' . $path;
-
- $path = str_replace('\\', '/', realpath($path));
- if (file_exists($path) && strpos($path, $root) === 0) {
- return $path;
- }
- return false;
- }
-
- /**
- * @param string $path
- * @return string
- */
- private function _getIcon($path)
- {
- if (is_dir($path)) {
- return 'folder';
- } else {
- if (false !== strrpos($path, '.')) {
- return 'file-' . substr($path, strrpos($path, '.') + 1);
- }
- }
- return 'file';
- }
-
- /**
- * @param string $path
- * @param string $mode ['all', 'file', 'folder']
- * @param bool $recursive
- * @param array $items [optional]
- * @return array
- */
- private function _scanFolder($path, $mode, $recursive, $items = array())
- {
- if (!is_dir($path)) {
- return $items;
- }
- try {
- $di = new DirectoryIterator($path);
- } catch (Exception $e) {
- throw new Ecart_Exception(
- Ecart::translate('core')->__(
- "Directory %s not readable", $path
- )
- );
- }
-
- foreach ($di as $f) {
- $s = $f->getFilename();
- if ($s[0] == '.') {
- continue;
- }
-
- $path = str_replace('\\', '/', $f->getPathname());
-
- $isDir = $f->isDir();
-
- if (($isDir && $mode != 'file') || (!$isDir && $mode != 'folder')) {
- $item = array(
- 'text' => $f->getBasename(),
- 'absolute_path' => $path,
- 'path' => str_replace(Ecart::config('system/path') . '/', '', $path),
- 'absolute_url' => $this->view->href(
- str_replace(Ecart::config('system/path'), '', $path),
- false
- ),
- 'iconCls' => $this->_getIcon($path),
- 'leaf' => !$isDir,
- 'is_dir' => $isDir
- );
- if (!$isDir) {
- $item['qtip'] = 'Size: ' . $f->getSize();
- }
- $items[] = $item;
- }
-
- if ($isDir && $recursive) {
- $items += $this->_scanFolder($path, $mode, $recursive, $items);
- }
- }
-
- return $items;
- }
-
- /**
- * @param string $path
- * @return bool
- */
- private function _delete($path)
- {
- /* check are we in 'ROOT/media' */
- if (false === strpos($path, Ecart::config()->system->path . '/media')) {
- return false;
- }
-
- if (is_dir($path)) {
- $path = rtrim($path, '/');
- $dir = dir($path);
- while (false !== ($file = $dir->read())) {
- if ($file != '.' && $file != '..') {
- (!is_link("$path/$file") && is_dir("$path/$file")) ?
- $this->_delete("$path/$file") : unlink("$path/$file");
- }
- }
- $dir->close();
- rmdir($path);
- return true;
- } else {
- unlink($path);
- }
-
- return false;
- }
-
- private function _getAction()
- {
- $result = array();
- if ($path = $this->_getPath($this->_getParam('path'))) {
- $result = $this->_scanFolder(
- $path,
- $this->_getParam('mode', 'all'),
- (bool) $this->_getParam('recursive', false)
- );
- }
-
- return $this->_helper->json->sendJson($result, false, false);
- }
-
- private function _uploadAction()
- {
- $this->_helper->layout->disableLayout();
-
- $result = array();
- foreach ($_FILES as $key => $values) {
- if (strpos($key, 'ext-gen') !== 0) {
- continue;
- }
- try {
- $uploader = new Ecart_File_Uploader($key);
- $file = $uploader
- ->setUseDispersion(false)
- ->save(Ecart::config()->system->path . '/' . $this->_getParam('path'));
-
- $result = array(
- 'success' => true,
- 'data' => array(
- 'path' => $file['path'],
- 'file' => $file['file']
- )
- );
- } catch (Ecart_Exception $e) {
- $result = array(
- 'success' => false,
- 'messages' => array(
- 'error' => $e->getMessage()
- )
- );
- }
- }
-
- return $this->getResponse()->appendBody(Zend_Json_Encoder::encode($result));
- }
-
- public function _deleteAction()
- {
- if ($this->_getParam('batch', 0)) {
- foreach (Zend_Json::decode($this->_getParam('file')) as $path) {
- $this->_delete(Ecart::config()->system->path . '/' . $path);
- }
- return $this->_helper->json->sendSuccess();
- }
-
- if (!$this->_delete(Ecart::config()->system->path . '/' . $this->_getParam('file'))) {
- return $this->_helper->json->sendFailure();
- }
-
- return $this->_helper->json->sendSuccess();
- }
-
- public function _newdirAction()
- {
- if (!@mkdir(Ecart::config()->system->path . '/' . $this->_getParam('dir'), 0777, true)) {
- return $this->_helper->json->sendFailure();
- }
-
- return $this->_helper->json->sendSuccess();
- }
-
- public function _renameAction()
- {
- if (!@rename(
- Ecart::config()->system->path . '/' . $this->_getParam('oldname'),
- Ecart::config()->system->path . '/' . $this->_getParam('newname')
- )) {
-
- return $this->_helper->json->sendFailure();
- }
-
- return $this->_helper->json->sendSuccess();
- }
-
- public function treePanelAction()
- {
- $this->_helper->layout->disableLayout();
-
- $cmd = $this->_getParam('cmd');
- $method = '_' . $cmd . 'Action';
-
- if (!method_exists($this, $method)) {
- Ecart::message()->addError(Ecart::translate('catalog')->__(
- 'Method %s not exist', $method
- ));
- return $this->getResponse()->appendBody(Zend_Json_Encoder::encode(array(
- 'success' => false,
- 'messages' => Ecart::message()->get()
- )));
- }
-
- return $this->$method();
- }
- }