PageRenderTime 57ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/modules/controllers/admin.php

https://github.com/BlendedByChris/pyrocms
PHP | 226 lines | 124 code | 30 blank | 72 comment | 17 complexity | 2ea7e7e8e88f124469282225238c05ec MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * Modules controller, lists all installed modules
  4. *
  5. * @author Yorick Peterse - PyroCMS Development Team
  6. * @package PyroCMS
  7. * @subpackage Core modules
  8. * @category Modules
  9. * @since v0.9.7
  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. // Check the referrer
  24. parse_url($this->input->server('HTTP_REFERER'), PHP_URL_HOST) == parse_url(BASE_URL, PHP_URL_HOST) or show_error('Invalid Referrer');
  25. }
  26. /**
  27. * Index method
  28. * @access public
  29. * @return void
  30. */
  31. public function index()
  32. {
  33. $this->modules_m->import_all();
  34. $this->cache->delete_all('modules_m');
  35. $this->data->modules = $this->modules_m->get_modules(NULL, TRUE);
  36. $this->template->build('admin/index', $this->data);
  37. }
  38. /**
  39. * Upload
  40. *
  41. * Uploads a third_party module
  42. *
  43. * @access public
  44. * @return void
  45. */
  46. public function upload()
  47. {
  48. if($this->input->post('btnAction') == 'upload')
  49. {
  50. $config['upload_path'] = APPPATH.'uploads/';
  51. $config['allowed_types'] = 'zip';
  52. $config['max_size'] = '2048';
  53. $config['overwrite'] = TRUE;
  54. $this->load->library('upload', $config);
  55. if ($this->upload->do_upload())
  56. {
  57. $upload_data = $this->upload->data();
  58. // Check if we already have a dir with same name
  59. if($this->modules_m->exists($upload_data['raw_name']))
  60. {
  61. $this->session->set_flashdata('error', sprintf(lang('modules.already_exists_error'), $upload_data['raw_name']));
  62. }
  63. else
  64. {
  65. // Now try to unzip
  66. $this->load->library('unzip');
  67. $this->unzip->allow(array('xml', 'html', 'css', 'js', 'png', 'gif', 'jpeg', 'jpg', 'swf', 'ico', 'php'));
  68. // Try and extract
  69. if($this->unzip->extract($upload_data['full_path'], dirname(APPPATH) . '/third_party/modules/'))
  70. {
  71. if($this->modules_m->install($upload_data['raw_name']))
  72. {
  73. $this->session->set_flashdata('success', sprintf(lang('modules.install_success'), $upload_data['raw_name']));
  74. }
  75. else
  76. {
  77. }
  78. }
  79. else
  80. {
  81. $this->session->set_flashdata('error', $this->unzip->error_string());
  82. }
  83. }
  84. // Delete uploaded file
  85. @unlink($upload_data['full_path']);
  86. }
  87. else
  88. {
  89. $this->session->set_flashdata('error', $this->upload->display_errors());
  90. }
  91. // Clear the cache
  92. $this->cache->delete_all('modules_m');
  93. redirect('admin/modules');
  94. }
  95. $this->template->build('admin/upload', $this->data);
  96. }
  97. /**
  98. * Uninstall
  99. *
  100. * Uninstalls a third_party module
  101. *
  102. * @param string $module_slug The slug of the module to uninstall
  103. * @access public
  104. * @return void
  105. */
  106. public function uninstall($module_slug = '')
  107. {
  108. // Don't allow user to delete the entire module folder
  109. if($module_slug == '/' || $module_slug == '*' || empty($module_slug))
  110. {
  111. show_error(lang('modules.module_not_specified'));
  112. }
  113. if($this->modules_m->uninstall($module_slug))
  114. {
  115. $this->session->set_flashdata('success', sprintf(lang('modules.uninstall_success'), $module_slug));
  116. $path = 'third_party/modules/' . $module_slug;
  117. if(!$this->_delete_recursive($path))
  118. {
  119. $this->session->set_flashdata('notice', sprintf(lang('modules.manually_remove'), $path));
  120. }
  121. redirect('admin/modules');
  122. }
  123. $this->session->set_flashdata('error', sprintf(lang('modules.uninstall_error'), $module_slug));
  124. redirect('admin/modules');
  125. }
  126. /**
  127. * Enable
  128. *
  129. * Enables a third_party module
  130. *
  131. * @param string $module_slug The slug of the module to enable
  132. * @access public
  133. * @return void
  134. */
  135. public function enable($module_slug)
  136. {
  137. if($this->modules_m->enable($module_slug))
  138. {
  139. // Clear the module cache
  140. $this->cache->delete_all('modules_m');
  141. $this->session->set_flashdata('success', sprintf(lang('modules.enable_success'), $module_slug));
  142. }
  143. else
  144. {
  145. $this->session->set_flashdata('error', sprintf(lang('modules.enable_error'), $module_slug));
  146. }
  147. redirect('admin/modules');
  148. }
  149. /**
  150. * Disable
  151. *
  152. * Disables a third_party module
  153. *
  154. * @param string $module_slug The slug of the module to disable
  155. * @access public
  156. * @return void
  157. */
  158. public function disable($module_slug)
  159. {
  160. if($this->modules_m->disable($module_slug))
  161. {
  162. // Clear the module cache
  163. $this->cache->delete_all('modules_m');
  164. $this->session->set_flashdata('success', sprintf(lang('modules.disable_success'), $module_slug));
  165. }
  166. else
  167. {
  168. $this->session->set_flashdata('error', sprintf(lang('modules.disable_error'), $module_slug));
  169. }
  170. redirect('admin/modules');
  171. }
  172. /**
  173. * Delete Recursive
  174. *
  175. * Recursively delete a folder
  176. *
  177. * @param string $str The path to delete
  178. * @return bool
  179. */
  180. private function _delete_recursive($str)
  181. {
  182. if(is_file($str))
  183. {
  184. return @unlink($str);
  185. }
  186. elseif(is_dir($str))
  187. {
  188. $scan = glob(rtrim($str,'/').'/*');
  189. foreach($scan as $index => $path)
  190. {
  191. $this->_delete_recursive($path);
  192. }
  193. return @rmdir($str);
  194. }
  195. }
  196. }