This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
/
chess.php
251 lines (186 loc) · 6.01 KB
/
chess.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
use Phalcon\Mvc\Micro;
use Phalcon\Mvc\View;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\View\Engine\Volt;
require('algorithm.php');
session_start();
ini_set('display_errors',1);
// ******************** //
// **** EXCEPTIONS **** //
// ******************** //
set_exception_handler(function(Exception $e){
switch (get_class($e)) {
case 'TypeError':
echo 'Type is not mismatch !';
break;
default:
echo $e->getMessage();
break;
}
echo '<br>Line'.$e->getLine();
});
// ******************** //
// **** INITIALIZE **** //
// ******************** //
$loader = new \Phalcon\Loader();
$loader->registerDirs(
[
__DIR__ . '/models/',
]
)->register();
$di = new FactoryDefault();
$di->set(
'db',
function () {
$db = new \Phalcon\Db\Adapter\Pdo\Mysql(
[
'host' => 'localhost',
'username' => 'chess',
'password' => 'Yq2B2owz0kAycDEA',
'dbname' => 'chess',
]
);
$db->query('SET NAMES UTF8;')->execute();
return $db;
}
);
$app = new Micro($di);
// set alert css classes
$app->flashSession->setCssClasses(['error' => 'alert alert-danger', 'warning' => 'alert alert-warning']);
$app->setModelBinder(new \Phalcon\Mvc\Model\Binder());
// view member
$app['view'] = function () {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir("./templates/");
$view->registerEngines(
[
".volt" => "Phalcon\\Mvc\\View\\Engine\\Volt"
]
);
return $view;
};
// **** BEFORE **** //
$app->before(
function () use ($app) {
if (NULL === $app->session->get('auth') && $app->request->get('_url') != '/login' ) {
$app->response->redirect('chess.php?_url=/login', true)->sendHeaders();
return false;
}
return true;
}
);
// **** INDEX **** //
$app->map('/index',
function () use ($app) {
$app->view->render('index', 'index');
}
);
// **** USER LOGIN **** //
$app->map('/login',
function () use ($app) {
if($app->session->get('auth') != NULL)
$app->response->redirect('chess.php?_url=/boards', true)->sendHeaders();
if($app->request->isPost())
{
// email validation
if(!filter_var($app->request->getPost('email'), FILTER_VALIDATE_EMAIL))
{
$app->flashSession->error('Email is invalid!');
$app->response->redirect('chess.php?_url=/login', true)->sendHeaders();
return false;
}
// passwords has to be greater than 5 characters
if(strlen($app->request->getPost('password')) < 6)
{
$app->flashSession->error('Password is less than 6 character!');
$app->response->redirect('chess.php?_url=/login', true)->sendHeaders();
return false;
}
$user = Users::findFirst(['email = ?1', 'bind' => [ 1 => $app->request->getPost('email') ]]);
// get user by email
if(!$user)
{
$app->flashSession->error('There is no user by registered with this email!');
$app->response->redirect('chess.php?_url=/login', true)->sendHeaders();
return false;
}
// check password
if(!$app->security->checkHash($app->request->getPost('password'), $user->password))
{
$app->flashSession->error('Password that you entered is invalid!');
$app->response->redirect('chess.php?_url=/login', true)->sendHeaders();
return false;
}
// user authorization
$app->session->set('auth', $user->toArray());
// go to boards list
$app->response->redirect('chess.php?_url=/boards', true)->sendHeaders();
}else{
$app->view->render('login', 'index');
}
}
);
// **** LOGOUT **** //
$app->map('/logout',
function () use ($app) {
$app->session->remove('auth');
$app->response->redirect('chess.php?_url=/login', true)->sendHeaders();
}
);
// **** FETCH **** //
$app->map('/fetch/{boardID}',
function (int $boardID) use ($app) {
if(($chess = new Algorithm($boardID)) == false)
return false;
if($app->request->isPost())
$chess->move(json_decode($app->request->getPost('cpos')), json_decode($app->request->getPost('tpos')));
echo $chess->output();
}
);
// **** SINGLE BOARD **** //
$app->map('/boards/{id}',
function (int $id) use ($app) {
$board = Boards::findFirst($id);
$app->view->render('boards','single');
}
);
// **** BOARDS **** //
$app->map('/boards',
function () use ($app) {
$boards = Boards::find();
$app->view->boards = $boards;
$app->view->render('boards','index');
}
);
// **** CREATE BOARD **** //
$app->post('/create',
function () use ($app) {
if($app->request->isPost())
{
}
$board = new Boards();
$boards = Boards::find();
$app->view->boards = $boards;
$app->view->render('boards','index');
}
);
// **** NOT FOUND **** //
$app->notFound(function () use ($app) {
$app->response->setStatusCode(404, "Not Found")->sendHeaders();
echo 'Aradığınız sayfa bulunamadı.';
});
// **** APPLICATION ERROR **** //
$app->error(
function (Exception $exception) {
echo json_encode(
[
'code' => $exception->getCode(),
'status' => 'error',
'message' => $exception->getMessage(),
'line' => $exception->getLine()
]
);
}
);
$app->handle();