-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaira.php
164 lines (162 loc) · 6.21 KB
/
Laira.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
<?php
/**
* Laira API Class
* © Hsilamot 2014-2020
* developed by Hidalgo Rionda
*
* +----------------------------------------------------------------------+
* | Laira v 1.0.1 |
* | released under Creative Commons BY-NC-SA 3.0 |
* | http://creativecommons.org/licenses/by-nc-sa/3.0/ |
* +----------------------------------------------------------------------+
*
* +----------------------------------------------------------------------+
* | changelog |
* | v4.0.1 (Hsilamot) Rework on 2020 |
* +----------------------------------------------------------------------+
*
* @access public
* @author Hsilamot <php@hsilamot.com>
* @license http://creativecommons.org/licenses/by-nc-sa/3.0/
* @version 1.0.1
*/
namespace Tomalish;
use Firebase\JWT as JWT;
use \JsonException as JsonException;
class Laira {
/**
* This contains the HTTP Response code to end the call
* @access private
* @var int
*/
private $http_code;
/**
* This contains an array with the Human readable messages to send back to the client
* @access private
* @var array
*/
private $messages;
/**
* This contains an array with the response to send to the client
* @access private
* @var array
*/
private $payload;
/**
* This tells the client if the result was successful or not
* @access public
* @var result
*/
public $result;
/**
* This contains an array with the API Version
* @access public
* @var array
*/
public $version;
public function __construct() {
$this->http_code = false; /* The HTTP Result Code */
$this->result = false; /* If the request was successful */
$this->messages = array(); /* This contains an array of messages to show */
$this->version = array(0,0,0); /* The Version of the API */
$this->payload = array(); /* Payload Contents */
return true;
}
public function addMessage($type='info',$msg='') {
$this->messages[] = array('type'=>$type,'message'=>$msg);
return true;
}
public function addPayload($name='',$content='') {
$this->payload[$name] = $content;
return true;
}
public function end($code=false,$msg=false,$return=false) {
/* PREFLIGHT CHECK */
header('Access-Control-Allow-Origin: *'); /* Dejamos que las solicitudes vengan de cualquier dominio */
header("Access-Control-Allow-Methods: POST, GET, DELETE, PUT, PATCH, OPTIONS"); /* Aceptamos los metodos */
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, appid, version'); /* Permitimos cabeceras */
if ($code) {
$this->http_code = $code;
}
if ($msg) {
$this->msg('unknown',$msg);
}
if ($return) {
$this->result = true;
}
if (isset($_SERVER['SERVER_PROTOCOL'])) {
$server_protocol = $_SERVER['SERVER_PROTOCOL'];
} else {
$server_protocol = 'CLI';
}
if (isset($_SERVER['REQUEST_METHOD'])) {
$request_method = $_SERVER['REQUEST_METHOD'];
} else {
$request_method = 'CLI';
}
if($request_method == 'OPTIONS'){
/* Estamos en preflight, respondemos apropiadamente... */
header($server_protocol.' 200 OK');
exit(0);
}
switch ($this->http_code) {
case 200: header($server_protocol.' 200 OK'); break;
case 400: header($server_protocol.' 400 Bad Request'); break;
case 401: header($server_protocol.' 401 Unauthorized'); break;
case 402: header($server_protocol.' 402 Payment Required'); break;
case 403: header($server_protocol.' 403 Forbidden'); break;
case 404: header($server_protocol.' 404 Not Found'); break;
case 405: header($server_protocol.' 405 Method Not Allowed'); break;
case 406: header($server_protocol.' 406 Not Acceptable'); break;
case 407: header($server_protocol.' 407 Proxy Authentication Required'); break;
case 408: header($server_protocol.' 408 Request Timeout'); break;
case 409: header($server_protocol.' 409 Conflict'); break;
case 410: header($server_protocol.' 410 Gone'); break;
case 412: header($server_protocol.' 412 Precondition Failed'); break;
case 413: header($server_protocol.' 413 Request Entity Too Large'); break;
case 415: header($server_protocol.' 415 Unsupported Media Type'); break;
case 418: header($server_protocol.' 418 I\'m a teapot'); break;
case 420: header($server_protocol.' 420 Enhance Your Calm'); break;
case 423: header($server_protocol.' 423 Locked'); break;
case 428: header($server_protocol.' 428 Precondition Required'); break;
case 429: header($server_protocol.' 429 Too Many Requests'); break;
case 451: header($server_protocol.' 451 Unavailable For Legal Reasons'); break;
case 500: header($server_protocol.' 500 Internal Server Error'); break;
case 501: header($server_protocol.' 501 Not Implemented'); break;
case 502: header($server_protocol.' 502 Bad Gateway'); break;
case 503: header($server_protocol.' 503 Service Unavailable'); break;
case 504: header($server_protocol.' 504 Gateway Timeout'); break;
case 505: header($server_protocol.' 505 HTTP Version Not Supported'); break;
case 507: header($server_protocol.' 507 Insufficient Storage'); break;
case 508: header($server_protocol.' 508 Loop Detected'); break;
case 509: header($server_protocol.' 509 Bandwidth Limit Exceeded'); break;
case 510: header($server_protocol.' 510 Not Extended'); break;
default: header($server_protocol.' 500 Internal Server Error'); break;
}
header('Content-Type: application/json'); // Las respuestas se proporcionan en JSON
$payload = $this->payload;
$payload['messages'] = $this->messages;
$payload['result'] = $this->result;
$payload['version'] = $this->version;
try {
$options = JSON_THROW_ON_ERROR;
if ($server_protocol=='CLI') {
$options &= JSON_PRETTY_PRINT;
}
$string = json_encode($payload,$options);
} catch (JsonException $e) {
$msg = 'JSON Exception Message: '.$e->getMessage();
trigger_error($msg,E_USER_NOTICE);
trigger_error('Object tried to encode: '.print_r($payload,true),E_USER_NOTICE);
$this->result = false;
$this->addMessage('error',$msg);
$this->payload = array();
$this->end();
}
echo $string;
if ($server_protocol=='CLI') {
echo chr(10);
}
exit(0);
}
}