Merge pull request #14062 from civicrm/5.13
[civicrm-core.git] / CRM / Utils / SoapServer.php
CommitLineData
40d837df
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
40d837df 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
40d837df
CW
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
40d837df 27
40d837df
CW
28/**
29 * This class handles all SOAP client requests.
30 *
31 * @package CRM
6b83d5bd 32 * @copyright CiviCRM LLC (c) 2004-2019
40d837df
CW
33 */
34class CRM_Utils_SoapServer {
35
36 /**
37 * Number of seconds we should let a soap process idle
6714d8d2 38 * @var int
40d837df 39 */
6714d8d2 40 public static $soap_timeout = 0;
40d837df
CW
41
42 /**
43 * Cache the actual UF Class
6714d8d2 44 * @var string
40d837df
CW
45 */
46 public $ufClass;
47
48 /**
49 * Class constructor. This caches the real user framework class locally,
50 * so we can use it for authentication and validation.
51 *
f4aaa82a 52 * @internal param string $uf The userframework class
40d837df
CW
53 */
54 public function __construct() {
55 // any external program which call SoapServer is responsible for
56 // creating and attaching the session
57 $args = func_get_args();
58 $this->ufClass = array_shift($args);
59 }
60
61 /**
62 * Simple ping function to test for liveness.
63 *
77855840
TO
64 * @param string $var
65 * The string to be echoed.
40d837df 66 *
a6c01b45 67 * @return string
40d837df
CW
68 */
69 public function ping($var) {
70 $session = CRM_Core_Session::singleton();
71 $key = $session->get('key');
72 $session->set('key', $var);
73 return "PONG: $var ($key)";
74 }
75
76 /**
fe482240 77 * Verify a SOAP key.
40d837df 78 *
77855840
TO
79 * @param string $key
80 * The soap key generated by authenticate().
40d837df 81 *
f4aaa82a 82 * @throws SoapFault
40d837df
CW
83 */
84 public function verify($key) {
85 $session = CRM_Core_Session::singleton();
86
87 $soap_key = $session->get('soap_key');
88 $t = time();
89
90 if ($key !== sha1($soap_key)) {
91 throw new SoapFault('Client', 'Invalid key');
92 }
93
40d837df
CW
94 if (self::$soap_timeout &&
95 $t > ($session->get('soap_time') + self::$soap_timeout)
96 ) {
97 throw new SoapFault('Client', 'Expired key');
98 }
99
50bfb460 100 // otherwise, we're ok. update the timestamp
40d837df
CW
101
102 $session->set('soap_time', $t);
103 }
104
105 /**
fe482240 106 * Authentication wrapper to the UF Class.
40d837df 107 *
77855840
TO
108 * @param string $name
109 * Login name.
110 * @param string $pass
111 * Password.
40d837df 112 *
f4aaa82a
EM
113 * @param bool $loadCMSBootstrap
114 *
115 * @throws SoapFault
a6c01b45
CW
116 * @return string
117 * The SOAP Client key
40d837df
CW
118 */
119 public function authenticate($name, $pass, $loadCMSBootstrap = FALSE) {
e7292422 120 require_once str_replace('_', DIRECTORY_SEPARATOR, $this->ufClass) . '.php';
40d837df 121
a8a8e3d2
AH
122 if ($this->ufClass == 'CRM_Utils_System_Joomla'
123 || $this->ufClass == 'CRM_Utils_System_WordPress') {
e7292422 124 $loadCMSBootstrap = TRUE;
40d837df
CW
125 }
126
34122894 127 $result = CRM_Utils_System::authenticate($name, $pass, $loadCMSBootstrap);
40d837df
CW
128
129 if (empty($result)) {
130 throw new SoapFault('Client', 'Invalid login');
131 }
132
133 $session = CRM_Core_Session::singleton();
134 $session->set('soap_key', $result[2]);
135 $session->set('soap_time', time());
136
137 return sha1($result[2]);
138 }
139
c301f76e 140 /**
fe482240 141 * MAILER API.
ad37ac8e 142 *
143 * @param string $key
144 * @param int $job
145 * @param int $queue
146 * @param string $hash
147 * @param string $body
148 *
149 * @return array|int
150 * @throws \SoapFault
c301f76e 151 */
40d837df
CW
152 public function mailer_event_bounce($key, $job, $queue, $hash, $body) {
153 $this->verify($key);
be2fb01f 154 $params = [
40d837df
CW
155 'job_id' => $job,
156 'time_stamp' => date('YmdHis'),
157 'event_queue_id' => $queue,
158 'hash' => $hash,
159 'body' => $body,
160 'version' => 3,
be2fb01f 161 ];
a83e8a28
SL
162 $result = civicrm_api('Mailing', 'event_bounce', $params);
163 return CRM_Utils_Array::encode_items($result);
40d837df
CW
164 }
165
5bc392e6 166 /**
ad37ac8e 167 * Mailer event unsubscribe.
168 *
169 * @param string $key
170 * @param int $job
171 * @param int $queue
172 * @param string $hash
5bc392e6
EM
173 *
174 * @return array|int
175 * @throws SoapFault
176 */
40d837df
CW
177 public function mailer_event_unsubscribe($key, $job, $queue, $hash) {
178 $this->verify($key);
be2fb01f 179 $params = [
40d837df
CW
180 'job_id' => $job,
181 'time_stamp' => date('YmdHis'),
182 'org_unsubscribe' => 0,
183 'event_queue_id' => $queue,
184 'hash' => $hash,
185 'version' => 3,
be2fb01f 186 ];
a83e8a28
SL
187 $result = civicrm_api('MailingGroup', 'event_unsubscribe', $params);
188 return CRM_Utils_Array::encode_items($result);
40d837df
CW
189 }
190
5bc392e6
EM
191 /**
192 * @param $key
193 * @param $job
194 * @param $queue
195 * @param $hash
196 *
197 * @return array|int
198 * @throws SoapFault
199 */
40d837df
CW
200 public function mailer_event_domain_unsubscribe($key, $job, $queue, $hash) {
201 $this->verify($key);
be2fb01f 202 $params = [
40d837df
CW
203 'job_id' => $job,
204 'time_stamp' => date('YmdHis'),
205 'org_unsubscribe' => 1,
206 'event_queue_id' => $queue,
207 'hash' => $hash,
208 'version' => 3,
be2fb01f 209 ];
a83e8a28
SL
210 $result = civicrm_api('MailingGroup', 'event_domain_unsubscribe', $params);
211 return CRM_Utils_Array::encode_items($result);
40d837df
CW
212 }
213
5bc392e6
EM
214 /**
215 * @param $key
216 * @param $job
217 * @param $queue
218 * @param $hash
219 *
220 * @return array|int
221 * @throws SoapFault
222 */
40d837df
CW
223 public function mailer_event_resubscribe($key, $job, $queue, $hash) {
224 $this->verify($key);
be2fb01f 225 $params = [
40d837df
CW
226 'job_id' => $job,
227 'time_stamp' => date('YmdHis'),
228 'org_unsubscribe' => 0,
229 'event_queue_id' => $queue,
230 'hash' => $hash,
231 'version' => 3,
be2fb01f 232 ];
a83e8a28
SL
233 $result = civicrm_api('MailingGroup', 'event_resubscribe', $params);
234 return CRM_Utils_Array::encode_items($result);
40d837df
CW
235 }
236
5bc392e6
EM
237 /**
238 * @param $key
239 * @param $email
240 * @param $domain
241 * @param $group
242 *
243 * @return array|int
244 * @throws SoapFault
245 */
40d837df
CW
246 public function mailer_event_subscribe($key, $email, $domain, $group) {
247 $this->verify($key);
be2fb01f 248 $params = [
40d837df
CW
249 'email' => $email,
250 'group_id' => $group,
251 'version' => 3,
be2fb01f 252 ];
a83e8a28
SL
253 $result = civicrm_api('MailingGroup', 'event_subscribe', $params);
254 return CRM_Utils_Array::encode_items($result);
40d837df
CW
255 }
256
5bc392e6
EM
257 /**
258 * @param $key
259 * @param $contact
260 * @param $subscribe
261 * @param $hash
262 *
263 * @return array|int
264 * @throws SoapFault
265 */
40d837df
CW
266 public function mailer_event_confirm($key, $contact, $subscribe, $hash) {
267 $this->verify($key);
be2fb01f 268 $params = [
40d837df
CW
269 'contact_id' => $contact,
270 'subscribe_id' => $subscribe,
271 'time_stamp' => date('YmdHis'),
272 'event_subscribe_id' => $subscribe,
273 'hash' => $hash,
274 'version' => 3,
be2fb01f 275 ];
a83e8a28
SL
276 $result = civicrm_api('Mailing', 'event_confirm', $params);
277 return CRM_Utils_Array::encode_items($result);
40d837df
CW
278 }
279
5bc392e6
EM
280 /**
281 * @param $key
282 * @param $job
283 * @param $queue
284 * @param $hash
285 * @param $bodyTxt
286 * @param $rt
287 * @param null $bodyHTML
288 * @param null $fullEmail
289 *
290 * @return array|int
291 * @throws SoapFault
292 */
40d837df
CW
293 public function mailer_event_reply($key, $job, $queue, $hash, $bodyTxt, $rt, $bodyHTML = NULL, $fullEmail = NULL) {
294 $this->verify($key);
be2fb01f 295 $params = [
40d837df
CW
296 'job_id' => $job,
297 'event_queue_id' => $queue,
298 'hash' => $hash,
299 'bodyTxt' => $bodyTxt,
300 'replyTo' => $rt,
301 'bodyHTML' => $bodyHTML,
302 'fullEmail' => $fullEmail,
303 'time_stamp' => date('YmdHis'),
304 'version' => 3,
be2fb01f 305 ];
a83e8a28
SL
306 $result = civicrm_api('Mailing', 'event_reply', $params);
307 return CRM_Utils_Array::encode_items($result);
40d837df
CW
308 }
309
5bc392e6
EM
310 /**
311 * @param $key
312 * @param $job
313 * @param $queue
314 * @param $hash
315 * @param $email
316 *
317 * @return array|int
318 * @throws SoapFault
319 */
40d837df
CW
320 public function mailer_event_forward($key, $job, $queue, $hash, $email) {
321 $this->verify($key);
be2fb01f 322 $params = [
40d837df
CW
323 'job_id' => $job,
324 'event_queue_id' => $queue,
325 'hash' => $hash,
326 'email' => $email,
327 'version' => 3,
be2fb01f 328 ];
a83e8a28
SL
329 $result = civicrm_api('Mailing', 'event_forward', $params);
330 return CRM_Utils_Array::encode_items($result);
40d837df
CW
331 }
332
5bc392e6
EM
333 /**
334 * @param $key
c490a46a 335 * @param array $params
5bc392e6
EM
336 *
337 * @return array|int
338 * @throws SoapFault
339 */
40d837df
CW
340 public function get_contact($key, $params) {
341 $this->verify($key);
342 $params['version'] = 3;
a83e8a28
SL
343 $result = civicrm_api('contact', 'get', $params);
344 return CRM_Utils_Array::encode_items($result);
40d837df 345 }
96025800 346
40d837df 347}