function data_href: new (optional) argument $details
[www.energie.vellum.cz.git] / common.php
index 94e1a70..59a765e 100644 (file)
@@ -10,7 +10,7 @@
        $cvs_id_split=split(" ",$cvs_id);
        if (count($cvs_id_split)==8) {
                $cvs_id_split[1]="<a href=\"$viewcvs\">".$cvs_id_split[1]."</a>";
-               $cvs_id_split[5]="<a href=\"mailto:".$cvs_id_split[5]."@".$HTTP_SERVER_VARS["HTTP_HOST"]."\">".$cvs_id_split[5]."</a>";
+               $cvs_id_split[5]="<a href=\"mailto:".$cvs_id_split[5]."@$cvs_mailhost\">".$cvs_id_split[5]."</a>";
                }
        $cvs_id_html=join(" ",$cvs_id_split);
 
        if (isset($detect_js) && !$have_js)
                $head.='<script type="text/javascript" src="have_js.js"></script>'."\n";
 
-       if (ereg("[[:<:]]MSIE ([0-9]+)\\.",$HTTP_SERVER_VARS["HTTP_USER_AGENT"],$msie_major_a))
+       $user_agent=(!isset($HTTP_SERVER_VARS["HTTP_USER_AGENT"]) ? "" : $HTTP_SERVER_VARS["HTTP_USER_AGENT"]);
+       if (ereg("[[:<:]]MSIE ([0-9]+)\\.",$user_agent,$msie_major_a))
                $msie_major=$msie_major_a[1];
+       else if (ereg("[[:<:]]Mozilla/([0-9]+)\\.",$user_agent,$mozilla_major_a))
+               $mozilla_major=$mozilla_major_a[1];
+
+       $have_style=!isset($mozilla_major) || $mozilla_major!=4;
+       $have_css=true; // doesn't hurt anybody AFAIK
 
 function addpercents($url)
 {
@@ -37,7 +43,8 @@ function fatal($msg="UNKNOWN")
 {
        global $admin_mail;
 
-       print("\n<br /><h1 class=\"error\">Nastala chyba pøi zpracování: $msg!</h1>\n"
+       heading(false/*title*/); // it is always safe
+       print("\n<p>&nbsp;<br />&nbsp;</p><hr /><h1 class=\"error\">Nastala chyba pøi zpracování: $msg!</h1>\n"
                        ."<p>Mù¾ete tento problém nahlásit <a href=\"mailto:$admin_mail\">správci tohoto webu</a>.</p>\n");
        footer();
 }
@@ -64,8 +71,7 @@ function prepvar($name,$regex=".",$require=true)
        if (!isset($v))
                return(0);
 
-       global $$name;
-       $$name=$v;
+       $GLOBALS[$name]=$v;
        return($v);
 }
 
@@ -76,7 +82,7 @@ function db_connect()
 
        if (isset($db_link))
                return;
-       if (!($db_link=@mysql_connect($db_host,$db_user,$db_pwd)))
+       if (!($db_link=mysql_connect($db_host,$db_user,$db_pwd)))
                fatal("MySQL connect: ".mysql_error());
        if (!mysql_select_db($db_name,$db_link))
                fatal("MySQL database select: ".mysql_error());
@@ -92,7 +98,7 @@ function db_query($query)
        return($r);
 }
 
-function db_row($query)
+function db_row($query) // pure indexes
 {
        $q=db_query($query);
        $r=mysql_fetch_row($q);
@@ -100,27 +106,264 @@ function db_row($query)
        return($r);
 }
 
+function db_array($query) // field-names associative
+{
+       $q=db_query($query);
+       $r=mysql_fetch_array($q);
+       mysql_free_result($q);
+       return($r);
+}
+
 function db_item($query)
 {
        $row=db_row($query);
        return($row[0]);
 }
 
-function month_full($year,$month,$month_last=0)
+function num2greg($num)
 {
-       global $tb_obsah;
+       $r="";
+       $vals=array(1=>"I",5=>"V",10=>"X",50=>"L",100=>"C",500=>"D",1000=>"M");
+       krsort($vals,SORT_NUMERIC);
+       foreach($vals as $val=>$sym) {
+               while ($num<0 && $num+$val<-$num) {
+                       $r=substr($r,0,-1).$sym.substr($r,-1,1);
+                       $num+=$val;
+                       }
+               while (10*$num>=8*$val+(substr($val,0,1)=="1")) {
+                       $r=$r.$sym;
+                       $num-=$val;
+                       }
+               }
+       return($r);
+}
 
