PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/FileManager/Controller/FileManagerController.php

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