PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/system/expressionengine/controllers/cp/addons_modules.php

https://bitbucket.org/mbaily/tremain
PHP | 468 lines | 270 code | 102 blank | 96 comment | 45 complexity | d83d69acbedd6cf7e1f565f93b3f9d0c MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2013, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Modules Administration Class
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Control Panel
  19. * @category Control Panel
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Addons_modules extends CP_Controller {
  24. var $_mcp_reference;
  25. /**
  26. * Constructor
  27. */
  28. function __construct()
  29. {
  30. parent::__construct();
  31. if ( ! $this->cp->allowed_group('can_access_addons', 'can_access_modules'))
  32. {
  33. show_error(lang('unauthorized_access'));
  34. }
  35. $this->load->model('addons_model');
  36. $this->lang->loadfile('modules');
  37. }
  38. // --------------------------------------------------------------------
  39. /**
  40. * Module Section Homepage
  41. *
  42. * @access public
  43. * @return void
  44. */
  45. function index()
  46. {
  47. // Set access status
  48. $can_admin = ( ! $this->cp->allowed_group('can_admin_modules')) ? FALSE : TRUE;
  49. $this->load->library(array('addons', 'table', 'typography'));
  50. $this->load->helper('directory');
  51. $this->cp->set_right_nav(array('update_modules' => BASE.AMP.'C=addons_modules'.AMP.'check_updates=y'));
  52. $this->jquery->tablesorter('.mainTable', '{
  53. textExtraction: "complex",
  54. widgets: ["zebra"]
  55. }');
  56. // Fetch all module names from "modules" folder
  57. $modules = $this->addons->get_files();
  58. foreach($modules as $module => $info)
  59. {
  60. $this->lang->loadfile(( ! isset($this->lang_overrides[$module])) ? $module : $this->lang_overrides[$module]);
  61. }
  62. $this->installed_modules = $this->addons->get_installed();
  63. // Fetch allowed Modules for a particular user
  64. $this->db->select('modules.module_name');
  65. $this->db->from('modules, module_member_groups');
  66. $this->db->where('module_member_groups.group_id', $this->session->userdata('group_id'));
  67. $this->db->where('modules.module_id = '.$this->db->dbprefix('module_member_groups').'.module_id', NULL, FALSE);
  68. $this->db->order_by('module_name');
  69. $query = $this->db->get();
  70. $allowed_mods = array();
  71. if ($query->num_rows() == 0 AND ! $can_admin)
  72. {
  73. show_error(lang('module_no_access'));
  74. }
  75. foreach ($query->result_array() as $row)
  76. {
  77. $allowed_mods[] = strtolower($row['module_name']);
  78. }
  79. $vars['table_headings'] = array(
  80. lang('module_name'),
  81. lang('module_description'),
  82. lang('module_version'),
  83. lang('module_status'),
  84. lang('module_action')
  85. );
  86. $modcount = 1;
  87. $vars['modules'] = array();
  88. $names = array();
  89. $data = array();
  90. $updated = array();
  91. foreach ($modules as $module => $module_info)
  92. {
  93. if (IS_CORE && in_array($module, $this->core->standard_modules))
  94. {
  95. continue;
  96. }
  97. if ( ! $can_admin)
  98. {
  99. if ( ! in_array($module, $allowed_mods))
  100. {
  101. continue;
  102. }
  103. }
  104. // Module Name
  105. $name = (lang(strtolower($module).'_module_name') != FALSE) ? lang(strtolower($module).'_module_name') : $module_info['name'];
  106. $names[$modcount] = strtolower($name);
  107. if (isset($this->installed_modules[$module]) AND $this->installed_modules[$module]['has_cp_backend'] == 'y')
  108. {
  109. $cp_theme = ($this->session->userdata['cp_theme'] == '') ? $this->config->item('cp_theme') : $this->session->userdata['cp_theme'];
  110. $name = '<a href="'.BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module='.strtolower($module).'"><strong>'.$name.'</strong></a>';
  111. }
  112. $data[$modcount][] = $name;
  113. // Module Description
  114. $data[$modcount][] = $this->typography->parse_type(
  115. lang(strtolower($module).'_module_description'),
  116. array(
  117. 'text_format' => 'none',
  118. 'html_format' => 'safe',
  119. 'auto_links' => 'y'
  120. )
  121. );
  122. // Module Version
  123. $version = ( ! isset($this->installed_modules[$module])) ? '--' : $this->installed_modules[$module]['module_version'];
  124. $data[$modcount][] = $version;
  125. // Module Status
  126. $status = ( ! isset($this->installed_modules[$module]) ) ? 'not_installed' : 'installed';
  127. $in_status = str_replace(" ", "&nbsp;", lang($status));
  128. $show_status = ($status == 'not_installed') ? '<span class="notice">'.$in_status.'</span>' : '<span class="go_notice">'.$in_status.'</span>';
  129. $data[$modcount][] = $show_status;
  130. // Module Action
  131. $action = ($status == 'not_installed') ? 'install' : 'deinstall';
  132. if ( ! $can_admin)
  133. {
  134. $show_action = '--';
  135. }
  136. elseif ($status == 'not_installed')
  137. {
  138. $show_action = '<a class="less_important_link" href="'.BASE.AMP.'C=addons_modules'.AMP.'M=module_installer'.AMP.'module='.$module.'" title="'.lang('install').'">'.lang('install').'</a>';
  139. }
  140. else
  141. {
  142. $show_action = '<a class="less_important_link" href="'.BASE.AMP.'C=addons_modules'.AMP.'M=module_uninstaller'.AMP.'module='.$module.'" title="'.lang('deinstall').'">'.lang('deinstall').'</a>';
  143. }
  144. $data[$modcount][] = $show_action;
  145. $modcount++;
  146. // Check for updates to module
  147. // Send version to update class and let it do any required work
  148. if ($this->input->get('check_updates') && $status == 'installed' && file_exists($this->installed_modules[$module]['path'].'upd.'.$module.'.php'))
  149. {
  150. require $this->installed_modules[$module]['path'].'upd.'.$module.'.php';
  151. $class = ucfirst($module).'_upd';
  152. $version = $this->installed_modules[$module]['module_version'];
  153. $this->load->add_package_path($this->installed_modules[$module]['path']);
  154. $UPD = new $class;
  155. $UPD->_ee_path = APPPATH;
  156. if (version_compare($UPD->version, $version, '>')
  157. && method_exists($UPD, 'update')
  158. && $UPD->update($version) !== FALSE)
  159. {
  160. $this->db->update('modules', array('module_version' => $UPD->version), array('module_name' => ucfirst($module)));
  161. $updated[] = $name.': '.lang('updated_to_version').' '.$UPD->version;
  162. }
  163. $this->load->remove_package_path($this->installed_modules[$module]['path']);
  164. }
  165. }
  166. // if we were running an update check, redirect with the appropriate message
  167. if ($this->input->get('check_updates'))
  168. {
  169. if (count($updated) > 0)
  170. {
  171. $flashmsg = '<strong>'.lang('updated').'</strong>:<br />'.implode('<br />', $updated);
  172. }
  173. else
  174. {
  175. $flashmsg = lang('all_modules_up_to_date');
  176. }
  177. $this->session->set_flashdata('message_success', $flashmsg);
  178. $this->functions->redirect(BASE.AMP.'C=addons_modules');
  179. }
  180. // Let's order by name just in case
  181. asort($names);
  182. $id = 1;
  183. foreach ($names as $k => $v)
  184. {
  185. $vars['modules'][$id] = $data[$k];
  186. $id++;
  187. }
  188. $this->view->cp_page_title = lang('modules');
  189. $this->cp->set_breadcrumb(BASE.AMP.'C=addons', lang('addons'));
  190. $this->cp->render('addons/modules', $vars);
  191. }
  192. // --------------------------------------------------------------------
  193. /**
  194. * Show Module CP
  195. *
  196. * Used as the router / gateway to module control panel back ends
  197. *
  198. * @access public
  199. * @param string
  200. * @return void
  201. */
  202. function show_module_cp()
  203. {
  204. $this->load->library('addons');
  205. // These can be overriden by individual modules
  206. $this->view->cp_page_title = lang('modules');
  207. // a bit of a breadcrumb override is needed
  208. $this->view->cp_breadcrumbs = array(
  209. BASE.AMP.'C=addons' => lang('addons'),
  210. BASE.AMP.'C=addons_modules'=> lang('addons_modules')
  211. );
  212. $module = $this->input->get_post('module');
  213. $module = $this->security->sanitize_filename(strtolower($module));
  214. $installed = $this->addons->get_installed();
  215. if ($this->session->userdata['group_id'] != 1)
  216. {
  217. // Do they have access to this module?
  218. if ( ! isset($installed[$module]) OR ! isset($this->session->userdata['assigned_modules'][$installed[$module]['module_id']]) OR $this->session->userdata['assigned_modules'][$installed[$module]['module_id']] !== TRUE)
  219. {
  220. show_error(lang('unauthorized_access'));
  221. }
  222. }
  223. else
  224. {
  225. if ( ! isset($installed[$module]))
  226. {
  227. show_error(lang('requested_module_not_installed').NBS.$module);
  228. }
  229. }
  230. $this->lang->loadfile($module);
  231. $view_folder = 'views';
  232. /* -------------------------------------------
  233. /* Hidden Configuration Variable
  234. /* - use_mobile_control_panel => Automatically use mobile cp theme when accessed with a mobile device? (y/n)
  235. /* -------------------------------------------*/
  236. if ($this->agent->is_mobile() && $this->config->item('use_mobile_control_panel') != 'n')
  237. {
  238. // iphone, ipod, blackberry, palm, etc.
  239. $agent = array_search($this->agent->mobile(), $this->agent->mobiles);
  240. $agent = $this->security->sanitize_filename($agent);
  241. if (is_dir($installed[$module]['path'].'mobile_'.$agent))
  242. {
  243. $view_folder = 'mobile_'.$agent;
  244. }
  245. elseif (is_dir($installed[$module]['path'].'mobile'))
  246. {
  247. $view_folder = 'mobile';
  248. }
  249. }
  250. // set the view path
  251. define('MODULE_VIEWS', $installed[$module]['path'].$view_folder.'/');
  252. // Add the helper/library load path and temporarily
  253. // switch the view path to the module's view folder
  254. $this->load->add_package_path($installed[$module]['path'], FALSE);
  255. // Update Module
  256. // Send version to update class and let it do any required work
  257. if (file_exists($installed[$module]['path'].'upd.'.$module.'.php'))
  258. {
  259. require $installed[$module]['path'].'upd.'.$module.'.php';
  260. $class = ucfirst($module).'_upd';
  261. $version = $installed[$module]['module_version'];
  262. $UPD = new $class;
  263. $UPD->_ee_path = APPPATH;
  264. if ($UPD->version > $version && method_exists($UPD, 'update') && $UPD->update($version) !== FALSE)
  265. {
  266. $this->db->update('modules', array('module_version' => $UPD->version), array('module_name' => ucfirst($module)));
  267. }
  268. }
  269. require_once $installed[$module]['path'].$installed[$module]['file'];
  270. // instantiate the module cp class
  271. $mod = new $installed[$module]['class'];
  272. $mod->_ee_path = APPPATH;
  273. // add validation callback support to the mcp class (see EE_form_validation for more info)
  274. $this->_mcp_reference =& $mod;
  275. $method = ($this->input->get('method') !== FALSE) ? $this->input->get('method') : 'index';
  276. // its possible that a module will try to call a method that does not exist
  277. // either by accident (ie: a missed function) or by deliberate user url hacking
  278. if (method_exists($mod, $method))
  279. {
  280. $vars['_module_cp_body'] = $mod->$method();
  281. }
  282. else
  283. {
  284. $vars['_module_cp_body'] = lang('requested_page_not_found');
  285. }
  286. // unset reference
  287. unset($this->_mcp_reference);
  288. // remove package paths
  289. $this->load->remove_package_path($installed[$module]['path']);
  290. $this->cp->render('addons/module_cp_container', $vars);
  291. }
  292. // --------------------------------------------------------------------
  293. /**
  294. * Module Installer
  295. *
  296. * @access public
  297. * @return void
  298. */
  299. function module_installer()
  300. {
  301. $module = $this->input->get_post('module');
  302. $module = $this->security->sanitize_filename(strtolower($module));
  303. $this->load->library('addons/addons_installer');
  304. $this->lang->loadfile($module);
  305. if ($this->addons_installer->install($module, 'module'))
  306. {
  307. $name = (lang($module.'_module_name') == FALSE) ? ucfirst($module) : lang($module.'_module_name');
  308. $cp_message = lang('module_has_been_installed').NBS.$name;
  309. $this->session->set_flashdata('message_success', $cp_message);
  310. $this->functions->redirect(BASE.AMP.'C=addons_modules');
  311. }
  312. }
  313. // --------------------------------------------------------------------
  314. /**
  315. * Module Uninstaller Confirmation
  316. *
  317. * @access public
  318. * @return void
  319. */
  320. function delete_module_confirm()
  321. {
  322. $module = $this->input->get_post('module');
  323. if ( ! $this->cp->allowed_group('can_admin_modules') OR $module === FALSE)
  324. {
  325. show_error(lang('unauthorized_access'));
  326. }
  327. $this->lang->loadfile($module);
  328. $vars['form_action'] = 'C=addons_modules'.AMP.'M=module_uninstaller';
  329. $vars['form_hidden'] = array('module' => $module, 'confirm' => 'delete');
  330. $vars['module_name'] = (lang($module.'_module_name') == FALSE) ? ucwords(str_replace('_', ' ', $module)) : lang($module.'_module_name');
  331. $this->view->cp_page_title = lang('delete_module');
  332. $this->view->cp_breadcrumbs = array(
  333. BASE.AMP.'C=addons' => lang('addons'),
  334. BASE.AMP.'C=addons_modules'=> lang('modules')
  335. );
  336. $this->cp->render('addons/module_delete_confirm', $vars);
  337. }
  338. // --------------------------------------------------------------------
  339. /**
  340. * Module Uninstaller
  341. *
  342. * @access public
  343. * @return void
  344. */
  345. function module_uninstaller()
  346. {
  347. $module = $this->input->get_post('module');
  348. $confirm = $this->input->get_post('confirm');
  349. if ($module === FALSE OR $confirm === FALSE)
  350. {
  351. return $this->delete_module_confirm();
  352. }
  353. $module = $this->security->sanitize_filename(strtolower($module));
  354. $this->load->library('addons/addons_installer');
  355. $this->lang->loadfile($module);
  356. if ($this->addons_installer->uninstall($module, 'module'))
  357. {
  358. $name = (lang($module.'_module_name') == FALSE) ? ucfirst($module) : lang($module.'_module_name');
  359. $this->session->set_flashdata('message_success', lang('module_has_been_removed').NBS.$name);
  360. $this->functions->redirect(BASE.AMP.'C=addons_modules');
  361. }
  362. }
  363. }
  364. // END CLASS
  365. /* End of file addons_modules.php */
  366. /* Location: ./system/expressionengine/controllers/cp/addons_modules.php */