| 1 |
<?php
|
| 2 |
// $Id: imagepath.module,v 1.1 2008/03/11 16:32:53 noelbush Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Allows an image in an imagefield to be displayed via a convenient path.
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Implementation of hook_menu()
|
| 10 |
*
|
| 11 |
* Currently this just deals with non-cacheable menu items --
|
| 12 |
* i.e., image request uris.
|
| 13 |
*/
|
| 14 |
function imagepath_menu($may_cache) {
|
| 15 |
$items = array();
|
| 16 |
if (!$may_cache) {
|
| 17 |
$node_type = arg(0);
|
| 18 |
$nid = arg(1);
|
| 19 |
$field_name = arg(2);
|
| 20 |
|
| 21 |
// Get (possibly create) the info for matching the request URI.
|
| 22 |
$domain = variable_get('imagepath_domain', NULL);
|
| 23 |
if ($domain == NULL) {
|
| 24 |
include_once drupal_get_path('module', 'imagepath') .'/imagepath.inc';
|
| 25 |
$domain = _imagepath_update_domain();
|
| 26 |
}
|
| 27 |
|
| 28 |
if (isset($domain[$node_type]) && is_numeric($nid)) {
|
| 29 |
|
| 30 |
// Allow for default field name when appropriate.
|
| 31 |
if (!isset($field_name) && isset($domain[$node_type]['default_field'])) {
|
| 32 |
$field_name = $domain[$node_type]['default_field'];
|
| 33 |
}
|
| 34 |
|
| 35 |
// Now we should have a valid field name, or this path just isn't ours.
|
| 36 |
if (isset($field_name)) {
|
| 37 |
|
| 38 |
// Check that field name is valid.
|
| 39 |
if (!in_array($field_name, $domain[$node_type]['fields'])) {
|
| 40 |
drupal_set_message(t('Incorrect field specified.'), 'error');
|
| 41 |
} else {
|
| 42 |
|
| 43 |
// For the rest we have to look at the actual node.
|
| 44 |
$node = node_load($nid);
|
| 45 |
|
| 46 |
// Require that URIs name the node type correctly.
|
| 47 |
if (!$node || $node->type != $node_type) {
|
| 48 |
drupal_set_message(t('Wrong node type specified in request URI.'), 'error');
|
| 49 |
} else {
|
| 50 |
|
| 51 |
// Check for content in the named field.
|
| 52 |
$field = $node->{"field_$field_name"};
|
| 53 |
if (count($field) == 0) {
|
| 54 |
drupal_set_message(t('Image field has no contents.'), 'error');
|
| 55 |
} else {
|
| 56 |
|
| 57 |
// Check access, and send the file if access is granted.
|
| 58 |
$file = $field[0];
|
| 59 |
if (node_access('view', $node)) {
|
| 60 |
file_transfer($file['filepath'], array('Content-Type: '. mime_header_encode($file['filemime']), 'Content-Length: '. $file['filesize']));
|
| 61 |
} else {
|
| 62 |
drupal_access_denied();
|
| 63 |
}
|
| 64 |
}
|
| 65 |
}
|
| 66 |
}
|
| 67 |
}
|
| 68 |
}
|
| 69 |
}
|
| 70 |
return $items;
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Implementation of hook_node_type()
|
| 75 |
*
|
| 76 |
* Update the cached domain info for imagepath
|
| 77 |
* whenever a node type has been changed.
|
| 78 |
*/
|
| 79 |
function imagepath_node_type($op, $info) {
|
| 80 |
include_once drupal_get_path('module', 'imagepath') .'/imagepath.inc';
|
| 81 |
_imagepath_update_domain();
|
| 82 |
}
|