Git Repository Public Repository

namibia

URLs

Copy to Clipboard
 
db7090095af602598e1fd96e638fe04dc5e9e2be
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
namespace Utility\Comms;


/**
 * This class facilitates basic sms sending.
 * @author Andre Fourie
 */
class Sms
{
    #-> Error Messages.
    const ERROR_ADDRESS     = 'Utility\Comms\Sms, invalid Address supplied.';

		#-> Status codes.
		static private $statusCode      = array(
				'001' => 'Message unknown',
				'002' => 'Message queued',
				'003' => 'Delivered to gateway',
				'004' => 'Received by recipient',
				'005' => 'Error with message',
				'006' => 'User cancelled message delivery',
				'007' => 'Error delivering message',
				'008' => 'OK',
				'009' => 'Routing error',
				'010' => 'Message expired',
				'011' => 'Message queued for later delivery',
				'012' => 'Out of credit',
				'014' => 'Maximum MT limit exceeded'
				);

    #-> Params
    private $sBody          = '';
    private $sTo            = '';
    private $sCc            = '';
    private $sBcc           = '';
    private $sFrom          = '';
    private $sSubject       = '';
    private $sSmscId        = '';



    /* ---------------------------------------------------------------------------------------- */
    #-> Summoning.

    public function __construct() {}



    /* ---------------------------------------------------------------------------------------- */
    #-> Private Functions

    private function checkNumericNotZero($val)
    {
    	return is_numeric($val) && 0 != $val;
    }


    /* ---------------------------------------------------------------------------------------- */
    #-> Public Functions - Util.

    /**
     * Swap sms status code for short status text.
     * @param  string $code
     * @return string
     */
    static public function getStatusText($code)
    {
    	$code = str_pad($code, 3, '0', STR_PAD_LEFT);
    	return isset(self::$statusCode[$code])
    		? self::$statusCode[$code]
    		: 'Unknown message status';
    }


    /* ---------------------------------------------------------------------------------------- */
    #-> Public Functions - Sending.

    /**
     * Set from.
     * @param string $sFrom
     * @return boolean
     */
    public function setFrom($sFrom)
    {
        if ($sFrom)
        {
            $this->sFrom = $sFrom;
            return true;
        }
        return false;
    }

    /**
     * Set to.
     * @param integer|array $mTo
     * @return boolean
     */
    public function setTo($mTo)
    {
        #-> Add to list
        if (!is_array($mTo) && is_numeric($mTo))
        {
            $this->sTo .= empty($this->sTo)
                ? $mTo
                : ',' . $mTo;
        }
        if (is_array($mTo) && !empty($mTo))
        {
            foreach ($mTo as $mAddress)
            {
                if (is_numeric($mAddress))
                {
                    $this->sTo .= empty($this->sTo)
                        ? $mAddress
                        : ',' . $mAddress;
                }
            }
            return true;
        }
        return false;
    }

    /**
     * Set to without concat.
     * @param string $sTo
     * @return boolean
     */
    public function setToNoConcat($sTo)
    {
        #-> Add to list
        if (!empty($sTo))
        {
            $this->sTo = $sTo;
            return true;
        }
        return false;
    }

    /**
     * Set cc.
     * @param multi $mCc
     * @return boolean
     */
    public function setCc($mCc)
    {
        return $this->SetTo($mCc);
    }

    /**
     * Set bcc.
     * @param multi $mBcc
     * @return boolean
     */
    public function setBcc($mBcc)
    {
        return $this->SetTo($mBcc);
    }

    /**
     * Set subject.
     * @param string $sSubject
     * @return boolean
     */
    public function setSubject($sSubject = 0)
    {
        if ($sSubject)
        {
            $this->sSubject = $sSubject;
            return true;
        }
        return false;
    }

    /**
     * Set body.
     * @param string $sBody
     * @return boolean
     */
    public function setBody($sBody)
    {
        if ($sBody)
        {
            $this->sBody = $sBody;
            return true;
        }
        return false;
    }

    /**
     * Set smsc id.
     * @param string $sSmscId
     * @return boolean
     */
    public function setSmscId($sSmscId)
    {
        if ($sSmscId)
        {
            $this->sSmscId = $sSmscId;
            return true;
        }
        return false;
    }

    /**
     * Set context parameters from array.
     * @param array $aContext
     * @return boolean
     */
    public function setContext(array $aContext = array())
    {
        #-> Check context.
        foreach ($aContext as $sParam => $mValue)
        {
            switch ($sParam)
            {
                case 'To':
                case 'Cc':
                case 'Bcc':
                    $this->setTo($mValue);
                    break;
                case 'From':
                    $this->setFrom($mValue);
                    break;
                case 'Subject':
                case 'Body':
                    $this->sBody .= empty($this->sBody)
                        ? $mValue
                        : "\n" . $mValue;
                    break;
            }
        }

    }

