-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseXClient.php
More file actions
182 lines (151 loc) · 4.23 KB
/
BaseXClient.php
File metadata and controls
182 lines (151 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/*
* PHP client for BaseX.
* Works with BaseX 7.0 and later
*
* Documentation: http://docs.basex.org/wiki/Clients
*
* (C) BaseX Team 2005-12, BSD License
*/
class Session {
// class variables.
var $socket, $info, $buffer, $bpos, $bsize;
function __construct($h, $p, $user, $pw) {
// create server connection
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!socket_connect($this->socket, $h, $p)) {
throw new Exception("Can't communicate with server.");
}
// receive timestamp
$ts = $this->readString();
// send username and hashed password/timestamp
$md5 = hash("md5", hash("md5", $pw).$ts);
socket_write($this->socket, $user.chr(0).$md5.chr(0));
// receives success flag
if(socket_read($this->socket, 1) != chr(0)) {
throw new Exception("Access denied.");
}
}
public function execute($com) {
// send command to server
socket_write($this->socket, $com.chr(0));
// receive result
$result = $this->receive();
$this->info = $this->readString();
if($this->ok() != True) {
throw new Exception($this->info);
}
return $result;
}
public function query($q) {
return new Query($this, $q);
}
public function create($name, $input) {
$this->sendCmd(8, $name, $input);
}
public function add($path, $input) {
$this->sendCmd(9, $path, $input);
}
public function replace($path, $input) {
$this->sendCmd(12, $path, $input);
}
public function store($path, $input) {
$this->sendCmd(13, $path, $input);
}
public function info() {
return $this->info;
}
public function close() {
socket_write($this->socket, "exit".chr(0));
socket_close($this->socket);
}
private function init() {
$this->bpos = 0;
$this->bsize = 0;
}
public function readString() {
$com = "";
while(($d = $this->read()) != chr(0)) {
$com .= $d;
}
return $com;
}
private function read() {
if($this->bpos == $this->bsize) {
$this->bsize = socket_recv($this->socket, $this->buffer, 4096, 0);
$this->bpos = 0;
}
return $this->buffer[$this->bpos++];
}
private function sendCmd($code, $arg, $input) {
socket_write($this->socket, chr($code).$arg.chr(0).$input.chr(0));
$this->info = $this->receive();
if($this->ok() != True) {
throw new Exception($this->info);
}
}
public function send($str) {
socket_write($this->socket, $str.chr(0));
}
public function ok() {
return $this->read() == chr(0);
}
public function receive() {
$this->init();
return $this->readString();
}
}
class Query {
var $session, $id, $open, $cache;
public function __construct($s, $q) {
$this->session = $s;
$this->id = $this->exec(chr(0), $q);
}
public function bind($name, $value, $type = "") {
$this->exec(chr(3), $this->id.chr(0).$name.chr(0).$value.chr(0).$type);
}
public function context($value, $type = "") {
$this->exec(chr(14), $this->id.chr(0).$value.chr(0).$type);
}
public function execute() {
return $this->exec(chr(5), $this->id);
}
public function more() {
if($this->cache == NULL) {
$this->pos = 0;
$this->session->send(chr(4).$this->id.chr(0));
while(!$this->session->ok()) {
$this->cache[] = $this->session->readString();
}
if(!$this->session->ok()) {
throw new Exception($this->session->readString());
}
}
if($this->pos < count($this->cache)) return true;
$this->cache = NULL;
return false;
}
public function next() {
if($this->more()) {
return $this->cache[$this->pos++];
}
}
public function info() {
return $this->exec(chr(6), $this->id);
}
public function options() {
return $this->exec(chr(7), $this->id);
}
public function close() {
$this->exec(chr(2), $this->id);
}
public function exec($cmd, $arg) {
$this->session->send($cmd.$arg);
$s = $this->session->receive();
if($this->session->ok() != True) {
throw new Exception($this->session->readString());
}
return $s;
}
}
?>