Location: PHPKode > projects > MercuryBoard > MercuryBoard/global.php
<?php
/*
global.php v3.2

Originally, we used a class so queries were $db->query($query), then
we decided that way sucked, and dropped this whole file, moving the
essential mysql login functions to the index. I now figure we'll try
itn without a class (so we don't need to go through the $db, which was
getting annoying). It also allows us flexibility with the database and
can later implement other db types.
-Jason 8/2/01

This file is being overhauled for the 1000th time, and it will become
class MercuryBoard, from which all other classes will extend.
Modulation, here we come!
-Jason 11/10/01

$usrdata replaced with $this->user. I hate globals, and with the new
template support, we need universal variables.
-Jason 12/8/01
*/

$internal_version = "1.00|Beta|2.0";

class MercuryBoard{

  var $title, $output=null, $user, $table, $etable, $post, $get, $cookie, $self, $ip, $agent, $query, $time, $width, $align, $temps;

  function MercuryBoard(){
    global $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $HTTP_GET_VARS;

    $this->temps  = $this->getTemplates();
    $this->query  = str_replace("&", "&amp;", getenv('QUERY_STRING'));
    $this->self   = getenv('PHP_SELF');
    $this->ip     = getenv('REMOTE_ADDR');
    $this->agent  = getenv('HTTP_USER_AGENT');
    $this->post   = $HTTP_POST_VARS;
    $this->get    = $HTTP_GET_VARS;
    $this->cookie = $HTTP_COOKIE_VARS;
    $this->time   = time();
    $this->width  = "95%";
    $this->align  = "center";
    $this->border = "#000000";
  }

  function emoticons($input){
    $emoticons = array(
      ':)'  => 'smile',
      ';)'  => 'wink',
      ':('  => 'sad',
      ':D'  => 'grin',
      ':p'  => 'tounge'
    );
    foreach($emoticons as $emoticon => $text){
      $input = str_replace($emoticon, "<img src='./emoticons/$text.gif' border='0' alt='Emoticon'>", $input);
    }

    $dir = opendir("emoticons");
    while($file = readdir($dir)){
      if(($file != ".") && ($file != "..")){
        $input = str_replace(":$file:", "<img src='./emoticons/$file' border='0' alt='Emoticon'>", $input);
      }
    }
    closedir($dir);

    return $input;
  }

  //ph33r /\/\y 1337 P\3g3>< 5|<1ll5!
  function mbcode($input){
    $input = preg_replace("/\[(\/*)([BIUSbius])\]/s", "<\\1\\2>", $input);

    //The next two lines of code have been combined by the third line, but unsuccessfully.
    //If you or anyone you know can fix this third line, please contact Regex's Most Wanted
    //at 555-5555. Take extreme caution, it can be considered armed and dangerous.

    $input = preg_replace("/\[url\](.+)\[\/url\]/i", "\\1", $input);
    $input = preg_replace("/[^=](http|https|ftp)(:\/\/\S+)/is", "<a href='\\1\\2' target='_blank'>\\1\\2</a>", $input);
    //$input = preg_replace("/[^=](\[url\]*)(http|https|ftp)(:\/\/\S+)(\[\/url\]*)/is", "<a href='\\2\\3' target='_blank'>\\2\\3</a>", $input);

    $input = preg_replace("/\[url=([^<]+?)\](.+?)\[\/url\]/i", "<a href='\\1' target='_blank'>\\2</a>", $input);
    $input = preg_replace("/\[img\](.+?)\[\/img\]/is", "<img src='\\1' border='0'>", $input);
    $input = preg_replace("/\[email\](.+?)\[\/email\]/is", "<a href='mailto:\\1' target='_blank'>\\1</a>", $input);
    $input = preg_replace("/\[quote\](.+)\[\/quote\]/is", "<b>Quote:</b><br><span class='quote'>\\1</span><br>", $input);
    $input = preg_replace("/\[color=(\S+?)\](.+?)\[\/color]/is","<font color='\\1'>\\2</font>", $input);
    $input = preg_replace("/\[font=(\S+?)\](.+?)\[\/font]/is","<font face='\\1'>\\2</font>", $input);
    $input = preg_replace("/\[size=([0-9]+?)\](.+?)\[\/size]/is","<font size='\\1'>\\2</font>", $input);
    return $input;
  }

