" . $str . "

"); } function mdl_exit_with_error($str) { global $MDLerr, $body; if ($str) mdl_error($str); if ($body) echo "Error"; echo "

Exiting with Errors...

" . $MDLerr; if ($body) echo ""; exit(); } function cache_flickr_data($collection,$pointer) { global $cachedir, $cacheversion, $MDLerr, $record_meta, $flickr_meta; $cachefile = "$cachedir$flickr_meta[Username]/$collection/$collection-$pointer.xml"; // mdl_exit_with_error(print_r($record_meta,true).print_r($flickr_meta,true)); $xml = new SimpleXMLElement(""); $xml->addChild("collection", urlencode($collection)); $xml->addChild("pointer", urlencode($pointer)); $xml->addChild("id", urlencode($record_meta['FlickrPhoto']['id'])); $xml->addChild("url", urlencode($record_meta['FlickrURL'])); $xml->addChild("square", urlencode($record_meta['FlickrSquare'])); $xml->addChild("user", urlencode($flickr_meta['Username'])); $xml->addChild("nsid", urlencode($flickr_meta['NSID'])); $xml->addChild("set", urlencode($flickr_meta['Set'])); $xml->addChild("version", urlencode($cacheversion)); $xml->addChild("date", urlencode($record_meta['Date'])); if (!write_file($cachefile, $xml->asXML())) { mdl_error("could not write to cache: $cachefile"); return false; } return true; } function cache_exists($collection,$pointer) { global $cachedir, $cacheversion, $MDLerr, $record_meta, $flickr_meta; $cachefile = "$cachedir$flickr_meta[Username]/$collection/$collection-$pointer.xml"; return file_exists($cachefile); } function range2array($rangeString) { // takes a string like "2,4,6,9-12 34 - 37 11 16;15 44, 45, 6 to 8 53; 112" // and turns it into an array with one entry for each number in the set $patterns = array ( '/(\d+) *- *(\d+)/', '/(\d+) *to *(\d+)/', '/[^\d-]+/' ); $replacements = array( '$1-$2', '$1-$2', ' ' ); $resultString = preg_replace($patterns, $replacements, $rangeString); $resultArray = array(); foreach(split(" ", $resultString) as $value) { list($first, $second) = split("-",$value); if ($second) { if ($first < $second) { $min = $first; $max = $second; } else { $max = $first; $min = $second; } foreach(range($min,$max) as $each) { $resultArray[$each] = $each; } } else { $resultArray[$first] = $first; } } sort($resultArray); return($resultArray); } function xml2assoc($xml) { $tree = null; while($xml->read()) switch ($xml->nodeType) { case XMLReader::END_ELEMENT: return $tree; case XMLReader::ELEMENT: $node = array('tag' => $xml->name, 'value' => $xml->isEmptyElement ? '' : xml2assoc($xml)); if($xml->hasAttributes) while($xml->moveToNextAttribute()) $node['attributes'][$xml->name] = $xml->value; $tree[] = $node; break; case XMLReader::TEXT: case XMLReader::CDATA: $tree .= $xml->value; } return $tree; } function get_oai_list ($collection) { global $contentdm_server, $config, $overall_tag; global $identifier_array, $identifier_js, $desc_fields; $record_list = array(); // this is the OAI url for CONTENTdm $filename = "http://$contentdm_server/cgi-bin/oai.exe?verb=ListIdentifiers&set=$collection&metadataPrefix=oai_dc"; // we load up the XML response from CONTENTdm if (!$xml = simplexml_load_file($filename)) { mdl_error("Could not read from OAI: $filename"); return(0); } if ($xml->error) { mdl_error("OAI Error Detected: $xml->error"); return(0); } // we start building some JavaScript to create an array for later $identifier_js = "var idArray=new Array("; // we walk down the XML tree looking for ListIdentifiers/header/identifier foreach ($xml->ListIdentifiers->header as $identifier_record) { // then we grab everything after the first slash, which should be the id number list($toss, $id) = split("/",$identifier_record->identifier); // save each id number to the identifier_array $identifier_array[$id] = $id; // and add each id number plus a comma to the JavaScript string $identifier_js = $identifier_js . "\"$id\","; } // close off the JavaScript string $identifier_js = $identifier_js . "\"\");"; return(1); } function clean_up($inString) { // it looks like MDL Reflections has a lot of "" in it // this function makes it easier to add other cleanup later global $config; if (in_array(strtolower($inString), $config[$config['use']][stoplist])) return(""); return str_replace('""','"',$inString); } function get_oai_data ($collection, $pointer) { global $contentdm_server, $config, $overall_tag; global $record_meta, $desc_fields; $record_meta = array(); $filename = "http://$contentdm_server/cgi-bin/oai.exe?verb=GetRecord&identifier=oai:reflections.mndigital.org:$collection/$pointer&metadataPrefix=oai_dc"; /* the namespaces in OAI records make this a big headache with SimpleXML */ $xml = new XMLReader(); $xml->open($filename); $assoc = xml2assoc($xml); $xml->close(); $error = $assoc[0]['value'][2]['attributes']['code']; if ($error) { mdl_error("OAI Error Detected: $error" . "
OAI response parsed into "
			. print_r($assoc)
			. "
