/views/helpers/filemanager.php

https://github.com/ShadowCross/croogo · PHP · 207 lines · 103 code · 11 blank · 93 comment · 18 complexity · 511f51a9095fb1bd47dac408794fe712 MD5 · raw file

  1. <?php
  2. /**
  3. * Filemanager Helper
  4. *
  5. * PHP version 5
  6. *
  7. * @category Helper
  8. * @package Croogo
  9. * @version 1.0
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class FilemanagerHelper extends AppHelper {
  15. /**
  16. * Other helpers used by this helper
  17. *
  18. * @var array
  19. * @access public
  20. */
  21. public $helpers = array('Html');
  22. /**
  23. * Get extension from a file name.
  24. *
  25. * @param string $filename file name
  26. * @return string
  27. */
  28. public function filename2ext($filename) {
  29. $filename = strtolower($filename) ;
  30. //$exts = split("[/\\.]", $filename) ;
  31. //$n = count($exts)-1;
  32. $filename_e = explode(".", $filename);
  33. if ($filename_e == 1) {
  34. return "file";
  35. } else {
  36. $n = count($filename_e) - 1;
  37. return $filename_e[$n];
  38. }
  39. }
  40. /**
  41. * Get icon from file extension
  42. *
  43. * @param string $ext
  44. * @return string
  45. */
  46. public function ext2icon($ext) {
  47. $ext = strtolower($ext);
  48. $ext2icon = array(
  49. 'css' => 'css.png',
  50. 'htm' => 'html.png',
  51. 'html' => 'html.png',
  52. 'php' => 'page_white_php.png',
  53. 'rar' => 'page_white_compressed.png',
  54. 'tar' => 'page_white_compressed.png',
  55. 'zip' => 'page_white_compressed.png',
  56. 'bmp' => 'picture.png',
  57. 'gif' => 'picture.png',
  58. 'jpg' => 'picture.png',
  59. 'jpeg' => 'picture.png',
  60. 'png' => 'picture.png',
  61. );
  62. if (isset($ext2icon[$ext])) {
  63. $output = $ext2icon[$ext];
  64. } else {
  65. $output = 'page_white.png';
  66. }
  67. return $output;
  68. }
  69. /**
  70. * Get icon from file name
  71. *
  72. * @param string $filename file name
  73. */
  74. public function filename2icon($filename) {
  75. $ext = $this->filename2ext($filename);
  76. $icon = $this->ext2icon($ext);
  77. return $icon;
  78. }
  79. /**
  80. * Breadcrumb
  81. *
  82. * @param string $path absolute path
  83. * @return string
  84. */
  85. public function breadcrumb($path) {
  86. $path_e = explode(DS, $path);
  87. $output = array();
  88. if (DS == '/') {
  89. $current_path = DS;
  90. } else {
  91. $current_path = '';
  92. }
  93. foreach ($path_e AS $p) {
  94. if ($p != null) {
  95. $current_path .= $p.DS;
  96. $output[$p] = $current_path;
  97. }
  98. }
  99. return $output;
  100. }
  101. /**
  102. * Generate anchor tag for a file/directory
  103. *
  104. * @param string $title link title
  105. * @param array $url link url
  106. * @param string $path file/directory path
  107. * @param string $pathKey default is 'path'
  108. * @return string
  109. */
  110. public function link($title, $url, $path, $pathKey = 'path') {
  111. $onclick = '';
  112. if (isset($url['action']) && ($url['action'] == 'delete_directory' || $url['action'] == 'delete_file')) {
  113. $onclick = 'return confirm(&#039;Are you sure?&#039;);';
  114. }
  115. $output = "<a onclick='{$onclick}' href='" . $this->Html->url($url) . "?{$pathKey}=" . urlencode($path) . "'>" . $title . "</a>";
  116. return $output;
  117. }
  118. /**
  119. * Generate anchor tag for directory
  120. *
  121. * @param string $title link title
  122. * @param string $path directory path
  123. * @return string
  124. */
  125. public function linkDirectory($title, $path) {
  126. $output = $this->link($title, array('controller' => 'filemanager', 'action' => 'browse'), $path);
  127. return $output;
  128. }
  129. /**
  130. * Generate anchor tag for file
  131. *
  132. * @param string $title
  133. * @param string $path
  134. * @return string
  135. */
  136. public function linkFile($title, $path) {
  137. $output = "<a href='" . $this->Html->url(array('controller' => 'filemanager', 'action' => 'editfile')) . "?path=" . urlencode($path) . "'>{$title}</a>";
  138. return $output;
  139. }
  140. /**
  141. * Generate anchor tag for upload link
  142. *
  143. * @param string $title link title
  144. * @param string $path absolute path
  145. * @return string
  146. */
  147. public function linkUpload($title, $path) {
  148. $output = $this->link($title, array('controller' => 'filemanager', 'action' => 'upload'), $path);
  149. return $output;
  150. }
  151. /**
  152. * Generate anchor tag for 'create a new directory' link
  153. *
  154. * @param string $title link title
  155. * @param string $path absolute path
  156. * @return string
  157. */
  158. public function linkCreateDirectory($title, $path) {
  159. $output = $this->link($title, array('controller' => 'filemanager', 'action' => 'new'), $path);
  160. return $output;
  161. }
  162. /**
  163. * Get icon from mime type
  164. *
  165. * @param string $mimeType mine type
  166. * @return string
  167. */
  168. public function mimeTypeToImage($mimeType) {
  169. $mime = explode('/', $mimeType);
  170. $mime = $mime['0'];
  171. $mimeToImages = array();
  172. $mimeToImages['text'] = 'page_white.png';
  173. if (isset($mimeToImages[$mime])) {
  174. $output = $mimeToImages[$mime];
  175. } else {
  176. $output = 'page_white.png';
  177. }
  178. return $output;
  179. }
  180. /**
  181. * Checks if searched location is under any of the paths
  182. *
  183. * @param array $paths
  184. * @param string $search
  185. * @return boolean
  186. */
  187. public function inPath($paths, $search) {
  188. foreach ($paths AS $path) {
  189. if (strpos($search, $path) !== false) {
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. }
  196. ?>