Beispiel #1
0
function recvsms_process($sms_datetime, $sms_sender, $message, $sms_receiver = '', $smsc = '')
{
    global $core_config;
    // blacklist
    if (blacklist_mobile_isexists(0, $sms_sender)) {
        logger_print("incoming SMS discarded sender is in the blacklist datetime:" . $sms_datetime . " sender:" . $sms_sender . " receiver:" . $sms_receiver . " message:[" . $message . "]  smsc:" . $smsc, 3, "recvsms_process");
        return false;
    }
    // incoming sms will be handled by plugins first
    $ret_intercept = recvsms_intercept($sms_datetime, $sms_sender, $message, $sms_receiver, $smsc);
    if ($ret_intercept['modified']) {
        $sms_datetime = $ret_intercept['param']['sms_datetime'] ? $ret_intercept['param']['sms_datetime'] : $sms_datetime;
        $sms_sender = $ret_intercept['param']['sms_sender'] ? $ret_intercept['param']['sms_sender'] : $sms_sender;
        $message = $ret_intercept['param']['message'] ? $ret_intercept['param']['message'] : $message;
        $sms_receiver = $ret_intercept['param']['sms_receiver'] ? $ret_intercept['param']['sms_receiver'] : $sms_receiver;
        $smsc = $ret_intercept['param']['smsc'] ? $ret_intercept['param']['smsc'] : $smsc;
    }
    // set active gateway module as default gateway
    // if (!$smsc) {
    // $smsc = core_smsc_get();
    // }
    // log it
    logger_print("dt:" . $sms_datetime . " sender:" . $sms_sender . " m:" . $message . " receiver:" . $sms_receiver . ' smsc:' . $smsc, 3, "recvsms_process");
    // if hooked function returns cancel=true then stop the processing incoming sms, return false
    if ($ret_intercept['cancel']) {
        logger_print("cancelled datetime:" . $sms_datetime . " sender:" . $sms_sender . " receiver:" . $sms_receiver . " message:[" . $message . "]  smsc:" . $smsc, 3, "recvsms_process");
        return false;
    }
    $c_uid = 0;
    $c_feature = "";
    $ok = false;
    $keyword_separator = $core_config['main']['keyword_separator'] ? $core_config['main']['keyword_separator'] : ' ';
    $array_target_keyword = explode($keyword_separator, $message);
    $target_keyword = strtoupper(trim($array_target_keyword[0]));
    $raw_message = $message;
    $message = $array_target_keyword[1];
    for ($i = 2; $i < count($array_target_keyword); $i++) {
        $message .= " " . $array_target_keyword[$i];
    }
    switch ($target_keyword) {
        case "BC":
            $c_uid = user_mobile2uid($sms_sender);
            $c_username = user_uid2username($c_uid);
            $c_feature = 'core';
            $array_target_group = explode(" ", $message);
            $target_group = strtoupper(trim($array_target_group[0]));
            $list = phonebook_search_group($c_uid, $target_group, '', TRUE);
            $c_gpid = $list[0]['gpid'];
            $message = $array_target_group[1];
            for ($i = 2; $i < count($array_target_group); $i++) {
                $message .= " " . $array_target_group[$i];
            }
            logger_print("bc username:"******" gpid:" . $c_gpid . " sender:" . $sms_sender . " receiver:" . $sms_receiver . " message:" . $message . " raw:" . $raw_message, 3, "recvsms_process");
            if ($c_username && $c_gpid && $message) {
                list($ok, $to, $smslog_id, $queue) = sendsms_bc($c_username, $c_gpid, $message);
                $ok = true;
            } else {
                _log('bc has failed due to missing option u:' . $c_username . ' gpid:' . $c_gpid . ' m:[' . $message . ']', 3, 'recvsms_process');
            }
            break;
        default:
            for ($c = 0; $c < count($core_config['plugins']['list']['feature']); $c++) {
                $c_feature = $core_config['plugins']['list']['feature'][$c];
                $ret = core_hook($c_feature, 'recvsms_process', array($sms_datetime, $sms_sender, $target_keyword, $message, $sms_receiver, $smsc, $raw_message));
                if ($ok = $ret['status']) {
                    $c_uid = $ret['uid'];
                    logger_print("feature:" . $c_feature . " datetime:" . $sms_datetime . " sender:" . $sms_sender . " receiver:" . $sms_receiver . " keyword:" . $target_keyword . " message:" . $message . " raw:" . $raw_message . " smsc:" . $smsc, 3, "recvsms_process");
                    break;
                }
            }
    }
    $c_status = $ok ? 1 : 0;
    if ($c_status == 0) {
        $c_feature = '';
        $target_keyword = '';
        $message = $raw_message;
        // from recvsms_intercept(), force status as 'handled'
        if ($ret_intercept['hooked']) {
            $c_status = 1;
            if ($ret_intercept['uid']) {
                $c_uid = $ret_intercept['uid'];
            }
            logger_print("intercepted datetime:" . $sms_datetime . " sender:" . $sms_sender . " receiver:" . $sms_receiver . " message:" . $message, 3, "recvsms_process");
        } else {
            logger_print("unhandled datetime:" . $sms_datetime . " sender:" . $sms_sender . " receiver:" . $sms_receiver . " message:" . $message, 3, "recvsms_process");
        }
    }
    // incoming sms intercept after
    unset($ret_intercept);
    $ret_intercept = recvsms_intercept_after($sms_datetime, $sms_sender, $message, $sms_receiver, $c_feature, $c_status, $c_uid, $smsc);
    if ($ret_intercept['modified']) {
        $sms_datetime = $ret_intercept['param']['sms_datetime'] ? $ret_intercept['param']['sms_datetime'] : $sms_datetime;
        $sms_sender = $ret_intercept['param']['sms_sender'] ? $ret_intercept['param']['sms_sender'] : $sms_sender;
        $message = $ret_intercept['param']['message'] ? $ret_intercept['param']['message'] : $message;
        $sms_receiver = $ret_intercept['param']['sms_receiver'] ? $ret_intercept['param']['sms_receiver'] : $sms_receiver;
        $c_feature = $ret_intercept['param']['feature'] ? $ret_intercept['param']['feature'] : $c_feature;
        $c_status = $ret_intercept['param']['status'] ? $ret_intercept['param']['status'] : $c_status;
        $c_uid = $ret_intercept['param']['uid'] ? $ret_intercept['param']['uid'] : $c_uid;
        $smsc = $ret_intercept['param']['smsc'] ? $ret_intercept['param']['smsc'] : $smsc;
    }
    // fixme anton - all incoming messages set to user with uid=1 if no one owns it
    $c_uid = $c_uid ? $c_uid : 1;
    $db_query = "\n\t\tINSERT INTO " . _DB_PREF_ . "_tblSMSIncoming\n\t\t(in_uid,in_feature,in_gateway,in_sender,in_receiver,in_keyword,in_message,in_datetime,in_status)\n\t\tVALUES\n\t\t('{$c_uid}','{$c_feature}','{$smsc}','{$sms_sender}','{$sms_receiver}','{$target_keyword}','{$message}','" . core_adjust_datetime($sms_datetime) . "','{$c_status}')";
    $db_result = dba_query($db_query);
    return $ok;
}
Beispiel #2
0
function phonebook_hook_webservices_output($operation, $requests)
{
    global $user_config;
    if (!auth_isvalid()) {
        return FALSE;
    }
    $keyword = stripslashes($requests['keyword']);
    if (!$keyword) {
        $keyword = $requests['tag'];
    }
    if ($keyword && $user_config['uid']) {
        if (substr($keyword, 0, 1) == '@') {
            $keyword = substr($keyword, 1);
            $list = phonebook_search_user($keyword);
            foreach ($list as $data) {
                $item[] = array('id' => '@' . $data['username'], 'text' => '@' . $data['name']);
            }
        } else {
            if (substr($keyword, 0, 1) == '#') {
                $keyword = substr($keyword, 1);
                $list = phonebook_search_group($user_config['uid'], $keyword);
                foreach ($list as $data) {
                    $item[] = array('id' => '#' . $data['code'], 'text' => _('Group') . ': ' . $data['group_name'] . ' (' . $data['code'] . ')');
                }
            } else {
                $list = phonebook_search($user_config['uid'], $keyword);
                foreach ($list as $data) {
                    $item[] = array('id' => $data['p_num'], 'text' => $data['p_desc'] . ' (' . $data['p_num'] . ')');
                }
            }
        }
    }
    if (count($item) == 0) {
        $item[] = array('id' => $keyword, 'text' => $keyword);
    }
    $content = json_encode($item);
    ob_end_clean();
    header('Content-Type: text/json; charset=utf-8');
    $ret = $content;
    return $ret;
}
Beispiel #3
0
/**
 * Intercept on before-process stage for incoming SMS
 *
 * @param $sms_datetime incoming
 *        SMS date/time
 * @param $sms_sender incoming
 *        SMS sender
 * @param $message incoming
 *        SMS message before interepted
 * @param $sms_receiver receiver
 *        number that is receiving incoming SMS
 * @param $reference_id reference_id
 *        data
 * @return array $ret
 */
