config.php introduced
[www.energie.vellum.cz.git] / common.php
1 <?php // $Id$
2
3         error_reporting(E_ALL);
4
5         include("config.php");
6
7         // $viewcvs prepared by "config.php"
8         if ($viewcvs==$HTTP_SERVER_VARS["SCRIPT_NAME"])
9                 unset($viewcvs);
10         $cvs_id_split=split(" ",$cvs_id);
11         if (count($cvs_id_split)==8) {
12                 $cvs_id_split[1]="<a href=\"$viewcvs\">".$cvs_id_split[1]."</a>";
13                 $cvs_id_split[5]="<a href=\"mailto:".$cvs_id_split[5]."@".$HTTP_SERVER_VARS["HTTP_HOST"]."\">".$cvs_id_split[5]."</a>";
14                 }
15         $cvs_id_html=join(" ",$cvs_id_split);
16
17         $have_js=(isset($HTTP_GET_VARS["have_js"]) || isset($HTTP_POST_VARS["have_js"])
18                         ? "?have_js=1" : "");
19         if (isset($detect_js) && !$have_js)
20                 $head.='<script type="text/javascript" src="have_js.js"></script>'."\n";
21
22         if (ereg("[[:<:]]MSIE ([0-9]+)\\.",$HTTP_SERVER_VARS["HTTP_USER_AGENT"],$msie_major_a))
23                 $msie_major=$msie_major_a[1];
24
25 function addpercents($url)
26 {
27         $r=$c="";
28         foreach(preg_split('//', $url, -1, PREG_SPLIT_NO_EMPTY) as $c)
29                 if (ereg("[a-zA-Z0-9]",$c))
30                         $r.=$c;
31                 else
32                         $r.=sprintf("%%%02X",ord($c));
33         return($r);
34 }
35
36 function fatal($msg="UNKNOWN")
37 {
38         global $admin_mail;
39
40         print("\n<br /><h1 class=\"error\">Nastala chyba pøi zpracování: $msg!</h1>\n"
41                         ."<p>Mù¾ete tento problém nahlásit <a href=\"mailto:$admin_mail\">správci tohoto webu</a>.</p>\n");
42         footer();
43 }
44
45 function prepvar($name,$regex=".",$require=true)
46 {
47         global $HTTP_GET_VARS,$HTTP_POST_VARS;
48
49              if (isset($HTTP_GET_VARS[$name]))
50                 $v=$HTTP_GET_VARS[$name];
51         else if (isset($HTTP_POST_VARS[$name]))
52                 $v=$HTTP_POST_VARS[$name];
53         else
54                 unset($v);
55
56         $name_html="Parametr <span class=\"quote\">".htmlspecialchars($name)."</span>";
57
58         $regex="^$regex\$";
59         if (isset($v) && !ereg($regex,$v))
60                 fatal("$name_html nevyhovuje po¾adovanému regexu <span class=\"quote\">".htmlspecialchars($regex)."</span>");
61         if (!isset($v) && $require)
62                 fatal("$name_html je vy¾adován");
63
64         if (!isset($v))
65                 return(0);
66
67         global $$name;
68         $$name=$v;
69         return($v);
70 }
71
72 function db_connect()
73 {
74         global $db_host,$db_user,$db_pwd,$db_name;
75         global $db_link;
76
77         if (isset($db_link))
78                 return;
79         if (!($db_link=@mysql_connect($db_host,$db_user,$db_pwd)))
80                 fatal("MySQL connect: ".mysql_error());
81         if (!mysql_select_db($db_name,$db_link))
82                 fatal("MySQL database select: ".mysql_error());
83 }
84
85 function db_query($query)
86 {
87         global $db_link;
88
89         db_connect();
90         if (!($r=mysql_query($query,$db_link)))
91                 fatal("MySQL query \"$query\": ".mysql_error());
92         return($r);
93 }
94
95 function db_row($query)
96 {
97         $q=db_query($query);
98         $r=mysql_fetch_row($q);
99         mysql_free_result($q);
100         return($r);
101 }
102
103 function db_item($query)
104 {
105         $row=db_row($query);
106         return($row[0]);
107 }
108
109 function month_full($year,$month,$month_last=0)
110 {
111         global $tb_obsah;
112
113         if (!$month_last)
114                 $month_last=db_item("select month_last from $tb_obsah where year='$year' and month='$month'");
115         return(sprintf("%02d".($month==$month_last ? "" : "-%02d"),$month,$month_last));
116 }
117
118 function img_size($width,$height)
119 {
120         return("style=\"border:0;width:${width}px;height:${height}px\" width=\"$width\" height=\"$height\"");
121 }
122
123 function footer()
124 {
125         // deadlock prevention:
126         global $footer_passed;
127         if (isset($footer_passed))
128                 exit();
129         $footer_passed=true;
130
131         global $cvs_id_html,$viewcvs,$viewcvs,$HTTP_SERVER_VARS;
132         ?>
133 <hr />
134 <table border="0" width="100%">
135 <tr><td align="left"><span class="cvs-id"><?php print($cvs_id_html); ?></span></td><td align="right"><a
136         href="http://validator.w3.org/check/referer"><img src="http://www.w3.org/Icons/valid-xhtml10"
137                 <?php print(img_size(88,31)); ?> alt="Valid XHTML 1.0!" /></a><a
138         href="http://jigsaw.w3.org/css-validator/validator?warning=2&amp;profile=css2&amp;uri=<?php
139                 print(addpercents("http://".$HTTP_SERVER_VARS["HTTP_HOST"].$HTTP_SERVER_VARS["REQUEST_URI"]));
140                 ?>"><img src="http://jigsaw.w3.org/css-validator/images/vcss"
141                 <?php print(img_size(88,31)); ?> alt="Valid CSS!" /></a></td></tr>
142 </table>
143 </body></html>
144         <?php
145         exit();
146 }
147
148 function heading()
149 {
150         global $msie_major,$title_tail,$head_css,$head;
151
152         header("Content-type: text/html; charset=iso-8859-2");
153         if (!isset($msie_major) || $msie_major>=4)
154                 print('<?xml version="1.0" encoding="iso-8859-2"?>'."\n");
155 ?>
156 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
157 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
158 <head><title>Energie &amp; peníze<?php
159         if (isset($title_tail))
160                 print(": $title_tail");
161 ?></title>
162 <style type="text/css"><!--
163 .cvs-id { font-family: monospace; }
164 .error  { color: red; }
165 .quote  { font-family: monospace; }
166 <?php
167         if (isset($head_css))
168                 print(trim($head_css)."\n");
169         print("--></style>\n");
170         if (isset($head))
171                 print($head);
172         print("</head><body>\n");
173 }
174 ?>