Merge pull request #1226 from kurund/CRM-11137
[civicrm-core.git] / CRM / Utils / SoapServer.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class handles all SOAP client requests.
38 *
39 * @package CRM
40 * @copyright CiviCRM LLC (c) 2004-2013
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 * @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 * @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 *
88 * @param string $key The soap key generated by authenticate()
89 *
90 * @return none
91 * @access public
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
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 Login name
119 * @param string $pass Password
120 *
121 * @return string The SOAP Client key
122 * @access public
123 * @static
124 */
125 public function authenticate($name, $pass, $loadCMSBootstrap = FALSE) {
126 require_once (str_replace('_', DIRECTORY_SEPARATOR, $this->ufClass) . '.php');
127
128 if ($this->ufClass == 'CRM_Utils_System_Joomla'){
129 $loadCMSBootstrap = true;
130 }
131
132 $className = $this->ufClass;
133 $result =& $className::authenticate($name, $pass, $loadCMSBootstrap );
134
135 if (empty($result)) {
136 throw new SoapFault('Client', 'Invalid login');
137 }
138
139 $session = CRM_Core_Session::singleton();
140 $session->set('soap_key', $result[2]);
141 $session->set('soap_time', time());
142
143 return sha1($result[2]);
144 }
145
146 /*** MAILER API ***/
147 public function mailer_event_bounce($key, $job, $queue, $hash, $body) {
148 $this->verify($key);
149 $params = array(
150 'job_id' => $job,
151 'time_stamp' => date('YmdHis'),
152 'event_queue_id' => $queue,
153 'hash' => $hash,
154 'body' => $body,
155 'version' => 3,
156 );
157 return civicrm_api('Mailing', 'event_bounce', $params);
158 }
159
160 public function mailer_event_unsubscribe($key, $job, $queue, $hash) {
161 $this->verify($key);
162 $params = array(
163 'job_id' => $job,
164 'time_stamp' => date('YmdHis'),
165 'org_unsubscribe' => 0,
166 'event_queue_id' => $queue,
167 'hash' => $hash,
168 'version' => 3,
169 );
170 return civicrm_api('MailingGroup', 'event_unsubscribe', $params);
171 }
172
173 public function mailer_event_domain_unsubscribe($key, $job, $queue, $hash) {
174 $this->verify($key);
175 $params = array(
176 'job_id' => $job,
177 'time_stamp' => date('YmdHis'),
178 'org_unsubscribe' => 1,
179 'event_queue_id' => $queue,
180 'hash' => $hash,
181 'version' => 3,
182 );
183 return civicrm_api('MailingGroup', 'event_domain_unsubscribe', $params);
184 }
185
186 public function mailer_event_resubscribe($key, $job, $queue, $hash) {
187 $this->verify($key);
188 $params = array(
189 'job_id' => $job,
190 'time_stamp' => date('YmdHis'),
191 'org_unsubscribe' => 0,
192 'event_queue_id' => $queue,
193 'hash' => $hash,
194 'version' => 3,
195 );
196 return civicrm_api('MailingGroup', 'event_resubscribe', $params);
197 }
198
199 public function mailer_event_subscribe($key, $email, $domain, $group) {
200 $this->verify($key);
201 $params = array(
202 'email' => $email,
203 'group_id' => $group,
204 'version' => 3,
205 );
206 return civicrm_api('MailingGroup', 'event_subscribe', $params);
207 }
208
209 public function mailer_event_confirm($key, $contact, $subscribe, $hash) {
210 $this->verify($key);
211 $params = array(
212 'contact_id' => $contact,
213 'subscribe_id' => $subscribe,
214 'time_stamp' => date('YmdHis'),
215 'event_subscribe_id' => $subscribe,
216 'hash' => $hash,
217 'version' => 3,
218 );
219 return civicrm_api('Mailing', 'event_confirm', $params);
220 }
221
222 public function mailer_event_reply($key, $job, $queue, $hash, $bodyTxt, $rt, $bodyHTML = NULL, $fullEmail = NULL) {
223 $this->verify($key);
224 $params = array(
225 'job_id' => $job,
226 'event_queue_id' => $queue,
227 'hash' => $hash,
228 'bodyTxt' => $bodyTxt,
229 'replyTo' => $rt,
230 'bodyHTML' => $bodyHTML,
231 'fullEmail' => $fullEmail,
232 'time_stamp' => date('YmdHis'),
233 'version' => 3,
234 );
235 return civicrm_api('Mailing', 'event_reply', $params);
236 }
237
238 public function mailer_event_forward($key, $job, $queue, $hash, $email) {
239 $this->verify($key);
240 $params = array(
241 'job_id' => $job,
242 'event_queue_id' => $queue,
243 'hash' => $hash,
244 'email' => $email,
245 'version' => 3,
246 );
247 return civicrm_api('Mailing', 'event_forward', $params);
248 }
249
250 public function get_contact($key, $params) {
251 $this->verify($key);
252 $params['version'] = 3;
253 return civicrm_api('contact', 'get', $params);
254 }
255 }
256