" ); return(0); } /* start by putting the institution name in the tags */ if ($config[$config['use']]['tag']) $meta['tags'][] = '"'.$config[$config['use']]['tag'].'"'; if ($overall_tag) $meta['tags'][] = "\"$overall_tag\""; $usedtags[$config[$config['use']]['tag']] = true; /* this is a very peculiar dive into a particular point in the returned array */ foreach($assoc[0]['value'][2]['value'][0]['value'][1]['value'][0]['value'] as $item) { if($item['value']) { switch($item['tag']) { case 'dc:format': if ($item['value'] != "image/tiff") { $meta[$item['tag']][] = $item['value']; } break; case 'dc:identifier': if (!strncmp($item['value'], "http://", 7)) { $meta['url'][] = $item['value']; } else { $meta[$item['tag']][] = $item['value']; } break; case 'dc:subject': case 'dc:coverage': /* case 'dc:publisher': // it appears this is NOT the owner */ $meta[$item['tag']][] = $item['value']; $tags = preg_split("/--|;\s*|:\s*/", $item['value']); foreach($tags as $tag) { $cleantag = clean_up($tag); if ($cleantag && (!$usedtags[$cleantag])) { $meta['tags'][] = "\"".$cleantag."\""; $usedtags[$cleantag] = true; } } break; default: $meta[$item['tag']][] = clean_up($item['value']); } } } $record_meta['Title'] = join("; ", $meta['dc:title']); $record_meta['Tags'] = join(" ", $meta['tags']); $record_meta['Description'] = join("; ", $meta["dc:description"]) . "\n\n"; foreach($desc_fields as $key => $value) { if ($key == "dc:description") continue; if ($meta[$key]) { $record_meta['Description'] = $record_meta['Description'] . "$value: " . join("; ", $meta[$key]) . "\n"; } } $record_meta['Description'] = $record_meta['Description'] . "Link to our record: "; foreach ($meta['url'] as $url) { $record_meta['Description'] = $record_meta['Description'] . "$url"; } $record_meta['Description'] = $record_meta['Description'] . "\n\n"; $record_meta['Description'] = $record_meta['Description'] . $config[$config['use']]['statement']; // mdl_exit_with_error(print_r($meta, true).print_r($record_meta, true)); return(1); } function check_flickr() { global $flickr, $flickr_meta, $config; if (! ($config['apikey'] && $config['secret'] && $config[$config['use']]['token'])) { mdl_error("Settings for ".$config['use']." are missing Flickr configuration information."); return(0); } // Create new phpFlickr object $flickr = new phpFlickr($config['apikey'],$config['secret']); // $flickr->enableCache("db","mysql://[username]:[password]@[server]/[database]"); $flickr->setToken($config[$config['use']]['token']); $token = $flickr->auth_checkToken(); // Find the NSID of the username inputted via the form $flickr_meta['NSID'] = $token['user']['nsid']; // Get the friendly URL of the user's photos $flickr_meta['PhotosURL'] = $flickr->urls_getUserPhotos($flickr_meta['NSID']); $flickr_meta['Username'] = $token['user']['username']; // find our target photo set $targetsetname = $config[$config['use']]['photoset']; $photosets = $flickr->photosets_getList($flickr_meta['NSID']); foreach ((array)$photosets['photoset'] as $photoset) { if ($photoset['title'] == $targetsetname) { $targetsetid = $photoset['id']; } } if ($targetsetid) { $flickr_meta['Set'] = $targetsetid; return(1); } else { mdl_error("Flickr set \"".$config[$config['use']]['photoset']."\" not found " . "on " . $flickr_meta['Username'] . "'s account." ); return(0); } } function get_target_image ($collection, $pointer) { global $fsdir, $record_meta, $target_dim, $contentdm_server; $record_meta['Image'] = ""; // mdl_error("seeing $collection/$pointer"); return(0); // debugging placeholder /* calculate and retrieve image */ $scale = 10; $filename = "http://$contentdm_server/cgi-bin/getimage.exe?CISOROOT=/$collection&CISOPTR=$pointer&DMSCALE=$scale&DMWIDTH=20000&DMHEIGHT=20000"; // Get an initial image from which we learn the dimensions $jpeg_header_data = get_jpeg_header_data( $filename ); $jpeg_intrinsic_array = get_jpeg_intrinsic_values($jpeg_header_data); $width = $jpeg_intrinsic_array['Image Width']; $height = $jpeg_intrinsic_array['Image Height']; $largest_dimension = ($width > $height) ? $width : $height; $new_scale = ($target_dim * $scale) / $largest_dimension; // Now aim for the image at our target resolution $filename = "http://$contentdm_server/cgi-bin/getimage.exe?CISOROOT=/$collection&CISOPTR=$pointer&DMSCALE=$new_scale&DMWIDTH=20000&DMHEIGHT=20000"; $jpeg_header_data = get_jpeg_header_data( $filename ); $jpeg_intrinsic_array = get_jpeg_intrinsic_values($jpeg_header_data); $record_meta['Width'] = $jpeg_intrinsic_array['Image Width']; $record_meta['Height'] = $jpeg_intrinsic_array['Image Height']; // grab a local copy of the target file $newname = "$collection$pointer-" . date('ymd') . "-$target_dim.jpg"; $ch = curl_init ($filename); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); $rawdata=curl_exec ($ch); curl_close ($ch); $fp = fopen("$fsdir$newname",'w'); fwrite($fp, $rawdata); fclose($fp); $record_meta['Image'] = $newname; return(1); } function add_record_to_flickr () { global $fsdir, $record_meta, $flickr_meta, $flickr; if(! $flickr) { if (! check_flickr()) { mdl_exit_with_error(); } } $photoid = $flickr->sync_upload( $fsdir.$record_meta['Image'], $record_meta['Title'], $record_meta['Description'], $record_meta['Tags'], 0, 1, 1 ); if ($flickr->getErrorCode()) { mdl_error($flickr->getErrorMsg()); return(0); } $flickr->photosets_addPhoto($flickr_meta['Set'], $photoid); if ($flickr->getErrorCode()) { mdl_error($flickr->getErrorMsg()); return(0); } $record_meta['FlickrPhoto'] = $flickr->photos_getInfo($photoid); $record_meta['FlickrURL'] = $record_meta['FlickrPhoto']['urls']['url'][0]['_content']; $record_meta['FlickrSquare'] = $flickr->buildPhotoURL($record_meta['FlickrPhoto'], "Square"); return(1); }