Project

General

Profile

1
<?php
2

    
3
class metacatClient {
4
	
5
	var $metacatUrl;
6
	//A file that the server has r/w access to
7
	var $cookieJar = './cookiejar';
8
	//Status messages sent back from metacat, getMessages()
9
	var $messages;
10

    
11
	//Constructor is run when oject is instanciated
12
	function metacatClient( $url ) {
13
		$this->metacatUrl = $url;
14
	}//END Constructor
15
		
16
	function login( $username, $password ) {	
17
		$post_data_array = array (
18
			'action'	=>	'login',
19
			'qformat'	=>	'xml',
20
			'username'	=>	$username,
21
			'password'	=>	$password
22
		);
23
		$response = $this->_request( $post_data_array );
24
		if(strpos($response, "<login>")) {
25
			return 1;
26
		}else{
27
			return 0;
28
		}
29
	}//END login
30
	
31
	function logout() {
32
		$post_data_array = array (
33
			'action'	=>	'logout',
34
			'qformat'	=>	'xml',	
35
		);
36
		$response = $this->_request( $post_data_array );
37
		if(strpos($response, "<logout>")) {
38
			return 1;
39
		}else{
40
			return 0;
41
		}
42
	}//END logout
43
	
44
	function loginAndSetBrowserCookies($username, $password) {
45
		//There could be multiple people logging in, so more than one cookieJar referenced by username is needed.
46
		$this->cookieJar = '/tmp/metacatPHPClientCookies/'.$username;
47
		//Now that the cookieJar has a new path, we can use our standard methods 
48
		$this->login($username, $password);
49
		//Set browser cookies from $this->cookieJar using fopen(...path..) or by parsing 
50
		//$this->getMessages() for metacat's xml response
51
		//Delete that cookieJar to be safe
52
		//return true if login == true && rm cookieJar == true
53
	}
54
	
55
	function setCookieJarFromBrowser() {
56
		//Get session cookie from browser
57
		//Write it to $this->cookieJar 
58
	}
59
		
60
	function read( $docid ) {
61
		$post_data_array = array (
62
			'action'	=>	'read',
63
			'qformat'	=>	'xml',
64
			'docid'		=>  $docid
65
		);
66
		$response = $this->_request( $post_data_array );
67
		if(strpos($response, "<error>")) {
68
			return 0;
69
		}else{
70
			return $response;
71
		}
72
	}//END read
73
	
74
	/**
75
	 *	Pass a docid without revision, or pisco_data.1.1
76
	 *	becomes pisco_data.1
77
	*/
78
	function getNewestDocId( $docidWithoutRevision ) {
79
		$pieces = explode(".", $docidWithoutRevision);
80
		if (count($pieces) > 2) {
81
			$this->_setMessages("You included the revision number. Changing $docidWithoutRevision to ".$pieces[0].".".$pieces[1]);
82
		}
83
		$docidWithoutRevision = $pieces[0].".".$pieces[1];
84
		$xml = $this->read($docidWithoutRevision);
85
		if(!$xml) {
86
			$this->_setMessages('No package with this docId found');
87
			return false;
88
		}
89
		
90
		$packageIdParser = new PackageIdParser();
91
		$xml_parser = xml_parser_create();
92
		xml_set_object($xml_parser,&$packageIdParser); 
93
		xml_set_element_handler($xml_parser, "startElement", "endElement");
94
		xml_parse($xml_parser, $xml, true);
95
		xml_parser_free($xml_parser);
96
		
97
		if($packageId = $packageIdParser->packageId) {
98
			return $packageId;
99
		}else{
100
			$this->_setMessages('No packageId found.');
101
			return false;
102
		}
103
	}
104
	
105
	function squery( $pathquery_doc ) {
106
		$post_data_array = array (
107
			'action'	=>	'squery',
108
			'qformat'	=>	'xml',
109
			'query'		=>  $pathquery_doc
110
		);
111
		$response = $this->_request( $post_data_array );
112
		if($response) {
113
			return $response;
114
		}else{
115
			return 0;
116
		}
117
	}//END squery
118

    
119
	function insert( $docid, $doctext ) {
120
		$post_data_array = array (
121
			'action'	=>	'insert',
122
			'docid'		=>  $docid,
123
			'doctext'	=>	$doctext
124
		);
125
		$response = $this->_request( $post_data_array );
126
		if(strpos($response, "<success>")) {
127
			return 1;
128
		}else{
129
			return 0;
130
		}
131
	}//END insert
132
	
133
	function update( $docid, $doctext ) {
134
		$post_data_array = array (
135
			'action'	=>	'update',
136
			'docid'		=>  $docid,
137
			'doctext'	=>	$doctext
138
		);
139
		$response = $this->_request( $post_data_array );
140
		if(strpos($response, "<success>")) {
141
			return 1;
142
		}else{
143
			return 0;
144
		}
145
	}//END update
146
	
147
	function delete( $docid ) {
148
		$post_data_array = array (
149
			'action'	=>	'delete',
150
			'docid'		=>  $docid,
151
		);
152
		$response = $this->_request( $post_data_array );
153
		if(strpos($response, "<success>")) {
154
			return 1;
155
		}else{
156
			return 0;
157
		}
158
	}//END delete
159
	
160
	function getMessages() {
161
		return $this->messages;
162
	}//END getMessages
163
	
164
	function _setMessages( $response ) {
165
		$this->messages .= " \n ".$response;
166
	}//END _set_messages
167
	
168
	function _request( $post_data_array ) {
169
		$request = curl_init();
170
		curl_setopt($request, CURLOPT_URL, $this->metacatUrl);
171
		curl_setopt($request, CURLOPT_COOKIEFILE, $this->cookieJar);
172
		curl_setopt($request, CURLOPT_COOKIEJAR, $this->cookieJar);
173
		curl_setopt($request, CURLOPT_POST, true);
174
		curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
175
		curl_setopt($request, CURLOPT_USERAGENT, 'PHP metacat client (version 1.0)');
176
		
177
		$post_data = $this->_encode_post_data($post_data_array);
178
		curl_setopt ($request, CURLOPT_POSTFIELDS, $post_data);
179
		$response = curl_exec($request);
180
		$this->_setMessages($response);
181
		return $response;
182
	}//END _request
183
	
184
	function _encode_post_data( $post_data_array ) {
185
		foreach($post_data_array as $arg => $value) {	
186
			$post_data_array[$arg] = $arg."=".urlencode($value);
187
		}
188
		$post_data = implode('&', $post_data_array);
189
		return $post_data;
190
	}//END _encode_post_data
191
	
192
		
193
}
194

    
195
class PackageIdParser {
196
	var $packageId;
197
	
198
	function startElement($parser, $name, $attrs) {
199
		if($name == "EML:EML") {
200
			$this->packageId = $attrs['PACKAGEID'];
201
		}
202
	}
203

    
204
	function endElement($parser, $name) {
205
	}
206
}
207

    
208
?>
    (1-1/1)