@@ -43,13 +43,19 @@ class PHPRestSQL {
43
43
* @var str
44
44
*/
45
45
var $ method = 'GET ' ;
46
-
46
+
47
47
/**
48
48
* The HTTP request data sent (if any).
49
49
* @var str
50
50
*/
51
51
var $ requestData = NULL ;
52
-
52
+
53
+ /**
54
+ * The URL extension stripped off of the request URL
55
+ * @var str
56
+ */
57
+ var $ extension = NULL ;
58
+
53
59
/**
54
60
* The database table to query.
55
61
* @var str
@@ -82,7 +88,7 @@ function PHPRestSQL($iniFile = 'phprestsql.ini') {
82
88
83
89
$ this ->config = parse_ini_file ($ iniFile , TRUE );
84
90
85
- if (isset ($ _SERVER ['REQUEST_URI ' ]) && isset ($ _SERVER ['REQUEST_METHOD ' ]) && isset ( $ _SERVER [ ' QUERY_STRING ' ]) ) {
91
+ if (isset ($ _SERVER ['REQUEST_URI ' ]) && isset ($ _SERVER ['REQUEST_METHOD ' ])) {
86
92
87
93
if (isset ($ _SERVER ['CONTENT_LENGTH ' ]) && $ _SERVER ['CONTENT_LENGTH ' ] > 0 ) {
88
94
$ this ->requestData = '' ;
@@ -92,9 +98,22 @@ function PHPRestSQL($iniFile = 'phprestsql.ini') {
92
98
}
93
99
fclose ($ httpContent );
94
100
}
95
- $ urlString = substr ($ _SERVER ['REQUEST_URI ' ], strlen ($ this ->config ['settings ' ]['baseURL ' ]), -(strlen ($ _SERVER ['QUERY_STRING ' ]) + 1 ));
96
- $ urlParts = explode ('/ ' , $ urlString );
97
-
101
+
102
+ $ urlString = substr ($ _SERVER ['REQUEST_URI ' ], strlen ($ this ->config ['settings ' ]['baseURL ' ]));
103
+ $ urlParts = explode ('/ ' , $ urlString );
104
+
105
+ $ lastPart = array_pop ($ urlParts );
106
+ $ dotPosition = strpos ($ lastPart , '. ' );
107
+ if ($ dotPosition !== FALSE ) {
108
+ $ this ->extension = substr ($ lastPart , $ dotPosition + 1 );
109
+ $ lastPart = substr ($ lastPart , 0 , $ dotPosition );
110
+ }
111
+ array_push ($ urlParts , $ lastPart );
112
+
113
+ if (isset ($ urlParts [0 ]) && $ urlParts [0 ] == '' ) {
114
+ array_shift ($ urlParts );
115
+ }
116
+
98
117
if (isset ($ urlParts [0 ])) $ this ->table = $ urlParts [0 ];
99
118
if (count ($ urlParts ) > 1 && $ urlParts [1 ] != '' ) {
100
119
array_shift ($ urlParts );
@@ -107,8 +126,6 @@ function PHPRestSQL($iniFile = 'phprestsql.ini') {
107
126
108
127
$ this ->method = $ _SERVER ['REQUEST_METHOD ' ];
109
128
110
- } else {
111
- trigger_error ('I require the server variables REQUEST_URI, REQUEST_METHOD and QUERY_STRING to work. ' , E_USER_ERROR );
112
129
}
113
130
}
114
131
@@ -215,7 +232,7 @@ function get() {
215
232
'value ' => $ data
216
233
);
217
234
if (substr ($ column , -strlen ($ this ->config ['database ' ]['foreignKeyPostfix ' ])) == $ this ->config ['database ' ]['foreignKeyPostfix ' ]) {
218
- $ field ['xlink ' ] = ' http:// ' . $ _SERVER [ ' HTTP_HOST ' ]. $ this ->config ['settings ' ]['baseURL ' ].substr ($ column , 0 , -strlen ($ this ->config ['database ' ]['foreignKeyPostfix ' ])).'/ ' .$ data. ' / ' ;
235
+ $ field ['xlink ' ] = $ this ->config ['settings ' ]['baseURL ' ].' / ' . substr ($ column , 0 , -strlen ($ this ->config ['database ' ]['foreignKeyPostfix ' ])).'/ ' .$ data ;
219
236
}
220
237
$ values [] = $ field ;
221
238
}
@@ -230,13 +247,12 @@ function get() {
230
247
}
231
248
} else { // get table
232
249
$ this ->display = 'table ' ;
233
- $ limit = isset ($ _GET ['limit ' ]) ? $ _GET ['limit ' ] : NULL ;
234
- $ resource = $ this ->db ->getTable (join (', ' , $ primary ), $ this ->table , $ limit );
250
+ $ resource = $ this ->db ->getTable (join (', ' , $ primary ), $ this ->table );
235
251
if ($ resource ) {
236
252
if ($ this ->db ->numRows ($ resource ) > 0 ) {
237
253
while ($ row = $ this ->db ->row ($ resource )) {
238
254
$ this ->output ['table ' ][] = array (
239
- 'xlink ' => ' http:// ' . $ _SERVER [ ' HTTP_HOST ' ]. $ this ->config ['settings ' ]['baseURL ' ].$ this ->table .'/ ' .join ('/ ' , $ row ). ' / ' ,
255
+ 'xlink ' => $ this ->config ['settings ' ]['baseURL ' ].' / ' . $ this ->table .'/ ' .join ('/ ' , $ row ),
240
256
'value ' => join (' ' , $ row )
241
257
);
242
258
}
@@ -254,13 +270,13 @@ function get() {
254
270
if ($ this ->db ->numRows ($ resource ) > 0 ) {
255
271
while ($ row = $ this ->db ->row ($ resource )) {
256
272
$ this ->output ['database ' ][] = array (
257
- 'xlink ' => ' http:// ' . $ _SERVER [ ' HTTP_HOST ' ]. $ this ->config ['settings ' ]['baseURL ' ].reset ($ row ). ' / ' ,
273
+ 'xlink ' => $ this ->config ['settings ' ]['baseURL ' ].' / ' . reset ($ row ),
258
274
'value ' => reset ($ row )
259
275
);
260
276
}
261
277
$ this ->generateResponseData ();
262
278
} else {
263
- $ this ->notFound ();
279
+ $ this ->notFound ();
264
280
}
265
281
} else {
266
282
$ this ->unauthorized ();
@@ -297,7 +313,7 @@ function post() {
297
313
'value ' => $ data
298
314
);
299
315
if (substr ($ column , -strlen ($ this ->config ['database ' ]['foreignKeyPostfix ' ])) == $ this ->config ['database ' ]['foreignKeyPostfix ' ]) {
300
- $ field ['xlink ' ] = ' http:// ' . $ _SERVER [ ' HTTP_HOST ' ]. $ this ->config ['settings ' ]['baseURL ' ].substr ($ column , 0 , -strlen ($ this ->config ['database ' ]['foreignKeyPostfix ' ])).'/ ' .$ data .'/ ' ;
316
+ $ field ['xlink ' ] = $ this ->config ['settings ' ]['baseURL ' ]. ' / ' .substr ($ column , 0 , -strlen ($ this ->config ['database ' ]['foreignKeyPostfix ' ])).'/ ' .$ data .'/ ' ;
301
317
}
302
318
$ values [] = $ field ;
303
319
}
@@ -323,7 +339,7 @@ function post() {
323
339
$ resource = $ this ->db ->insertRow ($ this ->table , $ names , $ values );
324
340
if ($ resource ) {
325
341
if ($ this ->db ->numAffected () > 0 ) {
326
- $ this ->created (' http:// ' . $ _SERVER [ ' HTTP_HOST ' ]. $ this ->config ['settings ' ]['baseURL ' ].$ this ->table .'/ ' .$ this ->db ->lastInsertId ().'/ ' );
342
+ $ this ->created ($ this ->config ['settings ' ]['baseURL ' ]. ' / ' .$ this ->table .'/ ' .$ this ->db ->lastInsertId ().'/ ' );
327
343
} else {
328
344
$ this ->badRequest ();
329
345
}
@@ -387,7 +403,7 @@ function put() {
387
403
$ this ->methodNotAllowed ('GET, HEAD ' );
388
404
}
389
405
}
390
-
406
+
391
407
/**
392
408
* Execute a DELETE request. A DELETE request removes a row from the database given a table and primary key(s).
393
409
*/
@@ -438,7 +454,14 @@ function parseRequestData() {
438
454
* Generate the HTTP response data.
439
455
*/
440
456
function generateResponseData () {
441
- if (isset ($ _SERVER ['HTTP_ACCEPT ' ])) {
457
+ if ($ this ->extension ) {
458
+ if (isset ($ this ->config ['mimetypes ' ][$ this ->extension ])) {
459
+ $ mimetype = $ this ->config ['mimetypes ' ][$ this ->extension ];
460
+ if (isset ($ this ->config ['renderers ' ][$ mimetype ])) {
461
+ $ renderClass = $ this ->config ['renderers ' ][$ mimetype ];
462
+ }
463
+ }
464
+ } elseif (isset ($ _SERVER ['HTTP_ACCEPT ' ])) {
442
465
$ accepts = explode (', ' , $ _SERVER ['HTTP_ACCEPT ' ]);
443
466
$ orderedAccepts = array ();
444
467
foreach ($ accepts as $ key => $ accept ) {
@@ -464,16 +487,17 @@ function generateResponseData() {
464
487
}
465
488
}
466
489
}
467
- if (!isset ($ renderClass )) {
468
- $ this ->notAcceptable ();
469
- exit ;
470
- }
471
490
} else {
472
491
$ renderClass = array_shift ($ this ->config ['renderers ' ]);
473
492
}
474
- require_once ($ renderClass );
475
- $ renderer =& new PHPRestSQLRenderer ();
476
- $ renderer ->render ($ this );
493
+ if (isset ($ renderClass )) {
494
+ require_once ($ renderClass );
495
+ $ renderer = new PHPRestSQLRenderer ();
496
+ $ renderer ->render ($ this );
497
+ } else {
498
+ $ this ->notAcceptable ();
499
+ exit ;
500
+ }
477
501
}
478
502
479
503
/**
@@ -497,17 +521,15 @@ function noContent() {
497
521
* Send a HTTP 400 response header.
498
522
*/
499
523
function badRequest () {
500
- header ('HTTP/1.0 400 Bad Request ' );
524
+ header ('HTTP/1.0 400 Bad Request ' );
501
525
}
502
526
503
527
/**
504
528
* Send a HTTP 401 response header.
505
529
*/
506
530
function unauthorized ($ realm = 'PHPRestSQL ' ) {
507
- if (!isset ($ _SERVER ['PHP_AUTH_USER ' ]) || !isset ($ _SERVER ['PHP_AUTH_PW ' ])) {
508
- header ('WWW-Authenticate: Basic realm=" ' .$ realm .'" ' );
509
- }
510
- header ('HTTP/1.0 401 Unauthorized ' );
531
+ header ('WWW-Authenticate: Basic realm=" ' .$ realm .'" ' );
532
+ header ('HTTP/1.0 401 Unauthorized ' );
511
533
}
512
534
513
535
/**
0 commit comments