minor code cleanups - add comments, remove unused var, declare function as static
[civicrm-core.git] / CRM / Utils / SoapServer.php
CommitLineData
40d837df
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 */
44class 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 *
73 * @param string $var The string to be echoed
74 *
75 * @return string $var
76 * @access public
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 *
f4aaa82a 88 * @param string $key The soap key generated by authenticate()
40d837df 89 *
f4aaa82a 90 * @throws SoapFault
355ba699 91 * @return void
40d837df
CW
92 * @access public
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
105 if (self::$soap_timeout &&
106 $t > ($session->get('soap_time') + self::$soap_timeout)
107 ) {
108 throw new SoapFault('Client', 'Expired key');
109 }
110
111 /* otherwise, we're ok. update the timestamp */
112
113 $session->set('soap_time', $t);
114 }
115
116 /**
117 * Authentication wrapper to the UF Class
118 *
f4aaa82a
EM
119 * @param string $name Login name
120 * @param string $pass Password
40d837df 121 *
f4aaa82a
EM
122 * @param bool $loadCMSBootstrap
123 *
124 * @throws SoapFault
40d837df
CW
125 * @return string The SOAP Client key
126 * @access public
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 public function mailer_event_unsubscribe($key, $job, $queue, $hash) {
165 $this->verify($key);
166 $params = array(
167 'job_id' => $job,
168 'time_stamp' => date('YmdHis'),
169 'org_unsubscribe' => 0,
170 'event_queue_id' => $queue,
171 'hash' => $hash,
172 'version' => 3,
173 );
174 return civicrm_api('MailingGroup', 'event_unsubscribe', $params);
175 }
176
177 public function mailer_event_domain_unsubscribe($key, $job, $queue, $hash) {
178 $this->verify($key);
179 $params = array(
180 'job_id' => $job,
181 'time_stamp' => date('YmdHis'),
182 'org_unsubscribe' => 1,
183 'event_queue_id' => $queue,
184 'hash' => $hash,
185 'version' => 3,
186 );
187 return civicrm_api('MailingGroup', 'event_domain_unsubscribe', $params);
188 }
189
190 public function mailer_event_resubscribe($key, $job, $queue, $hash) {
191 $this->verify($key);
192 $params = array(
193 'job_id' => $job,
194 'time_stamp' => date('YmdHis'),
195 'org_unsubscribe' => 0,
196 'event_queue_id' => $queue,
197 'hash' => $hash,
198 'version' => 3,
199 );
200 return civicrm_api('MailingGroup', 'event_resubscribe', $params);
201 }
202
203 public function mailer_event_subscribe($key, $email, $domain, $group) {
204 $this->verify($key);
205 $params = array(
206 'email' => $email,
207 'group_id' => $group,
208 'version' => 3,
209 );
210 return civicrm_api('MailingGroup', 'event_subscribe', $params);
211 }
212
213 public function mailer_event_confirm($key, $contact, $subscribe, $hash) {
214 $this->verify($key);
215 $params = array(
216 'contact_id' => $contact,
217 'subscribe_id' => $subscribe,
218 'time_stamp' => date('YmdHis'),
219 'event_subscribe_id' => $subscribe,
220 'hash' => $hash,
221 'version' => 3,
222 );
223 return civicrm_api('Mailing', 'event_confirm', $params);
224 }
225
226 public function mailer_event_reply($key, $job, $queue, $hash, $bodyTxt, $rt, $bodyHTML = NULL, $fullEmail = NULL) {
227 $this->verify($key);
228 $params = array(
229 'job_id' => $job,
230 'event_queue_id' => $queue,
231 'hash' => $hash,
232 'bodyTxt' => $bodyTxt,
233 'replyTo' => $rt,
234 'bodyHTML' => $bodyHTML,
235 'fullEmail' => $fullEmail,
236 'time_stamp' => date('YmdHis'),
237 'version' => 3,
238 );
239 return civicrm_api('Mailing', 'event_reply', $params);
240 }
241
242 public function mailer_event_forward($key, $job, $queue, $hash, $email) {
243 $this->verify($key);
244 $params = array(
245 'job_id' => $job,
246 'event_queue_id' => $queue,
247 'hash' => $hash,
248 'email' => $email,
249 'version' => 3,
250 );
251 return civicrm_api('Mailing', 'event_forward', $params);
252 }
253
254 public function get_contact($key, $params) {
255 $this->verify($key);
256 $params['version'] = 3;
257 return civicrm_api('contact', 'get', $params);
258 }
259}
260