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

https://github.com/abir/pyrocms · PHP · 277 lines · 152 code · 33 blank · 92 comment · 17 complexity · cbf561734f6724f6877cab8cb539d97f MD5 · raw file

  1. <?php
  2. /**
  3. * Modules controller, lists all installed modules
  4. *
  5. * @author Phil Sturgeon - PyroCMS Development Team
  6. * @package PyroCMS
  7. * @subpackage Core modules
  8. * @category Modules
  9. * @since v1.0
  10. */
  11. class Admin extends Admin_Controller
  12. {
  13. /**
  14. * Constructor method
  15. *
  16. * @access public
  17. * @return void
  18. */
  19. public function __construct()
  20. {
  21. parent::Admin_Controller();
  22. $this->lang->load('modules');
  23. $this->template->set_partial('shortcuts', 'admin/partials/shortcuts');
  24. // Check the referrer
  25. parse_url($this->input->server('HTTP_REFERER'), PHP_URL_HOST) == parse_url(base_url(), PHP_URL_HOST) or show_error('Invalid Referrer');
  26. }
  27. /**
  28. * Index method
  29. * @access public
  30. * @return void
  31. */
  32. public function index()
  33. {
  34. $this->module_m->import_unknown();
  35. $this->template
  36. ->title($this->module_details['name'])
  37. ->set('all_modules', $this->module_m->get_all(NULL, TRUE))
  38. ->build('admin/index');
  39. }
  40. /**
  41. * Upload
  42. *
  43. * Uploads an addon module
  44. *
  45. * @access public
  46. * @return void
  47. */
  48. public function upload()
  49. {
  50. if ($this->input->post('btnAction') == 'upload')
  51. {
  52. $config['upload_path'] = FCPATH.'uploads/';
  53. $config['allowed_types'] = 'zip';
  54. $config['max_size'] = '2048';
  55. $config['overwrite'] = TRUE;
  56. $this->load->library('upload', $config);
  57. if ($this->upload->do_upload())
  58. {
  59. $upload_data = $this->upload->data();
  60. // Check if we already have a dir with same name
  61. if ($this->module_m->exists($upload_data['raw_name']))
  62. {
  63. $this->session->set_flashdata('error', sprintf(lang('modules.already_exists_error'), $upload_data['raw_name']));
  64. }
  65. else
  66. {
  67. // Now try to unzip
  68. $this->load->library('unzip');
  69. $this->unzip->allow(array('xml', 'html', 'css', 'js', 'png', 'gif', 'jpeg', 'jpg', 'swf', 'ico', 'php'));
  70. // Try and extract
  71. if ($this->unzip->extract($upload_data['full_path'], ADDONPATH . 'modules/'))
  72. {
  73. if ($this->module_m->install($upload_data['raw_name']))
  74. {
  75. $this->session->set_flashdata('success', sprintf(lang('modules.install_success'), $upload_data['raw_name']));
  76. }
  77. else
  78. {
  79. }
  80. }
  81. else
  82. {
  83. $this->session->set_flashdata('error', $this->unzip->error_string());
  84. }
  85. }
  86. // Delete uploaded file
  87. @unlink($upload_data['full_path']);
  88. }
  89. else
  90. {
  91. $this->session->set_flashdata('error', $this->upload->display_errors());
  92. }
  93. redirect('admin/modules');
  94. }
  95. $this->template
  96. ->title($this->module_details['name'], lang('modules.upload_title'))
  97. ->build('admin/upload', $this->data);
  98. }
  99. /**
  100. * Uninstall
  101. *
  102. * Uninstalls an addon module
  103. *
  104. * @param string $slug The slug of the module to uninstall
  105. * @access public
  106. * @return void
  107. */
  108. public function uninstall($slug = '')
  109. {
  110. // Don't allow user to delete the entire module folder
  111. if ($slug == '/' OR $slug == '*' OR empty($slug))
  112. {
  113. show_error(lang('modules.module_not_specified'));
  114. }
  115. if ($this->module_m->uninstall($slug))
  116. {
  117. $this->session->set_flashdata('success', sprintf(lang('modules.uninstall_success'), $slug));
  118. $path = ADDONPATH . 'modules/' . $slug;
  119. if (!$this->_delete_recursive($path))
  120. {
  121. $this->session->set_flashdata('notice', sprintf(lang('modules.manually_remove'), $path));
  122. }
  123. redirect('admin/modules');
  124. }
  125. $this->session->set_flashdata('error', sprintf(lang('modules.uninstall_error'), $slug));
  126. redirect('admin/modules');
  127. }
  128. /**
  129. * Enable
  130. *
  131. * Enables an addon module
  132. *
  133. * @param string $slug The slug of the module to enable
  134. * @access public
  135. * @return void
  136. */
  137. public function install($slug)
  138. {
  139. if ($this->module_m->install($slug))
  140. {
  141. // Clear the module cache
  142. $this->cache->delete_all('module_m');
  143. $this->session->set_flashdata('success', sprintf(lang('modules.install_success'), $slug));
  144. }
  145. else
  146. {
  147. $this->session->set_flashdata('error', sprintf(lang('modules.install_error'), $slug));
  148. }
  149. redirect('admin/modules');
  150. }
  151. /**
  152. * Enable
  153. *
  154. * Enables an addon module
  155. *
  156. * @param string $slug The slug of the module to enable
  157. * @access public
  158. * @return void
  159. */
  160. public function enable($slug)
  161. {
  162. if ($this->module_m->enable($slug))
  163. {
  164. // Clear the module cache
  165. $this->cache->delete_all('module_m');
  166. $this->session->set_flashdata('success', sprintf(lang('modules.enable_success'), $slug));
  167. }
  168. else
  169. {
  170. $this->session->set_flashdata('error', sprintf(lang('modules.enable_error'), $slug));
  171. }
  172. redirect('admin/modules');
  173. }
  174. /**
  175. * Disable
  176. *
  177. * Disables an addon module
  178. *
  179. * @param string $slug The slug of the module to disable
  180. * @access public
  181. * @return void
  182. */
  183. public function disable($slug)
  184. {
  185. if ($this->module_m->disable($slug))
  186. {
  187. // Clear the module cache
  188. $this->cache->delete_all('module_m');
  189. $this->session->set_flashdata('success', sprintf(lang('modules.disable_success'), $slug));
  190. }
  191. else
  192. {
  193. $this->session->set_flashdata('error', sprintf(lang('modules.disable_error'), $slug));
  194. }
  195. redirect('admin/modules');
  196. }
  197. /**
  198. * Upgrade
  199. *
  200. * Upgrade an addon module
  201. *
  202. * @param string $slug The slug of the module to disable
  203. * @access public
  204. * @return void
  205. */
  206. public function upgrade($slug)
  207. {
  208. // If upgrade succeeded
  209. if ($this->module_m->upgrade($slug))
  210. {
  211. $this->session->set_flashdata('success', sprintf(lang('modules.upgrade_success'), $slug));
  212. }
  213. // If upgrade failed
  214. else
  215. {
  216. $this->session->set_flashdata('error', sprintf(lang('modules.upgrade_error'), $slug));
  217. }
  218. redirect('admin/modules');
  219. }
  220. /**
  221. * Delete Recursive
  222. *
  223. * Recursively delete a folder
  224. *
  225. * @param string $str The path to delete
  226. * @return bool
  227. */
  228. private function _delete_recursive($str)
  229. {
  230. if (is_file($str))
  231. {
  232. return @unlink($str);
  233. }
  234. elseif (is_dir($str))
  235. {
  236. $scan = glob(rtrim($str,'/').'/*');
  237. foreach($scan as $index => $path)
  238. {
  239. $this->_delete_recursive($path);
  240. }
  241. return @rmdir($str);
  242. }
  243. }
  244. }