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