b15822638fcb08c4c0032a30ac1f64b9438e5cde
[gwvp-mini.git] / gwvpmini / gwvpmini_web.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 gwvpmini_goWeb()
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(!gwvpmini_issetup()) {
20                 gwvpmini_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         gwvpmini_goMainPage();
39 }
40
41 function gwvpmini_SendMessage($messagetype, $message)
42 {
43         $_SESSION["messagetype"] = $messagetype;
44         $_SESSION["message"] = $message;
45 }
46
47 function gwvpmini_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 Mini</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 - Mini Version</h2>";
90         
91         
92         echo "<table width=\"100%\">";
93         
94         echo "<tr width=\"100%\" bgcolor=\"#ddddff\"><td colspan=\"2\" align=\"right\">";
95         gwvpmini_SearchBuilder();
96         echo "</td></tr>";\r
97         
98
99         if(isset($_SESSION["message"])) {
100                 echo "<tr width=\"100%\"><td colspan=\"2\">";
101                 gwvpmini_MessageBuilder();
102                 echo "</td></tr>";
103         }
104         
105         echo "<tr width=\"100%\" bgcolor=\"#ddffdd\"><td>";
106         gwvpmini_MenuBuilder();
107         echo "</td><td align=\"right\">";
108         gwvpmini_LoginBuilder();
109         echo "</td>";
110         
111         echo "</tr>";
112         
113         echo "<tr><td>";
114         if($bodyFunction == null) {
115                 gwvpmini_BodyBuilder();
116         } else {
117                 if(function_exists($bodyFunction)) {
118                         $bodyFunction();
119                 } else {
120                         error_log("Got called with non-existant body function, $bodyFunction");
121                         gwvpmini_BodyBuilder();
122                 }
123         }
124         echo "</td></tr>";
125         
126         echo "<tr><td>";
127         gwvpmini_TailBuilder();
128         echo "</td></tr></table></body></html>";
129         
130 }
131
132
133 // builds the message builder if its needed
134 function gwvpmini_MessageBuilder()
135 {
136         $message = "";
137         $messagetype = "info";
138         if(isset($_SESSION["message"])) $message = $_SESSION["message"];
139         if(isset($_SESSION["messagetype"])) $messagetype = $_SESSION["messagetype"];
140         
141         if($message != "") {
142                 switch($messagetype) {
143                         case "info":
144                                 echo "<table border=\"1\" width=\"100%\"><tr width=\"100%\"><td bgcolor=\"#AAFFAA\">$message</td></tr></table>";
145                                 break;
146                         case "error":
147                                 echo "<table border=\"1\" width=\"100%\"><tr width=\"100%\"><td bgcolor=\"#FFAAAA\">$message</td></tr></table>";
148                                 break;
149                 }
150                 unset($_SESSION["message"]);
151                 if(isset($_SESSION["messagetype"])) unset($_SESSION["messagetype"]);
152         }
153 }
154
155 // builds the menu structure
156 function gwvpmini_MenuBuilder()
157 {
158         global $MENU_ITEMS, $BASE_URL;
159         
160         ksort($MENU_ITEMS);
161         
162         echo "<table border=\"1\"><tr><td><b><i>Menu</i></b></td>";
163         foreach($MENU_ITEMS as $key => $val) {
164                 $link = $val["link"];
165                 $text = $val["text"];
166                 
167                 // TODO: redo this bit with stristr to find urls - special case for home
168                 $menucolor = "";
169                 if(isset($_REQUEST["q"])) {
170                         $extlink = str_replace("$BASE_URL/", "", $link);
171                         error_log("trying to do replace of $BASE_URL in $link, got $extlink for ".$_REQUEST["q"]);
172                         if(stristr($_REQUEST["q"], $extlink)!==false) {
173                                 $menucolor = " bgcolor=\"#ffdddd\"";
174                                 
175                         }
176                 } else {
177                         // special case for home
178                         if($link == $BASE_URL) $menucolor = " bgcolor=\"#ffdddd\"";
179                 }
180                 
181                 
182                 
183                 
184                 if(isset($val["userlevel"])) {
185                         if(gwvpmini_CheckAuthLevel($val["userlevel"])) {
186                                 echo "<td$menucolor><a href=\"$link\">$text</a></td>";
187                         }
188                         
189                 } else {
190                         echo "<td$menucolor><a href=\"$link\">$text</a></td>";
191                 }
192         }
193         echo "</tr></table>";
194         
195 }
196
197 function gwvpmini_LoginBuilder()
198 {
199         global $WEB_ROOT_FS, $BASE_URL;
200         
201         $login = gwvpmini_IsLoggedIn();
202         if($login === false) {
203                 gwvpmini_SingleLineLoginForm();
204         } else {
205                 echo "Hello ".$_SESSION["fullname"]." <a href=\"$BASE_URL/logout\">logout</a>";
206         }
207 }
208
209 // builds the body structure
210 function gwvpmini_BodyBuilder()
211 {
212         global $HOME_PAGE_PROVIDERS;
213         
214         echo "I AM THE MAIN BODY, FEAR ME!!!! - have no idea whats going to go here";
215         if(isset($HOME_PAGE_PROVIDERS)) {
216                 ksort($HOME_PAGE_PROVIDERS);
217                 foreach($HOME_PAGE_PROVIDERS as $provider) {
218                         error_log("Loading home_page_provider, $provider");
219                         $provider();
220                 }
221         }
222 }
223
224 // builds the tail structure
225 function gwvpmini_TailBuilder()
226 {
227         echo "<font size=\"-1\"><i>Copyright 2011, PJR - licensed under GPL</i></font>";
228 }
229
230 function gwvpmini_fourZeroThree()
231 {
232         error_log("403 called");
233         header("HTTP/1.0 403 Permission Denied");
234 }
235
236 function gwvpmini_fourZeroFour()
237 {
238         error_log("404 called");
239         header("HTTP/1.0 404 No Such Thing");
240 }
241
242
243
244 ?>