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