php/heslo/lib/funkce.inc.php
author František Kučera <franta-hg@frantovo.cz>
Tue, 05 May 2009 17:08:09 +0200
changeset 3 74a6a363ff9e
child 4 e6abafe31efb
permissions -rwxr-xr-x
Převzatá struktura z BG
http://sorry.vse.cz/~xkucf03/skola.php
franta-hg@3
     1
<?php
franta-hg@3
     2
franta-hg@3
     3
require('nastaveni.php');
franta-hg@3
     4
franta-hg@3
     5
$zacatek = getMicrotime();
franta-hg@3
     6
franta-hg@3
     7
function exception_handler($exception) {
franta-hg@3
     8
  html("Došlo k chybě: " . $exception->getMessage());
franta-hg@3
     9
  //header("Location: chyba_db.php");
franta-hg@3
    10
  exit();
franta-hg@3
    11
}
franta-hg@3
    12
franta-hg@3
    13
set_exception_handler('exception_handler');
franta-hg@3
    14
franta-hg@3
    15
session_set_cookie_params(0, $NASTAVENI['cookie_cesta']);
franta-hg@3
    16
session_start();
franta-hg@3
    17
franta-hg@3
    18
franta-hg@3
    19
$strankaP = split('/', $_SERVER['REQUEST_URI']);
franta-hg@3
    20
$stranka = $strankaP[count($strankaP)-1];
franta-hg@3
    21
franta-hg@3
    22
franta-hg@3
    23
if (empty($_SERVER['HTTP_ACCEPT'])) {
franta-hg@3
    24
	$mimeTyp = 'application/xhtml+xml';
franta-hg@3
    25
} else {
franta-hg@3
    26
	$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' );
franta-hg@3
    27
}
franta-hg@3
    28
header("Content-Type: $mimeTyp");
franta-hg@3
    29
//header("Content-Type: text/html");
franta-hg@3
    30
//header("Content-Type: application/xhtml+xml");
franta-hg@3
    31
franta-hg@3
    32
/** $nadpis = titulek stránky + h1 */
franta-hg@3
    33
