Merge pull request #15764 from civicrm/5.20
[civicrm-core.git] / CRM / Utils / SoapServer.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * This class handles all SOAP client requests.
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2020
33 */
34 class CRM_Utils_SoapServer {
35
36 /**
37 * Number of seconds we should let a soap process idle
38 * @var int
39 */
40 public static $soap_timeout = 0;
41
42 /**
43 * Cache the actual UF Class
44 * @var string
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 *
52 * @internal param string $uf The userframework class
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 *
64 * @param string $var
65 * The string to be echoed.
66 *
67 * @return string
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 /**
77 * Verify a SOAP key.
78 *
79 * @param string $key
80 * The soap key generated by authenticate().
81 *
82 * @throws SoapFault
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
94 if (self::$soap_timeout &&
95 $t > ($session->get('soap_time') + self::$soap_timeout)
96 ) {
97 throw new SoapFault('Client', 'Expired key');
98 }
99
100 // otherwise, we're ok. update the timestamp
101
102 $session->set('soap_time', $t);
103 }
104
105 /**
106 * Authentication wrapper to the UF Class.
107 *
108 * @param string $name
109 * Login name.
110 * @param string $pass
111 * Password.
112 *
113 * @param bool $loadCMSBootstrap
114 *
115 * @throws SoapFault
116 * @return string
117 * The SOAP Client key
118 */
119 public function authenticate($name, $pass, $loadCMSBootstrap = FALSE) {
120 require_once str_replace('_', DIRECTORY_SEPARATOR, $this->ufClass) . '.php';
121
122 if ($this->ufClass == 'CRM_Utils_System_Joomla'
123 || $this->ufClass == 'CRM_Utils_System_WordPress') {
124 $loadCMSBootstrap = TRUE;
125 }
126
127 $result = CRM_Utils_System::authenticate($name, $pass, $loadCMSBootstrap);
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
140 /**
141 * MAILER API.
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
151 */
152 public function mailer_event_bounce($key, $job, $queue, $hash, $body) {
153 $this->verify($key);
154 $params = [
155 'job_id' => $job,
156 'time_stamp' => date('YmdHis'),
157 'event_queue_id' => $queue,
158 'hash' => $hash,
159 'body' => $body,
160 'version' => 3,
161 ];
162 $result = civicrm_api('Mailing', 'event_bounce', $params);
163 return CRM_Utils_Array::encode_items($result);
164 }
165
166 /**
167 * Mailer event unsubscribe.
168 *
169 * @param string $key
170 * @param int $job
171 * @param int $queue
172 * @param string $hash
173 *
174 * @return array|int
175 * @throws SoapFault
176 */
177 public function mailer_event_unsubscribe($key, $job, $queue, $hash) {
178 $this->verify($key);
179 $params = [
180 'job_id' => $job,
181 'time_stamp' => date('YmdHis'),
182 'org_unsubscribe' => 0,
183 'event_queue_id' => $queue,
184 'hash' => $hash,
185 'version' => 3,
186 ];
187 $result = civicrm_api('MailingGroup', 'event_unsubscribe', $params);
188 return CRM_Utils_Array::encode_items($result);
189 }
190
191 /**
192 * @param $key
193 * @param $job
194 * @param $queue
195 * @param $hash
196 *
197 * @return array|int
198 * @throws SoapFault
199 */
200 public function mailer_event_domain_unsubscribe($key, $job, $queue, $hash) {
201 $this->verify($key);
202 $params = [
203 'job_id' => $job,
204 'time_stamp' => date('YmdHis'),
205 'org_unsubscribe' => 1,
206 'event_queue_id' => $queue,
207 'hash' => $hash,
208 'version' => 3,
209 ];
210 $result = civicrm_api('MailingGroup', 'event_domain_unsubscribe', $params);
211 return CRM_Utils_Array::encode_items($result);
212 }
213
214 /**
215 * @param $key
216 * @param $job
217 * @param $queue
218 * @param $hash
219 *
220 * @return array|int
221 * @throws SoapFault
222 */
223 public function mailer_event_resubscribe($key, $job, $queue, $hash) {
224 $this->verify($key);
225 $params = [
226 'job_id' => $job,
227 'time_stamp' => date('YmdHis'),
228 'org_unsubscribe' => 0,
229 'event_queue_id' => $queue,
230 'hash' => $hash,
231 'version' => 3,
232 ];
233 $result = civicrm_api('MailingGroup', 'event_resubscribe', $params);
234 return CRM_Utils_Array::encode_items($result);
235 }
236
237 /**
238 * @param $key
239 * @param $email
240 * @param $domain
241 * @param $group
242 *
243 * @return array|int
244 * @throws SoapFault
245 */
246 public function mailer_event_subscribe($key, $email, $domain, $group) {
247 $this->verify($key);
248 $params = [
249 'email' => $email,
250 'group_id' => $group,
251 'version' => 3,
252 ];
253 $result = civicrm_api('MailingGroup', 'event_subscribe', $params);
254 return CRM_Utils_Array::encode_items($result);
255 }
256
257 /**
258 * @param $key
259 * @param $contact
260 * @param $subscribe
261 * @param $hash
262 *
263 * @return array|int
264 * @throws SoapFault
265 */
266 public function mailer_event_confirm($key, $contact, $subscribe, $hash) {
267 $this->verify($key);
268 $params = [
269 'contact_id' => $contact,
270 'subscribe_id' => $subscribe,
271 'time_stamp' => date('YmdHis'),
272 'event_subscribe_id' => $subscribe,
273 'hash' => $hash,
274 'version' => 3,
275 ];
276 $result = civicrm_api('Mailing', 'event_confirm', $params);
277 return CRM_Utils_Array::encode_items($result);
278 }
279
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 */
293 public function mailer_event_reply($key, $job, $queue, $hash, $bodyTxt, $rt, $bodyHTML = NULL, $fullEmail = NULL) {
294 $this->verify($key);
295 $params = [
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,
305 ];
306 $result = civicrm_api('Mailing', 'event_reply', $params);
307 return CRM_Utils_Array::encode_items($result);
308 }
309
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 */
320 public function mailer_event_forward($key, $job, $queue, $hash, $email) {
321 $this->verify($key);
322 $params = [
323 'job_id' => $job,
324 'event_queue_id' => $queue,
325 'hash' => $hash,
326 'email' => $email,
327 'version' => 3,
328 ];
329 $result = civicrm_api('Mailing', 'event_forward', $params);
330 return CRM_Utils_Array::encode_items($result);
331 }
332
333 /**
334 * @param $key
335 * @param array $params
336 *
337 * @return array|int
338 * @throws SoapFault
339 */
340 public function get_contact($key, $params) {
341 $this->verify($key);
342 $params['version'] = 3;
343 $result = civicrm_api('contact', 'get', $params);
344 return CRM_Utils_Array::encode_items($result);
345 }
346
347 }