sms functions testing
[namibia] / module / Utility / src / Utility / Comms / Sms.php
1 <?php
2 namespace Utility\Comms;
3
4
5 /**
6  * This class facilitates basic sms sending.
7  * @author Andre Fourie
8  */
9 class Sms
10 {
11     #-> Error Messages.
12     const ERROR_ADDRESS     = 'Utility\Comms\Sms, invalid Address supplied.';
13
14                 #-> Status codes.
15                 static private $statusCode      = array(
16                                 '001' => 'Message unknown',
17                                 '002' => 'Message queued',
18                                 '003' => 'Delivered to gateway',
19                                 '004' => 'Received by recipient',
20                                 '005' => 'Error with message',
21                                 '006' => 'User cancelled message delivery',
22                                 '007' => 'Error delivering message',
23                                 '008' => 'OK',
24                                 '009' => 'Routing error',
25                                 '010' => 'Message expired',
26                                 '011' => 'Message queued for later delivery',
27                                 '012' => 'Out of credit',
28                                 '014' => 'Maximum MT limit exceeded'
29                                 );
30
31     #-> Params
32     private $sBody          = '';
33     private $sTo            = '';
34     private $sCc            = '';
35     private $sBcc           = '';
36     private $sFrom          = '';
37     private $sSubject       = '';
38     private $sSmscId        = '';
39
40
41
42     /* ---------------------------------------------------------------------------------------- */
43     #-> Summoning.
44
45     public function __construct() {}
46
47
48
49     /* ---------------------------------------------------------------------------------------- */
50     #-> Private Functions
51
52     private function checkNumericNotZero($val)
53     {
54         return is_numeric($val) && 0 != $val;
55     }
56
57
58     /* ---------------------------------------------------------------------------------------- */
59     #-> Public Functions - Util.
60
61     /**
62      * Swap sms status code for short status text.
63      * @param  string $code
64      * @return string
65      */
66     static public function getStatusText($code)
67     {
68         $code = str_pad($code, 3, '0', STR_PAD_LEFT);
69         return isset(self::$statusCode[$code])
70                 ? self::$statusCode[$code]
71                 : 'Unknown message status';
72     }
73
74
75     /* ---------------------------------------------------------------------------------------- */
76     #-> Public Functions - Sending.
77
78     /**
79      * Set from.
80      * @param string $sFrom
81      * @return boolean
82      */
83     public function setFrom($sFrom)
84     {
85         if ($sFrom)
86         {
87             $this->sFrom = $sFrom;
88             return true;
89         }
90         return false;
91     }
92
93     /**
94      * Set to.
95      * @param integer|array $mTo
96      * @return boolean
97      */
98     public function setTo($mTo)
99     {
100         #-> Add to list
101         if (!is_array($mTo) && is_numeric($mTo))
102         {
103             $this->sTo .= empty($this->sTo)
104                 ? $mTo
105                 : ',' . $mTo;
106         }
107         if (is_array($mTo) && !empty($mTo))
108         {
109             foreach ($mTo as $mAddress)
110             {
111                 if (is_numeric($mAddress))
112                 {
113                     $this->sTo .= empty($this->sTo)
114                         ? $mAddress
115                         : ',' . $mAddress;
116                 }
117             }
118             return true;
119         }
120         return false;
121     }
122
123     /**
124      * Set to without concat.
125      * @param string $sTo
126      * @return boolean
127      */
128     public function setToNoConcat($sTo)
129     {
130         #-> Add to list
131         if (!empty($sTo))
132         {
133             $this->sTo = $sTo;
134             return true;
135         }
136         return false;
137     }
138
139     /**
140      * Set cc.
141      * @param multi $mCc
142      * @return boolean
143      */
144     public function setCc($mCc)
145     {
146         return $this->SetTo($mCc);
147     }
148
149     /**
150      * Set bcc.
151      * @param multi $mBcc
152      * @return boolean
153      */
154     public function setBcc($mBcc)
155     {
156         return $this->SetTo($mBcc);
157     }
158
159     /**
160      * Set subject.
161      * @param string $sSubject
162      * @return boolean
163      */
164     public function setSubject($sSubject = 0)
165     {
166         if ($sSubject)
167         {
168             $this->sSubject = $sSubject;
169             return true;
170         }
171         return false;
172     }
173
174     /**
175      * Set body.
176      * @param string $sBody
177      * @return boolean
178      */
179     public function setBody($sBody)
180     {
181         if ($sBody)
182         {
183             $this->sBody = $sBody;
184             return true;
185         }
186         return false;
187     }
188
189     /**
190      * Set smsc id.
191      * @param string $sSmscId
192      * @return boolean
193      */
194     public function setSmscId($sSmscId)
195     {
196         if ($sSmscId)
197         {
198             $this->sSmscId = $sSmscId;
199             return true;
200         }
201         return false;
202     }
203
204     /**
205      * Set context parameters from array.
206      * @param array $aContext
207      * @return boolean
208      */
209     public function setContext(array $aContext = array())
210     {
211         #-> Check context.
212         foreach ($aContext as $sParam => $mValue)
213         {
214             switch ($sParam)
215             {
216                 case 'To':
217                 case 'Cc':
218                 case 'Bcc':
219                     $this->setTo($mValue);
220                     break;
221                 case 'From':
222                     $this->setFrom($mValue);
223                     break;
224                 case 'Subject':
225                 case 'Body':
226                     $this->sBody .= empty($this->sBody)
227                         ? $mValue
228                         : "\n" . $mValue;
229                     break;
230             }
231         }
232
233     }
234
235     /**
236      * Send the sms.
237      * @param array $aContext
238      * @return boolean
239      */
240     public function send(array $aContext = array())
241     {
242         #-> Config.
243         $config = \Utility\Registry::getConfigParam('SMS');
244
245         #-> Check context.
246         empty($aContext)
247             || $this->setContext($aContext);
248
249         #-> Environmental override.
250         if (IS_DEV_ENV && !IS_STAGE_ENV)
251         {
252             \Utility\Debug::errorLog("overriding","sms");
253                 \Utility\Debug::errorLog('Sms.send Override: ' . $this->sTo, $this->sBody);
254                 return;
255         }
256
257         #-> Send using api.
258         $sUrl = "https://api.clickatell.com/http/sendmsg.php"
259               . "?user=" . $config['Username']
260               . "&api_id=" . $config['ApiId']
261               . "&password=" . $config['Password']
262               . "&concat=4"
263               . "&callback=2"
264               . "&to=$this->sTo"
265               . "&text=".substr(urlencode($this->sBody),0,620);
266
267         \Utility\Debug::errorLog("sms url", $sUrl);
268
269         if (!empty($this->sSmscId))
270         {
271             $sUrl .= "&smsc=" . $this->sSmscId;
272         }
273
274        /*  if (!empty($this->sFrom))
275         {
276             $sUrl .= "&from=" . $this->sFrom;
277         } */
278
279         try
280         {
281             $aRet = file($sUrl);
282             return empty($aRet) || substr($aRet[0], 0, 2) == 'ID'
283                 ? substr($aRet[0], 4)
284                 : $aRet;
285         }
286         catch (\Exception $e)
287         {
288                 \Utility\Debug::errorLog('ERROR', $e->getMessage());
289                 \Utility\Debug::errorLog('TRACE', $e->getTraceAsString());
290         }
291         return false;
292     }
293
294     /**
295      * Query message status.
296      * @param string $apimsgid
297      * @return string|boolean
298      */
299     public function getStatus($apimsgid)
300     {
301         #-> Config.
302         $config = include __DIR__ . '/../../../config/service.config.php';
303
304         #-> Query message status.
305         $sUrl = "https://api.clickatell.com/http/querymsg.php"
306                         . "?user=" . $config['SMS']['Username']
307                         . "&api_id=" . $config['SMS']['Password']
308                         . "&password=" . $config['SMS']['ApiId']
309                         . "&apimsgid=" . $apimsgid;
310         try
311         {
312                 #-> Retrieve status and return.
313                 $aRet = file($sUrl);
314                 $status = preg_split("/ /", $aRet['data']);
315                 return $status[0] == "ID:"
316                         ? trim($status[3])
317                         : false;
318         }
319         catch (\Exception $e)
320         {
321                 \Utility\Debug::errorLog('ERROR', $e->getMessage());
322                 \Utility\Debug::errorLog('TRACE', $e->getTraceAsString());
323         }
324     }
325
326     /**
327      * Determine the cost of the message which was sent.
328      * @param string $apimsgid
329      * @return array|boolean
330      */
331     public function getChargeAndStatus($apimsgid)
332     {
333         #-> Config.
334         $config = include __DIR__ . '/../../../config/service.config.php';
335
336         #-> Query message status.
337         $sUrl = "https://api.clickatell.com/http/getmsgcharge.php"
338                         . "?user=" . $config['SMS']['Username']
339                         . "&api_id=" . $config['SMS']['Password']
340                         . "&password=" . $config['SMS']['ApiId']
341                         . "&apimsgid=" . $apimsgid;
342         try
343         {
344                 #-> Retrieve status and return.
345                 $aRet = file($sUrl);
346                 $charge = preg_split("/[\s:]+/", $aRet['data']);
347                 return $charge[2] == "charge"
348                         ? array('Charge' => $charge[3], 'Status' => $charge[5])
349                         : false;
350         }
351         catch (\Exception $e)
352         {
353                 \Utility\Debug::errorLog('ERROR', $e->getMessage());
354                 \Utility\Debug::errorLog('TRACE', $e->getTraceAsString());
355         }
356     }
357
358
359 }
360
361