XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
module.php
Go to the documentation of this file.
1 <?php
18 defined('XOOPS_ROOT_PATH') or die('Restricted access');
19 
26 class XoopsModule extends XoopsObject
27 {
32  var $modinfo;
42  var $_msg;
43 
47  function XoopsModule()
48  {
49  $this->XoopsObject();
50  $this->initVar('mid', XOBJ_DTYPE_INT, null, false);
51  $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
52  $this->initVar('version', XOBJ_DTYPE_INT, 100, false);
53  $this->initVar('last_update', XOBJ_DTYPE_INT, null, false);
54  $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
55  $this->initVar('isactive', XOBJ_DTYPE_INT, 1, false);
56  $this->initVar('dirname', XOBJ_DTYPE_OTHER, null, true);
57  $this->initVar('hasmain', XOBJ_DTYPE_INT, 0, false);
58  $this->initVar('hasadmin', XOBJ_DTYPE_INT, 0, false);
59  $this->initVar('hassearch', XOBJ_DTYPE_INT, 0, false);
60  $this->initVar('hasconfig', XOBJ_DTYPE_INT, 0, false);
61  $this->initVar('hascomments', XOBJ_DTYPE_INT, 0, false);
62  // RMV-NOTIFY
63  $this->initVar('hasnotification', XOBJ_DTYPE_INT, 0, false);
64  }
65 
72  function loadInfoAsVar($dirname, $verbose = true)
73  {
74  $dirname = basename($dirname);
75  if (!isset($this->modinfo)) {
76  $this->loadInfo($dirname, $verbose);
77  }
78  $this->setVar('name', $this->modinfo['name'], true);
79  $this->setVar('version', intval(100 * ($this->modinfo['version'] + 0.001)), true);
80  $this->setVar('dirname', $this->modinfo['dirname'], true);
81  $hasmain = (isset($this->modinfo['hasMain']) && $this->modinfo['hasMain'] == 1) ? 1 : 0;
82  $hasadmin = (isset($this->modinfo['hasAdmin']) && $this->modinfo['hasAdmin'] == 1) ? 1 : 0;
83  $hassearch = (isset($this->modinfo['hasSearch']) && $this->modinfo['hasSearch'] == 1) ? 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']) && $this->modinfo['hasComments'] == 1) ? 1 : 0;
86  // RMV-NOTIFY
87  $hasnotification = (isset($this->modinfo['hasNotification']) && $this->modinfo['hasNotification'] == 1) ? 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 
103  function setMessage($str)
104  {
105  $this->_msg[] = trim($str);
106  }
107 
114  function getMessages()
115  {
116  return $this->_msg;
117  }
118 
126  function setInfo($name, $value)
127  {
128  if (empty($name)) {
129  $this->modinfo = $value;
130  } else {
131  $this->modinfo[$name] = $value;
132  }
133  return true;
134  }
135 
143  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  return $return;
154  }
155  return $this->modinfo;
156  }
157 
163  function mainLink()
164  {
165  if ($this->getVar('hasmain') == 1) {
166  $ret = '<a href="' . XOOPS_URL . '/modules/' . $this->getVar('dirname') . '/">' . $this->getVar('name') . '</a>';
167  return $ret;
168  }
169  return false;
170  }
171 
177  function subLink()
178  {
179  $ret = array();
180  if ($this->getInfo('sub') && is_array($this->getInfo('sub'))) {
181  foreach ($this->getInfo('sub') as $submenu) {
182  $ret[] = array(
183  'name' => $submenu['name'] ,
184  'url' => $submenu['url']);
185  }
186  }
187  return $ret;
188  }
189 
193  function loadAdminMenu()
194  {
195  if ($this->getInfo('adminmenu') && $this->getInfo('adminmenu') != '' && file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu'))) {
196  $adminmenu = array();
197  include XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu');
198  $this->adminmenu =& $adminmenu;
199  }
200  }
201 
207  function &getAdminMenu()
208  {
209  if (!isset($this->adminmenu)) {
210  $this->loadAdminMenu();
211  }
212  return $this->adminmenu;
213  }
214 
221  function loadInfo($dirname, $verbose = true)
222  {
223  $dirname = basename($dirname);
224  global $xoopsConfig;
225  if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/' . $xoopsConfig['language'] . '/modinfo.php'))) {
226  include_once $file;
227  } else if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/english/modinfo.php'))) {
228  include_once $file;
229  }
230 
231  if (!file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/xoops_version.php'))) {
232  if (false != $verbose) {
233  echo "Module File for $dirname Not Found!";
234  }
235  return false;
236  }
237  include $file;
238  $this->modinfo = $modversion;
239  return true;
240  }
241 
252  function search($term = '', $andor = 'AND', $limit = 0, $offset = 0, $userid = 0)
253  {
254  if ($this->getVar('hassearch') != 1) {
255  return false;
256  }
257  $search =& $this->getInfo('search');
258  if ($this->getVar('hassearch') != 1 || ! isset($search['file']) || ! isset($search['func']) || $search['func'] == '' || $search['file'] == '') {
259  return false;
260  }
261  if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/' . $search['file']))) {
262  include_once $file;
263  } else {
264  return false;
265  }
266  if (function_exists($search['func'])) {
267  $func = $search['func'];
268  return $func($term, $andor, $limit, $offset, $userid);
269  }
270  return false;
271  }
272 
276  function id($format = 'N')
277  {
278  return $this->getVar('mid', $format);
279  }
280 
284  function mid($format = '')
285  {
286  return $this->getVar('mid', $format);
287  }
288 
292  function name($format = '')
293  {
294  return $this->getVar('name', $format);
295  }
296 
300  function version($format = '')
301  {
302  return $this->getVar('version', $format);
303  }
304 
308  function last_update($format = '')
309  {
310  return $this->getVar('last_update', $format);
311  }
312 
316  function weight($format = '')
317  {
318  return $this->getVar('weight', $format);
319  }
320 
324  function isactive($format = '')
325  {
326  return $this->getVar('isactive', $format);
327  }
328 
332  function dirname($format = '')
333  {
334  return $this->getVar('dirname', $format);
335  }
336 
340  function hasmain($format = '')
341  {
342  return $this->getVar('hasmain', $format);
343  }
344 
348  function hasadmin($format = '')
349  {
350  return $this->getVar('hasadmin', $format);
351  }
352 
356  function hassearch($format = '')
357  {
358  return $this->getVar('hassearch', $format);
359  }
360 
364  function hasconfig($format = '')
365  {
366  return $this->getVar('hasconfig', $format);
367  }
368 
372  function hascomments($format = '')
373  {
374  return $this->getVar('hascomments', $format);
375  }
376 
380  function hasnotification($format = '')
381  {
382  return $this->getVar('hasnotification', $format);
383  }
384 
385  function &getByDirName($dirname)
386  {
387  $modhandler =& xoops_gethandler('module');
388  $inst =& $modhandler->getByDirname($dirname);
389  return $inst;
390  }
391 
392  ##################### Deprecated Methods ######################
393 
397  function checkAccess()
398  {
399  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
400  return false;
401  }
402 
403  function loadLanguage($type = "main")
404  {
405  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
406  return false;
407  }
408 
409  function loadErrorMessages()
410  {
411  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
412  return false;
413  }
414 
415  function getCurrentPage()
416  {
417  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
418  return false;
419  }
420 
421  function install($admingroups = array(), $accessgroups = array())
422  {
423  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
424  return false;
425  }
426 
427  function update()
428  {
429  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
430  return false;
431  }
432 
433  function insert()
434  {
435  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
436  return false;
437  }
438 
439  function executeSQL()
440  {
441  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
442  return false;
443  }
444 
445  function insertTemplates()
446  {
447  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
448  return false;
449  }
450 
451  function gettemplate($template, $block = false)
452  {
453  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
454  return false;
455  }
456 
457  function insertBlocks()
458  {
459  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
460  return false;
461  }
462 
463  function insertConfigCategories()
464  {
465  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
466  return false;
467  }
468 
469  function insertConfig()
470  {
471  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
472  return false;
473  }
474 
475  function insertProfileFields()
476  {
477  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
478  return false;
479  }
480 
481  function executeScript($type, $state = 2)
482  {
483  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
484  return false;
485  }
486 
487  function insertGroupPermissions($groups, $type)
488  {
489  trigger_error(__CLASS__ . "::" . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
490  return false;
491  }
493 }
494 
506 {
513  var $_cachedModule_mid = array();
514 
521  var $_cachedModule_dirname = array();
522 
529  function &create($isNew = true)
530  {
531  $module = new XoopsModule();
532  if ($isNew) {
533  $module->setNew();
534  }
535  return $module;
536  }
537 
544  function &get($id)
545  {
546  static $_cachedModule_dirname;
547  static $_cachedModule_mid;
548  $id = intval($id);
549  $module = false;
550  if ($id > 0) {
551  if (!empty($_cachedModule_mid[$id])) {
552  return $_cachedModule_mid[$id];
553  } else {
554  $sql = 'SELECT * FROM ' . $this->db->prefix('modules') . ' WHERE mid = ' . $id;
555  if (!$result = $this->db->query($sql)) {
556  return $module;
557  }
558  $numrows = $this->db->getRowsNum($result);
559  if ($numrows == 1) {
560  $module = new XoopsModule();
561  $myrow = $this->db->fetchArray($result);
562  $module->assignVars($myrow);
563  $_cachedModule_mid[$id] = &$module;
564  $_cachedModule_dirname[$module->getVar('dirname')] = &$module;
565  return $module;
566  }
567  }
568  }
569  return $module;
570  }
571 
579  {
580  $dirname = basename($dirname);
581  //could not we check for spaces instead??
582  if (strpos(strtolower($dirname), ' union ')) {
583  return false;
584  }
585  static $_cachedModule_mid;
586  static $_cachedModule_dirname;
587  if (!empty($_cachedModule_dirname[$dirname])) {
589  } else {
590  $module = false;
591  $sql = "SELECT * FROM " . $this->db->prefix('modules') . " WHERE dirname = '" . trim($dirname) . "'";
592  if (!$result = $this->db->query($sql)) {
593  return $module;
594  }
595  $numrows = $this->db->getRowsNum($result);
596  if ($numrows == 1) {
597  $module = new XoopsModule();
598  $myrow = $this->db->fetchArray($result);
599  $module->assignVars($myrow);
601  $_cachedModule_mid[$module->getVar('mid')] =& $module;
602  }
603  return $module;
604  }
605  }
606 
613  function insert(&$module)
614  {
619  if (!is_a($module, 'xoopsmodule')) {
620  return false;
621  }
622  if (!$module->isDirty()) {
623  return true;
624  }
625  if (!$module->cleanVars()) {
626  return false;
627  }
628  foreach ($module->cleanVars as $k => $v) {
629  ${$k} = $v;
630  }
631  if ($module->isNew()) {
632  $mid = $this->db->genId('modules_mid_seq');
633  $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);
634  } else {
635  $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);
636  }
637  if (!$result = $this->db->query($sql)) {
638  return false;
639  }
640  if (empty($mid)) {
641  $mid = $this->db->getInsertId();
642  }
643  $module->assignVar('mid', $mid);
644  if (!empty($this->_cachedModule_dirname[$dirname])) {
645  unset($this->_cachedModule_dirname[$dirname]);
646  }
647  if (!empty($this->_cachedModule_mid[$mid])) {
648  unset($this->_cachedModule_mid[$mid]);
649  }
650  return true;
651  }
652 
659  function delete(&$module)
660  {
665  if (!is_a($module, 'xoopsmodule')) {
666  return false;
667  }
668  $sql = sprintf("DELETE FROM %s WHERE mid = %u", $this->db->prefix('modules'), $module->getVar('mid'));
669  if (!$result = $this->db->query($sql)) {
670  return false;
671  }
672  // delete admin permissions assigned for this module
673  $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_admin' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
674  $this->db->query($sql);
675  // delete read permissions assigned for this module
676  $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_read' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
677  $this->db->query($sql);
678 
679  $sql = sprintf("SELECT block_id FROM %s WHERE module_id = %u", $this->db->prefix('block_module_link'), $module->getVar('mid'));
680  if ($result = $this->db->query($sql)) {
681  $block_id_arr = array();
682  while ($myrow = $this->db->fetchArray($result)) {
683  array_push($block_id_arr, $myrow['block_id']);
684  }
685  }
686  // loop through block_id_arr
687  if (isset($block_id_arr)) {
688  foreach ($block_id_arr as $i) {
689  $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);
690  if ($result2 = $this->db->query($sql)) {
691  if (0 < $this->db->getRowsNum($result2)) {
692  // this block has other entries, so delete the entry for this module
693  $sql = sprintf("DELETE FROM %s WHERE (module_id = %u) AND (block_id = %u)", $this->db->prefix('block_module_link'), $module->getVar('mid'), $i);
694  $this->db->query($sql);
695  } else {
696  // 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!
697  $sql = sprintf("UPDATE %s SET visible = 0 WHERE bid = %u", $this->db->prefix('newblocks'), $i);
698  $this->db->query($sql);
699  $sql = sprintf("UPDATE %s SET module_id = -1 WHERE module_id = %u", $this->db->prefix('block_module_link'), $module->getVar('mid'));
700  $this->db->query($sql);
701  }
702  }
703  }
704  }
705 
706  if (!empty($this->_cachedModule_dirname[$module->getVar('dirname')])) {
707  unset($this->_cachedModule_dirname[$module->getVar('dirname')]);
708  }
709  if (!empty($this->_cachedModule_mid[$module->getVar('mid')])) {
710  unset($this->_cachedModule_mid[$module->getVar('mid')]);
711  }
712  return true;
713  }
714 
722  function getObjects($criteria = null, $id_as_key = false)
723  {
724  $ret = array();
725  $limit = $start = 0;
726  $sql = 'SELECT * FROM ' . $this->db->prefix('modules');
727  if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
728  $sql .= ' ' . $criteria->renderWhere();
729  $sql .= ' ORDER BY weight ' . $criteria->getOrder() . ', mid ASC';
730  $limit = $criteria->getLimit();
731  $start = $criteria->getStart();
732  }
733  $result = $this->db->query($sql, $limit, $start);
734  if (!$result) {
735  return $ret;
736  }
737  while ($myrow = $this->db->fetchArray($result)) {
738  $module = new XoopsModule();
739  $module->assignVars($myrow);
740  if (!$id_as_key) {
741  $ret[] =& $module;
742  } else {
743  $ret[$myrow['mid']] =& $module;
744  }
745  unset($module);
746  }
747  return $ret;
748  }
749 
756  function getCount($criteria = null)
757  {
758  $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('modules');
759  if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
760  $sql .= ' ' . $criteria->renderWhere();
761  }
762  if (!$result = $this->db->query($sql)) {
763  return 0;
764  }
765  list ($count) = $this->db->fetchRow($result);
766  return $count;
767  }
768 
777  function getList($criteria = null, $dirname_as_key = false)
778  {
779  $ret = array();
780  $modules = $this->getObjects($criteria, true);
781  foreach (array_keys($modules) as $i) {
782  if (!$dirname_as_key) {
783  $ret[$i] = $modules[$i]->getVar('name');
784  } else {
785  $ret[$modules[$i]->getVar('dirname')] = $modules[$i]->getVar('name');
786  }
787  }
788  return $ret;
789  }
790 }
791 
792 ?>