Direct payment method 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         global $$name;
71         $$name=$v;
72         return($v);
73 }
74
75 function db_connect()
76 {
77         global $db_host,$db_user,$db_pwd,$db_name;
78         global $db_link;
79
80         if (isset($db_link))
81                 return;
82         if (!($db_link=@mysql_connect($db_host,$db_user,$db_pwd)))
83                 fatal("MySQL connect: ".mysql_error());
84         if (!mysql_select_db($db_name,$db_link))
85                 fatal("MySQL database select: ".mysql_error());
86 }
87
88 function db_query($query)
89 {
90         global $db_link;
91
92         db_connect();
93         if (!($r=mysql_query($query,$db_link)))
94                 fatal("MySQL query \"$query\": ".mysql_error());
95         return($r);
96 }
97
98 function db_row($query)
99 {
100         $q=db_query($query);
101         $r=mysql_fetch_row($q);
102         mysql_free_result($q);
103         return($r);
104 }
105
106 function db_item($query)
107 {
108         $row=db_row($query);
109         return($row[0]);
110 }
111
112 function month_a($year,$month,$month_last=0)
113 {
114         global $tb_obsah;
115
116         if (!$month_last)
117                 $month_last=db_item("select month_last from $tb_obsah where year='$year' and month='$month'");
118         $month_full=sprintf("%02d".($month==$month_last ? "" : "-%02d"),$month,$month_last);
119
120         $r=array("year"=>$year,"month"=>$month,"month_last"=>$month_last,"month_full"=>$month_full,
121                         "name"=>"$year/${month_full}",
122                         "img" =>"img/eap-$year-${month_full}.jpg",
123                         "icon"=>"img/eap-$year-${month_full}s.jpg",
124                         );
125         return($r);
126 }
127
128 function img_size($width,$height=0)
129 {
130         if (!$height)
131                 list($width,$height)=getimagesize($width);
132         return("style=\"border:0;width:${width}px;height:${height}px\" width=\"$width\" height=\"$height\"");
133 }
134
135 function gsm_banking()
136 {
137         return("<img src=\"img/sluzby_bankovni_pggsm.gif\"".img_size(89,31)." alt=\"Paegas GSM banking\" class=\"img-align\" />");
138 }
139
140 function price_a()
141 {
142         if (!($f=fopen("objednavka.js","r")))
143                 fatal("Nepodaøilo se naèíst seznam dostupného zbo¾í");
144         $r=array();
145         while (($s=fgets($f,0x1000))) {
146                 if (!($s=trim($s)))
147                         break;
148                 if (!(ereg("^want_price\\[ *'([^']*)'\\]=([0-9]*);$",$s,$matched)))
149                         continue;
150                 $r[$matched[1]]=$matched[2];
151                 }
152         fclose($f);
153         return($r);
154 }
155
156 function footer()
157 {
158         // deadlock prevention:
159         global $footer_passed;
160         if (isset($footer_passed))
161                 exit();
162         $footer_passed=true;
163
164         global $cvs_id_html,$viewcvs,$viewcvs,$HTTP_SERVER_VARS;
165         ?>
166 <hr />
167 <table border="0" width="100%">
168 <tr><td align="left"><span class="cvs-id"><?php print($cvs_id_html); ?></span></td><td align="right"><a
169         href="http://validator.w3.org/check/referer"><img src="http://www.w3.org/Icons/valid-xhtml10"
170                 <?php print(img_size(88,31)); ?> alt="Valid XHTML 1.0!" /></a><a
171         href="http://jigsaw.w3.org/css-validator/validator?warning=2&amp;profile=css2&amp;uri=<?php
172                 print(addpercents("http://".$HTTP_SERVER_VARS["HTTP_HOST"].$HTTP_SERVER_VARS["REQUEST_URI"]));
173                 ?>"><img src="http://jigsaw.w3.org/css-validator/images/vcss"
174                 <?php print(img_size(88,31)); ?> alt="Valid CSS!" /></a></td></tr>
175 </table>
176 </body></html>
177         <?php
178         exit();
179 }
180
181 // Stolen from: php-manual.html#function.header
182 function no_cache()
183 {
184         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");          // Date in the past
185         header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); // always modified
186         header("Cache-Control: no-cache, must-revalidate");        // HTTP/1.1
187         header("Pragma: no-cache");                                // HTTP/1.0
188 }
189
190 function heading()
191 {
192         global $msie_major,$mozilla_major,$title_tail,$head_css,$head;
193
194         header("Content-type: text/html; charset=iso-8859-2");
195         if (!isset($msie_major) || $msie_major>=4)
196                 print('<?xml version="1.0" encoding="iso-8859-2"?>'."\n");
197 ?>
198 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
199 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
200 <head><title>Energie &amp; peníze<?php
201         if (isset($title_tail))
202                 print(": $title_tail");
203 ?></title>
204 <style type="text/css"><!--
205 .cvs-id  { font-family: monospace; }
206 .error   { color: red;    background-color: transparent; }
207 .quote   { font-family: monospace; }
208 body {
209                 background-color: black;
210                 color: white;
211                 }
212 :link    { color: aqua;   background-color: transparent; }
213 :visited { color: teal;   background-color: transparent; }
214 h1,h2    { color: yellow; background-color: transparent; }
215 <?php
216         if (isset($head_css))
217                 print(trim($head_css)."\n");
218         print("--></style>\n");
219         if (isset($head))
220                 print($head);
221         print("</head><body");
222         if (isset($mozilla_major) && $mozilla_major==4)
223                 print(" bgcolor=\"black\" text=\"white\" link=\"cyan\" vlink=\"teal\"");
224         print(">\n");
225 }
226 ?>