Added contact offers.
[readifood.git] / www / index.php
1 <?php
2
3   /* XXX */
4   date_default_timezone_set("UTC");
5
6   $charity = "Readifood";
7   $propel_project = "readifood";
8   $root = join(DIRECTORY_SEPARATOR, array($_SERVER['DOCUMENT_ROOT'], ".."));
9   $propel_root = join(DIRECTORY_SEPARATOR, array($root, "propel"));
10   $lib_root = join(DIRECTORY_SEPARATOR, array($root, "lib"));
11   require_once(join(DIRECTORY_SEPARATOR, array($root, "vendor", "autoload.php")));
12
13   Propel::init("$propel_root/build/conf/$propel_project-conf.php");
14   set_include_path(join(PATH_SEPARATOR, array(join(DIRECTORY_SEPARATOR, array($propel_root, "build", "classes")), get_include_path())));
15
16   $script_url = array_key_exists('SCRIPT_URL', $_SERVER) ? $_SERVER['SCRIPT_URL'] : $_SERVER['REDIRECT_URL'];
17   $request = preg_replace('!^/branches/[^/]+/!', "", $script_url);
18   $request = preg_replace('!^/+!', "", $request);
19   $request = preg_replace('!/+$!', "", $request);
20
21   # Extract module and parameters from URI.
22   $module = preg_replace('!/.*!', "", $request);
23   $parameters = split("/", preg_replace('!^[^/]*/!', "", $request));
24   if (count($parameters) == 1 && $parameters[0] == $module) $parameters = null;
25
26   # Sanitise module.
27   if (! preg_match('/^[A-Za-z0-9-_]+$/', $module)) $module = null;
28   else if (! file_exists("$lib_root/$module.php")) $module = null;
29   else $module = strtolower($module);
30   #echo "request: $request; module: $module; params: " . print_r($parameters, true);
31
32   $http = (isset($_SERVER['HTTPS'])) ? "https" : "http";
33   include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "constants.php")));
34   include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "functions.php")));
35   include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "session.php")));
36
37   if (isset($_GET['code'])) {
38     try {
39       $auth0->getUser();
40       header("Location: https://" . $_SERVER['HTTP_HOST'] . "/");
41       exit;
42     }
43     catch (\Exception $e) {
44       include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "header.php")));
45       echo "<h1 class=\"printonly\">$charity</h1>\n";
46       echo "<h1>Not logged in!</h1>\n";
47       echo "<p>There was an authentication error.</p>\n";
48       include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "footer.php")));
49     }
50   }
51
52   $auth0_user = $auth0->getUser();
53   if (isset($auth0_user)) $username = $auth0_user["sub"];
54   else $username = null;
55
56   include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "header.php")));
57
58   if ($username) {
59     $q = new UserQuery;
60     $user = $q->findOneByUsername($username);
61     if (! $q->count()) {
62       echo "<h1 class=\"printonly\">$charity</h1>\n";
63       echo "<h1>Not logged in!</h1>\n";
64       echo "<p>User <em>$username</em> needs an entry in the user table.</p>\n";
65     }
66     else {
67       $user_id = $user->getContactId();
68       $admin_level = $user->getAdmin();
69       if ($module) {
70         echo "<h3 class=\"printonly\">$charity $module</h3>\n";
71         include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "$module.php")));
72       }
73
74       #else include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "menu.php")));
75     }
76   }
77   else $auth0->login();
78   include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "footer.php")));
79 ?>