forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CouchDB.class.php
128 lines (109 loc) · 4.95 KB
/
CouchDB.class.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
<?php
// +-----------------------------------------------------------------------------+
// Copyright (C) 2012 Z&H Consultancy Services Private Limited <[email protected]>
//
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// A copy of the GNU General Public License is included along with this program:
// openemr/interface/login/GnuGPL.html
// For more information write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// Author: Eldho Chacko <[email protected]>
// Jacob T Paul <[email protected]>
//
// +------------------------------------------------------------------------------+
class CouchDB {
function __construct() {
$this->host = $GLOBALS['couchdb_host'];
$this->user = ($GLOBALS['couchdb_user'] != '') ? $GLOBALS['couchdb_user'] : null;
$this->pass = ($GLOBALS['couchdb_pass'] != '') ? $GLOBALS['couchdb_pass'] : null;
$this->port = $GLOBALS['couchdb_port'];
$this->dbase = $GLOBALS['couchdb_dbase'];
}
function check_connection(){
$resp = $this->send("GET", "/"); // response: string(46) "{"couchdb": "Welcome", "version": "0.7.0a553"}"
$response = json_decode($resp);
if($response->couchdb && $response->version)
return true;
else
return false;
}
function createDB($db){
$resp = $this->send("PUT", "/".$db);
return true;
}
function createView($db){
$resp = $this->send("PUT", "/".$db."/_design/FilteringViews", '{"_id":"_design/FilteringViews","views": {"FilterPid": {"map": "function(doc) { if(doc.pid){emit(doc._id, doc);} }"},
"FilterEncounter": {"map": "function(doc) { if(doc.encounter){emit(doc._id, doc);} }"},
"FilterPidEncounter": {"map": "function(doc) { if(doc.pid && doc.encounter){emit(doc._id, doc);} }"}}}');
return json_decode($resp);
}
function check_saveDOC($data){
list($db,$docid,$patient_id,$encounter,$type,$json) = $data;
$resp = $this->send("PUT", "/".$db."/".$docid, '{"_id":"'.$docid.'","pid":"'.$patient_id.'","encounter":"'.$encounter.'","mimetype":"'.$type.'","data":'.$json.'}');
return json_decode($resp);
}
function update_doc($data){
list($db,$docid,$revid,$patient_id,$encounter,$type,$json) = $data;
$resp = $this->send("PUT", "/".$db."/".$docid, '{"_id":"'.$docid.'","_rev":"'.$revid.'","pid":"'.$patient_id.'","encounter":"'.$encounter.'","mimetype":"'.$type.'","data":'.$json.'}');
return json_decode($resp);
}
function DeleteDoc($db,$docid,$revid){
$resp = $this->send("DELETE", "/".$db."/".$docid."?rev=".$revid);
return true;
}
function retrieve_doc($data){
list($db,$docid) = $data;
$resp = $this->send("GET", "/".$db."/".$docid);
return json_decode($resp); // string(47) "{"_id":"123","_rev":"2039697587","data":"Foo"}"
}
function stringToId( $string, $replace = '_' )
{
// First translit string to ASCII, as this characters are most probably
// supported everywhere
$string = iconv( 'UTF-8', 'ASCII//TRANSLIT', $string );
// And then still replace any obscure characters by _ to ensure nothing
// "bad" happens with this string.
$string = preg_replace( '([^A-Za-z0-9.-]+)', $replace, $string );
// Additionally we convert the string to lowercase, so that we get case
// insensitive fetching
return strtolower( $string );
}
function send($method, $url, $post_data = NULL) {
$s = fsockopen($this->host, $this->port, $errno, $errstr);
if(!$s) {
return false;
}
$request = "$method $url HTTP/1.0\r\nHost: $this->host\r\n";
if ($this->user) {
$request .= 'Authorization: Basic '.base64_encode($this->user.':'.$this->pass)."\r\n";
}
if($post_data) {
$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);
}
list($this->headers, $this->body) = explode("\r\n\r\n", $response);
return $this->body;
}
}
?>