not entirely sure, think its all the messaging components
[glcas.git] / libglcas / web.php
1 <?php 
2
3 // if i believed in name spacing in php, i'd use it.
4 error_log("glcasweb loaded");
5
6 global $CRON_CLASSES;
7 $CRON_CLASSES["GLCASWeb"] = "GLCASWeb";
8
9
10 class GLCASWeb {
11         
12         function __construct($config)
13         {
14                 $this->config = $config;
15         }
16         
17         function go($urlhandlers)
18         {
19                 $url = "/";
20                 if(isset($_REQUEST["q"])) {
21                         $url = $_REQUEST["q"];
22                 }
23                 
24                 // add a stat
25                 $ipaddr = $_SERVER["REMOTE_ADDR"];
26                 $this->config->delData("lastseen", "$ipaddr");
27                 $this->config->addData("lastseen", "$ipaddr", time(), "");
28                 
29                 // create a url parser
30                 $urlparser = new GLCASUrlParser($urlhandlers, $this->config);
31                 
32                 // 
33                 $call_class = $urlparser->getClass($url);
34                 error_log("urldecode: ".urldecode($url));
35                 $call_class->go($url);
36         }
37         
38         function cron()
39         {
40                 echo "WEB base running cron for ipseen roll-up<br>";
41                 $lastseen = $this->config->getData("lastseen");
42                 foreach($lastseen as $key=>$val) {
43                         $myip = $val["category"];
44                         $myls = $val["name"];
45                         $ip[$myip] = $myls;
46                 }
47                 
48                 foreach($ip as $key=>$val) {
49                         echo "Setting last seen for $key to $val<br>";
50                         $this->config->delData("lastseen", $key);
51                         $this->config->addData("lastseen", $key, $val);
52                 }
53         }
54         
55         private $config;
56         
57 }
58
59 function GLCASMenuBuilder()
60 {
61         global $BASE_URL, $MENU_ITEMS;
62         
63         ksort($MENU_ITEMS);
64         ?>
65 <table border="1"><tr><th>Menu</th><td>
66 <a href="<?php echo $BASE_URL ?>">Home</a></td>
67         <?php
68         foreach($MENU_ITEMS as $key => $val) {
69                 $link = $val["link"];
70                 $name = $val["name"];
71                 echo "<td><a href=$link>$name</a></td>";
72         }
73         
74         echo "</table>";
75 }
76
77 // just a short one to call the long one
78 function gcam($m, $s)
79 {
80         global $glconfig;
81         GLCASAddMessage($m, $s);
82 }
83
84 function GLCASAddMessage($messagelev, $message)
85 {
86         global $glconfig;
87         
88         $glconfig->addData("messages", time(), "$messagelev", $message);
89 }
90
91 function GLCASMessageBuilder()
92 {
93         global $glconfig;
94         $nm = 0;
95         $nml = null;
96         $msgs_v = $glconfig->getData("messages");
97         
98         // create a form for setting messages
99         global $BASE_URL;
100         echo "<form method=\"post\" action=\"$BASE_URL/?glcasbasesendmsg\">";
101         echo "Send a message";
102         echo "<input type=\"text\" name=\"message\">";
103         echo "<select name=\"msglev\"><option value=\"0\">Info</option><option value=\"1\">Warning</option>";
104         echo "<option value=\"2\">Error</option></select>";
105         echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
106         echo "</form>";
107         
108         if($msgs_v !== false) foreach($msgs_v as $msgs) {
109                 
110                 error_log("messages: ".$msgs["category"]. " - ".$msgs["val"]);
111                 
112                 $nml[$nm]["msg"] = glcas_tdiffToAgo(time()-$msgs["category"])." : ".$msgs["val"];
113                 $fontst = "";
114                 $fonten = "";
115                 switch($msgs["name"]) {
116                         case "0":
117                                 $fontst = "<font color=\"green\">";
118                                 $fonten = "</font>"; 
119                                 break;
120                         
121                         case "1":
122                                 $fontst = "<font color=\"yellow\">";
123                                 $fonten = "</font>"; 
124                                 break;
125                         case "2":
126                                 $fontst = "<font color=\"red\">";
127                                 $fonten = "</font>"; 
128                                 break;
129                                 
130                 }
131                 $nml[$nm]["font_start"] = $fontst;
132                 $nml[$nm]["font_end"] = $fonten;
133                 
134                 $nm++;
135         }
136         if($nm > 0 ) {
137                 echo "<hr>";
138                 echo "<table>";
139                 // we do it backwards
140                 for($i = ($nm-1); $i >= 0; $i--) {
141                         echo "<tr><td>".$nml[$i]["font_start"]."".$nml[$i]["msg"]."".$nml[$i]["font_end"]."</td></tr>";
142                 }
143                 echo "</table>";
144                 echo "<hr>";
145         } else echo "No Messages<br>";
146 }
147
148 function GLCASpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="GLCAS")
149 {
150         global $WEB_ROOT_FS, $BASE_URL;
151         
152         // TODO: load css
153         // header
154         echo "<html><head><title>$title</title>";
155         
156         // load css
157         if(file_exists("$WEB_ROOT_FS/css")) {
158                 $dh = opendir("$WEB_ROOT_FS/css");
159                 if($dh) {
160                         while(($file = readdir($dh))!==false) {
161                                 $mt = preg_match("/.*.css$/", $file);
162                                 if($mt > 0) {
163                                         error_log("loading css $file");
164                                         echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$BASE_URL/css/$file\">";
165                                         //echo "required $basedir/$file\n";
166                                 }
167                         }
168                 }               
169         }
170
171         // load js
172         if(file_exists("$WEB_ROOT_FS/js")) {
173                 $dh = opendir("$WEB_ROOT_FS/js");
174                 if($dh) {
175                         while(($file = readdir($dh))!==false) {
176                                 $mt = preg_match("/.*.js$/", $file);
177                                 if($mt > 0) {
178                                         error_log("loading js $file");
179                                         echo "<script type=\"text/javascript\" src=\"$BASE_URL/js/$file\"></script>";
180                                         //echo "required $basedir/$file\n";
181                                 }
182                         }
183                 }               
184         }
185         
186         
187         // start body
188         echo "</head><body>";
189         
190         // page top
191         echo "<h1>GLCAS</h1><br>";
192         echo "<table><tr><td>";
193         GLCASMessageBuilder();
194         echo "<td></tr><tr><td>";
195         
196         // menu, then body
197         echo "<table><tr><td>";
198         GLCASMenuBuilder();
199         echo "</td></tr><tr><td>";
200         // body
201         $url = "/";
202         if(isset($_REQUEST["q"])) {
203                 $url = $_REQUEST["q"];
204         }
205         
206         if($bodyClass != null) {
207                 $bodyClass->$bodyFunction($url);
208         } else if( $bodyFunction != null) {
209                 $bodyFunction($url);
210         } else echo $bodycontent;
211         echo "</td></tr></table>";
212         
213         
214         // close the big wrap-around table
215         echo "</td></tr></table>";
216         
217         // footer
218         echo "<br><font size=\"-1\">Copyright 2011, PJR</font><br></body></html>";
219         
220 }
221
222 function glcas_getLastSeen($ip, $data)
223 {
224         $last = "never";
225         
226         if(!$data) return $last;
227         
228         
229         
230         foreach($data as $key=>$val) {
231                 $last = $val["name"];
232                 error_log("got $last for $ip");
233         }
234         
235         if($last == "never") return $last;
236         
237         $last = glcas_tdiffToAgo(time()-$last);
238         
239         error_log("last for $ip set to $last");
240         
241         return $last;
242         
243 }
244
245 function glcas_tdiffToAgo($time_in_sec)
246 {
247         $tdiff = $time_in_sec;
248         $tdiff_min = (int)($tdiff/60);
249         $tdiff_hour = (int)($tdiff/3600);
250         $tdiff_days = (int)($tdiff/86400);
251         
252         if($tdiff < 60) $last = "Less then a minute";
253         if($tdiff >= 60 && $tdiff < 7200) $last = "$tdiff_min minutes ago";
254         if($tdiff >= 7200 && $tdiff < 86400) $last = "$tdiff_hour hours ago";
255         if($tdiff >= 86400) $last = "$tdiff_days days ago";
256         
257         return $last;
258 }
259
260 function glcas_isRemoteDir($url)
261 {
262         file_get_contents($url);
263         //error_log("did file_get_contents on $url");
264         foreach($http_response_header as $key => $val) {
265                 //error_log("got header of $key for $val");
266                 if(preg_match("/.*Location:.*/", $val)) {
267                         //error_log("in details apt got location as $val from $url");
268                         $realloc = preg_replace("/.*: /", "", $val);
269                         $urlwithslash = $url."/";
270                         //echo "<br>in comp with<br>$urlwithslash<br>$realloc<br>";
271                         if(strcasecmp($realloc,$urlwithslash)==0) {
272                                 //error_log("I believe $realloc is a directory redir for $url");
273                                 return true;
274                         } else {
275                                 //error_log("I dont believe $realloc is a directory redir for $url");
276                         }
277                 }
278         }
279         return false;
280 }
281
282 function glcas_fileExists($url)
283 {
284         $fp = fopen($url, "r");
285         //error_og("did file_get_contents on $url");
286         $retval = false;
287         foreach($http_response_header as $key => $val) {
288                 //error_log("got header of $key for $val");
289                 if(preg_match("/.*HTTP\/.*200.*/", $val)) {
290                         //echo "is true\n";
291                         
292                         $retval = true;
293                 }
294         }
295         fclose($fp);
296         return $retval;
297 }
298
299 function glcas_startInstaller()
300 {
301         global $WEB_ROOT_FS, $BASE_URL;
302         
303         $uid = posix_geteuid();
304         $gid = posix_getegid();
305         $uid_a = posix_getpwuid($uid);
306         $uid = $uid_a["name"];
307         
308         $gid_a = posix_getgrgid($gid);
309         $gid = $gid_a["name"];
310         error_log("user id is $uid, group id is $gid");
311         
312         if(isset($_REQUEST["installdir"])) {
313                 $c = new GLCASConfig();
314                 touch($_REQUEST["installdir"]."/webconfig");
315                 $c->loadConfig($_REQUEST["installdir"]."/webconfig");
316                 header("Location: index.php");
317                 return;
318         }
319         
320         $underroot = realpath($WEB_ROOT_FS."/../");
321         
322 ?>
323 <html>
324 <h1>Welcome to GLCAS</h1>
325 Welcome to GLCAS, I cant find my configuration file so im assuming you installing me for the first time<br>
326 If this is not correct then we have a big problem that needs to be solved, i hope you have a backup of the old
327 config, cause that will make life easier.<br><br>
328 However, if this is the first time you have run this app, then all is good with the world. All I need right
329 now is a place where i can store my config. I search the following directories for the location of my 
330 configuration (webconfig)<br>
331 <li> /var/run/glcas/
332 <li> /var/lib/glcas/
333 <li> <?php echo $underroot?>/var/glcas/
334 <br><br>
335
336 As root, you must now create one of these directories and change the ownership of the directory to the web owner.
337 (chown <?php echo $uid?>:<?php echo $gid ?> the_path_you_choose).<br>
338
339 Now, tell me where you want me to create the webconfig file:<br>
340 <form method="post">
341 <select name="installdir">
342 <option value="/var/run/glcas">/var/run/glcas</option>
343 <option value="/var/lib/glcas">/var/lib/glcas</option>
344 <option value="<?php echo $underroot?>/var/glcas/"><?php echo $underroot?>/var/glcas/</option>
345 </select>
346 <input type="submit" name="Go" value="Go">
347
348 </select>
349 </form>
350
351 </html>
352 <?php 
353 }
354
355
356 ?>