-
Notifications
You must be signed in to change notification settings - Fork 0
/
org_ndi_sms_telerivet.php
134 lines (122 loc) · 4.74 KB
/
org_ndi_sms_telerivet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.2 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2012 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
// Load the official Telerivet library
require_once 'lib/telerivet-php-client/telerivet.php';
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2012
* $Id$
*
*/
class org_ndi_sms_telerivet extends CRM_SMS_Provider {
/**
* provider details
* @var string
*/
protected $_providerInfo = array();
/**
* We only need one instance of this object. So we use the singleton
* pattern and cache the instance in this variable
*
* @var object
* @static
*/
static private $_singleton = array();
/**
* Constructor
*
* Create and auth a Telerivet session.
* This is not needed for Telerivet
*
* @return void
*/
function __construct($provider, $skipAuth = TRUE) {
// Instantiate the Telerivet client
$this->provider = $provider;
if(isset($provider['api_params'])){
$api = new Telerivet_API($provider['api_params']['api_key']);
$this->tr = $api->initProjectById($provider['api_params']['project_id']);
}
}
/**
* singleton function used to manage this object
*
* @return object
* @static
*
*/
static function &singleton($providerParams = array(), $force = FALSE) {
if(isset($providerParams['provider'])){
$providers = CRM_SMS_BAO_Provider::getProviders(NULL, array('name' => $providerParams['provider']));
$providerID = current($providers)['id'];
}else{
$providerID = CRM_Utils_Array::value('provider_id', $providerParams);
}
$skipAuth = $providerID ? FALSE : TRUE;
$cacheKey = (int) $providerID;
if (!isset(self::$_singleton[$cacheKey]) || $force) {
$provider = array();
if ($providerID) {
$provider = CRM_SMS_BAO_Provider::getProviderInfo($providerID);
}
self::$_singleton[$cacheKey] = new org_ndi_sms_telerivet($provider, $skipAuth);
}
return self::$_singleton[$cacheKey];
}
/**
* Send an SMS Message via the Telerivet API Server
*
* @param array the message with a to/from/text
*
* @return mixed SID on success or PEAR_Error object
* @access public
*/
function send($recipients, $header, $message, $jobID = NULL, $userID = NULL) {
try{
$message = $this->tr->sendMessage(array( 'content' => $message, 'to_number' => $header['To'] ));
}catch(Exception $e) {
return PEAR::raiseError( $e->getMessage(), $e->getCode(), PEAR_ERROR_RETURN );
}
$this->createActivity($message->id, $message, $header, $jobID, $userID);
return $message->id;
}
function callback() {
// For some reason, when status is defined in the request, the callback will
// call this function. Telerivet appears to be returning status = processing
// as part of its post data so it seems like the right thing to do in any
// case is call inbound.
return $this->inbound();
}
function inbound() {
if(CRM_Utils_Request::retrieve('secret', 'String') != $this->provider['api_params']['secret']){
CRM_Core_Error::debug_log_message("Bad secret.");
return;
};
return parent::processInbound( $this->retrieve('from_number', 'String'), $this->retrieve('content', 'String'), NULL, $this->retrieve('id', 'String') );
}
}