1: <?php
2:
3: /**
4: * Description of ISGNRegquest
5: *
6: * @author sfrenzel
7: * @property String $baseurl Landing page adress varaible to concatinate with igsnnnumber
8: * @property String $username Credential for endpoint
9: * @property String $password Credential for endpoint
10: * @property String $method POST GET (HTTP verbs)
11: * @property String $header HTTP-header
12: * @property String $payload content of HTTP-request (xml)
13: * @property String $endpoint Adress of IGSN-Application
14: * Errorcode return
15: * @todo //replaced echo messages during development in will be replaced log
16: * 0 ist success
17: * 1 IGSN not minted
18: * 2 Error IGSN regmetadatdata & metadata not stored
19: * 3 IGSN Sample Metadata not stored
20: * 9 This should never happen
21: * http:
22: * 2xx sucessfuly
23: * 400 -500 Errors
24: * /
25: */
26: class IGSNRestRequest {
27:
28: private $baseurl;
29: private $username;
30: private $password;
31: private $method;
32: private $header;
33: private $payload;
34: private $endpoint;
35:
36: /**
37: * Construtor with Connectionsset
38: * @param type $baseurl (basis landing page address
39: * @param type $username
40: * @param type $password
41: * @param type $endpoint
42: */
43: public function __construct($baseurl, $username, $password, $endpoint) {
44: $this->baseurl = $baseurl;
45: $this->username = $username;
46: $this->password = $password;
47: $this->endpoint = $endpoint;
48: $this->header = array("Content-type: application/xml;charset=UTF-8", "Accept: application/xml", "Authorization: Basic " . base64_encode($username . ":" . $password));
49: }
50:
51: /**
52: * @param type $igsn
53: * @param type $regmetadata
54: * @param type $descripmetadata
55: * @return int
56: */
57: public function registerIGSNGFZ($igsn, $regmetadata, $descripmetadata) {
58: $mintcode = $this->mintdoi($igsn);
59: if ($mintcode == 201) {
60: $regcode = $this->upload_regmetadata($regmetadata);
61: if ($regcode == 201) {
62: $metadata = $this->upload_metadata($descripmetadata, $igsn);
63: if ($metadata == 201) {
64: echo "Sucuess IGSN registered";
65: return 0;
66: } else {
67: echo "Error IGSN Sample Metadata not stored ";
68: return 3;
69: }
70: } else {
71: echo "Error IGSN regmetadatdata & metadata not stored ";
72: return 2;
73: }
74: } else {
75: echo "Error IGSN not even minted ";
76: return 1;
77: }
78: echo "This should never happen!";
79: return 9;
80: }
81:
82: /**
83: * register without sample Metadaten
84: * @param type $igsn
85: * @param type $regmetadata
86: * @return int
87: */
88: public function registerPlainIGSN($igsn, $regmetadata) {
89: $mintcode = $this->mintdoi($igsn);
90: if ($mintcode == 201) {
91: $regcode = $this->upload_regmetadata($regmetadata);
92: if ($regcode == 201) {
93: echo "Sucuess IGSN registered";
94: return 0;
95: } else {
96: echo "Error IGSN regmetadatdata";
97: return 2;
98: }
99: } else {
100: echo "Error IGSN not even minted ";
101: return 1;
102: }
103: echo "This should never happen!";
104: return 9;
105: }
106:
107: public function updateAllMetadata($igsn, $regmetadata, $descripmetada) {
108: $regcode = $this->upload_regmetadata($regmetadata);
109: if ($regcode == 201) {
110: $metadata = $this->upload_metadata($descripmetada, $igsn);
111: if ($metadata == 201) {
112: echo "Sucuess IGSN Metadata updated";
113: return 0;
114: } else {
115: echo "Error IGSN Sample Metadata not stored ";
116: return 2;
117: }
118: } else {
119:
120: echo "Error IGSN regmetadatdata & metadata not stored ";
121: return 3;
122: }
123: echo "This should never happen!";
124: return 9;
125: }
126:
127: /**
128: * Send igsn and Landingpage (webadresse with igsn)
129: * @param type $igsn
130: * @return type httprequest answercode
131: */
132: public function mintdoi($igsn) {
133: $this->url = $this->endpoint . "igsn";
134: $this->method = "POST";
135: $this->payload = "igsn=10273/" . $igsn . "\n" . "url=" . $this->baseurl . $igsn;
136: $result = $this->httprequest();
137: var_dump($result);
138: $resulthttp_code = $result["header"]["http_code"];
139: echo "mint";
140: var_dump($resulthttp_code);
141: return($resulthttp_code);
142: }
143:
144: /**
145: * Upload IGSN Registration schema
146: * @param type $regmetadata
147: * schema http://igsn.org/schema/kernel-v.1.0 http://doidb.wdc-terra.org/igsn/schemas/igsn.org/schema/1.0/igsn.xsd
148: * @return int http code
149: */
150: public function upload_regmetadata($regmetadata) {
151: echo(gettype($regmetadata));
152: $this->url = $this->endpoint . "metadata";
153: $this->method = "POST";
154: $this->payload = $regmetadata;
155: $result = $this->httprequest();
156: $resulthttp_code = $result["header"]["http_code"];
157: echo "reg";
158: var_dump($resulthttp_code);
159: var_dump($result);
160: return($resulthttp_code);
161: }
162:
163: /**
164: * Upload IGSN descriptiv schema
165: * ressource schema:
166: * sample schema:
167: * @param type $igsnmetadata
168: * @param type $igsn
169: * @return int http_code
170: */
171: public function upload_metadata($igsnmetadata, $igsn) {
172: $this->url = $this->endpoint . "igsnmetadata/10273/" . $igsn;
173: $this->method = "POST";
174: $this->payload = $igsnmetadata;
175: $result = $this->httprequest();
176: $resulthttp_code = $result["header"]["http_code"];
177: echo "meta";
178: var_dump($result);
179: var_dump($resulthttp_code);
180: return($resulthttp_code);
181: }
182:
183: public function fetchIGSNlist() {
184: $this->url = $this->endpoint . "igsn";
185: $this->method = "GET";
186: $result = $this->httprequest();
187: $resultContentString = $result["content"];
188: $resultContent = explode("\n", $resultContentString);
189: return $resultContent;
190: }
191:
192: public function fetchRegMeta($igsn) {
193: $this->url = $this->endpoint . "metadata/" . $igsn;
194: $this->method = "GET";
195: $result = $this->httprequest();
196:
197: if(strcasecmp($result["content"], 'no metadata for the IGSN')===0){
198: return false;
199: }
200: $resultXML = simplexml_load_string($result["content"]);
201: return $resultXML;
202: }
203:
204: public function fetchIGSNMeta($igsn) {
205: $this->url = $this->endpoint . "igsnmetadata/" . $igsn;
206: $this->method = "GET";
207: $result = $this->httprequest();
208: $resultXMLString = $result["content"];
209: if(strcasecmp($result["content"], 'no IGSN metadata for the IGSN')===0){
210: echo 'no Metadata file';
211: return false;
212: }
213: $resultXML = simplexml_load_string($result["content"]);
214: return $resultXML;
215:
216: }
217:
218: /**
219: * revolve existing IGSN
220: * useful to prevent reassigning IGSN twice
221: * @param type $igsn
222: * @return int http_code
223: *
224: */
225: public function resloveigsn($igsn) {
226: $this->url = $this->endpoint . "igsn/10273/" . $igsn;
227: $this->method = "GET";
228: $result = $this->httprequest();
229: $resulthttp_code = $result["header"]["http_code"];
230: // echo $resulthttp_code;
231: //return ($result);
232: return($resulthttp_code);
233: }
234:
235: /**
236:
237: * @todo speziell function to deal with parent
238: * * @draf
239: * @param type $parent
240: */
241: public function registerparent($parent) {
242: $result = $this->resloveigsn($parent);
243: $resulthttp_code = $result["header"]["http_code"];
244: var_dump($resulthttp_code);
245: }
246:
247: /**
248: * Perform Rest-Requests and return result
249: * @return array - result array of request
250: *
251: */
252: public function httprequest() {
253: $ch = curl_init();
254: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
255: curl_setopt($ch, CURLOPT_URL, $this->url);
256: if (isset($this->header))
257: curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
258:
259: switch ($this->method) {
260: case "GET":
261: //echo("debug GETS");
262: curl_setopt($ch, CURLOPT_HTTPGET, true);
263: break;
264: case "POST":
265: curl_setopt($ch, CURLOPT_POST, true);
266: curl_setopt($ch, CURLOPT_POSTFIELDS, $this->payload);
267: break;
268: case "PUT":
269: curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
270: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
271: curl_setopt($ch, CURLOPT_POSTFIELDS, $this->payload);
272: break;
273: case "DELETE":
274: //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
275: break;
276: default:
277: echo 'No methode in Header for' . $this->igsn;
278: }
279:
280: curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
281:
282: $result["content"] = curl_exec($ch);
283: $result["errcode"] = curl_errno($ch);
284: $result["errmsg"] = curl_error($ch);
285: $result["header"] = curl_getinfo($ch);
286: curl_close($ch);
287: return $result;
288: }
289:
290:
291:
292: }
293: