|
1
|
|
|
<?php |
|
2
|
|
|
namespace Fwlib\Net\Sms; |
|
3
|
|
|
|
|
4
|
|
|
use Fwlib\Db\AdodbAwareTrait; |
|
5
|
|
|
use Fwlib\Util\UtilContainerAwareTrait; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* SMS sent logger |
|
9
|
|
|
* |
|
10
|
|
|
* For schema of log table in db, see sms-log.sql. |
|
11
|
|
|
* |
|
12
|
|
|
* @copyright Copyright 2010-2015 Fwolf |
|
13
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
|
14
|
|
|
* @see sms-log.sql |
|
15
|
|
|
*/ |
|
16
|
|
|
class SmsLogger |
|
17
|
|
|
{ |
|
18
|
|
|
use AdodbAwareTrait; |
|
19
|
|
|
use UtilContainerAwareTrait; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Log table name |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public $table = 'sms_log'; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Count dest number for each mobile company |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $arDest |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function countDestCompany($arDest) |
|
37
|
|
|
{ |
|
38
|
|
|
$ar = [ |
|
39
|
|
|
'cm' => 0, |
|
40
|
|
|
'cu' => 0, |
|
41
|
|
|
'ct' => 0, |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
$arCm = [ |
|
45
|
|
|
134, 135, 136, 137, 138, 139, 147, 150, 151, 152, 157, |
|
46
|
|
|
158, 159, 182, 183, 184, 187, 188 |
|
47
|
|
|
]; |
|
48
|
|
|
$arCu = [130, 131, 132, 145, 155, 156, 185, 186]; |
|
49
|
|
|
$arCt = [133, 153, 180, 181, 189]; |
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
foreach ($arDest as $dest) { |
|
|
|
|
|
|
52
|
|
|
$i = intval(substr($dest, 0, 3)); |
|
53
|
|
|
|
|
54
|
|
|
if (in_array($i, $arCm)) { |
|
55
|
|
|
$ar['cm'] ++; |
|
56
|
|
|
|
|
57
|
|
|
} elseif (in_array($i, $arCu)) { |
|
58
|
|
|
$ar['cu'] ++; |
|
59
|
|
|
|
|
60
|
|
|
} elseif (in_array($i, $arCt)) { |
|
61
|
|
|
$ar['ct'] ++; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $ar; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Count SMS will split to how many parts to send |
|
71
|
|
|
* |
|
72
|
|
|
* If only ascii chars include, 140 chars for 1 sms part, if has chinese |
|
73
|
|
|
* chars, 70 chars for 1 sms part only. |
|
74
|
|
|
* |
|
75
|
|
|
* 1 chinese char will count as 1 char. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $sms |
|
78
|
|
|
* @return integer |
|
79
|
|
|
*/ |
|
80
|
|
|
public function countPart($sms = '') |
|
81
|
|
|
{ |
|
82
|
|
|
if (empty($sms)) { |
|
83
|
|
|
return 0; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
// Is there chinese in sms ? |
|
88
|
|
View Code Duplication |
if (mb_strlen($sms, 'utf-8') == strlen($sms)) { |
|
|
|
|
|
|
89
|
|
|
return intval(ceil(strlen($sms) / 140)); |
|
90
|
|
|
} else { |
|
91
|
|
|
return intval(ceil(mb_strlen($sms, 'utf-8') / 70)); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Log sent sms with stat information |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $arDest |
|
100
|
|
|
* @param string $sms |
|
101
|
|
|
* @param integer $cat |
|
102
|
|
|
*/ |
|
103
|
|
|
public function log($arDest, $sms, $cat) |
|
104
|
|
|
{ |
|
105
|
|
|
// Prepare data array |
|
106
|
|
|
$logData = []; |
|
107
|
|
|
$countDestCompany = $this->countDestCompany($arDest); |
|
108
|
|
|
|
|
109
|
|
|
$logData['uuid'] = $this->generateUuid(); |
|
110
|
|
|
$logData['st'] = date('Y-m-d H:i:s'); |
|
111
|
|
|
$logData['cat'] = $cat; |
|
112
|
|
|
$logData['cnt_dest'] = count($arDest); |
|
113
|
|
|
$logData['cnt_dest_cm'] = $countDestCompany['cm']; |
|
114
|
|
|
$logData['cnt_dest_cu'] = $countDestCompany['cu']; |
|
115
|
|
|
$logData['cnt_dest_ct'] = $countDestCompany['ct']; |
|
116
|
|
|
$logData['dest'] = implode(',', $arDest); |
|
117
|
|
|
$logData['cnt_part'] = $this->countPart($sms); |
|
118
|
|
|
$logData['sms'] = $sms; |
|
119
|
|
|
|
|
120
|
|
|
// Save to db |
|
121
|
|
|
$this->db->write($this->table, $logData, 'I'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Generate an UUID |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function generateUuid() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->getUtilContainer()->getUuidBase36()->generate(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.