Merge pull request #5235 from atif-shaikh/CRM-5039MinorFixes
[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 */
49 static $soap_timeout = 0;
50
51 /**
52 * Cache the actual UF Class
53 */
54 public $ufClass;
55
56 /**
57 * Class constructor. This caches the real user framework class locally,
58 * so we can use it for authentication and validation.
59 *
60 * @internal param string $uf The userframework class
61 */
62 public function __construct() {
63 // any external program which call SoapServer is responsible for
64 // creating and attaching the session
65 $args = func_get_args();
66 $this->ufClass = array_shift($args);
67 }
68
69 /**
70 * Simple ping function to test for liveness.
71 *
72 * @param string $var
73 * The string to be echoed.
74 *
75 * @return string
76 */
77 public function ping($var) {
78 $session = CRM_Core_Session::singleton();
79 $key = $session->get('key');
80 $session->set('key', $var);
81 return "PONG: $var ($key)";
82 }
83
84 /**
85 * Verify a SOAP key.
86 *
87 * @param string $key
88 * The soap key generated by authenticate().
89 *
90 * @throws SoapFault
91 * @return void
92 */
93 public function verify($key) {
94 $session = CRM_Core_Session::singleton();
95
96 $soap_key = $session->get('soap_key');
97 $t = time();
98
99 if ($key !== sha1($soap_key)) {
100 throw new SoapFault('Client', 'Invalid key');
101 }
102
103 if (self::$soap_timeout &&
104 $t > ($session->get('soap_time') + self::$soap_timeout)
105 ) {
106 throw new SoapFault('Client', 'Expired key');
107 }
108
109 /* otherwise, we're ok. update the timestamp */
110
111 $session->set('soap_time', $t);
112 }
113
114 /**
115 * Authentication wrapper to the UF Class.
116 *
117 * @param string $name
118 * Login name.
119 * @param string $pass
120 * Password.
121 *
122 * @param bool $loadCMSBootstrap
123 *
124 * @throws SoapFault
125 * @return string
126 * The SOAP Client key
127 */
128 public function authenticate($name, $pass, $loadCMSBootstrap = FALSE) {
129 require_once str_replace('_', DIRECTORY_SEPARATOR, $this->ufClass) . '.php';
130
131 if ($this->ufClass == 'CRM_Utils_System_Joomla') {
132 $loadCMSBootstrap = TRUE;
133 }
134
135 $className = $this->ufClass;
136 $result =& $className::authenticate($name, $pass, $loadCMSBootstrap);
137
138 if (empty($result)) {
139 throw new SoapFault('Client', 'Invalid login');
140 }
141
142 $session = CRM_Core_Session::singleton();
143 $session->set('soap_key', $result[2]);
144 $session->set('soap_time', time());
145
146 return sha1($result[2]);
147 }
148
149 /**
150 * MAILER API.
151 */
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
165 /**
166 * @param $key
167 * @param $job
168 * @param $queue
169 * @param $hash
170 *
171 * @return array|int
172 * @throws SoapFault
173 */
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
187 /**
188 * @param $key
189 * @param $job
190 * @param $queue
191 * @param $hash
192 *
193 * @return array|int
194 * @throws SoapFault
195 */
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
209 /**
210 * @param $key
211 * @param $job
212 * @param $queue
213 * @param $hash
214 *
215 * @return array|int
216 * @throws SoapFault
217 */
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
231 /**
232 * @param $key
233 * @param $email
234 * @param $domain
235 * @param $group
236 *
237 * @return array|int
238 * @throws SoapFault
239 */
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
250 /**
251 * @param $key
252 * @param $contact
253 * @param $subscribe
254 * @param $hash
255 *
256 * @return array|int
257 * @throws SoapFault
258 */
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
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 */
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
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 */
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
323 /**
324 * @param $key
325 * @param array $params
326 *
327 * @return array|int
328 * @throws SoapFault
329 */
330 public function get_contact($key, $params) {
331 $this->verify($key);
332 $params['version'] = 3;
333 return civicrm_api('contact', 'get', $params);
334 }
335
336 }