2 namespace Utility\Comms;
6 * This class facilitates basic sms sending.
12 const ERROR_ADDRESS = 'Utility\Comms\Sms, invalid Address supplied.';
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',
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'
37 private $sSubject = '';
38 private $sSmscId = '';
42 /* ---------------------------------------------------------------------------------------- */
45 public function __construct() {}
49 /* ---------------------------------------------------------------------------------------- */
52 private function checkNumericNotZero($val)
54 return is_numeric($val) && 0 != $val;
58 /* ---------------------------------------------------------------------------------------- */
59 #-> Public Functions - Util.
62 * Swap sms status code for short status text.
66 static public function getStatusText($code)
68 $code = str_pad($code, 3, '0', STR_PAD_LEFT);
69 return isset(self::$statusCode[$code])
70 ? self::$statusCode[$code]
71 : 'Unknown message status';
75 /* ---------------------------------------------------------------------------------------- */
76 #-> Public Functions - Sending.
80 * @param string $sFrom
83 public function setFrom($sFrom)
87 $this->sFrom = $sFrom;
95 * @param integer|array $mTo
98 public function setTo($mTo)
101 if (!is_array($mTo) && is_numeric($mTo))
103 $this->sTo .= empty($this->sTo)
107 if (is_array($mTo) && !empty($mTo))
109 foreach ($mTo as $mAddress)
111 if (is_numeric($mAddress))
113 $this->sTo .= empty($this->sTo)
124 * Set to without concat.
128 public function setToNoConcat($sTo)
144 public function setCc($mCc)
146 return $this->SetTo($mCc);
154 public function setBcc($mBcc)
156 return $this->SetTo($mBcc);
161 * @param string $sSubject
164 public function setSubject($sSubject = 0)
168 $this->sSubject = $sSubject;
176 * @param string $sBody
179 public function setBody($sBody)
183 $this->sBody = $sBody;
191 * @param string $sSmscId
194 public function setSmscId($sSmscId)
198 $this->sSmscId = $sSmscId;
205 * Set context parameters from array.
206 * @param array $aContext
209 public function setContext(array $aContext = array())
212 foreach ($aContext as $sParam => $mValue)
219 $this->setTo($mValue);
222 $this->setFrom($mValue);
226 $this->sBody .= empty($this->sBody)
237 * @param array $aContext
240 public function send(array $aContext = array())
243 $config = \Utility\Registry::getConfigParam('SMS');
247 || $this->setContext($aContext);
249 #-> Environmental override.
250 if (IS_DEV_ENV && !IS_STAGE_ENV)
252 \Utility\Debug::errorLog("overriding","sms");
253 \Utility\Debug::errorLog('Sms.send Override: ' . $this->sTo, $this->sBody);
258 $sUrl = "https://api.clickatell.com/http/sendmsg.php"
259 . "?user=" . $config['Username']
260 . "&api_id=" . $config['ApiId']
261 . "&password=" . $config['Password']
265 . "&text=".substr(urlencode($this->sBody),0,620);
267 \Utility\Debug::errorLog("sms url", $sUrl);
268 error_log("sms url", $sUrl);
270 if (!empty($this->sSmscId))
272 $sUrl .= "&smsc=" . $this->sSmscId;
275 /* if (!empty($this->sFrom))
277 $sUrl .= "&from=" . $this->sFrom;
283 return empty($aRet) || substr($aRet[0], 0, 2) == 'ID'
284 ? substr($aRet[0], 4)
287 catch (\Exception $e)
289 \Utility\Debug::errorLog('ERROR', $e->getMessage());
290 \Utility\Debug::errorLog('TRACE', $e->getTraceAsString());
296 * Query message status.
297 * @param string $apimsgid
298 * @return string|boolean
300 public function getStatus($apimsgid)
303 $config = include __DIR__ . '/../../../config/service.config.php';
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;
313 #-> Retrieve status and return.
315 $status = preg_split("/ /", $aRet['data']);
316 return $status[0] == "ID:"
320 catch (\Exception $e)
322 \Utility\Debug::errorLog('ERROR', $e->getMessage());
323 \Utility\Debug::errorLog('TRACE', $e->getTraceAsString());
328 * Determine the cost of the message which was sent.
329 * @param string $apimsgid
330 * @return array|boolean
332 public function getChargeAndStatus($apimsgid)
335 $config = include __DIR__ . '/../../../config/service.config.php';
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;
345 #-> Retrieve status and return.
347 $charge = preg_split("/[\s:]+/", $aRet['data']);
348 return $charge[2] == "charge"
349 ? array('Charge' => $charge[3], 'Status' => $charge[5])
352 catch (\Exception $e)
354 \Utility\Debug::errorLog('ERROR', $e->getMessage());
355 \Utility\Debug::errorLog('TRACE', $e->getTraceAsString());