fedoraUser = $connectionHelper->getUser();
//$this->fedoraPass = $connectionHelper->getPassword();
}
/**
* Grabs a stream from fedora sets the mimetype and returns it. $dsID is the
* datastream id. If $forceSoap is set, the function will always buffer the datastream from fedora. Otherwise, it will
* try and use a redirect if possible.
*
* @global type $user
* @param type $pid
* @param type $dsID
* @param type $asAttachment
* @param type $label
* @param type $filePath
* @param type $version
* @param type $forceSoap
* @return type
*/
function makeObject($pid, $dsID, $asAttachment = FALSE, $label = NULL, $filePath=FALSE, $version=NULL, $forceSoap = TRUE) {
global $user;
module_load_include('inc', 'fedora_repository', 'ContentModel');
if ($pid == NULL || $dsID == NULL) {
drupal_set_message(t("no pid or dsid given to create an object with"), 'error');
return ' ';
}
$headers = module_invoke_all('file_download', "/fedora/repository/$pid/$dsID");
if (in_array(-1, $headers)) {
drupal_set_message(t('hello'));
drupal_access_denied();
return ' ';
}
if (!fedora_repository_access(OBJECTHELPER :: $OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
drupal_set_message(t("You do not have access Fedora objects within the attempted namespace."), 'error');
drupal_access_denied();
return ' ';
}
if (variable_get('fedora_object_restrict_datastreams', FALSE) == TRUE) {
if (($cm = ContentModel::loadFromObject($pid)) == FALSE) {
drupal_set_message(t("You do not have access to objects without an Islandora Content Model."), 'error');
drupal_access_denied();
return ' ';
}
$cmDatastreams = $cm->listDatastreams();
if (!((isset($user) && in_array('administrator', $user->roles)) || in_array($dsID, $cmDatastreams))) {
drupal_set_message(t("You do not have access to the specified datastream."), 'error');
drupal_access_denied();
return ' ';
}
}
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
if (isset($item->datastreams[$dsID])) {
$mimeType = $item->datastreams[$dsID]['MIMEType'];
if ($label == NULL) {
$label = $item->datastreams[$dsID]['label'];
}
}
else {
drupal_not_found();
exit();
}
if ((!isset($user)) || $user->uid == 0) {
$fedoraUser = 'anonymous';
$fedoraPass = 'anonymous';
$contentSize = 0;
}
else {
$fedoraUser = $user->name;
$fedoraPass = $user->pass;
$dataStreamInfo = $item->get_datastream_info($dsID);
$contentSize = $dataStreamInfo->datastream->size;
}
if (function_exists("curl_init")) {
if (!isset($mimeType)) {
$pid = variable_get('fedora_default_display_pid', 'demo:10');
$dsID = variable_get('fedora_default_display_dsid', 'TN');
$mimeType = 'image/jpeg';
}
$url = variable_get('fedora_base_url', 'http://localhost:8080/fedora') . '/get/' . $pid . '/' . $dsID;
if ($version) {
$url .= '/' . $version; //drupal_urlencode($version);
}
$ch = curl_init();
$user_agent = "Mozilla/4.0 pp(compatible; MSIE 5.01; Windows NT 5.0)";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
//curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$fedoraUser:$fedoraPass");
// There seems to be a bug in Fedora 3.1's REST authentication, removing this line fixes the authorization denied error.
// curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // return into a variable
curl_setopt($ch, CURLOPT_URL, $url);
if ($filePath !== FALSE) {
$fp = fopen($filePath, 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
fclose($fp);
}
else {
header("Content-type: $mimeType");
if ($contentSize > 0) {
header("Content-length: $contentSize");
}
if ($asAttachment) {
$suggestedFileName = "$label";
$pos = strpos($suggestedFileName, '.');
/*
* Here we used to take an object of, say, type application/pdf with label, say, "My Document"
* and we assemble the output filename extension based on the post-slash portion of the mimetype.
* (If the label has a period anywhere in it, we leave it alone.)
*
* This is great for simple mimetypes like application/pdf, text/html, image/jpeg, etc.
* but it's terrible for, say, application/vnd.oasis.opendocument.text (.odt).
*
* Instead we'll use the get_extension function in MimeClass.inc to discover a valid extension
* for the mimetype in question.
*/
if ($pos === FALSE) {
module_load_include('inc', 'fedora_repository', 'MimeClass');
$mimeclass = new MimeClass();
$ext = $mimeclass->get_extension($mimeType);
$suggestedFileName = "$label.$ext";
}
header('Content-Disposition: attachment; filename="' . $suggestedFileName . '"');
}
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$curl_out = curl_exec($ch);
if ($curl_out !== FALSE) {
$info = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
//dd($info, 'effective URL');
if ($url !== $info) { //Handle redirect streams (the final URL is not the same as the Fedora URL)
//Add the parameters passed to Drupal, leaving out the 'q'
$query = array();
parse_str($_SERVER['QUERY_STRING'], $query);
if (isset($query['q'])) {
unset($query['q']);
}
header('HTTP/1.1 307 Moved Temporarily');
header('Location: ' . $info . '?' . http_build_query($query)); //Fedora seems to discard the query portion.
}
elseif ((isset($user) && $user->uid != 0) || $forceSoap || isset($_SERVER['HTTPS'])) { //If not anonymous, soap is force or we're using HTTPS
//Have the webserver mediate the transfer (download and restream)
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE); //CURLOPT_NOBODY sets it to 'HEAD'
$toReturn = curl_exec($ch);
echo $toReturn;
}
else {
header('Location: ' . $url);
}
}
else {
//Curl error...
}
}
curl_close($ch);
}
else {
drupal_set_message(t('No curl support.'), 'error');
}
}
/**
* Gets collection objects t
*
* @param type $pid
* @param type $query
* @return type
*/
function getCollectionInfo($pid, $query = NULL) {
module_load_include('inc', 'fedora_repository', 'CollectionClass');
$collectionClass = new CollectionClass();
$results = $collectionClass->getRelatedItems($pid, $query);
return $results;
}
/**
* returns the mime type
*
* @global type $user
* @param type $pid
* @param type $dsID
* @return type
*/
function getMimeType($pid, $dsID) {
global $user;
if (empty($pid) || empty($dsID)) {
drupal_set_message(t('You must specify an object pid and datastream ID.'), 'error');
return '';
}
if (!fedora_repository_access(ObjectHelper :: $OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
drupal_set_message(t('You do not have the appropriate permissions'), 'error');
return;
}
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new fedora_item($pid);
$datastream_list = $item->get_datastreams_list_as_SimpleXML();
if (!isset($datastream_list)) {
//drupal_set_message( t("No datastreams available."), 'status' );
return ' ';
}
foreach ($datastream_list as $datastream) {
foreach ($datastream as $datastreamValue) {
if ($datastreamValue->ID == $dsID) {
return $datastreamValue->MIMEType;
}
}
}
return '';
}
/**
* getDatastreamInfo ??
* @global type $user
* @param type $pid
* @param type $dsID
* @return type
*/
function getDatastreamInfo($pid, $dsID) {
global $user;
if (empty($pid) || empty($dsID)) {
drupal_set_message(t('You must specify an object pid and datastream ID.'), 'error');
return '';
}
if (!fedora_repository_access(ObjectHelper :: $OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
drupal_set_message(t('You do not have the appropriate permissions'), 'error');
return;
}
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new fedora_item($pid);
$datastream_list = $item->get_datastreams_list_as_SimpleXML();
if (!isset($datastream_list)) {
//drupal_set_message( t("No datastreams available."), 'status' );
return ' ';
}
foreach ($datastream_list as $datastream) {
foreach ($datastream as $datastreamValue) {
if ($datastreamValue->ID == $dsID) {
return $datastreamValue;
}
}
}
return '';
}
/**
* internal function
* @param $pid String
* @param $dataStreamValue Object
*/
function create_link_for_ds($pid, $dataStreamValue) {
global $base_url;
$path = drupal_get_path('module', 'fedora_repository');
require_once($path . '/api/fedora_item.inc');
$item = new Fedora_Item($pid);
if (user_access(ObjectHelper :: $PURGE_FEDORA_OBJECTSANDSTREAMS)) {
$allow = TRUE;
if (module_exists('fedora_fesl')) {
$allow = fedora_fesl_check_roles($pid, 'write');
}
if ($allow) {
$purgeImage = '
';
}
}
else {
$purgeImage = ' ';
}
$fullPath = base_path() . $path;
// Add an icon to replace a datastream
// @TODO Note: using l(theme_image(..), ...); for these image links (and other links) may remove the need to have clean urls enabled.
$replaceImage = ' ';
if (user_access(ObjectHelper :: $ADD_FEDORA_STREAMS)) {
$allow = TRUE;
if (module_exists('fedora_fesl')) {
$allow = fedora_fesl_check_roles($pid, 'write');
}
if ($allow) {
$replaceImage = 'label . '" href="' . $base_url . '/fedora/repository/replaceStream/' . $pid . '/' . $dataStreamValue->ID . '/' . $dataStreamValue->label . '">
';
}
}
$content = '';
$id = $dataStreamValue->ID;
$label = $dataStreamValue->label;
$label = str_replace("_", " ", $label);
$mimeType = $dataStreamValue->MIMEType;
$view = '' . t('View') . '';
$action = "$base_url/fedora/repository/object_download/" . drupal_urlencode($pid) . '/' . $id . '/' . drupal_urlencode(preg_replace('/\//i', '${1}_', $label)); // Necessary to handle the case of Datastream labels that contain slashes. Ugh.
$downloadVersion = '
';
}
}
return $output;
}
/**
* Gets a list of datastreams from an object using its pid
*
* We make some assumptions here. We have implemented a policy that
* we ingest in our repository will have TN (thumbnail) datastream. Even audio
* will have a picture of a speaker or something. This is not critical
* but makes searches etc. look better if there is a TN stream.
* This diplays all the streams in a collapsed fieldset at the bottom of the object page.
* you can implement a content model if you would like certain streams displayed in certain ways.
* @param $object_pid String
* @return String
*
*/
function get_formatted_datastream_list($object_pid, $contentModels, &$fedoraItem) {
global $fedoraUser, $fedoraPass, $base_url, $user;
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'fedora_repository', 'ContentModel');
$path = drupal_get_path('module', 'fedora_repository');
$dataStreamBody = '';
$fedoraItem = new Fedora_Item($object_pid);
if (user_access(ObjectHelper :: $VIEW_DETAILED_CONTENT_LIST)) {
$availableDataStreamsText = 'Detailed List of Content';
//$metaDataText='Description';
$mainStreamLabel = NULL;
$object = $fedoraItem->get_datastreams_list_as_SimpleXML();
if (!isset($object)) {
drupal_set_message(t("No datastreams available"));
return ' ';
}
$hasOBJStream = NULL;
$hasTNStream = FALSE;
$dataStreamBody = "' . t("!text", array('!text' => $availableDataStreamsText)) . ' | |||
|---|---|---|---|
| $key : | $value | ||
' . t('Add to this Collection');
}
}
}
else {
$ingestObject = ' ';
}
}
$datastreams .= $ingestObject;
$output .= $datastreams;
$showDesc = FALSE;
switch (variable_get('fedora_object_display_description', ObjectHelper :: $DISPLAY_NO_MODEL_OUTPUT)) {
case ObjectHelper :: $DISPLAY_NEVER: break;
case ObjectHelper :: $DISPLAY_NO_MODEL_OUTPUT:
if (trim($datastreams) == '') {
$showDesc = TRUE;
}
break;
case ObjectHelper :: $DISPLAY_ALWAYS:
default:
$showDesc = TRUE;
break;
}
if ($showDesc) {
//just show default dc or qdc as we could not find a content model
$metaDataText = t('Description');
$body = $this->getQDC($pid);
$fieldset = array(
'#title' => t("!metaDataText", array('!metaDataText' => $metaDataText)),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#value' => $body
);
$output .= theme('fieldset', $fieldset);
}
return $output;
}
/**
* Gets the parent objects that this object is related to
*
* @param unknown_type $pid
* @return unknown
*/
function get_parent_objects($pid) {
$query_string = 'select $object $title from <#ri>
where ($object