Security fixes 20
[openemr.git] / library / maviq_phone_api.php
blobfb72d84b01603257e227178772994837b10fd982
1 <?php
2 // Copyright (C) 2010 Maviq <info@maviq.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 if(!extension_loaded("curl")) die("Curl extension is required");
11 class MaviqClient {
13 protected $Endpoint;
14 protected $SiteId;
15 protected $Token;
17 public function __construct($siteId, $token, $endpoint) {
18 $this->SiteId = $siteId;
19 $this->Token = $token;
20 $this->Endpoint = $endpoint;
24 public function sendRequest($path, $method="POST", $vars=array()){
26 echo "Path: {$path}\n";
28 $encoded = "";
29 foreach($vars AS $key=>$value)
30 $encoded .= "$key=".urlencode($value)."&";
31 $encoded = substr($encoded, 0, -1);
32 $tmpfile = "";
33 $fp = null;
35 // construct full url
36 $url = "{$this->Endpoint}/$path";
38 echo "Url: {$url}\n";
40 // if GET and vars, append them
41 if($method == "GET")
42 $url .= (FALSE === strpos($path, '?')?"?":"&").$encoded;
44 // initialize a new curl object
45 $curl = curl_init($url);
46 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
47 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
48 curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
49 switch(strtoupper($method)) {
50 case "GET":
51 curl_setopt($curl, CURLOPT_HTTPGET, TRUE);
52 break;
53 case "POST":
54 curl_setopt($curl, CURLOPT_POST, TRUE);
55 curl_setopt($curl, CURLOPT_POSTFIELDS, $encoded);
56 break;
57 case "PUT":
58 // curl_setopt($curl, CURLOPT_PUT, TRUE);
59 curl_setopt($curl, CURLOPT_POSTFIELDS, $encoded);
60 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
61 file_put_contents($tmpfile = tempnam("/tmp", "put_"),
62 $encoded);
63 curl_setopt($curl, CURLOPT_INFILE, $fp = fopen($tmpfile,
64 'r'));
65 curl_setopt($curl, CURLOPT_INFILESIZE,
66 filesize($tmpfile));
67 break;
68 case "DELETE":
69 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
70 break;
71 default:
72 throw(new Exception("Unknown method $method"));
73 break;
76 // send credentials
77 curl_setopt($curl, CURLOPT_USERPWD,
78 $pwd = "{$this->SiteId}:{$this->Token}");
80 // do the request. If FALSE, then an exception occurred
81 if(FALSE === ($result = curl_exec($curl)))
82 throw(new Exception(
83 "Curl failed with error " . curl_error($curl)));
85 // get result code
86 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
88 // unlink tmpfiles
89 if($fp)
90 fclose($fp);
91 if(strlen($tmpfile))
92 unlink($tmpfile);
94 return new RestResponse($url, $result, $responseCode);
99 class RestResponse {
101 public $ResponseText;
102 public $ResponseXml;
103 public $HttpStatus;
104 public $Url;
105 public $QueryString;
106 public $IsError;
107 public $ErrorMessage;
109 public function __construct($url, $text, $status) {
110 preg_match('/([^?]+)\??(.*)/', $url, $matches);
111 $this->Url = $matches[1];
112 $this->QueryString = $matches[2];
113 $this->ResponseText = $text;
114 $this->HttpStatus = $status;
115 if($this->HttpStatus != 204)
116 $this->ResponseXml = @simplexml_load_string($text);
118 if($this->IsError = ($status >= 400))
119 $this->ErrorMessage =
120 (string)$this->ResponseXml->RestException->Message;