/
index.php
139 lines (122 loc) · 3.89 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
error_reporting(-1);
require_once __DIR__.'/vendor/autoload.php';
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class MyApp extends Application {
use Application\TwigTrait;
# use Application\SecurityTrait;
# use Application\FormTrait;
# use Application\UrlGeneratorTrait;
# use Application\SwiftmailerTrait;
# use Application\MonologTrait;
# use Application\TranslationTrait;
}
class SpfResponse {
public static function wrapJSON(array $body, $head='', $foot='') {
return (object) array(
'head' => $head ? $head : '<!-- Styles -->',
'body' => $body,
'foot' => $foot ? $foot : '<!-- Scripts -->',
);
}
public static function render($template, $twig, $isSpfRequest) {
if ($isSpfRequest === true) {
// wrap the content in JSON
$tpl = $twig->render($template.'.twig', array());
return json_encode(self::wrapJSON(['content' => $tpl]));
} else {
// render the html directly
return $twig->render('base.twig', array(
'header' => $twig->render('header.twig', array()),
'content' => $twig->render($template.'.twig', array()),
'footer' => $twig->render('footer.twig', array()),
));
}
}
}
$checkSPF = function (Request $request, MyApp $app) {
if ($request->query->get('spf') === 'navigate') {
// must return json-wrapped html fragments
$app->isSpfRequest = true;
} else {
// must return the full html page
$app->isSpfRequest = false;
}
};
$app = new MyApp();
$app['debug'] = true;
// set up twig
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views',
));
### ------
### ROUTES
### ------
# INDEX
$app->get('/', function () use ($app) {
// index is always full html
return $app['twig']->render('base.twig', array(
'header' => $app['twig']->render('header.twig', array()),
'content' => $app['twig']->render('index.twig', array()),
'footer' => $app['twig']->render('footer.twig', array()),
));
});
# PHOTOS
$app->get('/photos', function () use ($app) {
return SpfResponse::render('photos', $app['twig'], $app->isSpfRequest);
})->before($checkSPF);
# SINGLE PHOTO
# $app->get('/photos/{id}', function () use($app) {
# // return $spf->render('photo', $app);
# })->assert('id', '\d+');
# VIDEOS
$app->get('/videos', function () use ($app) {
return SpfResponse::render('videos', $app['twig'], $app->isSpfRequest);
})->before($checkSPF);
# SINGLE VIDEO
# $app->get('/video/{id}', function () use($app) {
# return $spf->render('video', $app);
# })->assert('id', '\d+');
# LOGIN
$app->get('/login', function () use($app) {
return SpfResponse::render('login', $app['twig'], $app->isSpfRequest);
})->before($checkSPF);
# JOIN
$app->get('/join', function () use($app) {
return SpfResponse::render('join', $app['twig'], $app->isSpfRequest);
})->before($checkSPF);
# -------------
# Error Handler
# -------------
$app->error(function (\Exception $e, $code) use ($app) {
# use default debug handler
if ($app['debug']) {
return;
}
switch ($code) {
case 401:
$message = '401 - Unauthorized';
break;
case 402:
$message = '402 - Payment Required';
break;
case 403:
$message = '403 - Forbidden';
break;
case 404:
$message = '404 - Not Found';
break;
case 405:
$message = '405 - Method Not Allowed';
break;
case 500:
$message = '500 - Internal Server Error';
break;
default:
$message = 'We are sorry, but something went terribly wrong.';
}
return new Response($message);
});
$app->run();