Support Auth0.
[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   $auth0_user = $auth0->getUser();
38   if (isset($auth0_user)) $username = $auth0_user["sub"];
39   else $username = null;
40
41   include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "header.php")));
42
43   if ($username) {
44     $q = new UserQuery;
45     $user = $q->findOneByUsername($username);
46     if (! $q->count()) {
47       echo "<h1 class=\"printonly\">$charity</h1>\n";
48       echo "<h1>Not logged in!</h1>\n";
49       echo "<p>User <em>$username</em> needs an entry in the user table.</p>\n";
50     }
51     else {
52       $user_id = $user->getContactId();
53       $admin_level = $user->getAdmin();
54       if ($module) {
55         echo "<h3 class=\"printonly\">$charity $module</h3>\n";
56         include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "$module.php")));
57       }
58
59       #else include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "menu.php")));
60     }
61   }
62   else $auth0->login();
63   include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "footer.php")));
64 ?>