forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSMSSyncController.php
More file actions
124 lines (104 loc) · 3.64 KB
/
Copy pathSMSSyncController.php
File metadata and controls
124 lines (104 loc) · 3.64 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
<?php
namespace Ushahidi\App\DataSource\SMSSync;
/**
* SMS Sync Callback controller
*
* @author Ushahidi Team <team@ushahidi.com>
* @package DataSource\SMSSync
* @copyright 2013 Ushahidi
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License Version 3 (GPLv3)
*/
use Ushahidi\App\DataSource\DataSourceController;
use Illuminate\Http\Request;
use Ushahidi\App\DataSource\Message\Type as MessageType;
use Ushahidi\App\DataSource\Message\Status as MessageStatus;
use Ushahidi\Core\Entity\Contact;
class SMSSyncController extends DataSourceController
{
protected $source = 'smssync';
public function handleRequest(Request $request)
{
// Authenticate the request
if (!$this->source->verifySecret($request->input('secret'))) {
return response(['payload' => [
'success' => false,
'error' => 'Incorrect or missing secret key'
]], 403);
}
// Process incoming messages from SMSSync only if the request is POST
if ($request->method() == 'POST') {
return $this->incoming($request);
}
// Attempt Task if request is GET and task type is 'send'
if ($request->method() == 'GET' and $request->input('task') == 'send') {
return $this->task($request);
}
// Set the response
return ['payload' => [
'success' => true,
'error' => null
]];
}
/**
* Process messages received from SMSSync
*/
private function incoming($request)
{
$from = $request->input('from');
if (empty($from)) {
return response(['payload' => [
'success' => false,
'error' => 'Missing from value'
]], 400);
}
$message_text = $request->input('message');
if (empty($message_text)) {
return response(['payload' => [
'success' => false,
'error' => 'Missing message'
]], 400);
}
// Remove Non-Numeric characters because that's what the DB has
$to = preg_replace("/[^0-9,+.]/", "", $request->input('sent_to'));
// Allow for Alphanumeric sender
$from = preg_replace("/[^0-9A-Za-z+ ]/", "", $from);
$date = $request->input('sent_timestamp') / 1000;
$this->save([
'type' => MessageType::SMS,
'from' => $from,
'contact_type' => Contact::PHONE,
'message' => $message_text,
'title' => null,
'datetime' => date_create_from_format('U', $date),
'data_source' => 'smssync'
]);
return ['payload' => [
'success' => true,
'error' => null
]];
}
/**
* Implement SMSSync task feature to allow SMSSync to send messages as SMS
* to users.
*/
private function task($request)
{
// Do we have any tasks for SMSSync?
// Grab messages to send, 20 at a time.
$messages = $this->getPendingMessages(20);
return ['payload' => [
'task' => "send",
'success' => true,
'secret' => $this->source->getSecret(),
'messages' => array_values(array_map(function ($message) {
// Reformat message for SMSSYnc
return [
'to' => $message->contact,
'message' => $message->message,
'message_id' => $message->id,
'uuid' => $message->data_source_message_id
];
}, $messages)),
]];
}
}