forked from gonzalo123/gam-sms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sms.php
244 lines (218 loc) · 6.4 KB
/
Sms.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
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
<?php
/**
* GSM Modem AT Send/receive
* Adapter is loader via dependency injection
*
* THIS PROGRAM COMES WITH ABSOLUTELY NO WARANTIES !
* USE IT AT YOUR OWN RISKS !
*
* @author Gonzalo Ayuso <gonzalo123@gmail.com>
* @copyright under GPL 2 licence
*/
class Sms
{
private $_serial;
private $debug;
protected $_pinOK = false;
protected $openAT = false;
const EXCEPTION_PIN_ERROR = 1;
const EXCEPTION_NO_PIN = 2;
const EXCEPTION_SERVICE_NOT_IMPLEMENTED = 3;
/**
* Factory. Creates new instance. Dependency injections with the type os Modem
* valid serial resources:
* Sms_Serial: GSM modem conected via seria interface
* Sms_Http: GSM modem conected via seria/ethernet converter
* Sms_Dummy: Mock for testing
*
* @param Sms_Interface $serial
* @param Boolean $debug
* @return Sms
*/
public static function factory($serial, $debug=false)
{
if (!($serial instanceof Sms_Serial ||
$serial instanceof Sms_Http ||
$serial instanceof Sms_Dummy
)) {
throw new Exception("NOT IMPLEMENTED", self::EXCEPTION_SERVICE_NOT_IMPLEMENTED);
}
$serial->setValidOutputs(array(
'OK',
'ERROR',
'+CPIN: SIM PIN',
'+CPIN: READY',
'>'
));
return new self($serial, $debug);
}
protected function __construct($serial, $debug=false)
{
$this->_serial = $serial;
$this->_debug = $debug;
}
private function readPort($returnBufffer = false)
{
$out = null;
list($last, $buffer) = $this->_serial->readPort();
if ($returnBufffer) {
$out = $buffer;
} else {
$out = strtoupper($last);
}
if ($this->_debug == true) {
echo $out . "\n";
}
return $out;
}
private function sendMessage($msg)
{
$this->_serial->sendMessage($msg);
}
private function deviceOpen()
{
$this->_serial->deviceOpen();
}
private function deviceClose()
{
$this->_serial->deviceClose();
}
/**
* Delete selected id from SMS SIM
*
* @param unknown_type $id
* @return unknown
*/
public function deleteSms($id)
{
$this->deviceOpen();
$this->sendMessage("AT+CMGD={$id}\r");
$out = $this->readPort();
$this->deviceClose();
if ($out == 'OK') {
return true;
} else {
return false;
}
}
/**
* Sends a SMS to a selected tlfn
* @param Integer $tlfn
* @param String $text
* @return Boolean
*/
public function sendSMS($tlfn, $text)
{
if ($this->_pinOK) {
$text = substr($text, 0, 160);
$this->deviceOpen();
if ($this->openAT === true) {
$this->sendMessage("AT+CMGS=\"{$tlfn}\"\n");
$out = $this->readPort();
if ($out == '>') {
$this->sendMessage("{$text}" . chr(26));
$out = $this->readPort();
} else {
return false;
}
} else {
$this->sendMessage("AT+CMGS=\"{$tlfn}\"\r{$text}" . chr(26));
$out = $this->readPort();
}
$this->deviceClose();
if ($out == 'OK') {
return true;
} else {
return false;
}
} else {
throw new Exception("Please insert the PIN", self::EXCEPTION_NO_PIN);
}
}
public function isPinOk()
{
return $this->_pinOK;
}
/**
* Inserts the pin number.
* first checks if PIN is set. If it's set nothing happens
* @param Integer $pin
* @return Sms
*/
public function insertPin($pin)
{
$this->deviceOpen();
$this->sendMessage("AT+CPIN?\r");
$out = $this->readPort();
$this->deviceClose();
if ($out == "+CPIN: SIM PIN") {
$this->deviceOpen();
if (is_null($pin) || $pin == '') {
throw new Exception("PIN ERROR", self::EXCEPTION_PIN_ERROR);
}
$this->sendMessage("AT+CPIN={$pin}\r");
$out = $this->readPort();
$this->deviceClose();
// I don't know why but I need to wait a few seconds until
// start sending SMS. Only after the first PIN
sleep(20);
}
switch ($out) {
case "+CPIN: READY":
case "OK":
$this->_pinOK = true;
break;
}
if ($this->_pinOK === true) {
return $this;
} else {
throw new Exception("PIN ERROR ({$out})", self::EXCEPTION_PIN_ERROR);
}
}
const ALL = "ALL";
const UNREAD = "REC UNREAD";
/**
* Read Inbox
*
* @param String $mode ALL | UNREAD
* @return Array
*/
public function readInbox($mode=self::ALL)
{
$inbox = $return = array();
if ($this->_pinOK) {
$this->deviceOpen();
$this->sendMessage("AT+CMGF=1\r");
$out = $this->readPort();
if ($out == 'OK') {
$this->sendMessage("AT+CMGL=\"{$mode}\"\r");
$inbox = $this->readPort(true);
}
$this->deviceClose();
if (count($inbox) > 2) {
array_pop($inbox);
array_pop($inbox);
$arr = explode("+CMGL:", implode("\n", $inbox));
for ($i = 1; $i < count($arr); $i++) {
$arrItem = explode("\n", $arr[$i], 2);
// Header
$headArr = explode(",", $arrItem[0]);
$fromTlfn = str_replace('"', null, $headArr[2]);
$id = $headArr[0];
$date = $headArr[4];
$hour = $headArr[5];
// txt
$txt = $arrItem[1];
$return[] = array('id' => $id, 'tlfn' => $fromTlfn, 'msg' => $txt, 'date' => $date, 'hour' => $hour);
}
}
return $return;
} else {
throw new Exception("Please insert the PIN", self::EXCEPTION_NO_PIN);
}
}
public function setOpenAT($openAT)
{
$this->openAT = $openAT;
}
}