| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
//////////////////////////////////////////////////////////////////////////////
|
| 5 |
|
| 6 |
module_load_include('inc', 'bitcache');
|
| 7 |
|
| 8 |
//////////////////////////////////////////////////////////////////////////////
|
| 9 |
// Menu callbacks
|
| 10 |
|
| 11 |
function bitcache_server($id = NULL) {
|
| 12 |
// The REST API must be explicitly enabled by the administrator:
|
| 13 |
if (!BITCACHE_SERVER) {
|
| 14 |
die(drupal_access_denied());
|
| 15 |
}
|
| 16 |
|
| 17 |
$method = $_SERVER['REQUEST_METHOD'];
|
| 18 |
$request = $_REQUEST['q']; // unlike $_GET['q'], aliases not expanded here
|
| 19 |
$query = array_diff_key($_GET, array('q' => NULL));
|
| 20 |
|
| 21 |
// Attempt to dispatch the request to the Bitcache server implementation.
|
| 22 |
if (strpos($request, BITCACHE_ALIAS) === 0) {
|
| 23 |
$request = substr($request, strlen(BITCACHE_ALIAS));
|
| 24 |
|
| 25 |
// Ensure that the bitstream index is accessed with a trailing slash.
|
| 26 |
if (!$request) {
|
| 27 |
die(drupal_goto(BITCACHE_ALIAS . '/', $query, NULL, 301));
|
| 28 |
}
|
| 29 |
|
| 30 |
$server = new Bitcache_DrupalServer(
|
| 31 |
bitcache_get_repository(NULL),
|
| 32 |
array('tmpdir' => file_directory_temp())
|
| 33 |
);
|
| 34 |
$server->$method($request, $query);
|
| 35 |
die();
|
| 36 |
}
|
| 37 |
|
| 38 |
// A path alias has been defined for the Bitcache server root URL, but the
|
| 39 |
// incoming request was still using an internal path; redirect the client
|
| 40 |
// to the correct address.
|
| 41 |
if (strpos($request, BITCACHE_BASE) === 0) {
|
| 42 |
$request = substr($request, strlen(BITCACHE_BASE));
|
| 43 |
die(drupal_goto(BITCACHE_ALIAS . ($request ? $request : '/'), $query, NULL, 301));
|
| 44 |
}
|
| 45 |
|
| 46 |
die(drupal_not_found()); // should never get here
|
| 47 |
}
|
| 48 |
|
| 49 |
//////////////////////////////////////////////////////////////////////////////
|
| 50 |
// Drupal server implementation
|
| 51 |
|
| 52 |
class Bitcache_DrupalServer extends Bitcache_Server {
|
| 53 |
|
| 54 |
function index($request, array $query = array(), $body = TRUE) {
|
| 55 |
static $formats = array(
|
| 56 |
'text/plain' => 'text',
|
| 57 |
//'text/html' => 'html', // TODO
|
| 58 |
'application/json' => 'json',
|
| 59 |
'text/x-ntriples' => 'ntriples',
|
| 60 |
);
|
| 61 |
|
| 62 |
// Content negotiation based on the HTTP Accept header and, secondarily,
|
| 63 |
// on the ?format= URL query parameter.
|
| 64 |
$format = isset($_GET['format']) ? $_GET['format'] : NULL;
|
| 65 |
foreach (explode(',', $_SERVER['HTTP_ACCEPT']) as $accept) {
|
| 66 |
list($mime, ) = explode(';', $accept);
|
| 67 |
if (isset($formats[$mime])) {
|
| 68 |
$format = $mime;
|
| 69 |
break;
|
| 70 |
}
|
| 71 |
}
|
| 72 |
$format = isset($_GET['format']) ? $_GET['format'] : $format;
|
| 73 |
$format = isset($formats[$format]) ? $format : 'text/plain';
|
| 74 |
|
| 75 |
$this->header('Content-Type: '. $format .'; charset=ascii');
|
| 76 |
|
| 77 |
if ($body) {
|
| 78 |
$handler = 'index_'. $formats[$format];
|
| 79 |
$this->$handler();
|
| 80 |
}
|
| 81 |
|
| 82 |
return TRUE;
|
| 83 |
}
|
| 84 |
|
| 85 |
protected function index_json() {
|
| 86 |
$index = array();
|
| 87 |
foreach ($this->repo as $id => $stream) {
|
| 88 |
$index[$id] = $stream->size();
|
| 89 |
}
|
| 90 |
print drupal_to_js($index);
|
| 91 |
}
|
| 92 |
|
| 93 |
protected function index_ntriples() {
|
| 94 |
foreach ($this->repo as $id => $stream) {
|
| 95 |
printf("<%s> <%s> \"%d\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n",
|
| 96 |
$subject = bitcache_resolve_id($id),
|
| 97 |
$predicate = 'http://purl.org/dc/terms/extent',
|
| 98 |
$object = $stream->size());
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
| 102 |
protected function allow($method, $id = NULL) {
|
| 103 |
$method = ($method == 'GET' && !$id) ? 'INDEX' : 'GET'; // TODO: should be handled in base class
|
| 104 |
|
| 105 |
$methods = bitcache_get_features();
|
| 106 |
if (($method != 'HEAD' && empty($methods[strtolower($method)])) || ($method == 'HEAD' && empty($methods['get']))) {
|
| 107 |
return FALSE;
|
| 108 |
}
|
| 109 |
|
| 110 |
switch ($method) {
|
| 111 |
case 'INDEX':
|
| 112 |
return user_access('list bitstreams');
|
| 113 |
case 'HEAD':
|
| 114 |
case 'GET':
|
| 115 |
foreach (bitcache_get_modules() as $module) {
|
| 116 |
if (($result = module_invoke($module, 'bitcache', 'access', $id)) !== NULL) {
|
| 117 |
return $result;
|
| 118 |
}
|
| 119 |
}
|
| 120 |
return user_access('access bitstreams');
|
| 121 |
case 'POST':
|
| 122 |
case 'PUT':
|
| 123 |
return user_access('upload bitstreams');
|
| 124 |
case 'DELETE':
|
| 125 |
return user_access('delete bitstreams');
|
| 126 |
}
|
| 127 |
return parent::allow($method, $id);
|
| 128 |
}
|
| 129 |
|
| 130 |
protected function header($text, $replace = TRUE) {
|
| 131 |
drupal_set_header($text);
|
| 132 |
if ($replace) {
|
| 133 |
header($text, TRUE);
|
| 134 |
}
|
| 135 |
}
|
| 136 |
|
| 137 |
protected function headers($id, $stream) {
|
| 138 |
parent::headers($id, $stream);
|
| 139 |
foreach (module_invoke_all('bitcache', 'download', $id, $stream) as $key => $value) {
|
| 140 |
$this->header($key . ': ' . $value);
|
| 141 |
}
|
| 142 |
|
| 143 |
// Override Drupal's Cache-Control headers to enable at least some forms of caching:
|
| 144 |
// @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.29
|
| 145 |
// @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
|
| 146 |
// @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21
|
| 147 |
$this->header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
| 148 |
$this->header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 365 * 86400) . ' GMT');
|
| 149 |
$this->header('Cache-Control: private, no-transform');
|
| 150 |
}
|
| 151 |
|
| 152 |
protected function created($id, $stream = NULL) {
|
| 153 |
watchdog('bitcache', 'Bitstream created: %id.', array('%id' => $id), WATCHDOG_NOTICE, l(t('view'), 'admin/content/bitcache/view/' . $id));
|
| 154 |
parent::created($id);
|
| 155 |
//module_invoke_all('bitcache', 'insert', $id, $stream); // handled in repository
|
| 156 |
}
|
| 157 |
|
| 158 |
protected function deleted($id) {
|
| 159 |
watchdog('bitcache', 'Bitstream deleted: %id.', array('%id' => $id), WATCHDOG_NOTICE, l(t('view'), 'admin/content/bitcache'));
|
| 160 |
parent::deleted($id);
|
| 161 |
//module_invoke_all('bitcache', 'delete', $id); // handled in repository
|
| 162 |
}
|
| 163 |
|
| 164 |
}
|