This repository has been archived by the owner on Nov 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsms_panel.module
218 lines (195 loc) · 5.26 KB
/
sms_panel.module
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
// $Id$
/**
* @file
* SMS Panel hooks implementations and helper API functions.
*/
/**
* Module constants.
* Defines placeholders for SMS message direction.
*/
define('SMS_PANEL_IN', 0);
define('SMS_PANEL_OUT', 1);
/**
* Implements hook_perm().
*/
function sms_panel_perm() {
return array(
'administer sms panel',
'send sms message',
'view sms message',
'delete sms message',
'view outgoing message list',
'view incoming message list',
);
}
/**
* Implements hook_sms_send_alter() to track the outgoing SMS.
*
* This is a temporary implementation since
* we're using a patched version of the SMSFramework and
* it's not available in its current 1.x branch.
*
* @param $outgoing
* An array of following data passed by reference:
* - number: Phone number of recipient.
* - message: SMS text body.
* - response: The response of gateway's send callback.
* @param $op
* The operation type.
*/
function sms_panel_sms_send_alter(&$outgoing, $op) {
if ($op == 'pre send' && strpos($outgoing['message'], t('confirmation')) === FALSE && !user_access('send sms message')) {
drupal_set_message(t('You have not enough permission to send SMS messages.'), 'error', FALSE);
// TODO: Just change the following lines!
drupal_goto($_SERVER['HTTP_REFERER']);
exit();
}
elseif ($op == 'post send' && $outgoing['response']) {
global $user;
$gateway = sms_default_gateway();
// Build and save the SMS object.
$sms = new stdClass();
$sms->uid = $user->uid;
$sms->timestamp = time();
$sms->direction = SMS_PANEL_OUT;
$sms->number = $outgoing['number'];
$sms->message = $outgoing['message'];
$sms->gateway = $gateway['identifier'];
sms_save($sms);
}
}
/**
* Implements hook_sms_incoming().
*/
function sms_panel_sms_incoming($op, $number, $message, $options) {
if ($op == 'post process') {
$sms = new stdClass();
$gateway = sms_default_gateway();
$sms->uid = 0;
$sms->number = $number;
$sms->timestamp = time();
$sms->message = $message;
$sms->direction = SMS_PANEL_IN;
$sms->gateway = $gateway['identifier'];
sms_save($sms);
}
}
/**
* API function to simply save a sms message.
*
* @param $sms
* SMS object to be saved.
*/
function sms_save(&$sms) {
$sms = (object) $sms;
$sms->timestamp = $sms->timestamp ? $sms->timestamp : time();
drupal_write_record('smses', $sms);
}
/**
* API function to delete a sms message.
*
* @param $sid
* SMS object or identifier to be deleted.
*/
function sms_delete($sid) {
global $user;
if (user_access('delete sms message')) {
$sid = is_object($sid) ? $sid->sid : $sid;
db_query("DELETE FROM {smses} WHERE sid = %d", $sid);
drupal_set_message(t('The SMS message logs has been successfully deleted.'), 'status', FALSE);
}
else {
drupal_set_message(t('You have not enough permission to delete the SMS message.'), 'warning', FALSE);
}
}
/**
* API function to load a sms message from database.
*
* @param $params
* The sid of the SMS message or an array of conditions to match against.
* @param $flush
* Whether to flush the internal cache.
* @param $fields
* An array of database field names to retrieve.
*
* @return
* The loaded SMS object or an array of SMS objects or FALSE on fail.
*
* @todo
* Extend to be able to match other conditions.
*/
function sms_load($params = array(), $fields = array(), $flush = FALSE) {
static $cache = array();
// Flush the internal cache, if set so.
if ($flush) {
$cache = array();
}
// Check for cache availablity or build the query.
$arguments = array();
if (is_numeric($params)) {
if (isset($cache[$params])) {
return (object) $cache[$params];
}
$conditions = 'sid = %d';
$arguments[] = $params;
}
elseif (is_array($params)) {
// Convert array formatted parameters into a query.
foreach ($params as $key => $value) {
if ($key == 'period' && isset($value['from'])) {
$conditions[] = '(timestamp BETWEEN %d AND %d)';
$arguments[] = $value['from'];
$arguments[] = isset($value['to']) ? $value['to'] : time();
}
else {
$conditions[] = db_escape_table($key) . " = '%s'";
$arguments[] = $value;
}
}
$conditions = implode(' AND ', $conditions);
}
else {
return FALSE;
}
// Preparing fields to be retrieved.
if (empty($fields) || !is_array($fields)) {
$fields = '*';
}
else {
$fields = implode(',', $fields);
}
// Get and iterate through the results.
$sms = array();
$conditions = $conditions ? $conditions : 'TRUE';
// Just ignore that coder critical warning ;)
$results = db_query("SELECT $fields FROM {smses} WHERE $conditions", $arguments);
while ($sms_object = db_fetch_object($results)) {
$sms[] = $sms_object;
}
// Remove the array wrapper, if there were only one result.
$count = count($sms);
if ($count == 1) {
$sms = $sms[0];
}
elseif (!$count) {
return FALSE;
}
$cache[$sms->sid] = $sms;
return $sms;
}
/**
* Helper function to build a title for a SMS message.
*
* @param $sid
* SMS identifier or object.
*
* @return
* SMS message title.
*/
function sms_title($sid) {
if (is_object($sid)) {
$sid = $sid->sid;
}
return t('SMS Message') . ' #' . $sid;
}