replacing the cli cmds with variables and configuration in config.php
[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, $force_ssl;
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         
28         if($force_ssl) {
29                 if(!isset($_SERVER['HTTPS'])) {
30                         header("Location: https://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"], true);
31                         return;
32                 }       
33         }
34         
35         
36         ksort($CALL_ME_FUNCTIONS);
37         foreach($CALL_ME_FUNCTIONS as $key => $val) {
38                 //// error_log("checking callmefunction $key as $val");
39                 $callme = $val();
40                 if($callme !== false) {
41                         $callme();
42                         return;
43                 }
44         }
45         
46         // we fell-thru to the main web page builder
47         gwvpmini_goMainPage();
48 }
49
50 function gwvpmini_SendMessage($messagetype, $message)
51 {
52         $_SESSION["messagetype"] = $messagetype;
53         $_SESSION["message"] = $message;
54 }
55
56 function gwvpmini_goMainPage($bodyFunction = null)
57 {
58         // the main page will look pretty simple, a title, a menu then a body
59         global $WEB_ROOT_FS, $BASE_URL;
60         
61         // a simple web page layout that loads any css and js files that exist in the css and js directories
62         echo "<html><head><title>GWVP</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         echo "<h1>Git over Web Via PHP</h1>";
99         
100         
101         echo "<table width=\"100%\">";
102         
103         echo "<tr width=\"100%\" bgcolor=\"#ddddff\"><td colspan=\"2\" align=\"right\">";
104         gwvpmini_SearchBuilder();
105         echo "</td></tr>";\r
106         
107         $havemessage = false;
108         if(isset($_SESSION["message"])) $havemessage = true;
109         if(isset($_SESSION["id"])) if(gwvpmini_GetMessagesForId($_SESSION["id"]) !== 0) $havemessage = true;
110         
111         if($havemessage) {
112                 echo "<tr width=\"100%\"><td colspan=\"2\">";
113                 gwvpmini_MessageBuilder();
114                 echo "</td></tr>";
115         }
116         
117         echo "<tr width=\"100%\" bgcolor=\"#ddffdd\"><td>";
118         gwvpmini_MenuBuilder();
119         echo "</td><td align=\"right\">";
120         gwvpmini_LoginBuilder();
121         echo "</td>";
122         
123         echo "</tr>";
124         
125         echo "<tr width=\"100%\"><td colspan=\"3\">";
126         
127         echo "<table width=\"100%\"><tr width=\"100%\" valign=\"top\"><td>";
128         if($bodyFunction == null) {
129                 gwvpmini_BodyBuilder();
130         } else {
131                 if(function_exists($bodyFunction)) {
132                         $bodyFunction();
133                 } else {
134                         // error_log("Got called with non-existant body function, $bodyFunction");
135                         gwvpmini_BodyBuilder();
136                 }
137         }
138         
139         echo "</td>";
140         
141         /* TODO: taking out chat for now
142         echo "<td align=\"right\">";
143         
144         gwvpmini_ChatBuilder();
145         echo "</td>"; */
146         
147         echo "</tr></table>";\r
148         
149         echo "</td></tr>";
150         
151         echo "<tr><td>";
152         gwvpmini_TailBuilder();
153         echo "</td></tr></table></body></html>";
154         
155 }
156
157
158 // builds the message builder if its needed
159 function gwvpmini_MessageBuilder()
160 {
161         
162         error_log("begin message building?");
163         $message = "";
164         $messagetype = "info";
165         if(isset($_SESSION["message"])) $message = $_SESSION["message"];
166         if(isset($_SESSION["messagetype"])) $messagetype = $_SESSION["messagetype"];
167         if(isset($_SESSION["id"])) $dbmsgs = gwvpmini_GetMessagesForId($_SESSION["id"]);
168         
169         error_log("dbmsgs was 0 - ".print_r($_SESSION,true));
170         
171         if($dbmsgs !== 0) {
172                 foreach($dbmsgs as $mgkey => $mgval) {
173                         $data = $mgval["data"];
174                         switch($mgval["type"]) {\r
175                                 case "info":\r
176                                         echo "<table border=\"1\" width=\"100%\"><tr width=\"100%\"><td bgcolor=\"#AAFFAA\">$data</td></tr></table>";\r
177                                         break;\r
178                                 case "error":\r
179                                         echo "<table border=\"1\" width=\"100%\"><tr width=\"100%\"><td bgcolor=\"#FFAAAA\">$data</td></tr></table>";\r
180                                         break;\r
181                         }
182                         gwvpmini_DeleteMessagesById($mgval["id"]);
183                 }
184         } 
185         if($message != "") {
186                 switch($messagetype) {
187                         case "info":
188                                 echo "<table border=\"1\" width=\"100%\"><tr width=\"100%\"><td bgcolor=\"#AAFFAA\">$message</td></tr></table>";
189                                 break;
190                         case "error":
191                                 echo "<table border=\"1\" width=\"100%\"><tr width=\"100%\"><td bgcolor=\"#FFAAAA\">$message</td></tr></table>";
192                                 break;
193                 }
194                 unset($_SESSION["message"]);
195                 if(isset($_SESSION["messagetype"])) unset($_SESSION["messagetype"]);
196         }
197 }
198
199 function gwvpmini_ChatBuilder()
200 {
201         gwvpmini_DisplayChat();
202 }
203
204 // builds the menu structure
205 function gwvpmini_MenuBuilder()
206 {
207         global $MENU_ITEMS, $BASE_URL;
208         
209         ksort($MENU_ITEMS);
210         
211         echo "<table border=\"1\"><tr><td><b><i>Menu</i></b></td>";
212         foreach($MENU_ITEMS as $key => $val) {
213                 $link = $val["link"];
214                 $text = $val["text"];
215                 
216                 // TODO: redo this bit with stristr to find urls - special case for home
217                 $menucolor = "";
218                 if(isset($_REQUEST["q"])) {
219                         $extlink = str_replace("$BASE_URL/", "", $link);
220                         // error_log("trying to do replace of $BASE_URL in $link, got $extlink for ".$_REQUEST["q"]);
221                         if(stristr($_REQUEST["q"], $extlink)!==false) {
222                                 $menucolor = " bgcolor=\"#ffdddd\"";
223                                 
224                         }
225                 } else {
226                         // special case for home
227                         if($link == $BASE_URL) $menucolor = " bgcolor=\"#ffdddd\"";
228                 }
229                 
230                 
231                 
232                 
233                 if(isset($val["userlevel"])) {
234                         if(gwvpmini_CheckAuthLevel($val["userlevel"])) {
235                                 echo "<td$menucolor><a href=\"$link\">$text</a></td>";
236                         }
237                         
238                 } else {
239                         echo "<td$menucolor><a href=\"$link\">$text</a></td>";
240                 }
241         }
242         echo "</tr></table>";
243         
244 }
245
246 function gwvpmini_LoginBuilder()
247 {
248         global $WEB_ROOT_FS, $BASE_URL;
249         
250         $login = gwvpmini_IsLoggedIn();
251         if($login === false) {
252                 gwvpmini_SingleLineLoginForm();
253         } else {
254                 echo "Hello <a href=\"$BASE_URL/user/".$_SESSION["username"]."\">".$_SESSION["fullname"]."</a> <a href=\"$BASE_URL/logout\">logout</a>";
255         }
256 }
257
258 // builds the body structure
259 function gwvpmini_BodyBuilder()
260 {
261         global $HOME_PAGE_PROVIDERS;
262         
263         if(isset($HOME_PAGE_PROVIDERS)) {
264                 ksort($HOME_PAGE_PROVIDERS);
265                 foreach($HOME_PAGE_PROVIDERS as $provider) {
266                         // error_log("Loading home_page_provider, $provider");
267                         $provider();
268                 }
269         }
270 }
271
272 // builds the tail structure
273 function gwvpmini_TailBuilder()
274 {
275         echo "<br><br><hr><font size=\"-1\"><b><a href=\"http://github.com/takigama/GWVP\">GWVP</a></b> - <i>Copyright 2011, 2012 PJR - <a href=\"http://www.gnu.org/copyleft/gpl.html\">GPL</a></i></font>";
276 }
277
278 function gwvpmini_emailToUserLink($email)
279 {
280         global $BASE_URL;
281         
282         $username = gwvpmini_GetUserNameFromEmail($email);
283         
284         if($username !== false) {
285                 return "<a href=\"$BASE_URL/user/$username\">$username</a>";
286         } else {
287                 return false;
288         }
289 }
290
291 function gwvpmini_fourZeroThree()
292 {
293         // error_log("403 called");
294         header("HTTP/1.1 403 Permission Denied");
295 }
296
297 function gwvpmini_fourZeroFour()
298 {
299         // error_log("404 called");
300         header("HTTP/1.1 404 No Such Thing");
301 }
302
303
304 /**\r
305  * Get either a Gravatar URL or complete image tag for a specified email address.\r
306  *\r
307  * @param string $email The email address\r
308  * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]\r
309  * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]\r
310  * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]\r
311  * @param boole $img True to return a complete IMG tag False for just the URL\r
312  * @param array $atts Optional, additional key/value attributes to include in the IMG tag\r
313  * @return String containing either just a URL or a complete image tag\r
314  * @source http://gravatar.com/site/implement/images/php/\r
315  */
316 function gwvpmini_HtmlGravatar($email, $size, $htmlappend="")
317 {
318         
319         global $use_gravatar;
320         
321         if($use_gravatar) {
322                 // error_log("call to gravatar with yes");
323         } else {
324                 // error_log("call to gravatar with no");
325         }
326         
327         if($use_gravatar == false) return "";
328         return get_gravatar( $email, $size, 'mm', 'g', true)."$htmlappend";
329 }
330 \r
331 function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {\r
332         $url = 'http://en.gravatar.com/avatar/';\r
333         $url .= md5( strtolower( trim( $email ) ) );\r
334         $url .= "?s=$s&d=$d&r=$r";\r
335         if ( $img ) {\r
336                 $url = '<img src="' . $url . '"';\r
337                 foreach ( $atts as $key => $val )\r
338                         $url .= ' ' . $key . '="' . $val . '"';\r
339                 $url .= ' />';\r
340         }\r
341         return $url;\r
342 }\r
343
344
345 ?>