Passed
Push — master ( 0e08b6...323a2a )
by axel
02:24
created

index.php (11 issues)

1
<?php
2
3
// Return json file
4
header('Content-type:application/json;charset=utf-8');
5
6
ini_set('max_execution_time', 0);
7
8
// Use the Mal-Scraper library
9
require 'vendor/autoload.php';
10
use MalScraper\MalScraper;
11
12
$myMalScraper = new MalScraper([
13
    'enable_cache' => true,
14
    'cache_time'   => 3600,
15
    'to_api'       => true,
16
]);
17
18
// Get the parameter
19
$method = isset($_GET['m']) ? $_GET['m'] : '';
20
21
$type = isset($_GET['t']) ? $_GET['t'] : '';
22
$id = isset($_GET['id']) ? $_GET['id'] : '';
23
24
$page = isset($_GET['p']) ? $_GET['p'] : 1;
25
26
$query = isset($_GET['q']) ? $_GET['q'] : '';
27
28
$year = isset($_GET['y']) ? $_GET['y'] : date('Y');
29
$season = isset($_GET['s']) ? $_GET['s'] : getCurrentSeason();
30
31
$user = isset($_GET['u']) ? $_GET['u'] : '';
32
$status = isset($_GET['st']) ? $_GET['st'] : 7;
33
34
// Call the requested method
35
switch ($method) {
36
37
// General Method ----------
38
    case 'info':
39
        if ($type && $id) {
40
            $result = $myMalScraper->getInfo($type, $id);
0 ignored issues
show
It seems like $id can also be of type string; however, parameter $id of MalScraper\MalScraper::getInfo() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
            $result = $myMalScraper->getInfo($type, /** @scrutinizer ignore-type */ $id);
Loading history...
41
            print_r($result);
42
        } else {
43
            print_r(paramError());
44
        }
45
        break;
46
    case 'character':
47
        if ($id) {
48
            $result = $myMalScraper->getCharacter($id);
0 ignored issues
show
It seems like $id can also be of type string; however, parameter $id of MalScraper\MalScraper::getCharacter() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            $result = $myMalScraper->getCharacter(/** @scrutinizer ignore-type */ $id);
Loading history...
49
            print_r($result);
50
        } else {
51
            print_r(paramError());
52
        }
53
        break;
54
    case 'people':
55
        if ($id) {
56
            $result = $myMalScraper->getPeople($id);
0 ignored issues
show
It seems like $id can also be of type string; however, parameter $id of MalScraper\MalScraper::getPeople() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
            $result = $myMalScraper->getPeople(/** @scrutinizer ignore-type */ $id);
Loading history...
57
            print_r($result);
58
        } else {
59
            print_r(paramError());
60
        }
61
        break;
62
    case 'studio-producer':
63
    case 'studioproducer':
64
        if ($id) {
65
            $result = $myMalScraper->getStudioProducer($id, $page);
0 ignored issues
show
It seems like $id can also be of type string; however, parameter $id of MalScraper\MalScraper::getStudioProducer() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            $result = $myMalScraper->getStudioProducer(/** @scrutinizer ignore-type */ $id, $page);
Loading history...
66
            print_r($result);
67
        } else {
68
            print_r(paramError());
69
        }
70
        break;
71
    case 'magazine':
72
        if ($id) {
73
            $result = $myMalScraper->getMagazine($id, $page);
0 ignored issues
show
It seems like $id can also be of type string; however, parameter $id of MalScraper\MalScraper::getMagazine() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            $result = $myMalScraper->getMagazine(/** @scrutinizer ignore-type */ $id, $page);
Loading history...
74
            print_r($result);
75
        } else {
76
            print_r(paramError());
77
        }
78
        break;
79
    case 'genre':
80
        if ($type && $id) {
81
            $result = $myMalScraper->getGenre($type, $id, $page);
0 ignored issues
show
It seems like $id can also be of type string; however, parameter $id of MalScraper\MalScraper::getGenre() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
            $result = $myMalScraper->getGenre($type, /** @scrutinizer ignore-type */ $id, $page);
Loading history...
82
            print_r($result);
83
        } else {
84
            print_r(paramError());
85
        }
86
        break;
87
88
// Additional Method ----------
89
    case 'character-staff':
90
    case 'characterstaff':
91
        if ($type && $id) {
92
            $result = $myMalScraper->getCharacterStaff($type, $id);
0 ignored issues
show
It seems like $id can also be of type string; however, parameter $id of MalScraper\MalScraper::getCharacterStaff() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
            $result = $myMalScraper->getCharacterStaff($type, /** @scrutinizer ignore-type */ $id);
Loading history...
93
            print_r($result);
94
        } else {
95
            print_r(paramError());
96
        }
97
        break;
98
    case 'stat':
99
        if ($type && $id) {
100
            $result = $myMalScraper->getStat($type, $id);
0 ignored issues
show
It seems like $id can also be of type string; however, parameter $id of MalScraper\MalScraper::getStat() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
            $result = $myMalScraper->getStat($type, /** @scrutinizer ignore-type */ $id);
Loading history...
101
            print_r($result);
102
        } else {
103
            print_r(paramError());
104
        }
105
        break;
106
    case 'picture':
107
        if ($type && $id) {
108
            $result = $myMalScraper->getPicture($type, $id);
0 ignored issues
show
It seems like $id can also be of type string; however, parameter $id of MalScraper\MalScraper::getPicture() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
            $result = $myMalScraper->getPicture($type, /** @scrutinizer ignore-type */ $id);
Loading history...
109
            print_r($result);
110
        } else {
111
            print_r(paramError());
112
        }
113
        break;
114
    case 'character-picture':
115
    case 'characterpicture':
116
        if ($id) {
117
            $result = $myMalScraper->getCharacterPicture($id);
0 ignored issues
show
It seems like $id can also be of type string; however, parameter $id of MalScraper\MalScraper::getCharacterPicture() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
            $result = $myMalScraper->getCharacterPicture(/** @scrutinizer ignore-type */ $id);
Loading history...
118
            print_r($result);
119
        } else {
120
            print_r(paramError());
121
        }
122
        break;
123
    case 'people-picture':
124
    case 'peoplepicture':
125
        if ($id) {
126
            $result = $myMalScraper->getPeoplePicture($id);
0 ignored issues
show
It seems like $id can also be of type string; however, parameter $id of MalScraper\MalScraper::getPeoplePicture() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

126
            $result = $myMalScraper->getPeoplePicture(/** @scrutinizer ignore-type */ $id);
Loading history...
127
            print_r($result);
128
        } else {
129
            print_r(paramError());
130
        }
131
        break;
132
133
// List ----------
134
    case 'all-anime-genre':
135
    case 'allanimegenre':
136
        $result = $myMalScraper->getAllAnimeGenre();
137
        print_r($result);
138
        break;
139
    case 'all-manga-genre':
140
    case 'allmangagenre':
141
        $result = $myMalScraper->getAllMangaGenre();
142
        print_r($result);
143
        break;
144
    case 'all-studio-producer':
145
    case 'allstudioproducer':
146
        $result = $myMalScraper->getAllStudioProducer();
147
        print_r($result);
148
        break;
149
    case 'all-magazine':
150
    case 'allmagazine':
151
        $result = $myMalScraper->getAllMagazine();
152
        print_r($result);
153
        break;
154
155
// Search ----------
156
    case 'search-anime':
157
    case 'searchanime':
158
        if ($query) {
159
            $result = $myMalScraper->searchAnime($query, $page);
160
            print_r($result);
161
        } else {
162
            print_r(paramError());
163
        }
164
        break;
165
    case 'search-manga':
166
    case 'searchmanga':
167
        if ($query) {
168
            $result = $myMalScraper->searchManga($query, $page);
169
            print_r($result);
170
        } else {
171
            print_r(paramError());
172
        }
173
        break;
174
    case 'search-character':
175
    case 'searchcharacter':
176
        if ($query) {
177
            $result = $myMalScraper->searchCharacter($query, $page);
178
            print_r($result);
179
        } else {
180
            print_r(paramError());
181
        }
182
        break;
183
    case 'search-people':
184
    case 'searchpeople':
185
        if ($query) {
186
            $result = $myMalScraper->searchPeople($query, $page);
187
            print_r($result);
188
        } else {
189
            print_r(paramError());
190
        }
191
        break;
192
    case 'search-user':
193
    case 'searchuser':
194
        if ($query) {
195
            $result = $myMalScraper->searchUser($query, $page);
196
            print_r($result);
197
        } else {
198
            print_r(paramError());
199
        }
200
        break;
201
202
// Seasonal ----------
203
    case 'season':
204
        $result = $myMalScraper->getSeason($year, $season);
205
        print_r($result);
206
        break;
207
208
// Top List ----------
209
    case 'top-anime':
210
    case 'topanime':
211
        $type = $type ? $type : 0;
212
        $result = $myMalScraper->getTopAnime($type, $page);
213
        print_r($result);
214
        break;
215
    case 'top-manga':
216
    case 'topmanga':
217
        $type = $type ? $type : 0;
218
        $result = $myMalScraper->getTopManga($type, $page);
219
        print_r($result);
220
        break;
221
    case 'top-character':
222
    case 'topcharacter':
223
        $result = $myMalScraper->getTopCharacter($page);
224
        print_r($result);
225
        break;
226
    case 'top-people':
227
    case 'toppeople':
228
        $result = $myMalScraper->getTopPeople($page);
229
        print_r($result);
230
        break;
231
232
// User ----------
233
    case 'user':
234
        if ($user) {
235
            $result = $myMalScraper->getUser($user);
236
            print_r($result);
237
        } else {
238
            print_r(paramError());
239
        }
240
        break;
241
    case 'user-friend':
242
    case 'userfriend':
243
        if ($user) {
244
            $result = $myMalScraper->getUserFriend($user);
245
            print_r($result);
246
        } else {
247
            print_r(paramError());
248
        }
249
        break;
250
    case 'user-history':
251
    case 'userhistory':
252
        if ($user) {
253
            $result = $myMalScraper->getUserHistory($user, $type);
254
            print_r($result);
255
        } else {
256
            print_r(paramError());
257
        }
258
        break;
259
    case 'user-list':
260
    case 'userlist':
261
        if ($user) {
262
            $type = $type ? $type : 'anime';
263
            $result = $myMalScraper->getUserList($user, $type, $status);
264
            print_r($result);
265
        } else {
266
            print_r(paramError());
267
        }
268
        break;
269
    case 'user-cover':
270
    case 'usercover':
271
        if ($user) {
272
            header('Content-Type: text/css');
273
274
            $type = $type ? $type : 'anime';
275
            $query = $query ? $query : false;
276
277
            $result = $myMalScraper->getUserCover($user, $type, $query);
278
            $result = json_decode($result, true);
279
            $result = $result['data'];
280
281
            print_r($result);
282
        } else {
283
            print_r(paramError());
284
        }
285
        break;
286
287
// Secret ----------
288
    case 'auto-cover':
289
        header('Content-Type: text/css');
290
291
        $user_url = $_SERVER['HTTP_REFERER'];
292
        $user_url = str_replace('https://myanimelist.net', '', $user_url);
293
294
        preg_match("/\/.+(list)\//", $user_url, $user_type);
295
        $type = str_replace(['/', 'list'], '', $user_type[0]);
296
297
        $user_url = str_replace(['/animelist/', '/mangalist/'], '', $user_url);
298
        $user_url = preg_replace('/\?+.+/', '', $user_url);
299
300
        $user = $user_url;
301
        $type = $type ? $type : 'anime';
302
        $query = $query ? $query : false;
303
304
        $result = $myMalScraper->getUserCover($user, $type, $query);
305
        $result = json_decode($result, true);
306
        $result = $result['data'];
307
308
        print_r($result);
309
        break;
310
    default:
311
        print_r(paramError(true));
312
        break;
313
}
314
315
// Return error parameter
316
function paramError($a = false)
317
{
318
    $result = [];
319
    if ($a) {
320
        header('HTTP/1.1 404');
321
        $result['status'] = 404;
322
        $result['status_message'] = 'Method not found';
323
        $result['data'] = [];
324
    } else {
325
        header('HTTP/1.1 400');
326
        $result['status'] = 400;
327
        $result['status_message'] = 'Bad Request';
328
        $result['data'] = [];
329
    }
330
331
    return json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
332
}
333
334
// Get current season (spring,summer,fall,winter)
335
function getCurrentSeason()
336
{
337
    $currentMonth = date('m');
338
339
    if ($currentMonth >= '01' && $currentMonth < '04') {
340
        return 'winter';
341
    }
342
    if ($currentMonth >= '04' && $currentMonth < '07') {
343
        return 'spring';
344
    }
345
    if ($currentMonth >= '07' && $currentMonth < '10') {
346
        return 'summer';
347
    }
348
349
    return 'autumn';
350
}
351