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