2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2012 Z&H Consultancy Services Private Limited <sam@zhservices.com>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
18 // A copy of the GNU General Public License is included along with this program:
19 // openemr/interface/login/GnuGPL.html
20 // For more information write to the Free Software
21 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 // Author: Eldho Chacko <eldho@zhservices.com>
24 // Jacob T Paul <jacob@zhservices.com>
26 // +------------------------------------------------------------------------------+
30 function __construct()
32 $this->host
= $GLOBALS['couchdb_host'];
33 $this->user
= ($GLOBALS['couchdb_user'] != '') ?
$GLOBALS['couchdb_user'] : null;
34 $this->pass
= ($GLOBALS['couchdb_pass'] != '') ?
$GLOBALS['couchdb_pass'] : null;
35 $this->port
= $GLOBALS['couchdb_port'];
36 $this->dbase
= $GLOBALS['couchdb_dbase'];
39 function check_connection()
41 $resp = $this->send("GET", "/"); // response: string(46) "{"couchdb": "Welcome", "version": "0.7.0a553"}"
42 $response = json_decode($resp);
43 if ($response->couchdb
&& $response->version
) {
50 function createDB($db)
52 $resp = $this->send("PUT", "/".$db);
56 function createView($db)
59 $resp = $this->send("PUT", "/".$db."/_design/FilteringViews", '{"_id":"_design/FilteringViews","views": {"FilterPid": {"map": "function(doc) { if(doc.pid){emit(doc._id, doc);} }"},
60 "FilterEncounter": {"map": "function(doc) { if(doc.encounter){emit(doc._id, doc);} }"},
61 "FilterPidEncounter": {"map": "function(doc) { if(doc.pid && doc.encounter){emit(doc._id, doc);} }"}}}');
62 return json_decode($resp);
65 function check_saveDOC($data)
67 list($db,$docid,$patient_id,$encounter,$type,$json, $th_json) = $data;
68 $couch_json = array();
69 $couch_json['_id'] = $docid;
70 $couch_json['pid'] = $patient_id;
71 $couch_json['encounter'] = $encounter;
72 $couch_json['mimetype'] = $type;
73 $couch_json['data'] = $json;
75 $couch_json['th_data'] = $th_json;
78 $resp = $this->send("PUT", "/".$db."/".$docid, json_encode($couch_json));
79 return json_decode($resp);
82 function update_doc($data)
84 list($db,$docid,$revid,$patient_id,$encounter,$type,$json, $th_json) = $data;
85 $couch_json = array();
86 $couch_json['_id'] = $docid;
87 $couch_json['_rev'] = $revid;
88 $couch_json['pid'] = $patient_id;
89 $couch_json['encounter'] = $encounter;
90 $couch_json['mimetype'] = $type;
91 $couch_json['data'] = $json;
93 $couch_json['th_data'] = $th_json;
96 $resp = $this->send("PUT", "/".$db."/".$docid, json_encode($couch_json));
97 return json_decode($resp);
100 function DeleteDoc($db, $docid, $revid)
102 $resp = $this->send("DELETE", "/".$db."/".$docid."?rev=".$revid);
106 function retrieve_doc($data)
108 list($db,$docid) = $data;
109 $resp = $this->send("GET", "/".$db."/".$docid);
110 return json_decode($resp); // string(47) "{"_id":"123","_rev":"2039697587","data":"Foo"}"
113 function stringToId($string, $replace = '_')
115 // First translit string to ASCII, as this characters are most probably
116 // supported everywhere
117 $string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
119 // And then still replace any obscure characters by _ to ensure nothing
120 // "bad" happens with this string.
121 $string = preg_replace('([^A-Za-z0-9.-]+)', $replace, $string);
123 // Additionally we convert the string to lowercase, so that we get case
124 // insensitive fetching
125 return strtolower($string);
128 function send($method, $url, $post_data = null)
130 $s = fsockopen($this->host
, $this->port
, $errno, $errstr);
135 $request = "$method $url HTTP/1.0\r\nHost: $this->host\r\n";
138 $request .= 'Authorization: Basic '.base64_encode($this->user
.':'.$this->pass
)."\r\n";
142 $request .= "Content-Length: ".strlen($post_data)."\r\n\r\n";
143 $request .= "$post_data\r\n";
148 fwrite($s, $request);
152 $response .= fgets($s);
155 list($this->headers
, $this->body
) = explode("\r\n\r\n", $response);