<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();

try {
     $db = new PDO("mysql:host=213.142.145.160:3306;dbname=lisans_db", "lisans_user", "1234789567");
} catch ( PDOException $e ){
     print $e->getMessage();
}


function authenticate(){
  if(!isset($_SESSION['venue']) || intval($_SESSION['venue']) == 0)
    header("Location: login.php");
}

function random($length){
  $str = "123456789ABCDEFGHJKLMNPRSTUVYZXQW";
  $str_length = strlen($str)-1;
  $rand = rand(0,$str_length);

  if($length>1)
    return $str[$rand].random($length-1);
  else
    return $str[$rand];
}

function generate($part, $length){
  if($part>1)
    return random($length).'-'.generate($part-1, $length);
  else
    return random($length);
}

function license_generate(){
  global $db;
  do{
    $key = generate(3,4);
    $check = $db->query("SELECT * FROM licenses WHERE license_key='$key'")->rowCount();
  } while($check>0);

  return $key;
}

?>
