php/heslo/lib/funkce.inc.php
author František Kučera <franta-hg@frantovo.cz>
Mon, 10 Mar 2014 12:21:02 +0100
changeset 27 60cfdaa89b3a
parent 15 ad391f838b9d
permissions -rwxr-xr-x
hgignore
     1 <?php
     2 
     3 require('nastaveni.php');
     4 
     5 $zacatek = getMicrotime();
     6 
     7 function exception_handler($exception) {
     8   html("Došlo k chybě: " . $exception->getMessage());
     9   //header("Location: chyba_db.php");
    10   exit();
    11 }
    12 
    13 set_exception_handler('exception_handler');
    14 
    15 
    16 if (empty($_SERVER['HTTP_ACCEPT'])) {
    17 	$mimeTyp = 'application/xhtml+xml';
    18 } else {
    19 	$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' );
    20 }
    21 header("Content-Type: $mimeTyp");
    22 
    23 /** $nadpis = titulek stránky + h1 */
    24 function zahlavi($nadpis) {
    25 	global $mimeTyp;
    26 	
    27 	html('<?xml version="1.0" encoding="UTF-8"?>
    28 <!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    29 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
    30   <head>
    31     <meta http-equiv="content-language" content="cs"/>
    32     <meta http-equiv="content-type" content="' . $mimeTyp . '; charset=UTF-8"/>
    33     <meta name="GOOGLEBOT" content="NOINDEX, NOFOLLOW"/>
    34     <meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/>
    35     <link href="styl.css" type="text/css" rel="StyleSheet"/>
    36     <title>' . $nadpis . '</title>
    37   </head>
    38   <body>  
    39 <h1>' . $nadpis . '</h1>');
    40 }
    41 
    42 function zapati() {
    43     html('</body>');
    44     htmlInfoKomentar();
    45     html('</html>');
    46 }
    47 
    48 function htmlInfoKomentar() {	
    49 	global $zacatek, $mimeTyp, $NASTAVENI;
    50 	$konec = getMicrotime();
    51 	html('<!--');
    52 	html('Stránka: ' . $_SERVER['REQUEST_URI']);
    53 	html('Klient:  ' . $_SERVER['REMOTE_ADDR']);
    54 	html('MIME:    ' . $mimeTyp);
    55 	html("Začátek zpracování: $zacatek");
    56 	html("Konec zpracování:   $konec");
    57 	html('Doba zpracování:    ' . ($konec - $zacatek) . 's');
    58 	html('Není potřeba hackovat, zdrojáky jsou k dispozici ;-)');
    59 	html('http://hg.vps.frantovo.cz/veverka.ch/');
    60 	html('-->');
    61 }
    62 
    63 function htmlOdstavec($text) {
    64 	html("<p>\n$text\n</p>");
    65 }
    66 
    67 function html($text) {	
    68 	global $stranka;	
    69 	/** Odstraníme odkazy, které vedou na tutéž stránku */
    70 	$text = str_replace(' href="' . $stranka . '"', '', $text);
    71 	
    72 	echo("$text\n");
    73 }
    74 
    75 function htmlZnacka($znacka, $text) {
    76 	html("<$znacka>$text</$znacka>");
    77 }
    78 
    79 function htmlTlacitkovyOdkaz($url, $text) {
    80 	html('<form action="' . $url . '"><fieldset><button>' . $text . '</button></fieldset></form>');
    81 }
    82 
    83 function sqlZjistiHodnotu($dotaz, $parametry = null) {
    84 	global $db;
    85 	
    86 	$dotaz = $db->prepare($dotaz);
    87 	
    88 	if ($parametry != null) {
    89 		for($i = 0; $i < count($parametry); $i++) {
    90 			$dotaz->bindParam($i+1, $parametry[$i]);
    91 		}
    92 	}
    93 	
    94 	$dotaz->execute();
    95 	$r = $dotaz->fetchAll();
    96 	
    97 	foreach ($r as $x) {
    98 		return $x[0];		
    99 	}
   100 	return null;
   101 }
   102 
   103 function sqlDotaz($dotaz, $parametry = null) {
   104         global $db;
   105         try {
   106                 $dotaz = $db->prepare($dotaz);
   107 
   108                 if ($parametry != null) {
   109                         for($i = 0; $i < count($parametry); $i++) {
   110                                 $dotaz->bindParam($i+1, $parametry[$i]);
   111                         }
   112                 }
   113 
   114                 $dotaz->execute();
   115                 $r = $dotaz->fetchAll();
   116 
   117                 return $r;
   118         } catch (PDOException $e) {
   119                 logujChybu("sqlDotaz($dotaz)");
   120         }
   121 }
   122 
   123 function echoClanek($id, $nadpis = 'h2') {
   124     $r = sqlDotaz("SELECT * FROM clanek WHERE id = ?", $id);
   125     foreach ($r as $x) {
   126 	htmlZnacka($nadpis, $x['nadpis']);
   127 	echo($x['text']);		
   128     }
   129 }
   130 
   131 function getMicrotime(){
   132    list($usec, $sec) = explode(" ",microtime());
   133    return ((float)$usec + (float)$sec);
   134 } 
   135 
   136 function logujChybu($popis, $chyba = null) {
   137 	htmlOdstavec($popis);
   138 }
   139 
   140 ?>