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