  function showPostingOps(){
    global $a, $s, $m, $f, $t;
    $reply = null;
    if(isset($f)){
      if(isset($t)){
        $locks = fetch("SELECT State FROM topics WHERE TID='$t' AND Forum='$f'");
        if(!$locks['State']){
          $reply .= "<a href='$this->self?a=topic&amp;s=reply&amp;t=$t&amp;f=$f'><img src='./images/addreply.gif' border='0' alt='Reply to Topic'></a>";
        }else{
          $reply .= "<img src='./images/noreply.gif' border='0' alt='Topic Locked'>";
        }
      }
      $reply .= "&nbsp; <a href='$this->self?a=topic&amp;f=$f&amp;s=add'><img src='./images/newtopic.gif' border='0' alt='Create New Topic'></a>";
      $reply .= "&nbsp; <a href='$this->self?a=topic&amp;f=$f&amp;s=poll'><img src='./images/newpoll.gif' border='0' alt='Create New Poll'></a>";
    }
    return $reply;
  }

  function getPages($query, $link, $min = 0, $n = 10){

    $tq        = query($query);
    $records   = num_rows($tq);
    $pagelinks = null;

    if(($min-$n) >= 0){
      $prev = $min-$n;
      $pagelinks .= "[<a href='$this->self?$link&amp;min=0&amp;n=$n' class='small'>&lt;&lt;</a>]";
      $pagelinks .= "[<a href='$this->self?$link&amp;min=$prev&amp;n=$n' class='small'>prev</a>]";
    }else{
      $pagelinks .= "[&lt;&lt;][prev]";
    }

    $num_of_pages = ceil($records / $n);

    for($i=0; $i<($num_of_pages); $i++){
      $minpag = $i * $n;
      $page = $i+1;
      if($minpag != $min){
        $pagelinks .= "[<a href='$this->self?$link&amp;min=$minpag&amp;n=$n' class='small'>$page</a>]";
      }else{
        $pagelinks .= "[$page]";
      }
    }

    $max_page = ($num_of_pages-1) * $n;
    if(($min+$n) < $records){
      $next = $min+$n;
      $pagelinks .= "[<a href='$this->self?$link&amp;min=$next&amp;n=$n' class='small'>next</a>]";
      $pagelinks .= "[<a href='$this->self?$link&amp;min=$max_page&amp;n=$n' class='small'>&gt;&gt;</a>]";
    }else{
      $pagelinks .= "[next][&gt;&gt;]";
    }

    return $pagelinks;
  }
  
  function getTopicPages($records, $link, $sep, $min = 0, $n = 10){
    
    $records++;
    $pages    = ceil($records / $n);
    $max_page = ($pages-1) * $n;
    
    if($pages == 1) return null;
    
    $pagelinks = null;
    if($pages > 3){
      $countfor = 3;
    }else{
      $countfor = $pages;
    }
    for($i = 0; $i < $countfor; $i++){
      $minpag = $i * $n;
      $page   = $i + 1;
      $pagelinks .= "<a href='$this->self?$link&min=$minpag&n=$n' class='pages'>$page</a>$sep";
    }
    $pagelinks = eregi_replace("$sep$", "", $pagelinks);
    if($pages > 3){
      $pagelinks .= "$sep<a href='$this->self?$link&min=$max_page&n=$n' class='pages'>..$pages</a>";
    }
    $pagelinks = "( $pagelinks )";

    return $pagelinks;
  }

  //broken due to dozens of tree rewrites
  function makeForumJump(){
    global $PHP_SELF;

    $return = "
    <script language='JavaScript'>
    <!--
    function jump(){
      location.href=document.forumjump.forumselect.options[document.forumjump.forumselect.selectedIndex].value;
    }
    //-->
    </script>
    <form name='forumjump'>
    <select name='forumselect' onchange='javascript:jump()'>\n";

    $cat = query("SELECT CID, CName, Position FROM categories ORDER BY Position");

    while($catrow = nqfetch($cat)){
      $return .= "
      <option value='$PHP_SELF?c={$catrow['CID']}'>&gt; {$catrow['CName']}</option>";
      $query = query("SELECT FID, FName FROM forums WHERE Category='{$catrow['CID']}' ORDER BY Position");
      while($data = nqfetch($query)){
        $return .= "
        <option value='$PHP_SELF?a=forum&f={$data['FID']}'>&nbsp;&nbsp;+-- {$data['FName']}</option>";
      }
    }
    $return .= "
    </select>
    </form>";

    return $return;
  }