-       if (!$month_last)
-               $month_last=db_item("select month_last from $tb_obsah where year='$year' and month='$month'");
-       return(sprintf("%02d".($month==$month_last ? "" : "-%02d"),$month,$month_last));
+function month_a($year,$month,$month_last=0,$sequential=0)
+{
+       global $tb_obsah,$obsah_year_base;
+
+       if (!$month_last || !$sequential)
+               list($month_last,$sequential)=db_row("select month_last,sequential from $tb_obsah where year='$year' and month='$month'");
+       $month_full=sprintf("%02d".($month==$month_last ? "" : "-%02d"),$month,$month_last);
+
+       $r=array("year"=>$year,"month"=>$month,"month_last"=>$month_last,"month_full"=>$month_full,
+                       "name"=>"$month"    .($month==$month_last ? "" : "-$month_last")."/$year",
+                       "img" =>"img/eap-$year-${month_full}.jpeg",
+                       "icon"=>"img/eap-$year-${month_full}s.jpeg",
+                       );
+       $r["name_full"]=$r["name"]
+                       ." ($sequential".($month==$month_last ? "" : "-".($sequential+$month_last-$month)).")"
+                       ." - ".num2greg($year-$obsah_year_base+1).". roèník";
+       return($r);
 }
 
 function img_size($width,$height)
 {
-       return("style=\"border:0;width:${width}px;height:${height}px\" width=\"$width\" height=\"$height\"");
+       global $have_style;
+
+       return(($have_style ? "style=\"border:0;width:${width}px;height:${height}px\"" : "border=\"0\"")
+                       ." width=\"$width\" height=\"$height\"");
+}
+
+function img($file,$alt,$attrs="")
+{
+       list($width,$height)=getimagesize($file);
+       return("<img src=\"$file\" alt=\"".htmlspecialchars($alt)."\" ".img_size($width,$height)
+                       .($attrs=="" ? "" : " ".$attrs)." />");
+}
+
+function gsm_banking()
+{
+       return(img("img/sluzby_bankovni_pggsm.gif","Paegas GSM banking","class=\"img-align\""));
 }
 
