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
|
<?php
/**
* Kalkun
* An open source web based SMS Management
*
* @package Kalkun
* @author Kalkun Dev Team
* @license https://spdx.org/licenses/GPL-2.0-or-later.html
* @link https://kalkun.sourceforge.io/
*/
// ------------------------------------------------------------------------
/**
* MsgIncoming Class
*
* @package Kalkun
* @subpackage Plugin
*/
namespace Kalkun\Plugins\StopManager;
defined('BASEPATH') OR exit('No direct script access allowed');
require_once (APPPATH . 'plugins/Plugin_helper.php');
class MsgIncoming {
const TYPE_NOT_SET = 'TYPE_NOT_SET_SO_STOP_ALL';
const IGNORE_STOP_MANAGER = 'IGNORE_STOP_MANAGER';
private $party = NULL;
private $cmd = NULL;
private $type = NULL;
private $origMsg = NULL;
private $insertDate = NULL;
private $config = NULL;
private $is_lang_loaded = NULL;
public function __construct(Object $sms)
{
$this->origMsg = $sms->TextDecoded;
$this->party = $sms->SenderNumber;
$this->config = Config::getInstance();
$this->parseStopMessage();
}
public function getParty()
{
return $this->party;
}
public function getType()
{
return $this->type;
}
public function getOrigMsg()
{
return $this->origMsg;
}
public function getInsertDate()
{
return $this->insertDate;
}
public function setParty($party)
{
$this->party = $party;
}
public function getCmd()
{
return $this->cmd;
}
public function setCmd($cmd)
{
$this->cmd = strtoupper($cmd);
}
public function setType($type)
{
$this->type = ($this->config->isTypeEnabled()) ? strtolower($type) : self::TYPE_NOT_SET;
}
public function setOrigMsg($origMsg)
{
$this->origMsg = $origMsg;
}
public function setInsertDate($insertDate)
{
$this->insertDate = $insertDate;
}
public function isStopMessage()
{
return ($this->getCmd() !== NULL);
}
public function isValidStopMessage()
{
return ($this->isOptOut() || $this->isOptIn());
}
public function parseStopMessage()
{
$ret = NULL;
$types_reg = implode('|', $this->config->getValidTypes());
$cmds_reg = implode('|', $this->config->getValidCmds());
if ($this->config->isTypeEnabled())
{
$ret = preg_match('/\b(' . $cmds_reg . ')\s*(' . $types_reg . ')\b/i', $this->origMsg, $matches);
}
else
{
$ret = preg_match('/\b(' . $cmds_reg . ')\b/i', $this->origMsg, $matches);
}
if ($ret === 1)
{
if ( ! empty($matches[1]))
{
$this->setCmd($matches[1]);
}
if ( ! empty($matches[2]))
{
$this->setType($matches[2]);
}
else
{
$this->setType('EMPTY?');
}
}
}
public function isOptOut()
{
return in_array($this->getCmd(), $this->config->getKeywordsOptOut());
}
public function isOptIn()
{
return (in_array($this->getCmd(), $this->config->getKeywordsOptIn()) && $this->config->getConfig('enable_optin'));
}
private function getAutoReplyMsgConfirm()
{
if ($this->isOptOut())
{
return $this->getAutoReplyMsgConfirmOptOut();
}
if ($this->isOptIn())
{
return $this->getAutoReplyMsgConfirmOptIn();
}
}
private function getAutoReplyMsgConfirmOptIn()
{
return tr(
'{0} taken into account. To opt-out, send "{1}".',
NULL,
($this->config->getConfig('enable_type')) ? $this->cmd . ' ' . $this->type : $this->cmd,
($this->config->getConfig('enable_type')) ? $this->config->getKeywordsOptOut()[0] . ' ' . $this->type : $this->config->getKeywordsOptOut()[0]
);
}
private function getAutoReplyMsgConfirmOptOut()
{
if ($this->config->getConfig('enable_optin'))
{
return tr(
'{0} taken into account. To opt-in again, send "{1}".',
NULL,
($this->config->getConfig('enable_type')) ? $this->cmd . ' ' . $this->type : $this->cmd,
($this->config->getConfig('enable_type')) ? $this->config->getKeywordsOptIn()[0] . ' ' . $this->type : $this->config->getKeywordsOptIn()[0]
);
}
else
{
return tr(
'{0} taken into account.',
NULL,
($this->config->getConfig('enable_type')) ? $this->cmd . ' ' . $this->type : $this->cmd
);
}
}
private function getAutoReplyMsgInvalidWithOptInWithType()
{
return tr(
'Request not valid ({0}). Send "{1} or {2} <type>". Possible values for <type> are: {3}. For example "{4}".',
NULL,
$this->getOrigMsg(),
$this->config->getKeywordsOptOut()[0],
$this->config->getKeywordsOptIn()[0],
implode(', ', $this->config->getValidTypes()),
$this->config->getKeywordsOptOut()[0] . ' ' . $this->config->getValidTypes()[0]
);
}
private function getAutoReplyMsgInvalidWithOptInWithoutType()
{
return tr(
'Request not valid ({0}). Send "{1}" or "{2}". For example "{3}".',
NULL,
$this->getOrigMsg(),
$this->config->getKeywordsOptOut()[0],
$this->config->getKeywordsOptIn()[0],
$this->config->getKeywordsOptOut()[0]
);
}
private function getAutoReplyMsgInvalidWithoutOptInWithType()
{
return tr(
'Request not valid ({0}). Send "{1} <type>". Possible values for <type> are: {2}. For example "{3}".',
NULL,
$this->getOrigMsg(),
$this->config->getKeywordsOptOut()[0],
implode(', ', $this->config->getValidTypes()),
$this->config->getKeywordsOptOut()[0] . ' ' . $this->config->getValidTypes()[0]
);
}
private function getAutoReplyMsgInvalidWithoutOptInWithoutType()
{
return tr(
'Request not valid ({0}). Send "{1}".',
NULL,
$this->getOrigMsg(),
$this->config->getKeywordsOptOut()[0]
);
}
private function getAutoReplyMsgInvalid()
{
if ($this->config->getConfig('enable_type'))
{
if ($this->config->getConfig('enable_optin'))
{
return $this->getAutoReplyMsgInvalidWithOptInWithType();
}
else
{
return $this->getAutoReplyMsgInvalidWithoutOptInWithType();
}
}
else
{
if ($this->config->getConfig('enable_optin'))
{
return $this->getAutoReplyMsgInvalidWithOptInWithoutType();
}
else
{
return $this->getAutoReplyMsgInvalidWithoutOptInWithoutType();
}
}
}
public function getAutoReplyMsg()
{
if ( ! $this->is_lang_loaded)
{
// Load Translation helper functions
$CI = &get_instance();
$CI->load->helper(['language', 'i18n']);
// We cannot determine the language of a specific user since this is called on incoming message
// So the language to use by this robot is read from plugin config
\Plugin_helper::load_lang('stop_manager', $this->config->getConfig('autoreply_language'));
$this->is_lang_loaded = TRUE;
}
if ($this->isValidStopMessage())
{
return $this->getAutoReplyMsgConfirm();
}
else
{
return $this->getAutoReplyMsgInvalid();
}
}
}
|