yf_service_turbosms class

Fileplugins/sms/classes/yf_service_turbosms.class.php
Use_class('service_turbosms')

Public properties

A site sets them in its config with $PROJECT_CONF['service_turbosms']['NAME'] = ...;

PropertyDefaultDescription
$_connection null
$allowed_countries ['UA' => '38']
$conf []
$copy_to []
$default_conf
['timeout' => 5, 'db_host' => '94.249.146.189', 'db_port' => '3306', 'db_name' => 'users', 'db_user' => '__override_user_here__', 'db_pswd' => '__override_pswd_here__', ... (8 items)]
array (
  'timeout' => 5,
  'db_host' => '94.249.146.189',
  'db_port' => '3306',
  'db_name' => 'users',
  'db_user' => '__override_user_here__',
  'db_pswd' => '__override_pswd_here__',
  'text' => '__override_default_text_here__',
  'sign' => '__override_sign_here__',
)

Public methods 5

public function insert_data_with_sql($data = [])
    public function insert_data_with_sql($data = [])
    {
        if ( ! $data) {
            return false;
        }
        $conf = $this->default_conf;
        foreach ($this->conf as $k => $v) {
            $conf[$k] = $v;
        }
        $db = mysqli_init();
        mysqli_options($db, MYSQLI_OPT_CONNECT_TIMEOUT, $conf['timeout']);
        mysqli_real_connect($db, $conf['db_host'], $conf['db_user'], $conf['db_pswd'], $conf['db_name'], $conf['db_port']);
        mysqli_set_charset($db, 'utf8');
        mysqli_select_db($db, $conf['db_name']);
        // TODO: create separate db connection for this
        // !!! DO not use insert_safe here, it cannot check fields for existance in turbosms and SMS will NOT send
        $sql = str_replace('INSERT INTO `' . DB_PREFIX . $conf['db_name'] . '`', 'INSERT INTO `' . $conf['db_name'] . '`', db()->insert($conf['db_name'], db()->es_for_builder($data), $as_sql = true));
        $result = mysqli_query($db, $sql);
        if (DEBUG_MODE) {
            debug(__FUNCTION__ . '[]', ['result' => (int) $result, 'data' => $data]);
        }
        mysqli_close($db);
        return $result;
    }

send

public function send($phone, $text, $params = [])
    public function send($phone, $text, $params = [])
    {
        $phone_error = null;
        if ( ! $this->_is_enabled()) {
            return false;
        }
        $phone_error = '';
        if ( ! strlen($phone) || ! strlen($text)) {
            throw new Exception('ERROR SMS: phone: ' . $phone . ', error: ' . $phone_error . ')');
            return false;
        }
        $phone = $this->_phone_cleanup($phone, $phone_error);
        // Example correct: +380 63 123 45 67 (spaces here for example readability)
        if (strlen($phone) != 13 || $phone_error) {
            throw new Exception('ERROR TURBOSMS: phone: ' . $phone . ', error: ' . $phone_error);
            return false;
        }
        if ($text == '') {
            $text = ($this->conf['text'] ?? null) ?: $this->default_conf['text'];
        }
        $sign = ($params['sign'] ?? null) ?: ($this->conf['sign'] ?? null) ?: $this->default_conf['sign'];
        $data[$phone] = [
            'sign' => $sign,
            'number' => $phone,
            'message' => $text,
        ];
        $sms_copy_to = $this->copy_to;
        if ($sms_copy_to) {
            if ( ! is_array($sms_copy_to)) {
                $sms_copy_to = [$sms_copy_to];
            }
            foreach ((array) $sms_copy_to as $phone_copy_to) {
                $phone_copy_error = '';
                $phone_copy_to = $this->_phone_cleanup($phone_copy_to, $phone_copy_error);
                if ( ! $phone_copy_to || $phone_copy_error) {
                    continue;
                }
                if ($phone_copy_to == $phone || isset($data[$phone])) {
                    continue;
                }
                $data[$phone] = [
                    'sign' => $sign,
                    'number' => $phone_copy_to,
                    'message' => $text,
                ];
            }
        }
        return $this->insert_data_with_sql($data);
    }

Methods starting with _ hooks and helpers; not routable as page actions

__call

public function __call($name, $args)

Catch missing method call.

    public function __call($name, $args)
    {
        // Support for driver-specific methods
        if (is_object($this->_connection) && method_exists($this->_connection, $name)) {
            return call_user_func_array([$this->_connection, $name], $args);
        }
        return main()->extend_call($this, $name, $args);
    }

_is_enabled

public function _is_enabled($params = [])
    public function _is_enabled($params = [])
    {
        if ((DEBUG_MODE || (defined('TEST_MODE') && TEST_MODE)) && ! (DEBUG_MODE && main()->_get('object') == 'test_sms')) {
            return false;
        }
        return true;
    }
public function _phone_cleanup($phone = '', &$error = '')
    public function _phone_cleanup($phone = '', &$error = '')
    {
        $error = false;
        $phone = preg_replace('/[^0-9]+/ims', '', strip_tags($phone));
        // TODO: implement $this->allowed_countries
        // 063 123 45 67 (spaces here for example readability)
        if (strlen($phone) == 10) {
            $phone = '+38' . $phone;
            // 63 123 45 67 (spaces here for example readability)
        } elseif (strlen($phone) == 9) {
            $phone = '+380' . $phone;
        } elseif (strlen($phone) == 12) {
            if (substr($phone, 0, 2) != '38') {
                $error = 'phone error: incorrect country: ' . $phone;
            } else {
                $phone = '+' . $phone;
            }
        } else {
            $error = 'phone error: number is incorrect: ' . $phone;
        }
        $phone = '+' . ltrim($phone, '+');
        return $phone;
    }