php/heslo/lib/funkce.inc.php
changeset 3 74a6a363ff9e
child 4 e6abafe31efb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/php/heslo/lib/funkce.inc.php	Tue May 05 17:08:09 2009 +0200
     1.3 @@ -0,0 +1,325 @@
     1.4 +<?php
     1.5 +
     1.6 +require('nastaveni.php');
     1.7 +
     1.8 +$zacatek = getMicrotime();
     1.9 +
    1.10 +function exception_handler($exception) {
    1.11 +  html("Došlo k chybě: " . $exception->getMessage());
    1.12 +  //header("Location: chyba_db.php");
    1.13 +  exit();
    1.14 +}
    1.15 +
    1.16 +set_exception_handler('exception_handler');
    1.17 +
    1.18 +session_set_cookie_params(0, $NASTAVENI['cookie_cesta']);
    1.19 +session_start();
    1.20 +
    1.21 +
    1.22 +$strankaP = split('/', $_SERVER['REQUEST_URI']);
    1.23 +$stranka = $strankaP[count($strankaP)-1];
    1.24 +
    1.25 +
    1.26 +if (empty($_SERVER['HTTP_ACCEPT'])) {
    1.27 +	$mimeTyp = 'application/xhtml+xml';
    1.28 +} else {
    1.29 +	$mimeTyp = (( stristr( $_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml' ) && !preg_match("/application\/xhtml\+xml;\s*q=0(\.0)?\s*(,|$)/",$_SERVER['HTTP_ACCEPT']) ) ? 'application/xhtml+xml' : 'text/html' );
    1.30 +}
    1.31 +header("Content-Type: $mimeTyp");
    1.32 +//header("Content-Type: text/html");
    1.33 +//header("Content-Type: application/xhtml+xml");
    1.34 +
    1.35 +/** $nadpis = titulek stránky + h1 */
    1.36 +function zahlavi($nadpis, $autorizace = true) {
    1.37 +	global $mimeTyp;
    1.38 +	if ($autorizace) {
    1.39 +		overPristup();
    1.40 +	}
    1.41 +	
    1.42 +	html('<?xml version="1.0" encoding="UTF-8"?>
    1.43 +<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    1.44 +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
    1.45 +  <head>
    1.46 +    <meta http-equiv="content-language" content="cs"/>
    1.47 +    <meta http-equiv="content-type" content="' . $mimeTyp . '; charset=UTF-8"/>
    1.48 +    <meta name="GOOGLEBOT" content="NOINDEX, NOFOLLOW"/>
    1.49 +    <meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/>
    1.50 +    <link href="styl.css" type="text/css" rel="StyleSheet"/>
    1.51 +    <script type="text/javaScript" src="hlavni.js"></script>
    1.52 +    <script type="text/javaScript" src="tridenitabulky.js"></script>
    1.53 +    <title>' . $nadpis . '</title>
    1.54 +  </head>
    1.55 +  <body>  
    1.56 +<h1>' . $nadpis . '</h1>');
    1.57 +if ($autorizace) {
    1.58 +html('<p id="prihlasenyUzivatel">Uživatel: ' . $_SESSION['login']  . '</p>');
    1.59 +}
    1.60 +}
    1.61 +
    1.62 +/** $zobrazitNabidku = zobrazí postranní menu */
    1.63 +function zapati($zobrazitNabidku = true) {
    1.64 +	if ($zobrazitNabidku) {
    1.65 +		html('<h2 id="skrytyNadpisObsahu">Obsah</h2>
    1.66 +<ul id="obsah">
    1.67 +<li>
    1.68 +<a href="index.php">Přehled</a>
    1.69 +</li>
    1.70 +<li>
    1.71 +<a href="moje_bugy.php">Moje bugy</a>
    1.72 +</li>
    1.73 +<li>
    1.74 +<a href="nahlasit_bug.php">Nahlásit bug</a>
    1.75 +</li>
    1.76 +<li>
    1.77 +<a href="hledani.php">Hledání</a>
    1.78 +</li>
    1.79 +<li>
    1.80 +<a href="sprava.php">Správa</a>
    1.81 +</li>
    1.82 +<li>
    1.83 +<a href="odhlaseni.php">Odhlášení</a>
    1.84 +</li>	
    1.85 +</ul>
    1.86 +</body>');
    1.87 +htmlInfoKomentar();
    1.88 +html('</html>');
    1.89 +	} else {
    1.90 +		html('</body>');
    1.91 +		htmlInfoKomentar();
    1.92 +		html('</html>');
    1.93 +	}
    1.94 +
    1.95 +}
    1.96 +
    1.97 +function htmlInfoKomentar() {	
    1.98 +	global $zacatek, $mimeTyp, $NASTAVENI;
    1.99 +	if ($NASTAVENI['ladiciKomentare']) {
   1.100 +		$konec = getMicrotime();
   1.101 +		html('<!--');
   1.102 +		html('Stránka: ' . $_SERVER['REQUEST_URI']);
   1.103 +		html('Klient:  ' . $_SERVER['REMOTE_ADDR']);
   1.104 +		html('MIME:    ' . $mimeTyp);
   1.105 +		html("Začátek zpracování: $zacatek");
   1.106 +		html("Konec zpracování:   $konec");
   1.107 +		html('Doba zpracování:    ' . ($konec - $zacatek) . 's');	
   1.108 +		html('-->');
   1.109 +	}
   1.110 +}
   1.111 +
   1.112 +function htmlOdstavec($text) {
   1.113 +	html("<p>\n$text\n</p>");
   1.114 +}
   1.115 +
   1.116 +function html($text) {	
   1.117 +	global $stranka;	
   1.118 +	/** Odstraníme odkazy, které vedou na tutéž stránku */
   1.119 +	$text = str_replace(' href="' . $stranka . '"', '', $text);
   1.120 +	
   1.121 +	echo("$text\n");
   1.122 +}
   1.123 +
   1.124 +function htmlZnacka($znacka, $text) {
   1.125 +	html("<$znacka>$text</$znacka>");
   1.126 +}
   1.127 +
   1.128 +function htmlTlacitkovyOdkaz($url, $text) {
   1.129 +	html('<form action="' . $url . '"><fieldset><button>' . $text . '</button></fieldset></form>');
   1.130 +}
   1.131 +
   1.132 +function escapuj($text, $delka = 256) {
   1.133 +	$text = str_replace("\n", "[br]", $text);
   1.134 +	$text = substr(mysql_escape_string(htmlspecialchars($text)), 0, $delka);
   1.135 +	$text = str_replace("[br]", "<br>", $text);
   1.136 +	//$retezec = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\">\\0</a>", $retezec);
   1.137 +	return $text;
   1.138 +}
   1.139 +
   1.140 +function htmlIkonaKontrolyIP () {
   1.141 +	global $NASTAVENI;
   1.142 +	if ($NASTAVENI['kontrolovatIPadresu']) {
   1.143 +		html('<img src="kontrola_ip.png" alt="Kontrola IP" title="Kontroluje se IP adresa"/>');
   1.144 +	}
   1.145 +}
   1.146 +
   1.147 +/** volá se na všech strákách, které vyžadují autorizaci (všechny kromě  prihlaseni.php) */
   1.148 +function overPristup() {
   1.149 +	global $NASTAVENI;
   1.150 +	
   1.151 +	if ($_SESSION['login'] == null || !isset($_SESSION['login']) || ($NASTAVENI['kontrolovatIPadresu'] && $_SERVER['REMOTE_ADDR'] != $_SESSION['ip_adresa_klienta'])) {
   1.152 +		header("Location: prihlaseni.php");
   1.153 +		exit();
   1.154 +	}
   1.155 +}
   1.156 +
   1.157 +/** volá se v prihlaseni.php */
   1.158 +function zkontrolujUzivatele($jmeno, $heslo) {
   1.159 +	global $db, $NASTAVENI;
   1.160 +	
   1.161 +	if ($NASTAVENI['hashovatHesla']) {
   1.162 +		$dotaz = $db->prepare("SELECT * FROM bg_uzivatel WHERE login = ? AND sha1(concat(?, heslo)) = ?");
   1.163 +		$dotaz->bindParam(1, $jmeno);
   1.164 +		$dotaz->bindParam(2, $_SESSION['hesloBordel']);
   1.165 +		$dotaz->bindParam(3, $heslo);
   1.166 +		$dotaz->execute();	
   1.167 +		$r = $dotaz->fetchAll();
   1.168 +	} else {
   1.169 +		$dotaz = $db->prepare("SELECT * FROM bg_uzivatel WHERE login = ? AND heslo = sha1(?)");
   1.170 +		$dotaz->bindParam(1, $jmeno);		
   1.171 +		$dotaz->bindParam(2, $heslo);
   1.172 +		$dotaz->execute();	
   1.173 +		$r = $dotaz->fetchAll();		
   1.174 +	}
   1.175 +	
   1.176 +	foreach ($r as $x) {
   1.177 +		$_SESSION['login'] = $x['login'];
   1.178 +		$_SESSION['ip_adresa_klienta'] = $_SERVER['REMOTE_ADDR'];
   1.179 +		return true;
   1.180 +	}
   1.181 +	
   1.182 +	$_SESSION['login'] = null;
   1.183 +	return false;
   1.184 +}
   1.185 +
   1.186 +
   1.187 +function sqlZjistiHodnotu($dotaz, $parametry = null) {
   1.188 +	global $db;
   1.189 +	
   1.190 +	$dotaz = $db->prepare($dotaz);
   1.191 +	
   1.192 +	if ($parametry != null) {
   1.193 +		for($i = 0; $i < count($parametry); $i++) {
   1.194 +			$dotaz->bindParam($i+1, $parametry[$i]);
   1.195 +		}
   1.196 +	}
   1.197 +	
   1.198 +	$dotaz->execute();
   1.199 +	$r = $dotaz->fetchAll();
   1.200 +	
   1.201 +	foreach ($r as $x) {
   1.202 +		return $x[0];		
   1.203 +	}
   1.204 +	return null;
   1.205 +}
   1.206 +
   1.207 +function sqlDotaz($dotaz, $parametry = null) {
   1.208 +	global $db;
   1.209 +	try {
   1.210 +		$dotaz = $db->prepare($dotaz);
   1.211 +	
   1.212 +		if ($parametry != null) {
   1.213 +			for($i = 0; $i < count($parametry); $i++) {
   1.214 +				$dotaz->bindParam($i+1, $parametry[$i]);
   1.215 +			}
   1.216 +		}
   1.217 +	
   1.218 +		$dotaz->execute();
   1.219 +		$r = $dotaz->fetchAll();
   1.220 +	
   1.221 +		return $r;
   1.222 +	} catch (PDOException $e) {
   1.223 +		logujChybu("sqlDotaz($dotaz)");		
   1.224 +	}
   1.225 +}
   1.226 +
   1.227 +function getMicrotime(){
   1.228 +   list($usec, $sec) = explode(" ",microtime());
   1.229 +   return ((float)$usec + (float)$sec);
   1.230 +} 
   1.231 +
   1.232 +function sqlHtmlTabulka($dotaz, $hlavicky = null, $parametry = null, $htmlID = null) {
   1.233 +	global $db;
   1.234 +	
   1.235 +	$dotaz = $db->prepare($dotaz);
   1.236 +	
   1.237 +	if ($parametry != null) {
   1.238 +		for($i = 0; $i < count($parametry); $i++) {
   1.239 +			$dotaz->bindParam($i+1, $parametry[$i]);
   1.240 +		}
   1.241 +	}
   1.242 +	
   1.243 +	$dotaz->execute();
   1.244 +	$r = $dotaz->fetchAll();
   1.245 +	
   1.246 +	if ($htmlID == null) {
   1.247 +		$htmlID = getMicrotime();
   1.248 +	}
   1.249 +	
   1.250 +	
   1.251 +	html('<table class="sortable" id="tabulka' . $htmlID . '">');	
   1.252 +	if ($hlavicky != null) {
   1.253 +		html('<thead><tr>');
   1.254 +		foreach ($hlavicky as $h) {
   1.255 +			htmlZnacka('td', $h);
   1.256 +		}
   1.257 +		html('</tr></thead>');
   1.258 +		$pocet = count($hlavicky);
   1.259 +	} else {
   1.260 +		$pocet = null;
   1.261 +	}
   1.262 +	html('<tbody>');	
   1.263 +		
   1.264 +	foreach ($r as $x) {
   1.265 +		html('<tr>');
   1.266 +		if ($pocet == null) {
   1.267 +			/** děleno 2 protože jsou tam číselné i řetězcové klíče */
   1.268 +			$pocet = count($x) / 2;
   1.269 +		}
   1.270 +		
   1.271 +		for ($i = 0; $i < $pocet; $i++) {			
   1.272 +			htmlZnacka('td', $x[$i]);
   1.273 +		}
   1.274 +		html('</tr>');
   1.275 +	}	
   1.276 +	html('</tbody></table>');
   1.277 +}
   1.278 +
   1.279 +
   1.280 +function htmlTabulkaUzivatelu() {
   1.281 +	sqlHtmlTabulka('SELECT concat(concat(concat(concat(\'<a href="uzivatel.php?login=\',login),\'">\'),login),\'</a>\'), jmeno, prijmeni, email FROM bg_uzivatel', array('Login', 'Jméno', 'Příjmení', 'e-mail'));
   1.282 +}
   1.283 +
   1.284 +function htmlTabulkaProduktu() {
   1.285 +	sqlHtmlTabulka('SELECT kod, nazev, popis FROM bg_produkt', array('Kód', 'Název', 'Popis'));
   1.286 +}
   1.287 +
   1.288 +
   1.289 +function sqlHtmlTabulkaBugu($r) {		
   1.290 +	if (count($r) > 0) {
   1.291 +		$htmlID = getMicrotime();
   1.292 +		html('<table class="sortable" id="tabulka' . $htmlID . '">');	
   1.293 +		html('<thead><tr>');
   1.294 +		htmlZnacka('td', 'Číslo');
   1.295 +		htmlZnacka('td', 'Název');
   1.296 +		htmlZnacka('td', 'Zadavatel');
   1.297 +		htmlZnacka('td', 'Řešitel');
   1.298 +		//htmlZnacka('td', 'Datum');
   1.299 +		html('</tr></thead><tbody>');	
   1.300 +		
   1.301 +		foreach ($r as $x) {
   1.302 +			html('<tr>');		
   1.303 +			htmlZnacka('td', '<a href="bug.php?id=' . $x['id'] . '">' . $x['id'] . '</a>');
   1.304 +			htmlZnacka('td', $x['nazev']);
   1.305 +			htmlZnacka('td', odkazNaUzivatele($x['zadavatel']));
   1.306 +			htmlZnacka('td', odkazNaUzivatele($x['resitel']));
   1.307 +			//htmlZnacka('td', $x['datum']);
   1.308 +			html('</tr>');
   1.309 +		}	
   1.310 +		html('</tbody></table>');
   1.311 +	} else {
   1.312 +		htmlOdstavec('Nic nenalezeno.');
   1.313 +	}
   1.314 +}
   1.315 +
   1.316 +function odkazNaUzivatele($uzivatel) {
   1.317 +	if ($uzivatel == '-') {
   1.318 +		return '-';
   1.319 +	} else {
   1.320 +		return '<a href="uzivatel.php?login=' . $uzivatel . '">' . $uzivatel . '</a>';
   1.321 +	}
   1.322 +}
   1.323 +
   1.324 +function logujChybu($popis, $chyba = null) {
   1.325 +	htmlOdstavec($popis);
   1.326 +}
   1.327 +
   1.328 +?>