<?php
function head($title) {
?>
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title><?php echo $title; ?></title>
	<meta name="keywords" content="knihy, katalog, kniznica, pozicat, catalogue, books, library, lend, personal" />
	<meta name="description" content="On this site you can find catalogue from which you can lend my personal books." />
	<link rel="shortcut icon" href="images/small.jpg" />
	<link href="styles.css" rel="stylesheet" type="text/css" />
	</head>
<?php
}
?>

<?php
function createRandomPassword() { 

    $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; 
    srand((double)microtime()*1000000); 
    $i = 0; 
    $pass = '' ; 

    while ($i <= 7) { 
        $num = rand() % 35; 
        $tmp = substr($chars, $num, 1); 
        $pass = $pass . $tmp; 
        $i++; 
    } 
    return $pass; 
}
?>

<?php
function connect_db() { //Connect to server and select databse.

	$host = "localhost";
	$username = "skcatalogue";
	$password = "catalogue_pass";
	$db_name = "skcatalogue";
	
	$db_link = mysql_connect($host, $username, $password) or die("cannot connect to server");
	mysql_query('SET NAMES UTF8');
	mysql_query('SET COLLATION_CONNECTION=UTF8_GENERAL_CI');
	mysql_select_db($db_name, $db_link) or die("cannot select DB");
	
	return $db_link;
	
}
?>

<?php
function protect($value){
	//$value = mysql_real_escape_string($value);
	$value = stripslashes($value);
	$value = strip_tags($value);
}
?>

<?php
function check_email($email)
{
    $atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]'; // znaky tvořící uživatelské jméno
    $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // jedna komponenta domény
    return eregi("^$atom+(\\.$atom+)*@($domain?\\.)+$domain\$", $email);
}
?>

<?php
function check_orders()
{

	$link = connect_db();

	$sql = "SELECT * FROM book_orders";
	$result = mysql_query($sql, $link);
	if ($result) {
		while ($row = mysql_fetch_assoc($result)) {
			
			$date = date("y-m-d");
			
			if (($date > $row['date_back']) && ($row['provided'] == 1)) {
			
				$sql3 = "SELECT * FROM users WHERE uid='" . $row['uid'] . "'";
				$result3 = mysql_query($sql3, $link) or die(mysql_error());
				$row3 = mysql_fetch_assoc($result3);
				
				$sql2 = "SELECT * FROM books WHERE id_book='" . $row['book_id'] . "'";
				$result2 = mysql_query($sql2, $link) or die(mysql_error());
				$row2 = mysql_fetch_assoc($result2);
				
				$msg = "";
			          
				$to = $row3['email']; 
					 
				$subject = "Upomienka"; 
					
				$header = "from: admin <jakub.husar@centrum.sk>";
				
				$messages= "Vážený " . $row3['name'] . " " . $row3['surname'] . ",\r\n";	
				$messages= "dňom " . $row['date_back'] . "bola prekročená doba na vrátenie knihy: \r\n";
				$messages.="" . $row2['name'] . ", " . $row2['author'] . ", " . $row2['year_published'] . "\r\n";
				$messages.="Vaše konto bude preto do vrátenia uvedeného titulu zablokované. \r\n\r\n";
				$messages.="S pozdravom administrátor systému. \r\n";
				
				$sentmail = mail($to,$subject,$messages,$header);
				
				if($sentmail){
					$sql = "UPDATE users SET status='0' WHERE username='" . $row3['name'] . "' LIMIT 1";
				}
			}
		}
				
		mysql_free_result($result);
	} else {
		echo '<p id="red">Nastal problém pri získavaní údajov z databázy - ' . mysql_errno($link) . ': ' . mysql_error($link) . '</p>';
	}
}
?>