Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Utils / SoapServer.php
CommitLineData
40d837df
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
40d837df 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 32 * @copyright CiviCRM LLC (c) 2004-2015
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
98 /* otherwise, we're ok. update the timestamp */
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
9b873358 120 if ($this->ufClass == 'CRM_Utils_System_Joomla') {
e7292422 121 $loadCMSBootstrap = TRUE;
40d837df
CW
122 }
123
124 $className = $this->ufClass;
481a74f4 125 $result =& $className::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.
c301f76e 140 */
40d837df
CW
141 public function mailer_event_bounce($key, $job, $queue, $hash, $body) {
142 $this->verify($key);
143 $params = array(
144 'job_id' => $job,
145 'time_stamp' => date('YmdHis'),
146 'event_queue_id' => $queue,
147 'hash' => $hash,
148 'body' => $body,
149 'version' => 3,
150 );
151 return civicrm_api('Mailing', 'event_bounce', $params);
152 }
153
5bc392e6
EM
154 /**
155 * @param $key
156 * @param $job
157 * @param $queue
158 * @param $hash
159 *
160 * @return array|int
161 * @throws SoapFault
162 */
40d837df
CW
163 public function mailer_event_unsubscribe($key, $job, $queue, $hash) {
164 $this->verify($key);
165 $params = array(
166 'job_id' => $job,
167 'time_stamp' => date('YmdHis'),
168 'org_unsubscribe' => 0,
169 'event_queue_id' => $queue,
170 'hash' => $hash,
171 'version' => 3,
172 );
173 return civicrm_api('MailingGroup', 'event_unsubscribe', $params);
174 }
175
5bc392e6
EM
176 /**
177 * @param $key
178 * @param $job
179 * @param $queue
180 * @param $hash
181 *
182 * @return array|int
183 * @throws SoapFault
184 */
40d837df
CW
185 public function mailer_event_domain_unsubscribe($key, $job, $queue, $hash) {
186 $this->verify($key);
187 $params = array(
188 'job_id' => $job,
189 'time_stamp' => date('YmdHis'),
190 'org_unsubscribe' => 1,
191 'event_queue_id' => $queue,
192 'hash' => $hash,
193 'version' => 3,
194 );
195 return civicrm_api('MailingGroup', 'event_domain_unsubscribe', $params);
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);
209 $params = array(
210 'job_id' => $job,
211 'time_stamp' => date('YmdHis'),
212 'org_unsubscribe' => 0,
213 'event_queue_id' => $queue,
214 'hash' => $hash,
215 'version' => 3,
216 );
217 return civicrm_api('MailingGroup', 'event_resubscribe', $params);
218 }
219
5bc392e6
EM
220 /**
221 * @param $key
222 * @param $email
223 * @param $domain
224 * @param $group
225 *
226 * @return array|int
227 * @throws SoapFault
228 */
40d837df
CW
229 public function mailer_event_subscribe($key, $email, $domain, $group) {
230 $this->verify($key);
231 $params = array(
232 'email' => $email,
233 'group_id' => $group,
234 'version' => 3,
235 );
236 return civicrm_api('MailingGroup', 'event_subscribe', $params);
237 }
238
5bc392e6
EM
239 /**
240 * @param $key
241 * @param $contact
242 * @param $subscribe
243 * @param $hash
244 *
245 * @return array|int
246 * @throws SoapFault
247 */
40d837df
CW
248 public function mailer_event_confirm($key, $contact, $subscribe, $hash) {
249 $this->verify($key);
250 $params = array(
251 'contact_id' => $contact,
252 'subscribe_id' => $subscribe,
253 'time_stamp' => date('YmdHis'),
254 'event_subscribe_id' => $subscribe,
255 'hash' => $hash,
256 'version' => 3,
257 );
258 return civicrm_api('Mailing', 'event_confirm', $params);
259 }
260
5bc392e6
EM
261 /**
262 * @param $key
263 * @param $job
264 * @param $queue
265 * @param $hash
266 * @param $bodyTxt
267 * @param $rt
268 * @param null $bodyHTML
269 * @param null $fullEmail
270 *
271 * @return array|int
272 * @throws SoapFault
273 */
40d837df
CW
274 public function mailer_event_reply($key, $job, $queue, $hash, $bodyTxt, $rt, $bodyHTML = NULL, $fullEmail = NULL) {
275 $this->verify($key);
276 $params = array(
277 'job_id' => $job,
278 'event_queue_id' => $queue,
279 'hash' => $hash,
280 'bodyTxt' => $bodyTxt,
281 'replyTo' => $rt,
282 'bodyHTML' => $bodyHTML,
283 'fullEmail' => $fullEmail,
284 'time_stamp' => date('YmdHis'),
285 'version' => 3,
286 );
287 return civicrm_api('Mailing', 'event_reply', $params);
288 }
289
5bc392e6
EM
290 /**
291 * @param $key
292 * @param $job
293 * @param $queue
294 * @param $hash
295 * @param $email
296 *
297 * @return array|int
298 * @throws SoapFault
299 */
40d837df
CW
300 public function mailer_event_forward($key, $job, $queue, $hash, $email) {
301 $this->verify($key);
302 $params = array(
303 'job_id' => $job,
304 'event_queue_id' => $queue,
305 'hash' => $hash,
306 'email' => $email,
307 'version' => 3,
308 );
309 return civicrm_api('Mailing', 'event_forward', $params);
310 }
311
5bc392e6
EM
312 /**
313 * @param $key
c490a46a 314 * @param array $params
5bc392e6
EM
315 *
316 * @return array|int
317 * @throws SoapFault
318 */
40d837df
CW
319 public function get_contact($key, $params) {
320 $this->verify($key);
321 $params['version'] = 3;
322 return civicrm_api('contact', 'get', $params);
323 }
96025800 324
40d837df 325}