PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/mul14/pyrocms
PHP | 355 lines | 231 code | 50 blank | 74 comment | 12 complexity | b955cfe9b9a282014e6ebb492baaa838 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. if ($error = $this->_check_dir())
  57. {
  58. $this->template->error = $this->_check_dir();
  59. }
  60. $this->template
  61. ->title($this->module_details['name'])
  62. ->set('file_folders', $file_folders)
  63. ->build('admin/layouts/index');
  64. }
  65. // ------------------------------------------------------------------------
  66. /**
  67. * Upload
  68. *
  69. * Upload a file to the destination folder
  70. *
  71. * @params int The folder id
  72. */
  73. public function upload($id = '')
  74. {
  75. $this->template->set_layout('modal', 'admin');
  76. $file->name = '';
  77. $file->description = '';
  78. $file->type = '';
  79. $file->folder_id = $id;
  80. $data->file =& $file;
  81. $data->folders = $this->file_folders_m->get_folders();
  82. $data->types = array('a' => lang('files.a'), 'v' => lang('files.v'), 'd' => lang('files.d'), 'i' => lang('files.i'), 'o' => lang('files.o'));
  83. $this->load->library('form_validation');
  84. $rules = array(
  85. array(
  86. 'field' => 'name',
  87. 'label' => 'lang:files.folders.name',
  88. 'rules' => 'trim|required'
  89. ),
  90. array(
  91. 'field' => 'description',
  92. 'label' => 'lang:files.description',
  93. 'rules' => ''
  94. ),
  95. array(
  96. 'field' => 'folder_id',
  97. 'label' => 'lang:files.labels.parent',
  98. 'rules' => ''
  99. ),
  100. array(
  101. 'field' => 'type',
  102. 'label' => 'lang:files.type',
  103. 'rules' => 'trim|required'
  104. ),
  105. );
  106. $this->form_validation->set_rules($rules);
  107. if ($this->form_validation->run())
  108. {
  109. // Setup upload config
  110. $type = $this->input->post('type');
  111. $allowed = $this->config->item('files_allowed_file_ext');
  112. $config['upload_path'] = $this->_path;
  113. $config['allowed_types'] = $allowed[$type];
  114. $this->load->library('upload', $config);
  115. if (!$this->upload->do_upload('userfile'))
  116. {
  117. $data->messages['notice'] = $this->upload->display_errors();
  118. }
  119. else
  120. {
  121. $img = array('upload_data' => $this->upload->data());
  122. $this->file_m->insert(array(
  123. 'folder_id' => $this->input->post('folder_id'),
  124. 'user_id' => $this->user->id,
  125. 'type' => $type,
  126. 'name' => $this->input->post('name'),
  127. 'description' => $this->input->post('description'),
  128. 'filename' => $img['upload_data']['file_name'],
  129. 'extension' => $img['upload_data']['file_ext'],
  130. 'mimetype' => $img['upload_data']['file_type'],
  131. 'filesize' => $img['upload_data']['file_size'],
  132. 'width' => (int) $img['upload_data']['image_width'],
  133. 'height' => (int) $img['upload_data']['image_height'],
  134. 'date_added' => time(),
  135. ));
  136. $data->messages['success'] = lang('files.success');
  137. #redirect('admin/files');
  138. }
  139. }
  140. $this->template->build('admin/files/upload', $data);
  141. }
  142. // ------------------------------------------------------------------------
  143. /**
  144. * Edit Uploaded file
  145. *
  146. */
  147. public function edit($id = '')
  148. {
  149. $id OR redirect('admin/files/upload');
  150. $this->template->set_layout('modal', 'admin');
  151. $data->error = '';
  152. $file = $this->file_m->get($id);
  153. $data->file =& $file;
  154. $data->folders = $this->file_folders_m->get_folders();
  155. $data->types = array('a' => lang('files.a'), 'v' => lang('files.v'), 'd' => lang('files.d'), 'i' => lang('files.i'), 'o' => lang('files.o'));
  156. $this->load->library('form_validation');
  157. $rules = array(
  158. array(
  159. 'field' => 'name',
  160. 'label' => 'lang:files.folders.name',
  161. 'rules' => 'trim|required'
  162. ),
  163. array(
  164. 'field' => 'description',
  165. 'label' => 'lang:files.description',
  166. 'rules' => ''
  167. ),
  168. array(
  169. 'field' => 'folder_id',
  170. 'label' => 'lang:files.labels.parent',
  171. 'rules' => ''
  172. ),
  173. array(
  174. 'field' => 'type',
  175. 'label' => 'lang:files.type',
  176. 'rules' => 'trim|required'
  177. ),
  178. );
  179. $this->form_validation->set_rules($rules);
  180. if ($this->form_validation->run())
  181. {
  182. $filename = $file->filename;
  183. $type = $this->input->post('type');
  184. if ( ! empty($_FILES['userfile']['name']))
  185. {
  186. //we are uploading a file
  187. $this->file_m->delete_file($id); //remove the original image
  188. // Setup upload config
  189. $allowed = $this->config->item('files_allowed_file_ext');
  190. $config['upload_path'] = $this->_path;
  191. $config['allowed_types'] = $allowed[$type];
  192. $this->load->library('upload', $config);
  193. if (!$this->upload->do_upload('userfile'))
  194. {
  195. $data->messages['notice'] = $this->upload->display_errors();
  196. }
  197. else
  198. {
  199. $img = array('upload_data' => $this->upload->data());
  200. $filename = $img['upload_data']['file_name'];
  201. $this->file_m->update($id, array(
  202. 'folder_id' => $this->input->post('folder_id'),
  203. 'user_id' => $this->user->id,
  204. 'type' => $type,
  205. 'name' => $this->input->post('name'),
  206. 'description' => $this->input->post('description'),
  207. 'filename' => $img['upload_data']['file_name'],
  208. 'extension' => $img['upload_data']['file_ext'],
  209. 'mimetype' => $img['upload_data']['file_type'],
  210. 'filesize' => $img['upload_data']['file_size'],
  211. 'width' => $img['upload_data']['image_width'],
  212. 'height' => $img['upload_data']['image_height'],
  213. ));
  214. $data->messages['success'] = lang('files.success');
  215. }
  216. }
  217. else
  218. {
  219. $this->file_m->update($id, array(
  220. 'folder_id' => $this->input->post('folder_id'),
  221. 'user_id' => $this->user->id,
  222. 'type' => $type,
  223. 'name' => $this->input->post('name'),
  224. 'description' => $this->input->post('description'),
  225. ));
  226. $data->messages['success'] = lang('files.success');
  227. }
  228. }
  229. $this->template->build('admin/files/edit', $data);
  230. }
  231. // ------------------------------------------------------------------------
  232. /**
  233. * Delete a file
  234. *
  235. * @params int The file id
  236. */
  237. public function delete($id = '')
  238. {
  239. // Delete one
  240. $ids = ($id) ? array($id) : $this->input->post('action_to');
  241. // Go through the array of ids to delete
  242. if ( ! empty($ids))
  243. {
  244. foreach ($ids as $id)
  245. {
  246. if ($this->file_m->exists($id))
  247. {
  248. $this->file_m->delete($id);
  249. }
  250. }
  251. $this->session->set_flashdata('success', lang('files.delete.success'));
  252. }
  253. else
  254. {
  255. show_error(lang('files.not_exists'));
  256. }
  257. redirect('admin/files');
  258. }
  259. /**
  260. * Helper method to determine what to do with selected items from form post
  261. * @access public
  262. * @return void
  263. */
  264. public function action()
  265. {
  266. switch($this->input->post('btnAction'))
  267. {
  268. case 'delete':
  269. $this->delete();
  270. break;
  271. default:
  272. redirect('admin/files');
  273. break;
  274. }
  275. }
  276. // ------------------------------------------------------------------------
  277. /**
  278. * Validate our upload directory.
  279. */
  280. private function _check_dir()
  281. {
  282. if (is_dir($this->_path) && is_really_writable($this->_path))
  283. {
  284. return TRUE;
  285. }
  286. elseif (!is_dir($this->_path))
  287. {
  288. if (!@mkdir($this->_path))
  289. {
  290. $this->session->set_flashdata('notice', lang('files.folders.mkdir'));
  291. return FALSE;
  292. }
  293. }
  294. else
  295. {
  296. if (!chmod($this->_path, 0777))
  297. {
  298. $this->session->set_flashdata('notice', lang('files.folders.chmod'));
  299. return FALSE;
  300. }
  301. }
  302. }
  303. }
  304. /* End of file admin.php */
  305. /* Location: ./system/pyrocms/modules/files/controllers/admin.php */