e4587c1eb7962aa9b9684b5d735bb0b080eca574
[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]."@$cvs_mailhost\">".$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=(!isset($HTTP_SERVER_VARS["HTTP_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<p>&nbsp;<br />&nbsp;</p><hr /><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_array($query)
109 {
110         $q=db_query($query);
111         $r=mysql_fetch_array($q);
112         mysql_free_result($q);
113         return($r);
114 }
115
116 function db_item($query)
117 {
118         $row=db_row($query);
119         return($row[0]);
120 }
121
122 function num2greg($num)
123 {
124         $r="";
125         $vals=array(1=>"I",5=>"V",10=>"X",50=>"L",100=>"C",500=>"D",1000=>"M");
126         krsort($vals,SORT_NUMERIC);
127         foreach($vals as $val=>$sym) {
128                 while ($num<0 && $num+$val<-$num) {
129                         $r=substr($r,0,-1).$sym.substr($r,-1,1);
130                         $num+=$val;
131                         }
132                 while (10*$num>=8*$val+(substr($val,0,1)=="1")) {
133                         $r=$r.$sym;
134                         $num-=$val;
135                         }
136                 }
137         return($r);
138 }
139
140 function month_a($year,$month,$month_last=0,$sequential=0)
141 {
142         global $tb_obsah,$obsah_year_base;
143
144         if (!$month_last || !$sequential)
145                 list($month_last,$sequential)=db_row("select month_last,sequential from $tb_obsah where year='$year' and month='$month'");
146         $month_full=sprintf("%02d".($month==$month_last ? "" : "-%02d"),$month,$month_last);
147
148         $r=array("year"=>$year,"month"=>$month,"month_last"=>$month_last,"month_full"=>$month_full,
149                         "name"=>"$month"    .($month==$month_last ? "" : "-$month_last")."/$year",
150                         "img" =>"img/eap-$year-${month_full}.jpeg",
151                         "icon"=>"img/eap-$year-${month_full}s.jpeg",
152                         );
153         $r["name_full"]=$r["name"]
154                         ." ($sequential".($month==$month_last ? "" : "-".($sequential+$month_last-$month)).")"
155                         ." - ".num2greg($year-$obsah_year_base+1).". roèník";
156         return($r);
157 }
158
159 function img_size($width,$height)
160 {
161         global $have_style;
162
163         return(($have_style ? "style=\"border:0;width:${width}px;height:${height}px\"" : "border=\"0\"")
164                         ." width=\"$width\" height=\"$height\"");
165 }
166
167 function img($file,$alt,$attrs="")
168 {
169         list($width,$height)=getimagesize($file);
170         return("<img src=\"$file\" alt=\"".htmlspecialchars($alt)."\" ".img_size($width,$height)
171                         .($attrs=="" ? "" : " ".$attrs)." />");
172 }
173
174 function gsm_banking()
175 {
176         return(img("img/sluzby_bankovni_pggsm.gif","Paegas GSM banking","class=\"img-align\""));
177 }
178
179 function price_a()
180 {
181         if (!($f=fopen("objednavka.js","r")))
182                 fatal("Nepodaøilo se naèíst seznam dostupného zbo¾í");
183         $r=array();
184         while (($s=fgets($f,0x1000))) {
185                 if (!($s=trim($s)))
186                         break;
187                 if (!(ereg("^want_price\\[ *'([^']*)'\\]=([0-9]*);$",$s,$matched)))
188                         continue;
189                 $r[$matched[1]]=$matched[2];
190                 }
191         fclose($f);
192         return($r);
193 }
194
195 function title_name($year,$month)
196 {
197              if (isset($year) && isset($month)) {
198                 $month_a=month_a($year,$month);
199                 return("Èíslo ".$month_a["name_full"]);
200                 }
201         else if (isset($year))
202                 return("Roèník $year");
203         else
204                 return("V¹echny roèníky");
205 }
206
207 function title_month($year,$month)
208 {
209   $month_a=month_a($year,$month);
210         return(""
211                         ."<table border=\"0\" width=\"100%\">\n"
212                         ."<tr><td align=\"center\"><table border=\"1\" cellpadding=\"10\">\n"
213                         ."<tr><td align=\"center\">".img($month_a["img"],"titulní stránka ".$month_a["name"])."</td></tr>\n"
214                         ."</table></td></tr>\n"
215                         ."</table>\n"
216                         );
217 }
218
219 function title_icons_table_month($year,$month,$month_last,$sequential)
220 {
221         global $tb_clanek;
222
223         $month_a=month_a($year,$month,$month_last,$sequential);
224         print("<table border=\"0\">"
225                         ."<tr><th align=\"center\">".$month_a["name"]."</th></tr>\n"
226                         ."<tr><td align=\"center\" valign=\"top\">"
227                                 .img($month_a["icon"],"titulní stránka ".$month_a["name_full"])."</td></tr>\n"
228                         ."<tr><td align=\"left\" valign=\"top\" class=\"nowrap\">\n"
229                                 ."&bull;&nbsp;<a href=\"title.php?year=$year&amp;month=$month\">titulní strana</a><br />\n"
230                                 ."&bull;&nbsp;<a href=\"obsah.php?year=$year&amp;month=$month\">obsah</a><br />\n");
231         $result=db_query("select name,id from $tb_clanek where year=$year and month=$month order by id");
232         while ($row=mysql_fetch_array($result))
233                 print("&bull;&nbsp;<a href=\"clanek.php?year=$year&amp;month=$month&amp;id=".$row["id"]."\">"
234                                 .htmlspecialchars($row["name"])."</a><br />\n");
235         mysql_free_result($result);
236         print("</td></tr></table>");
237 }
238
239 function title_icons($year,$month)
240 {
241         global $tb_obsah;
242
243         print("<h2>"
244                 .(isset($year) ? "<a id=\"year_$year\">" : "")
245                 .title_name(&$year,&$month)
246                 .(isset($year) ? "</a>" : "")
247                 ."</h2>\n");
248
249         $result=db_query("select year,month,month_last,sequential from $tb_obsah"
250                         .(isset($year) || isset($month) ? " where" : "")
251                         .(isset($year ) ? " year=$year"   : "")
252                         .(isset($year) && isset($month) ? " and" : "")
253                         .(isset($month) ?   " month=$month" : "")
254                         ." order by year,month");
255         $split=4;
256
257         // $year variable changes its meaning here!!!
258         if (isset($year))
259                 $wanted_year=$year;
260         $year=0;
261
262         $fin_split="";
263         $fin_year="";
264         while ($row=mysql_fetch_array($result)) {
265                 $row["month"     ]--;
266                 $row["month_last"]--;
267
268                 if ($row["year"]!=$year) {
269                         print($fin_split.$fin_year);
270
271                         $year=$row["year"];
272                         print(""
273                                         .(!isset($wanted_year) ? "<p><a id=\"year_$year\">&nbsp;</a></p>" : "")
274                                         ."<table border=\"0\" width=\"100%\"><tr><td align=\"center\"><table border=\"1\" cellpadding=\"5\">\n"
275                                         ."<tr><th colspan=\"$split\">Roèník $year (<a href=\"obsah.php?year=$year\">obsahy èísel</a>)</th></tr>\n"
276                                         );
277                         $fin_year="</table></td></tr></table>\n";
278                         $fin_split="";
279                         $month=-1;
280                         $floor=-1;
281                         }
282                 while ($floor<floor($row["month"]/$split)) {
283                         print($fin_split."<tr>");
284                         $fin_split="</tr>\n";
285                         $floor++;
286                         $month=$floor*$split-1;
287                         }
288                 while ($month+1<$row["month"]) {
289                         print("<td></td>");
290                         $month++;
291                         }
292                 print("<td align=\"center\" valign=\"top\""
293                                 .($row["month_last"]!=$row["month"] ? " colspan=\"".($row["month_last"]+1-$row["month"])."\"" : "")
294                                 .">");
295                 title_icons_table_month($year,$row["month"]+1,$row["month_last"]+1,$row["sequential"]);
296                 print("</td>\n");
297                 $month=$row["month_last"];
298                 }
299         mysql_free_result($result);
300         print($fin_split.$fin_year);
301 }
302
303 function title($year,$month)
304 {
305         if (isset($year) && isset($month))
306                 return(title_month( $year, $month));
307         else
308                 return(title_icons(&$year,&$month));
309 }
310
311 function image_supported($mime)
312 {
313         global $HTTP_SERVER_VARS;
314
315         if (!isset($HTTP_SERVER_VARS["HTTP_ACCEPT"]))
316                 return(false);
317         $exp=explode(",",$HTTP_SERVER_VARS["HTTP_ACCEPT"]);
318         while (($s=array_shift($exp))) {
319                 $s=trim(ereg_replace(";.*","",$s));
320                 if ($s==$mime)
321                         return(true);
322                 }
323         return(false);
324 }
325
326 // PHP dirname() is broken: ("/1/2/3"=>"/1/2", "/1"->"/")
327 function dirnameslashed($filename)
328 {
329         $r=dirname($filename);
330         if (substr($r,-1)!="/")
331                 $r.="/";
332         return($r);
333 }
334
335 function usersize($size)
336 {
337         $suffix_a=array("","k","M","G","T");
338         while ($size>=1000 && sizeof($suffix_a)>=2) {
339                 $size/=1000;
340                 array_shift($suffix_a);
341                 }
342         return(round($size)." ".$suffix_a[0]."B");
343 }
344
345 function data_href($filename,$text)
346 {
347         return("<a href=\"$filename\">$text (".usersize(filesize($filename)).")</a>");
348 }
349
350 function footer($delimit=true)
351 {
352         // deadlock prevention:
353         global $footer_passed;
354         if (isset($footer_passed))
355                 exit();
356         $footer_passed=true;
357
358         global $cvs_id_html,$viewcvs,$viewcvs,$HTTP_SERVER_VARS;
359         if ($delimit)
360                 print("<p>&nbsp;</p>\n");
361         $uri="uri=".addpercents("http://".$HTTP_SERVER_VARS["HTTP_HOST"].$HTTP_SERVER_VARS["REQUEST_URI"]);
362         ?>
363 <hr />
364 <table border="0" width="100%">
365 <tr><td align="left"><span class="cvs-id"><?php print($cvs_id_html); ?></span></td><td align="right"><a
366         href="http://validator.w3.org/check?<?php
367                 print($uri); ?>"><img src="http://www.w3.org/Icons/valid-xhtml11"
368                 <?php print(img_size(88,31)); ?> alt="Valid XHTML 1.1!" /></a><a
369         href="http://jigsaw.w3.org/css-validator/validator?warning=2&amp;profile=css2&amp;<?php
370                 print($uri); ?>"><img src="http://jigsaw.w3.org/css-validator/images/vcss"
371                 <?php print(img_size(88,31)); ?> alt="Valid CSS!" /></a></td></tr>
372 </table>
373 </body></html>
374 <?php
375         exit();
376 }
377
378 // Stolen from: php-manual.html#function.header
379 function no_cache()
380 {
381         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");          // Date in the past
382         header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); // always modified
383         header("Cache-Control: no-cache, must-revalidate");        // HTTP/1.1
384         header("Pragma: no-cache");                                // HTTP/1.0
385 }
386
387 function heading($title=false)
388 {
389         global $HTTP_SERVER_VARS,$msie_major,$mozilla_major,$title_tail,$head_css,$have_css,$head;
390
391         header("Content-type: text/html");
392         header("Content-Style-Type: text/css");
393         if (!isset($msie_major) || $msie_major>=4)
394                 print('<?xml version="1.0" encoding="'.$HTTP_SERVER_VARS["CLIENT_CHARSET"].'"?>'."\n");
395 ?>
396 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
397 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs">
398 <head><title>Energie &amp; peníze<?php
399         if (isset($title_tail))
400                 print(": ".htmlspecialchars($title_tail));
401         print("</title>\n");
402         if ($have_css) {
403 ?><style type="text/css"><!--
404 .cvs-id   { font-family: monospace; }
405 .error    { color: red;   background-color: transparent; }
406 .quote    { font-family: monospace; }
407 .nowrap   { white-space: nowrap; }
408 .centered { text-align: center; }
409 body {
410                 background-color: black;
411                 color: white;
412                 }
413 :link    { color: aqua;   background-color: transparent; }
414 :visited { color: teal;   background-color: transparent; }
415 h1,h2    { color: yellow; background-color: transparent; }
416 <?php
417                 if (isset($head_css))
418                         print(trim($head_css)."\n");
419                 print("--></style>\n");
420                 }
421         if (isset($head))
422                 print($head);
423         print("</head><body");
424         if (isset($mozilla_major) && $mozilla_major==4)
425                 print(" bgcolor=\"black\" text=\"white\" link=\"aqua\" vlink=\"teal\"");
426         print(">\n");
427         if ($title)
428                 print("<h1 class=\"centered\">"
429                                 .img("img/eap-title.".(image_supported("image/png") ? "png" : "gif"),"Energie & Peníze")
430                                 ."</h1>\n");
431 }
432 ?>