-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathio_3sd_dummysms.php
119 lines (105 loc) · 3.34 KB
/
io_3sd_dummysms.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
<?php
/**
* Class CRM_SMS_Provider_Dummysms
*/
class io_3sd_dummysms extends CRM_SMS_Provider {
/**
* provider details
* @var string
*/
protected $_providerInfo = array();
protected $_id = 0;
const MESSAGE_DIRECTION_OUTBOUND = 1;
const MESSAGE_DIRECTION_INBOUND = 2;
/**
* 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
* @return void
*/
function __construct($provider, $skipAuth = TRUE) {
// Instantiate the dummysms client
$this->provider = $provider;
}
/**
* 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 = empty($providers) ? 0 : 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 io_3sd_dummysms($provider, $skipAuth);
}
return self::$_singleton[$cacheKey];
}
/**
* Send an SMS Message to a log file
*
* @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) {
$id = date('YmdHis');
try {
self::logToFile($id, $header['To'], $message, self::MESSAGE_DIRECTION_OUTBOUND);
$this->createActivity($id, $message, $header, $jobID, $userID);
} catch(Exception $e) {
return PEAR::raiseError( $e->getMessage(), $e->getCode(), PEAR_ERROR_RETURN );
}
return $id;
}
/**
* Write the given SMS message to the sms log file.
*
* @param string $id Unique (in the scope of the log file0 ID for this message.
* @param string $number Mobile phone number for the recipient (if outbound)
* or sender (if inbound)
* @param string $message The message content
* @param int Whether the message is inbound (self::MESSAGE_DIRECTION_INBOUND)
* or outbound (self::MESSAGE_DIRECTION_OUTBOUND)
*/
function logToFile($id, $number, $message, $direction_id) {
$config = CRM_Core_Config::singleton();
if (!empty($config->configAndLogDir)) {
switch ($direction_id) {
case self::MESSAGE_DIRECTION_INBOUND:
$direction_label = "from";
break;
case self::MESSAGE_DIRECTION_OUTBOUND:
$direction_label = "to";
break;
}
$file = $config->configAndLogDir . "/sms_out.log";
$line = "{$id}: {$direction_label}_number: {$number}; {$message}" . PHP_EOL;
file_put_contents($file, $line, FILE_APPEND | LOCK_EX);
}
}
function inbound($from_number, $content, $id=NULL) {
if (!isset($id)) {
$id = date('YmdHis');
}
return parent::processInbound( $from_number, $content, NULL, $id );
}
}