<?php
/* First, db connection */
require_once "connect.php";
require_once 'lib/loadTwig.php';

function checkUptime($db_conn,$ID_C,$precision = 0){
	if ($precision == 0){
		$time = time()-30*7*3600; //Last week
	}else{
		$time = $precision;
	}
	$res = dbw_query($db_conn,"SELECT code FROM CHKHIST WHERE ID_C='$ID_C' AND `timestamp` > $time");
	$count = 0;
	$err = 0;
	while ($fila = dbw_fetch_array($db_conn,$res)){
		$count++;
		$err += $fila['code'];
	}
	if ($count == 0){
		return '-1'; //No registers
	}else{
		if ($err != 0){
			$uptime = (100 - round(($err/$count)*100,2));
			return $uptime;
		}else{
			return 100;
		}
	}

}


/** Ping a WebSite */
function ping($host,$port = '80') { 
  $fP = fSockOpen($host, $port, $errno, $errstr, 3);  //Host,port,Err,ErrString,Timeout(Sec)
  //echo "Error:$errno";
	return $fP ? "0" : "1";
}

/** Returns web root (ie. http://jkanetwork.com) */
function webRoot(){
	$root = $_SERVER['HTTPS'] ? "https://" : "http://";  //Variable = condicion ? Verdadero : Falso
	return $root . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
}

/** Returns a system option */
function getSystemOpt($db_conn,$sysopt){
	return dbw_query_fetch_array($db_conn,"SELECT * FROM SYS WHERE option = '$sysopt'")['value'];
}

/** Return HttpCode of page. Returns false if page is not found */
function httpCode($url, $wait = 5)
{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, TRUE);
		curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_TIMEOUT, $wait); //timeout in seconds
		$head = curl_exec($ch);
		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
		
		return $head ? $httpCode : FALSE;
} 

/** Return a String of $lenght chars */
function RandomString($length)
{
	//https://phpes.wordpress.com/2007/06/12/generador-de-una-cadena-aleatoria/
    $source = 'abcdefghijklmnopqrstuvwxyz';
    $source .= '1234567890';
    if($length>0){
        $rstr = "";
        $source = str_split($source,1);
        for($i=1; $i<=$length; $i++){
            $num = mt_rand(1,count($source));
            $rstr .= $source[$num-1];
        }

    }
    return $rstr;
}

/** 1 if its logued, 0 if not */
function isLogued(){
	if (isset($_SESSION['UserID'])){
		return 1;
	}else{
		return 0;
	}
}
function requireLogin(){
	if (isLogued() == 0){
		die('You dont have admin rights'); //This blocks edit or create nothing if its not logued
	} 
}

function nameFromIDC($db_conn,$ID_C){
	return dbw_query_fetch_array($db_conn,"SELECT name FROM CHECKS WHERE ID_C='$ID_C'")[0];
}

function nameGroupFromIDG($db_conn,$ID_G){
	return dbw_query_fetch_array($db_conn,"SELECT * FROM GROUPS WHERE ID_G='$ID_G'")['name'];
}

function IDGFromIDC($db_conn,$ID_C){
	return dbw_query_fetch_array($db_conn,"SELECT ID_G FROM CHECKS WHERE ID_C='$ID_C'")['ID_G'];
}

/* This funtion returns the array with type of checks */
function arrayTypeChk(){
	return array(
	'1' => 'PING_IP',
	'2' => 'HTTP_CODE',
	'3' => 'VISIT_COUNT',
	'4' => 'DATABASE');
}
/* This funtion returns the translated text of a type check */
function textTypeChk($T_,$typeChk){
	$arr = array(
	'1' => $T_['PING_IP'],
	'2' => $T_['HTTP_CODE'],
	'3' => $T_['VISIT_COUNT'],
	'4' => $T_['DATABASE']);
	return $arr[$typeChk];
}