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

https://github.com/gtenaschuk/pyrocms · PHP · 259 lines · 142 code · 34 blank · 83 comment · 16 complexity · 3935ce2ab3b75aeeb9a43a8c1fc1cc71 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. // We dont want to work with old data
  35. $this->cache->delete_all('module_m');
  36. $this->module_m->import_unknown();
  37. $this->template->all_modules = $this->module_m->get_all(NULL, TRUE);
  38. $this->template
  39. ->title($this->module_details['name'])
  40. ->build('admin/index', $this->data);
  41. }
  42. /**
  43. * Upload
  44. *
  45. * Uploads an addon module
  46. *
  47. * @access public
  48. * @return void
  49. */
  50. public function upload()
  51. {
  52. if ($this->input->post('btnAction') == 'upload')
  53. {
  54. $config['upload_path'] = FCPATH.'uploads/';
  55. $config['allowed_types'] = 'zip';
  56. $config['max_size'] = '2048';
  57. $config['overwrite'] = TRUE;
  58. $this->load->library('upload', $config);
  59. if ($this->upload->do_upload())
  60. {
  61. $upload_data = $this->upload->data();
  62. // Check if we already have a dir with same name
  63. if ($this->module_m->exists($upload_data['raw_name']))
  64. {
  65. $this->session->set_flashdata('error', sprintf(lang('modules.already_exists_error'), $upload_data['raw_name']));
  66. }
  67. else
  68. {
  69. // Now try to unzip
  70. $this->load->library('unzip');
  71. $this->unzip->allow(array('xml', 'html', 'css', 'js', 'png', 'gif', 'jpeg', 'jpg', 'swf', 'ico', 'php'));
  72. // Try and extract
  73. if ($this->unzip->extract($upload_data['full_path'], ADDONPATH . 'modules/'))
  74. {
  75. if ($this->module_m->install($upload_data['raw_name']))
  76. {
  77. $this->session->set_flashdata('success', sprintf(lang('modules.install_success'), $upload_data['raw_name']));
  78. }
  79. else
  80. {
  81. }
  82. }
  83. else
  84. {
  85. $this->session->set_flashdata('error', $this->unzip->error_string());
  86. }
  87. }
  88. // Delete uploaded file
  89. @unlink($upload_data['full_path']);
  90. }
  91. else
  92. {
  93. $this->session->set_flashdata('error', $this->upload->display_errors());
  94. }
  95. // Clear the cache
  96. $this->cache->delete_all('module_m');
  97. redirect('admin/modules');
  98. }
  99. $this->template
  100. ->title($this->module_details['name'],lang('modules.upload_title'))
  101. ->build('admin/upload', $this->data);
  102. }
  103. /**
  104. * Uninstall
  105. *
  106. * Uninstalls an addon module
  107. *
  108. * @param string $slug The slug of the module to uninstall
  109. * @access public
  110. * @return void
  111. */
  112. public function uninstall($slug = '')
  113. {
  114. // Don't allow user to delete the entire module folder
  115. if ($slug == '/' OR $slug == '*' OR empty($slug))
  116. {
  117. show_error(lang('modules.module_not_specified'));
  118. }
  119. if ($this->module_m->uninstall($slug))
  120. {
  121. $this->session->set_flashdata('success', sprintf(lang('modules.uninstall_success'), $slug));
  122. $path = ADDONPATH . 'modules/' . $slug;
  123. if (!$this->_delete_recursive($path))
  124. {
  125. $this->session->set_flashdata('notice', sprintf(lang('modules.manually_remove'), $path));
  126. }
  127. redirect('admin/modules');
  128. }
  129. $this->session->set_flashdata('error', sprintf(lang('modules.uninstall_error'), $slug));
  130. redirect('admin/modules');
  131. }
  132. /**
  133. * Enable
  134. *
  135. * Enables an addon module
  136. *
  137. * @param string $slug The slug of the module to enable
  138. * @access public
  139. * @return void
  140. */
  141. public function install($slug)
  142. {
  143. if ($this->module_m->install($slug))
  144. {
  145. // Clear the module cache
  146. $this->cache->delete_all('module_m');
  147. $this->session->set_flashdata('success', sprintf(lang('modules.install_success'), $slug));
  148. }
  149. else
  150. {
  151. $this->session->set_flashdata('error', sprintf(lang('modules.install_error'), $slug));
  152. }
  153. redirect('admin/modules');
  154. }
  155. /**
  156. * Enable
  157. *
  158. * Enables an addon module
  159. *
  160. * @param string $slug The slug of the module to enable
  161. * @access public
  162. * @return void
  163. */
  164. public function enable($slug)
  165. {
  166. if ($this->module_m->enable($slug))
  167. {
  168. // Clear the module cache
  169. $this->cache->delete_all('module_m');
  170. $this->session->set_flashdata('success', sprintf(lang('modules.enable_success'), $slug));
  171. }
  172. else
  173. {
  174. $this->session->set_flashdata('error', sprintf(lang('modules.enable_error'), $slug));
  175. }
  176. redirect('admin/modules');
  177. }
  178. /**
  179. * Disable
  180. *
  181. * Disables an addon module
  182. *
  183. * @param string $slug The slug of the module to disable
  184. * @access public
  185. * @return void
  186. */
  187. public function disable($slug)
  188. {
  189. if ($this->module_m->disable($slug))
  190. {
  191. // Clear the module cache
  192. $this->cache->delete_all('module_m');
  193. $this->session->set_flashdata('success', sprintf(lang('modules.disable_success'), $slug));
  194. }
  195. else
  196. {
  197. $this->session->set_flashdata('error', sprintf(lang('modules.disable_error'), $slug));
  198. }
  199. redirect('admin/modules');
  200. }
  201. /**
  202. * Delete Recursive
  203. *
  204. * Recursively delete a folder
  205. *
  206. * @param string $str The path to delete
  207. * @return bool
  208. */
  209. private function _delete_recursive($str)
  210. {
  211. if (is_file($str))
  212. {
  213. return @unlink($str);
  214. }
  215. elseif (is_dir($str))
  216. {
  217. $scan = glob(rtrim($str,'/').'/*');
  218. foreach($scan as $index => $path)
  219. {
  220. $this->_delete_recursive($path);
  221. }
  222. return @rmdir($str);
  223. }
  224. }
  225. }