PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cp/cp.modules.php

https://github.com/danboy/Croissierd
PHP | 421 lines | 236 code | 125 blank | 60 comment | 41 complexity | b4c6075f1b4a80d448765508159e495b MD5 | raw file
  1. <?php
  2. /*
  3. =====================================================
  4. ExpressionEngine - by EllisLab
  5. -----------------------------------------------------
  6. http://expressionengine.com/
  7. -----------------------------------------------------
  8. Copyright (c) 2003 - 2010 EllisLab, Inc.
  9. =====================================================
  10. THIS IS COPYRIGHTED SOFTWARE
  11. PLEASE READ THE LICENSE AGREEMENT
  12. http://expressionengine.com/docs/license.html
  13. =====================================================
  14. File: cp.modules.php
  15. -----------------------------------------------------
  16. Purpose: The module management class
  17. =====================================================
  18. */
  19. if ( ! defined('EXT'))
  20. {
  21. exit('Invalid file request');
  22. }
  23. class Modules {
  24. var $lang_overrides = array('forum' => 'forum_cp');
  25. /** -----------------------------
  26. /** Constructor
  27. /** -----------------------------*/
  28. function Modules()
  29. {
  30. global $IN;
  31. switch($IN->GBL('M'))
  32. {
  33. case FALSE : $this->module_home_page();
  34. break;
  35. case 'INST' : $this->module_installer();
  36. break;
  37. default : $this->module_handler();
  38. break;
  39. }
  40. }
  41. /* END */
  42. /** -----------------------------
  43. /** Module home page
  44. /** -----------------------------*/
  45. function module_home_page($message = '')
  46. {
  47. global $DSP, $LANG, $SESS, $DB;
  48. if ( ! $DSP->allowed_group('can_access_modules'))
  49. {
  50. return $DSP->no_access_message();
  51. }
  52. /** -----------------------------
  53. /** Assing page title
  54. /** -----------------------------*/
  55. $title = $LANG->line('modules');
  56. $DSP->title = $title;
  57. $DSP->crumb = $title;
  58. // Set access status
  59. $can_admin = ( ! $DSP->allowed_group('can_admin_modules')) ? FALSE : TRUE;
  60. /** -----------------------------------------------
  61. /** Fetch all module names from "modules" folder
  62. /** -----------------------------------------------*/
  63. $modules = array();
  64. if ($fp = @opendir(PATH_MOD))
  65. {
  66. while (false !== ($file = readdir($fp)))
  67. {
  68. if ( is_dir(PATH_MOD.$file) && ! preg_match("/[^a-z\_0-9]/", $file))
  69. {
  70. $LANG->fetch_language_file(( ! isset($this->lang_overrides[$file])) ? $file : $this->lang_overrides[$file]);
  71. $modules[] = ucfirst($file);
  72. }
  73. }
  74. }
  75. closedir($fp);
  76. sort($modules);
  77. /** --------------------------------------
  78. /** Fetch the installed modules from DB
  79. /** --------------------------------------*/
  80. $query = $DB->query("SELECT module_name, module_version, has_cp_backend FROM exp_modules ORDER BY module_name");
  81. $installed_mods = array();
  82. foreach ($query->result as $row)
  83. {
  84. $installed_mods[$row['module_name']] = array($row['module_version'], $row['has_cp_backend']);
  85. }
  86. /** --------------------------------------
  87. /** Fetch allowed Modules for a particular user
  88. /** --------------------------------------*/
  89. $sql = "SELECT exp_modules.module_name
  90. FROM exp_modules, exp_module_member_groups
  91. WHERE exp_module_member_groups.group_id = '".$SESS->userdata['group_id']."'
  92. AND exp_modules.module_id = exp_module_member_groups.module_id
  93. ORDER BY module_name";
  94. $query = $DB->query($sql);
  95. $allowed_mods = array();
  96. if ($query->num_rows == 0 AND ! $can_admin)
  97. {
  98. return $DSP->body = $DSP->qdiv('', $LANG->line('module_no_access'));
  99. }
  100. foreach ($query->result as $row)
  101. {
  102. $allowed_mods[] = $row['module_name'];
  103. }
  104. /** --------------------------------------
  105. /** Build page output
  106. /** --------------------------------------*/
  107. $r = '';
  108. if ($message != '')
  109. $r .= $DSP->qdiv('box', $message);
  110. $r .= $DSP->table('tableBorder', '0', '0', '100%').
  111. $DSP->tr().
  112. $DSP->table_qcell('tableHeading',
  113. array(
  114. NBS,
  115. $LANG->line('module_name'),
  116. $LANG->line('module_description'),
  117. $LANG->line('module_version'),
  118. $LANG->line('module_status'),
  119. $LANG->line('module_action')
  120. )
  121. ).
  122. $DSP->tr_c();
  123. $i = 0;
  124. $n = 1;
  125. foreach ($modules as $mod)
  126. {
  127. if ( ! $can_admin)
  128. {
  129. if (! in_array($mod, $allowed_mods))
  130. {
  131. continue;
  132. }
  133. }
  134. $style = ($i++ % 2) ? 'tableCellOne' : 'tableCellTwo';
  135. $r .= $DSP->tr();
  136. $r .= $DSP->table_qcell($style, $DSP->qspan('', $n++), '1%');
  137. // Module Name
  138. $name = ($LANG->line(strtolower($mod).'_module_name') != FALSE) ? $LANG->line(strtolower($mod).'_module_name') : $mod;
  139. if (isset($installed_mods[$mod]) AND $installed_mods[$mod]['1'] == 'y')
  140. {
  141. $name = $DSP->anchor(BASE.AMP.'C=modules'.AMP.'M='.$mod, $name);
  142. }
  143. $r .= $DSP->table_qcell($style, $DSP->qspan('defaultBold', $name), '29%');
  144. // Module Description
  145. $r .= $DSP->table_qcell($style, $LANG->line(strtolower($mod).'_module_description'), '36%');
  146. // Module Version
  147. $version = ( ! isset($installed_mods[$mod])) ? '--' : $installed_mods[$mod]['0'];
  148. $r .= $DSP->table_qcell($style, $version, '10%');
  149. // Module Status
  150. $status = ( ! isset($installed_mods[$mod]) ) ? 'not_installed' : 'installed';
  151. $in_status = str_replace(" ", "&nbsp;", $LANG->line($status));
  152. $show_status = ($status == 'not_installed') ? $DSP->qspan('highlight', $in_status) : $DSP->qspan('highlight_alt', $in_status);
  153. $r .= $DSP->table_qcell($style, $show_status, '12%');
  154. // Module Action
  155. $action = ($status == 'not_installed') ? 'install' : 'deinstall';
  156. $show_action = ($can_admin) ? $DSP->anchor(BASE.AMP.'C=modules'.AMP.'M=INST'.AMP.'MOD='.$mod, $LANG->line($action)) : '--';
  157. $r .= $DSP->table_qcell($style, $show_action, '10%');
  158. $r .= $DSP->tr_c();
  159. }
  160. $r .= $DSP->table_c();
  161. if ($message != '')
  162. $DSP->crumb_ov = TRUE;
  163. $DSP->body = $r;
  164. }
  165. /* END */
  166. /** -----------------------------
  167. /** Module handler
  168. /** -----------------------------*/
  169. function module_handler()
  170. {
  171. global $LANG, $IN, $DSP, $DB, $OUT, $SESS, $FNS;
  172. if ( ! $DSP->allowed_group('can_access_modules'))
  173. {
  174. return $DSP->no_access_message();
  175. }
  176. if ( ! $MOD = $IN->GBL('M', 'GET'))
  177. {
  178. return false;
  179. }
  180. $module = $FNS->filename_security(strtolower($MOD));
  181. if ($SESS->userdata['group_id'] != 1)
  182. {
  183. $query = $DB->query("SELECT module_id FROM exp_modules WHERE module_name = '".$DB->escape_str(ucfirst($module))."'");
  184. if ($query->num_rows == 0)
  185. {
  186. return false;
  187. }
  188. $access = FALSE;
  189. foreach ($SESS->userdata['assigned_modules'] as $key => $val)
  190. {
  191. if ($key == $query->row['module_id'])
  192. {
  193. $access = TRUE;
  194. break;
  195. }
  196. }
  197. if ($access == FALSE)
  198. {
  199. return $DSP->no_access_message();
  200. }
  201. }
  202. $LANG->fetch_language_file(( ! isset($this->lang_overrides[$module])) ? $module : $this->lang_overrides[$module]);
  203. $class = ucfirst($MOD).'_CP';
  204. $path = PATH_MOD.$module.'/mcp.'.$module.EXT;
  205. if ( ! is_file($path))
  206. {
  207. $OUT->fatal_error($LANG->line('module_can_not_be_found'));
  208. }
  209. // set the path to view files for this module
  210. $DSP->view_path = PATH_MOD.$module.'/views/';
  211. require $path;
  212. $MOD = new $class;
  213. }
  214. /* END */
  215. /** ----------------------------------
  216. /** Module installer / De-installer
  217. /** ----------------------------------*/
  218. function module_installer()
  219. {
  220. global $LANG, $IN, $DSP, $DB, $OUT, $FNS;
  221. if ( ! $DSP->allowed_group('can_admin_modules'))
  222. {
  223. return $DSP->no_access_message();
  224. }
  225. if ( ! $module = $IN->GBL('MOD', 'GET'))
  226. {
  227. return false;
  228. }
  229. $module = $FNS->filename_security(strtolower($module));
  230. $class = ucfirst($module).'_CP';
  231. $query = $DB->query("SELECT count(*) AS count FROM exp_modules WHERE module_name = '".$DB->escape_str(ucfirst($module))."'");
  232. if ($query->row['count'] != 0)
  233. {
  234. if ( ! $IN->GBL('DO', 'POST'))
  235. {
  236. return $this->deinstall_confirm($module);
  237. }
  238. $method = $module.'_module_deinstall';
  239. $error = 'module_deinstall_error';
  240. }
  241. else
  242. {
  243. $method = $module.'_module_install';
  244. $error = 'module_install_error';
  245. }
  246. $path = PATH_MOD.$module.'/mcp.'.$module.EXT;
  247. if ( ! is_file($path))
  248. {
  249. $OUT->fatal_error($LANG->line('module_can_not_be_found'));
  250. }
  251. if ( ! class_exists($class))
  252. {
  253. require $path;
  254. }
  255. $MOD = new $class(0);
  256. $MOD->$method();
  257. $LANG->fetch_language_file($module);
  258. $line = (stristr($method, 'deinstall')) ? $LANG->line('module_has_been_removed') : $LANG->line('module_has_been_installed');
  259. $name = ($LANG->line($module.'_module_name') == FALSE) ? ucfirst($module) : $LANG->line($module.'_module_name');
  260. $message = $DSP->qspan('success', $line).NBS.$DSP->qspan('defaultBold', $name);
  261. $this->module_home_page($message);
  262. }
  263. /* END */
  264. /** ----------------------------------
  265. /** De-install Confirm
  266. /** ----------------------------------*/
  267. function deinstall_confirm($module = '')
  268. {
  269. global $DSP, $LANG;
  270. if ( ! $DSP->allowed_group('can_admin_modules'))
  271. {
  272. return $DSP->no_access_message();
  273. }
  274. if ($module == '')
  275. {
  276. return $DSP->no_access_message();
  277. }
  278. $DSP->title = $LANG->line('delete_module');
  279. $DSP->crumb = $LANG->line('delete_module');
  280. $DSP->body = $DSP->form_open(
  281. array('action' => 'C=modules'.AMP.'M=INST'.AMP.'MOD='.$module),
  282. array('DO' => 'TRUE')
  283. );
  284. $DSP->body .= $DSP->qdiv('alertHeading', $LANG->line('delete_module'));
  285. $DSP->body .= $DSP->div('box');
  286. $DSP->body .= $DSP->qdiv('defaultBold', $LANG->line('delete_module_confirm'));
  287. $DSP->body .= $DSP->qdiv('defaultBold', BR.ucfirst($module));
  288. $DSP->body .= $DSP->qdiv('alert', BR.$LANG->line('data_will_be_lost')).BR;
  289. $DSP->body .= $DSP->div_c();
  290. $DSP->body .= $DSP->qdiv('itemWrapperTop', $DSP->input_submit($LANG->line('delete_module')));
  291. $DSP->body .= $DSP->form_close();
  292. }
  293. /* END */
  294. }
  295. // END CLASS
  296. ?>