  function doDebug(){
    global $fulldebug, $load, $querycount, $query_log, $querytime, $totaltime, $PHP_SELF, $QUERY_STRING;
    ob_end_clean();
    ob_start("ob_gzhandler");
    $percent  = round(($querytime / $totaltime) * 100, 1);
    $normview = $PHP_SELF . "?" . substr($QUERY_STRING, 0, -8);
    echo "
    <html>
    <head>
    <title>MercuryBoard Debug</title>
    </head>
    <body bgcolor='#FFFFFF'>
    <font face='verdana' size='4' color='#660000'><b>mercuryboard debug</b></font>
    <font face='verdana' size='2'><br><br>
      <hr width='50%' align='left' NOSHADE size='1'>
        <a href='#stats' style='text-decoration:none;color:#660000'>Jump to statistics</a> - <a href='$normview' style='text-decoration:none;color:#660000'>Back to normal view</a>
      <hr width='50%' align='left' NOSHADE size='1'>
    </font>
    $fulldebug
    <a name='stats'></a>
    <font size='2' face='Courier New'><br>
      <b>MySQL queries</b>: $querycount<br>
      <b>Server Load</b>: $load<br>
      <b>Execution Time</b>: $totaltime<br>
      <b>Query Time</b>: $querytime ($percent%)<br><br>
      <b>Queries</b>:<br>
      $query_log
    </font>";
  }

  function getIcons(){
      $i     = 0;
      $icons = array();
      $dir   = opendir("mbicons");
      while($file = readdir($dir)){
        if(($file != ".") && ($file != "..") && !is_dir("./mbicons/$file")){
          $icons[$i] = $file;
        }
        $i++;
      }
      closedir($dir);
      natsort($icons);

      $msgicons = null;
      $i        = 0;
      while(list($var, $val) = each($icons)){
        if(($i%6 == 0) && ($i != 0)){
          $msgicons .= "\n</tr><tr>\n\n";
        }
        if($i == 0){
          $check = " checked";
        }else{
          $check = "";
        }
        $msgicons .= "
        <td align='center' valign='middle'>
          <input type='radio' name='icon' value='$i'$check>
          <img src='./mbicons/$val' border='0' alt='Message Icon'>
        </td>";
        $i++;
      }
      return $msgicons;
  }

  function getSettings(){
    $set_query = query("SELECT Setting, Value FROM settings");
    if($setting = nqfetch($set_query)){
      do{ $sets[$setting['Setting']] = $setting['Value']; }
      while($setting = nqfetch($set_query));
      return $sets;
    }else{
      return false;
    }
  }

  function setTitle($title){
    global $forum_name;
    $this->title = "$forum_name - $title";
  }

  function getTemplates(){
    global $a;
    $temp_query = query("SELECT Piece, Code FROM templates WHERE Template='0' AND Section='Main' OR Section='$a'");
    if($template = nqfetch($temp_query)){
      do{ $templates[$template['Piece']] = $template['Code']; }
      while($template = nqfetch($temp_query));
      return $templates;
    }else{
      return false;
    }
  }

