-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi.php
More file actions
313 lines (268 loc) · 8.08 KB
/
api.php
File metadata and controls
313 lines (268 loc) · 8.08 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/*
* ======================================
* Configurações
* ======================================
*/
//Charset
header("Content-Type: text/html; charset=UTF-8", true);
//Test Server?
define('TEST_SERVER',true);
//Local Database
if(TEST_SERVER):
define('MYSQL_URL', '127.0.0.1');
define('MYSQL_USERNAME', 'root');
define('MYSQL_PASSWORD', '');
define('MYSQL_DBNAME', 'dev.meutroco');
define('MYSQL_PORT', '');
//Web Database
else:
define('MYSQL_URL', 'localhost');
define('MYSQL_USERNAME', 'letsr890_admin');
define('MYSQL_PASSWORD', '0x15ox}R#_Oy');
define('MYSQL_DBNAME', 'letsr890_meutroco');
define('MYSQL_PORT', '');
endif;
//Messages CONFIG
define('LANGUAGE', 'ptBR');
//Other Stuff (PIR CONNECTION) //TODO: REMOVE
$db_table_prefix = "";
$websiteName = "";
$websiteUrl = ""; //including trailing slash
//Do you wish UserPie to send out emails for confirmation of registration?
//We recommend this be set to true to prevent spam bots.
//False = instant activation
//If this variable is falses the resend-activation file not work.
$emailActivation = false;
$resend_activation_threshold = 1; //In hours, how long before UserPie will allow a user to request another account activation email //Set to 0 to remove threshold
$emailAddress = "noreply@meutroco.com.br"; //Tagged onto our outgoing emails
$emailDate = date("l \\t\h\e jS"); //Date format used on email's
$mail_templates_dir = "models/mail-templates/"; //Directory where txt files are stored for the email templates.
$default_hooks = array("#WEBSITENAME#","#WEBSITEURL#","#DATE#");
$default_replace = array($websiteName,$websiteUrl,$emailDate);
$debug_mode = true; //Display explicit error messages?
$remember_me_length = "1wk"; //Remember me - amount of time to remain logged in.
//Locale
setlocale(LC_MONETARY, 'pt_BR');
date_default_timezone_set('America/Sao_Paulo');
/*
* ======================================
* Required Files
* ======================================
*/
require_once('api.utils.php');
require_once('transactions/class.php');
require_once('users/class.php');
require_once('tags/class.php');
require_once('accounts/class.php');
require_once('token/class.php');
/*
* ======================================
* Rest Request
* ======================================
*/
class RestRequest {
private $request_vars;
private $data;
private $http_accept;
private $method;
/* Construct */
public function __construct() {
$this->request_vars = array();
$this->data = '';
$this->http_accept = 'json'/*(strpos($_SERVER['HTTP_ACCEPT'], 'json')) ? 'json' : 'xml'*/;
$this->method = 'get';
}
/* Set Data */
public function setData($data) {
$this->data = $data;
}
/* Get Data */
public function getData() {
return $this->data;
}
/* Set Method */
public function setMethod($method) {
$this->method = $method;
}
/* Get Method */
public function getMethod() {
return $this->method;
}
/* Set Request Variables */
public function setRequestVars($request_vars) {
$this->request_vars = $request_vars;
}
/* Get Request Variables */
public function getRequestVars() {
return $this->request_vars;
}
/* Get HTTP Accept */
public function getHttpAccept() {
return $this->http_accept;
}
}
/*
* ======================================
* Rest Utils
* ======================================
*/
class RestUtils {
/* */
public static function processRequest() {
//Get our request method and data storage var
$request_method = strtolower($_SERVER['REQUEST_METHOD']);
$return_obj = new RestRequest();
$data = array();
//Verify request method
switch ($request_method) {
//GET
case 'get':
$data = $_GET;
break;
//POST
case 'post':
$data = $_POST;
break;
//PUT
case 'put':
parse_str(file_get_contents('php://input'), $put_vars);
$data = $put_vars;
break;
//DELETE
case 'delete':
$data = $_GET;
break;
//OTHERS
default:
$data = $_GET;
break;
}
//Store the method
if(isset($_SERVER['HTTP_METHOD']))
$request_method = strtolower($_SERVER['HTTP_METHOD']);
elseif(isset($data['method']))
$request_method = strtolower($data['method']);
$return_obj->setMethod($request_method);
//Store the data
$return_obj->setRequestVars($data);
//Store the JSON object
if(isset($data['data'])) {
$return_obj->setData(json_decode($data['data']));
} else {
$return_obj->setData($data);
}
//Return
return $return_obj;
}
/* Send Response */
public static function sendResponse($status = 200, $body = '', $content_type = 'text/html') {
//Set status and content type
$status_header = 'HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status);
header($status_header);
header('Content-type: ' . $content_type);
//Body
if($body != '') {
if(is_array($body))
echo json_encode($body);
else
echo $body;
exit();
} else {
//Server Signature
$signature = ($_SERVER['SERVER_SIGNATURE'] == '') ? $_SERVER['SERVER_SOFTWARE'] . ' Server at ' . $_SERVER['SERVER_NAME'] . ' Port ' . $_SERVER['SERVER_PORT'] : $_SERVER['SERVER_SIGNATURE'];
//Body
$body = '{"message": "'.RestUtils::getStatusCodeMessage($status).'", "code":"'.$status.'", "signature":"'.$signature.'"}';
/*
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-UTF-8">
<title>' . $status . ' ' . RestUtils::getStatusCodeMessage($status) . '</title>
</head>
<body>
<h1>' . RestUtils::getStatusCodeMessage($status) . '</h1>
<p>' . $message . '</p>
<hr />
<address>' . $signature . '</address>
</body>
</html>'; */
echo $body;
exit;
}
}
/* Get Status Code Message */
public static function getStatusCodeMessage($status) {
$codes = parse_ini_file("statusCode.ini");
return (isset($codes[$status])) ? $codes[$status] : '';
}
}
/*
* ======================================
* MySQL Connection
* ======================================
*/
class DataBase {
//Variables
private $conn = NULL;
public $result = NULL;
public $insertId = NULL;
//Connect
public function connect() {
$this->conn = mysql_connect(MYSQL_URL,MYSQL_USERNAME,MYSQL_PASSWORD) or die(RestUtils::sendResponse('500',array('data' => 'db_connect', 'message' => 'Ops! Erro ao conectar ao BD.', 'error' => mysql_error())));
return mysql_select_db(MYSQL_DBNAME, $this->conn);
}
//Protect Query
private function protect($q){
return $q;
}
//Close
public function close() {
mysql_close($this->conn);
}
//Get Query
public function query($query){
$this->connect();
$sql = $query;
$query=mysql_query($this->protect($sql))or die(RestUtils::sendResponse('500',array('data' => 'db_query', 'message' => 'Ops! Erro ao consultar BD.', 'error' => mysql_error())));
$this->result = $query;
$this->insertId = mysql_insert_id();
return $query;
}
}
/*
* ======================================
* User Profile
* ======================================
*/
class CurrentUser {
public static function getId() {
//Get Token
if(isset($_GET['token'])):
//Verify token
$token = new Token;
if(!$token->verify($_GET['token'])):
//Connect
$sql = new DataBase;
$sql->connect();
$sql->query("
SELECT DISTINCT *
FROM token
WHERE token = '".$_GET['token']."'
");
//Data
while($data = mysql_fetch_array($sql->result)):
return $data['profile_id'];
break;
endwhile;
else:
RestUtils::sendResponse('400',array('data' => 'token', 'message' => 'A verificação do token falhou.'));
exit;
endif;
else:
RestUtils::sendResponse('412',array('data' => 'token', 'message' => 'O token não foi passado pela URL.'));
exit;
endif;
}
}
?>