forked from sdslabs/leaderboard
/
index.php
116 lines (100 loc) · 2.79 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Filepanda index.php
* This is the main router file.
*/
require 'lib/limonade.php';//Include the framework
require 'config.php';//Configuration
require 'HTTP/Request2.php';
/**
* This function is called before each
* route initialization.
*/
function before($route)
{
layout('layout.php');//default layout
set('userid', @$_SESSION['userid']);
}
/* Autoloading class.php **/
spl_autoload_register(function ($class) {
include 'lib/'.strtolower($class).'php';
});
/**
* 404 error page.
*/
function not_found($errno, $errstr, $errfile = null, $errline = null)
//if there is a missing image
{
if (strpos($errstr, '/public/soft_images/') !== false) {
render_file('public/img/down.png');
return;
}
before();
set('errno', $errno);
set('errstr', $errstr);
set('errfile', $errfile);
set('errline', $errline);
layout('layout.php');
return html('404.php');
}
/** Handle 500 errors */
function server_error($errno, $errstr, $errfile = null, $errline = null)
{
$args = compact('errno', 'errstr', 'errfile', 'errline');
return var_dump($args, true);
}
/*
* Home page shows all scores
* for all users
*/
dispatch('/', 'Score::view_all');
dispatch('/debug', function () {
return json($_SESSION);
});
/* Authentication Related Stuff */
dispatch('/login/:service', function () {
$serviceClassName = ucfirst(params('service'));
//since github is our main authentication method
//we allow that to run unauthenticated
if (!@$_SESSION['userid'] && $serviceClassName != 'Github') {
throw new Exception('You need to be logged in');
}
set('username', Token::get(params('service'), $_SESSION['userid']));
return $obj = $serviceClassName::login();
});
dispatch('/login/:service/callback', function () {
$serviceClassName = ucfirst(params('service'));
if (!@$_SESSION['userid'] && $serviceClassName != 'Github') {
throw new Exception('You need to be logged in');
}
return $serviceClassName::callback();
});
dispatch('/logout', function () {
unset($_SESSION['userid']);
redirect_to('/');
});
/*
* Update score for a particular
* user and service
*/
dispatch('/update/:service/:userid', function () {
$serviceClassName = ucfirst(params('service'));
$serviceClassName::update(params('userid'));
flash('message', 'Your score for '.params('service').' has been updated');
redirect_to('/accounts');
});
//Management of linked accounts
dispatch('/accounts', function () {
if (!@$_SESSION['userid']) {
throw new Exception('You need to be logged in');
}
global $db;
$userid = $_SESSION['userid'];
return html('accounts.php');
});
//Static pages
dispatch('/page/:page', function () {
return html('pages/'.params('page').'.html');
});
//Start the app
run();