3 // if i believed in name spacing in php, i'd use it.
4 error_log("glcasweb loaded");
7 $CRON_CLASSES["GLCASWeb"] = "GLCASWeb";
12 function __construct($config)
14 $this->config = $config;
17 function go($urlhandlers)
20 if(isset($_REQUEST["q"])) {
21 $url = $_REQUEST["q"];
25 $ipaddr = $_SERVER["REMOTE_ADDR"];
26 $this->config->delData("lastseen", "$ipaddr");
27 $this->config->addData("lastseen", "$ipaddr", time(), "");
29 // create a url parser
30 $urlparser = new GLCASUrlParser($urlhandlers, $this->config);
33 $call_class = $urlparser->getClass($url);
34 error_log("urldecode: ".urldecode($url));
35 $call_class->go($url);
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"];
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);
59 function GLCASMenuBuilder()
61 global $BASE_URL, $MENU_ITEMS;
65 <table border="1"><tr><th>Menu</th><td>
66 <a href="<?php echo $BASE_URL ?>">Home</a></td>
68 foreach($MENU_ITEMS as $key => $val) {
71 echo "<td><a href=$link>$name</a></td>";
77 // just a short one to call the long one
81 GLCASAddMessage($m, $s);
84 function GLCASAddMessage($messagelev, $message)
88 $glconfig->addData("messages", time(), "$messagelev", $message);
91 function GLCASMessageBuilder()
96 $msgs_v = $glconfig->getData("messages");
98 // create a form for setting messages
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\">";
108 if($msgs_v !== false) foreach($msgs_v as $msgs) {
110 error_log("messages: ".$msgs["category"]. " - ".$msgs["val"]);
112 $nml[$nm]["msg"] = glcas_tdiffToAgo(time()-$msgs["category"])." : ".$msgs["val"];
115 switch($msgs["name"]) {
117 $fontst = "<font color=\"green\">";
122 $fontst = "<font color=\"yellow\">";
126 $fontst = "<font color=\"red\">";
131 $nml[$nm]["font_start"] = $fontst;
132 $nml[$nm]["font_end"] = $fonten;
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>";
145 } else echo "No Messages<br>";
148 function GLCASpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="GLCAS")
150 global $WEB_ROOT_FS, $BASE_URL;
154 echo "<html><head><title>$title</title>";
157 if(file_exists("$WEB_ROOT_FS/css")) {
158 $dh = opendir("$WEB_ROOT_FS/css");
160 while(($file = readdir($dh))!==false) {
161 $mt = preg_match("/.*.css$/", $file);
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";
172 if(file_exists("$WEB_ROOT_FS/js")) {
173 $dh = opendir("$WEB_ROOT_FS/js");
175 while(($file = readdir($dh))!==false) {
176 $mt = preg_match("/.*.js$/", $file);
178 error_log("loading js $file");
179 echo "<script type=\"text/javascript\" src=\"$BASE_URL/js/$file\"></script>";
180 //echo "required $basedir/$file\n";
188 echo "</head><body>";
191 echo "<h1>GLCAS</h1><br>";
192 echo "<table><tr><td>";
193 GLCASMessageBuilder();
194 echo "<td></tr><tr><td>";
197 echo "<table><tr><td>";
199 echo "</td></tr><tr><td>";
202 if(isset($_REQUEST["q"])) {
203 $url = $_REQUEST["q"];
206 if($bodyClass != null) {
207 $bodyClass->$bodyFunction($url);
208 } else if( $bodyFunction != null) {
210 } else echo $bodycontent;
211 echo "</td></tr></table>";
214 // close the big wrap-around table
215 echo "</td></tr></table>";
218 echo "<br><font size=\"-1\">Copyright 2011, PJR</font><br></body></html>";
222 function glcas_getLastSeen($ip, $data)
226 if(!$data) return $last;
230 foreach($data as $key=>$val) {
231 $last = $val["name"];
232 error_log("got $last for $ip");
235 if($last == "never") return $last;
237 $last = glcas_tdiffToAgo(time()-$last);
239 error_log("last for $ip set to $last");
245 function glcas_tdiffToAgo($time_in_sec)
247 $tdiff = $time_in_sec;
248 $tdiff_min = (int)($tdiff/60);
249 $tdiff_hour = (int)($tdiff/3600);
250 $tdiff_days = (int)($tdiff/86400);
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";
260 function glcas_isRemoteDir($url)
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");
275 //error_log("I dont believe $realloc is a directory redir for $url");
282 function glcas_fileExists($url)
284 $fp = fopen($url, "r");
285 //error_og("did file_get_contents on $url");
287 foreach($http_response_header as $key => $val) {
288 //error_log("got header of $key for $val");
289 if(preg_match("/.*HTTP\/.*200.*/", $val)) {
299 function glcas_startInstaller()
301 global $WEB_ROOT_FS, $BASE_URL;
303 $uid = posix_geteuid();
304 $gid = posix_getegid();
305 $uid_a = posix_getpwuid($uid);
306 $uid = $uid_a["name"];
308 $gid_a = posix_getgrgid($gid);
309 $gid = $gid_a["name"];
310 error_log("user id is $uid, group id is $gid");
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");
320 $underroot = realpath($WEB_ROOT_FS."/../");
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>
333 <li> <?php echo $underroot?>/var/glcas/
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>
339 Now, tell me where you want me to create the webconfig file:<br>
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>
346 <input type="submit" name="Go" value="Go">