INFRA-132 - Add space before "{"
[civicrm-core.git] / CRM / Utils / SoapServer.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class handles all SOAP client requests.
38 *
39 * @package CRM
40 * @copyright CiviCRM LLC (c) 2004-2014
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 *
61 * @internal param string $uf The userframework class
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 *
73 * @param string $var
74 * The string to be echoed.
75 *
76 * @return string $var
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 *
88 * @param string $key
89 * The soap key generated by authenticate().
90 *
91 * @throws SoapFault
92 * @return void
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
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 *
118 * @param string $name
119 * Login name.
120 * @param string $pass
121 * Password.
122 *
123 * @param bool $loadCMSBootstrap
124 *
125 * @throws SoapFault
126 * @return string The SOAP Client key
127 * @static
128 */
129 public function authenticate($name, $pass, $loadCMSBootstrap = FALSE) {
130 require_once str_replace('_', DIRECTORY_SEPARATOR, $this->ufClass) . '.php';
131
132 if ($this->ufClass == 'CRM_Utils_System_Joomla') {
133 $loadCMSBootstrap = TRUE;
134 }
135
136 $className = $this->ufClass;
137 $result =& $className::authenticate($name, $pass, $loadCMSBootstrap );
138
139 if (empty($result)) {
140 throw new SoapFault('Client', 'Invalid login');
141 }
142
143 $session = CRM_Core_Session::singleton();
144 $session->set('soap_key', $result[2]);
145 $session->set('soap_time', time());
146
147 return sha1($result[2]);
148 }
149
150 /*** MAILER API ***/
151 public function mailer_event_bounce($key, $job, $queue, $hash, $body) {
152 $this->verify($key);
153 $params = array(
154 'job_id' => $job,
155 'time_stamp' => date('YmdHis'),
156 'event_queue_id' => $queue,
157 'hash' => $hash,
158 'body' => $body,
159 'version' => 3,
160 );
161 return civicrm_api('Mailing', 'event_bounce', $params);
162 }
163
164 /**
165 * @param $key
166 * @param $job
167 * @param $queue
168 * @param $hash
169 *
170 * @return array|int
171 * @throws SoapFault
172 */
173 public function mailer_event_unsubscribe($key, $job, $queue, $hash) {
174 $this->verify($key);
175 $params = array(
176 'job_id' => $job,
177 'time_stamp' => date('YmdHis'),
178 'org_unsubscribe' => 0,
179 'event_queue_id' => $queue,
180 'hash' => $hash,
181 'version' => 3,
182 );
183 return civicrm_api('MailingGroup', 'event_unsubscribe', $params);
184 }
185
186 /**
187 * @param $key
188 * @param $job
189 * @param $queue
190 * @param $hash
191 *
192 * @return array|int
193 * @throws SoapFault
194 */
195 public function mailer_event_domain_unsubscribe($key, $job, $queue, $hash) {
196 $this->verify($key);
197 $params = array(
198 'job_id' => $job,
199 'time_stamp' => date('YmdHis'),
200 'org_unsubscribe' => 1,
201 'event_queue_id' => $queue,
202 'hash' => $hash,
203 'version' => 3,
204 );
205 return civicrm_api('MailingGroup', 'event_domain_unsubscribe', $params);
206 }
207
208 /**
209 * @param $key
210 * @param $job
211 * @param $queue
212 * @param $hash
213 *
214 * @return array|int
215 * @throws SoapFault
216 */
217 public function mailer_event_resubscribe($key, $job, $queue, $hash) {
218 $this->verify($key);
219 $params = array(
220 'job_id' => $job,
221 'time_stamp' => date('YmdHis'),
222 'org_unsubscribe' => 0,
223 'event_queue_id' => $queue,
224 'hash' => $hash,
225 'version' => 3,
226 );
227 return civicrm_api('MailingGroup', 'event_resubscribe', $params);
228 }
229
230 /**
231 * @param $key
232 * @param $email
233 * @param $domain
234 * @param $group
235 *
236 * @return array|int
237 * @throws SoapFault
238 */
239 public function mailer_event_subscribe($key, $email, $domain, $group) {
240 $this->verify($key);
241 $params = array(
242 'email' => $email,
243 'group_id' => $group,
244 'version' => 3,
245 );
246 return civicrm_api('MailingGroup', 'event_subscribe', $params);
247 }
248
249 /**
250 * @param $key
251 * @param $contact
252 * @param $subscribe
253 * @param $hash
254 *
255 * @return array|int
256 * @throws SoapFault
257 */
258 public function mailer_event_confirm($key, $contact, $subscribe, $hash) {
259 $this->verify($key);
260 $params = array(
261 'contact_id' => $contact,
262 'subscribe_id' => $subscribe,
263 'time_stamp' => date('YmdHis'),
264 'event_subscribe_id' => $subscribe,
265 'hash' => $hash,
266 'version' => 3,
267 );
268 return civicrm_api('Mailing', 'event_confirm', $params);
269 }
270
271 /**
272 * @param $key
273 * @param $job
274 * @param $queue
275 * @param $hash
276 * @param $bodyTxt
277 * @param $rt
278 * @param null $bodyHTML
279 * @param null $fullEmail
280 *
281 * @return array|int
282 * @throws SoapFault
283 */
284 public function mailer_event_reply($key, $job, $queue, $hash, $bodyTxt, $rt, $bodyHTML = NULL, $fullEmail = NULL) {
285 $this->verify($key);
286 $params = array(
287 'job_id' => $job,
288 'event_queue_id' => $queue,
289 'hash' => $hash,
290 'bodyTxt' => $bodyTxt,
291 'replyTo' => $rt,
292 'bodyHTML' => $bodyHTML,
293 'fullEmail' => $fullEmail,
294 'time_stamp' => date('YmdHis'),
295 'version' => 3,
296 );
297 return civicrm_api('Mailing', 'event_reply', $params);
298 }
299
300 /**
301 * @param $key
302 * @param $job
303 * @param $queue
304 * @param $hash
305 * @param $email
306 *
307 * @return array|int
308 * @throws SoapFault
309 */
310 public function mailer_event_forward($key, $job, $queue, $hash, $email) {
311 $this->verify($key);
312 $params = array(
313 'job_id' => $job,
314 'event_queue_id' => $queue,
315 'hash' => $hash,
316 'email' => $email,
317 'version' => 3,
318 );
319 return civicrm_api('Mailing', 'event_forward', $params);
320 }
321
322 /**
323 * @param $key
324 * @param array $params
325 *
326 * @return array|int
327 * @throws SoapFault
328 */
329 public function get_contact($key, $params) {
330 $this->verify($key);
331 $params['version'] = 3;
332 return civicrm_api('contact', 'get', $params);
333 }
334 }