function zahlavi($nadpis, $autorizace = true) {
franta-hg@3
    34
	global $mimeTyp;
franta-hg@3
    35
	if ($autorizace) {
franta-hg@3
    36
		overPristup();
franta-hg@3
    37
	}
franta-hg@3
    38
	
franta-hg@3
    39
	html('<?xml version="1.0" encoding="UTF-8"?>
franta-hg@3
    40
<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
franta-hg@3
    41
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
franta-hg@3
    42
  <head>
franta-hg@3
    43
    <meta http-equiv="content-language" content="cs"/>
franta-hg@3
    44
    <meta http-equiv="content-type" content="' . $mimeTyp . '; charset=UTF-8"/>
franta-hg@3
    45
    <meta name="GOOGLEBOT" content="NOINDEX, NOFOLLOW"/>
franta-hg@3
    46
    <meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/>
franta-hg@3
    47
    <link href="styl.css" type="text/css" rel="StyleSheet"/>
franta-hg@3
    48
    <script type="text/javaScript" src="hlavni.js"></script>
franta-hg@3
    49
    <script type="text/javaScript" src="tridenitabulky.js"></script>
franta-hg@3
    50
    <title>' . $nadpis . '</title>
franta-hg@3
    51
  </head>
franta-hg@3
    52
  <body>  
franta-hg@3
    53
<h1>' . $nadpis . '</h1>');
franta-hg@3
    54
if ($autorizace) {
franta-hg@3
    55
html('<p id="prihlasenyUzivatel">Uživatel: ' . $_SESSION['login']  . '</p>');
franta-hg@3
    56
}
franta-hg@3
    57
}
franta-hg@3
    58
franta-hg@3
    59
/** $zobrazitNabidku = zobrazí postranní menu */
franta-hg@3
    60
function zapati($zobrazitNabidku = true) {
franta-hg@3
    61
	if ($zobrazitNabidku) {
franta-hg@3
    62
		html('<h2 id="skrytyNadpisObsahu">Obsah</h2>
franta-hg@3
    63
<ul id="obsah">
franta-hg@3
    64
<li>
franta-hg@3
    65
<a href="index.php">Přehled</a>
franta-hg@3
    66
</li>
franta-hg@3
    67
<li>
franta-hg@3
    68
<a href="moje_bugy.php">Moje bugy</a>
franta-hg@3
    69
</li>
franta-hg@3
    70
<li>
franta-hg@3
    71
<a href="nahlasit_bug.php">Nahlásit bug</a>
franta-hg@3
    72
</li>
franta-hg@3
    73
<li>
franta-hg@3
    74
<a href="hledani.php">Hledání</a>
franta-hg@3
    75
</li>
franta-hg@3
    76
<li>
franta-hg@3
    77
<a href="sprava.php">Správa</a>
franta-hg@3
    78
</li>
franta-hg@3
    79
<li>
franta-hg@3
    80
<a href="odhlaseni.php">Odhlášení</a>
franta-hg@3
    81
</li>	
franta-hg@3
    82
</ul>
franta-hg@3
    83
</body>');
franta-hg@3
    84
htmlInfoKomentar();
franta-hg@3
    85
html('</html>');
franta-hg@3
    86
	} else {
franta-hg@3
    87
		html('</body>');
franta-hg@3
    88
		htmlInfoKomentar();
franta-hg@3
    89
		html('</html>');
franta-hg@3
    90
	}
franta-hg@3
    91
franta-hg@3
    92
}
franta-hg@3
    93
franta-hg@3
    94
function htmlInfoKomentar() {	
franta-hg@3
    95
	global $zacatek, $mimeTyp, $NASTAVENI;
franta-hg@3
    96
	if ($NASTAVENI['ladiciKomentare']) {
franta-hg@3
    97
		$konec = getMicrotime();
franta-hg@3
    98
		html('<!--');
franta-hg@3
    99
		html('Stránka: ' . $_SERVER['REQUEST_URI']);
franta-hg@3
   100
		html('Klient:  ' . $_SERVER['REMOTE_ADDR']);
franta-hg@3
   101
		html('MIME:    ' . $mimeTyp);
franta-hg@3
   102
		html("Začátek zpracování: $zacatek");
franta-hg@3
   103
		html("Konec zpracování:   $konec");
franta-hg@3
   104
		html('Doba zpracování:    ' . ($konec - $zacatek) . 's');	
franta-hg@3
   105
		html('-->');
franta-hg@3
   106
	}
franta-hg@3
   107
}
franta-hg@3
   108
franta-hg@3
   109
function htmlOdstavec($text) {
franta-hg@3
   110
	html("<p>\n$text\n</p>");
franta-hg@3
   111
}
franta-hg@3
   112
franta-hg@3
   113
function html($text) {	
franta-hg@3
   114
	global $stranka;	
franta-hg@3
   115
	/** Odstraníme odkazy, které vedou na tutéž stránku */
franta-hg@3
   116
	$text = str_replace(' href="' . $stranka . '"', '', $text);
franta-hg@3
   117
	
franta-hg@3
   118
	echo("$text\n");
franta-hg@3
   119
}
franta-hg@3
   120
franta-hg@3
   121
function htmlZnacka($znacka, $text) {
franta-hg@3
   122
	html("<$znacka>$text</$znacka>");
franta-hg@3
   123
}
franta-hg@3
   124
franta-hg@3
   125
function htmlTlacitkovyOdkaz($url, $text) {
franta-hg@3
   126
	html('<form action="' . $url . '"><fieldset><button>' . $text . '</button></fieldset></form>');
franta-hg@3
   127
}
franta-hg@3
   128
franta-hg@3
   129
function escapuj($text, $delka = 256) {
franta-hg@3
   130
	$text = str_replace("\n", "[br]", $text);
franta-hg@3
   131
	$text = substr(mysql_escape_string(htmlspecialchars($text)), 0, $delka);
franta-hg@3
   132
	$text = str_replace("[br]", "<br>", $text);
franta-hg@3
   133
	//$retezec = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\">\\0</a>", $retezec);
franta-hg@3
   134
	return $text;
franta-hg@3
   135
}
franta-hg@3
   136
franta-hg@3
   137
function htmlIkonaKontrolyIP () {
franta-hg@3
   138
	global $NASTAVENI;
franta-hg@3
   139
	if ($NASTAVENI['kontrolovatIPadresu']) {
franta-hg@3
   140
		html('<img src="kontrola_ip.png" alt="Kontrola IP" title="Kontroluje se IP adresa"/>');
franta-hg@3
   141
	}
franta-hg@3
   142
}
franta-hg@3
   143
franta-hg@3
   144
/** volá se na všech strákách, které vyžadují autorizaci (všechny kromě  prihlaseni.php) */
franta-hg@3
   145
function overPristup() {
franta-hg@3
   146
	global $NASTAVENI;
franta-hg@3
   147
	
franta-hg@3
   148
	if ($_SESSION['login'] == null || !isset($_SESSION['login']) || ($NASTAVENI['kontrolovatIPadresu'] && $_SERVER['REMOTE_ADDR'] != $_SESSION['ip_adresa_klienta'])) {
franta-hg@3
   149
		header("Location: prihlaseni.php");
franta-hg@3
   150
		exit();
franta-hg@3
   151
	}
franta-hg@3
   152
}
franta-hg@3
   153
franta-hg@3
   154
/** volá se v prihlaseni.php */
franta-hg@3
   155
function zkontrolujUzivatele($jmeno, $heslo) {
franta-hg@3
   156
	global $db, $NASTAVENI;
franta-hg@3
   157
	
franta-hg@3
   158
	if ($NASTAVENI['hashovatHesla']) {
franta-hg@3
   159
		$dotaz = $db->prepare("SELECT * FROM bg_uzivatel WHERE login = ? AND sha1(concat(?, heslo)) = ?");
franta-hg@3
   160
		$dotaz->bindParam(1, $jmeno);
franta-hg@3
   161
		$dotaz->bindParam(2, $_SESSION['hesloBordel']);
franta-hg@3
   162
		$dotaz->bindParam(3, $heslo);
franta-hg@3
   163
		$dotaz->execute();	
franta-hg@3
   164
		$r = $dotaz->fetchAll();
franta-hg@3
   165
	} else {
franta-hg@3
   166
		$dotaz = $db->prepare("SELECT * FROM bg_uzivatel WHERE login = ? AND heslo = sha1(?)");
franta-hg@3
   167
		$dotaz->bindParam(1, $jmeno);		
franta-hg@3
   168
		$dotaz->bindParam(2, $heslo);
franta-hg@3
   169
		$dotaz->execute();	
franta-hg@3
   170
		$r = $dotaz->fetchAll();		
franta-hg@3
   171
	}
franta-hg@3
   172
	
franta-hg@3
   173
	foreach ($r as $x) {
franta-hg@3
   174
		$_SESSION['login'] = $x['login'];
franta-hg@3
   175
		$_SESSION['ip_adresa_klienta'] = $_SERVER['REMOTE_ADDR'];
franta-hg@3
   176
		return true;
franta-hg@3
   177
	}
franta-hg@3
   178
	
franta-hg@3
   179
	$_SESSION['login'] = null;
franta-hg@3
   180
	return false;
franta-hg@3
   181
}
franta-hg@3
   182
franta-hg@3
   183
franta-hg@3
   184
function sqlZjistiHodnotu($dotaz, $parametry = null) {
franta-hg@3
   185
	global $db;
franta-hg@3
   186
	
franta-hg@3
   187
	$dotaz = $db->prepare($dotaz);
franta-hg@3
   188
	
franta-hg@3
   189
	if ($parametry != null) {
franta-hg@3
   190
		for($i = 0; $i < count($parametry); $i++) {
franta-hg@3
   191
			$dotaz->bindParam($i+1, $parametry[$i]);
franta-hg@3
   192
		}
franta-hg@3
   193
	}
franta-hg@3
   194
	
franta-hg@3
   195
	$dotaz->execute();
franta-hg@3
   196
	$r = $dotaz->fetchAll();
franta-hg@3
   197
	
franta-hg@3
   198
	foreach ($r as $x) {
franta-hg@3
   199
		return $x[0];		
franta-hg@3
   200
	}
franta-hg@3
   201
	return null;
franta-hg@3
   202
}
franta-hg@3
   203
franta-hg@3
   204
function sqlDotaz($dotaz, $parametry = null) {
franta-hg@3
   205
	global $db;
franta-hg@3
   206
	try {
franta-hg@3
   207
		$dotaz = $db->prepare($dotaz);
franta-hg@3
   208
	
franta-hg@3
   209
		if ($parametry != null) {
franta-hg@3
   210
			for($i = 0; $i < count($parametry); $i++) {
franta-hg@3
   211
				$dotaz->bindParam($i+1, $parametry[$i]);
franta-hg@3
   212
			}
franta-hg@3
   213
		}
franta-hg@3
   214
	
franta-hg@3
   215
		$dotaz->execute();
franta-hg@3
   216
		$r = $dotaz->fetchAll();
franta-hg@3
   217
	
franta-hg@3
   218
		return $r;
franta-hg@3
   219
	} catch (PDOException $e) {
franta-hg@3
   220
		logujChybu("sqlDotaz($dotaz)");		
franta-hg@3
   221
	}
franta-hg@3
   222
}
franta-hg@3
   223
franta-hg@3
   224
function getMicrotime(){
franta-hg@3
   225
   list($usec, $sec) = explode(" ",microtime());
franta-hg@3
   226
   return ((float)$usec + (float)$sec);
franta-hg@3
   227
} 
franta-hg@3
   228
franta-hg@3
   229
function sqlHtmlTabulka($dotaz, $hlavicky = null, $parametry = null, $htmlID = null) {
franta-hg@3
   230
	global $db;
franta-hg@3
   231
	
franta-hg@3
   232
	$dotaz = $db->prepare($dotaz);
franta-hg@3
   233
	
franta-hg@3
   234
	if ($parametry != null) {
franta-hg@3
   235
		for($i = 0; $i < count($parametry); $i++) {
franta-hg@3
   236
			$dotaz->bindParam($i+1, $parametry[$i]);
franta-hg@3
   237
		}
franta-hg@3
   238
	}
franta-hg@3
   239
	
franta-hg@3
   240
	$dotaz->execute();
franta-hg@3
   241
	$r = $dotaz->fetchAll();
franta-hg@3
   242
	
franta-hg@3
   243
	if ($htmlID == null) {
franta-hg@3
   244
		$htmlID = getMicrotime();
franta-hg@3
   245
	}
franta-hg@3
   246
	
franta-hg@3
   247
	
franta-hg@3
   248
	html('<table class="sortable" id="tabulka' . $htmlID . '">');	
franta-hg@3
   249
	if ($hlavicky != null) {
franta-hg@3
   250
		html('<thead><tr>');
franta-hg@3
   251
		foreach ($hlavicky as $h) {
franta-hg@3
   252
			htmlZnacka('td', $h);
franta-hg@3
   253
		}
franta-hg@3
   254
		html('</tr></thead>');
franta-hg@3
   255
		$pocet = count($hlavicky);
franta-hg@3
   256
	} else {
franta-hg@3
   257
		$pocet = null;
franta-hg@3
   258
	}
franta-hg@3
   259
	html('<tbody>');	
franta-hg@3
   260
		
franta-hg@3
   261
	foreach ($r as $x) {
franta-hg@3
   262
		html('<tr>');
franta-hg@3
   263
		if ($pocet == null) {
franta-hg@3
   264
			/** děleno 2 protože jsou tam číselné i řetězcové klíče */
franta-hg@3
   265
			$pocet = count($x) / 2;
franta-hg@3
   266
		}
franta-hg@3
   267
		
franta-hg@3
   268
		for ($i = 0; $i < $pocet; $i++) {			
franta-hg@3
   269
			htmlZnacka('td', $x[$i]);
franta-hg@3
   270
		}
franta-hg@3
   271
		html('</tr>');
franta-hg@3
   272
	}	
franta-hg@3
   273
	html('</tbody></table>');
franta-hg@3
   274
}
franta-hg@3
   275
franta-hg@3
   276
franta-hg@3
   277
function htmlTabulkaUzivatelu() {
franta-hg@3
   278
	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'));
franta-hg@3
   279
}
franta-hg@3
   280
franta-hg@3
   281
function htmlTabulkaProduktu() {
franta-hg@3
   282
	sqlHtmlTabulka('SELECT kod, nazev, popis FROM bg_produkt', array('Kód', 'Název', 'Popis'));
franta-hg@3
   283
}
franta-hg@3
   284
franta-hg@3
   285
franta-hg@3
   286
function sqlHtmlTabulkaBugu($r) {		
franta-hg@3
   287
	if (count($r) > 0) {
franta-hg@3
   288
		$htmlID = getMicrotime();
franta-hg@3
   289
		html('<table class="sortable" id="tabulka' . $htmlID . '">');	
franta-hg@3
   290
		html('<thead><tr>');
franta-hg@3
   291
		htmlZnacka('td', 'Číslo');
franta-hg@3
   292
		htmlZnacka('td', 'Název');
franta-hg@3
   293
		htmlZnacka('td', 'Zadavatel');
franta-hg@3
   294
		htmlZnacka('td', 'Řešitel');
franta-hg@3
   295
		//htmlZnacka('td', 'Datum');
franta-hg@3
   296
		html('</tr></thead><tbody>');	
franta-hg@3
   297
		
franta-hg@3
   298
		foreach ($r as $x) {
franta-hg@3
   299
			html('<tr>');		
franta-hg@3
   300
			htmlZnacka('td', '<a href="bug.php?id=' . $x['id'] . '">' . $x['id'] . '</a>');
franta-hg@3
   301
			htmlZnacka('td', $x['nazev']);
franta-hg@3
   302
			htmlZnacka('td', odkazNaUzivatele($x['zadavatel']));
franta-hg@3
   303
			htmlZnacka('td', odkazNaUzivatele($x['resitel']));
franta-hg@3
   304
			//htmlZnacka('td', $x['datum']);
franta-hg@3
   305
			html('</tr>');
franta-hg@3
   306
		}	
franta-hg@3
   307
		html('</tbody></table>');
franta-hg@3
   308
	} else {
franta-hg@3
   309
		htmlOdstavec('Nic nenalezeno.');
franta-hg@3
   310
	}
franta-hg@3
   311
}
franta-hg@3
   312
franta-hg@3
   313
function odkazNaUzivatele($uzivatel) {
franta-hg@3
   314
	if ($uzivatel == '-') {
franta-hg@3
   315
		return '-';
franta-hg@3
   316
	} else {
franta-hg@3
   317
		return '<a href="uzivatel.php?login=' . $uzivatel . '">' . $uzivatel . '</a>';
franta-hg@3
   318
	}
franta-hg@3
   319
}
franta-hg@3
   320
franta-hg@3
   321
function logujChybu($popis, $chyba = null) {
franta-hg@3
   322
	htmlOdstavec($popis);
franta-hg@3
   323
}
franta-hg@3
   324
franta-hg@3
   325
?>