commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / vendor / civicrm / civicrm-cxn-rpc / src / ApiClient.php
1 <?php
2
3 /*
4 * This file is part of the civicrm-cxn-rpc package.
5 *
6 * Copyright (c) CiviCRM LLC <info@civicrm.org>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this package.
10 */
11
12 namespace Civi\Cxn\Rpc;
13
14 use Civi\Cxn\Rpc\Exception\GarbledMessageException;
15 use Civi\Cxn\Rpc\Exception\InvalidMessageException;
16 use Civi\Cxn\Rpc\Http\ViaPortHttp;
17 use Civi\Cxn\Rpc\Message\GarbledMessage;
18 use Civi\Cxn\Rpc\Message\StdMessage;
19
20 class ApiClient extends Agent {
21 /**
22 * @var array
23 */
24 protected $appMeta;
25
26 /**
27 * @var string
28 */
29 protected $cxnId;
30
31 /**
32 * @param array $appMeta
33 * @param CxnStore\CxnStoreInterface $cxnStore
34 * @param string $cxnId
35 */
36 public function __construct($appMeta, $cxnStore, $cxnId) {
37 parent::__construct(NULL, $cxnStore);
38 $this->appMeta = $appMeta;
39 $this->cxnId = $cxnId;
40 $this->http = new Http\PhpHttp();
41 }
42
43 /**
44 * Call a remote API.
45 *
46 * Protocol errors will be reported as exceptions.
47 *
48 * @param string $entity
49 * @param string $action
50 * @param array $params
51 * @throws GarbledMessageException
52 * @throws InvalidMessageException
53 * @return mixed
54 */
55 public function call($entity, $action, $params = array()) {
56 $this->log->debug("Send API call: {entity}.{action} over {cxnId}", array(
57 'entity' => $entity,
58 'action' => $action,
59 'cxnId' => $this->cxnId,
60 ));
61 $cxn = $this->cxnStore->getByCxnId($this->cxnId);
62 $req = new StdMessage($cxn['cxnId'], $cxn['secret'],
63 array($entity, $action, $params, $this->appMeta['appCert']));
64
65 $http = empty($cxn['viaPort']) ? $this->http : new ViaPortHttp($this->http, $cxn['viaPort']);
66
67 list($respHeaders, $respCiphertext, $respCode) = $http->send('POST', $cxn['siteUrl'], $req->encode(), array(
68 'Content-type' => Constants::MIME_TYPE,
69 ));
70 $respMessage = $this->decode(array(StdMessage::NAME, GarbledMessage::NAME), $respCiphertext);
71 if ($respMessage instanceof GarbledMessage) {
72 throw new GarbledMessageException($respMessage);
73 }
74 elseif ($respMessage instanceof StdMessage) {
75 if ($respMessage->getCxnId() != $cxn['cxnId']) {
76 // Tsk, tsk, Mallory!
77 throw new InvalidMessageException('Received response from incorrect connection.');
78 }
79 return $respMessage->getData();
80 }
81 else {
82 throw new InvalidMessageException('Unrecognized message type.');
83 }
84 }
85
86 /**
87 * Call a remote API.
88 *
89 * This is the same as call(), except that it wraps the request in a try-catch
90 * block and converts any exceptions to array(is_error=>...) format.
91 *
92 * @param $entity
93 * @param $action
94 * @param array $params
95 * @return array|mixed
96 */
97 public function callSafe($entity, $action, $params = array()) {
98 try {
99 return $this->call($entity, $action, $params);
100 }
101 catch (GarbledMessageException $e) {
102 return array(
103 'is_error' => 1,
104 'error_message' => "Client exception: " . $e->getMessage(),
105 'garbled_message' => substr($e->getGarbledMessage()->getData(), 0, 77),
106 );
107 }
108 catch (\Exception $e) {
109 return array(
110 'is_error' => 1,
111 'error_message' => "Client exception: " . $e->getMessage(),
112 'trace' => $e->getTraceAsString(),
113 );
114 }
115 }
116
117 }