-function footer()
+function price_a()
+{
+       if (!($f=fopen("objednavka.js","r")))
+               fatal("Nepodaøilo se naèíst seznam dostupného zbo¾í");
+       $r=array();
+       while (($s=fgets($f,0x1000))) {
+               if (!($s=trim($s)))
+                       break;
+               if (!(ereg("^want_price\\[ *'([^']*)'\\]=([0-9]*);$",$s,$matched)))
+                       continue;
+               $r[$matched[1]]=$matched[2];
+               }
+       fclose($f);
+       return($r);
+}
+
+function title_name($year,$month)
+{
+            if (isset($year) && isset($month)) {
+               $month_a=month_a($year,$month);
+               return("Èíslo ".$month_a["name_full"]);
+               }
+       else if (isset($year))
+               return("Roèník $year");
+       else
+               return("V¹echny roèníky");
+}
+
+function title_month($year,$month)
+{
+  $month_a=month_a($year,$month);
+       return(""
+                       ."<table border=\"0\" width=\"100%\">\n"
+                       ."<tr><td align=\"center\"><table border=\"1\" cellpadding=\"10\">\n"
+                       ."<tr><td align=\"center\">".img($month_a["img"],"titulní stránka ".$month_a["name"])."</td></tr>\n"
+                       ."</table></td></tr>\n"
+                       ."</table>\n"
+                       );
+}
+
+function title_icons_table_month($year,$month,$month_last,$sequential)
+{
+       global $tb_clanek;
+
+       $month_a=month_a($year,$month,$month_last,$sequential);
+       print("<table border=\"0\">"
+                       ."<tr><th align=\"center\">".$month_a["name"]."</th></tr>\n"
+                       ."<tr><td align=\"center\" valign=\"top\">"
+                               .img($month_a["icon"],"titulní stránka ".$month_a["name_full"])."</td></tr>\n"
+                       ."<tr><td align=\"left\" valign=\"top\" class=\"nowrap\">\n"
+                               ."&bull;&nbsp;<a href=\"title.php?year=$year&amp;month=$month\">titulní strana</a><br />\n"
+                               ."&bull;&nbsp;<a href=\"obsah.php?year=$year&amp;month=$month\">obsah</a><br />\n");
+       $result=db_query("select name,id from $tb_clanek where year=$year and month=$month order by id");
+       while ($row=mysql_fetch_array($result))
+               print("&bull;&nbsp;<a href=\"clanek.php?year=$year&amp;month=$month&amp;id=".$row["id"]."\">"
+                               .htmlspecialchars($row["name"])."</a><br />\n");
+       mysql_free_result($result);
+       print("</td></tr></table>");
+}
+
+function title_icons($year,$month)
+{
+       global $tb_obsah;
+
+       print("<h2>"
+               .(isset($year) ? "<a id=\"year_$year\">" : "")
+               .title_name(&$year,&$month)
+               .(isset($year) ? "</a>" : "")
+               ."</h2>\n");
+
+       $result=db_query("select year,month,month_last,sequential from $tb_obsah"
+                       .(isset($year) || isset($month) ? " where" : "")
+                       .(isset($year ) ? " year=$year"   : "")
+                       .(isset($year) && isset($month) ? " and" : "")
+                       .(isset($month) ?   " month=$month" : "")
+                       ." order by year,month");
+       $split=4;
+
+       // $year variable changes its meaning here!!!
+       if (isset($year))
+               $wanted_year=$year;
+       $year=0;
+
+       $fin_split="";
+       $fin_year="";
+       while ($row=mysql_fetch_array($result)) {
+               $row["month"     ]--;
+               $row["month_last"]--;
+
+               if ($row["year"]!=$year) {
+                       print($fin_split.$fin_year);
+
+                       $year=$row["year"];
+                       print(""
+                                       .(!isset($wanted_year) ? "<p><a id=\"year_$year\">&nbsp;</a></p>" : "")
+                                       ."<table border=\"0\" width=\"100%\"><tr><td align=\"center\"><table border=\"1\" cellpadding=\"5\">\n"
+                                       ."<tr><th colspan=\"$split\">Roèník $year (<a href=\"obsah.php?year=$year\">obsahy èísel</a>)</th></tr>\n"
+                                       );
+                       $fin_year="</table></td></tr></table>\n";
+                       $fin_split="";
+                       $month=-1;
+                       $floor=-1;
+                       }
+               while ($floor<floor($row["month"]/$split)) {
+                       print($fin_split."<tr>");
+                       $fin_split="</tr>\n";
+                       $floor++;
+                       $month=$floor*$split-1;
+                       }
+               while ($month+1<$row["month"]) {
+                       print("<td></td>");
+                       $month++;
+                       }
+               print("<td align=\"center\" valign=\"top\""
+                               .($row["month_last"]!=$row["month"] ? " colspan=\"".($row["month_last"]+1-$row["month"])."\"" : "")
+                               .">");
+               title_icons_table_month($year,$row["month"]+1,$row["month_last"]+1,$row["sequential"]);
+               print("</td>\n");
+               $month=$row["month_last"];
+               }
+       mysql_free_result($result);
+       print($fin_split.$fin_year);
+}
+
+function title($year,$month)
+{
+       if (isset($year) && isset($month))
+               return(title_month( $year, $month));
+       else
+               return(title_icons(&$year,&$month));
+}
+
+function image_supported($mime)
+{
+       global $HTTP_SERVER_VARS;
+
+       if (!isset($HTTP_SERVER_VARS["HTTP_ACCEPT"]))
+               return(false);
+       $exp=explode(",",$HTTP_SERVER_VARS["HTTP_ACCEPT"]);
+       while (($s=array_shift($exp))) {
+               $s=trim(ereg_replace(";.*","",$s));
+               if ($s==$mime)
+                       return(true);
+               }
+       return(false);
+}
+
+// PHP dirname() is broken: ("/1/2/3"=>"/1/2", "/1"->"/")
+function dirnameslashed($filename)
+{
+       $r=dirname($filename);
+       if (substr($r,-1)!="/")
+               $r.="/";
+       return($r);
+}
+
+function usersize($size)
+{
+       $suffix_a=array("","k","M","G","T");
+       while ($size>=1000 && sizeof($suffix_a)>=2) {
+               $size/=1000;
+               array_shift($suffix_a);
+               }
+       return(round($size)." ".$suffix_a[0]."B");
+}
+
+function data_href($filename,$text,$details="")
+{
+       return("<a href=\"$filename\">$text (".usersize(filesize($filename))."$details)</a>");
+}
+
+function img_href($filename,$text)
+{
+       list($width,$height)=getimagesize($filename);
+       return(data_href($filename,$text,", ${width}x${height} bodù"));
+}
+
+function footer_img($url,$text,$size)
+{
+       global $footer_LOCAL;
+       if (!isset($footer_LOCAL))
+               return("<img src=\"$url\" $size alt=\"$text\" />");
+       else
+               return($text);
+}
+
+function footer($delimit=true)
 {
        // deadlock prevention:
        global $footer_passed;
@@ -129,46 +372,89 @@ function footer()
        $footer_passed=true;
 
        global $cvs_id_html,$viewcvs,$viewcvs,$HTTP_SERVER_VARS;
+       if ($delimit)
+               print("<p>&nbsp;</p>\n");
+       $uri="uri=".addpercents("http://".$HTTP_SERVER_VARS["HTTP_HOST"].$HTTP_SERVER_VARS["REQUEST_URI"]);
        ?>
 <hr />
 <table border="0" width="100%">
 <tr><td align="left"><span class="cvs-id"><?php print($cvs_id_html); ?></span></td><td align="right"><a
-       href="http://validator.w3.org/check/referer"><img src="http://www.w3.org/Icons/valid-xhtml10"
-               <?php print(img_size(88,31)); ?> alt="Valid XHTML 1.0!" /></a><a
-       href="http://jigsaw.w3.org/css-validator/validator?warning=2&amp;profile=css2&amp;uri=<?php
-               print(addpercents("http://".$HTTP_SERVER_VARS["HTTP_HOST"].$HTTP_SERVER_VARS["REQUEST_URI"]));
-               ?>"><img src="http://jigsaw.w3.org/css-validator/images/vcss"
-               <?php print(img_size(88,31)); ?> alt="Valid CSS!" /></a></td></tr>
+       href="http://validator.w3.org/check?<?php
+               print($uri); ?>"><?php
+                               print footer_img("http://www.w3.org/Icons/valid-xhtml11","Valid XHTML 1.1!",img_size(88,31));
+                               ?></a><a href="http://jigsaw.w3.org/css-validator/validator?warning=2&amp;profile=css2&amp;<?php
+               print($uri); ?>"><?php
+                               print footer_img("http://jigsaw.w3.org/css-validator/images/vcss","Valid CSS!",img_size(88,31));
+                               ?></a></td></tr>
 </table>
 </body></html>
-       <?php
+<?php
        exit();
 }
 
