not sure what i did, but you can bet it was AWESOME
[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->addData("lastseen", "$ipaddr", time(), "");
27                 
28                 // create a url parser
29                 $urlparser = new GLCASUrlParser($urlhandlers, $this->config);
30                 
31                 // 
32                 $call_class = $urlparser->getClass($url);
33                 error_log("urldecode: ".urldecode($url));
34                 $call_class->go($url);
35         }
36         
37         function cron()
38         {
39                 echo "WEB base running cron for ipseen roll-up<br>";
40                 $lastseen = $this->config->getData("lastseen");
41                 foreach($lastseen as $key=>$val) {
42                         $myip = $val["category"];
43                         $myls = $val["name"];
44                         $ip[$myip] = $myls;
45                 }
46                 
47                 foreach($ip as $key=>$val) {
48                         echo "Setting last seen for $key to $val<br>";
49                         $this->config->delData("lastseen", $key);
50                         $this->config->addData("lastseen", $key, $val);
51                 }
52         }
53         
54         private $config;
55         
56 }
57
58 function GLCASMenuBuilder()
59 {
60         global $BASE_URL, $MENU_ITEMS;
61         
62         ksort($MENU_ITEMS);
63         ?>
64         <table border="1"><tr><th>Menu</th><td>
65 <a href="<?php echo $BASE_URL ?>">Home</a></td>
66         <?php
67         foreach($MENU_ITEMS as $key => $val) {
68                 $link = $val["link"];
69                 $name = $val["name"];
70                 echo "<td><a href=$link>$name</a></td>";
71         }
72         
73         echo "</table>";
74 }
75
76 function GLCASMessageBuilder()
77 {
78         echo "<i>Messages not implemented yet</i>";
79 }
80
81 function GLCASpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="GLCAS")
82 {
83         global $WEB_ROOT_FS, $BASE_URL;
84         
85         // TODO: load css
86         // header
87         echo "<html><head><title>$title</title>";
88         
89         // load css
90         if(file_exists("$WEB_ROOT_FS/css")) {
91                 $dh = opendir("$WEB_ROOT_FS/css");
92                 if($dh) {
93                         while(($file = readdir($dh))!==false) {
94                                 $mt = preg_match("/.*.css$/", $file);
95                                 if($mt > 0) {
96                                         error_log("loading css $file");
97                                         echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$BASE_URL/css/$file\">";
98                                         //echo "required $basedir/$file\n";
99                                 }
100                         }
101                 }               
102         }
103
104         // load js
105         if(file_exists("$WEB_ROOT_FS/js")) {
106                 $dh = opendir("$WEB_ROOT_FS/js");
107                 if($dh) {
108                         while(($file = readdir($dh))!==false) {
109                                 $mt = preg_match("/.*.js$/", $file);
110                                 if($mt > 0) {
111                                         error_log("loading js $file");
112                                         echo "<script type=\"text/javascript\" src=\"$BASE_URL/js/$file\"></script>";
113                                         //echo "required $basedir/$file\n";
114                                 }
115                         }
116                 }               
117         }
118         
119         
120         // start body
121         echo "</head><body>";
122         
123         // page top
124         echo "<h1>GLCAS</h1><br>";
125         echo "<table><tr><td>";
126         GLCASMessageBuilder();
127         echo "<td></tr><tr><td>";
128         
129         // menu, then body
130         echo "<table><tr><td>";
131         GLCASMenuBuilder();
132         echo "</td></tr><tr><td>";
133         // body
134         $url = "/";
135         if(isset($_REQUEST["q"])) {
136                 $url = $_REQUEST["q"];
137         }
138         
139         if($bodyClass != null) {
140                 $bodyClass->$bodyFunction($url);
141         } else if( $bodyFunction != null) {
142                 $bodyFunction($url);
143         } else echo $bodycontent;
144         echo "</td></tr></table>";
145         
146         
147         // close the big wrap-around table
148         echo "</td></tr></table>";
149         
150         // footer
151         echo "<br><font size=\"-1\">Copyright 2011, PJR</font><br></body></html>";
152         
153 }
154
155 function glcas_getLastSeen($ip, $data)
156 {
157         $last = "never";
158         
159         if(!$data) return $last;
160         
161         
162         
163         foreach($data as $key=>$val) {
164                 $last = $val["name"];
165                 error_log("got $last for $ip");
166         }
167         
168         if($last == "never") return $last;
169         
170         $last = glcas_tdiffToAgo(time()-$last);
171         
172         error_log("last for $ip set to $last");
173         
174         return $last;
175         
176 }
177
178 function glcas_tdiffToAgo($time_in_sec)
179 {
180         $tdiff = $time_in_sec;
181         $tdiff_min = (int)($tdiff/60);
182         $tdiff_hour = (int)($tdiff/3600);
183         $tdiff_days = (int)($tdiff/86400);
184         
185         if($tdiff < 60) $last = "Less then a minute";
186         if($tdiff >= 60 && $tdiff < 7200) $last = "$tdiff_min minutes ago";
187         if($tdiff >= 7200 && $tdiff < 86400) $last = "$tdiff_hour hours ago";
188         if($tdiff >= 86400) $last = "$tdiff_days days ago";
189         
190         return $last;
191 }
192
193 function glcas_isRemoteDir($url)
194 {
195         file_get_contents($url);
196         //error_log("did file_get_contents on $url");
197         foreach($http_response_header as $key => $val) {
198                 //error_log("got header of $key for $val");
199                 if(preg_match("/.*Location:.*/", $val)) {
200                         //error_log("in details apt got location as $val from $url");
201                         $realloc = preg_replace("/.*: /", "", $val);
202                         $urlwithslash = $url."/";
203                         //echo "<br>in comp with<br>$urlwithslash<br>$realloc<br>";
204                         if(strcasecmp($realloc,$urlwithslash)==0) {
205                                 //error_log("I believe $realloc is a directory redir for $url");
206                                 return true;
207                         } else {
208                                 //error_log("I dont believe $realloc is a directory redir for $url");
209                         }
210                 }
211         }
212         return false;
213 }
214
215 function glcas_fileExists($url)
216 {
217         $fp = fopen($url, "r");
218         //error_og("did file_get_contents on $url");
219         $retval = false;
220         foreach($http_response_header as $key => $val) {
221                 //error_log("got header of $key for $val");
222                 if(preg_match("/.*HTTP\/.*200.*/", $val)) {
223                         //echo "is true\n";
224                         
225                         $retval = true;
226                 }
227         }
228         fclose($fp);
229         return $retval;
230 }
231
232 function glcas_startInstaller()
233 {
234         global $WEB_ROOT_FS, $BASE_URL;
235         
236         $uid = posix_geteuid();
237         $gid = posix_getegid();
238         $uid_a = posix_getpwuid($uid);
239         $uid = $uid_a["name"];
240         
241         $gid_a = posix_getgrgid($gid);
242         $gid = $gid_a["name"];
243         error_log("user id is $uid, group id is $gid");
244         
245         if(isset($_REQUEST["installdir"])) {
246                 $c = new GLCASConfig();
247                 touch($_REQUEST["installdir"]."/webconfig");
248                 $c->loadConfig($_REQUEST["installdir"]."/webconfig");
249                 header("Location: index.php");
250                 return;
251         }
252         
253         $underroot = realpath($WEB_ROOT_FS."/../");
254         
255 ?>
256 <html>
257 <h1>Welcome to GLCAS</h1>
258 Welcome to GLCAS, I cant find my configuration file so im assuming you installing me for the first time<br>
259 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
260 config, cause that will make life easier.<br><br>
261 However, if this is the first time you have run this app, then all is good with the world. All I need right
262 now is a place where i can store my config. I search the following directories for the location of my 
263 configuration (webconfig)<br>
264 <li> /var/run/glcas/
265 <li> /var/lib/glcas/
266 <li> <?php echo $underroot?>/var/glcas/
267 <br><br>
268
269 As root, you must now create one of these directories and change the ownership of the directory to the web owner.
270 (chown <?php echo $uid?>:<?php echo $gid ?> the_path_you_choose).<br>
271
272 Now, tell me where you want me to create the webconfig file:<br>
273 <form method="post">
274 <select name="installdir">
275 <option value="/var/run/glcas">/var/run/glcas</option>
276 <option value="/var/lib/glcas">/var/lib/glcas</option>
277 <option value="<?php echo $underroot?>/var/glcas/"><?php echo $underroot?>/var/glcas/</option>
278 </select>
279 <input type="submit" name="Go" value="Go">
280
281 </select>
282 </form>
283
284 </html>
285 <?php 
286 }
287
288
289 ?>