<?php
include "config.inc";

function start_page()
{
    Header("Content-type: text/html; charset=\"" . $GLOBALS["OBEC_CHARSET"] . "\"");
    print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">");
}

// This function is used mainly in XML output
// since XML parsers choke on special characters
// people insert into their signatures.
// Dirty hack, of course, but seems to be working 
// for now.
// (lord)
function strip_invalid_chars($s, $charset)
{   
    if(strcasecmp($charset,"koi8-r")!=0)
    {
        // only koi8-r is currently supported
        return $s;
    }

    $res = "";
    $l   = strlen($s);
    for($i=0;$i<$l;$i++)
    {
            $c=substr($s,$i,1);
            $n=ord($c);
            if(($n<32 && $n!=10 && $n!=13) || ($n>127 && $n<191))
            {
                $res .= '?';
            } else
            {
                $res .= $c;
            }
    }
    return $res;
}

// Quote special characters which could appear in SQL LIKE
// query. For mysql these are: '%', '_', '\', '\'', '"'. 
function quote_LIKE($s)
{
        $res = "";
        $l   = strlen($s);
        for($i=0;$i<$l;$i++)
        {
                $c=substr($s,$i,1);
                if(strstr("%_", $c))
                {
                    $res .= '\\';
                }
                $res .= $c;
        }
        return $res;
}

// this method cannot find null (any empty value) in the array
function compat_in_array($needle,$haystack)
{
    if($haystack)
        for($ptr=0;$ptr<count($haystack);$ptr++)
        { 
                if($haystack[$ptr] && $haystack[$ptr] == $needle)
                { 
                        return true; 
                }
        }
    
    return false; 
} 

function do_redirect ( $url ) 
{
  Header ( "Location: $url" );
  echo "<HTML><HEAD>";
  echo "<TITLE>Redirect</TITLE></HEAD><BODY CLASS=body>" . 
    "Please go <A HREF=\"" . $url . "\">here</A>.</BODY></HTML>.\n";
  exit;
}

function do_transaction($db, $sql, $link)
{

       if(!mysql_db_query($db, "BEGIN", $link))
       { 
          return false; 
       }

       $sql= trim($sql);
       $Arr_Query = split(";", $sql); 
       reset ($Arr_Query); 
       while(list($k,$v)=each($Arr_Query))
       { 
               if ($v && $v !=" ")
               {
                   $result=mysql_db_query($db, $v, $link);
                   if(!$result)
                   { 
                       //print ("ERROR ON SQL: '" . $v . "'\n");
                       mysql_db_query($db, "ROLLBACK", $link);
                       return false; 
                   } else
                   {
                        if(strlen($v)>6 && strncmp($v,"SELECT", 6)==0)
                        {
                                mysql_free_result($result);
                        }
                   }
               }
       } 
       return mysql_db_query($db, "COMMIT", $link);
}

function is_favorie($db, $link, $id, $login)
{
       $result=mysql_db_query($db, "SELECT Id FROM FavoriteAuthors WHERE Login='". addslashes($login) ."' AND AuthorId='" . addslashes($id) . "'" , $link);
       if(!$result)
       {
           //TODO log
           return false;
       } 

       $row = mysql_fetch_array($result);
       mysql_free_result($result);
       if($row)
       {
           return true;
       } else
       {
           return false;
       }
}


?>
