From 2fcd486ab1a2b13e09e95d1f0d485cebec37499e Mon Sep 17 00:00:00 2001 From: short <> Date: Thu, 16 Aug 2001 01:39:37 +0000 Subject: [PATCH] config.php introduced 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 | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- config.php | 17 ++++++++++ index.php | 85 +++++++++++++++++++++++++++++------------------- objednavka.php | 4 +-- obsah.php | 48 +++++++++++++++++++++++++++ obsah_init.pl | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 301 insertions(+), 42 deletions(-) create mode 100644 config.php create mode 100644 obsah.php create mode 100755 obsah_init.pl diff --git a/common.php b/common.php index fac6f37..94e1a70 100644 --- a/common.php +++ b/common.php @@ -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 ".htmlspecialchars($name).""; + + $regex="^$regex\$"; + if (isset($v) && !ereg($regex,$v)) + fatal("$name_html nevyhovuje po¾adovanému regexu ".htmlspecialchars($regex).""); + 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() + alt="Valid CSS!" />
Valid XHTML 1.0! alt="Valid XHTML 1.0!" />">Valid CSS!
=4) print(''."\n"); @@ -78,11 +161,14 @@ function footer() ?> \n"); if (isset($head)) print($head); + print("\n"); +} ?> - diff --git a/config.php b/config.php new file mode 100644 index 0000000..5931f17 --- /dev/null +++ b/config.php @@ -0,0 +1,17 @@ + diff --git a/index.php b/index.php index 43507a5..663b19a 100644 --- a/index.php +++ b/index.php @@ -1,54 +1,73 @@ +'; + include("common.php"); + $head_css=" .tab-bold { font-weight: bold; } .post-type { font-family: monospace; } .nowrap { white-space: nowrap; } .centered { text-align: center; } "; - $head=' - -'; - $detect_js=true; - - include("common.php"); + heading(); ?> -

Teplo & Peníze

+

alt="Teplo & Peníze" />

 

" - ."\"titulní
\n" - ."$name" - .""); + ."\"titulní
\n" + ."obsah $name" + ); } -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("
\n"); - for ($month=0;$month<=$month_max;$month++) { - if (($month%$split)==0) - print(""); - print(""); - if (($month%$split)==$split-1 || $month==$month_max) - print("\n"); - } - print("
"); - title_table_month($year,$month); - print("
"); -} + if ($row["year"]!=$year) { + print($fin_split.$fin_year); -title_table_year(2001,5); + $year=$row["year"]; + print("
\n" + ."\n" + ); + $fin_year="
Roèník $year (obsahy èísel)
\n"; + $month=-$split; + } + if ($month && floor($row["month"]/$split)!=floor($month/$split)) { + print($fin_split.""); + $fin_split=""; + $month=$row["month"]-($row["month"]%$split)-1; + } + while ($month+1<$row["month"]) { + print(""); + $month++; + } + print(""); + title_table_month($year,$row["month"]+1,$row["month_last"]+1); + print(""); + $month=$row["month_last"]; + } +mysql_free_result($result); +print($fin_split.$fin_year); ?>

 

diff --git a/objednavka.php b/objednavka.php index cb9ff6b..1245326 100644 --- a/objednavka.php +++ b/objednavka.php @@ -1,13 +1,13 @@ $title_tail\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("

Èíslo ".$row["year"]."/".month_full($row["year"],$row["month"],$row["month_last"])."

\n"); + $contents=$sep_obsah_contents.$row["contents"]; + $contents=ereg_replace("http://[^[:space:]$page_obsah_contents$sep_obsah_contents]+", + "\\0",$contents); + $contents=ereg_replace("$page_obsah_contents([^$sep_obsah_contents]+)", + "
strana ... \\1
",$contents); + $contents=ereg_replace("\n","
\n\t\t",$contents); + $contents=ereg_replace("$sep_obsah_contents([^$sep_obsah_contents]*)","\t
  • \\1
  • \n",$contents); + print("\n"); + } + mysql_free_result($result); + + footer(); +?> diff --git a/obsah_init.pl b/obsah_init.pl new file mode 100755 index 0000000..a5fef69 --- /dev/null +++ b/obsah_init.pl @@ -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=; +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"); -- 1.8.3.1