3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public static function &singleton($providerParams = array(), $force = FALSE) {
58 $mailingID = CRM_Utils_Array
::value('mailing_id', $providerParams);
59 $providerID = CRM_Utils_Array
::value('provider_id', $providerParams);
60 $providerName = CRM_Utils_Array
::value('provider', $providerParams);
62 if (!$providerID && $mailingID) {
63 $providerID = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_Mailing', $mailingID, 'sms_provider_id', 'id');
64 $providerParams['provider_id'] = $providerID;
67 $providerName = CRM_SMS_BAO_Provider
::getProviderInfo($providerID, 'name');
71 CRM_Core_Error
::fatal('Provider not known or not provided.');
74 $providerName = CRM_Utils_Type
::escape($providerName, 'String');
75 $cacheKey = "{$providerName}_" . (int) $providerID . "_" . (int) $mailingID;
77 if (!isset(self
::$_singleton[$cacheKey]) ||
$force) {
78 $ext = CRM_Extension_System
::singleton()->getMapper();
79 if ($ext->isExtensionKey($providerName)) {
80 $paymentClass = $ext->keyToClass($providerName);
81 require_once ("{$paymentClass}.php");
83 CRM_Core_Error
::fatal("Could not locate extension for {$providerName}.");
86 self
::$_singleton[$cacheKey] = $paymentClass::singleton($providerParams, $force);
88 return self
::$_singleton[$cacheKey];
92 * Send an SMS Message via the API Server
95 abstract function send($recipients, $header, $message, $dncID = NULL);
98 * Return message text. Child class could override this function to have better control over the message being sent.
101 public function getMessage($message, $contactID, $contactDetails) {
102 $html = $message->getHTMLBody();
103 $text = $message->getTXTBody();
105 return $html ?
$html : $text;
110 * @param $additionalDetails
114 public function getRecipientDetails($fields, $additionalDetails) {
115 // we could do more altering here
116 $fields['To'] = $fields['phone'];
121 * @param int $apiMsgID
123 * @param array $headers
127 * @return $this|null|object
128 * @throws CRM_Core_Exception
130 public function createActivity($apiMsgID, $message, $headers = array(), $jobID = NULL, $userID = NULL) {
133 SELECT scheduled_id FROM civicrm_mailing m
134 INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
135 $sourceContactID = CRM_Core_DAO
::singleValueQuery($sql, array(1 => array($jobID, 'Integer')));
138 $sourceContactID=$userID;
141 $session = CRM_Core_Session
::singleton();
142 $sourceContactID = $session->get('userID');
145 if (!$sourceContactID) {
146 $sourceContactID = CRM_Utils_Array
::value('Contact', $headers);
148 if (!$sourceContactID) {
152 $activityTypeID = CRM_Core_OptionGroup
::getValue('activity_type', 'SMS delivery', 'name');
153 // note: lets not pass status here, assuming status will be updated by callback
154 $activityParams = array(
155 'source_contact_id' => $sourceContactID,
156 'target_contact_id' => $headers['contact_id'],
157 'activity_type_id' => $activityTypeID,
158 'activity_date_time' => date('YmdHis'),
159 'details' => $message,
160 'result' => $apiMsgID,
162 return CRM_Activity_BAO_Activity
::create($activityParams);
166 * @param string $name
169 * @param null $default
170 * @param string $location
174 public function retrieve($name, $type, $abort = TRUE, $default = NULL, $location = 'REQUEST') {
175 static $store = NULL;
176 $value = CRM_Utils_Request
::retrieve($name, $type, $store,
177 FALSE, $default, $location
179 if ($abort && $value === NULL) {
180 CRM_Core_Error
::debug_log_message("Could not find an entry for $name in $location");
181 echo "Failure: Missing Parameter<p>";
191 * @param int $trackID
193 * @return $this|null|object
194 * @throws CRM_Core_Exception
196 public function processInbound($from, $body, $to = NULL, $trackID = NULL) {
197 $formatFrom = $this->formatPhone($this->stripPhone($from), $like, "like");
198 $escapedFrom = CRM_Utils_Type
::escape($formatFrom, 'String');
199 $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 . '"');
201 if (! $fromContactID) {
202 // unknown mobile sender -- create new contact
203 // use fake @mobile.sms email address for new contact since civi
204 // requires email or name for all contacts
205 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
206 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
207 $phoneloc = array_search('Home', $locationTypes);
208 $phonetype = array_search('Mobile', $phoneTypes);
209 $stripFrom = $this->stripPhone($from);
210 $contactparams = array(
211 'contact_type' => 'Individual',
212 'email' => array(1 => array(
213 'location_type_id' => $phoneloc,
214 'email' => $stripFrom . '@mobile.sms'
216 'phone' => array(1 => array(
217 'phone_type_id' => $phonetype,
218 'location_type_id' => $phoneloc,
219 'phone' => $stripFrom
222 $fromContact = CRM_Contact_BAO_Contact
::create($contactparams, FALSE, TRUE, FALSE);
223 $fromContactID = $fromContact->id
;
227 $to = CRM_Utils_Type
::escape($to, 'String');
228 $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 . '"');
231 $toContactID = $fromContactID;
234 if ($fromContactID) {
235 $actStatusIDs = array_flip(CRM_Core_OptionGroup
::values('activity_status'));
236 $activityTypeID = CRM_Core_OptionGroup
::getValue('activity_type', 'Inbound SMS', 'name');
238 // note: lets not pass status here, assuming status will be updated by callback
239 $activityParams = array(
240 'source_contact_id' => $toContactID,
241 'target_contact_id' => $fromContactID,
242 'activity_type_id' => $activityTypeID,
243 'activity_date_time' => date('YmdHis'),
244 'status_id' => $actStatusIDs['Completed'],
246 'phone_number' => $from
249 $trackID = CRM_Utils_Type
::escape($trackID, 'String');
250 $activityParams['result'] = $trackID;
253 $result = CRM_Activity_BAO_Activity
::create($activityParams);
254 CRM_Core_Error
::debug_log_message("Inbound SMS recorded for cid={$fromContactID}.");
262 * @return mixed|string
264 public function stripPhone($phone) {
265 $newphone = preg_replace('/[^0-9x]/', '', $phone);
266 while (substr($newphone, 0, 1) == "1") {
267 $newphone = substr($newphone, 1);
269 while (strpos($newphone, "xx") !== FALSE) {
270 $newphone = str_replace("xx", "x", $newphone);
272 while (substr($newphone, -1) == "x") {
273 $newphone = substr($newphone, 0, -1);
281 * @param string $format
283 * @return mixed|string
285 public function formatPhone($phone, &$kind, $format = "dash") {
286 $phoneA = explode("x", $phone);
287 switch (strlen($phoneA[0])) {
297 $kind = $phoneA[1] ?
"LocalX" : "Local";
299 $exch = substr($phone, 0, 3);
300 $uniq = substr($phone, 3, 4);
305 $kind = $phoneA[1] ?
"LongX" : "Long";
306 $area = substr($phone, 0, 3);
307 $exch = substr($phone, 3, 3);
308 $uniq = substr($phone, 6, 4);
319 $newphone = '%' . $area . '%' . $exch . '%' . $uniq . '%' . $ext . '%';
320 $newphone = str_replace('%%', '%', $newphone);
321 $newphone = str_replace('%%', '%', $newphone);
325 $newphone = $area . "-" . $exch . "-" . $uniq . " x" . $ext;
326 $newphone = trim(trim(trim($newphone, "x"), "-"));
330 $newphone = $area . $exch . $uniq . "x" . $ext;
331 $newphone = trim(trim(trim($newphone, "x"), "-"));
347 public function urlEncode($values) {
349 foreach ($values as $key => $value) {
350 $value = urlencode($value);
351 $uri .= "&{$key}={$value}";
354 $uri = substr($uri, 1);