-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session.php
139 lines (115 loc) · 3.03 KB
/
Session.php
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
<?php
namespace BaseXClient;
/*
* 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;
/* see readme.txt */
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.");
}
}
/* see readme.txt */
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;
}
/* Returns the query object.*/
public function query($q) {
return new Query($this, $q);
}
/* see readme.txt */
public function create($name, $input) {
$this->sendCmd(8, $path, $input);
}
/* see readme.txt */
public function add($path, $input) {
$this->sendCmd(9, $path, $input);
}
/* see readme.txt */
public function replace($path, $input) {
$this->sendCmd(12, $path, $input);
}
/* see readme.txt */
public function store($path, $input) {
$this->sendCmd(13, $path, $input);
}
/* see readme.txt */
public function info() {
return $this->info;
}
/* see readme.txt */
public function close() {
socket_write($this->socket, "exit".chr(0));
socket_close($this->socket);
}
/* Initializes the byte transfer */
private function init() {
$this->bpos = 0;
$this->bsize = 0;
}
/* Receives a string from the socket. */
public function readString() {
$com = "";
while(($d = $this->read()) != chr(0)) {
$com .= $d;
}
return $com;
}
/* Returns a single byte from the socket. */
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++];
}
/* see readme.txt */
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);
}
}
/* Sends the str. */
public function send($str) {
socket_write($this->socket, $str.chr(0));
}
/* Returns success check. */
public function ok() {
return $this->read() == chr(0);
}
/* Returns the result. */
public function receive() {
$this->init();
return $this->readString();
}
}
?>