Skip to content

Commit b09b6a2

Browse files
author
peejeh
committed
Updated output formats formatting
Removed hostnames from URLs in output Added ability to use extension in URL to pick output format
1 parent 40839a7 commit b09b6a2

File tree

6 files changed

+112
-98
lines changed

6 files changed

+112
-98
lines changed

html.php

+42-42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
/* $id$ */
32

43
/**
54
* PHP REST SQL HTML renderer class
@@ -36,76 +35,77 @@ function render($PHPRestSQL) {
3635
*/
3736
function database() {
3837
header('Content-Type: text/html');
39-
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">';
40-
echo '<html>';
41-
echo '<head>';
42-
echo '<title>PHP REST SQL : Database "'.htmlspecialchars($this->PHPRestSQL->config['database']['database']).'"</title>';
43-
echo '</head>';
44-
echo '<body>';
45-
echo '<h1>Tables in database "'.htmlspecialchars($this->PHPRestSQL->config['database']['database']).'"</h1>';
38+
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">'."\n";
39+
echo '<html>'."\n";
40+
echo '<head>'."\n";
41+
echo '<title>PHP REST SQL : Database "'.htmlspecialchars($this->PHPRestSQL->config['database']['database']).'"</title>'."\n";
42+
echo '</head>'."\n";
43+
echo '<body>'."\n";
44+
echo '<h1>Tables in database "'.htmlspecialchars($this->PHPRestSQL->config['database']['database']).'"</h1>'."\n";
4645
if (isset($this->PHPRestSQL->output['database'])) {
47-
echo '<ul>';
46+
echo '<ul>'."\n";
4847
foreach ($this->PHPRestSQL->output['database'] as $table) {
49-
echo '<li><a href="'.htmlspecialchars($table['xlink']).'">'.htmlspecialchars($table['value']).'</a></li>';
48+
echo '<li><a href="'.htmlspecialchars($table['xlink']).'">'.htmlspecialchars($table['value']).'</a></li>'."\n";
5049
}
51-
echo '</ul>';
50+
echo '</ul>'."\n";
5251
}
53-
echo '</body>';
54-
echo '</html>';
52+
echo '</body>'."\n";
53+
echo '</html>'."\n";
5554
}
5655

5756
/**
5857
* Output the rows within a table.
5958
*/
6059
function table() {
6160
header('Content-Type: text/html');
62-
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">';
63-
echo '<html>';
64-
echo '<head>';
65-
echo '<title>PHP REST SQL : Table "'.htmlspecialchars($this->PHPRestSQL->table).'"</title>';
66-
echo '</head>';
67-
echo '<body>';
68-
echo '<h1>Records in table "'.htmlspecialchars($this->PHPRestSQL->table).'"</h1>';
61+
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">'."\n";
62+
echo '<html>'."\n";
63+
echo '<head>'."\n";
64+
echo '<title>PHP REST SQL : Table "'.htmlspecialchars($this->PHPRestSQL->table).'"</title>'."\n";
65+
echo '</head>'."\n";
66+
echo '<body>'."\n";
67+
echo '<h1>Records in table "'.htmlspecialchars($this->PHPRestSQL->table).'"</h1>'."\n";
6968
if (isset($this->PHPRestSQL->output['table'])) {
70-
echo '<ul>';
69+
echo '<ul>'."\n";
7170
foreach ($this->PHPRestSQL->output['table'] as $row) {
72-
echo '<li><a href="'.htmlspecialchars($row['xlink']).'">'.htmlspecialchars($row['value']).'</a></li>';
71+
echo '<li><a href="'.htmlspecialchars($row['xlink']).'">'.htmlspecialchars($row['value']).'</a></li>'."\n";
7372
}
74-
echo '</ul>';
73+
echo '</ul>'."\n";
7574
}
76-
echo '</body>';
77-
echo '</html>';
75+
echo '</body>'."\n";
76+
echo '</html>'."\n";
7877
}
7978

