XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
module.php
1<?php
12
13
14if (!defined('XOOPS_ROOT_PATH')) {
15 exit();
16}
17
24{
28 public $modinfo;
32 public $adminmenu;
33
34 public function __construct()
35 {
36 parent::__construct();
37 static $initVars;
38 if (isset($initVars)) {
39 $this->vars = $initVars;
40 return;
41 }
42 $this->initVar('mid', XOBJ_DTYPE_INT, null, false);
43 $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
44 $this->initVar('version', XOBJ_DTYPE_INT, 100, false);
45 $this->initVar('last_update', XOBJ_DTYPE_INT, null, false);
46 $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
47 $this->initVar('isactive', XOBJ_DTYPE_INT, 1, false);
48 $this->initVar('dirname', XOBJ_DTYPE_OTHER, null, true);
49 $this->initVar('trust_dirname', XOBJ_DTYPE_OTHER, null, true);
50 $this->initVar('role', XOBJ_DTYPE_OTHER, null, false);
51 $this->initVar('hasmain', XOBJ_DTYPE_INT, 0, false);
52 $this->initVar('hasadmin', XOBJ_DTYPE_INT, 0, false);
53 $this->initVar('hassearch', XOBJ_DTYPE_INT, 0, false);
54 $this->initVar('hasconfig', XOBJ_DTYPE_INT, 0, false);
55 $this->initVar('hascomments', XOBJ_DTYPE_INT, 0, false);
56 // RMV-NOTIFY
57 $this->initVar('hasnotification', XOBJ_DTYPE_INT, 0, false);
58 $initVars = $this->vars;
59 }
60
67 public function loadInfoAsVar(string $dirname, bool $verbose = true)
68 {
69 if (!isset($this->modinfo)) {
70 $this->loadInfo($dirname, $verbose);
71 }
72 $this->setVar('name', $this->modinfo['name'], true);
73 $this->setVar('version', Legacy_Utils::convertVersionFromModinfoToInt($this->modinfo['version'])); // TODO semver
74 $this->setVar('dirname', $this->modinfo['dirname'], true);
75 // $trustDirname = isset($this->modinfo['trust_dirname']) ? $this->modinfo['trust_dirname'] : null;
76 $trustDirname = $this->modinfo['trust_dirname'] ?? null;
77 $this->setVar('trust_dirname', $trustDirname, true);
78 //$role = isset($this->modinfo['role']) ? $this->modinfo['role'] : null;
79 $role = $this->modinfo['role'] ?? null;
80 $this->setVar('role', $role, true);
81 $hasmain = (isset($this->modinfo['hasMain']) && 1 == $this->modinfo['hasMain']) ? 1 : 0;
82 $hasadmin = (isset($this->modinfo['hasAdmin']) && 1 == $this->modinfo['hasAdmin']) ? 1 : 0;
83 $hassearch = (isset($this->modinfo['hasSearch']) && 1 == $this->modinfo['hasSearch']) ? 1 : 0;
84 $hasconfig = ((isset($this->modinfo['config']) && is_array($this->modinfo['config'])) || !empty($this->modinfo['hasComments'])) ? 1 : 0;
85 $hascomments = (isset($this->modinfo['hasComments']) && 1 == $this->modinfo['hasComments']) ? 1 : 0;
86 // RMV-NOTIFY
87 $hasnotification = (isset($this->modinfo['hasNotification']) && 1 == $this->modinfo['hasNotification']) ? 1 : 0;
88 $this->setVar('hasmain', $hasmain);
89 $this->setVar('hasadmin', $hasadmin);
90 $this->setVar('hassearch', $hassearch);
91 $this->setVar('hasconfig', $hasconfig);
92 $this->setVar('hascomments', $hascomments);
93 // RMV-NOTIFY
94 $this->setVar('hasnotification', $hasnotification);
95 }
96
104 public function &getInfo($name=null)
105 {
106 if (!isset($this->modinfo)) {
107 $this->loadInfo($this->getVar('dirname'));
108 }
109 if (isset($name)) {
110 if (isset($this->modinfo[$name])) {
111 return $this->modinfo[$name];
112 }
113 $ret = false;
114 return $ret;
115 }
116 return $this->modinfo;
117 }
118
124 public function mainLink()
125 {
126 if (1 == $this->getVar('hasmain')) {
127 $ret = '<a href="'.XOOPS_URL.'/modules/'.$this->getVar('dirname').'/">'.$this->getVar('name').'</a>';
128 return $ret;
129 }
130 return false;
131 }
132
138 public function &subLink()
139 {
140 $ret = [];
141 if ($this->getInfo('sub') && is_array($this->getInfo('sub'))) {
142 foreach ($this->getInfo('sub') as $submenu) {
143 $ret[] = ['name' => $submenu['name'], 'url' => $submenu['url']];
144 }
145 }
146 return $ret;
147 }
148
152 public function loadAdminMenu()
153 {
154 $menu = $this->getInfo('adminmenu');
155 if (is_array($menu)) {
156 // Array() Cube style
157 $this->adminmenu = $menu;
158 } else {
159 if ($menu && file_exists($path = XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/'.$menu)) {
160 include $path;
161 $this->adminmenu = (isset($adminmenu) && is_array($adminmenu))? $adminmenu : [];
162 }
163 }
164 }
165
171 public function &getAdminMenu()
172 {
173 if (!isset($this->adminmenu)) {
174 $this->loadAdminMenu();
175 }
176 return $this->adminmenu;
177 }
178
185 public function loadInfo($dirname, $verbose = true)
186 {
187 global $xoopsConfig;
188
189 //
190 // Guard multiplex loading.
191 //
192 if (!empty($this->modinfo)) {
193 return;
194 }
195
196 $root =& XCube_Root::getSingleton();
197 $root->mLanguageManager->loadModinfoMessageCatalog($dirname);
198
199 if (file_exists(XOOPS_ROOT_PATH.'/modules/'.$dirname.'/xoops_version.php')) {
200 include XOOPS_ROOT_PATH.'/modules/'.$dirname.'/xoops_version.php';
201 } else {
202 if (false != $verbose) {
203 echo "Module File for $dirname Not Found!";
204 }
205 return;
206 }
207
208 $this->modinfo =& $modversion;
209
210 if (isset($this->modinfo['version'])) {
211 $this->modinfo['version'] = (float)$this->modinfo['version'];
212 } else {
213 $this->modinfo['version'] = 0;
214 }
215
216 // set ['sqlfile']['mysqli']
217 if (isset($this->modinfo['sqlfile']) && isset($this->modinfo['sqlfile']['mysql']) && ! isset($this->modinfo['sqlfile']['mysqli'])) {
218 $this->modinfo['sqlfile']['mysqli'] = $this->modinfo['sqlfile']['mysql'];
219 }
220 }
221
232 public function &search($term = '', $andor = 'AND', $limit = 0, $offset = 0, $userid = 0)
233 {
234 $ret = false;
235 if (1 != $this->getVar('hassearch')) {
236 return $ret;
237 }
238 $search =& $this->getInfo('search');
239 if (1 != $this->getVar('hassearch') || !isset($search['file']) || !isset($search['func']) || '' == $search['func'] || '' == $search['file']) {
240 return $ret;
241 }
242 if (file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $search['file'])) {
243 include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/'.$search['file'];
244 } else {
245 return $ret;
246 }
247 if (function_exists($search['func'])) {
248 $func = $search['func'];
249 $ret = $func($term, $andor, $limit, $offset, $userid);
250 }
251 return $ret;
252 }
253
257 public function getRenderedVersion()
258 {
259 return sprintf('%01.2f', $this->getVar('version') / 100);
260 }
261
265 public function hasHelp()
266 {
267 $info =& $this->getInfo();
268 if (isset($info['cube_style']) && false != $info['cube_style'] && isset($info['help']) && strlen($info['help']) > 0) {
269 return true;
270 }
271
272 return false;
273 }
274
278 public function getHelp()
279 {
280 if ($this->hasHelp()) {
281 return $this->modinfo['help'];
282 }
283
284 return null;
285 }
286
290 public function hasNeedUpdate()
291 {
292 $info =& $this->getInfo();
293 return ($this->getVar('version') < Legacy_Utils::convertVersionFromModinfoToInt($info['version'])); // TODO semver
294 }
295
300 public function mid()
301 {
302 return $this->getVar('mid');
303 }
304 public function dirname()
305 {
306 return $this->getVar('dirname');
307 }
308 public function name()
309 {
310 return $this->getVar('name');
311 }
312 public function &getByDirName($dirname)
313 {
314 $modhandler = xoops_gethandler('module');
315 $ret =& $modhandler->getByDirname($dirname);
316 return $ret;
317 }
319}
320
321
334{
335 public $_tmp;
336
343 public $_cachedModule_mid = [];
344
351 public $_cachedModule_dirname = [];
352
359 public function &create($isNew = true)
360 {
361 $module =new XoopsModule();
362 if ($isNew) {
363 $module->setNew();
364 }
365 return $module;
366 }
367
375 public function &get($id)
376 {
377 $ret = false;
378 $id = (int)$id;
379 if ($id > 0) {
380 if (!empty($this->_cachedModule_mid[$id])) {
381 return $this->_cachedModule_mid[$id];
382 } else {
383 $sql = 'SELECT * FROM '.$this->db->prefix('modules').' WHERE mid = '.$id;
384 if ($result = $this->db->query($sql)) {
385 $numrows = $this->db->getRowsNum($result);
386 if (1 == $numrows) {
387 $module =new XoopsModule();
388 $myrow = $this->db->fetchArray($result);
389 $module->assignVars($myrow);
390 $this->_cachedModule_mid[$id] =& $module;
391 $this->_cachedModule_dirname[$module->getVar('dirname')] =& $module;
392 $ret =& $module;
393 }
394 }
395 }
396 }
397 return $ret;
398 }
399
407 public function &getByDirname($dirname)
408 {
409 $ret = false;
410 $dirname = trim($dirname);
411 $cache = &$this->_cachedModule_dirname;
412 if (!empty($cache[$dirname])) {
413 $ret = $cache[$dirname];
414 } elseif (0 == count($cache)) {
415 $db = $this->db;
416 $sql = 'SELECT * FROM ' . $db->prefix('modules');
417 if ($result = $db->query($sql)) {
418 while ($myrow = $db->fetchArray($result)) {
419 $module = new XoopsModule();
420 $module->assignVars($myrow);
421 $cache[$myrow['dirname']] =& $module;
422 $this->_cachedModule_mid[$myrow['mid']] =& $module;
423 unset($module);
424 }
425 }
426 if (!empty($cache[$dirname])) {
427 $ret = $cache[$dirname];
428 }
429 }
430 return $ret;
431 }
432
442 public function insert(&$module)
443 {
444 $name = null;
445 $version = null;
446 $weight = null;
447 $dirname = null;
448 $trust_dirname = null;
449 $role = null;
450 $hasmain = null;
451 $hasadmin = null;
452 $hassearch = null;
453 $hasconfig = null;
454 $hascomments = null;
455 $hasnotification = null;
456 $isactive = null;
457 if ('xoopsmodule' != strtolower(get_class($module))) {
458 return false;
459 }
460
461 if (!$module->isDirty()) {
462 return true;
463 }
464 if (!$module->cleanVars()) {
465 return false;
466 }
467 foreach ($module->cleanVars as $k => $v) {
468 ${$k} = $v;
469 }
470 if ($module->isNew()) {
471 if (empty($mid)) { //Memo: if system module, mid might be set to 1
472 $mid = $this->db->genId('modules_mid_seq');
473 }
474 $sql = sprintf('INSERT INTO %s (mid, name, version, last_update, weight, isactive, dirname, trust_dirname, role, hasmain, hasadmin, hassearch, hasconfig, hascomments, hasnotification) VALUES (%u, %s, %u, %u, %u, %u, %s, %s, %s, %u, %u, %u, %u, %u, %u)', $this->db->prefix('modules'), $mid, $this->db->quoteString($name), $version, time(), $weight, 1, $this->db->quoteString($dirname), $this->db->quoteString($trust_dirname), $this->db->quoteString($role), $hasmain, $hasadmin, $hassearch, $hasconfig, $hascomments, $hasnotification);
475 } else {
476 $sql = sprintf('UPDATE %s SET name = %s, dirname = %s, trust_dirname = %s, role = %s, version = %u, last_update = %u, weight = %u, isactive = %u, hasmain = %u, hasadmin = %u, hassearch = %u, hasconfig = %u, hascomments = %u, hasnotification = %u WHERE mid = %u', $this->db->prefix('modules'), $this->db->quoteString($name), $this->db->quoteString($dirname), $this->db->quoteString($trust_dirname), $this->db->quoteString($role), $version, time(), $weight, $isactive, $hasmain, $hasadmin, $hassearch, $hasconfig, $hascomments, $hasnotification, $mid);
477 }
478 if (!$result = $this->db->query($sql)) {
479 return false;
480 }
481 $module->unsetNew();
482 if (empty($mid)) {
483 $mid = $this->db->getInsertId();
484 }
485 $module->assignVar('mid', $mid);
486 if (!empty($this->_cachedModule_dirname[$dirname])) {
487 unset($this->_cachedModule_dirname[$dirname]);
488 }
489 if (!empty($this->_cachedModule_mid[$mid])) {
490 unset($this->_cachedModule_mid[$mid]);
491 }
492
493 $this->_cachedModule_dirname[$dirname] = $module;
494 $this->_cachedModule_mid[$mid] =& $this->_cachedModule_dirname[$dirname];
495
496 if (XC_CLASS_EXISTS('Legacy_AdminSideMenu')) {
497 Legacy_AdminSideMenu::clearCache();
498 }
499
500 return true;
501 }
502
509 public function delete(&$module)
510 {
511 if ('xoopsmodule' != strtolower(get_class($module))) {
512 return false;
513 }
514 $sql = sprintf('DELETE FROM %s WHERE mid = %u', $this->db->prefix('modules'), $module->getVar('mid'));
515 if (!$result = $this->db->query($sql)) {
516 return false;
517 }
518 // delete admin permissions assigned for this module
519 $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_admin' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
520 $this->db->query($sql);
521 // delete read permissions assigned for this module
522 $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_read' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
523 $this->db->query($sql);
524
525 if (1 == $module->getVar('mid')) {
526 $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'system_admin'", $this->db->prefix('group_permission'));
527 } else {
528 $sql = sprintf('DELETE FROM %s WHERE gperm_modid = %u', $this->db->prefix('group_permission'), $module->getVar('mid'));
529 }
530 $this->db->query($sql);
531
532 $sql = sprintf('SELECT block_id FROM %s WHERE module_id = %u', $this->db->prefix('block_module_link'), $module->getVar('mid'));
533 if ($result = $this->db->query($sql)) {
534 $block_id_arr = [];
535 while ($myrow = $this->db->fetchArray($result)) {
536 array_push($block_id_arr, $myrow['block_id']);
537 }
538 }
539 // loop through block_id_arr
540 if (isset($block_id_arr)) {
541 foreach ($block_id_arr as $i) {
542 $sql = sprintf('SELECT block_id FROM %s WHERE module_id != %u AND block_id = %u', $this->db->prefix('block_module_link'), $module->getVar('mid'), $i);
543 if ($result2 = $this->db->query($sql)) {
544 if (0 < $this->db->getRowsNum($result2)) {
545 // this block has other entries, so delete the entry for this module
546 $sql = sprintf('DELETE FROM %s WHERE (module_id = %u) AND (block_id = %u)', $this->db->prefix('block_module_link'), $module->getVar('mid'), $i);
547 $this->db->query($sql);
548 } else {
549 // this block doesnt have other entries, so disable the block and let it show on top page only. otherwise, this block will not display anymore on block admin page!
550 $sql = sprintf('UPDATE %s SET visible = 0 WHERE bid = %u', $this->db->prefix('newblocks'), $i);
551 $this->db->query($sql);
552 $sql = sprintf('UPDATE %s SET module_id = -1 WHERE module_id = %u', $this->db->prefix('block_module_link'), $module->getVar('mid'));
553 $this->db->query($sql);
554 }
555 }
556 }
557 }
558
559 if (!empty($this->_cachedModule_dirname[$module->getVar('dirname')])) {
560 unset($this->_cachedModule_dirname[$module->getVar('dirname')]);
561 }
562 if (!empty($this->_cachedModule_mid[$module->getVar('mid')])) {
563 unset($this->_cachedModule_mid[$module->getVar('mid')]);
564 }
565 return true;
566 }
567
575 public function &getObjects($criteria = null, $id_as_key = false)
576 {
577 $ret = [];
578 $limit = $start = 0;
579 $db = &$this->db;
580 $sql = 'SELECT * FROM '.$db->prefix('modules');
581 if (isset($criteria) && $criteria instanceof \criteriaelement) {
582 $sql .= ' '.$criteria->renderWhere();
583
584 if (null != $criteria->getSort()) {
585 $sql .= ' ORDER BY '.$criteria->getSort().' '.$criteria->getOrder();
586 } else {
587 $sql .= ' ORDER BY weight '.$criteria->getOrder().', mid ASC';
588 }
589
590 $limit = $criteria->getLimit();
591 $start = $criteria->getStart();
592 }
593 $result = $db->query($sql, $limit, $start);
594 if (!$result) {
595 return $ret;
596 }
597 while ($myrow = $db->fetchArray($result)) {
598 $mid = $myrow['mid'];
599 if (isset($this->_cachedModule_mid[$mid])) {
600 $module = $this->_cachedModule_mid[$mid];
601 } else {
602 $module =new XoopsModule();
603 $module->assignVars($myrow);
604 $this->_cachedModule_mid[$mid] = $module;
605 $this->_cachedModule_dirname[$myrow['dirname']] = $module;
606 }
607 if (!$id_as_key) {
608 $ret[] =& $module;
609 } else {
610 $ret[$mid] =& $module;
611 }
612 unset($module);
613 }
614 return $ret;
615 }
616
623 public function getCount($criteria = null)
624 {
625 $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('modules');
626 if (isset($criteria) && $criteria instanceof \criteriaelement) {
627 $sql .= ' '.$criteria->renderWhere();
628 }
629 if (!$result =& $this->db->query($sql)) {
630 return 0;
631 }
632 [$count] = $this->db->fetchRow($result);
633 return $count;
634 }
635
645 public function &getList($criteria = null, $dirname_as_key = false)
646 {
647 $ret = [];
648 $modules =& $this->getObjects($criteria, true);
649 foreach ($modules as $i=>$module) {
650 if (!$dirname_as_key) {
651 $ret[$i] =& $module->getVar('name');
652 } else {
653 $ret[$module->getVar('dirname')] =& $module->getVar('name');
654 }
655 }
656 return $ret;
657 }
658}
& getObjects($criteria=null, $id_as_key=false)
Definition module.php:575
insert(&$module)
Definition module.php:442
& getByDirname($dirname)
Definition module.php:407
getCount($criteria=null)
Definition module.php:623
& getList($criteria=null, $dirname_as_key=false)
Definition module.php:645
& create($isNew=true)
Definition module.php:359
__construct()
Definition module.php:34
loadInfo($dirname, $verbose=true)
Definition module.php:185
loadAdminMenu()
Definition module.php:152
loadInfoAsVar(string $dirname, bool $verbose=true)
Definition module.php:67
& subLink()
Definition module.php:138
hasNeedUpdate()
Definition module.php:290
& getAdminMenu()
Definition module.php:171
getRenderedVersion()
Definition module.php:257
& search($term='', $andor='AND', $limit=0, $offset=0, $userid=0)
Definition module.php:232
& getInfo($name=null)
Definition module.php:104
setVar($key, $value, $not_gpc=false)
Definition object.php:250
initVar($key, $data_type, $value=null, $required=false, $maxlength=null, $options='')
Definition object.php:206
& getVar($key, $format='s')
Definition object.php:317