-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbroadcast.php
More file actions
535 lines (469 loc) · 15.9 KB
/
Copy pathbroadcast.php
File metadata and controls
535 lines (469 loc) · 15.9 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<?php
/**
* @file
* Tools to send a mass text, and import, remove and list numbers.
*
*/
require_once 'config.php';
require_once $LIB_BASE . 'lib_sms.php';
// required to avoid output buffering problems when sending progress marks
// as texts are sent
header('Content-type: text/html; charset=utf-8');
include 'header.php';
// URL parameters
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
$text = isset($_REQUEST['text']) ? trim($_REQUEST['text']) : '';
$include_tags = isset($_REQUEST['include_tags']) ? $_REQUEST['include_tags'] : array();
$tag_names = isset($_REQUEST['tag_names']) ? $_REQUEST['tag_names'] : array();
$request_response = isset($_REQUEST['response']) ? trim($_REQUEST['response']) : '';
$confirmed = isset($_REQUEST['confirm']) ? $_REQUEST['confirm'] : '';
$communications_id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
// initialize variables
$broadcast_tags = array();
// Create an array of selected tags from the $tag_names and $include_tags parameters
$tags_selected = array();
foreach ($include_tags as $tag_number => $include_tag) {
if ($include_tag == 'on') {
$tags_selected[] = $tag_names[$tag_number];
}
}
// Authorized user?
$authorized = empty($BROADCAST_AUTHORIZED_USERS) ||
in_array($_SERVER['PHP_AUTH_USER'], $BROADCAST_AUTHORIZED_USERS);
if (!$authorized) {
// no
$error = "You are not authorized to send broadcast texts.";
}
// *** ACTIONS ***
// send a text message?
if ($action == 'broadcast' && $authorized) {
if ($confirmed == 'on') {
// confirmed, send the broadcast
if (sendBroadcastText($text, $request_response, $tags_selected, $error, $success)) {
$text = '';
$request_response = '';
}
} else {
// checkbox wasn't checked
$error = "Please check the confirmation checkbox.";
}
}
// send a text message to those who responded to a previous broadcast?
if ($action == 'broadcast_response' && $authorized) {
if (sendBroadcastResponseText($text, $communications_id, $error, $success)) {
$text = '';
}
}
// close the broadcast update?
if ($action == 'broadcast_close' && $authorized) {
if ($confirmed == 'on') {
// confirmed, close the broadcast
if (sms_closeBroadcastResponse($error)) {
$success = "The broadcast update was closed.";
}
} else {
// checkbox wasn't checked
$error = "Please check the confirmation checkbox.";
}
}
// load the tags
loadBroadcastTags($broadcast_tags, $error);
// any error message?
if (!empty($error)) {
?>
<div class="alert alert-danger" role="alert"><?php echo $error ?></div>
<?php
}
// any success message?
if (!empty($success)) {
?>
<div class="alert alert-success" role="alert"><?php echo $success ?></div>
<?php
}
// is a broadcast in progress?
if (sms_isBroadcastInProgress($remaining, $error)) {
?>
<div class="alert alert-info" role="alert">Broadcasts in progress (approx. <b><?php echo $remaining ?></b> remaining)</div>
<?php
}
// display the broadcast information
?>
<script type="text/javascript">
var broadcast_count = 0;
// calculate the number of characters and cost to send this text
function showTextLength() {
var count = $('#text_entry').val().trim().length;
var response_count = $('#request_response').val().trim().length;
if (response_count) {
count += response_count + 1;
}
var sms_count = Math.floor(count / 160) + 1;
var cost = sms_count * broadcast_count * <?php echo $TWILIO_COST_PER_TEXT ?>;
if (count == 0) {
$('#text_entry_length').text('(to ' + broadcast_count + ' numbers, 0 characters)');
} else {
$('#text_entry_length').text('(to ' + broadcast_count + ' numbers, ' + count + ' characters, $' + cost.toFixed(2) + ')');
}
}
// load the count of numbers to send to, which changes depending on which tags are checked
function updateBroadcastCount() {
$.post("broadcast_count.php", $( "#broadcast" ).serialize(),
function(data) {
// the count is returned
broadcast_count = parseInt(data);
showTextLength();
}
);
}
</script>
<h2 class="sub-header">Broadcast</h2>
<ul class="nav nav-pills">
<li role="presentation" class="active"><a href="broadcast.php">Send</a></li>
<li role="presentation"><a href="broadcast_admin.php">Import & Remove</a></li>
<li role="presentation"><a href="broadcast_admin.php?action=list">List</a></li>
<li role="presentation"><a href="log.php?ph=all_broadcast">Log</a></li>
</ul>
<br />
<form id="broadcast" action="broadcast.php" method="POST">
<input type="hidden" name="action" value="broadcast">
<div class="form-group">
<label for="text_entry">Send a new broadcast text message:</label>
<textarea class="form-control" rows="3" name="text" maxLength="1600"
id="text_entry" onKeyUp="showTextLength();" onKeyDown="showTextLength();"
placeholder="Text message"><?php echo $text ?></textarea>
<p class="help-block" id="text_entry_length"> </p>
</div>
<div class="form-group">
<label>Limit to tags:</label><br>
<?php
if (count($broadcast_tags) > 0) {
foreach ($broadcast_tags as $tag => $tag_data) {
?>
<label class="checkbox-inline">
<input type="hidden" name="tag_names[<?php echo $tag_data['id'] ?>]" value="<?php echo addslashes($tag) ?>">
<input type="checkbox" name="include_tags[<?php echo $tag_data['id'] ?>]" onClick="updateBroadcastCount();" <?php if ($include_tags[$tag_data['id']] == 'on') { echo " checked"; } ?>>
<span class="label label-primary"><?php echo $tag ?></span>
<span class="badge"><?php echo $tag_data['count'] ?></span>
</label>
<?php
}
} else {
echo "(no tags)";
}
?>
</div>
<div class="form-group">
<label for="request_response">Response requested?</label>
<input type="text" class="form-control" name="response" maxLength="1600"
id="request_response" onKeyUp="showTextLength();" onKeyDown="showTextLength();"
placeholder="Reply yes if you can participate"
value="<?php echo $request_response ?>">
<p class="help-block">
If set, if they reply 'yes' they'll get future texts for this broadcast. Make sure
to include the instruction to reply yes!
</p>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="confirm"> Confirm sending this broadcast
</label>
</div>
<button class="btn btn-success" id="button-text">Send broadcast</button>
</form>
<?php
// load the latest broadcast that requested a response
sms_getBroadcastResponse($broadcast_response, $error);
if ($broadcast_response) {
// get the list of people who have responded 'yes'
getBroadcastResponseConfirmed($broadcast_response['id'], $broadcast_response_confirmed, $error);
?>
<hr />
<h3>Send update</h3>
<p><b>For:</b> "<?php echo $broadcast_response['body'] ?>" (sent
<?php echo date("m/d/y h:i a", strtotime($broadcast_response['communication_time'])); ?>)</p>
<form id="broadcast-response" action="broadcast.php" method="POST">
<input type="hidden" name="action" value="broadcast_response">
<input type="hidden" name="id" value="<?php echo $broadcast_response['id'] ?>">
<div class="form-group">
<label for="broadcast_text_entry"><?php echo count($broadcast_response_confirmed) ?>
<?php echo (count($broadcast_response_confirmed) == 1) ? 'person has' : 'people have' ?> replied.
Send them a text:</label>
<textarea class="form-control" rows="3" name="text" maxLength="1600"
id="broadcast_text_entry"
placeholder="Text message"><?php echo $text ?></textarea>
</div>
<button class="btn btn-success" id="button-text">Send update</button>
</form>
<br />
<h4>Responders:</h4>
<p><?php echo implode(', ', $broadcast_response_confirmed) ?></p>
<h3>Close broadcast</h3>
<form id="broadcast-close" action="broadcast.php" method="POST">
<input type="hidden" name="action" value="broadcast_close">
<div class="checkbox">
<label>
<input type="checkbox" name="confirm"> Confirm closing this broadcast
</label>
</div>
<button class="btn btn-danger" id="button-text">Close broadcast</button>
</form>
<?php
}
// display the footer
include 'footer.php';
?>
<script type="text/javascript">
// load the broadcast count - placed at the end after jquery is loaded
updateBroadcastCount();
</script>
<?php
/**
* Send a broadcast text.
*
* Sends a text to all active numbers in the broadcast list.
*
* @param string $text
* The text to send.
* @param string $request_response
* The additional text requesting a response if they want to participate further.
* @param array $tags
* The tags to limit the broadcast to, if any are set.
* @param string &$error
* An error if one occurred.
* @param string &$message
* An informational message if appropriate.
*
* @return bool
* True if sent.
*/
function sendBroadcastText($text, $request_response, $tags, &$error, &$message)
{
global $BROADCAST_CALLER_IDS, $BROADCAST_PROGRESS_MARK_EVERY, $BROADCAST_TWILIO_NOTIFY_SERVICE,
$BROADCAST_LIMITED_TO_TAGS_TEXT;
$error = '';
$message = '';
$tags_description = '';
// is there a response request?
if (trim($request_response)) {
// trim it, and put a space between the text and the response request
$request_response = ' ' . trim($request_response);
} else {
// it's empty, or just whitespace
$request_response = '';
}
$text = trim($text) . $request_response;
if (!$text) {
$error = "No text was provided.";
return false;
}
// limited to specific tags?
if (count($tags)) {
$tags_description = " ({$BROADCAST_LIMITED_TO_TAGS_TEXT}: " . implode(', ', $tags) . ")";
}
// confirm that the text is not identical to the previous broadcast text sent
$sql = "SELECT * FROM communications WHERE phone_to LIKE 'BROADCAST%' ".
"ORDER BY communication_time DESC LIMIT 1";
if (!db_db_getrow($sql, $last_broadcast, $error)) {
return false;
}
if ($last_broadcast['body'] == ($text . $tags_description)) {
// it is identical!
$error = "This text is identical to the last one sent - aborting!";
return false;
}
// create the SQL for the tags
$sql_tags = '';
foreach ($tags as $tag) {
$sql_tags[] = "tag='".addslashes($tag)."'";
}
// load the numbers to broadcast to, limited by tags if any tags are set
$sql = "SELECT DISTINCT phone FROM broadcast ".
(count($tags)
? ("LEFT JOIN broadcast_tags ON broadcast.id = broadcast_tags.broadcast_id ".
"WHERE status='active' AND (".implode(' OR ', $sql_tags).")")
: "WHERE status='active'");
if (!db_db_getcol($sql, $numbers, $error)) {
return false;
}
if (count($numbers) == 0) {
$error = "No numbers to send to.";
return false;
}
// calculate how many texts will be sent, divide it by the number
// of broadcast numbers, and store that in the MessageSid (twilio_sid)
// field, which is not otherwise used
$count_per_number = floor(strlen($text) / 160) + 1;
$seconds_total = ceil(($count_per_number * count($numbers)) / count($BROADCAST_CALLER_IDS));
// store the text
$data = array(
'From' => reset($BROADCAST_CALLER_IDS),
'To' => ($request_response ? 'BROADCAST_RESPONSE' : 'BROADCAST'),
'Body' => $text . $tags_description,
'MessageSid' => 'text ' . $seconds_total
);
sms_storeCallData($data, $error);
// send via Twilio notify?
if ($BROADCAST_TWILIO_NOTIFY_SERVICE) {
// yes
if (!sms_sendViaNotify($numbers, $text, $error)) {
return false;
}
} else {
// no, send the texts one by one
if (!sms_send($numbers, $text, $error, $BROADCAST_CALLER_IDS,
$BROADCAST_PROGRESS_MARK_EVERY)) {
return false;
}
}
$message = "Text sent to " . count($numbers) . " numbers" .
($error ? ' (except errors listed above).' : '.');
return true;
}
/**
* Send a text to those who responded to a previous broadcast
*
* ...
*
* @param string $text
* The text to send.
* @param int $communications_id
* The original broadcast text id
* @param string &$error
* An error if one occurred.
* @param string &$message
* An informational message if appropriate.
*
* @return bool
* True if sent.
*/
function sendBroadcastResponseText($text, $communications_id, &$error, &$message)
{
global $BROADCAST_CALLER_IDS, $BROADCAST_TWILIO_NOTIFY_SERVICE, $BROADCAST_PROGRESS_MARK_EVERY;
$error = '';
$message = '';
$text = trim($text);
if (!$text) {
$error = "No text was provided.";
return false;
}
// make sure the broadcast response hasn't been closed in another window
if (!sms_getBroadcastResponse($broadcast_response, $error)) {
return false;
}
if (empty($broadcast_response)) {
// it has been closed
$error = "Failed to send the text. The broadcast response has been closed.";
return false;
}
// confirm that the text is not identical to the previous broadcast text sent
$sql = "SELECT * FROM communications WHERE phone_to LIKE 'BROADCAST%' ".
"ORDER BY communication_time DESC LIMIT 1";
if (!db_db_getrow($sql, $last_broadcast, $error)) {
return false;
}
if ($last_broadcast['body'] == $text) {
// it is identical!
$error = "This text is identical to the last one sent - aborting!";
return false;
}
// get the list of people who have responded 'yes'
if (!getBroadcastResponseConfirmed($communications_id, $numbers, $error)) {
return false;
}
if (count($numbers) == 0) {
$error = "No numbers to send to.";
return false;
}
// calculate how many texts will be sent, divide it by the number
// of broadcast numbers, and store that in the MessageSid (twilio_sid)
// field, which is not otherwise used
$count_per_number = floor(strlen($text) / 160) + 1;
$seconds_total = ceil(($count_per_number * count($numbers)) / count($BROADCAST_CALLER_IDS));
// store the text
$data = array(
'From' => reset($BROADCAST_CALLER_IDS),
'To' => 'BROADCAST_RESPONSE_UPDATE',
'Body' => $text,
'MessageSid' => 'text ' . $seconds_total
);
sms_storeCallData($data, $error);
// send via Twilio notify?
if ($BROADCAST_TWILIO_NOTIFY_SERVICE) {
// yes
if (!sms_sendViaNotify($numbers, $text, $error)) {
return false;
}
} else {
// no, send the texts one by one
if (!sms_send($numbers, $text, $error, $BROADCAST_CALLER_IDS,
$BROADCAST_PROGRESS_MARK_EVERY)) {
return false;
}
}
$message = "Text sent to " . count($numbers) . " numbers.";
return true;
}
/**
* Get the list of people who have responded to a particular broadcast
*
* ...
*
* @param int $communications_id
* The broadcast people have responded to
* @param array &$numbers
* Set to the list of numbers of people who have responded
* @param string &$error
* An error if one occurred.
*
* @return bool
* True unless an error occurred.
*/
function getBroadcastResponseConfirmed($communications_id, &$numbers, &$error)
{
// get the list of people who have responded 'yes'
$sql = "SELECT broadcast.phone FROM broadcast_responses ".
"LEFT JOIN broadcast ON broadcast.id = broadcast_responses.broadcast_id ".
"WHERE communications_id='".addslashes($communications_id)."' AND ".
" broadcast.status='active' ".
"ORDER BY broadcast.phone";
if (!db_db_getcol($sql, $numbers, $error)) {
return false;
}
return true;
}
/**
* Load tags and the count of numbers for each
*
* ...
*
* @param array &$broadcast_tags
* Set to an array - keys are the tags, values are the count for each tag
* @param string &$error
* An error if one occurred.
*
* @return bool
* True unless an error occurred.
*/
function loadBroadcastTags(&$broadcast_tags, &$error)
{
$sql = "SELECT DISTINCT tag FROM broadcast_tags ORDER BY tag";
if (!db_db_getcol($sql, $tags, $error)) {
return false;
}
$tag_id = 0;
$broadcast_tags = array();
foreach ($tags as $tag) {
$sql = "SELECT COUNT(*) FROM broadcast_tags ".
"LEFT JOIN broadcast ON broadcast.id = broadcast_tags.broadcast_id ".
"WHERE broadcast_tags.tag='".addslashes($tag)."' AND broadcast.status='active'";
if (!db_db_getone($sql, $tag_count, $error)) {
return false;
}
$broadcast_tags[$tag] = array(
'id' => $tag_id++,
'count' => $tag_count
);
}
return true;
}
?>