XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
plugins.php
Go to the documentation of this file.
1 <?php
2 // $Id: plugins.php 1026 2012-09-04 23:10:36Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
11 $p = isset($_REQUEST['p']) ? $_REQUEST['p'] : '';
12 if ($p==''){
13  define('RMCLOCATION','plugins');
14 }
15 include_once '../../include/cp_header.php';
16 require_once XOOPS_ROOT_PATH.'/modules/rmcommon/admin_loader.php';
17 
18 function rm_reload_plugins(){
19 
20  $path = RMCPATH.'/plugins';
21  $dir_list = XoopsLists::getDirListAsArray($path);
22 
23  $installed_plugins = array();
24 
25  foreach ($dir_list as $dir){
26 
27  if (!file_exists($path.'/'.$dir.'/'.strtolower($dir).'-plugin.php')) continue;
28 
29  $phand = new RMPlugin($dir); // PLugin handler
30 
31  if (!$phand->isNew() && $phand->getVar('status')){
32 
33  $installed_plugins[] = $phand;
34 
35  }
36  }
37 
38  $plugins = array();
39  foreach($installed_plugins as $p){
40  $plugins[] = $p->getVar('dir');
41  }
42 
43  file_put_contents(XOOPS_CACHE_PATH.'/plgs.cnf', json_encode($plugins));
44 
45 }
46 
47 function show_rm_plugins(){
48 
49  $path = RMCPATH.'/plugins';
50  $dir_list = XoopsLists::getDirListAsArray($path);
51 
52  $available_plugins = array();
53  $installed_plugins = array();
54 
55  foreach ($dir_list as $dir){
56 
57  if (!file_exists($path.'/'.$dir.'/'.strtolower($dir).'-plugin.php')) continue;
58 
59  $phand = new RMPlugin($dir); // PLugin handler
60 
61  if ($phand->isNew()){
62 
63  $phand->setVar('dir', $dir);
64  $available_plugins[] = $phand;
65 
66  } else {
67 
68  $installed_plugins[] = $phand;
69 
70  }
71 
72  }
73 
75 
77  xoops_cp_header();
78 
79  include RMTemplate::get()->get_template('rmc_plugins.php', 'module', 'rmcommon');
80 
81  xoops_cp_footer();
82 
83 }
84 
88 function install_rm_plugin(){
89 
90  $name = rmc_server_var($_GET, 'plugin', '');
91  if ($name==''){
92  redirectMsg('plugins.php', __('You must specify a existing plugin', 'rmcommon'), 1);
93  die();
94  }
95 
96  $plugin = new RMPlugin($name);
97 
98  if (!$plugin->isNew()){
99  redirectMsg('plugins.php', __('Specified plugin is installed already!', 'rmcommon'), 1);
100  die();
101  }
102 
103  if (!$plugin->load_from_dir($name)){
104  redirectMsg('plugins.php', sprintf(__('%s is not a valid plugin!', 'rmcommon'), $name), 1);
105  die();
106  }
107 
108  if (!$plugin->save()){
109  redirectMsg('plugins.php', __('Plugin could not be installed, please try again.','rmcommon'), 1);
110  die();
111  }
112 
113  if (!$plugin->on_install()){
114  redirectMsg('plugins.php', __('The plugin has been inserted on database, but erros ocurred on this process.', 'rmcommon').'<br />'.$plugin->errors(), 1);
115  die();
116  }
117 
119 
120  redirectMsg('plugins.php', __('Plugin installed succesfully!','rmcommon'), 0);
121 
122 }
123 
125 
126  $name = rmc_server_var($_GET, 'plugin', '');
127  if ($name==''){
128  redirectMsg('plugins.php', __('You must specify a existing plugin', 'rmcommon'), 1);
129  die();
130  }
131 
132  $plugin = new RMPlugin($name);
133 
134  if ($plugin->isNew()){
135  redirectMsg('plugins.php', __('Specified plugin is not installed yet!', 'rmcommon'), 1);
136  die();
137  }
138 
139 
140  if (!$plugin->delete()){
141  redirectMsg('plugins.php', __('Plugin could not be uninstalled, please try again.','rmcommon'), 1);
142  die();
143  }
144 
145  if (!$plugin->on_uninstall()){
146  redirectMsg('plugins.php', __('The plugin has been deleted from database, but erros ocurred on this process.', 'rmcommon').'<br />'.$plugin->errors(), 1);
147  die();
148  }
149 
151 
152  redirectMsg('plugins.php', __('Plugin uninstalled succesfully!','rmcommon'), 0);
153 
154 }
155 
156 function update_rm_plugin(){
157 
158  $name = rmc_server_var($_GET, 'plugin', '');
159  if ($name==''){
160  redirectMsg('plugins.php', __('You must specify a existing plugin', 'rmcommon'), 1);
161  die();
162  }
163 
164  $plugin = new RMPlugin($name);
165 
166  if ($plugin->isNew()){
167  redirectMsg('plugins.php', __('Specified plugin is not installed yet!', 'rmcommon'), 1);
168  die();
169  }
170 
171  if (!$plugin->save()){
172  redirectMsg('plugins.php', __('Plugin could not be updated, please try again.','rmcommon'), 1);
173  die();
174  }
175 
176  if (!$plugin->on_update()){
177  redirectMsg('plugins.php', __('The database has been updated, but erros ocurred on this process.', 'rmcommon').'<br />'.$plugin->errors(), 1);
178  die();
179  }
180 
182 
183  redirectMsg('plugins.php', __('Plugin updated succesfully!','rmcommon'), 0);
184 
185 }
186 
187 function activate_rm_plugin($q){
188 
189  $name = rmc_server_var($_GET, 'plugin', '');
190  if ($name==''){
191  redirectMsg('plugins.php', __('You must specify a existing plugin', 'rmcommon'), 1);
192  die();
193  }
194 
195  $plugin = new RMPlugin($name);
196 
197  if ($plugin->isNew()){
198  redirectMsg('plugins.php', __('Specified plugin is not installed yet!', 'rmcommon'), 1);
199  die();
200  }
201 
202  $plugin->setVar('status', $q);
203 
204  if (!$plugin->save()){
205  redirectMsg('plugins.php', __('Plugin could not be updated, please try again.','rmcommon'), 1);
206  die();
207  }
208 
209  if (!$plugin->on_activate()){
210  redirectMsg('plugins.php', __('The database has been updated, but erros ocurred on this process.', 'rmcommon').'<br />'.$plugin->errors(), 1);
211  die();
212  }
213 
215 
216  redirectMsg('plugins.php', __('Plugin status changed succesfully!','rmcommon'), 0);
217 
218 }
219 
221 
222  $name = rmc_server_var($_GET, 'plugin', '');
223  if ($name==''){
224  redirectMsg('plugins.php', __('You must specify a existing plugin', 'rmcommon'), 1);
225  die();
226  }
227 
228  $plugin = new RMPlugin($name);
229 
230  if ($plugin->isNew()){
231  redirectMsg('plugins.php', __('Specified plugin is not installed yet!', 'rmcommon'), 1);
232  die();
233  }
234 
235  if (!$plugin->getVar('status')){
236  redirectMsg('plugins.php', __('Specified plugin is not active!', 'rmcommon'), 1);
237  die();
238  }
239 
241  $settings = RMFunctions::get()->plugin_settings($name, true);
242 
243  $form = new RMForm(sprintf(__('%s configuration','rmcommon'), $plugin->getVar('name')),'frmconfig','plugins.php');
244  $form->addElement(new RMFormHidden('plugin', $plugin->getVar('dir')));
245  $form->addElement(new RMFormHidden('action', 'savesettings'));
246 
247  foreach ($plugin->options() as $config => $option){
248 
249  if (isset($settings[$config])){
250  $option['value'] = $settings[$config];
251  }
252 
253  if (isset($option['separator']) && !empty($option['separator'])){
254  $form->addElement(new RMFormSubTitle($option['separator']['title'], 1, '', $option['separator']['desc']));
255  continue;
256  }
257 
258  switch ($option['fieldtype']){
259  case 'checkbox_groups':
260  case 'group_multi':
261  $ele = new RMFormGroups($option['caption'], 'conf_' . $config, 1, 1, 3, $option['value']);
262  if ($option['desc']!='') $ele->setDescription($option['desc']);
263  $form->addElement($ele);
264  break;
265  case 'radio_groups':
266  $ele = new RMFormGroups($option['caption'], 'conf_' . $config, 0, 1, 3, $option['value']);
267  if ($option['desc']!='') $ele->setDescription($option['desc']);
268  $form->addElement($ele);
269  break;
270  case 'group':
271  case 'select_groups':
272  $ele = new RMFormGroups($option['caption'], 'conf_' . $config, 0, 0, 3, $option['value']);
273  if ($option['desc']!='') $ele->setDescription($option['desc']);
274  $form->addElement($ele);
275  break;
276  case 'select_groups_multi':
277  $ele = new RMFormGroups($option['caption'], 'conf_' . $config, 1, 0, 3, $option['value']);
278  if ($option['desc']!='') $ele->setDescription($option['desc']);
279  $form->addElement($ele);
280  break;
281  case 'editor':
282  if ($rmc_config['editor_type']=='tiny'){
284  $tiny->add_config('elements','conf_' . $config);
285  }
286  $ele = new RMFormEditor($option['caption'], 'conf_' . $config, is_numeric($option['size']) ? '90%' : $option['size'], '300px', $option['value'], '', 1, array('op'));
287  if ($option['desc']!='') $ele->setDescription($option['desc']);
288  $form->addElement($ele);
289  break;
290  case 'theme':
291  case 'select_theme':
292  $ele = new RMFormTheme($option['caption'], 'conf_' . $config, 0, 0, $option['value'], 3);
293  if ($option['desc']!='') $ele->setDescription($option['desc']);
294  $form->addElement($ele);
295  break;
296  case 'theme_multi':
297  case 'select_theme_multi':
298  $ele = new RMFormTheme($option['caption'], 'conf_' . $config, 0, 1, $option['value'], 3);
299  if ($option['desc']!='') $ele->setDescription($option['desc']);
300  $form->addElement($ele);
301  break;
302  case 'checkbox_theme':
303  $ele = new RMFormTheme($option['caption'], 'conf_' . $config, 1, 1, $option['value'], 4);
304  if ($option['desc']!='') $ele->setDescription($option['desc']);
305  $form->addElement($ele);
306  break;
307  case 'select_theme_admin':
308  $ele = new RMFormTheme($option['caption'], 'conf_' . $config, 0, 0, $option['value'], 3, 'GUI');
309  if ($option['desc']!='') $ele->setDescription($option['desc']);
310  $form->addElement($ele);
311  break;
312  case 'yesno':
313  $ele = new RMFormYesNo($option['caption'], 'conf_' . $config, $option['value']);
314  if ($option['desc']!='') $ele->setDescription($option['desc']);
315  $form->addElement($ele);
316  break;
317  case 'email':
318  $ele = new RMFormText($option['caption'], 'conf_' . $config, $option['size']>0 ? $option['size'] : 50, null, $option['value']);
319  if ($option['desc']!='') $ele->setDescription($option['desc']);
320  $form->addElement($ele, false, 'email');
321  break;
322  case 'select':
323  $ele = new RMFormSelect($option['caption'], 'conf_' . $config);
324  if ($option['desc']!='') $ele->setDescription($option['desc']);
325 
326  foreach ($option['options'] as $op => $opvalue){
327  $ele->addOption($opvalue, $op, $opvalue==$option['value'] ? 1 : 0);
328  }
329  $form->addElement($ele);
330  break;
331  case 'select_multi':
332  $ele = new RMFormSelect($option['caption'], 'conf_' . $config.'[]', 1, $option['value']);
333  if ($option['desc']!='') $ele->setDescription($option['desc']);
334 
335  foreach ($option['options'] as $op => $opvalue){
336  $ele->addOption($opvalue, $op);
337  }
338  $form->addElement($ele);
339  break;
340  case 'language':
341  case 'select_language':
342  $ele = new RMFormLanguageField($option['caption'], 'conf_' . $config, 0, 0, $option['value'], 3);
343  if ($option['desc']!='') $ele->setDescription($option['desc']);
344  $form->addElement($ele);
345  break;
346  case 'select_language_multi':
347  $ele = new RMFormLanguageField($option['caption'], 'conf_' . $config, 1, 0, $option['value'], 3);
348  if ($option['desc']!='') $ele->setDescription($option['desc']);
349  $form->addElement($ele);
350  break;
351  case 'checkbox_language':
352  $ele = new RMFormLanguageField($option['caption'], 'conf_' . $config, 1, 1, $option['value'], 3);
353  if ($option['desc']!='') $ele->setDescription($option['desc']);
354  $form->addElement($ele);
355  break;
356  case 'startpage':
357  case 'select_modules':
358  $ele = new RMFormModules($option['caption'], 'conf_' . $config, 0, 0, $option['value'], 3);
359  $ele->setInserted(array('--'=>__('None','rmcommon')));
360  if ($option['desc']!='') $ele->setDescription($option['desc']);
361  $form->addElement($ele);
362  break;
363  case 'select_modules_multi':
364  $ele = new RMFormModules($option['caption'], 'conf_' . $config, 1, 0, $option['value'], 3);
365  $ele->setInserted(array('--'=>__('None','rmcommon')));
366  if ($option['desc']!='') $ele->setDescription($option['desc']);
367  $form->addElement($ele);
368  break;
369  case 'checkbox_modules':
370  $ele = new RMFormModules($option['caption'], 'conf_' . $config, 1, 1, $option['value'], 3);
371  $ele->setInserted(array('--'=>__('None','rmcommon')));
372  if ($option['desc']!='') $ele->setDescription($option['desc']);
373  $form->addElement($ele);
374  break;
375  case 'radio_modules':
376  $ele = new RMFormModules($option['caption'], 'conf_' . $config, 0, 1, $option['value'], 3);
377  $ele->setInserted(array('--'=>__('None','rmcommon')));
378  if ($option['desc']!='') $ele->setDescription($option['desc']);
379  $form->addElement($ele);
380  break;
381  case 'timezone':
382  case 'select_timezone':
383  $ele = new RMFormTimeZoneField($option['caption'], 'conf_' . $config, 0, 0, $option['value'], 3);
384  if ($option['desc']!='') $ele->setDescription($option['desc']);
385  $form->addElement($ele);
386  break;
387  case 'select_timezone_multi':
388  $ele = new RMFormTimeZoneField($option['caption'], 'conf_' . $config, 0, 1, $option['value'], 3);
389  if ($option['desc']!='') $ele->setDescription($option['desc']);
390  $form->addElement($ele);
391  break;
392  case 'checkbox_timezone':
393  $ele = new RMFormTimeZoneField($option['caption'], 'conf_' . $config, 1, 1, $option['value'], 3);
394  if ($option['desc']!='') $ele->setDescription($option['desc']);
395  $form->addElement($ele);
396  break;
397  case 'radio_timezone':
398  $ele = new RMFormTimeZoneField($option['caption'], 'conf_' . $config, 1, 0, $option['value'], 3);
399  if ($option['desc']!='') $ele->setDescription($option['desc']);
400  $form->addElement($ele);
401  break;
402  case 'tplset':
403  $ele = new RMFormSelect($option['caption'], 'conf_' . $config);
404  $tplset_handler =& exm_gethandler('tplset');
405  $tplsetlist =& $tplset_handler->getList();
406  asort($tplsetlist);
407  foreach ($tplsetlist as $key => $name) {
408  $ele->addOption($key, $name, $option['value']==$key ? 1 : 0);
409  }
410  $form->addElement($ele);
411  break;
412  case 'textarea':
413  $ele = new RMFormTextArea($option['caption'], 'conf_' . $config, 5, $option['size']>0 ? $option['size'] : 50, $option['valuetype']=='array' ? $cleaner->htmlspecialchars(implode('|', $option['value'])) : $cleaner->htmlspecialchars($option['value']));
414  if ($option['desc']!='') $ele->setDescription($option['desc']);
415  $form->addElement($ele);
416  break;
417  case 'module_cache':
418  $ele = new RMFormCacheModuleField($option['caption'], 'conf_' . $config, $option['value']);
419  if ($option['desc']!='') $ele->setDescription($option['desc']);
420  $form->addElement($ele);
421  break;
422  case 'user_select':
423  $ele = new RMFormUser($option['caption'], 'conf_' . $config, $form->getName(), $option['value'], 'select', $limit='300', '');
424  $ele->setOnPage("document.forms[0].op.value='config';");
425  if ($option['desc']!='') $ele->setDescription($option['desc']);
426  $form->addElement($ele);
427  break;
428  case 'radio':
429  $ele = new RMFormRadio($option['caption'], 'conf_'.$config, 1);
430  if ($option['desc']!='') $ele->setDescription($option['desc']);
431 
432  foreach ($option['options'] as $op => $opvalue){
433  $ele->addOption($op, $opvalue, $opvalue==$option['value'] ? 1 : 0);
434  }
435  $form->addElement($ele);
436  break;
437  case 'font_select':
438  $ele = new RMFormSelect($option['caption'], 'conf_'.$config, 0, array($option['value']));
439  if ($option['desc']!='') $ele->setDescription($option['desc']);
440  $fonts = EXMLists::getFileListAsArray(ABSPATH.'/rmcommon/fonts/');
441  foreach ($fonts as $font){
442  $info = pathinfo(ABSPATH.'/rmcommon/fonts/'.$font);
443  if (strtoupper($info['extension'])!='TTF') continue;
444  $ele->addOption($font, $font);
445  }
446  $form->addElement($ele);
447  break;
448  case 'select_editor':
449  $ele = new RMFormSelect($option['caption'], 'conf_'.$config, 0, array($option['value']));
450  if ($option['desc']!='') $ele->setDescription($option['desc']);
451  $ele->addOption('tiny', __('Visual Editor','rmcommon'));
452  $ele->addOption('code', __('EXMCode Editor','rmcommon'));
453  $ele->addOption('textarea', __('Simple Editor','rmcommon'));
454  $ele->addOption('html', __('HTML Editor','rmcommon'));
455  $form->addElement($ele);
456  break;
457  case 'textbox':
458  case 'password':
459  default:
460  $ele = new RMFormText($option['caption'], 'conf_' . $config, $option['size']>0 ? $option['size'] : 50, null, $option['valuetype']=='array' ? implode('|', $option['value']) : $option['value'], $option['fieldtype']=='password'? 1: 0);
461  if ($option['desc']!='') $ele->setDescription($option['desc']);
462  $form->addElement($ele, false, $option['valuetype']=='int' || $option['valuetype']=='float' ? 'num' : '');
463  break;
464  }
465  }
466 
467  $ele = new RMFormButtonGroup();
468  $ele->addButton('', __('Save Settings','rmcommon'), 'submit');
469  $ele->addButton('',__('Cancel','rmcommon'), 'button','onclick="history.go(-1);"');
470 
471  $form->addElement($ele);
472 
473  // Other components can add items to database
474  $form = RMEvents::get()->run_event("rmcommon.settings.form", $form, $plugin);
475 
477  xoops_cp_header();
478  $form->display();
479  xoops_cp_footer();
480 
481 }
482 
484  global $xoopsSecurity;
485 
486  $name = rmc_server_var($_POST, 'plugin', '');
487  if ($name==''){
488  redirectMsg('plugins.php', __('You must specify a existing plugin', 'rmcommon'), 1);
489  die();
490  }
491 
492  $plugin = new RMPlugin($name);
493 
494  if ($plugin->isNew()){
495  redirectMsg('plugins.php', __('Specified plugin is not installed yet!', 'rmcommon'), 1);
496  die();
497  }
498 
499  if (!$plugin->getVar('status')){
500  redirectMsg('plugins.php', __('Specified plugin is not active!', 'rmcommon'), 1);
501  die();
502  }
503 
504  if (!$xoopsSecurity->check()){
505  redirectMsg('plugins.php?action=configure&plugin='.$name, __('Session token expired!','rmcommon'), 1);
506  die();
507  }
508 
509  $options = $plugin->options();
510  $db = XoopsDatabaseFactory::getDatabaseConnection();
511  $confs = array();
512  foreach ($options as $k => $option){
513  if (!isset($_POST['conf_'.$k])) continue;
514  $value = $_POST['conf_'.$k];
515  $option['value'] = is_array($value) ? serialize($value) : $value;
516 
517  $db->queryF("UPDATE ".$db->prefix("rmc_settings")." SET value='$option[value]' WHERE element='$name' AND type='plugin' AND name='$k'");
518 
519  }
520 
521  $events = RMEvents::get();
528  $events->run_event('rmcommon.save.plugin.settings', $options, $plugin->getVar('dir'), $plugin);
529 
530  redirectMsg('plugins.php', __('Settings updated!','rmcommon'), 0);
531 
532 }
533 
538 
539  $path = RMCPATH.'/plugins';
540 
541  if (!file_exists($path.'/'.$dir.'/'.strtolower($dir).'-plugin.php')){
542  header("location: plugins.php"); die();
543  }
544 
545  $plugin = new RMPlugin($dir);
546  if ($plugin->isNew()){
547  header("location: plugins.php"); die();
548  }
549 
550  if (!$plugin->get_info('hasmain')){
551  header("location: plugins.php"); die();
552  }
553 
554  $class = ucfirst($dir).'CUPlugin';
555  $plugin = new $class();
556  if (!method_exists($plugin, 'main')){
557  header("location: plugins.php"); die();
558  }
559 
561 
562  $plugin->main();
563 
564 }
565 
566 // Allow to plugins to take control over this section and show their own options
567 RMEvents::get()->run_event('rmcommon.plugins.check.actions');
568 
569 $dir = rmc_server_var($_REQUEST, 'p', '');
570 if ($dir!=''){
572  die();
573 }
574 
575 $action = rmc_server_var($_REQUEST,'action','');
576 
577 switch($action){
578  case 'install':
580  break;
581  case 'uninstall':
583  break;
584  case 'update':
586  break;
587  case 'enable':
589  break;
590  case 'disable':
592  break;
593  case 'configure':
595  break;
596  case 'savesettings':
598  break;
599  default:
600  show_rm_plugins();
601  break;
602 }