minor improvement to tabs style
[openemr.git] / library / classes / CouchDB.class.php
blob899e54afca49d46bb72feeb11d0b2284c09d100b
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.
23 // Author: Eldho Chacko <eldho@zhservices.com>
24 // Jacob T Paul <jacob@zhservices.com>
26 // +------------------------------------------------------------------------------+
28 class CouchDB {
29 function __construct() {
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, $th_json) = $data;
61 $couch_json = array();
62 $couch_json['_id'] = $docid;
63 $couch_json['pid'] = $patient_id;
64 $couch_json['encounter'] = $encounter;
65 $couch_json['mimetype'] = $type;
66 $couch_json['data'] = $json;
67 if($th_json) {
68 $couch_json['th_data'] = $th_json;
71 $resp = $this->send("PUT", "/".$db."/".$docid, json_encode($couch_json));
72 return json_decode($resp);
75 function update_doc($data){
76 list($db,$docid,$revid,$patient_id,$encounter,$type,$json, $th_json) = $data;
77 $couch_json = array();
78 $couch_json['_id'] = $docid;
79 $couch_json['_rev'] = $revid;
80 $couch_json['pid'] = $patient_id;
81 $couch_json['encounter'] = $encounter;
82 $couch_json['mimetype'] = $type;
83 $couch_json['data'] = $json;
84 if($th_json) {
85 $couch_json['th_data'] = $th_json;
87 $resp = $this->send("PUT", "/".$db."/".$docid, json_encode($couch_json));
88 return json_decode($resp);
91 function DeleteDoc($db,$docid,$revid){
92 $resp = $this->send("DELETE", "/".$db."/".$docid."?rev=".$revid);
93 return true;
96 function retrieve_doc($data){
97 list($db,$docid) = $data;
98 $resp = $this->send("GET", "/".$db."/".$docid);
99 return json_decode($resp); // string(47) "{"_id":"123","_rev":"2039697587","data":"Foo"}"
102 function stringToId( $string, $replace = '_' )
104 // First translit string to ASCII, as this characters are most probably
105 // supported everywhere
106 $string = iconv( 'UTF-8', 'ASCII//TRANSLIT', $string );
108 // And then still replace any obscure characters by _ to ensure nothing
109 // "bad" happens with this string.
110 $string = preg_replace( '([^A-Za-z0-9.-]+)', $replace, $string );
112 // Additionally we convert the string to lowercase, so that we get case
113 // insensitive fetching
114 return strtolower( $string );
117 function send($method, $url, $post_data = NULL) {
118 $s = fsockopen($this->host, $this->port, $errno, $errstr);
119 if(!$s) {
120 return false;
123 $request = "$method $url HTTP/1.0\r\nHost: $this->host\r\n";
125 if ($this->user) {
126 $request .= 'Authorization: Basic '.base64_encode($this->user.':'.$this->pass)."\r\n";
129 if($post_data) {
130 $request .= "Content-Length: ".strlen($post_data)."\r\n\r\n";
131 $request .= "$post_data\r\n";
133 else {
134 $request .= "\r\n";
137 fwrite($s, $request);
138 $response = "";
140 while(!feof($s)) {
141 $response .= fgets($s);
144 list($this->headers, $this->body) = explode("\r\n\r\n", $response);
145 return $this->body;