/src/classes/XLite/View/BrowseServer.php

https://github.com/Koc/core · PHP · 202 lines · 70 code · 20 blank · 112 comment · 1 complexity · 170c6e6215dacffdfd7cdf358d94d626 MD5 · raw file

  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * PHP version 5.3.0
  17. *
  18. * @category LiteCommerce
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2011 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link http://www.litecommerce.com/
  23. * @see ____file_see____
  24. * @since 1.0.7
  25. */
  26. namespace XLite\View;
  27. /**
  28. * File Selector Dialog widget
  29. *
  30. * @see ____class_see____
  31. * @since 1.0.7
  32. *
  33. * @ListChild (list="admin.center", zone="admin")
  34. */
  35. class BrowseServer extends \XLite\View\SimpleDialog
  36. {
  37. /**
  38. * File entries cache
  39. *
  40. * @var array
  41. * @see ____var_see____
  42. * @since 1.0.7
  43. *
  44. */
  45. protected $fsEntries = array('catalog' => array(), 'file' => array());
  46. /**
  47. * Return list of allowed targets
  48. *
  49. * @return array
  50. * @see ____func_see____
  51. * @since 1.0.0
  52. */
  53. public static function getAllowedTargets()
  54. {
  55. $list = parent::getAllowedTargets();
  56. $list[] = 'browse_server';
  57. return $list;
  58. }
  59. /**
  60. * Return files catalog repository. {lc_catalog}/files
  61. *
  62. * @return string
  63. * @see ____func_see____
  64. * @since 1.0.7
  65. */
  66. public static function getFilesCatalog()
  67. {
  68. return LC_DIR_ROOT . 'files';
  69. }
  70. /**
  71. * Check path to be inside the files catalog repository. {lc_catalog}/files
  72. * Return full path that inside the repository.
  73. * If path is out the one then returns the catalog repository path.
  74. *
  75. * @return string
  76. * @see ____func_see____
  77. * @since 1.0.7
  78. */
  79. public static function getNormalizedPath($path)
  80. {
  81. $filesCatalog = \XLite\View\BrowseServer::getFilesCatalog();
  82. $path = \Includes\Utils\FileManager::getRealPath(
  83. $filesCatalog . LC_DS . $path
  84. );
  85. return ($filesCatalog !== substr($path, 0, strlen($filesCatalog)))
  86. ? $filesCatalog
  87. : $path;
  88. }
  89. /**
  90. * Return title. "Browse server"
  91. *
  92. * @return string
  93. * @see ____func_see____
  94. * @since 1.0.0
  95. */
  96. protected function getHead()
  97. {
  98. return 'Browse server';
  99. }
  100. /**
  101. * Return file name for the center part template
  102. *
  103. * @return string
  104. * @see ____func_see____
  105. * @since 1.0.0
  106. */
  107. protected function getBody()
  108. {
  109. return 'browse_server/body.tpl';
  110. }
  111. /**
  112. * Return current catalog
  113. *
  114. * @return string
  115. * @see ____func_see____
  116. * @since 1.0.7
  117. */
  118. protected function getCurrentCatalog()
  119. {
  120. return \XLite\View\BrowseServer::getNormalizedPath(\XLite\Core\Request::getInstance()->catalog);
  121. }
  122. /**
  123. * Return catalog info for AJAX JS structure
  124. * current_catalog - current catalog
  125. * up_catalog - catalog path to UP level link
  126. *
  127. * @return array
  128. * @see ____func_see____
  129. * @since 1.0.7
  130. */
  131. protected function getCatalogInfo()
  132. {
  133. $currentCatalog = $this->getCurrentCatalog();
  134. $filesCatalog = $this->getFilesCatalog();
  135. return array(
  136. 'current_catalog' => str_replace($filesCatalog, '', $currentCatalog),
  137. 'up_catalog' => str_replace(
  138. $filesCatalog,
  139. '',
  140. $currentCatalog === $filesCatalog ? $currentCatalog : dirname($currentCatalog)
  141. ),
  142. );
  143. }
  144. /**
  145. * Return files entries structure
  146. * type - 'catalog' or 'file' value
  147. * extension - extension of file entry. CSS class will be added according this parameter
  148. * name - name of entry (catalog/file) inside the current catalog.
  149. *
  150. * Catalog entries go first in the entries list
  151. *
  152. * @return array
  153. * @see ____func_see____
  154. * @since 1.0.7
  155. */
  156. protected function getFSEntries()
  157. {
  158. $iterator = new \FilesystemIterator($this->getCurrentCatalog());
  159. foreach ($iterator as $file) {
  160. $path = $file->getPathname();
  161. $info = pathinfo($path);
  162. $type = $file->isDir() ? 'catalog' : 'file';
  163. $this->fsEntries[$type][$path] = array(
  164. 'type' => $type,
  165. 'extension' => $info['extension'],
  166. 'name' => $file->getBasename(),
  167. );
  168. }
  169. return $this->fsEntries['catalog'] + $this->fsEntries['file'];
  170. }
  171. /**
  172. * Return true if there is no files or catalogs inside the current one
  173. *
  174. * @return boolean
  175. * @see ____func_see____
  176. * @since 1.0.7
  177. */
  178. protected function isEmptyCatalog()
  179. {
  180. return count($this->fsEntries['catalog'] + $this->fsEntries['file']) == 0;
  181. }
  182. }