public function mint() { $ip_address = trim($_SERVER['REMOTE_ADDR']); $xml = ''; $errorMessages = ''; $notifyMessage = ''; $logMessage = ''; $outstr = ''; $doiObjects = null; $response1 = "OK"; $response2 = "OK"; $app_id = $this->input->get('app_id'); //passed as a parameter $urlValue = $this->input->get('url'); //passed as a parameter $client_id = checkDoisValidClient($ip_address, $app_id); $clientDetails = getDoisClient($app_id); foreach ($clientDetails->result() as $clientDetail) { if ($clientDetail->client_id < '10') { $client_id2 = "0" . $clientDetail->client_id; } else { $client_id2 = $clientDetail->client_id; } } $datacite_prefix = $clientDetail->datacite_prefix; $doiValue = strtoupper($datacite_prefix . $client_id2 . '/' . uniqid()); //generate a unique suffix for this doi for this client if ($_POST) { if (str_replace("<?xml version=", "", implode($_Post)) == implode($_Post)) { $xml = "<?xml version=" . implode($_Post); // passed as posted content } else { $xml = implode($_POST); // passed as posted content } } if ($xml == '') { $xml = ''; } $doiObjects = new DOMDocument(); $result = $doiObjects->loadXML($xml); $errors = error_get_last(); // we need to insert the determined doi value into the xml string to be sent to datacite // so we create a new 'identifier' element, set the identifierType attribute to DOI and // replace the current identifier element then write out to the xml string that is passed $currentIdentifier = $doiObjects->getElementsByTagName('identifier'); for ($i = 0; $i < $currentIdentifier->length; $i++) { $doiObjects->getElementsByTagName('resource')->item(0)->removeChild($currentIdentifier->item($i)); } $newdoi = $doiObjects->createElement('identifier', $doiValue); $newdoi->setAttribute('identifierType', "DOI"); $doiObjects->getElementsByTagName('resource')->item(0)->insertBefore($newdoi, $doiObjects->getElementsByTagName('resource')->item(0)->firstChild); //$xml = $doiObjects->saveXML(); if ($errors) { $errorMessages .= "Document Load Error: " . $errors['message'] . "\n"; header("HTTP/1.0 500 Internal Server Error"); } else { // Validate it against the datacite schema. error_reporting(0); // Create temporary file and save manually created DOMDocument. $tempFile = "/tmp/" . time() . '-' . rand() . '-document.tmp'; $doiObjects->save($tempFile); // Create temporary DOMDocument and re-load content from file. $doiObjects = new DOMDocument(); $doiObjects->load($tempFile); //Delete temporary file. if (is_file($tempFile)) { unlink($tempFile); } $result = $doiObjects->schemaValidate(gCMD_SCHEMA_URI); $xml = $doiObjects->saveXML(); $errors = error_get_last(); if ($errors) { $errorMessages .= doisGetUserMessage("MT006", $doi_id = NULL); $errorMessages .= "Document Validation Error: " . $errors['message'] . "\n"; header("HTTP/1.0 500 Internal Server Error"); } } if (!$client_id) { $errorMessages .= doisGetUserMessage("MT009", $doi_id = NULL); header("HTTP/1.0 415 Authentication Error"); } if ($urlValue == '') { $errorMessages .= "URL is a mandatory value to mint a doi.<br />"; header("HTTP/1.0 500 Internal Server Error"); } if ($errorMessages == '') { // Insert doi information into the database $insertResult = importDoiObject($doiObjects, $urlValue, $client_id, $created_who = 'SYSTEM', $status = 'REQUESTED', $xml); if (!$insertResult) { // Mint the DOI. $response = $this->doisRequest("mint", $doiValue, $urlValue, $xml, $client_id); if ($response) { if ($response == gDOIS_RESPONSE_SUCCESS) { // We have successfully minted the doi through datacite. $response = $this->doisRequest("update", $doiValue, $urlValue, $xml, $client_id); if ($response == gDOIS_RESPONSE_SUCCESS) { $notifyMessage = $this->doisGetUserMessage("MT001", $doiValue); $status = "ACTIVE"; $activateResult = setDoiStatus($doiValue, $status); header("HTTP/1.0 200 OK"); } else { $errorMessages .= $this->doisGetUserMessage("MT010", $doi = NULL); $logMessage = "MT010 " . $response; header("HTTP/1.0 500 Internal Server Error"); } } else { $errorMessages .= $this->doisGetUserMessage("MT010", $doi = NULL); $logMessage = "MT010 " . $response; header("HTTP/1.0 500 Internal Server Error"); } } else { $errorMessages .= $this->doisGetUserMessage("MT005", $doi = NULL); header("HTTP/1.0 500 Internal Server Error"); } } else { $errorMessages .= '..<br />' . $insertResult; header("HTTP/1.0 500 Internal Server Error"); } } if ($errorMessages) { $outstr = $errorMessages; //We need to log this activity as errorred if ($logMessage) { $errorMessages .= $logMessage; } insertDoiActivity("MINT", $doiValue, "FAILURE", $client_id, $errorMessages); } if ($notifyMessage) { //We need to log this activity insertDoiActivity("MINT", $doiValue, "SUCCESS", $client_id, $notifyMessage); $outstr = $notifyMessage; } //we now need to return the result back to the calling program. header('Content-type: text/html'); echo $outstr; }
function mint() { $dataciteSchema = $this->config->item('gCMD_SCHEMA_URIS'); if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else { if (isset($_SERVER["HTTP_CLIENT_IP"])) { $ip = $_SERVER["HTTP_CLIENT_IP"]; } else { if (isset($_SERVER["REMOTE_ADDR"])) { $ip = $_SERVER["REMOTE_ADDR"]; } else { // Run by command line?? $ip = "127.0.0.1"; } } } $xml = ''; $client_id = ''; $errorMessages = ''; $notifyMessage = ''; $logMessage = ''; $outstr = ''; $doiValue = ''; $verbosemessage = ''; $errors = ''; $testing = 'no'; $doiObjects = null; $response1 = "OK"; $response2 = "OK"; $debug = $this->input->get('debug'); $manual_mint = $this->input->get('manual_mint'); if ($manual_mint) { $doiValue = rawurldecode($this->input->get_post('doi_id')); $client_id = rawurldecode($this->input->get_post('client_id')); } if ($debug && $debug == 'true') { $this->debugOn(); } $app_id = $this->getAppId(); if (substr($app_id, 0, 4) == 'TEST') { $app_id = substr($app_id, 4, strlen($app_id)); $testing = 'yes'; } $urlValue = $this->input->get('url'); //passed as a parameter $urlValue = rawurldecode($urlValue); $response_type = $this->input->get('response_type'); if (!$response_type) { $response_type = 'string'; } $api_version = $this->input->get('api_version'); if (!$api_version) { $api_version = '1.0'; } //first up, lets check that this client is permitted to update this doi. if (!$app_id) { $errorMessages = doisGetUserMessage("MT010", $doiValue = NULL, $response_type, $app_id, "You must provide an app id to mint a doi", $urlValue); } if ($urlValue == '' && $errorMessages == '') { $verbosemessage = 'You must provide a url when minting a doi.'; $errorMessages = doisGetUserMessage("MT010", $doi_id = NULL, $response_type, $app_id, $verbosemessage, $urlValue); //"URL is a mandatory value to mint a doi.<br />"; } if ($errorMessages == '') { if (!$manual_mint) { $client_id = checkDoisValidClient($ip, trim($app_id)); } if ($client_id === false) { $verbosemessage = 'Client with app_id ' . $app_id . ' from ip address ' . $ip . ' is not a registered doi client.'; $errorMessages = doisGetUserMessage("MT009", $doi_id = NULL, $response_type, $app_id, $verbosemessage, $urlValue); } $xml = $this->getXmlInput(); if (!$xml) { $xml = ''; $verbosemessage = 'You must post xml when minting a doi.'; $errorMessages = doisGetUserMessage("MT010", $doi_id = NULL, $response_type, $app_id, $verbosemessage, $urlValue); } } if (!$errorMessages) { $clientDetails = getDoisClient($app_id); foreach ($clientDetails->result() as $clientDetail) { if ($clientDetail->client_id < '10') { $client_id2 = "0" . $clientDetail->client_id; } else { $client_id2 = $clientDetail->client_id; } $client_domains = getClientDomains($clientDetail->client_id); } if ($testing == 'yes') { $datacite_prefix = "10.5072/"; } else { $datacite_prefix = $clientDetail->datacite_prefix; } if (!$manual_mint) { $doiValue = strtoupper($datacite_prefix . $client_id2 . '/' . uniqid()); } //generate a unique suffix for this doi for this client $doiObjects = new DOMDocument(); $result = $doiObjects->loadXML($xml); $resources = $doiObjects->getElementsByTagName('resource'); $theSchema = 'unknown'; if ($resources->length > 0) { if (isset($resources->item(0)->attributes->item(0)->name)) { $theSchema = $this->getXmlSchema($resources->item(0)->attributes->item(0)->nodeValue); } } if ($theSchema == "unknown") { $errors['message'] = "You have not provided a known schema location in your xml"; } if ($errors) { $verbosemessage = "Document Load Error: " . $errors['message']; $errorMessages .= doisGetUserMessage("MT010", $doi_id = NULL, $response_type, $app_id, $verbosemessage, $urlValue); } else { $errors = error_get_last(); // we need to insert the determined doi value into the xml string to be sent to datacite // so we create a new 'identifier' element, set the identifierType attribute to DOI and // replace the current identifier element then write out to the xml string that is passed $currentIdentifier = $doiObjects->getElementsByTagName('identifier'); for ($i = 0; $i < $currentIdentifier->length; $i++) { $doiObjects->getElementsByTagName('resource')->item(0)->removeChild($currentIdentifier->item($i)); } $newdoi = $doiObjects->createElement('identifier', $doiValue); $newdoi->setAttribute('identifierType', "DOI"); $doiObjects->getElementsByTagName('resource')->item(0)->insertBefore($newdoi, $doiObjects->getElementsByTagName('resource')->item(0)->firstChild); } if ($errors) { $verbosemessage = "Document Load Error: " . $errors['message']; $errorMessages = doisGetUserMessage("MT010", $doi_id = NULL, $response_type, $app_id, $verbosemessage, $urlValue); } else { // Validate it against the datacite schema. error_reporting(0); // Create temporary file and save manually created DOMDocument. $tempFile = "/tmp/" . time() . '-' . rand() . '-document.tmp'; $doiObjects->save($tempFile); // Create temporary DOMDocument and re-load content from file. $doiObjects = new DOMDocument(); $doiObjects->load($tempFile); //Delete temporary file. if (is_file($tempFile)) { unlink($tempFile); } $result = $doiObjects->schemaValidate(asset_url('schema') . $dataciteSchema[$theSchema]); $xml = $doiObjects->saveXML(); $errors = error_get_last(); if ($errors || !$result) { $verbosemessage = "Document Validation Error: " . $errors['message']; $errorMessages = doisGetUserMessage("MT006", $doi_id = NULL, $response_type, $app_id, $verbosemessage, $urlValue); } } if (!$errors) { //ensure provided url is valid with registered top level domain if (!$this->validDomain($urlValue, $client_domains)) { $verbosemessage = 'URL not permitted.'; $errorMessages = doisGetUserMessage("MT014", $doi_id = NULL, $response_type, $app_id, $verbosemessage, $urlValue); } } } if ($errorMessages == '') { // Insert doi information into the database $insertResult = importDoiObject($doiObjects, $urlValue, $client_id, $created_who = 'SYSTEM', $status = 'REQUESTED', $xml); if (!$insertResult) { /* Fix: 09/01/2013, DataCite requires metadata FIRST, then DOI call */ // Send DataCite the metadata first $response = $this->doisRequest("update", $doiValue, $urlValue, $xml, $client_id); if ($response) { if (doisGetResponseType($response) == gDOIS_RESPONSE_SUCCESS) { // Now ask to mint the DOI $response = $this->doisRequest("mint", $doiValue, $urlValue, $xml, $client_id); if (doisGetResponseType($response) == gDOIS_RESPONSE_SUCCESS) { $notifyMessage = doisGetUserMessage("MT001", $doiValue, $response_type, $app_id, $response, $urlValue); $status = "ACTIVE"; $activateResult = setDoiStatus($doiValue, $status); } else { $errorMessages .= doisGetUserMessage("MT010", $doiValue, $response_type, $app_id, $response, $urlValue); } } else { $errorMessages .= doisGetUserMessage("MT010", $doiValue, $response_type, $app_id, $response, $urlValue); } } else { $errorMessages .= doisGetUserMessage("MT005", $doiValue, $response_type, $app_id, $response, $urlValue); } } else { $errorMessages .= '..<br />' . $insertResult; } } if ($errorMessages) { $outstr = $errorMessages; //We need to log this activity as errorred insertDoiActivity("MINT", $doiValue, "FAILURE", $client_id = 0, $errorMessages); } if ($notifyMessage) { //We need to log this activity insertDoiActivity("MINT", $doiValue, "SUCCESS", $client_id, $notifyMessage); $outstr = $notifyMessage; } //we now need to return the result back to the calling program. echo $outstr; }