8079
/**
8180
* Output the entry in a table row.
8281
*/
8382
function row() {
8483
header('Content-Type: text/html');
85-
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">';
86-
echo '<html>';
87-
echo '<head>';
88-
echo '<title>PHP REST SQL : Record #'.htmlspecialchars(join('/', $this->PHPRestSQL->uid)).'</title>';
89-
echo '</head>';
90-
echo '<body>';
91-
echo '<h1>Record #'.htmlspecialchars(join('/', $this->PHPRestSQL->uid)).'</h1>';
84+
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">'."\n";
85+
echo '<html>'."\n";
86+
echo '<head>'."\n";
87+
echo '<title>PHP REST SQL : Record #'.htmlspecialchars(join('/', $this->PHPRestSQL->uid)).'</title>'."\n";
88+
echo '</head>'."\n";
89+
echo '<body>'."\n";
90+
echo '<h1>Record #'.htmlspecialchars(join('/', $this->PHPRestSQL->uid)).'</h1>'."\n";
9291
if (isset($this->PHPRestSQL->output['row'])) {
93-
echo '<table>';
92+
echo '<table>'."\n";
9493
foreach ($this->PHPRestSQL->output['row'] as $field) {
95-
echo '<tr><th>'.htmlspecialchars($field['field']).'</th><td>';
94+
echo '<tr>'."\n";
95+
echo '<th>'.htmlspecialchars($field['field']).'</th>'."\n";
96+
echo '<td>'."\n";
9697
if (isset($field['xlink'])) {
97-
echo '<a href="'.htmlspecialchars($field['xlink']).'">'.htmlspecialchars($field['value']).'</a>';
98+
echo '<a href="'.htmlspecialchars($field['xlink']).'">'.htmlspecialchars($field['value']).'</a>'."\n";
9899
} else {
99-
echo htmlspecialchars($field['value']);
100+
echo htmlspecialchars($field['value'])."\n";
100101
}
101-
echo '</td></tr>';
102+
echo '</td>'."\n";
103+
echo '</tr>'."\n";
102104
}
103-
echo '</table>';
105+
echo '</table>'."\n";
104106
}
105-
echo '</body>';
106-
echo '</html>';
107+
echo '</body>'."\n";
108+
echo '</html>'."\n";
107109
}
108110

109111
}
110-
111-
?>

