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