updates
[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('Sms.send Override: ' . $this->sTo, $this->sBody);
253                 return;
254         }
255
256         #-> Send using api.
257         $sUrl = "https://api.clickatell.com/http/sendmsg.php"
258               . "?user=" . $config['Username']
259               . "&api_id=" . $config['ApiId']
260               . "&password=" . $config['Password']
261               . "&concat=4"
262               . "&callback=2"
263               . "&to=$this->sTo"
264               . "&text=".substr(urlencode($this->sBody),0,620);
265
266         \Utility\Debug::errorLog("sms url", $sUrl);
267
268         if (!empty($this->sSmscId))
269         {
270             $sUrl .= "&smsc=" . $this->sSmscId;
271         }
272
273        /*  if (!empty($this->sFrom))
274         {
275             $sUrl .= "&from=" . $this->sFrom;
276         } */
277
278         try
279         {
280             $aRet = file($sUrl);
281             return empty($aRet) || substr($aRet[0], 0, 2) == 'ID'
282                 ? substr($aRet[0], 4)
283                 : $aRet;
284         }
285         catch (\Exception $e)
286         {
287                 \Utility\Debug::errorLog('ERROR', $e->getMessage());
288                 \Utility\Debug::errorLog('TRACE', $e->getTraceAsString());
289         }
290         return false;
291     }
292
293     /**
294      * Query message status.
295      * @param string $apimsgid
296      * @return string|boolean
297      */
298     public function getStatus($apimsgid)
299     {
300         #-> Config.
301         $config = include __DIR__ . '/../../../config/service.config.php';
302
303         #-> Query message status.
304         $sUrl = "https://api.clickatell.com/http/querymsg.php"
305                         . "?user=" . $config['SMS']['Username']
306                         . "&api_id=" . $config['SMS']['Password']
307                         . "&password=" . $config['SMS']['ApiId']
308                         . "&apimsgid=" . $apimsgid;
309         try
310         {
311                 #-> Retrieve status and return.
312                 $aRet = file($sUrl);
313                 $status = preg_split("/ /", $aRet['data']);
314                 return $status[0] == "ID:"
315                         ? trim($status[3])
316                         : false;
317         }
318         catch (\Exception $e)
319         {
320                 \Utility\Debug::errorLog('ERROR', $e->getMessage());
321                 \Utility\Debug::errorLog('TRACE', $e->getTraceAsString());
322         }
323     }
324
325     /**
326      * Determine the cost of the message which was sent.
327      * @param string $apimsgid
328      * @return array|boolean
329      */
330     public function getChargeAndStatus($apimsgid)
331     {
332         #-> Config.
333         $config = include __DIR__ . '/../../../config/service.config.php';
334
335         #-> Query message status.
336         $sUrl = "https://api.clickatell.com/http/getmsgcharge.php"
337                         . "?user=" . $config['SMS']['Username']
338                         . "&api_id=" . $config['SMS']['Password']
339                         . "&password=" . $config['SMS']['ApiId']
340                         . "&apimsgid=" . $apimsgid;
341         try
342         {
343                 #-> Retrieve status and return.
344                 $aRet = file($sUrl);
345                 $charge = preg_split("/[\s:]+/", $aRet['data']);
346                 return $charge[2] == "charge"
347                         ? array('Charge' => $charge[3], 'Status' => $charge[5])
348                         : false;
349         }
350         catch (\Exception $e)
351         {
352                 \Utility\Debug::errorLog('ERROR', $e->getMessage());
353                 \Utility\Debug::errorLog('TRACE', $e->getTraceAsString());
354         }
355     }
356
357
358 }
359
360