    /**
     * Send the sms.
     * @param array $aContext
     * @return boolean
     */
    public function send(array $aContext = array())
    {
    	#-> Config.
        $config = \Utility\Registry::getConfigParam('SMS');

        #-> Check context.
        empty($aContext)
            || $this->setContext($aContext);

        #-> Environmental override.
        if (IS_DEV_ENV && !IS_STAGE_ENV)
        {
            \Utility\Debug::errorLog("overriding","sms");
        	\Utility\Debug::errorLog('Sms.send Override: ' . $this->sTo, $this->sBody);
        	return;
        }

        #-> Send using api.
        $sUrl = "https://api.clickatell.com/http/sendmsg.php"
              . "?user=" . $config['Username']
              . "&api_id=" . $config['ApiId']
              . "&password=" . $config['Password']
              . "&concat=4"
              . "&callback=2"
              . "&to=$this->sTo"
              . "&text=".substr(urlencode($this->sBody),0,620);

        \Utility\Debug::errorLog("sms url", $sUrl);
        error_log("sms url", $sUrl);

        if (!empty($this->sSmscId))
        {
            $sUrl .= "&smsc=" . $this->sSmscId;
        }

       /*  if (!empty($this->sFrom))
        {
            $sUrl .= "&from=" . $this->sFrom;
        } */

        try
        {
            $aRet = file($sUrl);
            return empty($aRet) || substr($aRet[0], 0, 2) == 'ID'
                ? substr($aRet[0], 4)
                : $aRet;
        }
        catch (\Exception $e)
        {
        	\Utility\Debug::errorLog('ERROR', $e->getMessage());
        	\Utility\Debug::errorLog('TRACE', $e->getTraceAsString());
        }
        return false;
    }

    /**
     * Query message status.
     * @param string $apimsgid
     * @return string|boolean
     */
    public function getStatus($apimsgid)
    {
    	#-> Config.
    	$config = include __DIR__ . '/../../../config/service.config.php';

    	#-> Query message status.
    	$sUrl = "https://api.clickatell.com/http/querymsg.php"
    			. "?user=" . $config['SMS']['Username']
    			. "&api_id=" . $config['SMS']['Password']
    			. "&password=" . $config['SMS']['ApiId']
    			. "&apimsgid=" . $apimsgid;
    	try
    	{
    		#-> Retrieve status and return.
    		$aRet = file($sUrl);
    		$status = preg_split("/ /", $aRet['data']);
    		return $status[0] == "ID:"
    			? trim($status[3])
    			: false;
    	}
    	catch (\Exception $e)
    	{
        	\Utility\Debug::errorLog('ERROR', $e->getMessage());
        	\Utility\Debug::errorLog('TRACE', $e->getTraceAsString());
    	}
    }

    /**
     * Determine the cost of the message which was sent.
     * @param string $apimsgid
     * @return array|boolean
     */
    public function getChargeAndStatus($apimsgid)
    {
    	#-> Config.
    	$config = include __DIR__ . '/../../../config/service.config.php';

    	#-> Query message status.
    	$sUrl = "https://api.clickatell.com/http/getmsgcharge.php"
    			. "?user=" . $config['SMS']['Username']
    			. "&api_id=" . $config['SMS']['Password']
    			. "&password=" . $config['SMS']['ApiId']
    			. "&apimsgid=" . $apimsgid;
    	try
    	{
    		#-> Retrieve status and return.
    		$aRet = file($sUrl);
    		$charge = preg_split("/[\s:]+/", $aRet['data']);
    		return $charge[2] == "charge"
    			? array('Charge' => $charge[3], 'Status' => $charge[5])
    			: false;
    	}
    	catch (\Exception $e)
    	{
        	\Utility\Debug::errorLog('ERROR', $e->getMessage());
        	\Utility\Debug::errorLog('TRACE', $e->getTraceAsString());
    	}
    }


}


Commits for namibiamodule/Utility/src/Utility/Comms/Sms.php

Diff revisions: vs.
Revision Author Commited Message
db7090 ... Diff Diff Mark Fri 23 Dec, 2016 14:03:22 +0000

testing

a32f73 ... Diff Diff Mark Mon 12 Dec, 2016 12:07:47 +0000

sms functions testing

f5b7b4 ... Diff Diff Mark Mon 12 Dec, 2016 11:55:35 +0000

updates

df0489 ... Mark Fri 14 Oct, 2016 10:01:00 +0000

initial commit