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