1
|
<?php
|
2
|
// Copyright: 2006 Regents of the University of California,
|
3
|
// Partnership for Interdisciplinary Studies of Coastal Oceans
|
4
|
// http://www.piscoweb.org
|
5
|
//
|
6
|
// This program is free software; you can redistribute it and/or modify
|
7
|
// it under the terms of the GNU General Public License as published by
|
8
|
// the Free Software Foundation; either version 2 of the License, or
|
9
|
// (at your option) any later version.
|
10
|
//
|
11
|
// This program is distributed in the hope that it will be useful,
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
// GNU General Public License for more details.
|
15
|
//
|
16
|
// You should have received a copy of the GNU General Public License
|
17
|
// along with this program; if not, write to the Free Software
|
18
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
19
|
// 02111-1307 USA
|
20
|
class metacatClient {
|
21
|
|
22
|
var $metacatUrl;
|
23
|
//A file that the server has r/w access to
|
24
|
var $cookieJar = './cookiejar';
|
25
|
//Status messages sent back from metacat, getMessages()
|
26
|
var $messages;
|
27
|
|
28
|
//Constructor is run when oject is instanciated
|
29
|
function metacatClient( $url ) {
|
30
|
$this->metacatUrl = $url;
|
31
|
}//END Constructor
|
32
|
|
33
|
function login( $username, $password ) {
|
34
|
$post_data_array = array (
|
35
|
'action' => 'login',
|
36
|
'qformat' => 'xml',
|
37
|
'username' => $username,
|
38
|
'password' => $password
|
39
|
);
|
40
|
$response = $this->_request( $post_data_array );
|
41
|
if(strpos($response, "<login>")) {
|
42
|
return 1;
|
43
|
}else{
|
44
|
return 0;
|
45
|
}
|
46
|
}//END login
|
47
|
|
48
|
function logout() {
|
49
|
$post_data_array = array (
|
50
|
'action' => 'logout',
|
51
|
'qformat' => 'xml',
|
52
|
);
|
53
|
$response = $this->_request( $post_data_array );
|
54
|
if(strpos($response, "<logout>")) {
|
55
|
return 1;
|
56
|
}else{
|
57
|
return 0;
|
58
|
}
|
59
|
}//END logout
|
60
|
|
61
|
function loginAndSetBrowserCookies($username, $password) {
|
62
|
//There could be multiple people logging in, so more than one cookieJar referenced by username is needed.
|
63
|
$this->cookieJar = '/tmp/metacatPHPClientCookies/'.$username;
|
64
|
//Now that the cookieJar has a new path, we can use our standard methods
|
65
|
$this->login($username, $password);
|
66
|
//Set browser cookies from $this->cookieJar using fopen(...path..) or by parsing
|
67
|
//$this->getMessages() for metacat's xml response
|
68
|
//Delete that cookieJar to be safe
|
69
|
//return true if login == true && rm cookieJar == true
|
70
|
}
|
71
|
|
72
|
function setCookieJarFromBrowser() {
|
73
|
//Get session cookie from browser
|
74
|
//Write it to $this->cookieJar
|
75
|
}
|
76
|
|
77
|
function read( $docid ) {
|
78
|
$post_data_array = array (
|
79
|
'action' => 'read',
|
80
|
'qformat' => 'xml',
|
81
|
'docid' => $docid
|
82
|
);
|
83
|
$response = $this->_request( $post_data_array );
|
84
|
if(strpos($response, "<error>")) {
|
85
|
return 0;
|
86
|
}else{
|
87
|
return $response;
|
88
|
}
|
89
|
}//END read
|
90
|
|
91
|
/**
|
92
|
* Pass a docid without revision, or pisco_data.1.1
|
93
|
* becomes pisco_data.1
|
94
|
*/
|
95
|
function getNewestDocId( $docidWithoutRevision ) {
|
96
|
$pieces = explode(".", $docidWithoutRevision);
|
97
|
if (count($pieces) > 2) {
|
98
|
$this->_setMessages("You included the revision number. Changing $docidWithoutRevision to ".$pieces[0].".".$pieces[1]);
|
99
|
}
|
100
|
$docidWithoutRevision = $pieces[0].".".$pieces[1];
|
101
|
$xml = $this->read($docidWithoutRevision);
|
102
|
if(!$xml) {
|
103
|
$this->_setMessages('No package with this docId found');
|
104
|
return false;
|
105
|
}
|
106
|
|
107
|
$packageIdParser = new PackageIdParser();
|
108
|
$xml_parser = xml_parser_create();
|
109
|
xml_set_object($xml_parser,&$packageIdParser);
|
110
|
xml_set_element_handler($xml_parser, "startElement", "endElement");
|
111
|
xml_parse($xml_parser, $xml, true);
|
112
|
xml_parser_free($xml_parser);
|
113
|
|
114
|
if($packageId = $packageIdParser->packageId) {
|
115
|
return $packageId;
|
116
|
}else{
|
117
|
$this->_setMessages('No packageId found.');
|
118
|
return false;
|
119
|
}
|
120
|
}
|
121
|
|
122
|
function squery( $pathquery_doc ) {
|
123
|
$post_data_array = array (
|
124
|
'action' => 'squery',
|
125
|
'qformat' => 'xml',
|
126
|
'query' => $pathquery_doc
|
127
|
);
|
128
|
$response = $this->_request( $post_data_array );
|
129
|
if($response) {
|
130
|
return $response;
|
131
|
}else{
|
132
|
return 0;
|
133
|
}
|
134
|
}//END squery
|
135
|
|
136
|
function insert( $docid, $doctext ) {
|
137
|
$post_data_array = array (
|
138
|
'action' => 'insert',
|
139
|
'docid' => $docid,
|
140
|
'doctext' => $doctext
|
141
|
);
|
142
|
$response = $this->_request( $post_data_array );
|
143
|
if(strpos($response, "<success>")) {
|
144
|
return 1;
|
145
|
}else{
|
146
|
return 0;
|
147
|
}
|
148
|
}//END insert
|
149
|
|
150
|
function update( $docid, $doctext ) {
|
151
|
$post_data_array = array (
|
152
|
'action' => 'update',
|
153
|
'docid' => $docid,
|
154
|
'doctext' => $doctext
|
155
|
);
|
156
|
$response = $this->_request( $post_data_array );
|
157
|
if(strpos($response, "<success>")) {
|
158
|
return 1;
|
159
|
}else{
|
160
|
return 0;
|
161
|
}
|
162
|
}//END update
|
163
|
|
164
|
function delete( $docid ) {
|
165
|
$post_data_array = array (
|
166
|
'action' => 'delete',
|
167
|
'docid' => $docid,
|
168
|
);
|
169
|
$response = $this->_request( $post_data_array );
|
170
|
if(strpos($response, "<success>")) {
|
171
|
return 1;
|
172
|
}else{
|
173
|
return 0;
|
174
|
}
|
175
|
}//END delete
|
176
|
|
177
|
function getMessages() {
|
178
|
return $this->messages;
|
179
|
}//END getMessages
|
180
|
|
181
|
function _setMessages( $response ) {
|
182
|
$this->messages .= " \n ".$response;
|
183
|
}//END _set_messages
|
184
|
|
185
|
function _request( $post_data_array ) {
|
186
|
$request = curl_init();
|
187
|
curl_setopt($request, CURLOPT_URL, $this->metacatUrl);
|
188
|
curl_setopt($request, CURLOPT_COOKIEFILE, $this->cookieJar);
|
189
|
curl_setopt($request, CURLOPT_COOKIEJAR, $this->cookieJar);
|
190
|
curl_setopt($request, CURLOPT_POST, true);
|
191
|
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
|
192
|
curl_setopt($request, CURLOPT_USERAGENT, 'PHP metacat client (version 1.0)');
|
193
|
|
194
|
$post_data = $this->_encode_post_data($post_data_array);
|
195
|
curl_setopt ($request, CURLOPT_POSTFIELDS, $post_data);
|
196
|
$response = curl_exec($request);
|
197
|
$this->_setMessages($response);
|
198
|
return $response;
|
199
|
}//END _request
|
200
|
|
201
|
function _encode_post_data( $post_data_array ) {
|
202
|
foreach($post_data_array as $arg => $value) {
|
203
|
$post_data_array[$arg] = $arg."=".urlencode($value);
|
204
|
}
|
205
|
$post_data = implode('&', $post_data_array);
|
206
|
return $post_data;
|
207
|
}//END _encode_post_data
|
208
|
|
209
|
|
210
|
}
|
211
|
|
212
|
class PackageIdParser {
|
213
|
var $packageId;
|
214
|
|
215
|
function startElement($parser, $name, $attrs) {
|
216
|
if($name == "EML:EML") {
|
217
|
$this->packageId = $attrs['PACKAGEID'];
|
218
|
}
|
219
|
}
|
220
|
|
221
|
function endElement($parser, $name) {
|
222
|
}
|
223
|
}
|
224
|
|
225
|
?>
|