PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/Controller/FilemanagerController.php

https://github.com/okatsuralau/croogo
PHP | 362 lines | 195 code | 49 blank | 118 comment | 45 complexity | 7534e9d4a401c6088c62e8d6ae134379 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. App::uses('File', 'Utility');
  3. /**
  4. * Filemanager Controller
  5. *
  6. * PHP version 5
  7. *
  8. * @category Controller
  9. * @package Croogo
  10. * @version 1.0
  11. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  12. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  13. * @link http://www.croogo.org
  14. */
  15. class FilemanagerController extends AppController {
  16. /**
  17. * Controller name
  18. *
  19. * @var string
  20. * @access public
  21. */
  22. public $name = 'Filemanager';
  23. /**
  24. * Models used by the Controller
  25. *
  26. * @var array
  27. * @access public
  28. */
  29. public $uses = array('Setting', 'User');
  30. /**
  31. * Helpers used by the Controller
  32. *
  33. * @var array
  34. * @access public
  35. */
  36. public $helpers = array('Html', 'Form', 'Filemanager');
  37. /**
  38. * Deletable Paths
  39. *
  40. * @var array
  41. * @access public
  42. */
  43. public $deletablePaths = array();
  44. /**
  45. * beforeFilter
  46. *
  47. * @return void
  48. * @access public
  49. */
  50. public function beforeFilter() {
  51. parent::beforeFilter();
  52. $this->deletablePaths = array(
  53. APP . 'View' . DS . 'Themed' . DS,
  54. WWW_ROOT,
  55. );
  56. $this->set('deletablePaths', $this->deletablePaths);
  57. }
  58. /**
  59. * Checks wether given $path is editable.
  60. * A file is editable when it resides under the APP directory
  61. *
  62. * @param $path string
  63. * @return boolean true if file is editable
  64. */
  65. protected function _isEditable($path) {
  66. $path = realpath($path);
  67. $regex = '/^' . preg_quote(realpath(APP), '/') . '/';
  68. return preg_match($regex, $path) > 0;
  69. }
  70. /**
  71. * Checks wether given $path is editable.
  72. * A file is deleteable when it resides under directories registered in
  73. * FilemanagerController::deletablePaths
  74. *
  75. * @param $path string
  76. * @return boolean true when file is deletable
  77. */
  78. protected function _isDeletable($path) {
  79. $path = realpath($path);
  80. $regex = array();
  81. for ($i = 0, $ii = count($this->deletablePaths); $i < $ii; $i++) {
  82. $regex[] = '(^' . preg_quote(realpath($this->deletablePaths[$i]), '/') . ')';
  83. }
  84. $regex = '/' . join($regex, '|') . '/';
  85. return preg_match($regex, $path) > 0;
  86. }
  87. /**
  88. * Admin index
  89. *
  90. * @return void
  91. * @access public
  92. */
  93. public function admin_index() {
  94. $this->redirect(array('action' => 'browse'));
  95. die();
  96. }
  97. /**
  98. * Admin browse
  99. *
  100. * @return void
  101. * @access public
  102. */
  103. public function admin_browse() {
  104. $this->folder = new Folder;
  105. if (isset($this->request->query['path'])) {
  106. $path = $this->request->query['path'];
  107. } else {
  108. $path = APP;
  109. }
  110. $this->set('title_for_layout', __('File Manager'));
  111. $path = realpath($path) . DS;
  112. $regex = '/^' . preg_quote(realpath(APP), '/') . '/';
  113. if (preg_match($regex, $path) == false) {
  114. $this->Session->setFlash(__(sprintf('Path %s is restricted', $path)));
  115. $path = APP;
  116. }
  117. $blacklist = array('.git', '.svn', '.CVS');
  118. $regex = '/(' . preg_quote(implode('|', $blacklist), '.') . ')/';
  119. if (in_array(basename($path), $blacklist) || preg_match($regex, $path)) {
  120. $this->Session->setFlash(__(sprintf('Path %s is restricted', $path)));
  121. $path = dirname($path);
  122. }
  123. $this->folder->path = $path;
  124. $content = $this->folder->read();
  125. $this->set(compact('content'));
  126. $this->set('path', $path);
  127. }
  128. /**
  129. * Admin editfile
  130. *
  131. * @return void
  132. * @access public
  133. */
  134. public function admin_editfile() {
  135. if (isset($this->request->query['path'])) {
  136. $path = $this->request->query['path'];
  137. $absolutefilepath = $path;
  138. } else {
  139. $this->redirect(array('controller' => 'filemanager', 'action' => 'browse'));
  140. }
  141. if (!$this->_isEditable($path)) {
  142. $this->Session->setFlash(__(sprintf('Path %s is restricted', $path), true));
  143. $this->redirect(array('controller' => 'filemanager', 'action' => 'browse'));
  144. }
  145. $this->set('title_for_layout', sprintf(__('Edit file: %s'), $path));
  146. $pathE = explode(DS, $path);
  147. $n = count($pathE) - 1;
  148. $filename = $pathE[$n];
  149. unset($pathE[$n]);
  150. $path = implode(DS, $pathE);
  151. $this->file = new File($absolutefilepath, true);
  152. if (!empty($this->request->data) ) {
  153. if ($this->file->write($this->request->data['Filemanager']['content'])) {
  154. $this->Session->setFlash(__('File saved successfully'), 'default', array('class' => 'success'));
  155. }
  156. }
  157. $content = $this->file->read();
  158. $this->set(compact('content', 'path', 'absolutefilepath'));
  159. }
  160. /**
  161. * Admin upload
  162. *
  163. * @return void
  164. * @access public
  165. */
  166. public function admin_upload() {
  167. $this->set('title_for_layout', __('Upload'));
  168. if (isset($this->request->query['path'])) {
  169. $path = $this->request->query['path'];
  170. } else {
  171. $path = APP;
  172. }
  173. $this->set(compact('path'));
  174. if (isset($this->request->data['Filemanager']['file']['tmp_name']) &&
  175. is_uploaded_file($this->request->data['Filemanager']['file']['tmp_name'])) {
  176. $destination = $path . $this->request->data['Filemanager']['file']['name'];
  177. move_uploaded_file($this->request->data['Filemanager']['file']['tmp_name'], $destination);
  178. $this->Session->setFlash(__('File uploaded successfully.'), 'default', array('class' => 'success'));
  179. $redirectUrl = Router::url(array('controller' => 'filemanager', 'action' => 'browse'), true) . '?path=' . urlencode($path);
  180. $this->redirect($redirectUrl);
  181. }
  182. }
  183. /**
  184. * Admin Delete File
  185. *
  186. * @return void
  187. * @access public
  188. */
  189. public function admin_delete_file() {
  190. if (!empty($this->request->data['path'])) {
  191. $path = $this->request->data['path'];
  192. } else {
  193. $this->redirect(array('controller' => 'filemanager', 'action' => 'browse'));
  194. }
  195. if (!$this->_isDeletable($path)) {
  196. $this->Session->setFlash(__(sprintf('Path %s is restricted', $path), true));
  197. $this->redirect(array('controller' => 'filemanager', 'action' => 'browse'));
  198. }
  199. if (file_exists($path) && unlink($path)) {
  200. $this->Session->setFlash(__('File deleted'), 'default', array('class' => 'success'));
  201. } else {
  202. $this->Session->setFlash(__('An error occured'), 'default', array('class' => 'error'));
  203. }
  204. if (isset($_SERVER['HTTP_REFERER'])) {
  205. $this->redirect($_SERVER['HTTP_REFERER']);
  206. } else {
  207. $this->redirect(array('controller' => 'filemanager', 'action' => 'index'));
  208. }
  209. exit();
  210. }
  211. /**
  212. * Admin Delete Directory
  213. *
  214. * @return void
  215. * @access public
  216. */
  217. public function admin_delete_directory() {
  218. if (!empty($this->request->data['path'])) {
  219. $path = $this->request->data['path'];
  220. } else {
  221. $this->redirect(array('controller' => 'filemanager', 'action' => 'browse'));
  222. }
  223. if (is_dir($path) && rmdir($path)) {
  224. $this->Session->setFlash(__('Directory deleted'), 'default', array('class' => 'success'));
  225. } else {
  226. $this->Session->setFlash(__('An error occured'), 'default', array('class' => 'error'));
  227. }
  228. if (isset($_SERVER['HTTP_REFERER'])) {
  229. $this->redirect($_SERVER['HTTP_REFERER']);
  230. } else {
  231. $this->redirect(array('controller' => 'filemanager', 'action' => 'index'));
  232. }
  233. exit();
  234. }
  235. /**
  236. * Admin Rename
  237. *
  238. * @return void
  239. * @access public
  240. */
  241. public function admin_rename() {
  242. if (isset($this->request->query['path'])) {
  243. $path = $this->request->query['path'];
  244. } else {
  245. $this->redirect(array('controller' => 'filemanager', 'action' => 'browse'));
  246. }
  247. if (isset($this->request->query['newpath'])) {
  248. // rename here
  249. }
  250. if (isset($_SERVER['HTTP_REFERER'])) {
  251. $this->redirect($_SERVER['HTTP_REFERER']);
  252. } else {
  253. $this->redirect(array('controller' => 'filemanager', 'action' => 'index'));
  254. }
  255. }
  256. /**
  257. * Admin Create Directory
  258. *
  259. * @return void
  260. * @access public
  261. */
  262. public function admin_create_directory() {
  263. $this->set('title_for_layout', __('New Directory'));
  264. if (isset($this->request->query['path'])) {
  265. $path = $this->request->query['path'];
  266. } else {
  267. $this->redirect(array('controller' => 'filemanager', 'action' => 'browse'));
  268. }
  269. if (!empty($this->request->data)) {
  270. $this->folder = new Folder;
  271. if ($this->folder->create($path . $this->request->data['Filemanager']['name'])) {
  272. $this->Session->setFlash(__('Directory created successfully.'), 'default', array('class' => 'success'));
  273. $redirectUrl = Router::url(array('controller' => 'filemanager', 'action' => 'browse'), true) . '?path=' . urlencode($path);
  274. $this->redirect($redirectUrl);
  275. } else {
  276. $this->Session->setFlash(__('An error occured'), 'default', array('class' => 'error'));
  277. }
  278. }
  279. $this->set(compact('path'));
  280. }
  281. /**
  282. * Admin Create File
  283. *
  284. * @return void
  285. * @access public
  286. */
  287. public function admin_create_file() {
  288. $this->set('title_for_layout', __('New File'));
  289. if (isset($this->request->query['path'])) {
  290. $path = $this->request->query['path'];
  291. } else {
  292. $this->redirect(array('controller' => 'filemanager', 'action' => 'browse'));
  293. }
  294. if (!empty($this->request->data)) {
  295. if (touch($path . $this->request->data['Filemanager']['name'])) {
  296. $this->Session->setFlash(__('File created successfully.'), 'default', array('class' => 'success'));
  297. $redirectUrl = Router::url(array('controller' => 'filemanager', 'action' => 'browse'), true) . '?path=' . urlencode($path);
  298. $this->redirect($redirectUrl);
  299. } else {
  300. $this->Session->setFlash(__('An error occured'), 'default', array('class' => 'error'));
  301. }
  302. }
  303. $this->set(compact('path'));
  304. }
  305. /**
  306. * Admin chmod
  307. *
  308. * @return void
  309. * @access public
  310. */
  311. public function admin_chmod() {
  312. }
  313. }