reformat initial comment block.
[openemr.git] / library / classes / CouchDB.class.php
blobb1d962e65729090cd0ddd2530253cd619bb0bc1a
1 <?php
2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2012 Z&H Consultancy Services Private Limited <sam@zhservices.com>
4 //
5 //
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.
22 //
23 // Author: Eldho Chacko <eldho@zhservices.com>
24 // Jacob T Paul <jacob@zhservices.com>
26 // +------------------------------------------------------------------------------+
28 class CouchDB {
29 function CouchDB() {
30 $this->host = $GLOBALS['couchdb_host'];
31 $this->user = ($GLOBALS['couchdb_user'] != '') ? $GLOBALS['couchdb_user'] : null;
32 $this->pass = ($GLOBALS['couchdb_pass'] != '') ? $GLOBALS['couchdb_pass'] : null;
33 $this->port = $GLOBALS['couchdb_port'];
34 $this->dbase = $GLOBALS['couchdb_dbase'];
37 function check_connection(){
38 $resp = $this->send("GET", "/"); // response: string(46) "{"couchdb": "Welcome", "version": "0.7.0a553"}"
39 $response = json_decode($resp);
40 if($response->couchdb && $response->version)
41 return true;
42 else
43 return false;
46 function createDB($db){
47 $resp = $this->send("PUT", "/".$db);
48 return true;
51 function createView($db){
53 $resp = $this->send("PUT", "/".$db."/_design/FilteringViews", '{"_id":"_design/FilteringViews","views": {"FilterPid": {"map": "function(doc) { if(doc.pid){emit(doc._id, doc);} }"},
54 "FilterEncounter": {"map": "function(doc) { if(doc.encounter){emit(doc._id, doc);} }"},
55 "FilterPidEncounter": {"map": "function(doc) { if(doc.pid && doc.encounter){emit(doc._id, doc);} }"}}}');
56 return json_decode($resp);
59 function check_saveDOC($data){
60 list($db,$docid,$patient_id,$encounter,$type,$json) = $data;
61 $resp = $this->send("PUT", "/".$db."/".$docid, '{"_id":"'.$docid.'","pid":"'.$patient_id.'","encounter":"'.$encounter.'","mimetype":"'.$type.'","data":'.$json.'}');
62 return json_decode($resp);
65 function update_doc($data){
66 list($db,$docid,$revid,$patient_id,$encounter,$type,$json) = $data;
67 $resp = $this->send("PUT", "/".$db."/".$docid, '{"_id":"'.$docid.'","_rev":"'.$revid.'","pid":"'.$patient_id.'","encounter":"'.$encounter.'","mimetype":"'.$type.'","data":'.$json.'}');
68 return json_decode($resp);
71 function DeleteDoc($db,$docid,$revid){
72 $resp = $this->send("DELETE", "/".$db."/".$docid."?rev=".$revid);
73 return true;
76 function retrieve_doc($data){
77 list($db,$docid) = $data;
78 $resp = $this->send("GET", "/".$db."/".$docid);
79 return json_decode($resp); // string(47) "{"_id":"123","_rev":"2039697587","data":"Foo"}"
82 function stringToId( $string, $replace = '_' )
84 // First translit string to ASCII, as this characters are most probably
85 // supported everywhere
86 $string = iconv( 'UTF-8', 'ASCII//TRANSLIT', $string );
88 // And then still replace any obscure characters by _ to ensure nothing
89 // "bad" happens with this string.
90 $string = preg_replace( '([^A-Za-z0-9.-]+)', $replace, $string );
92 // Additionally we convert the string to lowercase, so that we get case
93 // insensitive fetching
94 return strtolower( $string );
97 function send($method, $url, $post_data = NULL) {
98 $s = fsockopen($this->host, $this->port, $errno, $errstr);
99 if(!$s) {
100 return false;
103 $request = "$method $url HTTP/1.0\r\nHost: $this->host\r\n";
105 if ($this->user) {
106 $request .= 'Authorization: Basic '.base64_encode($this->user.':'.$this->pass)."\r\n";
109 if($post_data) {
110 $request .= "Content-Length: ".strlen($post_data)."\r\n\r\n";
111 $request .= "$post_data\r\n";
113 else {
114 $request .= "\r\n";
117 fwrite($s, $request);
118 $response = "";
120 while(!feof($s)) {
121 $response .= fgets($s);
124 list($this->headers, $this->body) = explode("\r\n\r\n", $response);
125 return $this->body;