CSS disabled for "Mozilla/4.[5-7]" as they will corrupt the page
[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         $user_agent=$HTTP_SERVER_VARS["HTTP_USER_AGENT"];
23         if (ereg("[[:<:]]MSIE ([0-9]+)\\.",$user_agent,$msie_major_a))
24                 $msie_major=$msie_major_a[1];
25         else if (ereg("[[:<:]]Mozilla/([0-9]+)\\.",$user_agent,$mozilla_major_a))
26                 $mozilla_major=$mozilla_major_a[1];
27
28         $have_css=!ereg("Mozilla/4\\.[5-7]",$user_agent);
29
30 function addpercents($url)
31 {
32         $r=$c="";
33         foreach(preg_split('//', $url, -1, PREG_SPLIT_NO_EMPTY) as $c)
34                 if (ereg("[a-zA-Z0-9]",$c))
35                         $r.=$c;
36                 else
37                         $r.=sprintf("%%%02X",ord($c));
38         return($r);
39 }
40
41 function fatal($msg="UNKNOWN")
42 {
43         global $admin_mail;
44
45         print("\n<br /><h1 class=\"error\">Nastala chyba pøi zpracování: $msg!</h1>\n"
46                         ."<p>Mù¾ete tento problém nahlásit <a href=\"mailto:$admin_mail\">správci tohoto webu</a>.</p>\n");
47         footer();
48 }
49
50 function prepvar($name,$regex=".",$require=true)
51 {
52         global $HTTP_GET_VARS,$HTTP_POST_VARS;
53
54              if (isset($HTTP_GET_VARS[$name]))
55                 $v=$HTTP_GET_VARS[$name];
56         else if (isset($HTTP_POST_VARS[$name]))
57                 $v=$HTTP_POST_VARS[$name];
58         else
59                 unset($v);
60
61         $name_html="Parametr <span class=\"quote\">".htmlspecialchars($name)."</span>";
62
63         $regex="^$regex\$";
64         if (isset($v) && !ereg($regex,$v))
65                 fatal("$name_html nevyhovuje po¾adovanému regexu <span class=\"quote\">".htmlspecialchars($regex)."</span>");
66         if (!isset($v) && $require)
67                 fatal("$name_html je vy¾adován");
68
69         if (!isset($v))
70                 return(0);
71
72         $globals[$name]=$v;
73         return($v);
74 }
75
76 function db_connect()
77 {
78         global $db_host,$db_user,$db_pwd,$db_name;
79         global $db_link;
80
81         if (isset($db_link))
82                 return;
83         if (!($db_link=mysql_connect($db_host,$db_user,$db_pwd)))
84                 fatal("MySQL connect: ".mysql_error());
85         if (!mysql_select_db($db_name,$db_link))
86                 fatal("MySQL database select: ".mysql_error());
87 }
88
89 function db_query($query)
90 {
91         global $db_link;
92
93         db_connect();
94         if (!($r=mysql_query($query,$db_link)))
95                 fatal("MySQL query \"$query\": ".mysql_error());
96         return($r);
97 }
98
99 function db_row($query)
100 {
101         $q=db_query($query);
102         $r=mysql_fetch_row($q);
103         mysql_free_result($q);
104         return($r);
105 }
106
107 function db_item($query)
108 {
109         $row=db_row($query);
110         return($row[0]);
111 }
112
113 function num2greg($num)
114 {
115         $r="";
116         $vals=array(1=>"I",5=>"V",10=>"X",50=>"L",100=>"C",500=>"D",1000=>"M");
117         krsort($vals,SORT_NUMERIC);
118         foreach($vals as $val=>$sym) {
119                 while ($num<0 && $num+$val<-$num) {
120                         $r=substr($r,0,-1).$sym.substr($r,-1,1);
121                         $num+=$val;
122                         }
123                 while (10*$num>=8*$val+(substr($val,0,1)=="1")) {
124                         $r=$r.$sym;
125                         $num-=$val;
126                         }
127                 }
128         return($r);
129 }
130
131 function month_a($year,$month,$month_last=0,$sequential=0)
132 {
133         global $tb_obsah,$obsah_year_base;
134
135         if (!$month_last || !$sequential)
136                 list($month_last,$sequential)=db_row("select month_last,sequential from $tb_obsah where year='$year' and month='$month'");
137         $month_full=sprintf("%02d".($month==$month_last ? "" : "-%02d"),$month,$month_last);
138
139         $r=array("year"=>$year,"month"=>$month,"month_last"=>$month_last,"month_full"=>$month_full,
140                         "name"=>"$month"    .($month==$month_last ? "" : "-$month_last")."/$year",
141                         "img" =>"img/eap-$year-${month_full}.jpeg",
142                         "icon"=>"img/eap-$year-${month_full}s.jpeg",
143                         );
144         $r["name_full"]=$r["name"]
145                         ." ($sequential".($month==$month_last ? "" : "-".($sequential+$month_last-$month)).")"
146                         ." - ".num2greg($year-$obsah_year_base+1).". roèník";
147         return($r);
148 }
149
150 function img_size($width,$height)
151 {
152         return("style=\"border:0;width:${width}px;height:${height}px\" width=\"$width\" height=\"$height\"");
153 }
154
155 function img($file,$alt,$attrs="")
156 {
157         list($width,$height)=getimagesize($file);
158         return("<img src=\"$file\" alt=\"".htmlspecialchars($alt)."\" ".img_size($width,$height)
159                         .($attrs=="" ? "" : " ".$attrs)." />");
160 }
161
162 function gsm_banking()
163 {
164         return(img("img/sluzby_bankovni_pggsm.gif","Paegas GSM banking","class=\"img-align\""));
165 }
166
167 function price_a()
168 {
169         if (!($f=fopen("objednavka.js","r")))
170                 fatal("Nepodaøilo se naèíst seznam dostupného zbo¾í");
171         $r=array();
172         while (($s=fgets($f,0x1000))) {
173                 if (!($s=trim($s)))
174                         break;
175                 if (!(ereg("^want_price\\[ *'([^']*)'\\]=([0-9]*);$",$s,$matched)))
176                         continue;
177                 $r[$matched[1]]=$matched[2];
178                 }
179         fclose($f);
180         return($r);
181 }
182
183 function title_name($year,$month)
184 {
185              if (isset($year) && isset($month)) {
186                 $month_a=month_a($year,$month);
187                 return("Èíslo ".$month_a["name_full"]);
188                 }
189         else if (isset($year))
190                 return("Roèník $year");
191         else
192                 return("V¹echny roèníky");
193 }
194
195 function title_month($year,$month)
196 {
197   $month_a=month_a($year,$month);
198         return(""
199                         ."<table border=\"0\" width=\"100%\">\n"
200                         ."<tr><td align=\"center\"><table border=\"1\" cellpadding=\"10\">\n"
201                         ."<tr><td align=\"center\">".img($month_a["img"],"titulní stránka ".$month_a["name"])."</td></tr>\n"
202                         ."</table></td></tr>\n"
203                         ."</table>\n"
204                         );
205 }
206
207 function title_icons_table_month($year,$month,$month_last,$sequential)
208 {
209         $month_a=month_a($year,$month,$month_last,$sequential);
210         print("<a href=\"title.php?year=$year&amp;month=$month\">"
211                                 .img($month_a["icon"],"titulní stránka ".$month_a["name_full"])."</a><br />"
212                         ."<a href=\"obsah.php?year=$year&amp;month=$month\">obsah ".$month_a["name"]."</a>"
213                         );
214 }
215
216 function title_icons($year,$month)
217 {
218         global $tb_obsah;
219
220         print("<h2>"
221                 .(isset($year) ? "<a name=\"year_$year\">" : "")
222                 .title_name(&$year,&$month)
223                 .(isset($year) ? "</a>" : "")
224                 ."</h2>\n");
225
226         $result=db_query("select year,month,month_last,sequential from $tb_obsah"
227                         .(isset($year) || isset($month) ? " where" : "")
228                         .(isset($year ) ? " year=$year"   : "")
229                         .(isset($year) && isset($month) ? " and" : "")
230                         .(isset($month) ?   " month=$month" : "")
231                         ." order by year,month");
232         $split=6;
233
234         // $year variable changes its meaning here!!!
235         if (isset($year))
236                 $wanted_year=$year;
237         $year=0;
238
239         $fin_split="";
240         $fin_year="";
241         while ($row=mysql_fetch_array($result)) {
242                 $row["month"     ]--;
243                 $row["month_last"]--;
244
245                 if ($row["year"]!=$year) {
246                         print($fin_split.$fin_year);
247
248                         $year=$row["year"];
249                         print(""
250                                         .(!isset($wanted_year) ? "<p><a name=\"year_$year\">&nbsp;</a></p>" : "")
251                                         ."<table border=\"0\" width=\"100%\"><tr><td align=\"center\"><table border=\"1\" cellpadding=\"5\">\n"
252                                         ."<tr><th colspan=\"$split\">Roèník $year (<a href=\"obsah.php?year=$year\">obsahy èísel</a>)</th></tr>\n"
253                                         );
254                         $fin_year="</table></td></tr></table>\n";
255                         $fin_split="";
256                         $month=-1;
257                         $floor=-1;
258                         }
259                 while ($floor<floor($row["month"]/$split)) {
260                         print($fin_split."<tr>");
261                         $fin_split="</tr>\n";
262                         $floor++;
263                         $month=$floor*$split-1;
264                         }
265                 while ($month+1<$row["month"]) {
266                         print("<td></td>");
267                         $month++;
268                         }
269                 print("<td align=\"center\""
270                                 .($row["month_last"]!=$row["month"] ? " colspan=\"".($row["month_last"]+1-$row["month"])."\"" : "")
271                                 .">");
272                 title_icons_table_month($year,$row["month"]+1,$row["month_last"]+1,$row["sequential"]);
273                 print("</td>\n");
274                 $month=$row["month_last"];
275                 }
276         mysql_free_result($result);
277         print($fin_split.$fin_year);
278 }
279
280 function title($year,$month)
281 {
282         if (isset($year) && isset($month))
283                 return(title_month( $year, $month));
284         else
285                 return(title_icons(&$year,&$month));
286 }
287
288 function image_supported($mime)
289 {
290         global $HTTP_SERVER_VARS;
291
292         if (!isset($HTTP_SERVER_VARS["HTTP_ACCEPT"]))
293                 return(false);
294         $exp=explode(",",$HTTP_SERVER_VARS["HTTP_ACCEPT"]);
295         while (($s=array_shift($exp))) {
296                 $s=trim(ereg_replace(";.*","",$s));
297                 if ($s==$mime)
298                         return(true);
299                 }
300         return(false);
301 }
302
303 function footer()
304 {
305         // deadlock prevention:
306         global $footer_passed;
307         if (isset($footer_passed))
308                 exit();
309         $footer_passed=true;
310
311         global $cvs_id_html,$viewcvs,$viewcvs,$HTTP_SERVER_VARS;
312         ?>
313 <p>&nbsp;</p>
314 <hr />
315 <table border="0" width="100%">
316 <tr><td align="left"><span class="cvs-id"><?php print($cvs_id_html); ?></span></td><td align="right"><a
317         href="http://validator.w3.org/check/referer"><img src="http://www.w3.org/Icons/valid-xhtml10"
318                 <?php print(img_size(88,31)); ?> alt="Valid XHTML 1.0!" /></a><a
319         href="http://jigsaw.w3.org/css-validator/validator?warning=2&amp;profile=css2&amp;uri=<?php
320                 print(addpercents("http://".$HTTP_SERVER_VARS["HTTP_HOST"].$HTTP_SERVER_VARS["REQUEST_URI"]));
321                 ?>"><img src="http://jigsaw.w3.org/css-validator/images/vcss"
322                 <?php print(img_size(88,31)); ?> alt="Valid CSS!" /></a></td></tr>
323 </table>
324 </body></html>
325         <?php
326         exit();
327 }
328
329 // Stolen from: php-manual.html#function.header
330 function no_cache()
331 {
332         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");          // Date in the past
333         header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); // always modified
334         header("Cache-Control: no-cache, must-revalidate");        // HTTP/1.1
335         header("Pragma: no-cache");                                // HTTP/1.0
336 }
337
338 function heading()
339 {
340         global $msie_major,$mozilla_major,$title_tail,$head_css,$have_css,$head;
341
342         header("Content-type: text/html; charset=iso-8859-2");
343         if (!isset($msie_major) || $msie_major>=4)
344                 print('<?xml version="1.0" encoding="iso-8859-2"?>'."\n");
345 ?>
346 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
347 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
348 <head><title>Energie &amp; peníze<?php
349         if (isset($title_tail))
350                 print(": $title_tail");
351         print("</title>\n");
352         if ($have_css) {
353 ?><style type="text/css"><!--
354 .cvs-id  { font-family: monospace; }
355 .error   { color: red;    background-color: transparent; }
356 .quote   { font-family: monospace; }
357 body {
358                 background-color: black;
359                 color: white;
360                 }
361 :link    { color: aqua;   background-color: transparent; }
362 :visited { color: teal;   background-color: transparent; }
363 h1,h2    { color: yellow; background-color: transparent; }
364 <?php
365                 if (isset($head_css))
366                         print(trim($head_css)."\n");
367                 print("--></style>\n");
368                 }
369         if (isset($head))
370                 print($head);
371         print("</head><body");
372         if (isset($mozilla_major) && $mozilla_major==4)
373                 print(" bgcolor=\"black\" text=\"white\" link=\"cyan\" vlink=\"teal\"");
374         print(">\n");
375 }
376 ?>