예제 #1
0
 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;
 }
예제 #2
0
 /**
  * Check the availability of the DOI service and upstream DataCite service
  * Note: this is an indication of service availability, not a guarantee
  *       that subsequent mints will be processed successfully. 
  *
  * @author Ben Greenwood <*****@*****.**>
  */
 function status()
 {
     // XXX: These parameter getters should be in a constructor somewhere
     $response_type = $this->input->get('response_type') ?: "string";
     $api_version = $this->input->get('api_version') ?: "1.0";
     $response_status = true;
     $timer_start = microtime(true);
     // Ping the local DOI database
     if (!$this->doi_db->simple_query("SELECT 1;")) {
         $response_status = false;
     }
     // Ping DataCite DOI HTTPS service
     if (!$this->_isDataCiteAlive(self::DATACITE_WAIT_TIMEOUT_SECONDS)) {
         $response_status = false;
     }
     // Send back our response
     if ($response_status) {
         $response_time = round(microtime(true) - $timer_start, 3) * 1000;
         echo doisGetUserMessage("MT090", NULL, $response_type, NULL, "(took " . $response_time . "ms)");
         return;
     } else {
         echo doisGetUserMessage("MT091", NULL, $response_type);
         return;
     }
 }