This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
/
api.php
79 lines (69 loc) · 1.83 KB
/
api.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
<?php
/**
* This is the main front-end entry point for scripts.
*
* All API responses start here.
*
* Example:
*
* ./api.php (same as: api.php?format=json&action=info)
* ./api.php?action=scores
* ./api.php?format=php&action=job&item=1
* ./api.php?format=jsonp&action=swarmstate
*
* @author Timo Tijhof
* @since 1.0.0
* @package TestSwarm
*/
// Valid entry point
define( 'SWARM_ENTRY', 'API' );
header( 'X-Robots-Tag: noindex,nofollow', true );
require_once __DIR__ . '/inc/init.php';
$action = $swarmContext->getRequest()->getVal( 'action' );
// getVal will only fallback to "help" if "action" is not set.
// Also fallback if it was set to an empty string.
if ( $action ) {
$defaultFormat = 'json';
} else {
$action = 'apihelp';
$defaultFormat = 'debug';
}
$format = $swarmContext->getRequest()->getVal( 'format', $defaultFormat );
$className = ucfirst( $action ) . 'Action';
$className = class_exists( $className ) ? $className : null;
if ( !Api::isGreyFormat( $format ) ) {
session_start();
}
$api = Api::newFromContext( $swarmContext );
if ( $className ) {
try {
$actionObj = $className::newFromContext( $swarmContext );
$actionObj->doAction();
$response = array();
if ( $actionObj->getError() ) {
$response['error'] = $actionObj->getError();
}
if ( $actionObj->getData() ) {
$response[$action] = $actionObj->getData();
}
$api->setAction( $action );
$api->setFormat( $format );
} catch ( Exception $e ) {
$response = array(
'error' => array(
'code' => 'internal-error',
'info' => 'An internal error occurred. Action could not be performed. Error message:' . "\n" . $e->getMessage(),
),
);
}
} else {
$response = array(
'error' => array(
'code' => 'invalid-input',
'info' => "Action `$action` does not exist",
),
);
}
$api->setResponse( $response );
$api->output();
exit;