function incoming_hook_recvsms_intercept($sms_datetime, $sms_sender, $message, $sms_receiver, $reference_id)
{
    $ret = array();
    $found_bc = FALSE;
    $found_pv = FALSE;
    // continue only when keyword does not exists
    $m = explode(' ', $message);
    if (!keyword_isavail($m[0])) {
        return $ret;
    }
    // get settings
    $settings = incoming_settings_get();
    // get post rules
    $pre_rules = incoming_pre_rules_get();
    // scan for #<sender's phonebook group code> and @<username> according to pre rules
    $msg = explode(' ', $message);
    if (count($msg) > 0) {
        $bc = array();
        $pv = array();
        for ($i = 0; $i < count($msg); $i++) {
            $c_text = trim($msg[$i]);
            // scan message for @username
            if ($pre_rules['match_username']) {
                if (substr($c_text, 0, 1) === '@') {
                    $pv[] = strtolower(substr($c_text, 1));
                    $found_pv = TRUE;
                }
            }
            // scan message for #groupcode
            if ($pre_rules['match_groupcode']) {
                if (substr($c_text, 0, 1) === '#') {
                    $bc[] = strtoupper(substr($c_text, 1));
                    $found_bc = TRUE;
                }
            }
        }
    }
    if ($found_bc || $found_pv) {
        _log("recvsms_intercept dt:" . $sms_datetime . " s:" . $sms_sender . " r:" . $sms_receiver . " m:" . $message, 3, 'incoming recvsms_intercept');
    }
    if ($found_bc) {
        $groups = array_unique($bc);
        foreach ($groups as $key => $c_group_code) {
            $c_uid = user_mobile2uid($sms_sender);
            $list = phonebook_search_group($c_uid, $c_group_code, '', TRUE);
            $c_gpid = $list[0]['gpid'];
            if ($c_uid && $c_gpid) {
                $c_username = user_uid2username($c_uid);
                _log("bc g:" . phonebook_code_clean($c_group_code) . " gpid:" . $c_gpid . " uid:" . $c_uid . " dt:" . $sms_datetime . " s:" . $sms_sender . " r:" . $sms_receiver . " m:" . $message, 3, 'incoming recvsms_intercept');
                sendsms_bc($c_username, $c_gpid, $message);
                _log("bc end", 3, 'incoming recvsms_intercept');
                $ret['uid'] = $c_uid;
                $ret['hooked'] = true;
            }
        }
    }
    if ($found_pv) {
        $users = array_unique($pv);
        foreach ($users as $key => $c_username) {
            $c_username = core_sanitize_username($c_username);
            if ($c_uid = user_username2uid($c_username)) {
                _log("pv u:" . $c_username . " uid:" . $c_uid . " dt:" . $sms_datetime . " s:" . $sms_sender . " r:" . $sms_receiver . " m:[" . $message . "] reference_id:" . $reference_id, 3, 'incoming recvsms_intercept');
                recvsms_inbox_add($sms_datetime, $sms_sender, $c_username, $message, $sms_receiver, $reference_id);
                _log("pv end", 3, 'incoming recvsms_intercept');
                $ret['uid'] = $c_uid;
                $ret['hooked'] = true;
            }
        }
    }
    return $ret;
}
Beispiel #4
0
function webservices_get_contact_group($c_uid, $name, $count)
{
    $list = phonebook_search_group($c_uid, $name, $count);
    $json['status'] = 'OK';
    $json['error'] = '0';
    $json['data'] = $list;
    return $json;
}
Beispiel #5
0
/**
 * Send SMS helper
 *
 * @global array $core_config, $user_config
 * @param string $username        
 * @param mixed $sms_to        
 * @param string $message        
 * @param string $sms_type        
 * @param integer $unicode        
 * @param string $smsc        
 * @param boolean $nofooter        
 * @param string $sms_footer        
 * @param string $sms_sender        
 * @param string $sms_schedule        
 * @param string $reference_id        
 * @return array array($status, $sms_to, $smslog_id, $queue, $counts, $sms_count, $sms_failed, $error_strings)
 */
