1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
18:
19: 20: 21: 22: 23: 24:
25: class XoopsModule extends XoopsObject
26: {
27: 28: 29: 30:
31: public $modinfo;
32: 33: 34: 35:
36: public $adminmenu;
37: 38: 39: 40:
41: public $_msg;
42:
43: 44: 45:
46: public function __construct()
47: {
48: parent::__construct();
49: $this->initVar('mid', XOBJ_DTYPE_INT, null, false);
50: $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
51: $this->initVar('version', XOBJ_DTYPE_INT, 100, false);
52: $this->initVar('last_update', XOBJ_DTYPE_INT, null, false);
53: $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
54: $this->initVar('isactive', XOBJ_DTYPE_INT, 1, false);
55: $this->initVar('dirname', XOBJ_DTYPE_OTHER, null, true);
56: $this->initVar('hasmain', XOBJ_DTYPE_INT, 0, false);
57: $this->initVar('hasadmin', XOBJ_DTYPE_INT, 0, false);
58: $this->initVar('hassearch', XOBJ_DTYPE_INT, 0, false);
59: $this->initVar('hasconfig', XOBJ_DTYPE_INT, 0, false);
60: $this->initVar('hascomments', XOBJ_DTYPE_INT, 0, false);
61:
62: $this->initVar('hasnotification', XOBJ_DTYPE_INT, 0, false);
63: }
64:
65: 66: 67: 68: 69: 70:
71: public function loadInfoAsVar($dirname, $verbose = true)
72: {
73: $dirname = basename($dirname);
74: if (!isset($this->modinfo)) {
75: $this->loadInfo($dirname, $verbose);
76: }
77: $this->setVar('name', $this->modinfo['name'], true);
78: $this->setVar('version', (int)(100 * ((float) $this->modinfo['version'] + 0.001)), true);
79: $this->setVar('dirname', $this->modinfo['dirname'], true);
80: $hasmain = (isset($this->modinfo['hasMain']) && $this->modinfo['hasMain'] == 1) ? 1 : 0;
81: $hasadmin = (isset($this->modinfo['hasAdmin']) && $this->modinfo['hasAdmin'] == 1) ? 1 : 0;
82: $hassearch = (isset($this->modinfo['hasSearch']) && $this->modinfo['hasSearch'] == 1) ? 1 : 0;
83: $hasconfig = ((isset($this->modinfo['config']) && is_array($this->modinfo['config'])) || !empty($this->modinfo['hasComments'])) ? 1 : 0;
84: $hascomments = (isset($this->modinfo['hasComments']) && $this->modinfo['hasComments'] == 1) ? 1 : 0;
85:
86: $hasnotification = (isset($this->modinfo['hasNotification']) && $this->modinfo['hasNotification'] == 1) ? 1 : 0;
87: $this->setVar('hasmain', $hasmain);
88: $this->setVar('hasadmin', $hasadmin);
89: $this->setVar('hassearch', $hassearch);
90: $this->setVar('hasconfig', $hasconfig);
91: $this->setVar('hascomments', $hascomments);
92:
93: $this->setVar('hasnotification', $hasnotification);
94: }
95:
96: 97: 98: 99: 100: 101:
102: public function setMessage($str)
103: {
104: $this->_msg[] = trim($str);
105: }
106:
107: 108: 109: 110: 111: 112:
113: public function getMessages()
114: {
115: return $this->_msg;
116: }
117:
118: 119: 120: 121: 122: 123: 124:
125: public function setInfo($name, $value)
126: {
127: if (empty($name)) {
128: $this->modinfo = $value;
129: } else {
130: $this->modinfo[$name] = $value;
131: }
132:
133: return true;
134: }
135:
136: 137: 138: 139: 140: 141: 142:
143: public function &getInfo($name = null)
144: {
145: if (!isset($this->modinfo)) {
146: $this->loadInfo($this->getVar('dirname'));
147: }
148: if (isset($name)) {
149: if (isset($this->modinfo[$name])) {
150: return $this->modinfo[$name];
151: }
152: $return = false;
153:
154: return $return;
155: }
156:
157: return $this->modinfo;
158: }
159:
160: 161: 162: 163: 164:
165: public function mainLink()
166: {
167: if ($this->getVar('hasmain') == 1) {
168: $ret = '<a href="' . XOOPS_URL . '/modules/' . $this->getVar('dirname') . '/">' . $this->getVar('name') . '</a>';
169:
170: return $ret;
171: }
172:
173: return false;
174: }
175:
176: 177: 178: 179: 180:
181: public function subLink()
182: {
183: $ret = array();
184: if ($this->getInfo('sub') && is_array($this->getInfo('sub'))) {
185: foreach ($this->getInfo('sub') as $submenu) {
186: $ret[] = array(
187: 'name' => $submenu['name'],
188: 'url' => $submenu['url']);
189: }
190: }
191:
192: return $ret;
193: }
194:
195: 196: 197:
198: public function loadAdminMenu()
199: {
200: if ($this->getInfo('adminmenu') && $this->getInfo('adminmenu') != '' && file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu'))) {
201: $adminmenu = array();
202: include XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu');
203: $this->adminmenu =& $adminmenu;
204: }
205: }
206:
207: 208: 209: 210: 211:
212: public function &getAdminMenu()
213: {
214: if (!isset($this->adminmenu)) {
215: $this->loadAdminMenu();
216: }
217:
218: return $this->adminmenu;
219: }
220:
221: 222: 223: 224: 225: 226: 227: 228:
229: public function loadInfo($dirname, $verbose = true)
230: {
231: static $modVersions;
232: $dirname = basename($dirname);
233: if (isset($modVersions[$dirname])) {
234: $this->modinfo = $modVersions[$dirname];
235:
236: return true;
237: }
238: global $xoopsConfig;
239: if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/' . $xoopsConfig['language'] . '/modinfo.php'))) {
240: include_once $file;
241: } elseif (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/english/modinfo.php'))) {
242: include_once $file;
243: }
244:
245: if (!file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/xoops_version.php'))) {
246: if (false !== (bool)$verbose) {
247: echo "Module File for $dirname Not Found!";
248: }
249:
250: return false;
251: }
252: include $file;
253: $modVersions[$dirname] = $modversion;
254: $this->modinfo = $modVersions[$dirname];
255:
256: return true;
257: }
258:
259: 260: 261: 262: 263: 264: 265: 266: 267: 268:
269: public function search($term = '', $andor = 'AND', $limit = 0, $offset = 0, $userid = 0)
270: {
271: if ($this->getVar('hassearch') != 1) {
272: return false;
273: }
274: $search =& $this->getInfo('search');
275: if ($this->getVar('hassearch') != 1 || !isset($search['file']) || !isset($search['func']) || $search['func'] == '' || $search['file'] == '') {
276: return false;
277: }
278: if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/' . $search['file']))) {
279: include_once $file;
280: } else {
281: return false;
282: }
283: if (function_exists($search['func'])) {
284: $func = $search['func'];
285:
286: return $func($term, $andor, $limit, $offset, $userid);
287: }
288:
289: return false;
290: }
291:
292: 293: 294: 295: 296:
297: public function id($format = 'N')
298: {
299: return $this->getVar('mid', $format);
300: }
301:
302: 303: 304: 305: 306:
307: public function mid($format = '')
308: {
309: return $this->getVar('mid', $format);
310: }
311:
312: 313: 314: 315: 316:
317: public function name($format = '')
318: {
319: return $this->getVar('name', $format);
320: }
321:
322: 323: 324: 325: 326:
327: public function version($format = '')
328: {
329: return $this->getVar('version', $format);
330: }
331:
332: 333: 334: 335: 336:
337: public function last_update($format = '')
338: {
339: return $this->getVar('last_update', $format);
340: }
341:
342: 343: 344: 345: 346:
347: public function weight($format = '')
348: {
349: return $this->getVar('weight', $format);
350: }
351:
352: 353: 354: 355: 356:
357: public function isactive($format = '')
358: {
359: return $this->getVar('isactive', $format);
360: }
361:
362: 363: 364: 365: 366:
367: public function dirname($format = '')
368: {
369: return $this->getVar('dirname', $format);
370: }
371:
372: 373: 374: 375: 376:
377: public function hasmain($format = '')
378: {
379: return $this->getVar('hasmain', $format);
380: }
381:
382: 383: 384: 385: 386:
387: public function hasadmin($format = '')
388: {
389: return $this->getVar('hasadmin', $format);
390: }
391:
392: 393: 394: 395: 396:
397: public function hassearch($format = '')
398: {
399: return $this->getVar('hassearch', $format);
400: }
401:
402: 403: 404: 405: 406:
407: public function hasconfig($format = '')
408: {
409: return $this->getVar('hasconfig', $format);
410: }
411:
412: 413: 414: 415: 416:
417: public function hascomments($format = '')
418: {
419: return $this->getVar('hascomments', $format);
420: }
421:
422: 423: 424: 425: 426:
427: public function hasnotification($format = '')
428: {
429: return $this->getVar('hasnotification', $format);
430: }
431:
432: 433: 434: 435: 436:
437: public static function getByDirname($dirname)
438: {
439:
440: $modhandler = xoops_getHandler('module');
441: $inst = $modhandler->getByDirname($dirname);
442:
443: return $inst;
444: }
445:
446:
447:
448: 449: 450:
451: public function checkAccess()
452: {
453: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
454:
455: return false;
456: }
457:
458: 459: 460: 461: 462:
463: public function loadLanguage($type = 'main')
464: {
465: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
466:
467: return false;
468: }
469:
470: 471: 472:
473: public function loadErrorMessages()
474: {
475: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
476:
477: return false;
478: }
479:
480: 481: 482:
483: public function getCurrentPage()
484: {
485: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
486:
487: return false;
488: }
489:
490: 491: 492: 493: 494: 495:
496: public function install($admingroups = array(), $accessgroups = array())
497: {
498: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
499:
500: return false;
501: }
502:
503: 504: 505:
506: public function update()
507: {
508: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
509:
510: return false;
511: }
512:
513: 514: 515:
516: public function insert()
517: {
518: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
519:
520: return false;
521: }
522:
523: 524: 525:
526: public function executeSQL()
527: {
528: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
529:
530: return false;
531: }
532:
533: 534: 535:
536: public function insertTemplates()
537: {
538: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
539:
540: return false;
541: }
542:
543: 544: 545: 546: 547: 548:
549: public function gettemplate($template, $block = false)
550: {
551: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
552:
553: return false;
554: }
555:
556: 557: 558:
559: public function insertBlocks()
560: {
561: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
562:
563: return false;
564: }
565:
566: 567: 568:
569: public function insertConfigCategories()
570: {
571: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
572:
573: return false;
574: }
575:
576: 577: 578:
579: public function insertConfig()
580: {
581: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
582:
583: return false;
584: }
585:
586: 587: 588:
589: public function insertProfileFields()
590: {
591: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
592:
593: return false;
594: }
595:
596: 597: 598: 599: 600: 601:
602: public function executeScript($type, $state = 2)
603: {
604: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
605:
606: return false;
607: }
608:
609: 610: 611: 612: 613: 614:
615: public function insertGroupPermissions($groups, $type)
616: {
617: trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
618:
619: return false;
620: }
621:
622: }
623:
624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635:
636: class XoopsModuleHandler extends XoopsObjectHandler
637: {
638: 639: 640: 641: 642: 643:
644: public $_cachedModule_mid = array();
645:
646: 647: 648: 649: 650: 651:
652: public $_cachedModule_dirname = array();
653:
654: 655: 656: 657: 658: 659:
660: public function create($isNew = true)
661: {
662: $module = new XoopsModule();
663: if ($isNew) {
664: $module->setNew();
665: }
666:
667: return $module;
668: }
669:
670: 671: 672: 673: 674: 675:
676: public function get($id)
677: {
678: static $_cachedModule_dirname;
679: static $_cachedModule_mid;
680: $id = (int)$id;
681: $module = false;
682: if ($id > 0) {
683: if (!empty($_cachedModule_mid[$id])) {
684: return $_cachedModule_mid[$id];
685: } else {
686: $sql = 'SELECT * FROM ' . $this->db->prefix('modules') . ' WHERE mid = ' . $id;
687: if (!$result = $this->db->query($sql)) {
688: return $module;
689: }
690: $numrows = $this->db->getRowsNum($result);
691: if ($numrows == 1) {
692: $module = new XoopsModule();
693: $myrow = $this->db->fetchArray($result);
694: $module->assignVars($myrow);
695: $_cachedModule_mid[$id] = &$module;
696: $_cachedModule_dirname[$module->getVar('dirname')] = &$module;
697:
698: return $module;
699: }
700: }
701: }
702:
703: return $module;
704: }
705:
706: 707: 708: 709: 710: 711:
712: public function getByDirname($dirname)
713: {
714: $dirname = basename($dirname);
715:
716: if (strpos(strtolower($dirname), ' union ')) {
717: return false;
718: }
719: static $_cachedModule_mid;
720: static $_cachedModule_dirname;
721: if (!empty($_cachedModule_dirname[$dirname])) {
722: return $_cachedModule_dirname[$dirname];
723: } else {
724: $module = false;
725: $sql = 'SELECT * FROM ' . $this->db->prefix('modules') . " WHERE dirname = '" . trim($dirname) . "'";
726: if (!$result = $this->db->query($sql)) {
727: return $module;
728: }
729: $numrows = $this->db->getRowsNum($result);
730: if ($numrows == 1) {
731: $module = new XoopsModule();
732: $myrow = $this->db->fetchArray($result);
733: $module->assignVars($myrow);
734: $_cachedModule_dirname[$dirname] =& $module;
735: $_cachedModule_mid[$module->getVar('mid')] =& $module;
736: }
737:
738: return $module;
739: }
740: }
741:
742: 743: 744: 745: 746: 747: 748:
749: public function insert(XoopsObject $module)
750: {
751: $className = 'XoopsModule';
752: if (!($module instanceof $className)) {
753: return false;
754: }
755: if (!$module->isDirty()) {
756: return true;
757: }
758: if (!$module->cleanVars()) {
759: return false;
760: }
761: foreach ($module->cleanVars as $k => $v) {
762: ${$k} = $v;
763: }
764: if ($module->isNew()) {
765: $mid = $this->db->genId('modules_mid_seq');
766: $sql = sprintf('INSERT INTO %s (mid, name, version, last_update, weight, isactive, dirname, hasmain, hasadmin, hassearch, hasconfig, hascomments, hasnotification) VALUES (%u, %s, %u, %u, %u, %u, %s, %u, %u, %u, %u, %u, %u)', $this->db->prefix('modules'), $mid, $this->db->quoteString($name), $version, time(), $weight, 1, $this->db->quoteString($dirname), $hasmain, $hasadmin, $hassearch, $hasconfig, $hascomments, $hasnotification);
767: } else {
768: $sql = sprintf('UPDATE %s SET name = %s, dirname = %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), $version, time(), $weight, $isactive, $hasmain, $hasadmin, $hassearch, $hasconfig, $hascomments, $hasnotification, $mid);
769: }
770: if (!$result = $this->db->query($sql)) {
771: return false;
772: }
773: if (empty($mid)) {
774: $mid = $this->db->getInsertId();
775: }
776: $module->assignVar('mid', $mid);
777: if (!empty($this->_cachedModule_dirname[$dirname])) {
778: unset($this->_cachedModule_dirname[$dirname]);
779: }
780: if (!empty($this->_cachedModule_mid[$mid])) {
781: unset($this->_cachedModule_mid[$mid]);
782: }
783:
784: return true;
785: }
786:
787: 788: 789: 790: 791: 792: 793:
794: public function delete(XoopsObject $module)
795: {
796: $className = 'XoopsModule';
797: if (!($module instanceof $className)) {
798: return false;
799: }
800: $sql = sprintf('DELETE FROM %s WHERE mid = %u', $this->db->prefix('modules'), $module->getVar('mid'));
801: if (!$result = $this->db->query($sql)) {
802: return false;
803: }
804:
805: $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_admin' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
806: $this->db->query($sql);
807:
808: $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_read' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
809: $this->db->query($sql);
810:
811: $sql = sprintf('SELECT block_id FROM %s WHERE module_id = %u', $this->db->prefix('block_module_link'), $module->getVar('mid'));
812: if ($result = $this->db->query($sql)) {
813: $block_id_arr = array();
814: while ($myrow = $this->db->fetchArray($result)) {
815: $block_id_arr[] = $myrow['block_id'];
816: }
817: }
818:
819: if (isset($block_id_arr)) {
820: foreach ($block_id_arr as $i) {
821: $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);
822: if ($result2 = $this->db->query($sql)) {
823: if (0 < $this->db->getRowsNum($result2)) {
824:
825: $sql = sprintf('DELETE FROM %s WHERE (module_id = %u) AND (block_id = %u)', $this->db->prefix('block_module_link'), $module->getVar('mid'), $i);
826: $this->db->query($sql);
827: } else {
828:
829: $sql = sprintf('UPDATE %s SET visible = 0 WHERE bid = %u', $this->db->prefix('newblocks'), $i);
830: $this->db->query($sql);
831: $sql = sprintf('UPDATE %s SET module_id = -1 WHERE module_id = %u', $this->db->prefix('block_module_link'), $module->getVar('mid'));
832: $this->db->query($sql);
833: }
834: }
835: }
836: }
837:
838: if (!empty($this->_cachedModule_dirname[$module->getVar('dirname')])) {
839: unset($this->_cachedModule_dirname[$module->getVar('dirname')]);
840: }
841: if (!empty($this->_cachedModule_mid[$module->getVar('mid')])) {
842: unset($this->_cachedModule_mid[$module->getVar('mid')]);
843: }
844:
845: return true;
846: }
847:
848: 849: 850: 851: 852: 853: 854:
855: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
856: {
857: $ret = array();
858: $limit = $start = 0;
859: $sql = 'SELECT * FROM ' . $this->db->prefix('modules');
860: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
861: $sql .= ' ' . $criteria->renderWhere();
862: $sql .= ' ORDER BY weight ' . $criteria->getOrder() . ', mid ASC';
863: $limit = $criteria->getLimit();
864: $start = $criteria->getStart();
865: }
866: $result = $this->db->query($sql, $limit, $start);
867: if (!$result) {
868: return $ret;
869: }
870: while ($myrow = $this->db->fetchArray($result)) {
871: $module = new XoopsModule();
872: $module->assignVars($myrow);
873: if (!$id_as_key) {
874: $ret[] =& $module;
875: } else {
876: $ret[$myrow['mid']] =& $module;
877: }
878: unset($module);
879: }
880:
881: return $ret;
882: }
883:
884: 885: 886: 887: 888: 889:
890: public function getCount(CriteriaElement $criteria = null)
891: {
892: $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('modules');
893: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
894: $sql .= ' ' . $criteria->renderWhere();
895: }
896: if (!$result = $this->db->query($sql)) {
897: return 0;
898: }
899: list($count) = $this->db->fetchRow($result);
900:
901: return $count;
902: }
903:
904: 905: 906: 907: 908: 909: 910: 911:
912: public function getList(CriteriaElement $criteria = null, $dirname_as_key = false)
913: {
914: $ret = array();
915: $modules = $this->getObjects($criteria, true);
916: foreach (array_keys($modules) as $i) {
917: if (!$dirname_as_key) {
918: $ret[$i] = $modules[$i]->getVar('name');
919: } else {
920: $ret[$modules[$i]->getVar('dirname')] = $modules[$i]->getVar('name');
921: }
922: }
923:
924: return $ret;
925: }
926: }
927: