config.php introduced
authorshort <>
Thu, 16 Aug 2001 01:39:37 +0000 (01:39 +0000)
committershort <>
Thu, 16 Aug 2001 01:39:37 +0000 (01:39 +0000)
prepvar() implemented for {GET,POST} input and validation
db_* implemented, currently just for content tables
headers of common.php must be now called by heading()
img_size() implemented for simpler XHTML image size declarations

common.php
config.php [new file with mode: 0644]
index.php
objednavka.php
obsah.php [new file with mode: 0644]
obsah_init.pl [new file with mode: 0755]

index fac6f37..94e1a70 100644 (file)
@@ -2,8 +2,9 @@
 
        error_reporting(E_ALL);
 
-       $admin_mail="short@ucw.cz";
-       $viewcvs=ereg_replace("^/~short/","\\0cgi-bin/viewcvs/",$HTTP_SERVER_VARS["SCRIPT_NAME"]);
+       include("config.php");
+
+       // $viewcvs prepared by "config.php"
        if ($viewcvs==$HTTP_SERVER_VARS["SCRIPT_NAME"])
                unset($viewcvs);
        $cvs_id_split=split(" ",$cvs_id);
@@ -41,6 +42,84 @@ function fatal($msg="UNKNOWN")
        footer();
 }
 
+function prepvar($name,$regex=".",$require=true)
+{
+       global $HTTP_GET_VARS,$HTTP_POST_VARS;
+
+            if (isset($HTTP_GET_VARS[$name]))
+               $v=$HTTP_GET_VARS[$name];
+       else if (isset($HTTP_POST_VARS[$name]))
+               $v=$HTTP_POST_VARS[$name];
+       else
+               unset($v);
+
+       $name_html="Parametr <span class=\"quote\">".htmlspecialchars($name)."</span>";
+
+       $regex="^$regex\$";
+       if (isset($v) && !ereg($regex,$v))
+               fatal("$name_html nevyhovuje po¾adovanému regexu <span class=\"quote\">".htmlspecialchars($regex)."</span>");
+       if (!isset($v) && $require)
+               fatal("$name_html je vy¾adován");
+
+       if (!isset($v))
+               return(0);
+
+       global $$name;
+       $$name=$v;
+       return($v);
+}
+
+function db_connect()
+{
+       global $db_host,$db_user,$db_pwd,$db_name;
+       global $db_link;
+
+       if (isset($db_link))
+               return;
+       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());
+}
+
+function db_query($query)
+{
+       global $db_link;
+
+       db_connect();
+       if (!($r=mysql_query($query,$db_link)))
+               fatal("MySQL query \"$query\": ".mysql_error());
+       return($r);
+}
+
+function db_row($query)
+{
+       $q=db_query($query);
+       $r=mysql_fetch_row($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)
+{
+       global $tb_obsah;
+
+       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 img_size($width,$height)
+{
+       return("style=\"border:0;width:${width}px;height:${height}px\" width=\"$width\" height=\"$height\"");
+}
+
 function footer()
 {
        // deadlock prevention:
@@ -55,17 +134,21 @@ function footer()
 <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"
-               style="border:0;width:88px;height:31px" width="88" height="31" alt="Valid XHTML 1.0!" /></a><a
+               <?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"
-               style="border:0;width:88px;height:31px" width="88" height="31" alt="Valid CSS!" /></a></td></tr>
+               <?php print(img_size(88,31)); ?> alt="Valid CSS!" /></a></td></tr>
 </table>
 </body></html>
        <?php
        exit();
 }
 
+function heading()
+{
+       global $msie_major,$title_tail,$head_css,$head;
+
        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");
@@ -78,11 +161,14 @@ function footer()
 ?></title>
 <style type="text/css"><!--
 .cvs-id { font-family: monospace; }
-.error  { color: red; }<?php
+.error  { color: red; }
+.quote  { font-family: monospace; }
+<?php
        if (isset($head_css))
-               print($head_css);
+               print(trim($head_css)."\n");
        print("--></style>\n");
        if (isset($head))
                print($head);
+       print("</head><body>\n");
+}
 ?>
-</head><body>
diff --git a/config.php b/config.php
new file mode 100644 (file)
index 0000000..5931f17
--- /dev/null
@@ -0,0 +1,17 @@
+<?php // $Id$
+
+       $admin_mail="short@ucw.cz";
+       $mail_to="short@ucw.cz,karel.kratochvil@centrum.cz";
+       $viewcvs=ereg_replace("^/~short/","\\0cgi-bin/viewcvs/",$HTTP_SERVER_VARS["SCRIPT_NAME"]);
+
+       // obsah.php:
+       $db_host="";
+       $db_user="short";
+       $db_pwd_a=file("/home/short/priv/mysql.${db_user}.pwd");
+       $db_pwd=trim($db_pwd_a[0]);
+       $db_name="short";
+       $tb_obsah="energie_obsah";
+       $sep_obsah_contents=chr(1);
+       $page_obsah_contents=chr(3);
+
+?>
index 43507a5..663b19a 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1,54 +1,73 @@
 <?php $cvs_id='$Id$';
 
+       $detect_js=true;
+       $head='
+<script type="text/javascript" src="objednavka.js"></script>
+';
+       include("common.php");
+
        $head_css="
 .tab-bold  { font-weight: bold; }
 .post-type { font-family: monospace; }
 .nowrap    { white-space: nowrap; }
 .centered  { text-align: center; }
 ";
-       $head='
-<script type="text/javascript" src="objednavka.js"></script>
-';
-       $detect_js=true;
-
-       include("common.php");
+       heading();
 ?>
-<h1 class="centered"><img src="img/eap-title.png" alt="Teplo &amp; Peníze" /></h1>
+<h1 class="centered"><img src="img/eap-title.png" <?php print(img_size(522,60)); ?> alt="Teplo &amp; Peníze" /></h1>
 <p>&nbsp;</p>
 <?php
 
-function title_table_month($year,$month)
+function title_table_month($year,$month,$month_last)
 {
-       $size_width_s= 98; $size_height_s=138;
-       $size_width  =486; $size_height  =688;
-
-       $base=sprintf("img/eap-%04d-%02d",$year,$month+1);
-       $name=sprintf(        "%04d/%02d",$year,$month+1);
+       $month_full=month_full($year,$month,$month_last);
+       $base="img/eap-$year-$month_full";
+       $name=        "$year/$month_full";
        print("<a href=\"${base}.jpg\">"
-                       ."<img src=\"${base}s.jpg\" width=\"$size_width_s\" height=\"$size_height_s\""
-                       ." style=\"border:0;width:${size_width_s}px;height:${size_height_s}px\" alt=\"titulní stránka $name\" /><br />\n"
-                       ."$name"
-                       ."</a>");
+                       ."<img src=\"${base}s.jpg\" ".img_size(98,138)." alt=\"titulní stránka $name\" /></a><br />\n"
+                       ."<a href=\"obsah.php?year=$year&month=$month\">obsah $name</a>"
+                       );
 }
 
-function title_table_year($year,$month_max=11)
-{
-       $split=6;
+db_connect();
+$result=db_query("select year,month,month_last from $tb_obsah order by year,month");
+$split=6;
+$year=0;
+$month=-$split;
+$fin_split="";
+$fin_year="";
+while ($row=mysql_fetch_array($result)) {
+       $row["month"     ]--;
+       $row["month_last"]--;
 
-       print("<table border=\"0\" width=\"100%\"><tr><td align=\"center\"><table border=\"1\" cellpadding=\"5\">\n");
-       for ($month=0;$month<=$month_max;$month++) {
-               if (($month%$split)==0)
-                       print("<tr>");
-               print("<td align=\"center\">");
-               title_table_month($year,$month);
-               print("</td>");
-               if (($month%$split)==$split-1 || $month==$month_max)
-                       print("</tr>\n");
-               }
-       print("</table></td></tr></table>");
-}
+       if ($row["year"]!=$year) {
+               print($fin_split.$fin_year);
 
-title_table_year(2001,5);
+               $year=$row["year"];
+               print("<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>)</td></tr>\n"
+                               );
+               $fin_year="</table></td></tr></table>\n";
+               $month=-$split;
+               }
+       if ($month && floor($row["month"]/$split)!=floor($month/$split)) {
+               print($fin_split."<tr>");
+               $fin_split="</tr>";
+               $month=$row["month"]-($row["month"]%$split)-1;
+               }
+       while ($month+1<$row["month"]) {
+               print("<td></td>");
+               $month++;
+               }
+       print("<td align=\"center\""
+                       .($row["month_last"]!=$row["month"] ? " colspan=\"".($row["month_last"]+1-$row["month"])."\"" : "")
+                       .">");
+       title_table_month($year,$row["month"]+1,$row["month_last"]+1);
+       print("</td>");
+       $month=$row["month_last"];
+       }
+mysql_free_result($result);
+print($fin_split.$fin_year);
 ?>
 
 <p>&nbsp;</p>
index cb9ff6b..1245326 100644 (file)
@@ -1,13 +1,13 @@
 <?php $cvs_id='$Id$';
 
-       $mail_to="short@ucw.cz,karel.kratochvil@centrum.cz";
+       include("common.php");
 
        $head_css="
 .red    { color: red; }
 .italic { font-style: italic; }
 .quote  { font-family: monospace; }
 ";
-       include("common.php");
+       heading();
 
 $out="stamp:\t".date("r")."\n";
 $err=false;
diff --git a/obsah.php b/obsah.php
new file mode 100644 (file)
index 0000000..4addde7
--- /dev/null
+++ b/obsah.php
@@ -0,0 +1,48 @@
+<?php $cvs_id='$Id$';
+
+       include("common.php");
+
+       /* assign it manually to force it to value "0" if it would be unset */
+       $year =prepvar("year" ,"[0-9]+",false/*required*/);
+       $month=prepvar("month","[0-9]+",false/*required*/);
+
+       if ($month && !$year)
+               $month=0;
+
+            if (!$year)
+               $title_tail="Obsahy v¹ech èísel";
+       else if (!$month)
+               $title_tail="Obsahy èísel roku $year";
+       else
+               $title_tail="Obsah èísla $year/".month_full($year,$month);
+
+       $head_css="
+.page { font-style: italic; }
+";
+       heading();
+       print("<h1>$title_tail</h1>\n");
+
+       db_connect();
+       $result=db_query("select year,month,month_last,contents from $tb_obsah"
+                       .($year  ? " where year='$year'"   : "")
+                       .($month ?   " and month='$month'" : "")
+                       ." order by year,month"
+                       );
+       if (!mysql_num_rows($result))
+               fatal("Obsah po¾adovan".($month ? "ého èísla" : "ých èísel")." bohu¾el není ulo¾en");
+       while ($row=mysql_fetch_array($result)) {
+               if (!$month)
+                       print("<h2>Èíslo ".$row["year"]."/".month_full($row["year"],$row["month"],$row["month_last"])."</h2>\n");
+               $contents=$sep_obsah_contents.$row["contents"];
+               $contents=ereg_replace("http://[^[:space:]$page_obsah_contents$sep_obsah_contents]+",
+                               "<a href=\"\\0\">\\0</a>",$contents);
+               $contents=ereg_replace("$page_obsah_contents([^$sep_obsah_contents]+)",
+                               "<br /><div class=\"page\">strana ... \\1</div>",$contents);
+               $contents=ereg_replace("\n","<br />\n\t\t",$contents);
+               $contents=ereg_replace("$sep_obsah_contents([^$sep_obsah_contents]*)","\t<li>\\1</li>\n",$contents);
+               print("<ul>\n$contents</ul>\n");
+               }
+       mysql_free_result($result);
+
+       footer();
+?>
diff --git a/obsah_init.pl b/obsah_init.pl
new file mode 100755 (executable)
index 0000000..a5fef69
--- /dev/null
@@ -0,0 +1,89 @@
+#! /usr/bin/perl
+
+use strict;
+use DBI;
+
+use vars qw/$db_driver $db_host $db_user $db_pwd $DB_PWD $db_name $db/;
+use vars qw/$tb_obsah/;
+
+$db_driver="mysql";
+$db_host="";
+$db_user="short";
+$DB_PWD=$ENV{"HOME"}."/priv/mysql.${db_user}.pwd";
+$db_name="short";
+$tb_obsah="energie_obsah";
+
+open DB_PWD or die "Failed open \"$DB_PWD\": $!";
+$db_pwd=<DB_PWD>;
+chomp $db_pwd;
+close DB_PWD;
+
+$db=DBI->connect("DBI:$db_driver:database=$db_name;host=$db_host",$db_user,$db_pwd) or die "Database open fail: $!";
+
+sub db_do
+{
+my( $cmd )=@_;
+
+       $db->do($cmd) or die("SQL command \"$cmd\" failed: $!");
+}
+
+eval { &db_do("drop table $tb_obsah") };
+
+&db_do("create table $tb_obsah ("
+               ."year year(4) not null,"
+               ."month tinyint not null,"
+               ."month_last tinyint not null,"
+               ."contents text not null"
+               .")");
+
+&db_do("alter table $tb_obsah add unique (year,month)");
+
+
+use vars qw/$insert_tb_obsah $year $month $month_last $contents/;
+
+sub where
+{
+       return " in file $ARGV on line $.";
+}
+
+sub flush_month
+{
+       $_=$contents;
+       return if !defined $_;
+       die "Empty contents".&where() if !$_;
+       tr/ \t\n/  \n/s;
+       s/([\001\n]) | ([\002\n])/$1$2/g;
+       s/\nstrana \.\.\. ([\d ,]+\002)/\003$1/g;
+       die "Page marker not found somewhere in this month".&where() if (/\001[^\003]*\002/);
+       tr/\002//d;
+       $insert_tb_obsah->execute($year,$month,$month_last,substr($_,1)) or die "SQL insert failure: $!";
+       undef $year,$month;
+       undef $contents;
+}
+
+$insert_tb_obsah=$db->prepare("insert into $tb_obsah (year,month,month_last,contents) values (?,?,?,?)")
+               or die "Prepare fail: $!";
+
+while (<>) {
+       chomp;
+       if (m#^EaP (\d+)(-(\d+))?/(\d+)$#) {
+               &flush_month();
+               $month=$1;
+               $month_last=($3 ? $3 : $1);
+               $year =$4;
+               $contents="";
+               next;
+               }
+       if (/^\.\s(.*)$/) {
+               $contents.="\001$1\002";
+               next;
+               }
+       if (/^\s(.*)$/) {
+               $contents=substr($contents,0,-1)."\n$1\002";
+               next;
+               }
+       die "Unexpected text".&where().": $_" if (/\S/);
+       }
+&flush_month();
+
+print("success.\n");