XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
functions.php
Go to the documentation of this file.
1 <?php
19 defined('XOOPS_ROOT_PATH') or die('Restricted access');
20 
28 function &xoops_gethandler($name, $optional = false)
29 {
30  static $handlers;
31  $name = strtolower(trim($name));
32  if (!isset($handlers[$name])) {
33  if (file_exists($hnd_file = XOOPS_ROOT_PATH . '/kernel/' . $name . '.php')) {
34  require_once $hnd_file;
35  }
36  $class = 'Xoops' . ucfirst($name) . 'Handler';
37  if (class_exists($class)) {
39  $handlers[$name] = new $class($xoopsDB);
40  }
41  }
42  if (!isset($handlers[$name])) {
43  trigger_error('Class <strong>' . $class . '</strong> does not exist<br />Handler Name: ' . $name,
44  $optional ? E_USER_WARNING : E_USER_ERROR);
45  }
46  if (isset($handlers[$name])) {
47  return $handlers[$name];
48  }
49  $inst = false;
50  return $inst;
51 }
52 
61 function &xoops_getmodulehandler($name = null, $module_dir = null, $optional = false)
62 {
63  static $handlers;
64  // if $module_dir is not specified
65  if (!isset($module_dir)) {
66  // if a module is loaded
67  if (isset($GLOBALS['xoopsModule']) && is_object($GLOBALS['xoopsModule'])) {
68  $module_dir = $GLOBALS['xoopsModule']->getVar('dirname', 'n');
69  } else {
70  trigger_error('No Module is loaded', E_USER_ERROR);
71  }
72  } else {
73  $module_dir = trim($module_dir);
74  }
75  $name = (!isset($name)) ? $module_dir : trim($name);
76  if (!isset($handlers[$module_dir][$name])) {
77  if (file_exists($hnd_file = XOOPS_ROOT_PATH . "/modules/{$module_dir}/class/{$name}.php")) {
78  include_once $hnd_file;
79  }
80  $class = ucfirst(strtolower($module_dir)) . ucfirst($name) . 'Handler';
81  if (class_exists($class)) {
83  $handlers[$module_dir][$name] = new $class($xoopsDB);
84  }
85  }
86  if (!isset($handlers[$module_dir][$name])) {
87  trigger_error('Handler does not exist<br />Module: ' . $module_dir . '<br />Name: ' . $name,
88  $optional ? E_USER_WARNING : E_USER_ERROR);
89  }
90  if (isset($handlers[$module_dir][$name])) {
91  return $handlers[$module_dir][$name];
92  }
93  $inst = false;
94  return $inst;
95 }
96 
106 function xoops_load($name, $type = 'core')
107 {
108  if (!class_exists('XoopsLoad')) {
109  require_once XOOPS_ROOT_PATH . '/class/xoopsload.php';
110  }
111  return XoopsLoad::load($name, $type);
112 }
113 
126 function xoops_loadLanguage($name, $domain = '', $language = null)
127 {
131  if ($name == 'pagetype') {
132  $name = xoops_getOption('pagetype');
133  }
137  if (empty($name)) {
138  return false;
139  }
140  $language = empty($language) ? $GLOBALS['xoopsConfig']['language'] : $language;
141  $path = ((empty($domain) || 'global' == $domain) ? '' : "modules/{$domain}/" ) . 'language';
142  if (!file_exists($fileinc = $GLOBALS['xoops']->path("{$path}/{$language}/{$name}.php" ))) {
143  if (!file_exists( $fileinc = $GLOBALS['xoops']->path("{$path}/english/{$name}.php"))) {
144  return false;
145  }
146  }
147  $ret = include_once $fileinc;
148  return $ret;
149 }
150 
162 {
163  static $modules_active;
164  if (is_array($modules_active)) {
165  return $modules_active;
166  }
167  xoops_load('XoopsCache');
168  if (!$modules_active = XoopsCache::read('system_modules_active')) {
169  $modules_active = xoops_setActiveModules();
170  }
171  return $modules_active;
172 }
173 
185 {
186  xoops_load('XoopsCache');
187  $module_handler =& xoops_gethandler('module');
188  $modules_obj = $module_handler->getObjects(new Criteria('isactive', 1));
189  $modules_active = array();
190  foreach (array_keys($modules_obj) as $key) {
191  $modules_active[] = $modules_obj[$key]->getVar('dirname');
192  }
193  unset($modules_obj);
194  XoopsCache::write('system_modules_active', $modules_active);
195  return $modules_active;
196 }
197 
209 {
210  if (isset($dirname) && in_array($dirname, xoops_getActiveModules())) {
211  return true;
212  }
213  return false;
214 }
215 
222 function xoops_header($closehead = true)
223 {
224  global $xoopsConfig, $xoopsTheme, $xoopsConfigMetaFooter;
225 
227  if (!headers_sent()) {
228  header('Content-Type:text/html; charset=' . _CHARSET);
229  header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
230  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
231  header('Cache-Control: no-store, no-cache, max-age=1, s-maxage=1, must-revalidate, post-check=0, pre-check=0');
232  header("Pragma: no-cache");
233  }
234 
235  echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>\n";
236  echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . _LANGCODE . '" lang="' . _LANGCODE . '">
237  <head>
238  <meta http-equiv="content-type" content="text/html; charset=' . _CHARSET . '" />
239  <meta http-equiv="content-language" content="' . _LANGCODE . '" />
240  <meta name="robots" content="' . htmlspecialchars($xoopsConfigMetaFooter['meta_robots']) . '" />
241  <meta name="keywords" content="' . htmlspecialchars($xoopsConfigMetaFooter['meta_keywords']) . '" />
242  <meta name="description" content="' . htmlspecialchars($xoopsConfigMetaFooter['meta_desc']) . '" />
243  <meta name="rating" content="' . htmlspecialchars($xoopsConfigMetaFooter['meta_rating']) . '" />
244  <meta name="author" content="' . htmlspecialchars($xoopsConfigMetaFooter['meta_author']) . '" />
245  <meta name="copyright" content="' . htmlspecialchars($xoopsConfigMetaFooter['meta_copyright']) . '" />
246  <meta name="generator" content="XOOPS" />
247  <title>' . htmlspecialchars($xoopsConfig['sitename']) . '</title>
248  <script type="text/javascript" src="' . XOOPS_URL . '/include/xoops.js"></script>';
249  $themecss = xoops_getcss($xoopsConfig['theme_set']);
250  echo '<link rel="stylesheet" type="text/css" media="all" href="' . XOOPS_URL . '/xoops.css" />';
251  $language = xoops_getConfigOption('language');
252  if (file_exists($GLOBALS['xoops']->path('language/' . $language . '/style.css'))) {
253  echo '<link rel="stylesheet" type="text/css" media="all" href="' . XOOPS_URL . '/language/' . $language . '/style.css" />';
254  }
255  if ($themecss) {
256  echo '<link rel="stylesheet" type="text/css" media="all" href="' . $themecss . '" />';
257  }
258  if ($closehead) {
259  echo '</head><body>';
260  }
261 }
262 
268 function xoops_footer()
269 {
270  echo '</body></html>';
271  ob_end_flush();
272 }
273 
281 function xoops_error($msg, $title = '')
282 {
283  echo '<div class="errorMsg">';
284  if ($title != '') {
285  echo '<strong>' . $title . '</strong><br /><br />';
286  }
287  if (is_object($msg)) {
288  $msg = (array) $msg;
289  }
290  if (is_array($msg)) {
291  foreach ($msg as $key => $value) {
292  if (is_numeric($key)) {
293  $key = '';
294  }
295  xoops_error($value, $key);
296  }
297  } else {
298  echo "<div>{$msg}</div>";
299  }
300  echo '</div>';
301 }
302 
310 function xoops_result($msg, $title = '')
311 {
312  echo '<div class="resultMsg">';
313  if ($title != '') {
314  echo '<strong>' . $title . '</strong><br /><br />';
315  }
316  if (is_object($msg)) {
317  $msg = (array) $msg;
318  }
319  if (is_array($msg)) {
320  foreach ($msg as $key => $value) {
321  if (is_numeric($key)) {
322  $key = '';
323  }
324  xoops_result($value, $key);
325  }
326  } else {
327  echo "<div>{$msg}</div>";
328  }
329  echo '</div>';
330 }
331 
342 function xoops_confirm($hiddens, $action, $msg, $submit = '', $addtoken = true)
343 {
344  $submit = ($submit != '') ? trim($submit) : _SUBMIT;
345  echo '<div class="confirmMsg">' . $msg . '<br />
346  <form method="post" action="' . $action . '">';
347  foreach ($hiddens as $name => $value) {
348  if (is_array($value)) {
349  foreach ($value as $caption => $newvalue) {
350  echo '<input type="radio" name="' . $name . '" value="' . htmlspecialchars($newvalue) . '" /> ' . $caption;
351  }
352  echo '<br />';
353  } else {
354  echo '<input type="hidden" name="' . $name . '" value="' . htmlspecialchars($value) . '" />';
355  }
356  }
357  if ($addtoken != false) {
358  echo $GLOBALS['xoopsSecurity']->getTokenHTML();
359  }
360  echo '<input type="submit" name="confirm_submit" value="' . $submit . '" title="' . $submit . '"/>
361  <input type="button" name="confirm_back" value="' . _CANCEL . '" onclick="javascript:history.go(-1);" title="' . _CANCEL . '" />
362  </form>
363  </div>';
364 }
365 
373 function xoops_getUserTimestamp($time, $timeoffset = '')
374 {
375  global $xoopsConfig, $xoopsUser;
376  if ($timeoffset == '') {
377  if ($xoopsUser) {
378  $timeoffset = $xoopsUser->getVar('timezone_offset');
379  } else {
380  $timeoffset = $xoopsConfig['default_TZ'];
381  }
382  }
383  $usertimestamp = intval($time) + (floatval($timeoffset) - $xoopsConfig['server_TZ']) * 3600;
384  return $usertimestamp;
385 }
386 
390 function formatTimestamp($time, $format = 'l', $timeoffset = '')
391 {
392  xoops_load('XoopsLocal');
393  return XoopsLocal::formatTimestamp($time, $format, $timeoffset);
394 }
395 
399 function userTimeToServerTime($timestamp, $userTZ = null)
400 {
401  global $xoopsConfig;
402  if (!isset($userTZ)) {
403  $userTZ = $xoopsConfig['default_TZ'];
404  }
405  $timestamp = $timestamp - (($userTZ - $xoopsConfig['server_TZ']) * 3600);
406  return $timestamp;
407 }
408 
414 function xoops_makepass()
415 {
416  $makepass = '';
417  $syllables = array('er', 'in', 'tia', 'wol', 'fe', 'pre', 'vet', 'jo', 'nes',
418  'al', 'len', 'son', 'cha', 'ir', 'ler', 'bo', 'ok', 'tio',
419  'nar', 'sim', 'ple', 'bla', 'ten', 'toe', 'cho', 'co', 'lat',
420  'spe', 'ak', 'er', 'po', 'co', 'lor', 'pen','cil', 'li', 'ght',
421  'wh', 'at', 'the', 'he', 'ck', 'is', 'mam', 'bo', 'no', 'fi',
422  've', 'any', 'way', 'pol', 'iti', 'cs', 'ra', 'dio', 'sou',
423  'rce', 'sea', 'rch', 'pa', 'per', 'com', 'bo', 'sp', 'eak',
424  'st', 'fi', 'rst', 'gr', 'oup', 'boy', 'ea', 'gle', 'tr',
425  'ail', 'bi', 'ble', 'brb', 'pri', 'dee', 'kay', 'en', 'be', 'se');
426  srand((double) microtime() * 1000000);
427  for($count = 1; $count <= 4; $count ++) {
428  if (rand() % 10 == 1) {
429  $makepass .= sprintf('%0.0f', (rand() % 50) + 1);
430  } else {
431  $makepass .= sprintf('%s', $syllables[rand() % 62]);
432  }
433  }
434  return $makepass;
435 }
436 
444 function checkEmail($email, $antispam = false)
445 {
446  if (!$email || !preg_match('/^[^@]{1,64}@[^@]{1,255}$/', $email)) {
447  return false;
448  }
449  $email_array = explode("@", $email);
450  $local_array = explode(".", $email_array[0]);
451  for ($i = 0; $i < sizeof($local_array); $i++) {
452  if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/\=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/\=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
453  return false;
454  }
455  }
456  if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) {
457  $domain_array = explode(".", $email_array[1]);
458  if (sizeof($domain_array) < 2) {
459  return false; // Not enough parts to domain
460  }
461  for ($i = 0; $i < sizeof($domain_array); $i++) {
462  if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
463  return false;
464  }
465  }
466  }
467  if ($antispam) {
468  $email = str_replace("@", " at ", $email);
469  $email = str_replace(".", " dot ", $email);
470  }
471  return $email;
472 }
473 
480 function formatURL($url)
481 {
482  $url = trim($url);
483  if ($url != '') {
484  if ((!preg_match('/^http[s]*:\/\//i', $url))
485  && (!preg_match('/^ftp*:\/\//i', $url))
486  && (!preg_match('/^ed2k*:\/\//i', $url))
487  ) {
488  $url = 'http://' . $url;
489  }
490  }
491  return $url;
492 }
493 
497 function xoops_getbanner()
498 {
499  global $xoopsConfig;
500 
502  $bresult = $db->query('SELECT COUNT(*) FROM ' . $db->prefix('banner'));
503  list ($numrows) = $db->fetchRow($bresult);
504  if ($numrows > 1) {
505  $numrows = $numrows - 1;
506  mt_srand((double) microtime() * 1000000);
507  $bannum = mt_rand(0, $numrows);
508  } else {
509  $bannum = 0;
510  }
511  if ($numrows > 0) {
512  $bresult = $db->query('SELECT * FROM ' . $db->prefix('banner'), 1, $bannum);
513  list ($bid, $cid, $imptotal, $impmade, $clicks, $imageurl, $clickurl, $date, $htmlbanner, $htmlcode) = $db->fetchRow($bresult);
514  if ($xoopsConfig['my_ip'] == xoops_getenv('REMOTE_ADDR')) {
515  // EMPTY
516  } else {
517  $impmade = $impmade + 1;
518  $db->queryF(sprintf('UPDATE %s SET impmade = %u WHERE bid = %u', $db->prefix('banner'), $impmade, $bid));
522  if ($imptotal > 0 && $impmade >= $imptotal) {
523  $newid = $db->genId($db->prefix('bannerfinish') . '_bid_seq');
524  $sql = sprintf('INSERT INTO %s (bid, cid, impressions, clicks, datestart, dateend) VALUES (%u, %u, %u, %u, %u, %u)', $db->prefix('bannerfinish'), $newid, $cid, $impmade, $clicks, $date, time());
525  $db->queryF($sql);
526  $db->queryF(sprintf('DELETE FROM %s WHERE bid = %u', $db->prefix('banner'), $bid));
527  }
528  }
532  if ($htmlbanner) {
533  $bannerobject = $htmlcode;
534  } else {
535  $bannerobject = '<div id="xo-bannerfix">';
536  if (stristr($imageurl, '.swf')) {
537  $bannerobject = $bannerobject
538  . '<div id ="xo-fixbanner">'
539  . '<a href="' . XOOPS_URL . '/banners.php?op=click&amp;bid=' . $bid . '" rel="external" title="' . $clickurl . '"></a></div>'
540  . '<object type="application/x-shockwave-flash" width="468" height="60" data="' . $imageurl . '" style="z-index:100;">'
541  . '<param name="movie" value="' . $imageurl . '" />'
542  . '<param name="wmode" value="opaque" />'
543  . '</object>';
544 
545  } else {
546  $bannerobject = $bannerobject . '<a href="' . XOOPS_URL . '/banners.php?op=click&amp;bid=' . $bid . '" rel="external" title="' . $clickurl . '"><img src="' . $imageurl . '" alt="' . $clickurl . '" /></a>';
547  }
548 
549  $bannerobject = $bannerobject . '</div>';
550  }
551  return $bannerobject;
552  }
553 }
554 
558 function redirect_header($url, $time = 3, $message = '', $addredirect = true, $allowExternalLink = false)
559 {
561 
563  $xoopsPreload->triggerEvent('core.include.functions.redirectheader', array($url, $time, $message, $addredirect, $allowExternalLink));
564 
565  if (preg_match("/[\\0-\\31]|about:|script:/i", $url)) {
566  if (!preg_match('/^\b(java)?script:([\s]*)history\.go\(-[0-9]*\)([\s]*[;]*[\s]*)$/si', $url)) {
567  $url = XOOPS_URL;
568  }
569  }
570  if (!$allowExternalLink && $pos = strpos($url, '://')) {
571  $xoopsLocation = substr(XOOPS_URL, strpos(XOOPS_URL, '://') + 3);
572  if (strcasecmp(substr($url, $pos + 3, strlen($xoopsLocation)), $xoopsLocation)) {
573  $url = XOOPS_URL;
574  }
575  }
576  if (defined('XOOPS_CPFUNC_LOADED')) {
577  $theme = 'default';
578  } else {
579  $theme = $xoopsConfig['theme_set'];
580  }
581 
582  require_once XOOPS_ROOT_PATH . '/class/template.php';
583  require_once XOOPS_ROOT_PATH . '/class/theme.php';
584  $xoopsThemeFactory = null;
586  $xoopsThemeFactory->allowedThemes = $xoopsConfig['theme_set_allowed'];
587  $xoopsThemeFactory->defaultTheme = $theme;
588  $xoTheme =& $xoopsThemeFactory->createInstance(array(
589  "plugins" => array(), "renderBanner" => false));
590  $xoopsTpl =& $xoTheme->template;
591  $xoopsTpl->assign(array(
592  'xoops_theme' => $theme ,
593  'xoops_imageurl' => XOOPS_THEME_URL . '/' . $theme . '/' ,
594  'xoops_themecss' => xoops_getcss($theme) ,
595  'xoops_requesturi' => htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES) ,
596  'xoops_sitename' => htmlspecialchars($xoopsConfig['sitename'], ENT_QUOTES) ,
597  'xoops_slogan' => htmlspecialchars($xoopsConfig['slogan'], ENT_QUOTES) ,
598  'xoops_dirname' => isset($xoopsModule) && is_object($xoopsModule) ? $xoopsModule->getVar('dirname') : 'system' ,
599  'xoops_pagetitle' => isset($xoopsModule) && is_object($xoopsModule) ? $xoopsModule->getVar('name') : htmlspecialchars($xoopsConfig['slogan'], ENT_QUOTES)));
600  if ($xoopsConfig['debug_mode'] == 2 && $xoopsUserIsAdmin) {
601  $xoopsTpl->assign('time', 300);
602  $xoopsTpl->assign('xoops_logdump', $xoopsLogger->dump());
603  } else {
604  $xoopsTpl->assign('time', intval($time));
605  }
606  if (!empty($_SERVER['REQUEST_URI']) && $addredirect && strstr($url, 'user.php')) {
607  if (!strstr($url, '?')) {
608  $url .= '?xoops_redirect=' . urlencode($_SERVER['REQUEST_URI']);
609  } else {
610  $url .= '&amp;xoops_redirect=' . urlencode($_SERVER['REQUEST_URI']);
611  }
612  }
613  if (defined('SID')&& SID && (!isset($_COOKIE[session_name()]) || ($xoopsConfig['use_mysession'] && $xoopsConfig['session_name'] != '' && !isset($_COOKIE[$xoopsConfig['session_name']])))) {
614  if (!strstr($url, '?')) {
615  $url .= '?' . SID;
616  } else {
617  $url .= '&amp;' . SID;
618  }
619  }
620  $url = preg_replace("/&amp;/i", '&', htmlspecialchars($url, ENT_QUOTES));
621  $xoopsTpl->assign('url', $url);
622  $message = trim($message) != '' ? $message : _TAKINGBACK;
623  $xoopsTpl->assign('message', $message);
624  $xoopsTpl->assign('lang_ifnotreload', sprintf(_IFNOTRELOAD, $url));
625 
626  $xoopsTpl->display('db:system_redirect.html');
627  exit();
628 }
629 
636 function xoops_getenv($key)
637 {
638  $ret = '';
639  if (array_key_exists($key, $_SERVER) && isset($_SERVER[$key])) {
640  $ret = $_SERVER[$key];
641  return $ret;
642  }
643  if (array_key_exists($key, $_ENV) && isset($_ENV[$key])) {
644  $ret = $_ENV[$key];
645  return $ret;
646  }
647  return $ret;
648 }
649 
653 function xoops_getcss($theme = '')
654 {
655  if ($theme == '') {
656  $theme = $GLOBALS['xoopsConfig']['theme_set'];
657  }
658  $uagent = xoops_getenv('HTTP_USER_AGENT');
659  if (stristr($uagent, 'mac')) {
660  $str_css = 'styleMAC.css';
661  } elseif (preg_match("/MSIE ([0-9]\.[0-9]{1,2})/i", $uagent)) {
662  $str_css = 'style.css';
663  } else {
664  $str_css = 'styleNN.css';
665  }
666  if (is_dir(XOOPS_THEME_PATH . '/' . $theme)) {
667  if (file_exists(XOOPS_THEME_PATH . '/' . $theme . '/' . $str_css)) {
668  return XOOPS_THEME_URL . '/' . $theme . '/' . $str_css;
669  } elseif (file_exists(XOOPS_THEME_PATH . '/' . $theme . '/style.css')) {
670  return XOOPS_THEME_URL . '/' . $theme . '/style.css';
671  }
672  }
673  if (is_dir(XOOPS_THEME_PATH . '/' . $theme . '/css')) {
674  if (file_exists(XOOPS_THEME_PATH . '/' . $theme . '/css/' . $str_css)) {
675  return XOOPS_THEME_URL . '/' . $theme . '/css/' . $str_css;
676  } elseif (file_exists(XOOPS_THEME_PATH . '/' . $theme . '/css/style.css')) {
677  return XOOPS_THEME_URL . '/' . $theme . '/css/style.css';
678  }
679  }
680  return '';
681 }
682 
688 function &xoops_getMailer()
689 {
690  static $mailer;
691  global $xoopsConfig;
692  if (is_object($mailer)) {
693  return $mailer;
694  }
695  include_once XOOPS_ROOT_PATH . '/class/xoopsmailer.php';
696  if (file_exists($file = XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/xoopsmailerlocal.php')) {
697  include_once $file;
698  } else if (file_exists($file = XOOPS_ROOT_PATH . '/language/english/xoopsmailerlocal.php')) {
699  include_once $file;
700  }
701  unset($mailer);
702  if (class_exists('XoopsMailerLocal')) {
703  $mailer = new XoopsMailerLocal();
704  } else {
705  $mailer = new XoopsMailer();
706  }
707  return $mailer;
708 }
709 
717 function xoops_getrank($rank_id = 0, $posts = 0)
718 {
721  $rank_id = intval($rank_id);
722  $posts = intval($posts);
723  if ($rank_id != 0) {
724  $sql = "SELECT rank_title AS title, rank_image AS image FROM " . $db->prefix('ranks') . " WHERE rank_id = " . $rank_id;
725  } else {
726  $sql = "SELECT rank_title AS title, rank_image AS image FROM " . $db->prefix('ranks') . " WHERE rank_min <= " . $posts . " AND rank_max >= " . $posts . " AND rank_special = 0";
727  }
728  $rank = $db->fetchArray($db->query($sql));
729  $rank['title'] = $myts->htmlspecialchars($rank['title']);
730  $rank['id'] = $rank_id;
731  return $rank;
732 }
733 
743 function xoops_substr($str, $start, $length, $trimmarker = '...')
744 {
745  xoops_load('XoopsLocal');
746  return XoopsLocal::substr($str, $start, $length, $trimmarker);
747 }
748 // RMV-NOTIFY
749 // ################ Notification Helper Functions ##################
750 // We want to be able to delete by module, by user, or by item.
751 // How do we specify this??
753 {
754  $notification_handler =& xoops_gethandler('notification');
755  return $notification_handler->unsubscribeByModule($module_id);
756 }
757 
765 {
766  $notification_handler =& xoops_gethandler('notification');
767  return $notification_handler->unsubscribeByUser($user_id);
768 }
769 
778 function xoops_notification_deletebyitem($module_id, $category, $item_id)
779 {
780  $notification_handler =& xoops_gethandler('notification');
781  return $notification_handler->unsubscribeByItem($module_id, $category, $item_id);
782 }
783 
791 function xoops_comment_count($module_id, $item_id = null)
792 {
793  $comment_handler =& xoops_gethandler('comment');
794  $criteria = new CriteriaCompo(new Criteria('com_modid', intval($module_id)));
795  if (isset($item_id)) {
796  $criteria->add(new Criteria('com_itemid', intval($item_id)));
797  }
798  return $comment_handler->getCount($criteria);
799 }
800 
808 function xoops_comment_delete($module_id, $item_id)
809 {
810  if (intval($module_id) > 0 && intval($item_id) > 0) {
811  $comment_handler =& xoops_gethandler('comment');
812  $comments =& $comment_handler->getByItemId($module_id, $item_id);
813  if (is_array($comments)) {
814  $count = count($comments);
815  $deleted_num = array();
816  for($i = 0; $i < $count; $i++) {
817  if (false != $comment_handler->delete($comments[$i])) {
818  // store poster ID and deleted post number into array for later use
819  $poster_id = $comments[$i]->getVar('com_uid');
820  if ($poster_id != 0) {
821  $deleted_num[$poster_id] = !isset($deleted_num[$poster_id]) ? 1 : ($deleted_num[$poster_id] + 1);
822  }
823  }
824  }
825  $member_handler =& xoops_gethandler('member');
826  foreach ($deleted_num as $user_id => $post_num) {
827  // update user posts
828  $com_poster = $member_handler->getUser($user_id);
829  if (is_object($com_poster)) {
830  $member_handler->updateUserByField($com_poster, 'posts', $com_poster->getVar('posts') - $post_num);
831  }
832  }
833  return true;
834  }
835  }
836  return false;
837 }
838 
849 function xoops_groupperm_deletebymoditem($module_id, $perm_name, $item_id = null)
850 {
851  // do not allow system permissions to be deleted
852  if (intval($module_id) <= 1) {
853  return false;
854  }
855  $gperm_handler =& xoops_gethandler('groupperm');
856  return $gperm_handler->deleteByModule($module_id, $perm_name, $item_id);
857 }
858 
865 function xoops_utf8_encode(&$text)
866 {
867  xoops_load('XoopsLocal');
868  return XoopsLocal::utf8_encode($text);
869 }
870 
877 function xoops_convert_encoding(&$text)
878 {
879  return xoops_utf8_encode($text);
880 }
881 
888 function xoops_trim($text)
889 {
890  xoops_load('XoopsLocal');
891  return XoopsLocal::trim($text);
892 }
893 
905 function xoops_getOption($option)
906 {
907  $ret = '';
908  if (isset($GLOBALS['xoopsOption'][$option])) {
909  $ret = $GLOBALS['xoopsOption'][$option];
910  }
911  return $ret;
912 }
913 
914 
926 function xoops_getConfigOption($option, $type = 'XOOPS_CONF')
927 {
928  static $coreOptions = array();
929 
930  if (is_array($coreOptions) && array_key_exists($option, $coreOptions)) {
931  return $coreOptions[$option];
932  }
933  $ret = false;
934  $config_handler =& xoops_gethandler('config');
935  $configs = $config_handler->getConfigsByCat((is_array($type)) ? $type : constant($type));
936  if ($configs) {
937  if (isset($configs[$option])) {
938  $ret = $configs[$option];
939  }
940  }
941  $coreOptions[$option] = $ret;
942  return $ret;
943 }
944 
955 function xoops_setConfigOption($option, $new = null)
956 {
957  if (isset($GLOBALS['xoopsConfig'][$option]) && !is_null($new)) {
958  $GLOBALS['xoopsConfig'][$option] = $new;
959  }
960 }
961 
975 function xoops_getModuleOption($option, $dirname = '')
976 {
977  static $modOptions = array();
978  if (is_array($modOptions) && array_key_exists($option, $modOptions)) {
979  return $modOptions[$option];
980  }
981 
982  $ret = false;
983  $module_handler =& xoops_gethandler('module');
984  $module =& $module_handler->getByDirname($dirname);
985  $config_handler =& xoops_gethandler('config');
986  if (is_object($module)) {
987  $moduleConfig =& $config_handler->getConfigsByCat(0, $module->getVar('mid'));
988  if (isset($moduleConfig[$option])) {
989  $ret = $moduleConfig[$option];
990  }
991  }
992  $modOptions[$option] = $ret;
993  return $ret;
994 }
995 
1007 function xoops_getBaseDomain($url, $debug = 0)
1008 {
1009  $base_domain = '';
1010  $url = strtolower($url);
1011 
1012  // generic tlds (source: http://en.wikipedia.org/wiki/Generic_top-level_domain)
1013  $G_TLD = array(
1014  'biz','com','edu','gov','info','int','mil','name','net','org','aero','asia','cat','coop','jobs','mobi','museum','pro','tel','travel',
1015  'arpa','root','berlin','bzh','cym','gal','geo','kid','kids','lat','mail','nyc','post','sco','web','xxx',
1016  'nato', 'example','invalid','localhost','test','bitnet','csnet','ip','local','onion','uucp','co');
1017 
1018  // country tlds (source: http://en.wikipedia.org/wiki/Country_code_top-level_domain)
1019  $C_TLD = array(
1020  // active
1021  'ac','ad','ae','af','ag','ai','al','am','an','ao','aq','ar','as','at','au','aw','ax','az',
1022  'ba','bb','bd','be','bf','bg','bh','bi','bj','bm','bn','bo','br','bs','bt','bw','by','bz',
1023  'ca','cc','cd','cf','cg','ch','ci','ck','cl','cm','cn','co','cr','cu','cv','cx','cy','cz',
1024  'de','dj','dk','dm','do','dz','ec','ee','eg','er','es','et','eu','fi','fj','fk','fm','fo',
1025  'fr','ga','gd','ge','gf','gg','gh','gi','gl','gm','gn','gp','gq','gr','gs','gt','gu','gw',
1026  'gy','hk','hm','hn','hr','ht','hu','id','ie','il','im','in','io','iq','ir','is','it','je',
1027  'jm','jo','jp','ke','kg','kh','ki','km','kn','kr','kw','ky','kz','la','lb','lc','li','lk',
1028  'lr','ls','lt','lu','lv','ly','ma','mc','md','mg','mh','mk','ml','mm','mn','mo','mp','mq',
1029  'mr','ms','mt','mu','mv','mw','mx','my','mz','na','nc','ne','nf','ng','ni','nl','no','np',
1030  'nr','nu','nz','om','pa','pe','pf','pg','ph','pk','pl','pn','pr','ps','pt','pw','py','qa',
1031  're','ro','ru','rw','sa','sb','sc','sd','se','sg','sh','si','sk','sl','sm','sn','sr','st',
1032  'sv','sy','sz','tc','td','tf','tg','th','tj','tk','tl','tm','tn','to','tr','tt','tv','tw',
1033  'tz','ua','ug','uk','us','uy','uz','va','vc','ve','vg','vi','vn','vu','wf','ws','ye','yu',
1034  'za','zm','zw',
1035  // inactive
1036  'eh','kp','me','rs','um','bv','gb','pm','sj','so','yt','su','tp','bu','cs','dd','zr');
1037 
1038  // get domain
1039  if (!$full_domain = xoops_getUrlDomain($url)) {
1040  return $base_domain;
1041  }
1042 
1043  // break up domain, reverse
1044  $DOMAIN = explode('.', $full_domain);
1045  if ($debug) {
1046  print_r($DOMAIN);
1047  }
1048  $DOMAIN = array_reverse($DOMAIN);
1049  if ($debug) {
1050  print_r($DOMAIN);
1051  }
1052  // first check for ip address
1053  if (count($DOMAIN) == 4 && is_numeric($DOMAIN[0]) && is_numeric($DOMAIN[3])) {
1054  return $full_domain;
1055  }
1056 
1057  // if only 2 domain parts, that must be our domain
1058  if (count($DOMAIN) <= 2) {
1059  return $full_domain;
1060  }
1061 
1062  /*
1063  finally, with 3+ domain parts: obviously D0 is tld now,
1064  if D0 = ctld and D1 = gtld, we might have something like com.uk so,
1065  if D0 = ctld && D1 = gtld && D2 != 'www', domain = D2.D1.D0 else if D0 = ctld && D1 = gtld && D2 == 'www',
1066  domain = D1.D0 else domain = D1.D0 - these rules are simplified below.
1067  */
1068  if (in_array($DOMAIN[0], $C_TLD) && in_array($DOMAIN[1], $G_TLD) && $DOMAIN[2] != 'www') {
1069  $full_domain = $DOMAIN[2] . '.' . $DOMAIN[1] . '.' . $DOMAIN[0];
1070  } else {
1071  $full_domain = $DOMAIN[1] . '.' . $DOMAIN[0];
1072  }
1073  // did we succeed?
1074  return $full_domain;
1075 }
1076 
1088 {
1089  $domain = '';
1090  $_URL = parse_url($url);
1091 
1092  if (!empty($_URL) || !empty($_URL['host'])) {
1093  $domain = $_URL['host'];
1094  }
1095  return $domain;
1096 }
1097 
1098 include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'functions.encoding.php';
1099 include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'functions.legacy.php';
1100 
1101 ?>