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