moved the old git poc code off and away from the main directory
[gwvp.git] / gwvplib / gwvpweb.php
1 <?php
2
3 // this function is the initial insertion point for the web calls, here we need to determine where we go
4 global $CALL_ME_FUNCTIONS;
5
6 // the home_page_provders bit is an array 
7 global $HOME_PAGE_PROVIDERS;
8
9 $MENU_ITEMS["00home"]["text"] = "Home";
10 $MENU_ITEMS["00home"]["link"] = "$BASE_URL";
11
12
13
14 function gwvp_goWebBegin()
15 {
16         global $CALL_ME_FUNCTIONS;
17         
18         // first we determine if we have a valid setup and run the installer if not
19         if(!gwvp_issetup()) {
20                 gwvp_goSetup();
21                 return;
22         }
23         
24         // next, we go thru the CALL_ME_FUNCTIONS - the purpose of call_me_functions is to determine if a function should be called based on
25         // the functions return (i.e. if function returns false, its not it, otherwise it returns a function name we have to call)
26         // this is important for our plugin structure later on - the key on the array serves an an ordering method
27         ksort($CALL_ME_FUNCTIONS);
28         foreach($CALL_ME_FUNCTIONS as $key => $val) {
29                 error_log("checking callmefunction $key as $val");
30                 $callme = $val();
31                 if($callme !== false) {
32                         $callme();
33                         return;
34                 }
35         }
36         
37         // we fell-thru to the main web page builder
38         gwvp_goMainPage();
39 }
40
41 function gwvp_SendMessage($messagetype, $message)
42 {
43         $_SESSION["messagetype"] = $messagetype;
44         $_SESSION["message"] = $message;
45 }
46
47 function gwvp_goMainPage($bodyFunction = null)
48 {
49         // the main page will look pretty simple, a title, a menu then a body
50         global $WEB_ROOT_FS, $BASE_URL;
51         
52         // a simple web page layout that loads any css and js files that exist in the css and js directories
53         echo "<html><head><title>GWVP</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         echo "<h1>Git over Web Via PHP</h2>";
90         
91         
92         echo "<table width=\"100%\">";
93
94         if(isset($_SESSION["message"])) {
95                 echo "<tr width=\"100%\"><td colspan=\"2\">";
96                 gwvp_MessageBuilder();
97                 echo "</td></tr>";
98         }
99         
100         echo "<tr width=\"100%\" bgcolor=\"#ddffdd\"><td>";
101         gwvp_MenuBuilder();
102         echo "</td><td align=\"right\">";
103         gwvp_LoginBuilder();
104         echo "</td>";
105         
106         echo "</tr>";
107         
108         echo "<tr><td>";
109         if($bodyFunction == null) {
110                 gwvp_BodyBuilder();
111         } else {
112                 if(function_exists($bodyFunction)) {
113                         $bodyFunction();
114                 } else {
115                         error_log("Got called with non-existant body function, $bodyFunction");
116                         gwvp_BodyBuilder();
117                 }
118         }
119         echo "</td></tr>";
120         
121         echo "<tr><td>";
122         gwvp_TailBuilder();
123         echo "</td></tr></table></body></html>";
124         
125 }
126
127
128 // builds the message builder if its needed
129 function gwvp_MessageBuilder()
130 {
131         $message = "";
132         $messagetype = "info";
133         if(isset($_SESSION["message"])) $message = $_SESSION["message"];
134         if(isset($_SESSION["messagetype"])) $messagetype = $_SESSION["messagetype"];
135         
136         if($message != "") {
137                 switch($messagetype) {
138                         case "info":
139                                 echo "<table border=\"1\" width=\"100%\"><tr width=\"100%\"><td bgcolor=\"#AAFFAA\">$message</td></tr></table>";
140                                 break;
141                         case "error":
142                                 echo "<table border=\"1\" width=\"100%\"><tr width=\"100%\"><td bgcolor=\"#FFAAAA\">$message</td></tr></table>";
143                                 break;
144                 }
145                 unset($_SESSION["message"]);
146                 if(isset($_SESSION["messagetype"])) unset($_SESSION["messagetype"]);
147         }
148 }
149
150 // builds the menu structure
151 function gwvp_MenuBuilder()
152 {
153         global $MENU_ITEMS, $BASE_URL;
154         
155         ksort($MENU_ITEMS);
156         
157         echo "<table border=\"1\"><tr><td><b><i>Menu</i></b></td>";
158         foreach($MENU_ITEMS as $key => $val) {
159                 $link = $val["link"];
160                 $text = $val["text"];
161                 
162                 // TODO: redo this bit with stristr to find urls - special case for home
163                 $menucolor = "";
164                 if(isset($_REQUEST["q"])) {
165                         $extlink = str_replace("$BASE_URL/", "", $link);
166                         error_log("trying to do replace of $BASE_URL in $link, got $extlink for ".$_REQUEST["q"]);
167                         if(stristr($_REQUEST["q"], $extlink)!==false) {
168                                 $menucolor = " bgcolor=\"#ffdddd\"";
169                                 
170                         }
171                 } else {
172                         // special case for home
173                         if($link == $BASE_URL) $menucolor = " bgcolor=\"#ffdddd\"";
174                 }
175                 
176                 
177                 
178                 
179                 if(isset($val["userlevel"])) {
180                         if(gwvp_CheckAuthLevel($val["userlevel"])) {
181                                 echo "<td$menucolor><a href=\"$link\">$text</a></td>";
182                         }
183                         
184                 } else {
185                         echo "<td$menucolor><a href=\"$link\">$text</a></td>";
186                 }
187         }
188         echo "</tr></table>";
189         
190 }
191
192 function gwvp_LoginBuilder()
193 {
194         global $WEB_ROOT_FS, $BASE_URL;
195         
196         $login = gwvp_IsLoggedIn();
197         if($login === false) {
198                 gwvp_SingleLineLoginForm();
199         } else {
200                 echo "Hello, ".gwvp_GetFullName($login)." <a href=\"$BASE_URL/logout\">logout</a>";
201         }
202 }
203
204 // builds the body structure
205 function gwvp_BodyBuilder()
206 {
207         global $HOME_PAGE_PROVIDERS;
208         
209         echo "I AM THE MAIN BODY, FEAR ME!!!! - have no idea whats going to go here";
210         if(isset($HOME_PAGE_PROVIDERS)) {
211                 ksort($HOME_PAGE_PROVIDERS);
212                 foreach($HOME_PAGE_PROVIDERS as $provider) {
213                         error_log("Loading home_page_provider, $provider");
214                         $provider();
215                 }
216         }
217 }
218
219 // builds the tail structure
220 function gwvp_TailBuilder()
221 {
222         echo "<font size=\"-1\"><i>Copyright 2011, PJR - licensed under GPL</i></font>";
223 }
224
225 function gwvp_fourZeroThree()
226 {
227         error_log("403 called");
228         header("HTTP/1.0 403 Permission Denied");
229 }
230
231 function gwvp_fourZeroFour()
232 {
233         error_log("404 called");
234         header("HTTP/1.0 404 No Such Thing");
235 }
236
237
238
239 ?>