PageRenderTime 194ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/system/pyrocms/modules/files/controllers/admin.php

https://github.com/uitto/pyrocms
PHP | 422 lines | 286 code | 58 blank | 78 comment | 20 complexity | d09f912456fed55728c1a8c485e57e74 MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * PyroCMS
  4. *
  5. * An open source CMS based on CodeIgniter
  6. *
  7. * @package PyroCMS
  8. * @author PyroCMS Dev Team
  9. * @license Apache License v2.0
  10. * @link http://pyrocms.com
  11. * @since Version 1.0
  12. * @filesource
  13. */
  14. /**
  15. * PyroCMS file Admin Controller
  16. *
  17. * Provides an admin for the file module.
  18. *
  19. * @author Dan Horrigan <dan@dhorrigan.com>
  20. * @author Eric Barnes <eric@pyrocms.com>
  21. * @package PyroCMS
  22. * @subpackage file
  23. */
  24. class Admin extends Admin_Controller {
  25. private $_folders = array();
  26. private $_path = '';
  27. /**
  28. * Constructor
  29. *
  30. * Loads dependencies.
  31. *
  32. * @access public
  33. * @return void
  34. */
  35. public function __construct()
  36. {
  37. parent::Admin_Controller();
  38. $this->load->models(array('file_m', 'file_folders_m'));
  39. $this->lang->load('files');
  40. $this->config->load('files');
  41. $this->template->set_partial('shortcuts', 'admin/partials/shortcuts');
  42. $this->template->set_partial('nav', 'admin/partials/nav');
  43. $this->_path = FCPATH . '/' . $this->config->item('files_folder') . '/';
  44. }
  45. /**
  46. * Index
  47. *
  48. * Shows the default
  49. *
  50. * @access public
  51. * @return void
  52. */
  53. public function index()
  54. {
  55. $file_folders = $this->file_folders_m->order_by('name')->get_many_by(array('parent_id' => '0'));
  56. $folder_options = $this->file_folders_m->dropdown('id', 'name');
  57. if ($error = $this->_check_dir())
  58. {
  59. $this->template->error = $this->_check_dir();
  60. }
  61. $this->template
  62. ->append_metadata( css('jquery.fileupload-ui.css', 'files') )
  63. ->append_metadata( js('jquery.fileupload.js', 'files') )
  64. ->append_metadata( js('jquery.fileupload-ui.js', 'files') )
  65. ->append_metadata( js('jquery/jquery.cookie.js') )
  66. ->append_metadata( css('files.css', 'files') )
  67. ->title($this->module_details['name'])
  68. ->set('folder_options', $folder_options)
  69. ->set('file_folders', $file_folders)
  70. ->build('admin/layouts/index');
  71. }
  72. // ------------------------------------------------------------------------
  73. /**
  74. * Upload
  75. *
  76. * Upload a file to the destination folder
  77. *
  78. * @params int The folder id
  79. */
  80. public function upload($id = '')
  81. {
  82. $this->template->set_layout('modal', 'admin');
  83. $file->name = '';
  84. $file->description = '';
  85. $file->type = '';
  86. $file->folder_id = $id;
  87. $data->file =& $file;
  88. $data->folders = $this->file_folders_m->get_folders();
  89. $data->types = array('a' => lang('files.a'), 'v' => lang('files.v'), 'd' => lang('files.d'), 'i' => lang('files.i'), 'o' => lang('files.o'));
  90. $this->load->library('form_validation');
  91. $rules = array(
  92. array(
  93. 'field' => 'userfile',
  94. 'label' => 'lang:files.file',
  95. 'rules' => 'callback__check_ext'
  96. ),
  97. array(
  98. 'field' => 'name',
  99. 'label' => 'lang:files.folders.name',
  100. 'rules' => 'trim|required'
  101. ),
  102. array(
  103. 'field' => 'description',
  104. 'label' => 'lang:files.description',
  105. 'rules' => ''
  106. ),
  107. array(
  108. 'field' => 'folder_id',
  109. 'label' => 'lang:files.labels.parent',
  110. 'rules' => ''
  111. )
  112. );
  113. $this->form_validation->set_rules($rules);
  114. if ($this->form_validation->run())
  115. {
  116. // Setup upload config
  117. $allowed = $this->config->item('files_allowed_file_ext');
  118. $config['upload_path'] = $this->_path;
  119. $config['allowed_types'] = '';
  120. while ($str = current($allowed))
  121. {
  122. if (preg_match('/'.strtolower($this->ext).'/', $str))
  123. {
  124. $config['allowed_types'] = $allowed[key($allowed)];
  125. break;
  126. }
  127. next($allowed);
  128. }
  129. $this->load->library('upload', $config);
  130. if (!$this->upload->do_upload('userfile'))
  131. {
  132. $data->messages['notice'] = $this->upload->display_errors();
  133. }
  134. else
  135. {
  136. $img = array('upload_data' => $this->upload->data());
  137. $this->file_m->insert(array(
  138. 'folder_id' => $this->input->post('folder_id'),
  139. 'user_id' => $this->user->id,
  140. 'type' => key($allowed),
  141. 'name' => $this->input->post('name'),
  142. 'description' => $this->input->post('description') ? $this->input->post('description') : '',
  143. 'filename' => $img['upload_data']['file_name'],
  144. 'extension' => $img['upload_data']['file_ext'],
  145. 'mimetype' => $img['upload_data']['file_type'],
  146. 'filesize' => $img['upload_data']['file_size'],
  147. 'width' => (int) $img['upload_data']['image_width'],
  148. 'height' => (int) $img['upload_data']['image_height'],
  149. 'date_added' => time(),
  150. ));
  151. $data->messages['success'] = lang('files.success');
  152. #redirect('admin/files');
  153. $json = array(
  154. 'name' => $img['upload_data']['file_name'],
  155. 'type' => $img['upload_data']['file_type'],
  156. 'size' => $img['upload_data']['file_size']
  157. );
  158. if($this->input->is_ajax_request())
  159. {
  160. echo json_encode($json);
  161. return;
  162. }
  163. }
  164. }
  165. $this->template->build('admin/files/upload', $data);
  166. }
  167. // ------------------------------------------------------------------------
  168. /**
  169. * Edit Uploaded file
  170. *
  171. */
  172. public function edit($id = '')
  173. {
  174. $id OR redirect('admin/files/upload');
  175. $this->template->set_layout('modal', 'admin');
  176. $data->error = '';
  177. $file = $this->file_m->get($id);
  178. $data->file =& $file;
  179. $data->folders = $this->file_folders_m->get_folders();
  180. $data->types = array('a' => lang('files.a'), 'v' => lang('files.v'), 'd' => lang('files.d'), 'i' => lang('files.i'), 'o' => lang('files.o'));
  181. $this->load->library('form_validation');
  182. $rules = array(
  183. array(
  184. 'field' => 'name',
  185. 'label' => 'lang:files.folders.name',
  186. 'rules' => 'trim|required'
  187. ),
  188. array(
  189. 'field' => 'description',
  190. 'label' => 'lang:files.description',
  191. 'rules' => ''
  192. ),
  193. array(
  194. 'field' => 'folder_id',
  195. 'label' => 'lang:files.labels.parent',
  196. 'rules' => ''
  197. )
  198. );
  199. $this->form_validation->set_rules($rules);
  200. if ($this->form_validation->run())
  201. {
  202. $filename = $file->filename;
  203. if ( ! empty($_FILES['userfile']['name']))
  204. {
  205. //we are uploading a file
  206. $this->file_m->delete_file($id); //remove the original image
  207. // Setup upload config
  208. $allowed = $this->config->item('files_allowed_file_ext');
  209. $ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
  210. $config['upload_path'] = $this->_path;
  211. $config['allowed_types'] = '';
  212. while ($str = current($allowed))
  213. {
  214. if (preg_match('/'.$ext.'/', $str))
  215. {
  216. $config['allowed_types'] = $allowed[key($allowed)];
  217. break;
  218. }
  219. next($allowed);
  220. }
  221. $this->load->library('upload', $config);
  222. if (empty($ext))
  223. {
  224. $data->messages['notice'] = lang('files.file.no_extension');
  225. }
  226. elseif (!$this->upload->do_upload('userfile'))
  227. {
  228. $data->messages['notice'] = $this->upload->display_errors();
  229. }
  230. else
  231. {
  232. $img = array('upload_data' => $this->upload->data());
  233. $filename = $img['upload_data']['file_name'];
  234. $this->file_m->update($id, array(
  235. 'folder_id' => $this->input->post('folder_id'),
  236. 'user_id' => $this->user->id,
  237. 'type' => key($allowed),
  238. 'name' => $this->input->post('name'),
  239. 'description' => $this->input->post('description'),
  240. 'filename' => $img['upload_data']['file_name'],
  241. 'extension' => $img['upload_data']['file_ext'],
  242. 'mimetype' => $img['upload_data']['file_type'],
  243. 'filesize' => $img['upload_data']['file_size'],
  244. 'width' => (int) $img['upload_data']['image_width'],
  245. 'height' => (int) $img['upload_data']['image_height'],
  246. ));
  247. $data->messages['success'] = lang('files.success');
  248. }
  249. }
  250. else
  251. {
  252. $this->file_m->update($id, array(
  253. 'folder_id' => $this->input->post('folder_id'),
  254. 'user_id' => $this->user->id,
  255. 'name' => $this->input->post('name'),
  256. 'description' => $this->input->post('description'),
  257. ));
  258. $data->messages['success'] = lang('files.success');
  259. }
  260. }
  261. $this->template->build('admin/files/edit', $data);
  262. }
  263. // ------------------------------------------------------------------------
  264. /**
  265. * Delete a file
  266. *
  267. * @params int The file id
  268. */
  269. public function delete($id = '')
  270. {
  271. // Delete one
  272. $ids = ($id) ? array($id) : $this->input->post('action_to');
  273. // Go through the array of ids to delete
  274. if ( ! empty($ids))
  275. {
  276. foreach ($ids as $id)
  277. {
  278. if ($this->file_m->exists($id))
  279. {
  280. $file = $this->file_m->get($id);
  281. $folder = $this->file_folders_m->get($file->folder_id);
  282. $this->file_m->delete($id);
  283. }
  284. }
  285. $this->session->set_flashdata('success', lang('files.delete.success'));
  286. }
  287. else
  288. {
  289. show_error(lang('files.not_exists'));
  290. }
  291. isset($folder) ? redirect('admin/files#' . $folder->slug) : redirect('admin/files');
  292. }
  293. /**
  294. * Helper method to determine what to do with selected items from form post
  295. * @access public
  296. * @return void
  297. */
  298. public function action()
  299. {
  300. switch($this->input->post('btnAction'))
  301. {
  302. case 'delete':
  303. $this->delete();
  304. break;
  305. default:
  306. redirect('admin/files');
  307. break;
  308. }
  309. }
  310. // ------------------------------------------------------------------------
  311. /**
  312. * Validate our upload directory.
  313. */
  314. private function _check_dir()
  315. {
  316. if (is_dir($this->_path) && is_really_writable($this->_path))
  317. {
  318. return TRUE;
  319. }
  320. elseif (!is_dir($this->_path))
  321. {
  322. if (!@mkdir($this->_path))
  323. {
  324. $this->session->set_flashdata('notice', lang('files.folders.mkdir'));
  325. return FALSE;
  326. }
  327. }
  328. else
  329. {
  330. if (!chmod($this->_path, 0777))
  331. {
  332. $this->session->set_flashdata('notice', lang('files.folders.chmod'));
  333. return FALSE;
  334. }
  335. }
  336. }
  337. // ------------------------------------------------------------------------
  338. /**
  339. * Validate upload file name and extension.
  340. */
  341. function _check_ext()
  342. {
  343. if(!empty($_FILES['userfile']['name']))
  344. {
  345. $this->ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
  346. if($this->ext == '')
  347. {
  348. $this->form_validation->set_message('_check_ext', lang('files.file.no_extension'));
  349. return FALSE;
  350. }
  351. else
  352. {
  353. return TRUE;
  354. }
  355. }
  356. else
  357. {
  358. $this->form_validation->set_message('_check_ext', lang('files.file.no_upload'));
  359. return FALSE;
  360. }
  361. }
  362. }
  363. /* End of file admin.php */
  364. /* Location: ./system/pyrocms/modules/files/controllers/admin.php */