mysql.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,10 @@ function getRow($table, $where) {
6565
* Get the rows in a table.
6666
* @param str primary The names of the primary columns to return
6767
* @param str table
68-
* @param str limit
6968
* @return resource A resultset resource
7069
*/
71-
function getTable($primary, $table, $limit) {
72-
if (preg_match('/[0-9]+(,[0-9]+)?/', $limit)) {
73-
return mysql_query(sprintf('SELECT %s FROM %s LIMIT %s', $primary, $table, $limit));
74-
} else {
75-
return mysql_query(sprintf('SELECT %s FROM %s', $primary, $table));
76-
}
70+
function getTable($primary, $table) {
71+
return mysql_query(sprintf('SELECT %s FROM %s', $primary, $table));
7772
}
7873

7974
/**

phprestsql.ini

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
[settings]
2-
;baseURL = "/demo/"
3-
baseURL = "/phprestsql/demo/"
2+
baseURL = "/phprestsql"
43

54
[database]
65
type = "mysql"
76
server = "localhost"
8-
;server = "mysql.sourceforge.net"
97
database = "phprestsql"
108
;username = "root"
119
;password = ""
@@ -15,3 +13,8 @@ foreignKeyPostfix = "_uid"
1513
text/xml = xml.php
1614
text/plain = plain.php
1715
text/html = html.php
16+
17+
[mimetypes]
18+
xml = text/xml
19+
txt = text/plain
20+
html = text/html

phprestsql.php

+52-30
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,19 @@ class PHPRestSQL {
4343
* @var str
4444
*/
4545
var $method = 'GET';
46-
46+
4747
/**
4848
* The HTTP request data sent (if any).
4949
* @var str
5050
*/
5151
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+
5359
/**
5460
* The database table to query.
5561
* @var str
@@ -82,7 +88,7 @@ function PHPRestSQL($iniFile = 'phprestsql.ini') {
8288

8389
$this->config = parse_ini_file($iniFile, TRUE);
8490

85-
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && isset($_SERVER['QUERY_STRING'])) {
91+
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD'])) {
8692

8793
if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > 0) {
8894
$this->requestData = '';
@@ -92,9 +98,22 @@ function PHPRestSQL($iniFile = 'phprestsql.ini') {
9298
}
9399
fclose($httpContent);
94100
}
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+
98117
if (isset($urlParts[0])) $this->table = $urlParts[0];
99118
if (count($urlParts) > 1 && $urlParts[1] != '') {
100119
array_shift($urlParts);
@@ -107,8 +126,6 @@ function PHPRestSQL($iniFile = 'phprestsql.ini') {
107126

108127
$this->method = $_SERVER['REQUEST_METHOD'];
109128

110-
} else {
111-
trigger_error('I require the server variables REQUEST_URI, REQUEST_METHOD and QUERY_STRING to work.', E_USER_ERROR);
112129
}
113130
}
114131

@@ -215,7 +232,7 @@ function get() {
215232
'value' => $data
216233
);
217234
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;
219236
}
220237
$values[] = $field;
221238
}
@@ -230,13 +247,12 @@ function get() {
230247
}
231248
} else { // get table
232249
$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);
235251
if ($resource) {
236252
if ($this->db->numRows($resource) > 0) {
237253
while ($row = $this->db->row($resource)) {
238254
$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),
240256
'value' => join(' ', $row)
241257
);
242258
}
@@ -254,13 +270,13 @@ function get() {
254270
if ($this->db->numRows($resource) > 0) {
255271
while ($row = $this->db->row($resource)) {
256272
$this->output['database'][] = array(
257-
'xlink' => 'http://'.$_SERVER['HTTP_HOST'].$this->config['settings']['baseURL'].reset($row).'/',
273+
'xlink' => $this->config['settings']['baseURL'].'/'.reset($row),
258274
'value' => reset($row)
259275
);
260276
}
261277
$this->generateResponseData();
262278
} else {
263-
$this->notFound();
279+
$this->notFound();
264280
}
265281
} else {
266282
$this->unauthorized();
@@ -297,7 +313,7 @@ function post() {
297313
'value' => $data
298314
);
299315
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.'/';
301317
}
302318
$values[] = $field;
303319
}
@@ -323,7 +339,7 @@ function post() {
323339
$resource = $this->db->insertRow($this->table, $names, $values);
324340
if ($resource) {
325341
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().'/');
327343
} else {
328344
$this->badRequest();
329345
}
@@ -387,7 +403,7 @@ function put() {
387403
$this->methodNotAllowed('GET, HEAD');
388404
}
389405
}
390-
406+
391407
/**
392408
* Execute a DELETE request. A DELETE request removes a row from the database given a table and primary key(s).
393409
*/
@@ -438,7 +454,14 @@ function parseRequestData() {
438454
* Generate the HTTP response data.
439455
*/
440456
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'])) {
442465
$accepts = explode(',', $_SERVER['HTTP_ACCEPT']);
443466
$orderedAccepts = array();
444467
foreach ($accepts as $key => $accept) {
@@ -464,16 +487,17 @@ function generateResponseData() {
464487
}
465488
}
466489
}
467-
if (!isset($renderClass)) {
468-
$this->notAcceptable();
469-
exit;
470-
}
471490
} else {
472491
$renderClass = array_shift($this->config['renderers']);
473492
}
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+
}
477501
}
478502

479503
/**
@@ -497,17 +521,15 @@ function noContent() {
497521
* Send a HTTP 400 response header.
498522
*/
499523
function badRequest() {
500-
header('HTTP/1.0 400 Bad Request');
524+
header('HTTP/1.0 400 Bad Request');
501525
}
502526

503527
/**
504528
* Send a HTTP 401 response header.
505529
*/
506530
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');
511533
}
512534

513535
/**

plain.php

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
/* $id$ */
32

43
/**
54
* PHP REST SQL plain text renderer class
@@ -73,5 +72,3 @@ function row() {
7372
}
7473

7574
}
76-
77-
?>

0 commit comments

Comments
 (0)