Highway to PSR2
[openemr.git] / library / classes / CouchDB.class.php
blobaea5bf2c199ca06f07a4b47cc652e3abf70fe017
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
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) {
44 return true;
45 } else {
46 return false;
50 function createDB($db)
52 $resp = $this->send("PUT", "/".$db);
53 return true;
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;
74 if ($th_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;
92 if ($th_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);
103 return true;
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);
131 if (!$s) {
132 return false;
135 $request = "$method $url HTTP/1.0\r\nHost: $this->host\r\n";
137 if ($this->user) {
138 $request .= 'Authorization: Basic '.base64_encode($this->user.':'.$this->pass)."\r\n";
141 if ($post_data) {
142 $request .= "Content-Length: ".strlen($post_data)."\r\n\r\n";
143 $request .= "$post_data\r\n";
144 } else {
145 $request .= "\r\n";
148 fwrite($s, $request);
149 $response = "";
151 while (!feof($s)) {
152 $response .= fgets($s);
155 list($this->headers, $this->body) = explode("\r\n\r\n", $response);
156 return $this->body;