3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 abstract class CRM_SMS_Provider
{
20 * We only need one instance of this object. So we use the singleton
21 * pattern and cache the instance in this variable
25 static private $_singleton = [];
26 const MAX_SMS_CHAR
= 460;
29 * Singleton function used to manage this object.
31 * @param array $providerParams
35 * @throws CRM_Core_Exception
37 public static function &singleton($providerParams = [], $force = FALSE) {
38 $mailingID = $providerParams['mailing_id'] ??
NULL;
39 $providerID = $providerParams['provider_id'] ??
NULL;
40 $providerName = $providerParams['provider'] ??
NULL;
42 if (!$providerID && $mailingID) {
43 $providerID = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_Mailing', $mailingID, 'sms_provider_id', 'id');
44 $providerParams['provider_id'] = $providerID;
47 $providerName = CRM_SMS_BAO_Provider
::getProviderInfo($providerID, 'name');
51 throw new CRM_Core_Exception('Provider not known or not provided.');
54 $providerName = CRM_Utils_Type
::escape($providerName, 'String');
55 $cacheKey = "{$providerName}_" . (int) $providerID . "_" . (int) $mailingID;
57 if (!isset(self
::$_singleton[$cacheKey]) ||
$force) {
58 $ext = CRM_Extension_System
::singleton()->getMapper();
59 if ($ext->isExtensionKey($providerName)) {
60 $providerClass = $ext->keyToClass($providerName);
61 require_once "{$providerClass}.php";
64 // If we are running unit tests we simulate an SMS provider with the name "CiviTestSMSProvider"
65 if ($providerName !== 'CiviTestSMSProvider') {
66 throw new CRM_Core_Exception("Could not locate extension for {$providerName}.");
68 $providerClass = 'CiviTestSMSProvider';
71 self
::$_singleton[$cacheKey] = $providerClass::singleton($providerParams, $force);
73 return self
::$_singleton[$cacheKey];
77 * Send an SMS Message via the API Server.
79 * @param array $recipients
80 * @param string $header
81 * @param string $message
84 abstract public function send($recipients, $header, $message, $dncID = NULL);
87 * @param int $apiMsgID
89 * @param array $headers
93 * @return self|null|object
94 * @throws CRM_Core_Exception
96 public function createActivity($apiMsgID, $message, $headers = [], $jobID = NULL, $userID = NULL) {
99 SELECT scheduled_id FROM civicrm_mailing m
100 INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
101 $sourceContactID = CRM_Core_DAO
::singleValueQuery($sql, array(1 => array($jobID, 'Integer')));
104 $sourceContactID = $userID;
107 $session = CRM_Core_Session
::singleton();
108 $sourceContactID = $session->get('userID');
111 if (!$sourceContactID) {
112 $sourceContactID = $headers['Contact'] ??
NULL;
114 if (!$sourceContactID) {
118 // note: lets not pass status here, assuming status will be updated by callback
119 $activityParams = array(
120 'source_contact_id' => $sourceContactID,
121 'target_contact_id' => $headers['contact_id'],
122 'activity_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS delivery'),
123 'activity_date_time' => date('YmdHis'),
124 'details' => $message,
125 'result' => $apiMsgID,
127 return CRM_Activity_BAO_Activity
::create($activityParams);
131 * @param string $name
134 * @param null $default
135 * @param string $location
139 public function retrieve($name, $type, $abort = TRUE, $default = NULL, $location = 'REQUEST') {
140 static $store = NULL;
141 $value = CRM_Utils_Request
::retrieve($name, $type, $store,
142 FALSE, $default, $location
144 if ($abort && $value === NULL) {
145 Civi
::log()->warning("Could not find an entry for $name in $location");
146 echo "Failure: Missing Parameter<p>";
156 * @param int $trackID
158 * @return self|null|object
159 * @throws CRM_Core_Exception
161 public function processInbound($from, $body, $to = NULL, $trackID = NULL) {
162 $message = new CRM_SMS_Message();
163 $message->from
= $from;
165 $message->body
= $body;
166 $message->trackID
= $trackID;
167 // call hook_civicrm_inboundSMS
168 CRM_Utils_Hook
::inboundSMS($message);
170 if (!$message->fromContactID
) {
171 // find sender by phone number if $fromContactID not set by hook
172 $formatFrom = '%' . $this->formatPhone($this->stripPhone($message->from
), $like, "like");
173 $message->fromContactID
= CRM_Core_DAO
::singleValueQuery("SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE %1", array(
174 1 => array($formatFrom, 'String'),
178 if (!$message->fromContactID
) {
179 // unknown mobile sender -- create new contact
180 // use fake @mobile.sms email address for new contact since civi
181 // requires email or name for all contacts
182 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
183 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
184 $phoneloc = array_search('Home', $locationTypes);
185 $phonetype = array_search('Mobile', $phoneTypes);
186 $stripFrom = $this->stripPhone($message->from
);
187 $contactparams = array(
188 'contact_type' => 'Individual',
191 'location_type_id' => $phoneloc,
192 'email' => $stripFrom . '@mobile.sms',
197 'phone_type_id' => $phonetype,
198 'location_type_id' => $phoneloc,
199 'phone' => $stripFrom,
203 $fromContact = CRM_Contact_BAO_Contact
::create($contactparams, FALSE, TRUE, FALSE);
204 $message->fromContactID
= $fromContact->id
;
207 if (!$message->toContactID
) {
208 // find recipient if $toContactID not set by hook
210 $message->toContactID
= CRM_Core_DAO
::singleValueQuery("SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE %1", array(
211 1 => array('%' . $message->to
, 'String'),
215 $message->toContactID
= $message->fromContactID
;
219 if ($message->fromContactID
) {
220 // note: lets not pass status here, assuming status will be updated by callback
221 $activityParams = array(
222 'source_contact_id' => $message->toContactID
,
223 'target_contact_id' => $message->fromContactID
,
224 'activity_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Inbound SMS'),
225 'activity_date_time' => date('YmdHis'),
226 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'),
227 'details' => $message->body
,
228 'phone_number' => $message->from
,
230 if ($message->trackID
) {
231 $activityParams['result'] = CRM_Utils_Type
::escape($message->trackID
, 'String');
234 $result = CRM_Activity_BAO_Activity
::create($activityParams);
235 Civi
::log()->info("Inbound SMS recorded for cid={$message->fromContactID}.");
243 * @return mixed|string
245 public function stripPhone($phone) {
246 $newphone = preg_replace('/[^0-9x]/', '', $phone);
247 while (substr($newphone, 0, 1) == "1") {
248 $newphone = substr($newphone, 1);
250 while (strpos($newphone, "xx") !== FALSE) {
251 $newphone = str_replace("xx", "x", $newphone);
253 while (substr($newphone, -1) == "x") {
254 $newphone = substr($newphone, 0, -1);
262 * @param string $format
264 * @return mixed|string
266 public function formatPhone($phone, &$kind, $format = "dash") {
267 $phoneA = explode("x", $phone);
268 switch (strlen($phoneA[0])) {
278 $kind = $phoneA[1] ?
"LocalX" : "Local";
280 $exch = substr($phone, 0, 3);
281 $uniq = substr($phone, 3, 4);
286 $kind = $phoneA[1] ?
"LongX" : "Long";
287 $area = substr($phone, 0, 3);
288 $exch = substr($phone, 3, 3);
289 $uniq = substr($phone, 6, 4);
300 $newphone = '%' . $area . '%' . $exch . '%' . $uniq . '%' . $ext . '%';
301 $newphone = str_replace('%%', '%', $newphone);
302 $newphone = str_replace('%%', '%', $newphone);
306 $newphone = $area . "-" . $exch . "-" . $uniq . " x" . $ext;
307 $newphone = trim(trim(trim($newphone, "x"), "-"));
311 $newphone = $area . $exch . $uniq . "x" . $ext;
312 $newphone = trim(trim(trim($newphone, "x"), "-"));
328 public function urlEncode($values) {
330 foreach ($values as $key => $value) {
331 $value = urlencode($value);
332 $uri .= "&{$key}={$value}";
335 $uri = substr($uri, 1);