  function addTemplates($section){
    $query = query("SELECT Piece, Code FROM templates WHERE Template='0' AND Section='$section'");
    while($template = nqfetch($query)){
      $this->temps[$template['Piece']] = $template['Code'];
    }
  }

function getServerLoad(){
  if(file_exists("/proc/loadavg")){
     $file = fopen("/proc/loadavg", "r");
     $load = @fread($file, 6);
     fclose($file);
     $loadavg = explode(" ", $load);
  }else if(substr(php_uname(), 0, 7) == "Windows"){
     return "Unknown";
  }else{
     $load = exec("uptime");
     $load = split("load averages?: ", $load);
     $loadavg = explode(",", $load[1]);
  }
  return(trim($loadavg[0]));
}

function numOfPMS($seen=0, $folder=0){
  if(!$seen){
    $unseen_nums = query("SELECT * FROM pmsystem WHERE ToWho='{$this->user['UID']}' AND Folder='$folder' AND Viewed='0'");
    $unseenPMS   = num_rows($unseen_nums);
    return $unseenPMS;
  }else{
    $seen_nums = query("SELECT * FROM pmsystem WHERE ToWho='{$this->user['UID']}' AND Folder='$folder'");
    $seenPMS   = num_rows($seen_nums);
    return $seenPMS;
  }
}

function makeTree($a){
  global $f, $t, $c, $s, $PHP_SELF, $forum_name;

  $tree = "<b>&raquo;</b> <a href='$PHP_SELF' class='small'>$forum_name</a>";

  if(isset($c)){
    $f = $c;
  }

  if(isset($f)){
    if(!isset($t)){
      $q = query("SELECT f.Tree, f.FID, f.FName FROM forums f ORDER BY f.FID");
    }else{
      $q = query("SELECT f.Tree, f.FID, f.FName, t.Title FROM forums f, topics t WHERE TID='$t' ORDER BY f.FID");
    }
    while($qr = nqfetch($q)){
      if(isset($t)) $topic = $qr['Title'];
      $id         = $qr['FID'];
      $fid[$id]   = $qr['FID'];
      $ftree[$id] = $qr['Tree'];
      $fname[$id] = $qr['FName'];
    }

    $cat = 1; //first one's always a category

    $ft = explode(",", $ftree[$f]);
    foreach($ft as $i){
      if($i){
        if(!$cat){
          $tree .= " <b>&raquo;</b> <a href='$PHP_SELF?a=forum&amp;f={$fid[$i]}' class='small'>{$fname[$i]}</a>";
        }else{
          $tree .= " <b>&raquo;</b> <a href='$PHP_SELF?c={$fid[$i]}' class='small'>{$fname[$i]}</a>";
          $cat   = 0;
        }
      }
    }
    $tree .= " <b>&raquo;</b> <a href='$PHP_SELF?a=forum&amp;f={$fid[$f]}' class='small'>{$fname[$f]}</a>";

    if(isset($t)){
      if(strlen($topic) > 30) $topic = substr($topic, 0, 29) . '...';
      $tree .= " <b>&raquo;</b> <span class='small'>$topic</span>";
    }
  }else if(($a != 'board') && ($a != 'forum') && ($a != 'topic') && ($a != 'ssi')){
    $text = array(
        'login'    =>  "Log In/Out",
        'user'     =>  "User Controls",
        'mod'      =>  "Moderator Controls",
        'search'   =>  "Search",
        'members'  =>  "Member List",
        'help'     =>  "Help",
        'print'    =>  "Print Page",
        'pm'       =>  "Messenger",
        'active'   =>  "Active Users",
        'cp'       =>  "Control Panel",
        'profile'  =>  "Profile",
        'register' =>  "Registering"
    );
    if(isset($text[$a])){
      $tree .= " <b>&raquo;</b> <a href='$PHP_SELF?a=$a' class='small'>$text[$a]</a>";
    }
  }
  return $tree;
}

function getUser(){
  global $HTTP_COOKIE_VARS;

  if(!isset($HTTP_COOKIE_VARS['mercury_user']) || !isset($HTTP_COOKIE_VARS['mercury_pass'])){
    return false;
  }

  $user = fetch("SELECT * FROM members m JOIN groups g WHERE m.UID='{$HTTP_COOKIE_VARS['mercury_user']}' AND g.GID=m.MemberGroup");
  if($user['Password'] == $HTTP_COOKIE_VARS['mercury_pass']){
    return $user;
  }else{
    // Cookie may have been hacked
    setcookie("mercury_user", "", time()-900);
    setcookie("mercury_pass", "", time()-900);
    return false;
  }
}

function message($title, $message, $align='left', $width='50%'){
  return eval(template('Message'));
}
}

if($QUERY_STRING == 'mb'){
  die("all your mercuryboards are belong to us<br><br>
  <img src='$PHP_SELF?=".php_logo_guid()."'>");
}

function error($number, $error=null, $file=null, $line=null){
  require("./func/error.php");
}

function template($piece){
  global $admin_replace, $mercury;
  if(empty($mercury->temps[$piece])){
    $code = fetch("SELECT Code FROM templates WHERE Piece='$piece'");
    $mercury->temps[$piece] = $code['Code'];
  }
  $mercury->temps[$piece] = ereg_replace("<ADMIN>(.*)</ADMIN>", $admin_replace, $mercury->temps[$piece]);
  return 'return "'. str_replace('"', '\\"', eregi_replace("\{\{([^\{\}]*)\}\}", "{\$\\1}", $mercury->temps[$piece])).'\n";';
}
?>
Return current item: MercuryBoard