1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32: if (!defined('XOOPS_ROOT_PATH')) {
33: exit();
34: }
35:
36: 37: 38: 39: 40: 41: 42: 43:
44: class XoopsModule extends XoopsObject
45: {
46: 47: 48:
49: var $modinfo;
50: 51: 52:
53: var $adminmenu;
54:
55: 56: 57:
58: function XoopsModule()
59: {
60: $this->XoopsObject();
61: static $initVars;
62: if (isset($initVars)) {
63: $this->vars = $initVars;
64: return;
65: }
66: $this->initVar('mid', XOBJ_DTYPE_INT, null, false);
67: $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
68: $this->initVar('version', XOBJ_DTYPE_INT, 100, false);
69: $this->initVar('last_update', XOBJ_DTYPE_INT, null, false);
70: $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
71: $this->initVar('isactive', XOBJ_DTYPE_INT, 1, false);
72: $this->initVar('dirname', XOBJ_DTYPE_OTHER, null, true);
73: $this->initVar('trust_dirname', XOBJ_DTYPE_OTHER, null, true);
74: $this->initVar('role', XOBJ_DTYPE_OTHER, null, false);
75: $this->initVar('hasmain', XOBJ_DTYPE_INT, 0, false);
76: $this->initVar('hasadmin', XOBJ_DTYPE_INT, 0, false);
77: $this->initVar('hassearch', XOBJ_DTYPE_INT, 0, false);
78: $this->initVar('hasconfig', XOBJ_DTYPE_INT, 0, false);
79: $this->initVar('hascomments', XOBJ_DTYPE_INT, 0, false);
80:
81: $this->initVar('hasnotification', XOBJ_DTYPE_INT, 0, false);
82: $initVars = $this->vars;
83: }
84:
85: 86: 87: 88: 89: 90:
91: function loadInfoAsVar($dirname, $verbose = true)
92: {
93: if ( !isset($this->modinfo) ) {
94: $this->loadInfo($dirname, $verbose);
95: }
96: $this->setVar('name', $this->modinfo['name'], true);
97: $this->setVar('version', Legacy_Utils::convertVersionFromModinfoToInt($this->modinfo['version']));
98: $this->setVar('dirname', $this->modinfo['dirname'], true);
99: $trustDirname = isset($this->modinfo['trust_dirname']) ? $this->modinfo['trust_dirname'] : null;
100: $this->setVar('trust_dirname', $trustDirname , true);
101: $role = isset($this->modinfo['role']) ? $this->modinfo['role'] : null;
102: $this->setVar('role', $role , true);
103: $hasmain = (isset($this->modinfo['hasMain']) && $this->modinfo['hasMain'] == 1) ? 1 : 0;
104: $hasadmin = (isset($this->modinfo['hasAdmin']) && $this->modinfo['hasAdmin'] == 1) ? 1 : 0;
105: $hassearch = (isset($this->modinfo['hasSearch']) && $this->modinfo['hasSearch'] == 1) ? 1 : 0;
106: $hasconfig = ((isset($this->modinfo['config']) && is_array($this->modinfo['config'])) || !empty($this->modinfo['hasComments'])) ? 1 : 0;
107: $hascomments = (isset($this->modinfo['hasComments']) && $this->modinfo['hasComments'] == 1) ? 1 : 0;
108:
109: $hasnotification = (isset($this->modinfo['hasNotification']) && $this->modinfo['hasNotification'] == 1) ? 1 : 0;
110: $this->setVar('hasmain', $hasmain);
111: $this->setVar('hasadmin', $hasadmin);
112: $this->setVar('hassearch', $hassearch);
113: $this->setVar('hasconfig', $hasconfig);
114: $this->setVar('hascomments', $hascomments);
115:
116: $this->setVar('hasnotification', $hasnotification);
117: }
118:
119: 120: 121: 122: 123: 124: 125:
126: function &getInfo($name=null)
127: {
128: if ( !isset($this->modinfo) ) {
129: $this->loadInfo($this->getVar('dirname'));
130: }
131: if ( isset($name) ) {
132: if ( isset($this->modinfo[$name]) ) {
133: return $this->modinfo[$name];
134: }
135: $ret = false;
136: return $ret;
137: }
138: return $this->modinfo;
139: }
140:
141: 142: 143: 144: 145:
146: function mainLink()
147: {
148: if ( $this->getVar('hasmain') == 1 ) {
149: $ret = '<a href="'.XOOPS_URL.'/modules/'.$this->getVar('dirname').'/">'.$this->getVar('name').'</a>';
150: return $ret;
151: }
152: return false;
153: }
154:
155: 156: 157: 158: 159:
160: function &subLink()
161: {
162: $ret = array();
163: if ( $this->getInfo('sub') && is_array($this->getInfo('sub')) ) {
164: foreach ( $this->getInfo('sub') as $submenu ) {
165: $ret[] = array('name' => $submenu['name'], 'url' => $submenu['url']);
166: }
167: }
168: return $ret;
169: }
170:
171: 172: 173:
174: function loadAdminMenu()
175: {
176: $menu = $this->getInfo('adminmenu');
177: if ($menu && file_exists($path = XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/'.$menu)) {
178: include $path;
179: $this->adminmenu =& $adminmenu;
180: }
181: }
182:
183: 184: 185: 186: 187:
188: function &getAdminMenu()
189: {
190: if ( !isset($this->adminmenu) ) {
191: $this->loadAdminMenu();
192: }
193: return $this->adminmenu;
194: }
195:
196: 197: 198: 199: 200: 201:
202: function loadInfo($dirname, $verbose = true)
203: {
204: global $xoopsConfig;
205:
206:
207:
208:
209: if (!empty($this->modinfo)) {
210: return;
211: }
212:
213: $root =& XCube_Root::getSingleton();
214: $root->mLanguageManager->loadModinfoMessageCatalog($dirname);
215:
216: if (file_exists(XOOPS_ROOT_PATH.'/modules/'.$dirname.'/xoops_version.php')) {
217: include XOOPS_ROOT_PATH.'/modules/'.$dirname.'/xoops_version.php';
218: } else {
219: if (false != $verbose) {
220: echo "Module File for $dirname Not Found!";
221: }
222: return;
223: }
224:
225: $this->modinfo =& $modversion;
226:
227: if (isset($this->modinfo['version'])) {
228: $this->modinfo['version'] = (float)$this->modinfo['version'];
229: } else {
230: $this->modinfo['version'] = 0;
231: }
232: }
233:
234: 235: 236: 237: 238: 239: 240: 241: 242: 243:
244: function &search($term = '', $andor = 'AND', $limit = 0, $offset = 0, $userid = 0)
245: {
246: $ret = false;
247: if ($this->getVar('hassearch') != 1) {
248: return $ret;
249: }
250: $search =& $this->getInfo('search');
251: if ($this->getVar('hassearch') != 1 || !isset($search['file']) || !isset($search['func']) || $search['func'] == '' || $search['file'] == '') {
252: return $ret;
253: }
254: if (file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname').'/'.$search['file'])) {
255: include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/'.$search['file'];
256: } else {
257: return $ret;
258: }
259: if (function_exists($search['func'])) {
260: $func = $search['func'];
261: $ret = $func($term, $andor, $limit, $offset, $userid);
262: }
263: return $ret;
264: }
265:
266: 267: 268:
269: function getRenderedVersion()
270: {
271: return sprintf('%01.2f', $this->getVar('version') / 100);
272: }
273:
274: 275: 276:
277: function hasHelp()
278: {
279: $info =& $this->getInfo();
280: if (isset($info['cube_style']) && $info['cube_style'] != false && isset($info['help']) && strlen($info['help']) > 0) {
281: return true;
282: }
283:
284: return false;
285: }
286:
287: 288: 289:
290: function getHelp()
291: {
292: if ($this->hasHelp()) {
293: return $this->modinfo['help'];
294: }
295:
296: return null;
297: }
298:
299: 300: 301:
302: function hasNeedUpdate()
303: {
304: $info =& $this->getInfo();
305: return ($this->getVar('version') < Legacy_Utils::convertVersionFromModinfoToInt($info['version']));
306: }
307:
308: 309: 310: 311:
312: function mid()
313: {
314: return $this->getVar('mid');
315: }
316: function dirname()
317: {
318: return $this->getVar('dirname');
319: }
320: function name()
321: {
322: return $this->getVar('name');
323: }
324: function &getByDirName($dirname)
325: {
326: $modhandler = xoops_gethandler('module');
327: $ret =& $modhandler->getByDirname($dirname);
328: return $ret;
329: }
330:
331: }
332:
333:
334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344:
345: class XoopsModuleHandler extends XoopsObjectHandler
346: {
347: var $_tmp;
348:
349: 350: 351: 352: 353: 354:
355: var $_cachedModule_mid = array();
356:
357: 358: 359: 360: 361: 362:
363: var $_cachedModule_dirname = array();
364:
365: 366: 367: 368: 369: 370:
371: function &create($isNew = true)
372: {
373: $module =new XoopsModule();
374: if ($isNew) {
375: $module->setNew();
376: }
377: return $module;
378: }
379:
380: 381: 382: 383: 384: 385: 386:
387: function &get($id)
388: {
389: $ret = false;
390: $id = (int)$id;
391: if ($id > 0) {
392: if (!empty($this->_cachedModule_mid[$id])) {
393: return $this->_cachedModule_mid[$id];
394: } else {
395: $sql = 'SELECT * FROM '.$this->db->prefix('modules').' WHERE mid = '.$id;
396: if ($result = $this->db->query($sql)) {
397: $numrows = $this->db->getRowsNum($result);
398: if ($numrows == 1) {
399: $module =new XoopsModule();
400: $myrow = $this->db->fetchArray($result);
401: $module->assignVars($myrow);
402: $this->_cachedModule_mid[$id] =& $module;
403: $this->_cachedModule_dirname[$module->getVar('dirname')] =& $module;
404: $ret =& $module;
405: }
406: }
407: }
408: }
409: return $ret;
410: }
411:
412: 413: 414: 415: 416: 417: 418:
419: function &getByDirname($dirname)
420: {
421: $ret = false;
422: $dirname = trim($dirname);
423: $cache = &$this->_cachedModule_dirname;
424: if (!empty($cache[$dirname])) {
425: $ret = $cache[$dirname];
426: }
427: elseif (count($cache)==0) {
428: $db = $this->db;
429: $sql = "SELECT * FROM ".$db->prefix('modules');
430: if ($result = $db->query($sql)) {
431: while ($myrow = $db->fetchArray($result)) {
432: $module = new XoopsModule();
433: $module->assignVars($myrow);
434: $cache[$myrow['dirname']] =& $module;
435: $this->_cachedModule_mid[$myrow['mid']] =& $module;
436: unset($module);
437: }
438: }
439: if (!empty($cache[$dirname])) {
440: $ret = $cache[$dirname];
441: }
442: }
443: return $ret;
444: }
445:
446: 447: 448: 449: 450: 451: 452: 453: 454:
455: function insert(&$module)
456: {
457: if (strtolower(get_class($module)) != 'xoopsmodule') {
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)) {
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] =& $module;
495:
496: if (XC_CLASS_EXISTS('Legacy_AdminSideMenu')) {
497: Legacy_AdminSideMenu::clearCache();
498: }
499:
500: return true;
501: }
502:
503: 504: 505: 506: 507: 508:
509: function delete(&$module)
510: {
511: if (strtolower(get_class($module)) != 'xoopsmodule') {
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:
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:
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 ($module->getVar('mid')==1) {
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 = array();
535: while ($myrow = $this->db->fetchArray($result))
536: {
537: array_push($block_id_arr, $myrow['block_id']);
538: }
539: }
540:
541: if (isset($block_id_arr)) {
542: foreach ($block_id_arr as $i) {
543: $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);
544: if ($result2 = $this->db->query($sql)) {
545: if (0 < $this->db->getRowsNum($result2)) {
546:
547: $sql = sprintf("DELETE FROM %s WHERE (module_id = %u) AND (block_id = %u)", $this->db->prefix('block_module_link'), $module->getVar('mid'), $i);
548: $this->db->query($sql);
549: } else {
550:
551: $sql = sprintf("UPDATE %s SET visible = 0 WHERE bid = %u", $this->db->prefix('newblocks'), $i);
552: $this->db->query($sql);
553: $sql = sprintf("UPDATE %s SET module_id = -1 WHERE module_id = %u", $this->db->prefix('block_module_link'), $module->getVar('mid'));
554: $this->db->query($sql);
555: }
556: }
557: }
558: }
559:
560: if (!empty($this->_cachedModule_dirname[$module->getVar('dirname')])) {
561: unset ($this->_cachedModule_dirname[$module->getVar('dirname')]);
562: }
563: if (!empty($this->_cachedModule_mid[$module->getVar('mid')])) {
564: unset ($this->_cachedModule_mid[$module->getVar('mid')]);
565: }
566: return true;
567: }
568:
569: 570: 571: 572: 573: 574: 575:
576: function &getObjects($criteria = null, $id_as_key = false)
577: {
578: $ret = array();
579: $limit = $start = 0;
580: $db = &$this->db;
581: $sql = 'SELECT * FROM '.$db->prefix('modules');
582: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
583: $sql .= ' '.$criteria->renderWhere();
584:
585: if($criteria->getSort()!=null) {
586: $sql .= ' ORDER BY '.$criteria->getSort().' '.$criteria->getOrder();
587: }
588: else {
589: $sql .= ' ORDER BY weight '.$criteria->getOrder().', mid ASC';
590: }
591:
592: $limit = $criteria->getLimit();
593: $start = $criteria->getStart();
594: }
595: $result = $db->query($sql, $limit, $start);
596: if (!$result) {
597: return $ret;
598: }
599: while ($myrow = $db->fetchArray($result)) {
600: $mid = $myrow['mid'];
601: if (isset($this->_cachedModule_mid[$mid])) {
602: $module = $this->_cachedModule_mid[$mid];
603: } else {
604: $module =new XoopsModule();
605: $module->assignVars($myrow);
606: $this->_cachedModule_mid[$mid] = $module;
607: $this->_cachedModule_dirname[$myrow['dirname']] = $module;
608: }
609: if (!$id_as_key) {
610: $ret[] =& $module;
611: } else {
612: $ret[$mid] =& $module;
613: }
614: unset($module);
615: }
616: return $ret;
617: }
618:
619: 620: 621: 622: 623: 624:
625: function getCount($criteria = null)
626: {
627: $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('modules');
628: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
629: $sql .= ' '.$criteria->renderWhere();
630: }
631: if (!$result =& $this->db->query($sql)) {
632: return 0;
633: }
634: list($count) = $this->db->fetchRow($result);
635: return $count;
636: }
637:
638: 639: 640: 641: 642: 643: 644: 645: 646:
647: function &getList($criteria = null, $dirname_as_key = false)
648: {
649: $ret = array();
650: $modules =& $this->getObjects($criteria, true);
651: foreach ($modules as $i=>$module) {
652: if (!$dirname_as_key) {
653: $ret[$i] =& $module->getVar('name');
654: } else {
655: $ret[$module->getVar('dirname')] =& $module->getVar('name');
656: }
657: }
658: return $ret;
659: }
660: }
661: ?>
662: