/
index.php
139 lines (97 loc) · 2.93 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
# Composer autoload
require 'vendor/autoload.php';
require 'config.php';
# Initiate the Slim Framework
$app = new \Slim\Slim();
# Initiate the Database PDO
$dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8';
$usr = DB_USER;
$pwd = DB_PASS;
$pdo = new \Slim\PDO\Database($dsn, $usr, $pwd);
# Initiate the Twig Template Engine
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem(TPL_PATH);
$twig = new Twig_Environment($loader, ['cache' => false]);
# ROUTES
## Admin Routes
## Frontend Routes
/**
* The user wants to go to a specific thread.
* @format www.example.com/boards/vg/thread/1
*/
$app->get('/boards/:board/thread/:threadId', function($board, $threadId) use ($app, $pdo, $twig) {
$boardHandler = new \MIBS\Board($pdo, $board);
$board_exists = $boardHandler->fetchBoardData();
$board_data = $boardHandler->getBoardInfo();
if($board_exists) {
$thread = new \MIBS\Thread($app, $pdo, $board, $threadId);
if($thread->getOriginalPost()) {
$data = $thread->getResponses()->getThreadArray();
echo $twig->render('thread.html', ['board_info' => $board_data, 'thread_id' => $threadId, 'thread_data' => $data]);
} else {
$app->notFound();
}
} else {
$app->notFound();
}
});
/**
* The user messed with the url, requested no Thread.
* Let's 404.
*/
$app->get('/boards/:board/thread', function($board) use($app) {
$app->notFound();
});
/**
* The user wants to go to page N of a Board.
* @format www.example.com/boards/2
*/
$app->get('/boards/:board/:page', function($board, $page) use ($app) {
# The Board (N) Page
});
/**
* The user wants to see the Board first page.
* @format www.example.com/board
*/
$app->get('/boards/:board', function($board) use ($app, $pdo, $twig) {
$boardHandler = new \MIBS\Board($pdo, $board);
$board_exists = $boardHandler->fetchBoardData();
$board_data = $boardHandler->getBoardInfo();
if($board_exists) {
$pageHandler = new \MIBS\Page($app, $pdo, $board, 1);
$threads_exist = $pageHandler->fetchThreads();
$threads_data = $pageHandler->getThreads();
$pagination = $pageHandler->createPageSelector();
echo $twig->render('page.html', ['board_data' => $board_data, 'threadsData' => $threads_data, 'pagination' => $pagination]);
} else {
$app->notFound();
}
});
/**
* The user messed with the url, no board is set.
* Let's call 404.
*/
$app->get('/boards', function() use ($app) {
$app->notFound();
});
/**
* The welcome page.
* @format www.example.com
*/
$app->get('/', function() use ($app) {
# The welcome page.
});
# Error handler
$app->error(function(\Exception $e) use($app) {
echo 'Erro 500.<br/>';
echo 'Mensagem: '.$e->getMessage();
echo 'Arquivo: '.$e->getFile();
echo 'Linha: '.$e->getLine();
});
# 404 Not Found handler
$app->notFound(function() use($app) {
echo '404 - Página não encontrada!'; # 404 - Page Not Found
});
# Start Slim Framework
$app->run();