-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.php
More file actions
102 lines (83 loc) · 2.92 KB
/
index.php
File metadata and controls
102 lines (83 loc) · 2.92 KB
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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$request = Request::createFromGlobals();
$path = $request->getPathInfo();
logMsg(
sprintf('Got request %s', json_encode($request)),
'request'
);
try {
if (preg_match('/contents/', $path) !== 0) {
if ($request->query->get('term') !== null) {
$term = $request->query->get('term');
}
header('Content-Type: application/json; charset=utf-8');
$response = new JsonResponse(['data' => (new \Squiz\PhpCodeExam\Searcher())->execute($term, $type = 'content')], Response::HTTP_OK);
logMsg(
sprintf('Sent response %s', $response->getContent()),
'response'
);
$response->send();
exit(0);
}
if (preg_match('/tags/', $path) !== 0) {
if ($request->query->get('term') != null) {
$term = $request->query->get('term');
}
header('Content-Type: application/json; charset=utf-8');
$response = new JsonResponse(['data' => (new \Squiz\PhpCodeExam\Searcher())->execute($term, 'tags')], Response::HTTP_OK);
$response->send();
logMsg(
sprintf('Sent response %s', $response->getContent()),
'response'
);
exit(0);
}
if (preg_match('/pages/', $path) !== 0) {
$paths = explode('/', $path);
$id = $paths[2];
header('Content-Type: application/json; charset=utf-8');
$response = new JsonResponse(['data' => (new \Squiz\PhpCodeExam\Searcher())->getPageById($id)], Response::HTTP_PARTIAL_CONTENT);
$response->send();
die();
}
$searcher = new \Squiz\PhpCodeExam\Searcher();
$data = $searcher->allData;
$null = NULL;
$response = empty($data) ? new Response($null, Response::HTTP_NO_CONTENT) : new JsonResponse($data, Response::HTTP_ACCEPTED);
error_log(
sprintf('Sent response %s', $response->getContent()),
0,
__DIR__ . '/logs/response.log'
);
$response->send();
} catch (Exception $ex) {
new JsonResponse(['exception' => $ex->getMessage()], Response::HTTP_BAD_REQUEST);
logMsg(
sprintf('%s: %s', $ex->getCode(), $ex->getMessage())
);
}
$response = new JsonResponse(['error' => 'Failed to get pages'], Response::HTTP_INTERNAL_SERVER_ERROR);
$response->send();
logMsg(
sprintf('Failed to get pages'),
'failure'
);
function logMsg($message, $type = 'error')
{
$logger = new \Squiz\PhpCodeExam\Logger();
switch ($type) {
case 'error':
$file = __DIR__ . '/logs/error.log';
case 'request':
$file = __DIR__ . '/logs/request.log';
case 'response':
$file = __DIR__ . '/logs/response.log';
default:
$file = __DIR__ . '/logs/log.log';
}
$logger->log($message, $file);
}