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