-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCouchDB.php
executable file
·145 lines (90 loc) · 3.82 KB
/
CouchDB.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
140
141
142
143
144
145
<?php
class CouchDB {
function CouchDB( $options ) {
$this->force_no_decode = false;
$this->debug = false;
foreach($options as $key => $value) $this->$key = $value;
$auth = array();
}
function log( $logthis ) {
if( $this->debug ) {
echo $logthis."\n";
} else {
file_put_contents("/tmp/testing.log",date("Y-m-d H:i:s")." - ".$logthis."\n",FILE_APPEND);
}
}
function get( $type, $id = null, $filters = array(), $account_id = null ) {
if( $account_id == null ) $account_id = $this->use_account_id;
$filter = '';
if( count($filters) ) {
foreach( $filters as $key => $val ) $filter .= "filter_$key=$val&";
if( strlen($filter) ) $filter = '?'.substr($filter,0,-1);
} else if( strlen($id) ) {
$filter = "/$id";
}
//$this->log("GET /v1/accounts/{$account_id}/$type$filter");
$response = $this->send("GET","/v1/accounts/{$account_id}/$type$filter");
return($response['data']);
}
function post( $type, $id, $data, $account_id = null ) {
if( $account_id == null ) $account_id = $this->use_account_id;
$response = $this->send("POST","/v1/accounts/{$account_id}/$type/$id", json_encode(array('data'=>$data)));
return($response);
}
function put( $type, $data, $account_id = null ) {
if( $account_id == null ) $account_id = $this->use_account_id;
$response = $this->send("PUT","/v1/accounts/{$account_id}/$type/", json_encode(array('data'=>$data)));
return($response);
}
function del( $type, $id, $account_id = null ) {
if( $account_id == null ) $account_id = $this->use_account_id;
$response = $this->send("DELETE","/v1/accounts/{$account_id}/$type/$id");
return($response);
}
function send( $method, $url, $post_data = NULL, $type = 'application/json' ) {
$bldred=chr(0x1B).'[1;31m'; $bldgrn=chr(0x1B).'[1;32m'; $bldylw=chr(0x1B).'[1;33m'; $bldblu=chr(0x1B).'[1;34m'; $bldpur=chr(0x1B).'[1;35m'; $bldcyn=chr(0x1B).'[1;36m'; $bldwht=chr(0x1B).'[1;37m'; $txtrst=chr(0x1B).'[0m';
$mstart = microtime(true);
$s = fsockopen($this->host, $this->port, $errno, $errstr);
if(!$s) {
echo "$errno: $errstr\n";
return false;
}
//$request = "$method $url HTTP/1.1\r\nHost: $this->host:$this->port\r\n";
$request = "$method $url HTTP/1.0\r\nHost: $this->host\r\n";
if (isset($this->user)) $request .= "Authorization: Basic ".base64_encode("$this->user:$this->pass")."\r\n";
$request .= "Content-Type: $type\r\n";
$request .= "Accept: application/json, application/octet-stream, audio/*\r\n";
if( isset($this->xauth) ) $request .= "X-Auth-Token: {$this->xauth}\r\n";
if($post_data) {
//$request .= "Content-Type: application/json\r\n";
$request .= "Content-Length: ".strlen($post_data)."\r\n\r\n";
$request .= "$post_data\r\n";
} else {
$request .= "\r\n";
}
fwrite($s, $request);
$response = "";
while(!feof($s)) { $response .= fgets($s); }
$mend = microtime(true);
//if( $this->profile ) printf("{$bldblu}URL:{$bldylw}$url {$bldblu}µT:{$bldylw}".( $mend - $mstart ).$txtrst."\n");
$this->log( "$url µT:".( $mend - $mstart ));
list($this->headers, $this->body) = explode("\r\n\r\n", $response);
if( $method == "DELETE" ) {
if( stristr($this->headers,"204 No Content") ) {
return( array('status' => 'success'));
}
}
if( !stristr($this->headers,"200 OK") && !stristr($this->headers,"201 Created")) {
$this->log("{$bldpur}>>>>: $method $url HTTP/1.0 ($type) POST_LENGTH:".strlen($post_data)."$txtrst \n");
if( $post_data && $type == 'application/json' ) printf("{$bldpur}POST_DATA: ".trim($post_data)."\n");
$this->log("{$bldylw}<<<<: µT=".( $mend - $mstart )."{$txtrst}\n");
$this->log("{$bldylw}<<<<: H:".$this->headers."{$txtrst}");
$this->log("{$bldylw}<<<<: B:".$this->body."{$txtrst}");
}
if( $this->force_no_decode ) {
return $this->body;
}
return json_decode($this->body,true);
}
}
?>