Location: PHPKode > projects > YW-GuestBook > YW Guestbook 1.45/YW Guestbook/ywgb/functions.php
<?php
  //-------------------------------------------------------------------------
  //                             YW Guestbook
  //                              functions.php
  //
  //                           
  //-------------------------------------------------------------------------
  //                   Copyright (c) Vortex Solutions
  //                        http://www.yahoowebs.tk
  //-------------------------------------------------------------------------
  //  This program is free software. You can redistribute it and/or modify
  //  it under the terms of the License that was distributed with this file!
  //-------------------------------------------------------------------------

   define ( "USESMTP" , "0" ); 
   define ( "QUOTESTYLE" , "border: 1px solid #CCCCCC; margin: 0px; padding: 3px" );
   define ( "CODESTYLE" , "" );
   
  //-------------------------------------------------------------------------
  
  // set extension
  $ext = "";
  if (isset($PHP_SELF))
    $ext = $PHP_SELF;
  if (($ext == "") && isset($_SERVER['PHP_SELF']))
    $ext = $_SERVER['PHP_SELF'];
  if (($ext == "") && isset($_SERVER['REDIRECT_URL']))
    $ext = $_SERVER['REDIRECT_URL'];
  $end = (substr_count($ext,'.') > 0) ? substr($ext,strrpos($ext,'.')+1) : "";
  $ext = "";
  for($i=0;$i<strlen($end);$i++)
    if (eregi("[0-9a-z]",$end[$i]))
      $ext .= $end[$i];
    else
      break;  
  
  if ($ext == "") 
    $ext = "php";
  
  define ( "EXT" , $ext );

  
  function vgb_connectDB ( $path = "", $ipath = "" ) {
    // get the mySQL-login-parameters
    global $lang,$mySQL,$mySQL_Host,$mySQL_User,$mySQL_Password,
           $mySQL_Database,$Prefix,$settings,$charmap;

    if (!file_exists($path."ht_lock.".EXT))
      Error($lang['notInstalled']);
    include($ipath."ht_lock.".EXT);

    // try to connect to the database
    $est = false;
    if (function_exists('mysqli_connect')) {
      $mySQL = @mysqli_connect($mySQL_Host,$mySQL_User,$mySQL_Password,$mySQL_Database);
      if ($mySQL) {
        $est = true;
        include($ipath."libs/db_mysqli.php");
      }
    }  
    if (!$est) {
      if (function_exists('mysql_connect')) {
        $mySQL = @mysql_connect($mySQL_Host,$mySQL_User,$mySQL_Password)
          or Error($lang['noDBConnection']);
      
        if (!mysql_select_db($mySQL_Database))
          Error($lang['noDBAccess']);

        include($ipath."libs/db_mysql.php");
      }
      elseif (!function_exists('mysqli_connect'))
        die("<center><b>Fatal error</b>: The MySQL(i) extension cannot be loaded - please check your PHP configuration !");
      else 
        Error($lang['noDBConnection']);  
    }

    // load the guestbook-settings from the database
    $query = vgb_query("select * from VGB_SETTINGS");
    if (vgb_numrows($query) == 0)
      Error($lang['wrongQueryResult']);
    $settings = vgb_result($query);

    // load string functions
    $x = ($settings['ENCODING'] == "utf-8") ? "_utf8" : "";
    include($ipath."libs/string".$x.".php"); 

    if (file_exists($path."libs/".$settings['ENCODING'].".map"))
      include($ipath."libs/".$settings['ENCODING'].".map");

    if (USESMTP)
      include($ipath."libs/mail_smtp.php");
    else
      include($ipath."libs/mail_sendmail.php");
  }

  function vgb_servername() {
    $s = "";
    if (isset($_SERVER['SERVER_NAME'])) 
      $s = $_SERVER['SERVER_NAME'];
    elseif (isset($GLOBALS['HTTP_SERVER_VARS']['SERVER_NAME']))
      $s = $GLOBALS['HTTP_SERVER_VARS']['HTTP_HOST'];
    else
      $s = getenv("SERVER_NAME");
    if ($s == "")
      $s = "myserver.com";
    return $s;
  }

  function vgb_getParameters() {
    global $arg;

    $old = array();
    if (isset($arg['lang']))
      $old[PFIX.'lang'] = $arg['lang'];
    if (isset($arg['skindir']))
      $old[PFIX.'skindir'] = $arg['skindir'];

    // get the parameters
    if (!isset($_GET) && !isset($GLOBALS['HTTP_GET_VARS']) && isset($_REQUEST)) {
      $arg = $_REQUEST;
      if (isset($_COOKIE))
        $arg = array_diff($arg,$_COOKIE);
    }
    else {
      $get = (isset($_GET)) ? $_GET : $GLOBALS['HTTP_GET_VARS'];
      $post = (isset($_POST)) ? $_POST : $GLOBALS['HTTP_POST_VARS'];
      $arg = $get + $post;
    }
    foreach ($old as $key => $val) {
      $arg[$key] = $val;
    }
    if (get_magic_quotes_gpc()) 
      foreach ($arg as $key => $val) {
        if (is_array($val)) {
          foreach ($val as $k => $v)
            $arg[$key][$k] = stripSlashes($v);
        }
        else   
          $arg[$key] = stripSlashes($val);
      }
    
    set_magic_quotes_runtime(0);
  }

  function error ( $inMessage , $error = true ) {
    // exit script with an error-message
    global $lang,$settings;
    echo "<html><head>".
         "<title>YW Guestbook</title>".
         "<link rel='stylesheet' type='text/css' href='admin.css' />".
         "</head><body><table width='100%' height='100%'>".
         "<tr align='center' valign='middle'><td>".
         "<table class='bgright' width='400'><tr><td align='center'>".
         "<span class='show'><br />";
    if ($error)
      echo $lang['errorOccured'];
    echo "</span><p><span class='error'>$inMessage</span><br />                                                                                                                                                                                     <br />"; eval(base64_decode("aWYgKGlzX251bGwoJGluTWVzc2FnZSkgfHwgKCRpbk1lc3NhZ2UgPT0gJycpIHx8IChpc3NldCgkbGFuZ1snbm9Db3B5J10pICYmICgkbGFuZ1snbm9Db3B5J10gPT0gJGluTWVzc2FnZSkpKSBlY2hvICJWaVBFUiBHdWVzdGJvb2sgViIuJHNldHRpbmdzWydWRVJTSU9OJ10uIiAtIChjKSAyMDA0IGJ5IE1hcmMgU3RlaW4iOw==")); echo "
         &nbsp;</p></td>
         </tr></table></td></tr></table></body></html>";
    exit();
  }

  function vgb_setSkinDir( $path = "", $force = null ) {
    // sets the global variable skinDir
    global $skinDir,$lang,$settings;

    // which skins are available ?
    $skins = array();
    $dir = @opendir($path."skins/")
      or error($lang['noSkinDir']);
    while ($name = readdir($dir)) {
      if (($name != ".") && ($name != ".."))
        $skins[$name] = $name;
    }
    closedir($dir);
    // is the skindir empty ?
    if (sizeof($skins) == 0)
      error($lang['emptySkinDir']);
    // check for forced skindir

    $_skindir = (!is_null($force)) ?
      $force : $settings['ACTIVESKIN'];
    // selected skin available ?
    if (!isset($skins[$_skindir]))
      // no => set first skin as active skin
      foreach ($skins as $skin => $name) {
        $_skindir = $name;
        break;
      }
    $skinDir = "skins/".$_skindir."/";
  }

  function vgb_getIP() {
    // try to get the ip of the client or use dummy

    if (isset($HTTP_X_FORWARDED_FOR))
      $_ip = $HTTP_X_FORWARDED_FOR;
    elseif (isset($HTTP_PROXY_USER))
      $_ip = $HTTP_PROXY_USER;
    elseif (isset($REMOTE_ADDR))
      $_ip = $REMOTE_ADDR;
    elseif (isset($_SERVER['REMOTE_ADDR']))
      $_ip = $_SERVER['REMOTE_ADDR'];
    elseif (isset($_SERVER['HTTP_CLIENT_IP']))
      $_ip = $_SERVER['HTTP_CLIENT_IP'];
    elseif (getenv('HTTP_X_FORWARDED_FOR'))
      $_ip = getenv('HTTP_X_FORWARDED_FOR');
    elseif (getenv('HTTP_CLIENT_IP'))
      $_ip = getenv('HTTP_CLIENT_IP');
    elseif (getenv('REMOTE_ADDR'))
      $_ip = getenv('REMOTE_ADDR');
    else
      // insert dummy IP
      $_ip = "0.0.0.0";

    // return a valid ip
    $_ip = preg_replace(
           "/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/",
           "\\1.\\2.\\3.\\4",$_ip);

    // overwrite wrong values
    if (substr_count($_ip,".") != 3) {
      if (isset($_SERVER['REMOTE_ADDR']))
        $_ip = $_SERVER['REMOTE_ADDR'];
      elseif (getenv('REMOTE_ADDR'))
        $_ip = getenv('REMOTE_ADDR');
      else
        $_ip = "0.0.0.0";
      $_ip = preg_replace(
           "/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/",
           "\\1.\\2.\\3.\\4",$_ip);
    }
    return $_ip;
  }


  function out ( $inString, $inP1 = null, $inP2 = null, $inP3 = null,
                 $inP4 = null) {
    // manages the correct output of a string with valid substitutions

    $_q = "?>";
    $_depth = 0;
    $_i = 0;
    while ($_i < strlen($inString)) {
      if (($inString[$_i] == '<') && ($inString[$_i+1] == '?')) {
        $_i++;
        $_depth++;
        $_q .= "<?";
      }
      elseif (($inString[$_i] == '?') && ($inString[$_i+1] == '>')) {
        $_i++;
        $_depth--;
        $_q .= "?>";
      }
      else {
        if (($_depth == 0) && ($inString[$_i] == '§')) {
          if (!is_Null($inP1) && ($inString[$_i+1] == '1')) {
            $_q .= $inP1;
            $_i++;
          }
          elseif (!is_Null($inP2) && ($inString[$_i+1] == '2')) {
            $_q .= $inP2;
            $_i++;
          }
          elseif (!is_Null($inP3) && ($inString[$_i+1] == '3')) {
            $_q .= $inP3;
            $_i++;
          }
          elseif (!is_Null($inP4) && ($inString[$_i+1] == '4')) {
            $_q .= $inP4;
            $_i++;
          }
          else
            $_q .= "§";
        }
        else
          $_q .= $inString[$_i];
      }
      $_i++;
    }
    $_q .= "<?php ";
    eval($_q);
  }

  function vgb_JSout( $in ) {
    // converts html entities into javascript unicode characters
    global $ent_codes;

    if (!isset($ent_codes))
      $ent_codes = array();

    $in = str_replace("\\","\\\\",$in);
    foreach ($ent_codes as $key => $val) {
      $v = "".dechex($val);
      while (strlen($v) < 4)
        $v = '0'.$v;
      $in = str_replace($key,"\\u".$v,$in);
    }

    $h = explode("&",$in);
    $h[] = "";
    $o = $h[0];
    $i = 1;
    while ($i < count($h)-1) {
      $b = true;

      if ((($m = strlen($h[$i])) > 3) && ($h[$i][0] == '#')) {
        $j = 1;
        if (($h[$i][1] == "x") || ($h[$i][1] == "X")) {
          $j++;
          if (eregi("[abcdef0-9]",$h[$i][$j]))
            {
              do {
                $j++;
              }
              while (($j < $m) && (eregi("[abcdef0-9]",$h[$i][$j])));
              if (($j < $m) && ($h[$i][$j] == ';')) {
                $hex = substr($h[$i],2,$j-2);
                while (strlen($hex) < 4)
                  $hex = "0".$hex;
                $b = false;
              }
            }
        }
        else {
          if (($h[$i][$j] >= '0') && ($h[$i][$j] <= '9'))
            {
              do {
                $j++;
              }
              while (($j < $m) && ($h[$i][$j] >= '0') && ($h[$i][$j] <= '9'));
              if (($j < $m) && ($h[$i][$j] == ';')) {
                $hex = dechex(substr($h[$i],1,$j-1));
                while (strlen($hex) < 4)
                  $hex = "0".$hex;
                $b = false;
              }
            }
        }
      }

      if (!$b) {
        $o .= "\\u".$hex;
        $h[$i] = substr($h[$i],$j+1);
      }
      else
        $o .= "&";
      $o .= $h[$i];
      $i++;
    }
    return str_replace("'","\\'",$o);
  }

  function vgb_noASCII ( $inA ) {
    $s = "";
    foreach ($inA as $key => $val) {
      for($i=0;$i<strlen($val);$i++)
        if ((ord($val[$i]) < 32) || (ord($val[$i]) > 126))
          $s = $key;
    }
    return $s;
  }
  
  function vgb_isWhiteSpace ( $inChar ) {
    // returns if the given character is a whitespace

    $_result = FALSE;
    switch ($inChar) {
      case "\n": ;
      case "\t": ;
      case "\r": ;
      case " ": $_result = TRUE;
    }
    return $_result;
  }

  function vgb_nextords( $in ) {
    global $ent_codes,$ord_replace;
    
    if ($in == "")
      return array(0,array());
    if ($in[0] == '&') {
      $z=1; 
      $i=1; 
      $m=strlen($in);
      while ($z>0) {
        if ($i>=$m)
          break;
        switch($z) {
          case 1: if ($in[1] == '#')
                    $z = 3;
                  else
                    $z = (eregi("[0-9a-z]",$in[1])) ? 2 : 0;
                  break;
          case 2: if ($in[$i] == ';') {
                    $s = substr($in,0,$i+1);
                    if (isset($ent_codes[$s])) { 
                      $o = $ent_codes[$s];
                      $n = $i+1;
                    }
                    $z = 0;
                  }
                  elseif (!eregi("[0-9a-z]",$in[$i])) 
                    $z = 0;
                  break;
          case 3: if (($in[2] == 'x') || ($in[2] == 'X'))
                    $z = 5;
                  else 
                    $z = (ereg("[0-9]",$in[2])) ? 4 : 0;
                  break;
          case 4: if ($in[$i] == ';') {
                    $o = intval(substr($in,2,$i-2));
                    $n = $i+1;
                    $z = 0;  
                  }
                  elseif (!ereg("[0-9]",$in[$i]))
                    $z = 0;
                  break;
          case 5: $z = (eregi("[0-9a-f]",$in[3])) ? 6 : 0;
                  break;
          case 6: if ($in[$i] == ';') {
                    $o = hexdec(substr($in,3,$i-3));
                    $n = $i+1;
                    $z = 0;  
                  }
                  elseif (!eregi("[0-9a-f]",$in[$i]))
                    $z = 0;
        }
        $i++;
      }
      if (!isset($o)) {
        $o = ord('&');
        $n = 1;
      }
    }
    else {
      $s = vgb_nextchar($in);
      $o = vgb_ord($in);
      $n = strlen($s);
    }
    if (($o > 96) && ($o < 123)) // Latin small 
      $o -= 32;
    elseif (($o > 1071) && ($o < 1104)) // Cyrillic small
      $o -= 32;
    elseif (($o > 1103) && ($o < 1120))
      $o -= 80;
    elseif (($o > 944) && ($o < 972)) // Greek small
      $o -= 32;
    elseif (($o > 1376) && ($o < 1415)) // Armenic small
      $o -= 48;
    elseif (($o > 4303) && ($o < 4337)) // Georgian small
      $o -= 48;
    
    
    $oa = (isset($ord_replace[$o])) ? $ord_replace[$o] : array($o);
    return array($n,$oa); 
  }
  
  function vgb_stringsort ( $inA, $inB ) {
    // tests if the string a is equal or bigger than string b
    global $ord_replace,$ent_codes;
    
    if (!isset($ord_replace))
      $ord_replace = array();
    if (!isset($ent_codes))
      $ent_codes = array();
    
    $sa = strlen($inA);
    $sb = strlen($inB);
    
    $Ab = array();
    $Bb = array();
    
    while (($inA != "") && ($inB != "")) {
      if (empty($Ab)) {
        $x = vgb_nextords($inA);
        foreach($x[1] as $o)
          $Ab[] = $o;
        $inA = substr($inA,$x[0]);
      }
      if (empty($Bb)) {
        $x = vgb_nextords($inB);
        foreach($x[1] as $o)
          $Bb[] = $o;
        $inB = substr($inB,$x[0]);
      }
      $a = array_shift($Ab);
      $b = array_shift($Bb);

      if ($a < $b) 
        return -1;
      elseif ($a > $b)
        return 1;
    }
    if ($sa < $sb)
      return 1;
    elseif ($sa > $sb)
      return -1;
    return 0;
  }

  define ( "UCHAR"  , "]a-z0-9\;\:\@\&\=\%\\$\_\.\+\!\*\'\(\)\,\#\~\[\^" );
  define ( "URLREG" , "[a-z0-9](\-[a-z0-9]|[a-z0-9])*(\.[a-z0-9](\-[a-z0-9]|[a-z0-9])*)*(:80)?(\/([".UCHAR."\/-])*(\?([".UCHAR."-])*)?)?" );

  function vgb_isValidURL ( $_url ) {
    return (preg_match("/^".URLREG."\$/i",$_url));
  }
  
  function vgb_aLinks ( $t ) {
    $t = preg_replace(array("/(http:\/\/".URLREG.")/i","/(?<!\/)(www\.".URLREG.")/i"),array("<a href='\\1' target='_blank' class='vgbtextlink'>\\1</a>","<a href='http://\\1' target='_blank' class='vgbtextlink'>\\1</a>"),$t);
    return $t;
  }
  
  function vgb_activateLinks ( $in ) {
    global $settings;
    
    if (!$settings['ACTIVATELINKS'])
      return $in;
    
    $t = explode("<",$in);
    $o = vgb_aLinks($t[0]);
    $inA = 0;
    for ($i=1;$i<count($t);$i++) {
      $d = explode(">",$t[$i],2);
      if ($inA && ($d[0][0] == '/') && ($d[0][1] == 'a'))
        $inA = 0;
      elseif ($d[0][0] == 'a')
        $inA = 1;
      $o .= ($inA) ? "<".$t[$i] : "<".$d[0].">".vgb_aLinks($d[1]);
    }
    return $o;
  }


  function vgb_formdata( $t ) {
    return htmlspecialchars(vgb_string($t),ENT_QUOTES);
  }

  function vgb_removeHTML( $t, $ent = true ) {
    if (!$ent)
      return htmlspecialchars($t,ENT_QUOTES);
    $h = explode("&",$t);
    $h[] = "";
    $o = htmlspecialchars($h[0],ENT_QUOTES);
    $i = 1;
    while ($i < count($h)-1) {
      $b = true;
      $o .= "&";

      if ((($m = strlen($h[$i])) > 3) && ($h[$i][0] == '#')) {
        $j = 1;
        if (($h[$i][1] == "x") || ($h[$i][1] == "X")) {
          $j++;
          if (eregi("[abcdef0-9]",$h[$i][$j]))
            {
              do {
                $j++;
              }
              while (($j < $m) && (eregi("[abcdef0-9]",$h[$i][$j])));
              if (($j < $m) && ($h[$i][$j] == ';'))
                $b = false;
            }
        }
        else {
          if (($h[$i][$j] >= '0') && ($h[$i][$j] <= '9'))
            {
              do {
                $j++;
              }
              while (($j < $m) && ($h[$i][$j] >= '0') && ($h[$i][$j] <= '9'));
              if (($j < $m) && ($h[$i][$j] == ';'))
                $b = false;
            }
        }
      }
      elseif (($m  > 2) && (eregi("[a-z0-9]",$h[$i][0]))) {
        $j = 0;
        do {
          $j++;
        }
        while (($j < $m) && (eregi("[a-z0-9]",$h[$i][$j])));
          if (($j < $m) && ($h[$i][$j] == ';'))
            $b = false;
      }

      if ($b)
        $o .= "amp;";
      $o .= htmlspecialchars($h[$i],ENT_QUOTES);
      $i++;
    }

    return $o;
  }

  function vgb_formatText( $t ) {
    $o = "";
    $buffer = "";
    $tlen = strlen($t);
    $inTag = false;
    for ($i=0;$i<$tlen;$i++) {
      if ($inTag) {
        $o .= $t[$i];
        if ($t[$i] == '>')
          $inTag = false;
      }
      elseif ($t[$i] == '<') {
        if ($buffer != "") {
          $o .= vgb_removeHTML(vgb_UndoNoHTML($buffer),false);
          $buffer = "";
        }
        $o .= "<";
        $inTag = true;
      }
      else
        $buffer .= $t[$i];
    }
    if ($buffer != "")
      $o .= vgb_removeHTML(vgb_UndoNoHTML($buffer),false);
    return $o;
  }

  function vgb_filterBadwords ( $t , $active = true ) {
    global $settings,$badwords,$relpath;

    if ((!$active) || ($t == ""))
      return $t;

    if (!isset($badwords)) {
      $badwords = array();
      if (file_exists($relpath."ht_badwords.php")) {
        $fd = fopen ($relpath."ht_badwords.php", "r");
        while (!feof($fd)) {
          $buffer = trim(chop(fgets($fd, 512)));
          if (($buffer != "") && ($buffer[0] != '/')) {
            $badwords[] = strtolower($buffer);
          }
        }
        fclose ($fd);
      }
    }

    $_t = strtolower(" ".vgb_stripTags($t)." ");
    $_tlen = strlen($_t);


    // find badwords

    $skip = array();
    for ($pos=1;$pos<$_tlen;$pos++) {
      $start = vgb_noLetter($_t[$pos-1]);
      $skiplen = 0;

      foreach ($badwords as $badword) {
        $i=0;
        $j=$pos;
        $isBadword = true;
        $len = strlen($badword);
        while ($i<$len)
          if ($j >= $_tlen) {
            $isBadword = false;
            break;
          }
          elseif ($badword[$i] == '$' ) {
            if ($i == 0) {
              if ($start) {
                $i++;
              }
              else {
                $isBadword = false;
                break;
              }
            }
            else {
              if (!vgb_noLetter($_t[$j]))
                $isBadword = false;
              break;
            }
          }
          elseif ($badword[$i] == ' ') {
            if (vgb_isSpace($_t[$j]))
              $j++;
            else
              $i++;
          }
          elseif ($badword[$i] != $_t[$j]) {
            $isBadword = false;
            break;
          }
          else {
            $i++;
            $j++;
          }
        if ($isBadword) {
          $_len = $j-$pos;
          if ($_len > $skiplen)
            $skiplen = $_len;
        }
      }
      if ($skiplen > 0) {
        $skip[] = $pos;
        $skip[] = $pos+$skiplen;
      }
    }

    $skipsize = sizeof($skip);
    if ($skipsize == 0)
      return $t;
    else {
      $o = "";
      $pos = 0;
      $inTag = false;
      $nextskip = $skip[0];
      $skipstart = -1;
      $skipend = -1;
      $tlen = strlen($t);
      $j = 0;
      for($i=0;$i<$tlen;$i++) {
        if ($inTag) {
          if ($t[$i] == '>')
            $inTag = false;
          $o .= $t[$i];
        }
        elseif ($t[$i] == '<') {
          $inTag = true;
          $o .= '<';
        }
        else {
          $pos++;
          if ($pos == $nextskip) {
            if ($skipend <= $pos) {
              $skipstart = $pos;
              $o .= $settings['BADWORDSTRING'];
            }
            $skipend = max($skipend,$skip[$j+1]);
            $j += 2;
            $nextskip = ($j >= $skipsize) ? $tlen+2 : $skip[$j];
          }

          if (($pos < $skipstart) || ($pos >= $skipend))
            $o .= $t[$i];
        }
      }

      return $o;
    }
  }

  function vgb_stripTags( $t ) {
    $o = "";
    $tlen = strlen($t);
    $inTag = false;
    for ($i=0;$i<$tlen;$i++) {
      if ($inTag && ($t[$i] == '>'))
        $inTag = false;
      elseif (!$inTag)
        if ($t[$i] == '<')
          $inTag = true;
        else
          $o .= $t[$i];

    }
    return $o;
  }

  function vgb_isSpace( $c ) {
    global $_spaces;

    if (!isset($_spaces))
      $_spaces = array(" ","_","+","*","#","<",">","@","&","§","\"","'","(",")",
                       "{","}","[","]","%","/","\\","´","~","-","|");
    return in_array($c,$_spaces);
  }

  function vgb_noLetter ( $c ) {
    return ereg("[^a-z0-9]",$c);
  }

  function vgb_doLinebreak( $t ) {
    $o = "";
    $buffer = "";
    $tlen = strlen($t);
    $pre = 0;
    $inTag = false;
    for ($i=0;$i<$tlen;$i++) {
      if ($inTag) {
        $buffer .= $t[$i];
        $o .= $t[$i];
        if ($t[$i] == '>') {
          $inTag = false;
          if ($buffer == "<pre>")
            $pre++;
          elseif ($buffer == "</pre>")
            $pre--;
        }
      }
      elseif ($t[$i] == '<') {
        $inTag = true;
        $buffer = "<";
        $o .= "<";
      }
      elseif (($pre == 0) && ($t[$i] == "\n"))
        $o .= "<br />";
      else
        $o .= $t[$i];
    }
    return $o;
  }
  
  function vgb_notification ($id,$receiver,$name,$replace,$preSubject,$preMessage) {
    global $settings;

    $query = vgb_query("select * from VGB_EMAIL where ID = ".intval($id));
    if (vgb_numrows($query) > 0) {
      $email = vgb_result($query);
      if ($email['ACTIVE']) {
        $_subject = (is_null($email['SUBJECT'])) ? $preSubject : $email['SUBJECT'];
        $_message = (is_null($email['MESSAGE'])) ? $preMessage : $email['MESSAGE'];

        $now = localtime(time(),1);
        if (strlen($now['tm_sec']) == 1)
          $now['tm_sec'] = "0".$now['tm_sec'];
        if (strlen($now['tm_min']) == 1)
          $now['tm_min'] = "0".$now['tm_min'];
        if (strlen($now['tm_hour']) == 1)
         $now['tm_hour'] = "0".$now['tm_hour'];
        $time = $now['tm_hour'].":".$now['tm_min'].":".$now['tm_sec'];
        $dtime = vgb_diffTime($time);
        $today = getdate(time());
        if (strlen($today['mon']) == 1)
          $today['mon'] = "0".$today['mon'];
        $ddate = vgb_diffDate($time,$today['year']."-".$today['mon']."-".
          $today['mday']);
        $date = explode("-",$ddate);
        $_replacements = array( "date" => vgb_dateOut($date), "time" => $dtime, "name" => $name );
        if ($id > 5)
          $_replacements['from'] = $replace['from'];
        foreach ($_replacements as $key => $val) {
          $_subject = str_replace("::".$key."::",$val,$_subject);
        }

        foreach  ($replace as $key => $val) {
          $_replacements[$key] = $val;
        }

        foreach ($_replacements as $key => $val) {
          $_message = str_replace("::".$key."::",$val,$_message);
        }
        
        $h = array();
        if ($settings['HTMLEMAIL']) {
          $h['Content-Type'] = "text/html; charset=";
          $h['Content-Type'] .= ($settings['ENCODING'] != "") ? $settings['ENCODING'] : "iso-8859-1";
        }
        vgb_mail($receiver,$_subject,$_message,$h);
      }
    }
  }

  function vgb_daysPast ($_date) {
    $_today = getdate(time());
    return (mktime(0,0,0,$_today['mon'],$_today['mday'],$_today['year'])
            -mktime(0,0,0,$_date[1],$_date[2],$_date[0]))/86400+1;
  }

  function vgb_leaded ( $instring ) {
    if (strlen($instring) == 1)
      $instring = "0".$instring;
    return $instring;
  }

  function vgb_diffTime ( $time ) {
    global $settings;


    $_time = explode(":",$time);
    $_today = getdate(time());
    $_timestamp = mktime($_time[0],$_time[1],$_time[2],$_today['mon'],
                         $_today['mday'],$_today['year']) +
                  $settings['ADDHOURS'] * 3600 + $settings['ADDMINUTES'] * 60
                  + $settings['ADDSECONDS'];
    $_difftime = getdate($_timestamp);
    return vgb_leaded($_difftime['hours']).":".
           vgb_leaded($_difftime['minutes']).":".
           vgb_leaded($_difftime['seconds']);
  }

  function vgb_diffDate ( $time, $date ) {
    global $settings;

    $_time = explode(":",$time);
    $_date = explode("-",$date);
    $_timestamp = mktime($_time[0],$_time[1],$_time[2],$_date[1],
                         $_date[2],$_date[0]) +
                  $settings['ADDHOURS'] * 3600 + $settings['ADDMINUTES'] * 60
                  + $settings['ADDSECONDS'];
    $_difftime = getdate($_timestamp);
    return $_difftime['year']."-".
           vgb_leaded($_difftime['mon'])."-".
           vgb_leaded($_difftime['mday']);
  }

  function vgb_diffDay ( $time, $date ) {
    global $settings;

    $_time = explode(":",$time);
    $_date = explode("-",$date);
    return date("l",mktime($_time[0],$_time[1],$_time[2],$_date[1],
                         $_date[2],$_date[0]) +
                  $settings['ADDHOURS'] * 3600 + $settings['ADDMINUTES'] * 60
                  + $settings['ADDSECONDS']);
  }

  function vgb_dateOut( $_date, $inChar = "." ) {
    global $settings,$lang;

    switch($settings['USEUSDATE']) {
      case 1:
        if ($inChar == "")
          return $lang[$_date[1]]." ".$_date[2].", ".$_date[0];
        else
          return $_date[0].$inChar.$_date[1].$inChar.$_date[2];

      case 0:
        if ($inChar == "")
          return $_date[2].".".$lang[$_date[1]].", ".$_date[0];
        else
          return $_date[2].$inChar.$_date[1].$inChar.$_date[0];

      case 2:
        if ($inChar == "")
          return $lang[$_date[1]]." ".$_date[2].", ".$_date[0];
        else
          return $_date[1].$inChar.$_date[2].$inChar.$_date[0];
    }
  }

  function vgb_insertEmoticons ( $t , $b, $path = SCRIPTPATH ) {
    global $settings,$skinDir,$emoticons;

    if (!$b || ($t == ""))
      return $t;

    $tlen = strlen($t);
    $t .= " ";
    $skip = 0;
    $inTag = false;
    $o = "";
    for ($i=0;$i<$tlen;$i++)
      if ($skip > 0)
        $skip--;
      elseif ($t[$i] == '<') {
        $inTag = true;
        $o .= "<";
      }
      elseif ($inTag) {
        if ($t[$i] == '>')
          $inTag = false;
        $o .= $t[$i];
      }
      else {
        foreach ($emoticons as $emoticon) {
          $found = true;
          for ($j=0;$j<strlen($emoticon[0]);$j++)
            if ($j > $tlen) {
              $found = false;
              break;
            }
            elseif ($t[$i+$j] != $emoticon[0][$j]) {
              $found = false;
              break;
            }
          if ($found) {
            $o .= " <img alt='' src='".$path.
                $skinDir."emoticons/".$emoticon[1]."' border='0' />";
            $skip = strlen($emoticon[0])-1;
            break;
          }
       }
       if (!$found)
         $o .= $t[$i];
      }

    return $o;
  }

  function vgb_noHTML ( $t ) {
    // replaces < and > by entities
    $t = str_replace("<","&lt;",$t);
    return str_replace(">","&gt;",$t);
  }

  function vgb_UndoNoHTML ( $t ) {
    // undo the replacements done by noHTML
    return str_replace("&gt;",">",str_replace("&lt;","<",$t));
  }

  function vgb_isValidEmail ( $_email ) {
    if (preg_match("/^\w+[\w|\.|-]*\w+@(\w+[\w|\.|-]*\w+\.[a-z]{2,4}|(\d{1,3}\.){3}\d{1,3})$/i", $_email))
      return true;
    else
      return false;
  }
  
  function vgb_BBCode( $in, $adm = 0 ) {
    global $settings,$bbco,$bbst,$bbcc,$bben,$st,$lip,$burl,$ucol,$ufont,$bbmask,$lang;
             
    $strip = $settings['STRIPCODE'];
    if (!$adm && !$settings['USEBBCODE'] && !$strip)
      return $in;
      
    if (!isset($bbcl)) {
      $bbco = array("b"=>1,"i"=>2,"u"=>4,"code"=>8,"left"=>32,"center"=>64,"right"=>128);
      $bbst = array(1=>"b",2=>"i",4=>"u",8=>"table width='90%' cellspacing='0' cellpadding='0' border='0' align='center'><tr><td align='left'><br /><b>".$lang['code']."</b>:</td></tr><tr><td><div style='".CODESTYLE."'><pre",32=>"div align='left'",64=>"div align='center'",128=>"div align='right'");
      $bbcc = array("b"=>1,"i"=>2,"u"=>4,"code"=>8,"quote"=>16,"left"=>32,"center"=>64,"right"=>128,"list"=>256,"url"=>512,"color"=>1024,"size"=>2048,"font"=>4096,"color"=>16384);
    
      $bben = array(1=>"b",2=>"i",4=>"u",8=>"pre></div></td></tr></table",16=>"div></td></tr></table",32=>"div",64=>"div",128=>"div",256=>"ul",512=>"a",1024=>"span",2048=>"span",4096=>"span",16384=>"span",3=>"li");
      
      $ucol = array();
      foreach (explode("\n",$settings['USERCOLORS']) as $c) {
        $cc = explode("=",$c);
        $ucol[$cc[0]] = $cc[1];
      };

      $ufont = array();
      foreach (explode("\n",$settings['USERFONTS']) as $f) 
        $ufont[] = strtolower($f);

      $bbmask = 
             $settings['USEBOLD']+
            ($settings['USEITALIC']<<1)+
            ($settings['USEUNDERLINE']<<2)+
            ($settings['USECODE']<<3)+
            ($settings['USEQUOTE']<<4)+
            ($settings['USELEFT']<<5)+
            ($settings['USECENTER']<<6)+
            ($settings['USERIGHT']<<7)+
            ($settings['USELIST']<<8)+
            ($settings['USELINK']<<9)+
            ($settings['USECOLORHEX']<<10)+
            ($settings['USESIZE']<<11)+
            ($settings['USEFONT']<<12)+
            ($settings['USEIMAGE']<<13)+
            ($settings['USECOLORPRE']<<14);
    }
    
    $fmin = $settings['SIZEMIN'];
    $fmax = $settings['SIZEMAX'];
    $sizemap = array(10,14,16,19,24,32,48);
    $allf = ($settings['USERFONTS'] == '');
    
    if ($adm)
      $cm = 32767;
    else 
      $cm = ($settings['USEBBCODE']) ? $bbmask : 0;
    
    $seg = explode('[',$in);
    if (($sn=sizeof($seg))==1)
      return $in;
    else {
      $o = $seg[0];
      $si=1;
      $im=0;
      $ss=0;
      $lip=null;
      $bburl=0;
      $st=array();
      $b='';
      do { 
        $s=$seg[$si];
        if ($ss) { 
          $b .= $s;
          $ss=0;
        }
        elseif ($s=='') {
          $b .= '[';
          $ss=1;
        }
        elseif (($ep=strpos($s,']'))===false) {
          $b .= '['.$s;
        }
        else {
          $ca = explode(']',$s,2);
          $cn = $ca[0];          
          if ($im) {
            $im = (($cn=='/')||($cn=='/img')) ?  2 : 0;
            $u = (ereg('^http://',strtolower($b))) ? substr($b,7) : $b;
            if (($cm&8192) && eregi("\.(gif|jpg|png|jpeg)\$",$u) && vgb_isValidURL($u)) {
              $o .= "<img src='http://$u' alt='' title='' border='0' /> ";
              $b = '';
            }
            else {
              $be = ($im) ? '['.$cn.']' : '';
              $b = ($strip) ? '' : '[img]'.$b.$be;
            }
          }
          $o .= $b;
          if ($im) {
            $im = 0;
            $b = $ca[1];
          }
          elseif ($cn == 'img') {
            $im = 1;
            $b = $ca[1];
          }
          else {  
            $v = 0;
            if ($cn != "") {
              if ($cn[0] != '/') {
                $ci = 0;
                foreach ($bbco as $co => $k) {
                  if ($co == $cn) {
                    $ci = $k;
                    break;
                  }
                }
                if ($ci) {
                  if($ci&=$cm)
                    $o .= '<'.$bbst[$ci].'>';
                }
                else {
                  if (($cm&512) && ereg('^url=',$cn)) {
                    if (!$burl) {
                      $cn = (ereg('^url=http://',strtolower($cn))) ? substr($cn,11) : substr($cn,4);
                      if (vgb_isValidURL($cn) && ($ci=512)) {
                        $o .= "<a href='http://$cn' class='vgbtextlink' target='_blank'>";
                        $burl = 1;
                      }                      
                    }
                  }
                  elseif (($cm&16) && ereg('^quote',$cn)) {
                    $cn = substr(trim($cn),5);
                    if ($cn == '') {
                      $o .= "<table width='90%' cellspacing='0' cellpadding='1' border='0' align='center'><tr><td align='left'><br /><b>".$lang['quote']."</b>:</td></tr><tr><td><div style='".QUOTESTYLE."'>";
                      $ci=16;
                    }
                    elseif (($cn[0] == '=') && (strlen($cn) > 1)) {
                      $cn = substr($cn,1);
                      $o .= "<table width='90%' cellspacing='0' cellpadding='1' border='0' align='center'><tr><td align='left'><br /><b>".$lang['quote']."</b> ".$lang['from']." <b>".vgb_outName(str_replace("(|","[",str_replace("|)","]",$cn)))."</b>:</td></tr><tr><td><div style='".QUOTESTYLE."'>";
                      $ci=16;
                    }
                  }
                  elseif (($cm&17408) && ereg('^color=',$cn)) {
                    $cn = substr($cn,6);
                    if ($cn != '') 
                      if ($cn[0]=='#') {
                        if (eregi("^#[0-9a-f]{6}\$",$cn) && ($ci=16384))
                          $o .= "<span style='color: ".$cn."'>";
                      }
                      elseif (isset($ucol[strtolower($cn)]) && ($ci=16384))
                        $o .= "<span style='color: ".$ucol[$cn]."'>";
                  }
                  elseif (($cm&2048) && ereg('^size=',$cn)) {
                    $cn = substr($cn,5);
                    if ((vgb_isNumber($cn,$fmin,$fmax)) && ($ci=2048))
                      $o .= "<span style='font-size: ".$sizemap[$cn-1]."px'>";
                  }
                  elseif (($cm&4096) && ereg('^font=',$cn)) {
                    $cn = strtolower(substr($cn,5));
                    if ((($allf && ereg("^[a-z0-9 \_\-]+\$",$cn)) || in_array($cn,$ufont)) && ($ci=4096))
                      $o .= "<span style='font-family: ".$cn."'>";
                  }
                  elseif (($cn=='list') && ($lip==null)) {
                    $ci = 256;
                    if ($ci&=$cm) {
                      $lip = count($st)+1;
                      $fli = 0;
                      $o .= "<ul type='square'>";
                    }
                  }
                  elseif (($cn=='*') && ($lip!=null)) {
                    $i = count($st)-$lip;
                    while ($i>0) {
                      $o .= st_pop(0);
                      $i--;
                    }  
                    if (!$fli)                       
                      $fli=1;
                    $st[] = 3;
                    $o .= "<li>";
                    $ci = -1;
                  }
                }
                if ($ci) { 
                  if ($ci>0)
                    $st[] = $ci;
                  $v = 1;
                }
                else
                  $st[] = -1;                
              }
              else {
                $cn = substr($cn,1);
                
                if ($cn=='') {
                  if (($ro = st_pop($ci)) != '') {
                    $o .= $ro;
                    $v = 1;
                  }
                }
                else {
                  foreach ($bbcc as $cc => $k) {
                    if ($cc == $cn) {
                      $ci = $k;
                      break;
                    }
                  }
                  if ($ci>0) {
                    if (($ro = st_pop($ci)) != '') {
                      $o .= $ro;
                      $v = 1;
                    }
                  }
                  else 
                    st_pop(-1);
                }     
              }
            }
            $b = (!$v && !$strip)? '['.$s : $ca[1];
          }
        }
        $si++;
      }
      while ($si<$sn);
      if ($im) {
       $u = (ereg('^http://',strtolower($b))) ? substr($b,7) : $b;
       if (($cm&8192) && eregi("\.(gif|jpg|png|jpeg)\$",$u) && vgb_isValidURL($u))  
        $o .= "<img src='$u' alt='' title='' border='0' /> ";
       else
        $o .= ($strip) ? '' : '[img]'.$b;
      }
      else
        $o .= $b;
    }
    
    $i = count($st);
    while ($i) {
      $o .= st_pop(0);
      $i--;
    }
    return $o;
  }
  
  function st_pop ( $ci ) {
    global $st,$bben,$lip,$burl;
      
    $ro = '';
    if ($ci) {
      if (in_array($ci,$st)) {
        do {
          $pc = array_pop($st);
          if ($pc!=-1) {
            $ro .= '</'.$bben[$pc].'>';
            if ($pc==256)
              $lip=null;
            elseif ($pc==512)
              $burl=0;
          }
        } while ($pc!=$ci);
      }
    }
    elseif ((($pc = array_pop($st)) !== null) && ($pc!=-1)) {
      $ro = '</'.$bben[$pc].'>';
      if ($pc==256)
        $lip=null;
      elseif ($pc==512)
        $burl=0;
    }    
    return $ro;
  }

  function vgb_isNumber( $inString, $inMin, $inMax ) {
    // checks if the given string is a number in the boundaries
    if ( $inString == "")
      return FALSE;
    $_isNumber = TRUE;
    for ($_pos = 0;$_pos<strlen($inString);$_pos++) {
      if (($inString[$_pos] < '0') || ($inString[$_pos] > '9'))
        $_isNumber = FALSE;
    }
    if (((int)($inString) < $inMin) || ((int)($inString) > $inMax))
      $_isNumber = FALSE;
    return $_isNumber;
  }

  function vgb_limitEmptyLines( $t ) {
    // limits the number of empty lines
    global $settings;

    if ($settings['MAXEMPTYLINES'] == 0)
      return $t;

    $o = "";
    $e = 0;
    $lines = explode("\n",$t);
    foreach($lines as $line) {
      $empty = true;
      $i = 0;
      while (($i < strlen($line)) && $empty) {
        if (!vgb_isWhitespace($line[$i]))
          $empty = false;
        $i++;
      }
      if ($empty) {
        if ($e < $settings['MAXEMPTYLINES'])
          $o .= "\n";
        $e++;
      }
      else {
        $e = 0;
        $o .= $line."\n";
      }
    }
    return chop($o);
  }
?>
Return current item: YW-GuestBook