<?php

global $bDbConnected;
$bDbConnected = false;
global $pLink;

function Blokuje($sIP) {
global $pLink;

  $sHostname = gethostbyaddr($sIP);
  $aHostname = explode(".", $sHostname);
  $sIsp = "." . $aHostname[count($aHostname)-2] . "." . $aHostname[count($aHostname)-1] . ";";

  // vyhledáme v databázi
  PripojDb();

  $sSql = sprintf("insert ignore into ip_rev (ip,  ptr) values (\"%s\", \"%s\");", mysqli_real_escape_string($pLink,$sIP), mysqli_real_escape_string($pLink,$sHostname));
  askDb($sSql);

/*  if($sIsp == ".upcbroadband.cz;" || $sIsp == ".upcbusiness.cz;") {
    $lines = file('upc.txt');
    if(!in_array($sIP . "\n", $lines)) {
      $fp = fopen('upc.txt', 'a');
      fwrite($fp, $sIP . "\n");
      fclose($fp);
      if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
        $fp = fopen('upc-log.txt', 'a');
        fwrite($fp, $sIP . " https" . "\n");
        fclose($fp);
        header('Location: https://images.brmlab.cz/');
	die();
      } else {
        $fp = fopen('upc-log.txt', 'a');
        fwrite($fp, $sIP . " http" . "\n");
        fclose($fp);
        header('Location: http://images.brmlab.cz/');
	die();
      }
    }
  }*/

  $sSql = sprintf("SELECT * FROM `isp` WHERE `revs` LIKE '%s'", "%" . mysqli_real_escape_string($pLink,$sIsp) . "%");
  $pRes = askDb($sSql);
  if(mysqli_num_rows($pRes)) {
    $oRes = mysqli_fetch_object($pRes);
    global $sIspName;
    global $sIspRevs;
    $sIspName = $oRes->name;
    $sIspRevs = $oRes->revs;
    return(1);
  } else {
    $aIP = explode(".", $sIP);
    $sIsp = $aIP[0] . "." . $aIP[1] . "." . $aIP[2] . ";";
    $sSql = sprintf("SELECT * FROM `isp` WHERE `revs` LIKE '%s'", "%" . mysqli_real_escape_string($pLink,$sIsp) . "%");
    $pRes = askDb($sSql);
    if(mysqli_num_rows($pRes)) {
      $oRes = mysqli_fetch_object($pRes);
      global $sIspName;
      global $sIspRevs;
      $sIspName = $oRes->name;
      $sIspRevs = $oRes->revs;
      return(1);
    } else {
      return(0);
    }
  }

}

function askDb($sSql) {
global $pLink;
if(!$pRes = mysqli_query($pLink, $sSql))
  die(
    "Obecná chyba database: " .
    mysqli_errno($pLink) .
    mysqli_error($pLink)
    );
return $pRes;
}

function PripojDb() {
  require_once("../dbconfig.php");

  /* 

  v souboru dbconfig.php by mělo být něco jako 

  define("SERVER", "norasan.hrach.eu");
  define("USER", "blok");
  define("PASS", "nepovim");
  define("DB", "blok");

  tabulka isp by měla mít strukturu

  id [int, auto_increment, pri_key], name [varchar], revs [text]

  */
  global $bDbConnected;
global $pLink;
#  if (!$bDbConnected) {
    if(!$pLink = mysqli_connect(SERVER, USER, PASS))
      die("Nepodařilo se připojit k databázi :-(");
    if(!mysqli_select_db($pLink, DB))
      die("Nepodařilo se vybrat databázi :-(");
    if(!mysqli_query($pLink, "SET NAMES `utf8`"))
      die("Nepodařilo se nastavit kódování :-(");
    $bDbConnected = true;
#  }
}

function OdpojDb() {
  global $bDbConnected;
#  if($bDbConnected){
#    if(mysql_close($pLink)) {
#      $bDbConnected = 0;
#    } else {
#      die("Nepodařilo se odpojit databázi");
#    }
#  }
}

function dumpniDb() {
global $pLink;
  PripojDb();
  $pRes = askDb("SELECT * FROM `isp` ORDER BY `id`");
  if(mysqli_num_rows($pRes)) {
    echo "id,name,revs\n\n";
    while($oRes = mysqli_fetch_object($pRes)) {
      echo '"' . $oRes->id . "\";\"" . $oRes->name . "\";\"" . $oRes->revs . '"' . "\n";
    }
  }
  else {
    echo "Databáze je prázdná!";
  }
  OdpojDb();
}

function pocitadlo($sCenzor) {
  PripojDb();
global $pLink;

  //dementni pocitani requestu
  $pRes = askDb("SELECT * FROM `counter_int` WHERE `cenzor` = '2'");
  $oRes = mysqli_fetch_object($pRes);
  $iNavstev = $oRes->value;
  $iNavstev++;
  askDb("UPDATE `counter_int` SET `value` = '$iNavstev' WHERE `cenzor` = '2' LIMIT 1");

  settype($sCenzor, "string");
  if(empty($_GET["ip"])) {
    $sRemote_ip = $_SERVER['REMOTE_ADDR'];
  } else {
    $sRemote_ip = $_GET["ip"];
  }

  $sSql = sprintf("SELECT * FROM counter_ip WHERE ip='%s'", mysqli_real_escape_string($pLink,$sRemote_ip));
  $pRes = askDb($sSql);
  if (!mysqli_num_rows($pRes)){
    $oRes = mysqli_fetch_object($pRes);
    $sSql = sprintf("INSERT INTO counter_ip (ip, ua) VALUES ('%s', '%s')", mysqli_real_escape_string($pLink,$sRemote_ip), mysqli_real_escape_string($pLink,$_SERVER["HTTP_USER_AGENT"]));
    askDb($sSql);
    $pRes = askDb("SELECT * FROM `counter_int` WHERE `cenzor` = '$sCenzor'");
    $oRes = mysqli_fetch_object($pRes);
    $iNavstev = $oRes->value;
    $iNavstev++;
    askDb("UPDATE `counter_int` SET `value` = '$iNavstev' WHERE `cenzor` = '$sCenzor' LIMIT 1");
  }
  OdpojDb();
}

?>
