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