function sendsms_helper($username, $sms_to, $message, $sms_type = 'text', $unicode = 0, $smsc = '', $nofooter = false, $sms_footer = '', $sms_sender = '', $sms_schedule = '', $reference_id = '')
{
    global $core_config, $user_config;
    // htmlspecialchars_decode to message and footer
    $message = htmlspecialchars_decode($message);
    $sms_footer = htmlspecialchars_decode($sms_footer);
    // get user data
    if ($username && $user_config['username'] != $username) {
        $user_config = user_getdatabyusername($username);
    }
    if (!is_array($sms_to)) {
        $sms_to = explode(',', $sms_to);
    }
    // get destinations
    for ($i = 0; $i < count($sms_to); $i++) {
        if (substr(trim($sms_to[$i]), 0, 1) == '#') {
            if ($c_group_code = substr(trim($sms_to[$i]), 1)) {
                $list = phonebook_search_group($user_config['uid'], $c_group_code, '', TRUE);
                $c_gpid = $list[0]['gpid'];
                $members = phonebook_getdatabyid($c_gpid);
                foreach ($members as $member) {
                    if ($c_sms_to = trim($member['p_num'])) {
                        $array_sms_to[] = trim($c_sms_to);
                    }
                }
            }
        } else {
            if (substr(trim($sms_to[$i]), 0, 1) == '@') {
                if ($c_username = substr(trim($sms_to[$i]), 1)) {
                    // reference self will be ignored
                    if ($c_username != $user_config['username']) {
                        $array_username[] = $c_username;
                    }
                }
            } else {
                $array_sms_to[] = trim($sms_to[$i]);
            }
        }
    }
    // remove duplicates destinations
    $array_sms_to = array_unique($array_sms_to, SORT_STRING);
    $sms_queued = 0;
    $sms_failed = 0;
    // sendsms
    if (is_array($array_sms_to) && $array_sms_to[0]) {
        list($ok, $to, $smslog_id, $queue, $counts, $error_strings) = sendsms($user_config['username'], $array_sms_to, $message, $sms_type, $unicode, $smsc, $nofooter, $sms_footer, $sms_sender, $sms_schedule);
    }
    // fixme anton - IMs doesn't count
    // count SMSes only
    for ($i = 0; $i < count($ok); $i++) {
        if ($ok[$i]) {
            $sms_count += $counts[$i];
        } else {
            $sms_failed += $counts[$i];
        }
    }
    // sendsms_im
    if (is_array($array_username) && $array_username[0]) {
        $im_sender = '@' . $user_config['username'];
        foreach ($array_username as $target_user) {
            $im_sender = '@' . $user_config['username'];
            if (recvsms_inbox_add(core_get_datetime(), $im_sender, $target_user, $message, '', $reference_id)) {
                $ok[] = '1';
                $to[] = '@' . $target_user;
                $queue[] = md5($target_user . microtime());
                $sms_count++;
            }
        }
    }
    return array($ok, $to, $smslog_id, $queue, $counts, $sms_count, $sms_failed, $error_strings);
}
Beispiel #6
0
function phonebook_hook_webservices_output($operation, $requests, $returns)
{
    global $user_config;
    $keyword = stripslashes($requests['keyword']);
    if (!$keyword) {
        $keyword = $requests['tag'];
    }
    if (!($operation == 'phonebook' && $keyword)) {
        return FALSE;
    }
    if (!auth_isvalid()) {
        return FALSE;
    }
    if ($returns['modified'] && $returns['param']['operation'] == 'phonebook') {
        $item = json_decode($returns['param']['content'], TRUE);
    } else {
        $item = array();
    }
    if ($keyword && $user_config['uid']) {
        if (substr($keyword, 0, 1) == '@') {
            $keyword = substr($keyword, 1);
            $list = phonebook_search_user($user_config['uid'], $keyword);
            foreach ($list as $data) {
                $item[] = array('id' => '@' . $data['username'], 'text' => '@' . $data['name']);
            }
        } else {
            if (substr($keyword, 0, 1) == '#') {
                $keyword = substr($keyword, 1);
                $list = phonebook_search_group($user_config['uid'], $keyword);
                foreach ($list as $data) {
                    $item[] = array('id' => '#' . $data['code'], 'text' => _('Group') . ': ' . $data['group_name'] . ' (' . $data['code'] . ')');
                }
            } else {
                $list = phonebook_search($user_config['uid'], $keyword);
                foreach ($list as $data) {
                    $item[] = array('id' => $data['p_num'], 'text' => $data['p_desc'] . ' (' . $data['p_num'] . ')');
                }
            }
        }
    }
    // safety net
    if (count($item) == 0) {
        $item[] = array('id' => $keyword, 'text' => $keyword);
    }
    $returns['modified'] = TRUE;
    $returns['param']['content'] = json_encode($item);
    if ($requests['debug'] == '1') {
        $returns['param']['content-type'] = "text/plain";
    }
    return $returns;
}