/handlers/adminpluginshandler.php

https://github.com/itnaegele/system · PHP · 245 lines · 199 code · 17 blank · 29 comment · 38 complexity · eac68fc2f4adb96e5f35897cacad5351 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Habari
  4. *
  5. */
  6. /**
  7. * Habari AdminPluginsHandler Class
  8. * Handles plugin-related actions in the admin
  9. *
  10. */
  11. class AdminPluginsHandler extends AdminHandler
  12. {
  13. /**
  14. * Display the plugin administration page
  15. */
  16. public function get_plugins()
  17. {
  18. $all_plugins = Plugins::list_all();
  19. $active_plugins = Plugins::get_active();
  20. $sort_active_plugins = array();
  21. $sort_inactive_plugins = array();
  22. $providing = array();
  23. foreach ( $all_plugins as $file ) {
  24. $plugin = array();
  25. $plugin_id = Plugins::id_from_file( $file );
  26. $plugin['plugin_id'] = $plugin_id;
  27. $plugin['file'] = $file;
  28. $error = '';
  29. if ( Utils::php_check_file_syntax( $file, $error ) ) {
  30. $plugin['debug'] = false;
  31. $plugin['info'] = Plugins::load_info( $file );
  32. if ( array_key_exists( $plugin_id, $active_plugins ) ) {
  33. $plugin['verb'] = _t( 'Deactivate' );
  34. $pluginobj = $active_plugins[$plugin_id];
  35. $plugin['active'] = true;
  36. $plugin_actions = array();
  37. $plugin_actions1 = Plugins::filter_id( 'plugin_config', $plugin_id, $plugin_actions, $plugin_id );
  38. $plugin_actions = Plugins::filter( 'plugin_config_any', $plugin_actions1, $plugin_id );
  39. $plugin['actions'] = array();
  40. foreach ( $plugin_actions as $plugin_action => $plugin_action_caption ) {
  41. if ( is_numeric( $plugin_action ) ) {
  42. $plugin_action = $plugin_action_caption;
  43. }
  44. $action = array(
  45. 'caption' => $plugin_action_caption,
  46. 'action' => $plugin_action,
  47. );
  48. $urlparams = array( 'page' => 'plugins', 'configure'=>$plugin_id);
  49. $action['url'] = URL::get( 'admin', $urlparams );
  50. if ( $action['caption'] == _t( '?' ) ) {
  51. if ( isset( $_GET['configaction'] ) ) {
  52. $urlparams['configaction'] = $_GET['configaction'];
  53. }
  54. if ( $_GET['help'] != $plugin_action ) {
  55. $urlparams['help'] = $plugin_action;
  56. }
  57. $action['url'] = URL::get( 'admin', $urlparams );
  58. $plugin['help'] = $action;
  59. }
  60. else {
  61. if ( isset( $_GET['help'] ) ) {
  62. $urlparams['help'] = $_GET['help'];
  63. }
  64. $urlparams['configaction'] = $plugin_action;
  65. $action['url'] = URL::get( 'admin', $urlparams );
  66. $plugin['actions'][$plugin_action] = $action;
  67. }
  68. }
  69. $plugin['actions']['deactivate'] = array(
  70. 'url' => URL::get( 'admin', 'page=plugin_toggle&plugin_id=' . $plugin['plugin_id'] . '&action=deactivate' ),
  71. 'caption' => _t( 'Deactivate' ),
  72. 'action' => 'Deactivate',
  73. );
  74. if ( isset( $plugin['info']->provides ) ) {
  75. foreach ( $plugin['info']->provides->feature as $feature ) {
  76. $providing[(string) $feature] = $feature;
  77. }
  78. }
  79. }
  80. else {
  81. // instantiate this plugin
  82. // in order to get its info()
  83. $plugin['active'] = false;
  84. $plugin['verb'] = _t( 'Activate' );
  85. $plugin['actions'] = array(
  86. 'activate' => array(
  87. 'url' => URL::get( 'admin', 'page=plugin_toggle&plugin_id=' . $plugin['plugin_id'] . '&action=activate' ),
  88. 'caption' => _t( 'Activate' ),
  89. 'action' => 'activate',
  90. ),
  91. );
  92. if ( isset( $plugin['info']->help ) ) {
  93. if ( isset( $_GET['configaction'] ) ) {
  94. $urlparams['configaction'] = $_GET['configaction'];
  95. }
  96. if ( $_GET['help'] != '_help' ) {
  97. $urlparams['help'] = '_help';
  98. }
  99. $action['caption'] = _t( '?' );
  100. $action['action'] = '_help';
  101. $urlparams = array( 'page' => 'plugins', 'configure' => $plugin_id );
  102. $action['url'] = URL::get( 'admin', $urlparams );
  103. $plugin['help'] = $action;
  104. }
  105. }
  106. }
  107. else {
  108. $plugin['debug'] = true;
  109. $plugin['error'] = $error;
  110. $plugin['active'] = false;
  111. }
  112. if ( isset( $this->handler_vars['configure'] ) && ( $this->handler_vars['configure'] == $plugin['plugin_id'] ) ) {
  113. if ( isset( $plugin['help'] ) && Controller::get_var( 'configaction' ) == $plugin['help']['action'] ) {
  114. $this->theme->config_plugin_caption = _t( 'Help' );
  115. }
  116. else {
  117. if ( isset( $plugin['actions'][Controller::get_var( 'configaction' )] ) ) {
  118. $this->theme->config_plugin_caption = $plugin['actions'][Controller::get_var( 'configaction' )]['caption'];
  119. }
  120. else {
  121. $this->theme->config_plugin_caption = Controller::get_var( 'configaction' );
  122. }
  123. }
  124. unset( $plugin['actions'][Controller::get_var( 'configaction' )] );
  125. $this->theme->config_plugin = $plugin;
  126. }
  127. else if ( $plugin['active'] ) {
  128. $sort_active_plugins[$plugin_id] = $plugin;
  129. }
  130. else {
  131. $sort_inactive_plugins[$plugin_id] = $plugin;
  132. }
  133. }
  134. // Get the features that the current theme provides
  135. $themeinfo = Themes::get_active_data();
  136. if ( isset( $themeinfo['info']->provides ) ) {
  137. foreach ( $themeinfo['info']->provides->feature as $feature ) {
  138. $providing[(string) $feature] = $feature;
  139. }
  140. }
  141. foreach ( $sort_inactive_plugins as $plugin_id => $plugin ) {
  142. if ( isset( $plugin['info']->requires ) ) {
  143. foreach ( $plugin['info']->requires->feature as $feature ) {
  144. if ( !isset( $providing[(string) $feature] ) ) {
  145. if ( !isset( $sort_inactive_plugins[$plugin_id]['missing'] ) ) {
  146. $sort_inactive_plugins[$plugin_id]['missing'] = array();
  147. }
  148. $sort_inactive_plugins[$plugin_id]['missing'][(string) $feature] = isset( $feature['url'] ) ? $feature['url'] : '';
  149. unset( $sort_inactive_plugins[$plugin_id]['actions']['activate'] );
  150. }
  151. }
  152. }
  153. }
  154. //$this->theme->plugins = array_merge($sort_active_plugins, $sort_inactive_plugins);
  155. $this->theme->assign( 'configaction', Controller::get_var( 'configaction' ) );
  156. $this->theme->assign( 'helpaction', Controller::get_var( 'help' ) );
  157. $this->theme->assign( 'configure', Controller::get_var( 'configure' ) );
  158. uasort($sort_active_plugins, array( $this, 'compare_names' ) );
  159. uasort($sort_inactive_plugins, array( $this, 'compare_names' ) );
  160. $this->theme->active_plugins = $sort_active_plugins;
  161. $this->theme->inactive_plugins = $sort_inactive_plugins;
  162. $this->theme->plugin_loader = Plugins::filter( 'plugin_loader', '', $this->theme );
  163. $this->display( 'plugins' );
  164. }
  165. /**
  166. * A POST handler for the admin plugins page that simply passes those options through.
  167. */
  168. public function post_plugins()
  169. {
  170. return $this->get_plugins();
  171. }
  172. /**
  173. * Handles plugin activation or deactivation.
  174. */
  175. public function get_plugin_toggle()
  176. {
  177. $extract = $this->handler_vars->filter_keys( 'plugin_id', 'action' );
  178. foreach ( $extract as $key => $value ) {
  179. $$key = $value;
  180. }
  181. $plugins = Plugins::list_all();
  182. foreach ( $plugins as $file ) {
  183. if ( Plugins::id_from_file( $file ) == $plugin_id ) {
  184. switch ( strtolower( $action ) ) {
  185. case 'activate':
  186. if ( Plugins::activate_plugin( $file ) ) {
  187. $plugins = Plugins::get_active();
  188. Session::notice(
  189. _t( "Activated plugin '%s'", array( $plugins[Plugins::id_from_file( $file )]->info->name ) ),
  190. $plugins[Plugins::id_from_file( $file )]->plugin_id
  191. );
  192. }
  193. break;
  194. case 'deactivate':
  195. if ( Plugins::deactivate_plugin( $file ) ) {
  196. $plugins = Plugins::get_active();
  197. Session::notice(
  198. _t( "Deactivated plugin '%s'", array( $plugins[Plugins::id_from_file( $file )]->info->name ) ),
  199. $plugins[Plugins::id_from_file( $file )]->plugin_id
  200. );
  201. }
  202. break;
  203. default:
  204. Plugins::act(
  205. 'adminhandler_get_plugin_toggle_action',
  206. $action,
  207. $file,
  208. $plugin_id,
  209. $plugins
  210. );
  211. break;
  212. }
  213. }
  214. }
  215. Utils::redirect( URL::get( 'admin', 'page=plugins' ) );
  216. }
  217. /*
  218. * Compare function for uasort()
  219. * @param array $a The first element to compare
  220. * @param array $b The second element to compare
  221. * @return integer. 0 if the strings are equal, <0 if the first parameter is less than the second,
  222. * and >0 if the first parameter is greater than the second
  223. */
  224. protected function compare_names( $a, $b )
  225. {
  226. return strcmp( MultiByte::strtolower( $a['info']->name), MultiByte::strtolower( $b['info']->name ) );
  227. }
  228. }