-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
291 lines (175 loc) · 6.25 KB
/
index.php
File metadata and controls
291 lines (175 loc) · 6.25 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
<?php //reSTATE a restful api mini framework. Written by Bruno Trombi. Released under the terms of the MIT license
error_reporting( E_ERROR );
function handleError($errno, $errstr,$error_file,$error_line ,$error_context)
{
global $test;
//echo "<br>ErrorLevel:$errno<hr>";
//echo "<b>Errore:</b> [$errno] $errstr - $error_file:$error_line\n<br>";
//var_export( $error_context );
//echo "<br />";
//echo "Terminating PHP Script";
global $data;
$data['msg']="errore amici miei";
$sData= restReturn( 17,$data ,$errno,$errstr,$error_file,$error_line );
$file = "../jErrors.txt";
file_put_contents($file, "\n" .$sData ,FILE_APPEND);
// the message
//$msg = "First line of text\nSecond line of text";
// use wordwrap() if lines are longer than 70 characters
//$msg = wordwrap($msg,70);
// send email, if not on localhost
if( $_SERVER['SERVER_NAME'] != 'localhost' )
mail("bruno.trombi@gmail.com","bug:${data['url']}",$sData);
die('');
}
//set error handler
set_error_handler("handleError");
//json pretty print, se il php ce lo ha!
function json_encode_pretty($data)
{
//questo paramtro serve a specificare, se vogliomo disabilitare il pretty print, per un piu compatto formato dati
$bJsonCompact= false;
if( array_key_exists( 'bJsonCompact' ,$data["input"] ) )
{
$bJsonCompact= $data["input"]["bJsonCompact"];
}
//se php veccio, non abbiamo pretty_print
if( intval( PHP_VERSION_ID ) > 50400 )
if( $bJsonCompact == false )
return json_encode($data ,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
else
return json_encode($data ,JSON_UNESCAPED_SLASHES);
//tophsot ha una versione piu vecchia e non supporta il pretty print
else
return json_encode($data );
}
//start the buffer control, to prevent dirt into output
/*
ob_start();
serviamo la risposta compressa, se non puo, attiviamo il buffer normalmente
se e' riuscito ad attivare il buffer, setta l'header a gzip
*/
if(!ob_start("ob_gzhandler")) ob_start();
else
header('Content-Encoding:gzip');
//versione
// PHP_VERSION_ID is available as of PHP 5.2.7, if our
// version is lower than that, then emulate it
if (!defined('PHP_VERSION_ID')) {
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}
//$ver= PHP_VERSION_ID;
//$table= basename( getcwd() );
//global $table;
//echo "table:$table";
$serverName= $_SERVER['SERVER_NAME'];
$_METHOD= strtolower( $_SERVER['REQUEST_METHOD'] );
//$pathOrigin= getcwd();
//carichiamo il modello, ovvero l'ogetto che interagisce col db
//chdir("..");
//scopriamo su che tabella dobbiamo lavorare: dal .table.field o dal penultimo ultimo elemento dell'url
//questo permette di poter diporre liberamente le api rest
//$table con il valore di .table.field (se esite)
if( file_exists( ".table.field" ) )
$table= chop( file_get_contents( ".table.field" ) );
//se $table non ancora valorizzato, utilizza il nome della directory di livello superiore,
//ex: da /pippo/pluto/antani/ ritorna pluto
if( empty( $table ) )
$table= basename( getcwd() );
require_once( ".Model.php" );
$db = new Model( $table );
if(!$db) // fail fast: se qualcosa va male, esci subito
{
echo $db->lastErrorMsg();
}
//chdir( $pathOrigin );
//exit(3);
//load requested method
//fixme cosa fare se il metodo manca
$data= array();
$data["method"]= $_METHOD;
$data["url"]= $serverName. dirname( $_SERVER["SCRIPT_NAME"] ) .'/'; //a questo va tolto alla fine 'index.php'
//$data["cwd"]= getcwd();
//ricostruiamoci la url
$url= array(
'http://',
$_SERVER['SERVER_NAME']
);
if( $_SERVER['SERVER_PORT'] != 80 )
$url[]= ':'.$_SERVER['SERVER_PORT'];
$url[]=$_SERVER['REQUEST_URI'];
$url= implode( $url,'' );
$data["url"]= $url;
$data["tsRequest"]= $_SERVER['REQUEST_TIME_FLOAT'];
$data["api-ver"]= 0.3;
$sQuery= $_SERVER["QUERY_STRING"];
$sBody= file_get_contents("php://input");
//per supportare internet explorer
//fixme supponiamo che i dati siano sempre urlencoded, se $_method==get via url, altrimenti nel body
if( !in_array( $_METHOD ,array( "get","post","put","delete","options" ) ) )
$db->restDie( -99 ,"methods allowed must be get|post|put|delete" );
//la get prende i parametri da get, altrimenti da body
if( $_METHOD == "get" )
parse_str($sQuery, $data["input"] );
else
parse_str($sBody, $data["input"] );
require_once( ".allowed.${_METHOD}.php" ); //files starting with . are hidden in linux and not served outside.
$f= "${_METHOD}";
$data["result"]= $f( $db ,$table ,$data["input"] );
//$data= $f( $data["input"] );
/*!!!!
se ho catturato output tramite ob, significa che abbiamo errore server, per cui $data["status"] =1
se parametri sbagliati, status negativo
*/
//$data["msg"]= $db["msg"];
//$data["status"]= $db["status"];
//return $db->restDie( 99 ,"aaa" );
$tmp= $db->status;
$bufferOutput = ob_get_contents();
ob_clean();
if( strlen($bufferOutput) )
{
$tmp=1; //roba sul buffer
$db->msg= "errore: roba sul buffer";
$data["bufferOutput"]= $bufferOutput;
}
restReturn( $tmp,$data );
function restReturn( $iTmp,$data ,$errono=null,$errstr=null,$error_file=null,$error_line=null )
{
global $db;
//set header
if( $iTmp == 0 ) header('HTTP/1.1 200 OK');
if( $iTmp < 0 ) header('HTTP/1.1 400 Bad Request');
if( $iTmp > 0 ) header('HTTP/1.1 500 Internal Server Error');
//set header mime type as json utf8
header('Content-Type: application/json; charset=UTF-8');
//accept cors call
header("Access-Control-Allow-Origin: *");
//no cache
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.
//con cache settata, da testare
//header('Cache-Control: private'); // HTTP 1.1.
//header('Expires: 999'); // Proxies.
//header('Etags: tie'); // Proxies.
$data["status"]= $iTmp; //msg errore per il programmatore!
$data["msg"]= $db->msg; //!!! utilizzare restdie e' meglio, e permette di eliminare questi parametri zozzi
if( $errono>0 ) $data["errono"]= $errono;
if( $errstr ) $data["errstr"]= $errstr;
if( $error_file ) $data["error_file"]= $error_file;
if( $error_line ) $data["error_line"]= $error_line;
/*
<0 500=>'500 Internal Server Error',
0= 200=>'200 OK',
>0 400=>'400 Bad Request',
*/
$r= json_encode_pretty($data);
echo $r;
return $r;
//send the data buffer to output
//ob_end_flush();
//echo "prima ha inviato:$bufferOutput\n";
}
?>