96b787e9f9ac8d5ca98ca26bf964014d36389484
[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 class GLCASWeb {
7         
8         function __construct($config)
9         {
10                 $this->config = $config;
11         }
12         
13         function go($urlhandlers)
14         {
15                 $url = "/";
16                 if(isset($_REQUEST["q"])) {
17                         $url = $_REQUEST["q"];
18                 }
19                 
20                 // create a url parser
21                 $urlparser = new GLCASUrlParser($urlhandlers, $this->config);
22                 
23                 // 
24                 $call_class = $urlparser->getClass($url);
25                 error_log("urldecode: ".urldecode($url));
26                 $call_class->go($url);
27         }
28         
29         private $config;
30         
31 }
32
33 function GLCASMenuBuilder()
34 {
35         global $BASE_URL, $MENU_ITEMS;
36         
37         sort($MENU_ITEMS);
38         ?>
39         <table border="1"><tr><th>Menu</th><td>
40 <a href="<?php echo $BASE_URL ?>">Home</a></td>
41         <?php
42         foreach($MENU_ITEMS as $key => $val) {
43                 $link = $val["link"];
44                 $name = $val["name"];
45                 echo "<td><a href=$link>$name</a></td>";
46         }
47         
48         echo "</table>";
49 }
50
51 function GLCASMessageBuilder()
52 {
53         echo "<i>Messages not implemented yet</i>";
54 }
55
56 function GLCASpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="GLCAS")
57 {
58         global $WEB_ROOT_FS, $BASE_URL;
59         
60         // TODO: load css
61         // header
62         echo "<html><head><title>$title</title>";
63         
64         // load css
65         if(file_exists("$WEB_ROOT_FS/css")) {
66                 $dh = opendir("$WEB_ROOT_FS/css");
67                 if($dh) {
68                         while(($file = readdir($dh))!==false) {
69                                 $mt = preg_match("/.*.css$/", $file);
70                                 if($mt > 0) {
71                                         error_log("loading css $file");
72                                         echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$BASE_URL/css/$file\">";
73                                         //echo "required $basedir/$file\n";
74                                 }
75                         }
76                 }               
77         }
78
79         // load js
80         if(file_exists("$WEB_ROOT_FS/js")) {
81                 $dh = opendir("$WEB_ROOT_FS/js");
82                 if($dh) {
83                         while(($file = readdir($dh))!==false) {
84                                 $mt = preg_match("/.*.js$/", $file);
85                                 if($mt > 0) {
86                                         error_log("loading js $file");
87                                         echo "<script type=\"text/javascript\" src=\"$BASE_URL/js/$file\"></script>";
88                                         //echo "required $basedir/$file\n";
89                                 }
90                         }
91                 }               
92         }
93         
94         
95         // start body
96         echo "</head><body>";
97         
98         // page top
99         echo "<h1>GLCAS</h1><br>";
100         echo "<table><tr><td>";
101         GLCASMessageBuilder();
102         echo "<td></tr><tr><td>";
103         
104         // menu, then body
105         echo "<table><tr><td>";
106         GLCASMenuBuilder();
107         echo "</td></tr><tr><td>";
108         // body
109         $url = "/";
110         if(isset($_REQUEST["q"])) {
111                 $url = $_REQUEST["q"];
112         }
113         
114         if($bodyClass != null) {
115                 $bodyClass->$bodyFunction($url);
116         } else if( $bodyFunction != null) {
117                 $bodyFunction($url);
118         } else echo $bodycontent;
119         echo "</td></tr></table>";
120         
121         
122         // close the big wrap-around table
123         echo "</td></tr></table>";
124         
125         // footer
126         echo "<br><font size=\"-1\">Copyright 2011, PJR</font><br></body></html>";
127         
128 }
129
130 function glcas_isRemoteDir($url)
131 {
132         file_get_contents($url);
133         //error_log("did file_get_contents on $url");
134         foreach($http_response_header as $key => $val) {
135                 //error_log("got header of $key for $val");
136                 if(preg_match("/.*Location:.*/", $val)) {
137                         //error_log("in details apt got location as $val from $url");
138                         $realloc = preg_replace("/.*: /", "", $val);
139                         $urlwithslash = $url."/";
140                         //echo "<br>in comp with<br>$urlwithslash<br>$realloc<br>";
141                         if(strcasecmp($realloc,$urlwithslash)==0) {
142                                 //error_log("I believe $realloc is a directory redir for $url");
143                                 return true;
144                         } else {
145                                 //error_log("I dont believe $realloc is a directory redir for $url");
146                         }
147                 }
148         }
149         return false;
150 }
151
152 function glcas_fileExists($url)
153 {
154         $fp = fopen($url, "r");
155         //error_og("did file_get_contents on $url");
156         $retval = false;
157         foreach($http_response_header as $key => $val) {
158                 //error_log("got header of $key for $val");
159                 if(preg_match("/.*HTTP\/.*200.*/", $val)) {
160                         //echo "is true\n";
161                         
162                         $retval = true;
163                 }
164         }
165         fclose($fp);
166         return $retval;
167 }
168
169 function glcas_startInstaller()
170 {
171         global $WEB_ROOT_FS, $BASE_URL;
172         
173         $uid = posix_geteuid();
174         $gid = posix_getegid();
175         $uid_a = posix_getpwuid($uid);
176         $uid = $uid_a["name"];
177         
178         $gid_a = posix_getgrgid($gid);
179         $gid = $gid_a["name"];
180         error_log("user id is $uid, group id is $gid");
181         
182         if(isset($_REQUEST["installdir"])) {
183                 $c = new GLCASConfig();
184                 touch($_REQUEST["installdir"]."/webconfig");
185                 $c->loadConfig($_REQUEST["installdir"]."/webconfig");
186                 header("Location: index.php");
187                 return;
188         }
189         
190         $underroot = realpath($WEB_ROOT_FS."/../");
191         
192 ?>
193 <html>
194 <h1>Welcome to GLCAS</h1>
195 Welcome to GLCAS, I cant find my configuration file so im assuming you installing me for the first time<br>
196 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
197 config, cause that will make life easier.<br><br>
198 However, if this is the first time you have run this app, then all is good with the world. All I need right
199 now is a place where i can store my config. I search the following directories for the location of my 
200 configuration (webconfig)<br>
201 <li> /var/run/glcas/
202 <li> /var/lib/glcas/
203 <li> <?php echo $underroot?>/var/glcas/
204 <br><br>
205
206 As root, you must now create one of these directories and change the ownership of the directory to the web owner.
207 (chown <?php echo $uid?>:<?php echo $gid ?> the_path_you_choose).<br>
208
209 Now, tell me where you want me to create the webconfig file:<br>
210 <form method="post">
211 <select name="installdir">
212 <option value="/var/run/glcas">/var/run/glcas</option>
213 <option value="/var/lib/glcas">/var/lib/glcas</option>
214 <option value="<?php echo $underroot?>/var/glcas/"><?php echo $underroot?>/var/glcas/</option>
215 </select>
216 <input type="submit" name="Go" value="Go">
217
218 </select>
219 </form>
220
221 </html>
222 <?php 
223 }
224
225
226 ?>