fwolf /
fwlib
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Fwlib\Net\Sms; |
||
| 3 | |||
| 4 | use Fwlib\Config\ConfigAwareTrait; |
||
| 5 | use Fwlib\Db\AdodbAwareTrait; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * SMS Sender |
||
| 9 | * |
||
| 10 | * Supported SMS send method: |
||
| 11 | * - gammu smsd inject command |
||
| 12 | * |
||
| 13 | * @copyright Copyright 2010-2015 Fwolf |
||
| 14 | * @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
||
| 15 | */ |
||
| 16 | class SmsSender |
||
| 17 | { |
||
| 18 | use AdodbAwareTrait; |
||
| 19 | use ConfigAwareTrait; |
||
| 20 | |||
| 21 | |||
| 22 | /** |
||
| 23 | * SMS logger object |
||
| 24 | * |
||
| 25 | * @var SmsLogger |
||
| 26 | */ |
||
| 27 | protected $smsLogger = null; |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * {@inheritdoc} |
||
| 32 | */ |
||
| 33 | protected function getDefaultConfigs() |
||
| 34 | { |
||
| 35 | $configs = []; |
||
| 36 | |||
| 37 | // SMS send method |
||
| 38 | $configs['method'] = 'gammuSmsdInject'; |
||
| 39 | |||
| 40 | // Possible bin path |
||
| 41 | $configs['path.bin'] = [ |
||
| 42 | '/usr/bin/', |
||
| 43 | '/usr/local/bin/', |
||
| 44 | '/bin/', |
||
| 45 | ]; |
||
| 46 | |||
| 47 | // Path of gammu-smsd-inject, leave empty to find in path.bin. |
||
| 48 | // Set this will bypass inject cmd search in path.bin. |
||
| 49 | $configs['path.gammuSmsdInject'] = ''; |
||
| 50 | |||
| 51 | // Cmd template of gammu-smsd-inject cmd |
||
| 52 | /** @noinspection SpellCheckingInspection */ |
||
| 53 | $configs['cmd.gammuSmsdInject'] |
||
| 54 | = '[cmd] TEXT [dest] -autolen 600 -report -validity MAX -unicode -textutf8 "[sms]"'; |
||
| 55 | |||
| 56 | return $configs; |
||
| 57 | } |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * Find path of gammu smsd inject cmd |
||
| 62 | * |
||
| 63 | * If find in $path fail, will try path in $this->config['path.bin']. The |
||
| 64 | * exe filename is hardcoded 'gammu-smsd-inject'. |
||
| 65 | * |
||
| 66 | * @param string $path Manual additional path |
||
| 67 | * @return mixed Path of inject cmd, false when fail |
||
| 68 | */ |
||
| 69 | public function getPathOfGammuSmsdInject($path = '') |
||
| 70 | { |
||
| 71 | if (!empty($this->getConfig('path.gammuSmsdInject'))) { |
||
| 72 | return $this->getConfig('path.gammuSmsdInject'); |
||
| 73 | } |
||
| 74 | |||
| 75 | |||
| 76 | $arPath = $this->getConfig('path.bin'); |
||
| 77 | if (!empty($path)) { |
||
| 78 | array_unshift($arPath, $path); |
||
| 79 | } |
||
| 80 | |||
| 81 | // Find a usable path |
||
| 82 | $found = false; |
||
| 83 | View Code Duplication | while (!$found && !empty($arPath)) { |
|
|
0 ignored issues
–
show
|
|||
| 84 | $cmd = array_shift($arPath) . 'gammu-smsd-inject'; |
||
| 85 | if (is_executable($cmd)) { |
||
| 86 | $found = true; |
||
| 87 | break; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | if ($found) { |
||
| 92 | return $cmd; |
||
|
0 ignored issues
–
show
The variable
$cmd does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
| 93 | } else { |
||
| 94 | return false; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * Get SMS logger instance |
||
| 101 | * |
||
| 102 | * @return SmsLogger |
||
| 103 | */ |
||
| 104 | protected function getSmsLogger() |
||
| 105 | { |
||
| 106 | if (is_null($this->smsLogger)) { |
||
| 107 | $this->smsLogger = |
||
| 108 | new SmsLogger($this->getDb()); |
||
|
0 ignored issues
–
show
The call to
SmsLogger::__construct() has too many arguments starting with $this->getDb().
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. In this case you can add the Loading history...
|
|||
| 109 | } |
||
| 110 | |||
| 111 | return $this->smsLogger; |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Parse phone number string |
||
| 117 | * |
||
| 118 | * Do: |
||
| 119 | * Split phone number, |
||
| 120 | * Format phone number, |
||
| 121 | * Remove duplicate number. |
||
| 122 | * |
||
| 123 | * Only support mobile number of china mainland (start with +86 or 0086). |
||
| 124 | * |
||
| 125 | * @param array|string $number |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | public function parsePhoneNumber($number) |
||
| 129 | { |
||
| 130 | // If array given, still need convert to string, for format and |
||
| 131 | // validate later. |
||
| 132 | if (is_array($number)) { |
||
| 133 | $number = implode(',', $number); |
||
| 134 | } |
||
| 135 | |||
| 136 | // Remove special chars |
||
| 137 | $number = str_replace([',', '。', ';'], ',', $number); |
||
| 138 | $number = preg_replace('/[ ,;\r\n\t]{1,}/', ',', $number); |
||
| 139 | $arNumber = explode(',', $number); |
||
| 140 | |||
| 141 | // Format and remove invalid number |
||
| 142 | foreach ($arNumber as $k => &$n) { |
||
| 143 | // Remove +86, 0086 |
||
| 144 | if ('+86' == substr($n, 0, 3)) { |
||
| 145 | $n = substr($n, 3); |
||
| 146 | } |
||
| 147 | if ('0086' == substr($n, 0, 4)) { |
||
| 148 | $n = substr($n, 4); |
||
| 149 | } |
||
| 150 | |||
| 151 | // Invalid length or not special service number |
||
| 152 | if (11 != strlen($n) && '10' != substr($n, 0, 2)) { |
||
| 153 | unset($arNumber[$k]); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | unset($n); |
||
| 157 | |||
| 158 | // Remove duplicate |
||
| 159 | $arNumber = array_unique($arNumber); |
||
| 160 | |||
| 161 | // Resort array index |
||
| 162 | $arNumber = array_merge($arNumber, []); |
||
| 163 | |||
| 164 | return $arNumber; |
||
| 165 | } |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Send SMS |
||
| 170 | * |
||
| 171 | * @param mixed $destNumber |
||
| 172 | * @param string $sms |
||
| 173 | * @param integer $cat |
||
| 174 | * @return integer Actual valid phone number sent. |
||
| 175 | */ |
||
| 176 | public function send($destNumber, $sms, $cat = 0) |
||
| 177 | { |
||
| 178 | // Map of method config to send function |
||
| 179 | $map = [ |
||
| 180 | 'gammuSmsdInject' => 'sendUsingGammuSmsdInject', |
||
| 181 | ]; |
||
| 182 | |||
| 183 | |||
| 184 | $destNumber = $this->parsePhoneNumber($destNumber); |
||
| 185 | if (1 > count($destNumber)) { |
||
| 186 | throw new \Exception('No valid number to sent.'); |
||
| 187 | } |
||
| 188 | |||
| 189 | |||
| 190 | $method = $this->getConfig('method'); |
||
| 191 | if (isset($map[$method])) { |
||
| 192 | $func = $map[$this->getConfig('method')]; |
||
| 193 | $i = $this->$func($destNumber, $sms); |
||
| 194 | |||
| 195 | $this->getSmsLogger()->log($destNumber, $sms, $cat); |
||
| 196 | |||
| 197 | return $i; |
||
| 198 | |||
| 199 | } else { |
||
| 200 | throw new \Exception("Method $method not supported."); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * Send SMS using gammu smsd inject method |
||
| 207 | * |
||
| 208 | * Notice: On web server, need assign user www-data to gammu group, and |
||
| 209 | * make /var/log/gammu-smsd.log g+w. |
||
| 210 | * |
||
| 211 | * Modem server need not, only conn to db is required. |
||
| 212 | * |
||
| 213 | * $destNumber may be array of phone number, or string of numbers splitted |
||
| 214 | * by any char of " ,;,;。\r\n". |
||
| 215 | * |
||
| 216 | * @param mixed $destNumber |
||
| 217 | * @param string $sms |
||
| 218 | * @return integer Actual valid phone number sent. |
||
| 219 | */ |
||
| 220 | protected function sendUsingGammuSmsdInject($destNumber, $sms) |
||
| 221 | { |
||
| 222 | $injectCmd = $this->getPathOfGammuSmsdInject(); |
||
| 223 | if (empty($injectCmd)) { |
||
| 224 | throw new \Exception( |
||
| 225 | 'Can\'t find gammu smsd inject execute file.' |
||
| 226 | ); |
||
| 227 | } |
||
| 228 | |||
| 229 | // Prepare cmd to sent |
||
| 230 | $cmd = str_replace( |
||
| 231 | ['[cmd]', '[sms]'], |
||
| 232 | [$this->getConfig('path.gammuSmsdInject'), addslashes($sms)], |
||
| 233 | $this->getConfig('cmd.gammuSmsdInject') |
||
| 234 | ); |
||
| 235 | $i = strpos($cmd, '[dest]'); |
||
| 236 | if (1 > $i) { |
||
| 237 | throw new \Exception( |
||
| 238 | 'Command template of gammu smsd inject error.' |
||
| 239 | ); |
||
| 240 | } |
||
| 241 | $cmd1 = substr($cmd, 0, $i); |
||
| 242 | $cmd2 = substr($cmd, $i + 6); // 6 is length of '[dest]' |
||
| 243 | |||
| 244 | // Loop to sent each number |
||
| 245 | foreach ($destNumber as $dest) { |
||
| 246 | $cmd = $cmd1 . $dest . $cmd2; |
||
| 247 | $output = []; |
||
| 248 | $returnValue = 0; |
||
| 249 | exec($cmd, $output, $returnValue); |
||
| 250 | |||
| 251 | if (0 != $returnValue) { |
||
|
0 ignored issues
–
show
|
|||
| 252 | throw new \Exception('Gammu inject error: ' . $output[1]); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | return count($destNumber); |
||
| 257 | } |
||
| 258 | } |
||
| 259 |
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.