| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
//////////////////////////////////////////////////////////////////////////////
|
| 5 |
// Menu callbacks
|
| 6 |
|
| 7 |
function exhibit_output_convert($url, $callback, $input_format = NULL, $output_format = 'application/json') {
|
| 8 |
if (!preg_match('!^exhibit/[\w]+/(.*)$!', $_GET['q'], $matches)) {
|
| 9 |
return drupal_not_found();
|
| 10 |
}
|
| 11 |
|
| 12 |
$url = 'http://' . $matches[1];
|
| 13 |
if (!valid_url($url, TRUE)) {
|
| 14 |
return drupal_access_denied();
|
| 15 |
}
|
| 16 |
|
| 17 |
$response = drupal_http_request($url);
|
| 18 |
if (!empty($response->error)) {
|
| 19 |
// TODO
|
| 20 |
return drupal_access_denied();
|
| 21 |
}
|
| 22 |
|
| 23 |
$output = $callback($response->data);
|
| 24 |
|
| 25 |
exhibit_output($output_format, drupal_to_js($output));
|
| 26 |
}
|
| 27 |
|
| 28 |
function exhibit_output_node($node) {
|
| 29 |
$function = 'exhibit_output_node_' . $node->type;
|
| 30 |
exhibit_output('application/json', drupal_to_js(is_callable($function) ? call_user_func($function, $node) : array('items' => array())));
|
| 31 |
}
|
| 32 |
|
| 33 |
function exhibit_output_feed($feed) {
|
| 34 |
exhibit_output($feed['type'], exhibit_get_feed_contents($feed['url']));
|
| 35 |
}
|
| 36 |
|
| 37 |
function exhibit_output($type, $body) {
|
| 38 |
//$type = 'text/plain'; // DEBUG
|
| 39 |
drupal_set_header('Content-Type: ' . $type . '; charset=utf-8');
|
| 40 |
drupal_set_header('Content-Length: ' . strlen($body));
|
| 41 |
|
| 42 |
if (EXHIBIT_FEED_ETAG) {
|
| 43 |
$md5 = base64_encode(md5($body, TRUE));
|
| 44 |
drupal_set_header('Content-MD5: ' . $md5);
|
| 45 |
drupal_set_header('ETag: "' . $md5 . '"'); // strong entity tag
|
| 46 |
}
|
| 47 |
|
| 48 |
if (EXHIBIT_FEED_LIFETIME > 0) {
|
| 49 |
drupal_set_header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
| 50 |
drupal_set_header('Expires: ' . gmdate('D, d M Y H:i:s', time() + EXHIBIT_FEED_LIFETIME) . ' GMT');
|
| 51 |
drupal_set_header('Cache-Control: max-age=' . EXHIBIT_FEED_LIFETIME . ', private, must-revalidate');
|
| 52 |
drupal_set_header('Pragma: cache'); // need to override no-cache set by Drupal.
|
| 53 |
}
|
| 54 |
|
| 55 |
print $body;
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* @see http://simile.mit.edu/wiki/Exhibit/Template/_history_.html
|
| 60 |
*/
|
| 61 |
function exhibit_output_history() {
|
| 62 |
print '<html><body></body></html>';
|
| 63 |
}
|
| 64 |
|
| 65 |
//////////////////////////////////////////////////////////////////////////////
|
| 66 |
|
| 67 |
function exhibit_parse_tsv($input) {
|
| 68 |
$output = array();
|
| 69 |
|
| 70 |
$lines = explode("\n", $input);
|
| 71 |
$fields = explode("\t", array_shift($lines));
|
| 72 |
foreach ($lines as $line) {
|
| 73 |
$item = array();
|
| 74 |
foreach (explode("\t", $line) as $index => $value) {
|
| 75 |
$item[$fields[$index]] = $value;
|
| 76 |
}
|
| 77 |
$output[] = $item;
|
| 78 |
}
|
| 79 |
|
| 80 |
return exhibit_json($output);
|
| 81 |
}
|