-function heading()
+// Stolen from: php-manual.html#function.header
+function no_cache()
 {
-       global $msie_major,$title_tail,$head_css,$head;
+       header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");          // Date in the past
+       header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); // always modified
+       header("Cache-Control: no-cache, must-revalidate");        // HTTP/1.1
+       header("Pragma: no-cache");                                // HTTP/1.0
+}
+
+function heading($title=false)
+{
+       global $HTTP_SERVER_VARS,$msie_major,$mozilla_major,$title_tail,$head_css,$have_css,$head,$heading_done,$title_prefix,$force_charset;
+
+       if (isset($heading_done))
+               return;
+       $heading_done=1;
 
-       header("Content-type: text/html; charset=iso-8859-2");
-       if (!isset($msie_major) || $msie_major>=4)
-               print('<?xml version="1.0" encoding="iso-8859-2"?>'."\n");
+       if (isset($force_charset))
+               $client_charset=$force_charset;
+       else
+               $client_charset=(!isset($HTTP_SERVER_VARS["CLIENT_CHARSET"]) ? "iso-8859-2" : $HTTP_SERVER_VARS["CLIENT_CHARSET"]);
+       // When "CLIENT_CHARSET" is set we MUST NOT explicitely specify our "charset"
+       header("Content-type: text/html".(isset($HTTP_SERVER_VARS["CLIENT_CHARSET"]) ? "" : "; charset=$client_charset"));
+       header("Content-Style-Type: text/css");
+       if (!isset($msie_major) || $msie_major>=4) {
+               print('<?xml version="1.0" encoding="'.$client_charset.'"?>'."\n");
+               }
 ?>
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
-<head><title>Energie &amp; peníze<?php
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs">
+<head><?php
+       print("<title>$title_prefix");
        if (isset($title_tail))
-               print(": $title_tail");
-?></title>
-<style type="text/css"><!--
-.cvs-id { font-family: monospace; }
-.error  { color: red; }
-.quote  { font-family: monospace; }
+               print(": ".htmlspecialchars($title_tail));
+       print("</title>\n");
+       if ($have_css) {
+?><style type="text/css"><!--
+.cvs-id   { font-family: monospace; }
+.error    { color: red;   background-color: transparent; }
+.quote    { font-family: monospace; }
+.nowrap   { white-space: nowrap; }
+.centered { text-align: center; }
+body {
+               background-color: black;
+               color: white;
+               }
+:link    { color: aqua;   background-color: transparent; }
+:visited { color: teal;   background-color: transparent; }
+h1,h2    { color: yellow; background-color: transparent; }
 <?php
-       if (isset($head_css))
-               print(trim($head_css)."\n");
-       print("--></style>\n");
+               if (isset($head_css))
+                       print(trim($head_css)."\n");
+               print("--></style>\n");
+               }
        if (isset($head))
                print($head);
-       print("</head><body>\n");
+       print("</head><body");
+       if (isset($mozilla_major) && $mozilla_major==4)
+               print(" bgcolor=\"black\" text=\"white\" link=\"aqua\" vlink=\"teal\"");
+       print(">\n");
+       if ($title)
+               print("<h1 class=\"centered\">"
+                               .img("img/eap-title.".(image_supported("image/png") ? "png" : "gif"),"Energie & Peníze")
+                               ."</h1>\n");
 }
 ?>