MCPcopy Create free account
hub / github.com/BaseXdb/basex / __construct

Method __construct

basex-api/src/main/php/BaseXClient/Session.php:22–59  ·  view source on GitHub ↗
($hostname, $port, $user, $password)

Source from the content-addressed store, hash-verified

20 protected $bsize;
21
22 public function __construct($hostname, $port, $user, $password)
23 {
24 // create server connection
25 $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
26 if (!$this->socket) {
27 throw $this->error("Socket creation failed");
28 }
29 if (!socket_connect($this->socket, $hostname, $port)) {
30 throw $this->error("Cannot connect");
31 }
32
33 // receive timestamp
34 $ts = $this->readString();
35 // Hash container
36 if (strpos($ts, ':') !== false) {
37 // digest-auth
38 $challenge = explode(':', $ts, 2);
39 $md5 = hash("md5", hash("md5", $user.':'.$challenge[0].':'.$password).$challenge[1]);
40 } else {
41 // Legacy: cram-md5
42 $md5 = hash("md5", hash("md5", $password).$ts);
43 }
44
45 // send username and hashed password/timestamp
46 $result = $this->send($user.chr(0).$md5.chr(0));
47 if ($result === false) {
48 throw $this->error("Write failed");
49 }
50
51 // receives success flag
52 $result = socket_read($this->socket, 1);
53 if ($result === false) {
54 throw $this->error("Read failed");
55 }
56 if ($result != chr(0)) {
57 throw new BaseXException("Access denied.");
58 }
59 }
60
61 /**
62 * Executes a command.

Callers

nothing calls this directly

Calls 3

errorMethod · 0.95
readStringMethod · 0.95
sendMethod · 0.95

Tested by

no test coverage detected