function data_href: new (optional) argument $details
[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         heading(false/*title*/); // it is always safe
47         print("\n<p>&nbsp;<br />&nbsp;</p><hr /><h1 class=\"error\">Nastala chyba pøi zpracování: $msg!</h1>\n"
48                         ."<p>Mù¾ete tento problém nahlásit <a href=\"mailto:$admin_mail\">správci tohoto webu</a>.</p>\n");
49         footer();
50 }
51
52 function prepvar($name,$regex=".",$require=true)
53 {
54         global $HTTP_GET_VARS,$HTTP_POST_VARS;
55
56              if (isset($HTTP_GET_VARS[$name]))
57                 $v=$HTTP_GET_VARS[$name];
58         else if (isset($HTTP_POST_VARS[$name]))
59                 $v=$HTTP_POST_VARS[$name];
60         else
61                 unset($v);
62
63         $name_html="Parametr <span class=\"quote\">".htmlspecialchars($name)."</span>";
64
65         $regex="^$regex\$";
66         if (isset($v) && !ereg($regex,$v))
67                 fatal("$name_html nevyhovuje po¾adovanému regexu <span class=\"quote\">".htmlspecialchars($regex)."</span>");
68         if (!isset($v) && $require)
69                 fatal("$name_html je vy¾adován");
70
71         if (!isset($v))
72                 return(0);
73
74         $GLOBALS[$name]=$v;
75         return($v);
76 }
77
78 function db_connect()
79 {
80         global $db_host,$db_user,$db_pwd,$db_name;
81         global $db_link;
82
83         if (isset($db_link))
84                 return;
85         if (!($db_link=mysql_connect($db_host,$db_user,$db_pwd)))
86                 fatal("MySQL connect: ".mysql_error());
87         if (!mysql_select_db($db_name,$db_link))
88                 fatal("MySQL database select: ".mysql_error());
89 }
90
91 function db_query($query)
92 {
93         global $db_link;
94
95         db_connect();
96         if (!($r=mysql_query($query,$db_link)))
97                 fatal("MySQL query \"$query\": ".mysql_error());
98         return($r);
99 }
100
101 function db_row($query) // pure indexes
102 {
103         $q=db_query($query);
104         $r=mysql_fetch_row($q);
105         mysql_free_result($q);
106         return($r);
107 }
108
109 function db_array($query) // field-names associative
110 {
111         $q=db_query($query);
112         $r=mysql_fetch_array($q);
113         mysql_free_result($q);
114         return($r);
115 }
116
117 function db_item($query)
118 {
119         $row=db_row($query);
120         return($row[0]);
121 }
122
123 function num2greg($num)
124 {
125         $r="";
126         $vals=array(1=>"I",5=>"V",10=>"X",50=>"L",100=>"C",500=>"D",1000=>"M");
127         krsort($vals,SORT_NUMERIC);
128         foreach($vals as $val=>$sym) {
129                 while ($num<0 && $num+$val<-$num) {
130                         $r=substr($r,0,-1).$sym.substr($r,-1,1);
131                         $num+=$val;
132                         }
133                 while (10*$num>=8*$val+(substr($val,0,1)=="1")) {
134                         $r=$r.$sym;
135                         $num-=$val;
136                         }
137                 }
138         return($r);
139 }
140
141 function month_a($year,$month,$month_last=0,$sequential=0)
142 {
143         global $tb_obsah,$obsah_year_base;
144
145         if (!$month_last || !$sequential)
146                 list($month_last,$sequential)=db_row("select month_last,sequential from $tb_obsah where year='$year' and month='$month'");
147         $month_full=sprintf("%02d".($month==$month_last ? "" : "-%02d"),$month,$month_last);
148
149         $r=array("year"=>$year,"month"=>$month,"month_last"=>$month_last,"month_full"=>$month_full,
150                         "name"=>"$month"    .($month==$month_last ? "" : "-$month_last")."/$year",
151                         "img" =>"img/eap-$year-${month_full}.jpeg",
152                         "icon"=>"img/eap-$year-${month_full}s.jpeg",
153                         );
154         $r["name_full"]=$r["name"]
155                         ." ($sequential".($month==$month_last ? "" : "-".($sequential+$month_last-$month)).")"
156                         ." - ".num2greg($year-$obsah_year_base+1).". roèník";
157         return($r);
158 }
159
160 function img_size($width,$height)
161 {
162         global $have_style;
163
164         return(($have_style ? "style=\"border:0;width:${width}px;height:${height}px\"" : "border=\"0\"")
165                         ." width=\"$width\" height=\"$height\"");
166 }
167
168 function img($file,$alt,$attrs="")
169 {
170         list($width,$height)=getimagesize($file);
171         return("<img src=\"$file\" alt=\"".htmlspecialchars($alt)."\" ".img_size($width,$height)
172                         .($attrs=="" ? "" : " ".$attrs)." />");
173 }
174
175 function gsm_banking()
176 {
177         return(img("img/sluzby_bankovni_pggsm.gif","Paegas GSM banking","class=\"img-align\""));
178 }
179
180 function price_a()
181 {
182         if (!($f=fopen("objednavka.js","r")))
183                 fatal("Nepodaøilo se naèíst seznam dostupného zbo¾í");
184         $r=array();
185         while (($s=fgets($f,0x1000))) {
186                 if (!($s=trim($s)))
187                         break;
188                 if (!(ereg("^want_price\\[ *'([^']*)'\\]=([0-9]*);$",$s,$matched)))
189                         continue;
190                 $r[$matched[1]]=$matched[2];
191                 }
192         fclose($f);
193         return($r);
194 }
195
196 function title_name($year,$month)
197 {
198              if (isset($year) && isset($month)) {
199                 $month_a=month_a($year,$month);
200                 return("Èíslo ".$month_a["name_full"]);
201                 }
202         else if (isset($year))
203                 return("Roèník $year");
204         else
205                 return("V¹echny roèníky");
206 }
207
208 function title_month($year,$month)
209 {
210   $month_a=month_a($year,$month);
211         return(""
212                         ."<table border=\"0\" width=\"100%\">\n"
213                         ."<tr><td align=\"center\"><table border=\"1\" cellpadding=\"10\">\n"
214                         ."<tr><td align=\"center\">".img($month_a["img"],"titulní stránka ".$month_a["name"])."</td></tr>\n"
215                         ."</table></td></tr>\n"
216                         ."</table>\n"
217                         );
218 }
219
220 function title_icons_table_month($year,$month,$month_last,$sequential)
221 {
222         global $tb_clanek;
223
224         $month_a=month_a($year,$month,$month_last,$sequential);
225         print("<table border=\"0\">"
226                         ."<tr><th align=\"center\">".$month_a["name"]."</th></tr>\n"
227                         ."<tr><td align=\"center\" valign=\"top\">"
228                                 .img($month_a["icon"],"titulní stránka ".$month_a["name_full"])."</td></tr>\n"
229                         ."<tr><td align=\"left\" valign=\"top\" class=\"nowrap\">\n"
230                                 ."&bull;&nbsp;<a href=\"title.php?year=$year&amp;month=$month\">titulní strana</a><br />\n"
231                                 ."&bull;&nbsp;<a href=\"obsah.php?year=$year&amp;month=$month\">obsah</a><br />\n");
232         $result=db_query("select name,id from $tb_clanek where year=$year and month=$month order by id");
233         while ($row=mysql_fetch_array($result))
234                 print("&bull;&nbsp;<a href=\"clanek.php?year=$year&amp;month=$month&amp;id=".$row["id"]."\">"
235                                 .htmlspecialchars($row["name"])."</a><br />\n");
236         mysql_free_result($result);
237         print("</td></tr></table>");
238 }
239
240 function title_icons($year,$month)
241 {
242         global $tb_obsah;
243
244         print("<h2>"
245                 .(isset($year) ? "<a id=\"year_$year\">" : "")
246                 .title_name(&$year,&$month)
247                 .(isset($year) ? "</a>" : "")
248                 ."</h2>\n");
249
250         $result=db_query("select year,month,month_last,sequential from $tb_obsah"
251                         .(isset($year) || isset($month) ? " where" : "")
252                         .(isset($year ) ? " year=$year"   : "")
253                         .(isset($year) && isset($month) ? " and" : "")
254                         .(isset($month) ?   " month=$month" : "")
255                         ." order by year,month");
256         $split=4;
257
258         // $year variable changes its meaning here!!!
259         if (isset($year))
260                 $wanted_year=$year;
261         $year=0;
262
263         $fin_split="";
264         $fin_year="";
265         while ($row=mysql_fetch_array($result)) {
266                 $row["month"     ]--;
267                 $row["month_last"]--;
268
269                 if ($row["year"]!=$year) {
270                         print($fin_split.$fin_year);
271
272                         $year=$row["year"];
273                         print(""
274                                         .(!isset($wanted_year) ? "<p><a id=\"year_$year\">&nbsp;</a></p>" : "")
275                                         ."<table border=\"0\" width=\"100%\"><tr><td align=\"center\"><table border=\"1\" cellpadding=\"5\">\n"
276                                         ."<tr><th colspan=\"$split\">Roèník $year (<a href=\"obsah.php?year=$year\">obsahy èísel</a>)</th></tr>\n"
277                                         );
278                         $fin_year="</table></td></tr></table>\n";
279                         $fin_split="";
280                         $month=-1;
281                         $floor=-1;
282                         }
283                 while ($floor<floor($row["month"]/$split)) {
284                         print($fin_split."<tr>");
285                         $fin_split="</tr>\n";
286                         $floor++;
287                         $month=$floor*$split-1;
288                         }
289                 while ($month+1<$row["month"]) {
290                         print("<td></td>");
291                         $month++;
292                         }
293                 print("<td align=\"center\" valign=\"top\""
294                                 .($row["month_last"]!=$row["month"] ? " colspan=\"".($row["month_last"]+1-$row["month"])."\"" : "")
295                                 .">");
296                 title_icons_table_month($year,$row["month"]+1,$row["month_last"]+1,$row["sequential"]);
297                 print("</td>\n");
298                 $month=$row["month_last"];
299                 }
300         mysql_free_result($result);
301         print($fin_split.$fin_year);
302 }
303
304 function title($year,$month)
305 {
306         if (isset($year) && isset($month))
307                 return(title_month( $year, $month));
308         else
309                 return(title_icons(&$year,&$month));
310 }
311
312 function image_supported($mime)
313 {
314         global $HTTP_SERVER_VARS;
315
316         if (!isset($HTTP_SERVER_VARS["HTTP_ACCEPT"]))
317                 return(false);
318         $exp=explode(",",$HTTP_SERVER_VARS["HTTP_ACCEPT"]);
319         while (($s=array_shift($exp))) {
320                 $s=trim(ereg_replace(";.*","",$s));
321                 if ($s==$mime)
322                         return(true);
323                 }
324         return(false);
325 }
326
327 // PHP dirname() is broken: ("/1/2/3"=>"/1/2", "/1"->"/")
328 function dirnameslashed($filename)
329 {
330         $r=dirname($filename);
331         if (substr($r,-1)!="/")
332                 $r.="/";
333         return($r);
334 }
335
336 function usersize($size)
337 {
338         $suffix_a=array("","k","M","G","T");
339         while ($size>=1000 && sizeof($suffix_a)>=2) {
340                 $size/=1000;
341                 array_shift($suffix_a);
342                 }
343         return(round($size)." ".$suffix_a[0]."B");
344 }
345
346 function data_href($filename,$text,$details="")
347 {
348         return("<a href=\"$filename\">$text (".usersize(filesize($filename))."$details)</a>");
349 }
350
351 function img_href($filename,$text)
352 {
353         list($width,$height)=getimagesize($filename);
354         return(data_href($filename,$text,", ${width}x${height} bodù"));
355 }
356
357 function footer_img($url,$text,$size)
358 {
359         global $footer_LOCAL;
360         if (!isset($footer_LOCAL))
361                 return("<img src=\"$url\" $size alt=\"$text\" />");
362         else
363                 return($text);
364 }
365
366 function footer($delimit=true)
367 {
368         // deadlock prevention:
369         global $footer_passed;
370         if (isset($footer_passed))
371                 exit();
372         $footer_passed=true;
373
374         global $cvs_id_html,$viewcvs,$viewcvs,$HTTP_SERVER_VARS;
375         if ($delimit)
376                 print("<p>&nbsp;</p>\n");
377         $uri="uri=".addpercents("http://".$HTTP_SERVER_VARS["HTTP_HOST"].$HTTP_SERVER_VARS["REQUEST_URI"]);
378         ?>
379 <hr />
380 <table border="0" width="100%">
381 <tr><td align="left"><span class="cvs-id"><?php print($cvs_id_html); ?></span></td><td align="right"><a
382         href="http://validator.w3.org/check?<?php
383                 print($uri); ?>"><?php
384                                 print footer_img("http://www.w3.org/Icons/valid-xhtml11","Valid XHTML 1.1!",img_size(88,31));
385                                 ?></a><a href="http://jigsaw.w3.org/css-validator/validator?warning=2&amp;profile=css2&amp;<?php
386                 print($uri); ?>"><?php
387                                 print footer_img("http://jigsaw.w3.org/css-validator/images/vcss","Valid CSS!",img_size(88,31));
388                                 ?></a></td></tr>
389 </table>
390 </body></html>
391 <?php
392         exit();
393 }
394
395 // Stolen from: php-manual.html#function.header
396 function no_cache()
397 {
398         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");          // Date in the past
399         header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); // always modified
400         header("Cache-Control: no-cache, must-revalidate");        // HTTP/1.1
401         header("Pragma: no-cache");                                // HTTP/1.0
402 }
403
404 function heading($title=false)
405 {
406         global $HTTP_SERVER_VARS,$msie_major,$mozilla_major,$title_tail,$head_css,$have_css,$head,$heading_done,$title_prefix,$force_charset;
407
408         if (isset($heading_done))
409                 return;
410         $heading_done=1;
411
412         if (isset($force_charset))
413                 $client_charset=$force_charset;
414         else
415                 $client_charset=(!isset($HTTP_SERVER_VARS["CLIENT_CHARSET"]) ? "iso-8859-2" : $HTTP_SERVER_VARS["CLIENT_CHARSET"]);
416         // When "CLIENT_CHARSET" is set we MUST NOT explicitely specify our "charset"
417         header("Content-type: text/html".(isset($HTTP_SERVER_VARS["CLIENT_CHARSET"]) ? "" : "; charset=$client_charset"));
418         header("Content-Style-Type: text/css");
419         if (!isset($msie_major) || $msie_major>=4) {
420                 print('<?xml version="1.0" encoding="'.$client_charset.'"?>'."\n");
421                 }
422 ?>
423 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
424 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs">
425 <head><?php
426         print("<title>$title_prefix");
427         if (isset($title_tail))
428                 print(": ".htmlspecialchars($title_tail));
429         print("</title>\n");
430         if ($have_css) {
431 ?><style type="text/css"><!--
432 .cvs-id   { font-family: monospace; }
433 .error    { color: red;   background-color: transparent; }
434 .quote    { font-family: monospace; }
435 .nowrap   { white-space: nowrap; }
436 .centered { text-align: center; }
437 body {
438                 background-color: black;
439                 color: white;
440                 }
441 :link    { color: aqua;   background-color: transparent; }
442 :visited { color: teal;   background-color: transparent; }
443 h1,h2    { color: yellow; background-color: transparent; }
444 <?php
445                 if (isset($head_css))
446                         print(trim($head_css)."\n");
447                 print("--></style>\n");
448                 }
449         if (isset($head))
450                 print($head);
451         print("</head><body");
452         if (isset($mozilla_major) && $mozilla_major==4)
453                 print(" bgcolor=\"black\" text=\"white\" link=\"aqua\" vlink=\"teal\"");
454         print(">\n");
455         if ($title)
456                 print("<h1 class=\"centered\">"
457                                 .img("img/eap-title.".(image_supported("image/png") ? "png" : "gif"),"Energie & Peníze")
458                                 ."</h1>\n");
459 }
460 ?>