| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * This class handles all SOAP client requests. |
| 14 | * |
| 15 | * @package CRM |
| 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 17 | */ |
| 18 | class CRM_Utils_SoapServer { |
| 19 | |
| 20 | /** |
| 21 | * Number of seconds we should let a soap process idle |
| 22 | * @var int |
| 23 | */ |
| 24 | public static $soap_timeout = 0; |
| 25 | |
| 26 | /** |
| 27 | * Cache the actual UF Class |
| 28 | * @var string |
| 29 | */ |
| 30 | public $ufClass; |
| 31 | |
| 32 | /** |
| 33 | * Class constructor. This caches the real user framework class locally, |
| 34 | * so we can use it for authentication and validation. |
| 35 | * |
| 36 | * @internal param string $uf The userframework class |
| 37 | */ |
| 38 | public function __construct() { |
| 39 | // any external program which call SoapServer is responsible for |
| 40 | // creating and attaching the session |
| 41 | $args = func_get_args(); |
| 42 | $this->ufClass = array_shift($args); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Simple ping function to test for liveness. |
| 47 | * |
| 48 | * @param string $var |
| 49 | * The string to be echoed. |
| 50 | * |
| 51 | * @return string |
| 52 | */ |
| 53 | public function ping($var) { |
| 54 | $session = CRM_Core_Session::singleton(); |
| 55 | $key = $session->get('key'); |
| 56 | $session->set('key', $var); |
| 57 | return "PONG: $var ($key)"; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Verify a SOAP key. |
| 62 | * |
| 63 | * @param string $key |
| 64 | * The soap key generated by authenticate(). |
| 65 | * |
| 66 | * @throws SoapFault |
| 67 | */ |
| 68 | public function verify($key) { |
| 69 | $session = CRM_Core_Session::singleton(); |
| 70 | |
| 71 | $soap_key = $session->get('soap_key'); |
| 72 | $t = time(); |
| 73 | |
| 74 | if ($key !== sha1($soap_key)) { |
| 75 | throw new SoapFault('Client', 'Invalid key'); |
| 76 | } |
| 77 | |
| 78 | if (self::$soap_timeout && |
| 79 | $t > ($session->get('soap_time') + self::$soap_timeout) |
| 80 | ) { |
| 81 | throw new SoapFault('Client', 'Expired key'); |
| 82 | } |
| 83 | |
| 84 | // otherwise, we're ok. update the timestamp |
| 85 | |
| 86 | $session->set('soap_time', $t); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Authentication wrapper to the UF Class. |
| 91 | * |
| 92 | * @param string $name |
| 93 | * Login name. |
| 94 | * @param string $pass |
| 95 | * Password. |
| 96 | * |
| 97 | * @param bool $loadCMSBootstrap |
| 98 | * |
| 99 | * @throws SoapFault |
| 100 | * @return string |
| 101 | * The SOAP Client key |
| 102 | */ |
| 103 | public function authenticate($name, $pass, $loadCMSBootstrap = FALSE) { |
| 104 | require_once str_replace('_', DIRECTORY_SEPARATOR, $this->ufClass) . '.php'; |
| 105 | |
| 106 | if ($this->ufClass == 'CRM_Utils_System_Joomla' |
| 107 | || $this->ufClass == 'CRM_Utils_System_WordPress') { |
| 108 | $loadCMSBootstrap = TRUE; |
| 109 | } |
| 110 | |
| 111 | $result = CRM_Utils_System::authenticate($name, $pass, $loadCMSBootstrap); |
| 112 | |
| 113 | if (empty($result)) { |
| 114 | throw new SoapFault('Client', 'Invalid login'); |
| 115 | } |
| 116 | |
| 117 | $session = CRM_Core_Session::singleton(); |
| 118 | $session->set('soap_key', $result[2]); |
| 119 | $session->set('soap_time', time()); |
| 120 | |
| 121 | return sha1($result[2]); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * MAILER API. |
| 126 | * |
| 127 | * @param string $key |
| 128 | * @param int $job |
| 129 | * @param int $queue |
| 130 | * @param string $hash |
| 131 | * @param string $body |
| 132 | * |
| 133 | * @return array|int |
| 134 | * @throws \SoapFault |
| 135 | */ |
| 136 | public function mailer_event_bounce($key, $job, $queue, $hash, $body) { |
| 137 | $this->verify($key); |
| 138 | $params = [ |
| 139 | 'job_id' => $job, |
| 140 | 'time_stamp' => date('YmdHis'), |
| 141 | 'event_queue_id' => $queue, |
| 142 | 'hash' => $hash, |
| 143 | 'body' => $body, |
| 144 | 'version' => 3, |
| 145 | ]; |
| 146 | $result = civicrm_api('Mailing', 'event_bounce', $params); |
| 147 | return CRM_Utils_Array::encode_items($result); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Mailer event unsubscribe. |
| 152 | * |
| 153 | * @param string $key |
| 154 | * @param int $job |
| 155 | * @param int $queue |
| 156 | * @param string $hash |
| 157 | * |
| 158 | * @return array|int |
| 159 | * @throws SoapFault |
| 160 | */ |
| 161 | public function mailer_event_unsubscribe($key, $job, $queue, $hash) { |
| 162 | $this->verify($key); |
| 163 | $params = [ |
| 164 | 'job_id' => $job, |
| 165 | 'time_stamp' => date('YmdHis'), |
| 166 | 'org_unsubscribe' => 0, |
| 167 | 'event_queue_id' => $queue, |
| 168 | 'hash' => $hash, |
| 169 | 'version' => 3, |
| 170 | ]; |
| 171 | $result = civicrm_api('MailingGroup', 'event_unsubscribe', $params); |
| 172 | return CRM_Utils_Array::encode_items($result); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @param $key |
| 177 | * @param $job |
| 178 | * @param $queue |
| 179 | * @param $hash |
| 180 | * |
| 181 | * @return array|int |
| 182 | * @throws SoapFault |
| 183 | */ |
| 184 | public function mailer_event_domain_unsubscribe($key, $job, $queue, $hash) { |
| 185 | $this->verify($key); |
| 186 | $params = [ |
| 187 | 'job_id' => $job, |
| 188 | 'time_stamp' => date('YmdHis'), |
| 189 | 'org_unsubscribe' => 1, |
| 190 | 'event_queue_id' => $queue, |
| 191 | 'hash' => $hash, |
| 192 | 'version' => 3, |
| 193 | ]; |
| 194 | $result = civicrm_api('MailingGroup', 'event_domain_unsubscribe', $params); |
| 195 | return CRM_Utils_Array::encode_items($result); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @param $key |
| 200 | * @param $job |
| 201 | * @param $queue |
| 202 | * @param $hash |
| 203 | * |
| 204 | * @return array|int |
| 205 | * @throws SoapFault |
| 206 | */ |
| 207 | public function mailer_event_resubscribe($key, $job, $queue, $hash) { |
| 208 | $this->verify($key); |
| 209 | $params = [ |
| 210 | 'job_id' => $job, |
| 211 | 'time_stamp' => date('YmdHis'), |
| 212 | 'org_unsubscribe' => 0, |
| 213 | 'event_queue_id' => $queue, |
| 214 | 'hash' => $hash, |
| 215 | 'version' => 3, |
| 216 | ]; |
| 217 | $result = civicrm_api('MailingGroup', 'event_resubscribe', $params); |
| 218 | return CRM_Utils_Array::encode_items($result); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @param $key |
| 223 | * @param $email |
| 224 | * @param $domain |
| 225 | * @param $group |
| 226 | * |
| 227 | * @return array|int |
| 228 | * @throws SoapFault |
| 229 | */ |
| 230 | public function mailer_event_subscribe($key, $email, $domain, $group) { |
| 231 | $this->verify($key); |
| 232 | $params = [ |
| 233 | 'email' => $email, |
| 234 | 'group_id' => $group, |
| 235 | 'version' => 3, |
| 236 | ]; |
| 237 | $result = civicrm_api('MailingGroup', 'event_subscribe', $params); |
| 238 | return CRM_Utils_Array::encode_items($result); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * @param $key |
| 243 | * @param $contact |
| 244 | * @param $subscribe |
| 245 | * @param $hash |
| 246 | * |
| 247 | * @return array|int |
| 248 | * @throws SoapFault |
| 249 | */ |
| 250 | public function mailer_event_confirm($key, $contact, $subscribe, $hash) { |
| 251 | $this->verify($key); |
| 252 | $params = [ |
| 253 | 'contact_id' => $contact, |
| 254 | 'subscribe_id' => $subscribe, |
| 255 | 'time_stamp' => date('YmdHis'), |
| 256 | 'event_subscribe_id' => $subscribe, |
| 257 | 'hash' => $hash, |
| 258 | 'version' => 3, |
| 259 | ]; |
| 260 | $result = civicrm_api('Mailing', 'event_confirm', $params); |
| 261 | return CRM_Utils_Array::encode_items($result); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @param $key |
| 266 | * @param $job |
| 267 | * @param $queue |
| 268 | * @param $hash |
| 269 | * @param $bodyTxt |
| 270 | * @param $rt |
| 271 | * @param null $bodyHTML |
| 272 | * @param null $fullEmail |
| 273 | * |
| 274 | * @return array|int |
| 275 | * @throws SoapFault |
| 276 | */ |
| 277 | public function mailer_event_reply($key, $job, $queue, $hash, $bodyTxt, $rt, $bodyHTML = NULL, $fullEmail = NULL) { |
| 278 | $this->verify($key); |
| 279 | $params = [ |
| 280 | 'job_id' => $job, |
| 281 | 'event_queue_id' => $queue, |
| 282 | 'hash' => $hash, |
| 283 | 'bodyTxt' => $bodyTxt, |
| 284 | 'replyTo' => $rt, |
| 285 | 'bodyHTML' => $bodyHTML, |
| 286 | 'fullEmail' => $fullEmail, |
| 287 | 'time_stamp' => date('YmdHis'), |
| 288 | 'version' => 3, |
| 289 | ]; |
| 290 | $result = civicrm_api('Mailing', 'event_reply', $params); |
| 291 | return CRM_Utils_Array::encode_items($result); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * @param $key |
| 296 | * @param $job |
| 297 | * @param $queue |
| 298 | * @param $hash |
| 299 | * @param $email |
| 300 | * |
| 301 | * @return array|int |
| 302 | * @throws SoapFault |
| 303 | */ |
| 304 | public function mailer_event_forward($key, $job, $queue, $hash, $email) { |
| 305 | $this->verify($key); |
| 306 | $params = [ |
| 307 | 'job_id' => $job, |
| 308 | 'event_queue_id' => $queue, |
| 309 | 'hash' => $hash, |
| 310 | 'email' => $email, |
| 311 | 'version' => 3, |
| 312 | ]; |
| 313 | $result = civicrm_api('Mailing', 'event_forward', $params); |
| 314 | return CRM_Utils_Array::encode_items($result); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * @param $key |
| 319 | * @param array $params |
| 320 | * |
| 321 | * @return array|int |
| 322 | * @throws SoapFault |
| 323 | */ |
| 324 | public function get_contact($key, $params) { |
| 325 | $this->verify($key); |
| 326 | $params['version'] = 3; |
| 327 | $result = civicrm_api('contact', 'get', $params); |
| 328 | return CRM_Utils_Array::encode_items($result); |
| 329 | } |
| 330 | |
| 331 | } |