XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
modules.php
Go to the documentation of this file.
1 <?php
2 // $Id: modules.php 965 2012-05-28 03:18:09Z 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 define('RMCLOCATION','modules');
12 include_once '../../include/cp_header.php';
13 
14 function show_modules_list(){
15  global $xoopsSecurity;
16 
17  $installed_modules = array();
18 
19  $limit = rmc_server_var($_SESSION, 'mods_limit', 4);
20 
21  include_once XOOPS_ROOT_PATH.'/kernel/module.php';
22 
23  $db = XoopsDatabaseFactory::getDatabaseConnection();
24 
25  $sql = "SELECT * FROM ".$db->prefix("modules")." ORDER BY `name`";
26  $result = $db->query($sql);
27  $installed_dirs = array();
28 
29  while($row = $db->fetchArray($result)){
30  $mod = new XoopsModule();
31  $mod->assignVars($row);
32  $installed_dirs[] = $mod->dirname();
33 
34  if (file_exists(XOOPS_ROOT_PATH.'/modules/'.$mod->getVar('dirname').'/class/'.strtolower($mod->getVar('dirname').'controller').'.php')){
35  include_once XOOPS_ROOT_PATH.'/modules/'.$mod->getVar('dirname').'/class/'.strtolower($mod->getVar('dirname').'controller').'.php';
36  $class = ucfirst($mod->getVar('dirname')).'Controller';
37  $class = new $class();
38  if (method_exists($class, 'get_main_link')){
39  $main_link = $class->get_main_link();
40  } else {
41 
42  if ($mod->getVar('hasmain')){
43  $main_link = XOOPS_URL.'/modules/'.$mod->dirname();
44  } else {
45  $main_link = "#";
46  }
47 
48  }
49  } else {
50 
51  if ($mod->getVar('hasmain')){
52  $main_link = XOOPS_URL.'/modules/'.$mod->dirname();
53  } else {
54  $main_link = "#";
55  }
56 
57  }
58 
59  // Admin section
60  $admin_link = $mod->getVar('hasadmin') ? XOOPS_URL.'/modules/'.$mod->dirname().'/'.$mod->getInfo('adminindex') : '';
61 
62  $modules[] = array(
63  'id' => $mod->getVar('mid'),
64  'name' => $mod->getVar('name'),
65  'realname' => $mod->getInfo('name'),
66  'version' => $mod->getInfo('rmnative') ? RMUtilities::format_version($mod->getInfo('rmversion')) : $mod->getInfo('version'),
67  'description' => $mod->getInfo('description'),
68  'icon' => XOOPS_URL.'/modules/'.$mod->getVar('dirname').'/'.($mod->getInfo('icon48') ? $mod->getInfo('icon48') : $mod->getInfo('image')),
69  'image' => XOOPS_URL.'/modules/'.$mod->getVar('dirname').'/'.$mod->getInfo('image'),
70  'link' => $main_link,
71  'admin_link' => $admin_link,
72  'updated' => formatTimestamp($mod->getVar('last_update'), 's'),
73  'author' => $mod->getInfo('author'),
74  'author_mail' => $mod->getInfo('authormail'),
75  'author_web' => $mod->getInfo('authorweb'),
76  'author_url' => $mod->getInfo('authorurl'),
77  'license' => $mod->getInfo('license'),
78  'dirname' => $mod->getInfo('dirname'),
79  'active' => $mod->getVar('isactive'),
80  'help' => $mod->getInfo('help')
81  );
82  }
83 
84  // Event for installed modules
85  $modules = RMEvents::get()->run_event('rmcommon.installed.modules', $modules, $installed_dirs);
86 
87  require_once XOOPS_ROOT_PATH . "/class/xoopslists.php";
88  $dirlist = XoopsLists::getModulesList();
89  $available_mods = array();
90  $module_handler = xoops_gethandler('module');
91  foreach ($dirlist as $file) {
92  clearstatcache();
93  $file = trim($file);
94  if (!in_array($file, $installed_dirs)) {
95  $module =& $module_handler->create();
96  if (!$module->loadInfo($file, false)) {
97  continue;
98  }
99  $available_mods[] = $module;
100  unset($module);
101  }
102  }
103 
104  // Event for available modules
105  $available_mods = RMEvents::get()->run_event('rmcommon.available.modules', $available_mods);
106 
107  $GLOBALS['available_mods'] = $available_mods;
108 
110  RMTemplate::get()->assign('xoops_pagetitle', __('Modules Management','rmcommon'));
111  RMTemplate::get()->add_style('modules.css', 'rmcommon');
112  RMTemplate::get()->add_local_script('modules.js', 'rmcommon', 'include');
113  RMTemplate::get()->set_help('http://www.redmexico.com.mx/docs/common-utilities/uso-de-common-utilities/standalone/1/#administrador-de-modulos');
114  xoops_cp_header();
115  include RMTemplate::get()->get_template('rmc_modules.php', 'module', 'rmcommon');
116  xoops_cp_footer();
117 
118 }
119 
123 function module_install(){
124  global $xoopsSecurity;
125 
126  $dir = rmc_server_var($_GET,'dir','');
127 
128  if ($dir=='' || !file_exists(XOOPS_ROOT_PATH.'/modules/'.$dir.'/xoops_version.php')){
129  redirectMsg('modules.php', __('Specified module is not valid!','rmcommon'), 1);
130  die();
131  }
132 
133  $module_handler = xoops_gethandler('module');
134 
135  if ($module = $module_handler->getByDirname($dir)){
136  redirectMsg('modules.php', sprintf(__('%s is already installed!', 'rmcommon'), $module->name()), 1);
137  die();
138  }
139 
140  $module =& $module_handler->create();
141  if (!$module->loadInfo($dir, false)) {
142  redirectMsg('modules.php',__('Sepecified module is not a valid Xoops Module!','rmcommon'), 1);
143  die();
144  }
145 
146  $module = RMEvents::get()->run_event('rmcommon.preinstall.module', $module);
147 
148  RMTEmplate::get()->add_script('include/js/modules.js');
149  RMTemplate::get()->add_style('modules.css', 'rmcommon');
151  xoops_cp_header();
152 
153  include RMTemplate::get()->get_template('rmc_mod_preinstall.php', 'module', 'rmcommon');
154 
155  xoops_cp_footer();
156 
157 }
158 
161 
162  $mod = rmc_server_var($_POST, 'module', '');
163 
164  if (!$xoopsSecurity->check()){
165  redirectMsg('modules.php?action=install&dir='.$module, __('Sorry, this operation could not be completed!', 'rmcommon'), 1);
166  die();
167  }
168 
169  $module_handler = xoops_gethandler('module');
170 
171  if ($module = $module_handler->getByDirname($mod)){
172  redirectMsg('modules.php', sprintf(__('%s is already installed!', 'rmcommon'), $module->name()), 1);
173  die();
174  }
175 
176  $file = XOOPS_ROOT_PATH.'/modules/system/language/'.$xoopsConfig['language'].'/admin/modulesadmin.php';
177  if (file_exists($file)){
178  include_once $file;
179  } else {
180  include_once str_replace($xoopsConfig['language'], 'english', $file);
181  }
182 
183  $file = XOOPS_ROOT_PATH.'/modules/system/language/'.$xoopsConfig['language'].'/admin/admin.php';
184  if (file_exists($file)){
185  include_once $file;
186  } else {
187  include_once str_replace($xoopsConfig['language'], 'english', $file);
188  }
189 
190 
191  include_once XOOPS_ROOT_PATH.'/modules/system/admin/modulesadmin/modulesadmin.php';
192 
193  RMEvents::get()->run_event('rmcommon.installing.module', $mod);
194 
195  $module_log = xoops_module_install($mod);
196 
197  $module_log = RMEvents::get()->run_event('rmcommon.module.installed', $module_log, $mod);
198 
200  RMTemplate::get()->add_style('modules.css', 'rmcommon');
201  xoops_cp_header();
202 
203  $module = $module_handler->getByDirname($mod);
204  $log_title = sprintf(__('Installation log for %s', 'rmcommon'), $module ? $module->name() : $mod);
205  $action = rmc_server_var($_POST, 'action', '');
206  include RMTemplate::get()->get_template('rmc_mod_log.php','module','rmcommon');
207 
208  xoops_cp_footer();
209 
210 }
211 
212 
215 
216  $mod = rmc_server_var($_POST, 'module', '');
217 
218  if (!$xoopsSecurity->check()){
219  redirectMsg('modules.php', __('Sorry, this operation could not be completed!', 'rmcommon'), 1);
220  die();
221  }
222 
223  $module_handler = xoops_gethandler('module');
224 
225  if (!$module_handler->getByDirname($mod)){
226  redirectMsg('modules.php', sprintf(__('Module %s is not installed yet!', 'rmcommon'), $mod), 1);
227  die();
228  }
229 
230  $file = XOOPS_ROOT_PATH.'/modules/system/language/'.$xoopsConfig['language'].'/admin/modulesadmin.php';
231  if (file_exists($file)){
232  include_once $file;
233  } else {
234  include_once str_replace($xoopsConfig['language'], 'english', $file);
235  }
236 
237  include_once XOOPS_ROOT_PATH.'/modules/system/admin/modulesadmin/modulesadmin.php';
238 
239  RMEvents::get()->run_event('rmcommon.uninstalling.module', $mod);
240 
241  $module_log = xoops_module_uninstall($mod);
242 
243  $module_log = RMEvents::get()->run_event('rmcommon.module.uninstalled', $module_log, $mod);
244 
246  RMTemplate::get()->add_style('modules.css', 'rmcommon');
247  xoops_cp_header();
248 
249  $module = new XoopsModule();
250  $module->loadInfo($mod, false);
251  $log_title = sprintf(__('Uninstall log for %s', 'rmcommon'), $module ? $module->getInfo('name') : $mod);
252  $action = rmc_server_var($_POST, 'action', '');
253  include RMTemplate::get()->get_template('rmc_mod_log.php','module','rmcommon');
254 
255  xoops_cp_footer();
256 
257 }
258 
259 
260 function xoops_module_update($dirname){
261  global $xoopsConfig, $xoopsDB;
262 
263  $dirname = trim($dirname);
264  $module_handler =& xoops_gethandler('module');
265  $module =& $module_handler->getByDirname($dirname);
266  // Save current version for use in the update function
267  $prev_version = $module->getVar('version');
268  include_once XOOPS_ROOT_PATH.'/class/template.php';
269  $xoopsTpl = new XoopsTpl();
270  $xoopsTpl->clearCache($dirname);
271  //xoops_template_clear_module_cache($module->getVar('mid'));
272  // we dont want to change the module name set by admin
273  $temp_name = $module->getVar('name');
274  $module->loadInfoAsVar($dirname);
275  $module->setVar('name', $temp_name);
276 
277  $log = '';
278  if (!$module_handler->insert($module)) {
279  $log .= sprintf(__('Could not update %s','rmcommon'), $module->getVar('name'));
280  } else {
281  $newmid = $module->getVar('mid');
282  $msgs = array();
283  $msgs[] = sprintf(__('Updating module %s','rmcommon'), $module->getVar('name'));
284  $tplfile_handler =& xoops_gethandler('tplfile');
285  $deltpl = $tplfile_handler->find('default', 'module', $module->getVar('mid'));
286  $delng = array();
287  if (is_array($deltpl)) {
288  // delete template file entry in db
289  $dcount = count($deltpl);
290  for ($i = 0; $i < $dcount; $i++) {
291  if (!$tplfile_handler->delete($deltpl[$i])) {
292  $delng[] = $deltpl[$i]->getVar('tpl_file');
293  }
294  }
295  }
296  $templates = $module->getInfo('templates');
297  if ($templates != false) {
298  $msgs[] = __('Updating templates...','rmcommon');
299  foreach ($templates as $tpl) {
300  $tpl['file'] = trim($tpl['file']);
301  if (!in_array($tpl['file'], $delng)) {
302  $tpldata =& xoops_module_gettemplate($dirname, $tpl['file']);
303  $tplfile =& $tplfile_handler->create();
304  $tplfile->setVar('tpl_refid', $newmid);
305  $tplfile->setVar('tpl_lastimported', 0);
306  $tplfile->setVar('tpl_lastmodified', time());
307  if (preg_match("/\.css$/i", $tpl['file'])) {
308  $tplfile->setVar('tpl_type', 'css');
309  } else {
310  $tplfile->setVar('tpl_type', 'module');
311  }
312  $tplfile->setVar('tpl_source', $tpldata, true);
313  $tplfile->setVar('tpl_module', $dirname);
314  $tplfile->setVar('tpl_tplset', 'default');
315  $tplfile->setVar('tpl_file', $tpl['file'], true);
316  $tplfile->setVar('tpl_desc', $tpl['description'], true);
317  if (!$tplfile_handler->insert($tplfile)) {
318  $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">'.sprintf(__('Template %s could not be inserted!','rmcommon'), "<strong>".$tpl['file']."</strong>").'</span>';
319  } else {
320  $newid = $tplfile->getVar('tpl_id');
321  $msgs[] = '&nbsp;&nbsp;'.sprintf(__('Template %s inserted to the database.','rmcommon'), "<strong>".$tpl['file']."</strong>");
322  if ($xoopsConfig['template_set'] == 'default') {
323  if (!xoops_template_touch($newid)) {
324  $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">'.sprintf(__('ERROR: Could not recompile template %s','rmcommon'), "<strong>".$tpl['file']."</strong>").'</span>';
325  } else {
326  $msgs[] = '&nbsp;&nbsp;<span>'.sprintf(__('Template %s recompiled','rmcommon'), "<strong>".$tpl['file']."</strong>").'</span>';
327  }
328  }
329  }
330  unset($tpldata);
331  } else {
332  $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">'.sprintf(__('ERROR: Could not delete old template %s. Aborting update of this file.','rmcommon'), "<strong>".$tpl['file']."</strong>").'</span>';
333  }
334  }
335  }
336  $blocks = $module->getInfo('blocks');
337  $msgs[] = __('Rebuilding blocks...','rmcommon');
338  if ($blocks != false) {
339  $showfuncs = array();
340  $funcfiles = array();
341  foreach ($blocks as $i => $block) {
342  if (isset($block['show_func']) && $block['show_func'] != '' && isset($block['file']) && $block['file'] != '') {
343  $editfunc = isset($block['edit_func']) ? $block['edit_func'] : '';
344  $showfuncs[] = $block['show_func'];
345  $funcfiles[] = $block['file'];
346  $template = '';
347  if ((isset($block['template']) && trim($block['template']) != '')) {
348  $content = xoops_module_gettemplate($dirname, $block['template'], 'blocks');
349  }
350 
351  if (!$content) {
352  $content = '';
353  } else {
354  $template = $block['template'];
355  }
356  $options = '';
357  if (!empty($block['options'])) {
358  $options = $block['options'];
359  }
360  $sql = "SELECT bid, name FROM ".$xoopsDB->prefix('newblocks')." WHERE mid=".$module->getVar('mid')." AND func_num=".$i." AND show_func='".addslashes($block['show_func'])."' AND func_file='".addslashes($block['file'])."'";
361  $fresult = $xoopsDB->query($sql);
362  $fcount = 0;
363  while ($fblock = $xoopsDB->fetchArray($fresult)) {
364  $fcount++;
365  $sql = "UPDATE ".$xoopsDB->prefix("newblocks")." SET name='".addslashes($block['name'])."', edit_func='".addslashes($editfunc)."', content='', template='".$template."', last_modified=".time()." WHERE bid=".$fblock['bid'];
366  $result = $xoopsDB->query($sql);
367  if (!$result) {
368  $msgs[] = "&nbsp;&nbsp;".sprintf(__('ERROR: Could not update %s'), $fblock['name']);
369  } else {
370  $msgs[] = "&nbsp;&nbsp;".sprintf(__('Block %s updated.','rmcommon'), $fblock['name']).sprintf(__('Block ID: %s','rmcommon'), "<strong>".$fblock['bid']."</strong>");
371  if ($template != '') {
372  $tplfile = $tplfile_handler->find('default', 'block', $fblock['bid']);
373  if (count($tplfile) == 0) {
374  $tplfile_new =& $tplfile_handler->create();
375  $tplfile_new->setVar('tpl_module', $dirname);
376  $tplfile_new->setVar('tpl_refid', $fblock['bid']);
377  $tplfile_new->setVar('tpl_tplset', 'default');
378  $tplfile_new->setVar('tpl_file', $block['template'], true);
379  $tplfile_new->setVar('tpl_type', 'block');
380  } else {
381  $tplfile_new = $tplfile[0];
382  }
383  $tplfile_new->setVar('tpl_source', $content, true);
384  $tplfile_new->setVar('tpl_desc', $block['description'], true);
385  $tplfile_new->setVar('tpl_lastmodified', time());
386  $tplfile_new->setVar('tpl_lastimported', 0);
387  if (!$tplfile_handler->insert($tplfile_new)) {
388  $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">'.sprintf(__('ERROR: Could not update %s template.','rmcommon'), "<strong>".$block['template']."</strong>").'</span>';
389  } else {
390  $msgs[] = "&nbsp;&nbsp;".sprintf(__('Template %s updated.','rmcommon'), "<strong>".$block['template']."</strong>");
391  if ($xoopsConfig['template_set'] == 'default') {
392  if (!xoops_template_touch($tplfile_new->getVar('tpl_id'))) {
393  $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">'.sprintf(__('ERROR: Could not recompile template %s','rmcommon'), "<strong>".$block['template']."</strong>").'</span>';
394  } else {
395  $msgs[] = "&nbsp;&nbsp;".sprintf(__('Template %s recompiled','rmcommon'), "<strong>".$block['template']."</strong>");
396  }
397  }
398 
399  }
400  }
401  }
402  }
403  if ($fcount == 0) {
404  $newbid = $xoopsDB->genId($xoopsDB->prefix('newblocks').'_bid_seq');
405  $block_name = addslashes($block['name']);
406  $block_type = ($module->getVar('dirname') == 'system') ? 'S' : 'M';
407  $sql = "INSERT INTO ".$xoopsDB->prefix("newblocks")." (bid, mid, func_num, options, name, title, content, side, weight, visible, block_type, isactive, dirname, func_file, show_func, edit_func, template, last_modified) VALUES (".$newbid.", ".$module->getVar('mid').", ".$i.",'".addslashes($options)."','".$block_name."', '".$block_name."', '', 0, 0, 0, '{$block_type}', 1, '".addslashes($dirname)."', '".addslashes($block['file'])."', '".addslashes($block['show_func'])."', '".addslashes($editfunc)."', '".$template."', ".time().")";
408  $result = $xoopsDB->query($sql);
409  if (!$result) {
410  $msgs[] = '&nbsp;&nbsp;'.sprintf(_('ERROR: Could not create %s','rmcommon'), $block['name']);$log .= $sql;
411  } else {
412  if (empty($newbid)) {
413  $newbid = $xoopsDB->getInsertId();
414  }
415  if ($module->getInfo('hasMain')) {
416  $groups = array(XOOPS_GROUP_ADMIN, XOOPS_GROUP_USERS, XOOPS_GROUP_ANONYMOUS);
417  } else {
418  $groups = array(XOOPS_GROUP_ADMIN);
419  }
420  $gperm_handler =& xoops_gethandler('groupperm');
421  foreach ($groups as $mygroup) {
422  $bperm =& $gperm_handler->create();
423  $bperm->setVar('gperm_groupid', $mygroup);
424  $bperm->setVar('gperm_itemid', $newbid);
425  $bperm->setVar('gperm_name', 'block_read');
426  $bperm->setVar('gperm_modid', 1);
427  if (!$gperm_handler->insert($bperm)) {
428  $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">'.__('ERROR: Could not add block access right','rmcommon') .' '.sprintf(__("Block ID: %s",'rmcommon'), "<strong>".$newbid."</strong>"). ' '.sprintf(__('Group ID: %s','rmcommon'), "<strong>".$mygroup."</strong>").'</span>';
429  } else {
430  $msgs[] = '&nbsp;&nbsp;'.__('Added block access right','rmcommon'). ' ' . sprintf(__("Block ID: %s",'rmcommon'), "<strong>".$newbid."</strong>") . ' ' . sprintf(__('Group ID: %s','rmcommon'), "<strong>".$mygroup."</strong>");
431  }
432  }
433 
434  if ($template != '') {
435  $tplfile =& $tplfile_handler->create();
436  $tplfile->setVar('tpl_module', $dirname);
437  $tplfile->setVar('tpl_refid', $newbid);
438  $tplfile->setVar('tpl_source', $content, true);
439  $tplfile->setVar('tpl_tplset', 'default');
440  $tplfile->setVar('tpl_file', $block['template'], true);
441  $tplfile->setVar('tpl_type', 'block');
442  $tplfile->setVar('tpl_lastimported', 0);
443  $tplfile->setVar('tpl_lastmodified', time());
444  $tplfile->setVar('tpl_desc', $block['description'], true);
445  if (!$tplfile_handler->insert($tplfile)) {
446  $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">'.sprintf(__('ERROR: Could not insert template %s to the database.','rmcommon'), "<strong>".$block['template']."</strong>").'</span>';
447  } else {
448  $newid = $tplfile->getVar('tpl_id');
449  $msgs[] = '&nbsp;&nbsp;'.sprintf(__('Template %s added to the database','rmcommon'), "<strong>".$block['template']."</strong>");
450  if ($xoopsConfig['template_set'] == 'default') {
451  if (!xoops_template_touch($newid)) {
452  $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">'.sprintf(__('ERROR: Template %s recompile failed','rmcommon'), "<strong>".$block['template']."</strong>").'</span>';
453  } else {
454  $msgs[] = '&nbsp;&nbsp;'.sprintf(__('Template %s recompiled','rmcommon'), "<strong>".$block['template']."</strong>");
455  }
456  }
457  }
458  }
459  $msgs[] = '&nbsp;&nbsp;'.sprintf(__('Block %s created','rmcommon'), "<strong>".$block['name']."</strong>").sprintf(__("Block ID: %s",'rmcommon'), "<strong>".$newbid."</strong>");
460  $sql = 'INSERT INTO '.$xoopsDB->prefix('block_module_link').' (block_id, module_id) VALUES ('.$newbid.', -1)';
461  $xoopsDB->query($sql);
462  }
463  }
464  }
465  }
466  $block_arr = XoopsBlock::getByModule($module->getVar('mid'));
467  foreach ($block_arr as $block) {
468  if (!in_array($block->getVar('show_func'), $showfuncs) || !in_array($block->getVar('func_file'), $funcfiles)) {
469  $sql = sprintf("DELETE FROM %s WHERE bid = %u", $xoopsDB->prefix('newblocks'), $block->getVar('bid'));
470  if(!$xoopsDB->query($sql)) {
471  $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">'.sprintf(__('ERROR: Could not delete block %s','rmcommon'), "<strong>".$block->getVar('name')."</strong>").sprintf(__("Block ID: %s",'rmcommon'), "<strong>".$block->getVar('bid')."</strong>").'</span>';
472  } else {
473  $msgs[] = '&nbsp;&nbsp;Block <strong>'.$block->getVar('name').' deleted. Block ID: <strong>'.$block->getVar('bid').'</strong>';
474  if ($block->getVar('template') != '') {
475  $tplfiles = $tplfile_handler->find(null, 'block', $block->getVar('bid'));
476  if (is_array($tplfiles)) {
477  $btcount = count($tplfiles);
478  for ($k = 0; $k < $btcount; $k++) {
479  if (!$tplfile_handler->delete($tplfiles[$k])) {
480  $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">'.__('ERROR: Could not remove deprecated block template.','rmcommon'). '(ID: <strong>'.$tplfiles[$k]->getVar('tpl_id').'</strong>)</span>';
481  } else {
482  $msgs[] = '&nbsp;&nbsp;'.sprintf(__('Block template %s deprecated','rmcommon'), "<strong>".$tplfiles[$k]->getVar('tpl_file')."</strong>");
483  }
484  }
485  }
486  }
487  }
488  }
489  }
490  }
491 
492  // reset compile_id
493  $xoopsTpl->setCompileId();
494 
495  // first delete all config entries
496  $config_handler =& xoops_gethandler('config');
497  $configs = $config_handler->getConfigs(new Criteria('conf_modid', $module->getVar('mid')));
498  $confcount = count($configs);
499  $config_delng = array();
500  if ($confcount > 0) {
501  $msgs[] = __('Deleting module config options...','rmcommon');
502  for ($i = 0; $i < $confcount; $i++) {
503  if (!$config_handler->deleteConfig($configs[$i])) {
504  $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">'.__('ERROR: Could not delete config data from the database','rmcommon'). sprintf(__('Config ID: %s','rmcommon'), "<strong>".$configs[$i]->getvar('conf_id')."</strong>").'</span>';
505  // save the name of config failed to delete for later use
506  $config_delng[] = $configs[$i]->getvar('conf_name');
507  } else {
508  $config_old[$configs[$i]->getvar('conf_name')]['value'] = $configs[$i]->getvar('conf_value', 'N');
509  $config_old[$configs[$i]->getvar('conf_name')]['formtype'] = $configs[$i]->getvar('conf_formtype');
510  $config_old[$configs[$i]->getvar('conf_name')]['valuetype'] = $configs[$i]->getvar('conf_valuetype');
511  $msgs[] = "&nbsp;&nbsp;".__('Config data deleted from the database.','rmcommon'). ' ' . sprintf(__('Config ID: %s','rmcommon'), "<strong>".$configs[$i]->getVar('conf_id')."</strong>");
512  }
513  }
514  }
515 
516  // now reinsert them with the new settings
517  $configs = $module->getInfo('config');
518  // Include
519  if ($configs != false) {
520  if ($module->getVar('hascomments') != 0) {
521  include_once(XOOPS_ROOT_PATH.'/include/comment_constants.php');
522  array_push($configs, array('name' => 'com_rule', 'title' => '_CM_COMRULES', 'description' => '', 'formtype' => 'select', 'valuetype' => 'int', 'default' => 1, 'options' => array('_CM_COMNOCOM' => XOOPS_COMMENT_APPROVENONE, '_CM_COMAPPROVEALL' => XOOPS_COMMENT_APPROVEALL, '_CM_COMAPPROVEUSER' => XOOPS_COMMENT_APPROVEUSER, '_CM_COMAPPROVEADMIN' => XOOPS_COMMENT_APPROVEADMIN)));
523  array_push($configs, array('name' => 'com_anonpost', 'title' => '_CM_COMANONPOST', 'description' => '', 'formtype' => 'yesno', 'valuetype' => 'int', 'default' => 0));
524  }
525  } else {
526  if ($module->getVar('hascomments') != 0) {
527  $configs = array();
528  include_once(XOOPS_ROOT_PATH.'/include/comment_constants.php');
529  $configs[] = array('name' => 'com_rule', 'title' => '_CM_COMRULES', 'description' => '', 'formtype' => 'select', 'valuetype' => 'int', 'default' => 1, 'options' => array('_CM_COMNOCOM' => XOOPS_COMMENT_APPROVENONE, '_CM_COMAPPROVEALL' => XOOPS_COMMENT_APPROVEALL, '_CM_COMAPPROVEUSER' => XOOPS_COMMENT_APPROVEUSER, '_CM_COMAPPROVEADMIN' => XOOPS_COMMENT_APPROVEADMIN));
530  $configs[] = array('name' => 'com_anonpost', 'title' => '_CM_COMANONPOST', 'description' => '', 'formtype' => 'yesno', 'valuetype' => 'int', 'default' => 0);
531  }
532  }
533  // RMV-NOTIFY
534  if ($module->getVar('hasnotification') != 0) {
535  if (empty($configs)) {
536  $configs = array();
537  }
538  // Main notification options
539  include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
540  include_once XOOPS_ROOT_PATH . '/include/notification_functions.php';
541  $options = array();
542  $options['_NOT_CONFIG_DISABLE'] = XOOPS_NOTIFICATION_DISABLE;
543  $options['_NOT_CONFIG_ENABLEBLOCK'] = XOOPS_NOTIFICATION_ENABLEBLOCK;
544  $options['_NOT_CONFIG_ENABLEINLINE'] = XOOPS_NOTIFICATION_ENABLEINLINE;
545  $options['_NOT_CONFIG_ENABLEBOTH'] = XOOPS_NOTIFICATION_ENABLEBOTH;
546 
547  //$configs[] = array ('name' => 'notification_enabled', 'title' => '_NOT_CONFIG_ENABLED', 'description' => '_NOT_CONFIG_ENABLEDDSC', 'formtype' => 'yesno', 'valuetype' => 'int', 'default' => 1);
548  $configs[] = array ('name' => 'notification_enabled', 'title' => '_NOT_CONFIG_ENABLE', 'description' => '_NOT_CONFIG_ENABLEDSC', 'formtype' => 'select', 'valuetype' => 'int', 'default' => XOOPS_NOTIFICATION_ENABLEBOTH, 'options'=>$options);
549  // Event specific notification options
550  // FIXME: for some reason the default doesn't come up properly
551  // initially is ok, but not when 'update' module..
552  $options = array();
553  $categories =& notificationCategoryInfo('',$module->getVar('mid'));
554  foreach ($categories as $category) {
555  $events =& notificationEvents ($category['name'], false, $module->getVar('mid'));
556  foreach ($events as $event) {
557  if (!empty($event['invisible'])) {
558  continue;
559  }
560  $option_name = $category['title'] . ' : ' . $event['title'];
561  $option_value = $category['name'] . '-' . $event['name'];
562  $options[$option_name] = $option_value;
563  //$configs[] = array ('name' => notificationGenerateConfig($category,$event,'name'), 'title' => notificationGenerateConfig($category,$event,'title_constant'), 'description' => notificationGenerateConfig($category,$event,'description_constant'), 'formtype' => 'yesno', 'valuetype' => 'int', 'default' => 1);
564  }
565  }
566  $configs[] = array ('name' => 'notification_events', 'title' => '_NOT_CONFIG_EVENTS', 'description' => '_NOT_CONFIG_EVENTSDSC', 'formtype' => 'select_multi', 'valuetype' => 'array', 'default' => array_values($options), 'options' => $options);
567  }
568 
569  if ($configs != false) {
570  $msgs[] = 'Adding module config data...';
571  $config_handler =& xoops_gethandler('config');
572  $order = 0;
573  foreach ($configs as $config) {
574  // only insert ones that have been deleted previously with success
575  if (!in_array($config['name'], $config_delng)) {
576  $confobj =& $config_handler->createConfig();
577  $confobj->setVar('conf_modid', $newmid);
578  $confobj->setVar('conf_catid', 0);
579  $confobj->setVar('conf_name', $config['name']);
580  $confobj->setVar('conf_title', $config['title'], true);
581  $confobj->setVar('conf_desc', $config['description'], true);
582  $confobj->setVar('conf_formtype', $config['formtype']);
583  $confobj->setVar('conf_valuetype', $config['valuetype']);
584  if (isset($config_old[$config['name']]['value']) && $config_old[$config['name']]['formtype'] == $config['formtype'] && $config_old[$config['name']]['valuetype'] == $config['valuetype']) {
585  // preserver the old value if any
586  // form type and value type must be the same
587  $confobj->setVar('conf_value', $config_old[$config['name']]['value'], true);
588  } else {
589  $confobj->setConfValueForInput($config['default'], true);
590 
591  //$confobj->setVar('conf_value', $config['default'], true);
592  }
593  $confobj->setVar('conf_order', $order);
594  $confop_msgs = '';
595  if (isset($config['options']) && is_array($config['options'])) {
596  foreach ($config['options'] as $key => $value) {
597  $confop =& $config_handler->createConfigOption();
598  $confop->setVar('confop_name', $key, true);
599  $confop->setVar('confop_value', $value, true);
600  $confobj->setConfOptions($confop);
601  $confop_msgs .= '<br />&nbsp;&nbsp;&nbsp;&nbsp; ' . __('Config option added','rmcommon') . ' ' . __('Name:','rmcommon') . ' <strong>' . ( defined($key) ? constant($key) : $key ) . '</strong> ' . __('Value:','rmcommon') . ' <strong>' . $value . '</strong> ';
602  unset($confop);
603  }
604  }
605  $order++;
606  if (false != $config_handler->insertConfig($confobj)) {
607  //$msgs[] = '&nbsp;&nbsp;Config <strong>'.$config['name'].'</strong> added to the database.'.$confop_msgs;
608  $msgs[] = "&nbsp;&nbsp;".sprintf(__('Config %s added to the database','rmcommon'), "<strong>" . $config['name'] . "</strong>") . $confop_msgs;
609  } else {
610  $msgs[] = '&nbsp;&nbsp;<span style="color:#ff0000;">' . sprintf(__('ERROR: Could not insert config %s to the database.','rmcommon'), "<strong>" . $config['name'] . "</strong>") . '</span>';
611  }
612  unset($confobj);
613  }
614  }
615  unset($configs);
616  }
617 
618  // execute module specific update script if any
619  $update_script = $module->getInfo('onUpdate');
620  if (false != $update_script && trim($update_script) != '') {
621  include_once XOOPS_ROOT_PATH.'/modules/'.$dirname.'/'.trim($update_script);
622  if (function_exists('xoops_module_update_'.$dirname)) {
623  $func = 'xoops_module_update_'.$dirname;
624  if (!$func($module, $prev_version)) {
625  $msgs[] = "<p>".sprintf(__('Failed to execute %s','rmcommon'), $func)."</p>";
626  } else {
627  $msgs[] = "<p>".sprintf(__('%s executed successfully.','rmcommon'), "<strong>".$func."</strong>")."</p>";
628  }
629  }
630  }
631 
632  foreach ($msgs as $msg) {
633  $log .= $msg.'<br />';
634  }
635  $log .= "<p>".sprintf(__('Module %s updated successfully!','rmcommon'), "<strong>".$module->getVar('name')."</strong>")."</p>";
636  }
637 
638  // Flush cache files for cpanel GUIs
639  xoops_load("cpanel", "system");
640  XoopsSystemCpanel::flush();
641 
642  return $log;
643 
644 }
645 
646 
647 function module_update_now(){
648 
650 
651  $mod = rmc_server_var($_POST, 'module', '');
652 
653  if (!$xoopsSecurity->check()){
654  redirectMsg('modules.php', __('Sorry, this operation could not be completed!', 'rmcommon'), 1);
655  die();
656  }
657 
658  $module_handler = xoops_gethandler('module');
659  $module = $module_handler->getByDirname($mod);
660 
661  if (!$module){
662  redirectMsg('modules.php', sprintf(__('Module %s is not installed yet!', 'rmcommon'), $mod), 1);
663  die();
664  }
665 
666  $file = XOOPS_ROOT_PATH.'/modules/system/language/'.$xoopsConfig['language'].'/admin/modulesadmin.php';
667  if (file_exists($file)){
668  include_once $file;
669  } else {
670  include_once str_replace($xoopsConfig['language'], 'english', $file);
671  }
672 
673  include_once XOOPS_ROOT_PATH.'/modules/system/admin/modulesadmin/modulesadmin.php';
674 
675  RMEvents::get()->run_event('rmcommon.updating.module', $module);
676 
677  $module_log = xoops_module_update($mod);
678 
679  $module_log = RMEvents::get()->run_event('rmcommon.module.updated', $module_log, $module_log);
680 
682  RMTemplate::get()->add_style('modules.css', 'rmcommon');
683  xoops_cp_header();
684 
685  $log_title = sprintf(__('Update log for %s', 'rmcommon'), $module ? $module->getInfo('name') : $mod);
686  $action = rmc_server_var($_POST, 'action', '');
687  include RMTemplate::get()->get_template('rmc_mod_log.php','module','rmcommon');
688 
689  xoops_cp_footer();
690 
691 }
692 
693 
694 function module_disable_now($enable=0){
696 
697  $mod = rmc_server_var($_POST, 'module', '');
698 
699  if (!$xoopsSecurity->check()){
700  redirectMsg('modules.php', __('Sorry, this operation could not be completed!', 'rmcommon'), 1);
701  die();
702  }
703 
704  $module_handler = xoops_gethandler('module');
705 
706  if (!$module = $module_handler->getByDirname($mod)){
707  redirectMsg('modules.php', sprintf(__('Module %s is not installed yet!', 'rmcommon'), $mod), 1);
708  die();
709  }
710 
711  RMEvents::get()->run_event('rmcommon.disabling.module', $module);
712 
713  $db = XoopsDatabaseFactory::getDatabaseConnection();
714  $sql = "UPDATE ".$db->prefix("modules")." SET isactive='$enable' WHERE dirname='$mod'";
715  if (!$db->queryF($sql)){
716  redirectMsg('modules.php', sprintf(__('Module %s could not be disabled!', 'rmcommon'), $mod), 1);
717  die();
718  }
719 
720  RMEvents::get()->run_event('rmcommon.module.disabled', $module);
721 
722  redirectMsg("modules.php", sprintf(__('Module %s successfully disabled!','rmcommon'), $module->name()), 0);
723 
724 }
725 
726 
727 function load_modules_page(){
729  error_reporting(0);
730  $xoopsLogger->activated = false;
731 
732  if (!$xoopsSecurity->check(true, rmc_server_var($_POST, 'token', ''))){
733  echo __("Sorry, you don't have access to this page", 'rmcommon');
734  echo "<br /><a href='javascript:;' onclick='location.reload();'>".__('Click here to refresh','rmcommon')."</a>";
735  die();
736  }
737 
738  $db = XoopsDatabaseFactory::getDatabaseConnection();
739  $sql = "SELECT * FROM ".$db->prefix("modules")." ORDER BY `name`";
740  $result = $db->query($sql);
741  $installed_dirs = array();
742 
743  while($row = $db->fetchArray($result)){
744  $installed_dirs[] = $row['dirname'];
745  }
746 
747  require_once XOOPS_ROOT_PATH . "/class/xoopslists.php";
748  $dirlist = XoopsLists::getModulesList();
749  $available_mods = array();
750  $module_handler = xoops_gethandler('module');
751  foreach ($dirlist as $file) {
752  clearstatcache();
753  $file = trim($file);
754  if (!in_array($file, $installed_dirs)) {
755  $module =& $module_handler->create();
756  if (!$module->loadInfo($file, false)) {
757  continue;
758  }
759  $available_mods[] = $module;
760  unset($module);
761  }
762  }
763 
764  unset($dirlist);
765  unset($module_handler);
766 
767  $limit = 10;
768  $tpages = ceil(count($available_mods)/$limit);
769  $page = rmc_server_var($_POST, 'page', 1);
770  if ($page>$tpages) $page = 1;
771  $start = ($page<=0 ? 0 : $page-1) * $limit;
772 
773  $nav = new RMPageNav(count($available_mods), $limit, $page, 3);
774  $nav->target_url('javascript:;" onclick="load_page({PAGE_NUM});');
775 
776  // Event for available modules
777  $available_mods = RMEvents::get()->run_event('rmcommon.available.modules', $available_mods);
778  $end = ($page*$limit);
779 
780  if ($end>count($available_mods)) $end = count($available_mods);
781 
782  ob_start();
783 ?>
784  <div class="rmc_widget_content_reduced rmc_modules_widget">
785  <img id="img-load" src="images/loading.gif" style="display: none; margin: 15px auto;" />
786  <div id="mods-widget-container">
787  <?php for($i=$start;$i<$end;$i++): ?>
788  <?php $mod = $available_mods[$i]; ?>
789  <div class="<?php echo tpl_cycle("even,odd"); ?>">
790  <a href="modules.php?action=install&amp;dir=<?php echo $mod->getInfo('dirname'); ?>" class="rmc_mod_img" style="background: url(<?php echo XOOPS_URL; ?>/modules/<?php echo $mod->getInfo('dirname'); ?>/<?php echo $mod->getInfo('image'); ?>) no-repeat center;"><span>&nbsp;</span></a>
791  <strong><a href="modules.php?action=install&amp;dir=<?php echo $mod->getInfo('dirname'); ?>"><?php echo $mod->getInfo('name'); ?></a></strong>
792  <span class="rmc_available_options">
793  <a href="modules.php?action=install&amp;dir=<?php echo $mod->getInfo('dirname'); ?>"><?php _e('Install','rmcommon'); ?></a> |
794  <a href="javascript:;" onclick="show_module_info('<?php echo $mod->getInfo('dirname'); ?>');"><?php _e('More info','rmcommon'); ?></a>
795  </span>
796  <span class="rmc_mod_info" id="mod-<?php echo $mod->getInfo('dirname'); ?>">
797  <?php _e('Version:','rmcommon'); ?>
798  <?php if($mod->getInfo('rmnative')): ?>
799  <?php echo RMUtilities::format_version($mod->getInfo('rmversion')); ?>
800  <?php else: ?>
801  <?php echo $mod->getInfo('version'); ?>
802  <?php endif; ?><br />
803  <?php _e('Author:', 'rmcommon'); ?> <?php echo substr(strip_tags($mod->getInfo('author')), 0, 12); ?>
804  </span>
805  </div>
806  <input type="hidden" id="token" value="<?php echo $xoopsSecurity->createToken(); ?>" />
807  <?php endfor; ?>
808  <?php $nav->display(false); ?>
809  </div>
810  </div>
811 <?php
812  $ret = ob_get_clean();
813  echo $ret;
814 }
815 
816 
817 function module_rename(){
819 
820  error_reporting(0);
821  $xoopsLogger->activated = false;
822 
823  if (!$xoopsSecurity->check()){
824 
825  $ret = array(
826  'error' => 1,
827  'message' => __('You can not rename modules. Session token not valid!','rmcommon').' '.rmc_server_var($_POST, 'token')
828  );
829 
830  echo json_encode($ret);
831  die();
832 
833  }
834 
835  $id = rmc_server_var($_POST, 'id', 0);
836  $name = trim(rmc_server_var($_POST, 'name', ''));
837 
838  if ($id<=0 || $name==''){
839  $ret = array(
840  'error' => 1,
841  'message' => __('Data provided is not valid','rmcommon'),
842  'token' => $xoopsSecurity->createToken()
843  );
844 
845  echo json_encode($ret);
846  die();
847  }
848 
849  $db = XoopsDatabaseFactory::getDatabaseConnection();
850  $sql = "UPDATE ".$db->prefix("modules")." SET `name`='$name' WHERE mid='$id'";
851  if (!$db->queryF($sql)){
852  $ret = array(
853  'error' => 1,
854  'message' => __('Module name could not be changed!','rmcommon').'\n'.$db->error(),
855  'token' => $xoopsSecurity->createToken()
856  );
857 
858  echo json_encode($ret);
859  die();
860  }
861 
862  $ret = array(
863  'error'=>0,
864  'message'=>__('Module name changed successfully!','rmcommon'),
865  'token'=>$xoopsSecurity->createToken(),
866  'id'=>$id
867  );
868 
869  echo json_encode($ret);
870 
871  die();
872 
873 }
874 
875 
876 $action = rmc_server_var($_REQUEST, 'action', '');
877 switch($action){
878  case 'install':
879  module_install();
880  break;
881  case 'install_now':
883  break;
884  case 'uninstall_module':
886  break;
887  case 'update_module':
889  break;
890  case 'disable_module':
892  break;
893  case 'enable_module':
895  break;
896  case 'load_page':
898  break;
899  case 'savename':
900  module_rename();
901  break;
902  default:
903  $_REQUEST['action'] = '';
905  break;
906 }