Mercurial > hg > marc_php
annotate marccore.php @ 3:5c8c4fa95803 draft
Added support for transfer chaining and some bugfixes
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Mon, 17 Nov 2014 01:19:05 +0100 |
parents | 3ac7bd7495fd |
children | c642254dc9ee |
rev | line source |
---|---|
0 | 1 <?php |
2 if (!function_exists('hex2bin')) { function hex2bin($h) { return pack("H*", $h); } } | |
3 | |
4 class MARCUpdate implements ArrayAccess { | |
5 private $_version, $_key, $_serial, $_label, $_extensions, $_value = FALSE, $_valueoffset = -1, $_message, $_tags = array(); | |
6 public function __get($name) { | |
7 switch (strtolower($name)) { | |
8 case 'version': return $this->_version; | |
9 case 'key': return $this->_key; | |
10 case 'serial': return $this->_serial; | |
11 case 'label': return $this->_label; | |
12 case 'expiration': return (isset($this->_extensions[4]) && strlen($this->_extensions[4]) >= 4) ? marc_decode_int32be($this->_extensions[4]) : NULL; | |
13 case 'transfer': return isset($this->_extensions[1]) ? $this->_extensions[1] : NULL; | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
14 case 'transferchain': return isset($this->_extensions[5]) ? $this->_extensions[5] : NULL; |
0 | 15 case 'value': |
16 if ($this->_value === FALSE) $this->_value = self::DecodeValue($this->_message, $this->_valueoffset); | |
17 return $this->_value; | |
18 case 'updatemessage': return $this->_message; | |
19 case 'tags': return $this->_tags; break; | |
20 default: throw new Exception('Property '.$name.' does not exist'); | |
21 } | |
22 } | |
23 public function __set($name, $value) { | |
24 switch (strtolower($name)) { | |
25 case 'version': case 'key': case 'serial': case 'label': case 'value': case 'expiration': case 'transfer': case 'updatemessage': case 'tags': throw new Exception('Property '.$name.' is read only'); | |
26 default: throw new Exception('Property '.$name.' does not exist or is read-only'); | |
27 } | |
28 } | |
29 public function __isset($name) { | |
30 switch (strtolower($name)) { | |
31 case 'version': case 'key': case 'serial': case 'label': case 'value': case 'updatemessage': case 'tags': return TRUE; | |
32 case 'expiration': return isset($this->_extensions[4]); | |
33 case 'transfer': return isset($this->_extensions[1]); | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
34 case 'transferchain': return isset($this->_extensions[5]); |
0 | 35 default: return FALSE; |
36 } | |
37 } | |
38 public function __unset($name) { | |
39 switch (strtolower($name)) { | |
40 case 'version': case 'key': case 'serial': case 'label': case 'value': case 'expiration': case 'transfer': case 'updatemessage': case 'tags': throw new Exception('Property '.$name.' is read only'); | |
41 default: throw new Exception('Property '.$name.' does not exist'); | |
42 } | |
43 } | |
44 public function offsetSet($offset, $value) { $this->__set($offset, $value); } | |
45 public function offsetExists($offset) { return $this->__isset($offset); } | |
46 public function offsetUnset($offset) { $this->__unset($offset); } | |
47 public function offsetGet($offset) { return $this->__get($offset); } | |
48 | |
49 private function __construct() { } | |
50 public static function Decode($data) { | |
51 $upd = new MARCUpdate(); | |
52 $upd->_message = $data; | |
53 if (strlen($data) < 1 + NACL_CRYPTO_SIGN_ed25519_PUBLICKEYBYTES) return FALSE; | |
54 $upd->_version = ord($data[0]); | |
55 if ($upd->_version != 2) return FALSE; | |
56 $upd->_key = substr($data, 1, NACL_CRYPTO_SIGN_ed25519_PUBLICKEYBYTES); | |
57 $i = NACL_CRYPTO_SIGN_ed25519_PUBLICKEYBYTES + 64 + 1; | |
58 $l = strlen($data); | |
59 if (!$l || $l < $i+4+1+1) return FALSE; | |
60 $upd->_serial = marc_decode_int32be($data, $i); $i += 4; | |
61 $labellen = ord($data[$i++]); | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
62 if ($l < $i+$labellen+1) return FALSE; |
0 | 63 $upd->_label = substr($data, $i, $labellen); $i += $labellen; |
64 $upd->_extensions = array(); | |
65 for ($numext = ord($data[$i++]); $numext > 0; $numext--) { | |
66 if ($l < $i+1+2) return FALSE; | |
67 $extid = ord($data[$i++]); | |
68 $extlen = marc_decode_int16be($data, $i); $i += 2; | |
69 if ($l < $i + $extlen) return FALSE; | |
70 $upd->_extensions[$extid] = substr($data, $i, $extlen); | |
71 $i += $extlen; | |
72 } | |
73 $upd->_valueoffset = $i; | |
74 return $upd; | |
75 } | |
76 public function Verify() { | |
77 $data = $this->_message; | |
78 if (strlen($data) < 1 + NACL_CRYPTO_SIGN_ed25519_PUBLICKEYBYTES) return FALSE; | |
79 $version = ord($data[0]); | |
80 if ($version != 2) return FALSE; | |
81 $key = substr($data, 1, NACL_CRYPTO_SIGN_ed25519_PUBLICKEYBYTES); | |
82 $data = substr($data, NACL_CRYPTO_SIGN_ed25519_PUBLICKEYBYTES + 1); | |
83 $data = nacl_crypto_sign_ed25519_open($data, $key); | |
84 return $data !== NULL && $data !== FALSE; | |
85 } | |
86 public function Create($upd, $seckey, $current = NULL) { | |
87 if (strlen($seckey) == 32) nacl_crypto_sign_ed25519_keypair($seckey, $seckey); | |
88 if (strlen($seckey) < 64) throw new Exception('Signing key is not valid'); | |
89 if (!isset($upd['key'])) $upd['key'] = substr($seckey, 32, 32); | |
90 if ($upd['key'] != substr($seckey, 32, 32)) throw new Exception('Resource key is not valid'); | |
91 if (!isset($upd['label'])) throw new Exception('Resource label not set'); | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
92 $upd['label'] = (string)$upd['label']; |
0 | 93 if (strlen($upd['label']) > 255) throw new Exception('Resource label too long'); |
94 if (!isset($upd['serial'])) $upd['serial'] = time(); | |
95 if ($current) { | |
96 if ($upd['serial'] <= $current['serial']) $upd['serial'] = $current['serial'] + 1; | |
97 if (!self::CanImport($upd, $current)) throw new Exception('Can not update resource'); | |
98 } | |
99 if (isset($upd['transfer']) && (strlen($upd['transfer']) != 0 && strlen($upd['transfer']) != NACL_CRYPTO_SIGN_ed25519_PUBLICKEYBYTES)) throw new Exception('Transfer recipient key is not valid'); | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
100 if ($current) { |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
101 unset($upd['transferchain']); |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
102 if (isset($current['transferchain']) && ($chain = self::Decode($current['transferchain'])) && $chain->Verify() && $chain->serial >= time() - 365*24*60*60 && isset($chain->transfer) && ($chain->transfer == $upd['key'] || ($upd['key'] == $current['key'] && !strlen($chain->transfer)))) { |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
103 $upd['transferchain'] = $current['transferchain']; |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
104 } elseif (isset($current['transfer']) && isset($current['updatemessage']) && $current['serial'] >= time() - 365*24*60*60 && isset($current['transfer']) && ($current['transfer'] == $upd['key'] || !strlen($current['transfer']))) { |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
105 $upd['transferchain'] = $current['updatemessage']; |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
106 } |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
107 } |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
108 if (isset($upd['transfer'])) { |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
109 if (isset($upd['transferchain'])) { |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
110 $chain = self::Decode($upd['transferchain']); |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
111 while ($chain && $chain->key == $upd['key']) $chain = ($chain->Verify() && $chain->serial >= time() - 365*24*60*60 && isset($chain->transferchain)) ? self::Decode($chain->transferchain) : NULL; |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
112 if ($chain && $chain->Verify() && $chain->serial >= time() - 365*24*60*60) $upd['transferchain'] = $chain->updatemessage; else unset($upd['transferchain']); |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
113 } |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
114 if (isset($upd['value']) && !is_null($upd['value'])) { |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
115 $chain = array('label' => $upd['label'], 'serial' => $upd['serial'], 'key' => $upd['key'], 'transfer' => $upd['transfer']); |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
116 if (isset($upd['expiration'])) $chain['expiration'] = $upd['expiration']; |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
117 if (isset($upd['transferchain'])) $chain['transferchain'] = $upd['transferchain']; |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
118 $chain = self::Create($chain, $seckey); |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
119 if ($chain && strlen($chain->updatemessage) <= 0xffff) $upd['transferchain'] = $chain->updatemessage; |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
120 } |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
121 } |
0 | 122 $data = marc_encode_int32be($upd['serial']); |
123 $data .= chr(strlen($upd['label'])).$upd['label']; | |
124 $value = array(); | |
125 if (isset($upd->_extensions)) foreach ($upd->_extensions as $identifier => $item) $value[$identifier] = $item; | |
126 if (isset($upd['transfer'])) $value[1] = $upd['transfer']; | |
127 if (isset($upd['expiration'])) $value[4] = marc_encode_int32be($upd['expiration']); | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
128 if (isset($upd['transferchain'])) $value[5] = $upd['transferchain']; |
0 | 129 if (count($value) > 0xff) throw new Exception('Too many extensions used'); |
130 $data .= chr(count($value)); | |
131 foreach ($value as $identifier => $item) { | |
132 $item = (string)$item; | |
133 if (strlen($item) > 0xffff) throw new Exception('Extension data too big'); | |
134 $data .= chr($identifier).marc_encode_int16be(strlen($item)).$item; | |
135 } | |
136 if (isset($upd['value'])) $data .= self::EncodeValue($upd['value']); | |
137 $data = nacl_crypto_sign_ed25519($data, $seckey); | |
138 if (!strlen($data)) throw new Exception('Failed to sign data'); | |
139 if (!strlen(nacl_crypto_sign_ed25519_open($data, $upd['key']))) throw new Exception('Key pair is not valid'); | |
140 $data = chr(2).$upd['key'].$data; | |
141 return self::Decode($data); | |
142 } | |
143 public function ToArray() { | |
144 $arr = array('version' => $this->version, 'key' => $this->key, 'serial' => $this->serial, 'label' => $this->label, 'value' => $this->value); | |
145 if (isset($this->expiration)) $arr['expiration'] = $this->expiration; | |
146 if (isset($this->transfer)) $arr['transfer'] = $this->transfer; | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
147 if (isset($this->transferchain)) $arr['transferchain'] = $this->transferchain; |
0 | 148 return $arr; |
149 } | |
150 | |
151 private static function EncodeValue($data) { | |
152 if (is_null($data)) { | |
153 return chr(0); | |
154 } else if (is_scalar($data)) { | |
155 return chr(1).(string)$data; | |
156 } else if (is_array($data)) { | |
157 $iscollection = TRUE; | |
158 foreach ($data as $key => $value) if (!is_int($key) || $key < 0) $iscollection = FALSE; | |
159 $ret = $iscollection ? chr(2) : chr(3); | |
160 foreach ($data as $key => $value) { | |
161 if (!$iscollection) { | |
162 if (strlen($key) > 0xff) throw new Exception('Dictionary key is too long'); | |
163 $ret .= chr(strlen($key)).$key; | |
164 } | |
165 $bytes = self::EncodeValue($value); | |
166 $ret .= marc_encode_int32be(strlen($bytes)).$bytes; | |
167 } | |
168 return $ret; | |
169 } else { | |
170 throw new Exception('Unable to encode type '.gettype($data)); | |
171 } | |
172 } | |
173 private static function DecodeValue($data, $offset = 0, $length = -1) { | |
174 if ($length == -1) $length = strlen($data) - $offset; | |
175 if ($length < 0) throw new Exception('Truncated'); | |
176 if ($length == 0) return NULL; | |
177 $type = ord($data[$offset]); $offset++; $length--; | |
178 switch ($type) { | |
179 case 0: return NULL; | |
180 case 1: return substr($data, $offset, $length); | |
181 case 2: | |
182 $value = array(); | |
183 while ($length > 0) { | |
184 if (4 > $length) throw new Exception('Truncated'); | |
185 $len = marc_decode_int32be($data, $offset); $offset += 4; $length -= 4; | |
186 if ($len > $length) throw new Exception('Truncated'); | |
187 $value[] = self::DecodeValue($data, $offset, $len); $offset += $len; $length -= $len; | |
188 } | |
189 return $value; | |
190 case 3: | |
191 $value = array(); | |
192 while ($length > 0) { | |
193 if (1 > $length) throw new Exception('Truncated'); | |
194 $len = ord($data[$offset]); $offset++; $length--; | |
195 if ($len + 4 > $length) throw new Exception('Truncated'); | |
196 $key = substr($data, $offset, $len); $offset += $len; $length -= $len; | |
197 $len = marc_decode_int32be($data, $offset); $offset += 4; $length -= 4; | |
198 if ($len > $length) throw new Exception('Truncated'); | |
199 $value[$key] = self::DecodeValue($data, $offset, $len); $offset += $len; $length -= $len; | |
200 } | |
201 return $value; | |
202 default: throw new Exception('Unsupported type code '.$type); | |
203 } | |
204 } | |
205 public static function CanImport($nw, $cu = NULL) { | |
206 if (!$nw || !isset($nw['label'])) return FALSE; | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
207 if ($nw['serial'] > time() + 7*24*60*60) return FALSE; |
0 | 208 if ($cu) { |
209 if ($nw['label'] != $cu['label']) return FALSE; | |
210 if ($cu['serial'] >= $nw['serial']) return FALSE; | |
211 if ($cu['key'] == $nw['key']) return TRUE; | |
212 if (isset($cu['expiration']) && $cu['expiration'] < time()) return TRUE; | |
213 if ($cu['serial'] < time() - 365*24*60*60) return TRUE; | |
214 if (isset($cu['transfer']) && (!strlen($cu['transfer']) || $cu['transfer'] == $nw['key'])) return TRUE; | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
215 if (isset($nw['transferchain']) && ($chain = MARCUpdate::Decode($nw['transferchain'])) && $chain->Verify() && self::CanImport($nw, $chain) && self::CanImport($chain, $cu)) return TRUE; |
0 | 216 return FALSE; |
217 } else { | |
218 if ($nw['serial'] < time() - 365*24*60*60) return FALSE; | |
219 if (isset($nw['expiration']) && $nw['expiration'] < time()) return FALSE; | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
220 return TRUE; |
0 | 221 } |
222 } | |
223 } | |
224 abstract class MARCDatabase { | |
225 private $_importResourceFilterCallbacks = array(), $_importResourceCallbacks = array(); | |
226 public abstract function GetResource($label); | |
227 protected abstract function ImportInternal($resource); | |
228 public abstract function GetResources($labelprefix = NULL, $mints = NULL); | |
229 public abstract function DeleteResource($label); | |
230 public function Import($resource, $force = FALSE) { | |
231 if (is_string($resource)) $resource = MARCUpdate::Decode($resource); | |
232 if (!$resource) return FALSE; | |
233 $current = $this->GetResource($resource['label']); | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
234 if (!$force && !$this->CanImportResource($resource, $current)) return FALSE; |
0 | 235 if (!$resource->Verify()) return FALSE; |
236 if (!$this->ImportInternal($resource)) return FALSE; | |
237 $this->ResourceImported($resource); | |
238 return $resource; | |
239 } | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
240 protected function CanImportResource($resource, $current) { |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
241 foreach ($this->_importResourceFilterCallbacks as $callback) if (!call_user_func($callback, $this, $resource, $current)) return FALSE; |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
242 if (!MARCUpdate::CanImport($resource, $current)) return FALSE; |
0 | 243 return TRUE; |
244 } | |
245 protected function ResourceImported($resource) { | |
246 foreach ($this->_importResourceCallbacks as $callback) call_user_func($callback, $this, $resource); | |
247 } | |
248 public function RegisterResourceFilterCallback($callback) { | |
249 $this->_importResourceFilterCallbacks[] = $callback; | |
250 } | |
251 public function RegisterResourceImportCallback($callback) { | |
252 $this->_importResourceCallbacks[] = $callback; | |
253 } | |
3
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
254 public function UpdateResource($resource, $seckey, $force = FALSE) { |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
255 if (is_a($resource, 'MARCUpdate')) $resource = $resource->ToArray(); |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
256 if (!$force) unset($resource['serial'], $resource['key']); |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
257 $res = MARCUpdate::Create($resource, $seckey, $force ? NULL : $this->GetResource($resource['label'])); |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
258 if (!$res) return FALSE; |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
259 return $this->Import($res, $force); |
5c8c4fa95803
Added support for transfer chaining and some bugfixes
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
260 } |
0 | 261 public function SyncHTTP($server, $options = array()) { |
262 $log = isset($options['log']) ? $options['log'] : TRUE; | |
263 $result = array(); | |
264 $method = isset($options['method']) ? $options['method'] : 'PUT'; | |
265 if ($method != 'POST' && $method != 'PUT' && $method != 'GET') throw new Exception('Unsupported method '.$method); | |
266 $result['exported'] = 0; | |
267 $post = NULL; | |
268 if ($method != 'GET' && (!isset($options['noexport']) || !$options['noexport'])) { | |
269 $post = $method == 'POST' ? array() : ''; | |
270 $updates = $this->GetResources(NULL, isset($options['exporttimestamp']) ? $options['exporttimestamp'] : NULL); | |
271 foreach ($updates as $update) { | |
272 if (!is_string($update)) $update = $update['updatemessage']; | |
273 if ($method == 'PUT') { | |
274 $post .= marc_encode_int32be(strlen($update)).$update; | |
275 } else { | |
276 $post[] = 'update[]='.urlencode($update); | |
277 } | |
278 $result['exported']++; | |
279 } | |
280 $updates = NULL; | |
281 if ($method == 'POST') $post = implode('&', $post); | |
282 } | |
283 if ($log) echo "Sending ".strlen($post)." bytes... "; | |
284 $result['bytessent'] = strlen($post); | |
285 $ch = curl_init(); | |
286 $timestamp = isset($options['importtimestamp']) ? intval($options['importtimestamp']) : 0; | |
287 curl_setopt($ch, CURLOPT_URL, $server.'?version=3&get='.$timestamp); | |
288 if ($method == 'PUT') curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); | |
289 else if ($method == 'POST') curl_setopt($ch, CURLOPT_POST, TRUE); | |
290 if ($post != NULL) curl_setopt($ch, CURLOPT_POSTFIELDS, $post); | |
291 $post = NULL; | |
292 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
293 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); | |
294 curl_setopt($ch, CURLOPT_TIMEOUT, 60); | |
295 $response = curl_exec($ch); | |
296 if ($response === FALSE) throw new Exception('HTTP request failed'); | |
297 if ($log) echo "received ".strlen($response)." bytes.\n"; | |
298 $result['bytesreceived'] = strlen($response); | |
299 if (($httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE)) != 200) throw new Exception('Received unexpected HTTP code '.$httpcode.' - '.(strlen($response)?var_export($response,TRUE):'')); | |
300 if (($contenttype = curl_getinfo($ch, CURLINFO_CONTENT_TYPE)) != 'application/octet-stream') throw new Exception('Received unexpected content type '.$contenttype.' - '.(strlen($response)?var_export($response,TRUE):'')); | |
301 curl_close($ch); | |
302 $i = 0; | |
303 if ($i + 2 > strlen($response)) throw new Exception('Data has been truncated'); | |
304 $version = ord($response[$i++]); | |
305 if ($version != 3) throw new Exception('Unsupported protocol version '.$version); | |
306 for ($numext = ord($response[$i++]); $numext > 0; $numext--) { | |
307 if ($i + 3 > strlen($response)) throw new Exception('Truncated'); | |
308 $extid = ord($response[$i++]); | |
309 $extlen = marc_decode_int16be($response, $i); $i += 2; | |
310 if ($i + $extlen > strlen($response)) throw new Exception('Truncated'); | |
311 switch ($extid) { | |
312 case 2: | |
313 if ($extlen >= 4 * 3) { | |
314 $result['remotereceived'] = marc_decode_int32be($response, $i + 0); | |
315 $result['remoteimported'] = marc_decode_int32be($response, $i + 4); | |
316 $result['remoteexported'] = marc_decode_int32be($response, $i + 8); | |
317 } | |
318 break; | |
319 case 3: if ($extlen >= 4) $result['importtimestamp'] = marc_decode_int32be($response, $i); break; | |
320 } | |
321 $i += $extlen; | |
322 } | |
323 if ($log) echo "Exported ".$result['exported']." updates of which ".(isset($result['remoteimported']) ? $result['remoteimported'] : 'none')." were imported.\n"; | |
324 $result['updates'] = array(); | |
325 $result['imported'] = 0; | |
326 $result['updatesreceived'] = 0; | |
327 while ($i < strlen($response)) { | |
328 if ($i + 4 > strlen($response)) throw new Exception('Truncated'); | |
329 $len = marc_decode_int32be($response, $i); $i += 4; | |
330 if ($i + $len > strlen($response)) throw new Exception('Truncated'); | |
331 $result['updatesreceived']++; | |
332 $value = substr($response, $i, $len); $i += $len; | |
333 $res = MARCUpdate::Decode($value); | |
334 $result['updates'][] = $res; | |
335 if (!$this->Import($value)) continue; | |
336 $result['imported']++; | |
337 } | |
338 if ($log) echo "Imported ".$result['imported']." out of ".$result['updatesreceived']." updates.\n"; | |
339 return $result; | |
340 } | |
341 } | |
342 class MARCDatabaseFlatFile extends MARCDatabase { | |
343 private $dbfile = NULL; | |
344 private $resources = array(); | |
345 private $changed = FALSE; | |
346 public function GetResource($label) { | |
347 if (!isset($this->resources[$label])) return NULL; | |
348 return $this->resources[$label]['update']; | |
349 } | |
350 public function DeleteResource($label) { | |
351 if (!isset($this->resources[$label])) return FALSE; | |
352 unset($this->resources[$label]); | |
353 $this->changed = TRUE; | |
354 return TRUE; | |
355 } | |
356 protected function ImportInternal($update) { | |
357 $this->resources[$update['label']] = array('timestamp' => time(), 'update' => $update); | |
358 $this->changed = TRUE; | |
359 return TRUE; | |
360 } | |
361 public function GetResources($labelprefix = NULL, $mints = NULL) { | |
362 return new MARCDatabaseFlatFileFilteredResourceIterator(new ArrayIterator($this->resources), $labelprefix, $mints); | |
363 } | |
364 public function __construct($filename = FALSE) { | |
365 if ($filename) $this->Open($filename); | |
366 } | |
367 private function OpenFile($filename) { | |
368 $this->Close(); | |
369 $this->dbfile = fopen($filename, 'c+'); | |
370 if (!$this->dbfile) throw new Exception('Could not open database file'); | |
371 if (!flock($this->dbfile, LOCK_EX)) throw new Exception('Could not lock database file'); | |
372 } | |
373 public function Open($filename) { | |
374 $this->OpenFile($filename); | |
375 $this->resources = array(); | |
376 $this->changed = FALSE; | |
377 rewind($this->dbfile); | |
378 while (true) { | |
379 $data = fread($this->dbfile, 8); | |
380 if (strlen($data) == 0) break; | |
381 if (strlen($data) != 8) throw new Exception('Database truncated'); | |
382 $ts = marc_decode_int32be($data, 0); | |
383 $len = marc_decode_int32be($data, 4); | |
384 $data = fread($this->dbfile, $len); | |
385 if (strlen($data) != $len) throw new Exception('Database truncated'); | |
386 $res = MARCUpdate::Decode($data); | |
387 if (!$res) continue; | |
388 $this->resources[$res['label']] = array('timestamp' => $ts, 'update' => $res); | |
389 } | |
390 } | |
391 public function Save() { | |
392 if (!$this->dbfile) throw new Exception('No database file is open'); | |
393 rewind($this->dbfile); | |
394 ftruncate($this->dbfile, 0); | |
395 foreach ($this->resources as $res) { | |
396 fwrite($this->dbfile, marc_encode_int32be($res['timestamp'])); | |
397 $u = (string)$res['update']['updatemessage']; | |
398 fwrite($this->dbfile, marc_encode_int32be(strlen($u))); | |
399 fwrite($this->dbfile, $u); | |
400 } | |
401 $this->changed = FALSE; | |
402 } | |
403 public function SaveAs($filename) { | |
404 $this->OpenFile($filename); | |
405 $this->Save(); | |
406 } | |
407 public function Close() { | |
408 if ($this->dbfile) { | |
409 flock($this->dbfile, LOCK_UN); | |
410 fclose($this->dbfile); | |
411 $this->dbfile = NULL; | |
412 } | |
413 } | |
414 public function IsChanged() { | |
415 return $this->changed; | |
416 } | |
417 } | |
418 class MARCDatabaseFlatFileFilteredResourceIterator implements Iterator { | |
419 private $source, $labelprefixlength, $labelprefix, $mints; | |
420 public function __construct($source, $labelprefix, $mints) { | |
421 $this->source = $source; | |
422 $this->labelprefixlength = is_null($labelprefix) ? 0 : strlen($labelprefix); | |
423 $this->labelprefix = $labelprefix; | |
424 $this->mints = intval($mints); | |
425 } | |
426 public function current() { $r = $this->source->current(); return $r ? $r['update'] : NULL; } | |
427 public function key() { return $this->source->key(); } | |
428 public function valid() { return $this->source->valid(); } | |
429 public function next() { $this->source->next(); $this->findnext(); } | |
430 public function rewind() { $this->source->rewind(); $this->findnext(); } | |
431 private function findnext() { | |
432 while ($this->source->valid()) { | |
433 $c = $this->source->current(); | |
434 if ($c['timestamp'] >= $this->mints && (!$this->labelprefixlength || substr($c['update']['label'], 0, $this->labelprefixlength) == $this->labelprefix)) break; | |
435 $this->source->next(); | |
436 } | |
437 } | |
438 } | |
439 class MARCDatabaseSQLite extends MARCDatabase { | |
440 private $pdo = NULL; | |
441 protected function DBPrepareStatement($query, $args = NULL) { | |
442 $stmt = $this->pdo->prepare($query); | |
443 if (!is_array($args) && !is_null($args)) $args = array($args); | |
444 $stmt->execute($args); | |
445 return $stmt; | |
446 } | |
447 private function DBUpdate($query, $args = NULL) { | |
448 $stmt = $this->DBPrepareStatement($query, $args); | |
449 $cnt = $stmt->rowCount(); | |
450 $stmt->closeCursor(); | |
451 return $cnt; | |
452 } | |
453 public function __construct($filename) { | |
454 $this->pdo = new PDO('sqlite:'.$filename); | |
455 $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
456 $this->DBUpdate('CREATE TABLE IF NOT EXISTS `resources` (`timestamp` INTEGER, `label` BLOB PRIMARY KEY, `update` BLOB)'); | |
457 } | |
458 public function GetResource($label) { | |
459 $stmt = $this->DBPrepareStatement('SELECT `update` FROM `resources` WHERE `label` = ?', $label); | |
460 $res = $stmt->fetchColumn(); | |
461 $stmt->closeCursor(); | |
462 if ($res === FALSE) return NULL; | |
463 return MARCUpdate::Decode($res); | |
464 } | |
465 public function DeleteResource($label) { | |
466 return $this->DBUpdate('DELETE FROM `resources` WHERE `label` = ?', $label) != 0; | |
467 } | |
468 protected function ImportInternal($update) { | |
469 $args = array('d' => $update['updatemessage'], 't' => time(), 'l' => $update['label']); | |
470 if ($this->DBUpdate('UPDATE `resources` SET `update` = :d, `timestamp` = :t WHERE `label` = :l', $args) == 0) | |
471 $this->DBUpdate('INSERT OR IGNORE INTO `resources` (`label`, `update`, `timestamp`) VALUES (:l, :d, :t)', $args); | |
472 return TRUE; | |
473 } | |
474 public function GetResources($labelprefix = NULL, $mints = NULL) { | |
475 $labelprefix = is_null($labelprefix) ? '' : (string)$labelprefix; | |
476 return $this->GetResourcesFromQuery('SELECT `update` FROM `resources` WHERE `timestamp` >= ? AND SUBSTR(`label`, 1, ?) = ? ORDER BY `label`', array(intval($mints), strlen($labelprefix), $labelprefix)); | |
477 } | |
478 public function GetResourcesFromQuery($query, $args = NULL) { | |
479 return $this->GetResourcesFromStatement($this->DBPrepareStatement($query, $args)); | |
480 } | |
481 public function GetResourcesFromStatement($statement) { | |
482 return new MARCDatabaseSQLiteResourceIterator($statement); | |
483 } | |
484 public function IsChanged() { | |
485 return FALSE; | |
486 } | |
487 public function Close() { | |
488 } | |
489 } | |
490 class MARCDatabaseSQLiteResourceIterator implements Iterator { | |
491 private $source, $current = FALSE; | |
492 public function __construct($source) { $this->source = $source; } | |
493 public function current() { return $this->current === FALSE ? NULL : MARCUpdate::Decode($this->current); } | |
494 public function key() { return NULL; } | |
495 public function valid() { return $this->current !== FALSE; } | |
496 public function next() { $this->current = $this->source->fetchColumn(); if ($this->current === FALSE) $this->source->closeCursor(); } | |
497 public function rewind() { $this->next(); } | |
498 } | |
499 class MARCDatabaseDBA extends MARCDatabase { | |
500 private $db = NULL; | |
501 public function __construct($path, $mode = 'cd', $handler = 'qdbm') { | |
502 $this->db = dba_open($path, $mode, $handler); | |
503 if ($this->db === FALSE) throw new Exception('Could not open database'); | |
504 } | |
505 public function GetResource($label) { | |
506 $r = dba_fetch($label, $this->db); | |
507 if ($r === FALSE) return NULL; | |
508 return MARCUpdate::Decode($r); | |
509 } | |
510 public function DeleteResource($label) { | |
511 return dba_delete($label, $this->db); | |
512 } | |
513 protected function ImportInternal($update) { | |
514 return dba_replace($update['label'], $update['updatemessage'], $this->db); | |
515 } | |
516 public function GetResources($labelprefix = NULL, $mints = NULL) { | |
517 return new MARCDatabaseDBAFilteredResourceIterator($this->db, $labelprefix, $mints); | |
518 } | |
519 public function Save() { | |
520 dba_sync($this->db); | |
521 } | |
522 public function Close() { | |
523 dba_close($this->db); | |
524 } | |
525 public function IsChanged() { | |
526 return FALSE; | |
527 } | |
528 } | |
529 class MARCDatabaseDBAFilteredResourceIterator implements Iterator { | |
530 private $db, $currentkey = FALSE, $labelprefixlength, $labelprefix, $mints; | |
531 public function __construct($db, $labelprefix, $mints) { | |
532 $this->db = $db; | |
533 $this->labelprefixlength = is_null($labelprefix) ? 0 : strlen($labelprefix); | |
534 $this->labelprefix = $labelprefix; | |
535 $this->mints = intval($mints); | |
536 } | |
537 public function current() { return MARCUpdate::Decode(dba_fetch($this->currentkey, $this->db)); } | |
538 public function key() { return $this->currentkey; } | |
539 public function valid() { return strlen($this->currentkey); } | |
540 public function next() { $this->currentkey = dba_nextkey($this->db); $this->findnext(); } | |
541 public function rewind() { $this->currentkey = dba_firstkey($this->db); $this->findnext(); } | |
542 private function findnext() { while ($this->currentkey !== FALSE && ($this->labelprefixlength && substr($this->currentkey, 0, $this->labelprefixlength) != $this->labelprefix)) $this->currentkey = dba_nextkey($this->db); } | |
543 } | |
544 | |
545 function marc_decode_int32be($data, $i = 0) { return (ord($data[$i]) << 24) | (ord($data[$i+1]) << 16) | (ord($data[$i+2]) << 8) | ord($data[$i+3]); } | |
546 function marc_decode_int16be($data, $i = 0) { return (ord($data[$i+0]) << 8) | ord($data[$i+1]); } | |
547 function marc_encode_int32be($v) { return pack("N", $v); } | |
548 function marc_encode_int16be($v) { return pack("n", $v); } | |
549 |