3 // +-----------------------------------------------------------------------------+
4 // Copyright (C) 2012 Z&H Consultancy Services Private Limited <sam@zhservices.com>
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
19 // A copy of the GNU General Public License is included along with this program:
20 // openemr/interface/login/GnuGPL.html
21 // For more information write to the Free Software
22 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 // Author: Eldho Chacko <eldho@zhservices.com>
25 // Jacob T Paul <jacob@zhservices.com>
27 // +------------------------------------------------------------------------------+
29 use OpenEMR\Common\Crypto\CryptoGen
;
30 use OpenEMR\Common\Uuid\UuidRegistry
;
34 function __construct()
36 $this->host
= $GLOBALS['couchdb_host'];
37 $this->user
= ($GLOBALS['couchdb_user'] != '') ?
$GLOBALS['couchdb_user'] : null;
38 $cryptoGen = new CryptoGen();
39 $this->pass
= ($cryptoGen->decryptStandard($GLOBALS['couchdb_pass']) != '') ?
$cryptoGen->decryptStandard($GLOBALS['couchdb_pass']) : null;
40 $this->port
= $GLOBALS['couchdb_port'];
41 $this->dbase
= $GLOBALS['couchdb_dbase'];
44 function check_connection()
46 $resp = $this->send("GET", "/"); // response: string(46) "{"couchdb": "Welcome", "version": "0.7.0a553"}"
47 $response = json_decode($resp);
48 if ($response->couchdb
&& $response->version
) {
57 $resp = $this->send("PUT", "/" . $this->dbase
);
61 // note this will include _id (and not allow _rev) in the $data
62 function save_doc($data)
65 foreach ($data as $key => $value) {
69 $couch_json[$key] = $value;
71 $resp = $this->send("PUT", "/" . $this->dbase
. "/" . $data['_id'], json_encode($couch_json));
72 return json_decode($resp);
75 // note this will include _id and _rev in the $data
76 function update_doc($data)
79 foreach ($data as $key => $value) {
80 $couch_json[$key] = $value;
82 $resp = $this->send("PUT", "/" . $this->dbase
. "/" . $data['_id'], json_encode($couch_json));
83 return json_decode($resp);
86 function DeleteDoc($docid, $revid)
88 $resp = $this->send("DELETE", "/" . $this->dbase
. "/" . $docid . "?rev=" . $revid);
92 function retrieve_doc($docid)
94 $resp = $this->send("GET", "/" . $this->dbase
. "/" . $docid);
95 return json_decode($resp); // string(47) "{"_id":"123","_rev":"2039697587","data":"Foo"}"
98 // category is either documents or ccda
99 function createDocId($category)
101 return UuidRegistry
::uuidToString((new UuidRegistry(['couchdb' => $category]))->createUuid());
104 function send($method, $url, $post_data = null)
106 if ($GLOBALS['couchdb_connection_ssl']) {
107 // encrypt couchdb over the wire
109 file_exists($GLOBALS['OE_SITE_DIR'] . "/documents/certificates/couchdb-ca") &&
110 file_exists($GLOBALS['OE_SITE_DIR'] . "/documents/certificates/couchdb-cert") &&
111 file_exists($GLOBALS['OE_SITE_DIR'] . "/documents/certificates/couchdb-key")
113 // support cacert_file and client certificates
114 $stream_context = stream_context_create(
118 'cafile' => "${GLOBALS['OE_SITE_DIR']}/documents/certificates/couchdb-ca",
119 'local_cert' => "${GLOBALS['OE_SITE_DIR']}/documents/certificates/couchdb-cert",
120 'local_pk' => "${GLOBALS['OE_SITE_DIR']}/documents/certificates/couchdb-key"
124 $s = stream_socket_client('ssl://' . $this->host
. ":" . $this->port
, $errno, $errstr, ini_get("default_socket_timeout"), STREAM_CLIENT_CONNECT
, $stream_context);
125 } elseif (file_exists($GLOBALS['OE_SITE_DIR'] . "/documents/certificates/couchdb-ca")) {
126 // support cacert_file
127 $stream_context = stream_context_create(
131 'cafile' => "${GLOBALS['OE_SITE_DIR']}/documents/certificates/couchdb-ca"
135 $s = stream_socket_client('ssl://' . $this->host
. ":" . $this->port
, $errno, $errstr, ini_get("default_socket_timeout"), STREAM_CLIENT_CONNECT
, $stream_context);
137 if ($GLOBALS['couchdb_ssl_allow_selfsigned']) {
138 // support self-signed
139 $stream_context = stream_context_create(
143 'verify_peer' => false,
144 'allow_self_signed' => true
148 $s = stream_socket_client('ssl://' . $this->host
. ":" . $this->port
, $errno, $errstr, ini_get("default_socket_timeout"), STREAM_CLIENT_CONNECT
, $stream_context);
150 // self-signed, not supported so do not proceed and return false
155 // do not encrypt couchdb over the wire
156 $s = stream_socket_client('tcp://' . $this->host
. ":" . $this->port
, $errno, $errstr);
163 $request = "$method $url HTTP/1.0\r\nHost: $this->host\r\n";
166 $request .= 'Authorization: Basic ' . base64_encode($this->user
. ':' . $this->pass
) . "\r\n";
170 $request .= "Content-Length: " . strlen($post_data) . "\r\n\r\n";
171 $request .= "$post_data\r\n";
176 fwrite($s, $request);
180 $response .= fgets($s);
183 list($this->headers
, $this->body
) = explode("\r\n\r\n", $response);