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