3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
36 abstract class CRM_SMS_Provider
{
39 * We only need one instance of this object. So we use the singleton
40 * pattern and cache the instance in this variable
45 static private $_singleton = array();
46 CONST MAX_SMS_CHAR
= 460;
49 * singleton function used to manage this object
51 * @param array $providerParams
57 static function &singleton($providerParams = array(
59 $mailingID = CRM_Utils_Array
::value('mailing_id', $providerParams);
60 $providerID = CRM_Utils_Array
::value('provider_id', $providerParams);
61 $providerName = CRM_Utils_Array
::value('provider', $providerParams);
63 if (!$providerID && $mailingID) {
64 $providerID = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_Mailing', $mailingID, 'sms_provider_id', 'id');
65 $providerParams['provider_id'] = $providerID;
68 $providerName = CRM_SMS_BAO_Provider
::getProviderInfo($providerID, 'name');
72 CRM_Core_Error
::fatal('Provider not known or not provided.');
75 $providerName = CRM_Utils_Type
::escape($providerName, 'String');
76 $cacheKey = "{$providerName}_" . (int) $providerID . "_" . (int) $mailingID;
78 if (!isset(self
::$_singleton[$cacheKey]) ||
$force) {
79 $ext = CRM_Extension_System
::singleton()->getMapper();
80 if ($ext->isExtensionKey($providerName)) {
81 $paymentClass = $ext->keyToClass($providerName);
82 require_once ("{$paymentClass}.php");
84 CRM_Core_Error
::fatal("Could not locate extension for {$providerName}.");
87 self
::$_singleton[$cacheKey] = $paymentClass::singleton($providerParams, $force);
89 return self
::$_singleton[$cacheKey];
93 * Send an SMS Message via the API Server
97 abstract function send($recipients, $header, $message, $dncID = NULL);
100 * Function to return message text. Child class could override this function to have better control over the message being sent.
104 function getMessage($message, $contactID, $contactDetails) {
105 $html = $message->getHTMLBody();
106 $text = $message->getTXTBody();
108 return $html ?
$html : $text;
111 function getRecipientDetails($fields, $additionalDetails) {
112 // we could do more altering here
113 $fields['To'] = $fields['phone'];
117 function createActivity($apiMsgID, $message, $headers = array(
118 ), $jobID = NULL, $userID = NULL) {
121 SELECT scheduled_id FROM civicrm_mailing m
122 INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
123 $sourceContactID = CRM_Core_DAO
::singleValueQuery($sql, array(1 => array($jobID, 'Integer')));
126 $sourceContactID=$userID;
129 $session = CRM_Core_Session
::singleton();
130 $sourceContactID = $session->get('userID');
133 if (!$sourceContactID) {
134 $sourceContactID = CRM_Utils_Array
::value('Contact', $headers);
136 if (!$sourceContactID) {
140 $activityTypeID = CRM_Core_OptionGroup
::getValue('activity_type', 'SMS delivery', 'name');
141 // note: lets not pass status here, assuming status will be updated by callback
142 $activityParams = array(
143 'source_contact_id' => $sourceContactID,
144 'target_contact_id' => $headers['contact_id'],
145 'activity_type_id' => $activityTypeID,
146 'activity_date_time' => date('YmdHis'),
147 'details' => $message,
148 'result' => $apiMsgID,
150 return CRM_Activity_BAO_Activity
::create($activityParams);
153 function retrieve($name, $type, $abort = TRUE, $default = NULL, $location = 'REQUEST') {
154 static $store = NULL;
155 $value = CRM_Utils_Request
::retrieve($name, $type, $store,
156 FALSE, $default, $location
158 if ($abort && $value === NULL) {
159 CRM_Core_Error
::debug_log_message("Could not find an entry for $name in $location");
160 echo "Failure: Missing Parameter<p>";
166 function processInbound($from, $body, $to = NULL, $trackID = NULL) {
167 $formatFrom = $this->formatPhone($this->stripPhone($from), $like, "like");
168 $escapedFrom = CRM_Utils_Type
::escape($formatFrom, 'String');
169 $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 "%' . $escapedFrom . '"');
171 if (! $fromContactID) {
172 // unknown mobile sender -- create new contact
173 // use fake @mobile.sms email address for new contact since civi
174 // requires email or name for all contacts
175 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
176 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
177 $phoneloc = array_search('Home', $locationTypes);
178 $phonetype = array_search('Mobile', $phoneTypes);
179 $stripFrom = $this->stripPhone($from);
180 $contactparams = array(
181 'contact_type' => 'Individual',
182 'email' => array(1 => array(
183 'location_type_id' => $phoneloc,
184 'email' => $stripFrom . '@mobile.sms'
186 'phone' => array(1 => array(
187 'phone_type_id' => $phonetype,
188 'location_type_id' => $phoneloc,
189 'phone' => $stripFrom
192 $fromContact = CRM_Contact_BAO_Contact
::create($contactparams, FALSE, TRUE, FALSE);
193 $fromContactID = $fromContact->id
;
197 $to = CRM_Utils_Type
::escape($to, 'String');
198 $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 "%' . $to . '"');
201 $toContactID = $fromContactID;
204 if ($fromContactID) {
205 $actStatusIDs = array_flip(CRM_Core_OptionGroup
::values('activity_status'));
206 $activityTypeID = CRM_Core_OptionGroup
::getValue('activity_type', 'Inbound SMS', 'name');
208 // note: lets not pass status here, assuming status will be updated by callback
209 $activityParams = array(
210 'source_contact_id' => $toContactID,
211 'target_contact_id' => $fromContactID,
212 'activity_type_id' => $activityTypeID,
213 'activity_date_time' => date('YmdHis'),
214 'status_id' => $actStatusIDs['Completed'],
216 'phone_number' => $from
219 $trackID = CRM_Utils_Type
::escape($trackID, 'String');
220 $activityParams['result'] = $trackID;
223 $result = CRM_Activity_BAO_Activity
::create($activityParams);
224 CRM_Core_Error
::debug_log_message("Inbound SMS recorded for cid={$fromContactID}.");
229 function stripPhone($phone) {
230 $newphone = preg_replace('/[^0-9x]/', '', $phone);
231 while (substr($newphone, 0, 1) == "1") {
232 $newphone = substr($newphone, 1);
234 while (strpos($newphone, "xx") !== FALSE) {
235 $newphone = str_replace("xx", "x", $newphone);
237 while (substr($newphone, -1) == "x") {
238 $newphone = substr($newphone, 0, -1);
243 function formatPhone($phone, &$kind, $format = "dash") {
244 $phoneA = explode("x", $phone);
245 switch (strlen($phoneA[0])) {
255 $kind = $phoneA[1] ?
"LocalX" : "Local";
257 $exch = substr($phone, 0, 3);
258 $uniq = substr($phone, 3, 4);
263 $kind = $phoneA[1] ?
"LongX" : "Long";
264 $area = substr($phone, 0, 3);
265 $exch = substr($phone, 3, 3);
266 $uniq = substr($phone, 6, 4);
277 $newphone = '%' . $area . '%' . $exch . '%' . $uniq . '%' . $ext . '%';
278 $newphone = str_replace('%%', '%', $newphone);
279 $newphone = str_replace('%%', '%', $newphone);
283 $newphone = $area . "-" . $exch . "-" . $uniq . " x" . $ext;
284 $newphone = trim(trim(trim($newphone, "x"), "-"));
288 $newphone = $area . $exch . $uniq . "x" . $ext;
289 $newphone = trim(trim(trim($newphone, "x"), "-"));
300 function urlEncode($values) {
302 foreach ($values as $key => $value) {
303 $value = urlencode($value);
304 $uri .= "&{$key}={$value}";
307 $uri = substr($uri, 1);