Example #1
0
/**
 * Intercept on after-process stage for incoming SMS and forward it to selected user's inbox
 *
 * @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 $feature feature
 *        managed to hook current incoming SMS
 * @param $status recvsms()
 *        status, 0 or FALSE for unhandled
 * @param $uid keyword
 *        owner
 * @return array $ret
 */
function incoming_hook_recvsms_intercept_after($sms_datetime, $sms_sender, $message, $sms_receiver, $feature, $status, $uid, $smsc)
{
    global $core_config;
    $ret = array();
    $users = array();
    $is_routed = FALSE;
    // continue only if its sandbox
    if ($status) {
        return $ret;
    }
    // get settings
    $settings = incoming_settings_get();
    // get post rules
    $post_rules = incoming_post_rules_get();
    // sandbox match receiver number and sender ID
    if (!$is_routed) {
        // route sandbox if receiver number matched with default sender ID of users
        if ($post_rules['match_sender_id']) {
            $s = array();
            if ($settings['match_all_sender_id']) {
                // get all approved sender ID
                $s = sender_id_getall();
            } else {
                $data = user_search($sms_receiver, 'sender');
                foreach ($data as $user) {
                    // get default sender ID
                    if ($user['sender']) {
                        $s[] = $user['sender'];
                        // in case an error occured where multiple users own the same sender ID
                        break;
                    }
                }
            }
            // start matching
            foreach ($s as $sender_id) {
                if ($sender_id && $sms_receiver && $sender_id == $sms_receiver) {
                    unset($usernames);
                    unset($username);
                    if ($settings['match_all_sender_id']) {
                        // get $username who owns $sender_id
                        $uids = sender_id_owner($sender_id);
                        foreach ($uids as $uid) {
                            $usernames[] = user_uid2username($uid);
                        }
                    } else {
                        $usernames[] = $user['username'];
                    }
                    $usernames = array_unique($usernames);
                    foreach ($usernames as $username) {
                        if ($username) {
                            _log("sandbox match sender start u:" . $username . " dt:" . $sms_datetime . " s:" . $sms_sender . " r:" . $sms_receiver . " m:[" . $message . "]", 3, 'incoming recvsms_intercept_after');
                            recvsms_inbox_add($sms_datetime, $sms_sender, $username, $message, $sms_receiver);
                            _log("sandbox match sender end u:" . $username, 3, 'incoming recvsms_intercept_after');
                            $is_routed = TRUE;
                            // single match only
                            // break;
                        }
                    }
                }
            }
        }
        // sandbox prefix
        if (!$is_routed) {
            // route sandbox by adding a prefix to message and re-enter it to recvsms()
            //if ($post_rules['insert_prefix'] && trim($message)) {
            //	$message = $post_rules['insert_prefix'] . ' ' . trim($message);
            //	_log("sandbox add prefix start keyword:" . $post_rules['insert_prefix'] . " dt:" . $sms_datetime . " s:" . $sms_sender . " r:" . $sms_receiver . " m:" . $message, 3, 'incoming recvsms_intercept_after');
            //	recvsms($sms_datetime, $sms_sender, $message, $sms_receiver, $smsc);
            //	_log("sandbox add prefix end keyword:" . $post_rules['insert_prefix'], 3, 'incoming recvsms_intercept_after');
            //	$is_routed = TRUE;
            //}
        }
        // sandbox forward to users
        if (!$is_routed) {
            foreach ($post_rules['forward_to'] as $uid) {
                $c_username = user_uid2username($uid);
                if ($c_username) {
                    $users[] = $c_username;
                }
            }
            // route sandbox to users inbox
            foreach ($users as $username) {
                _log("sandbox to user start u:" . $username . " dt:" . $sms_datetime . " s:" . $sms_sender . " r:" . $sms_receiver . " m:[" . $message . "]", 3, 'incoming recvsms_intercept_after');
                recvsms_inbox_add($sms_datetime, $sms_sender, $username, $message, $sms_receiver);
                _log("sandbox to user end u:" . $username, 3, 'incoming recvsms_intercept_after');
                $is_routed = TRUE;
            }
        }
        // sandbox forward to URL
        if ($url = trim($post_rules['forward_to_url'])) {
            $payload = array('datetime' => core_get_datetime(), 'sms_datetime' => $sms_datetime, 'sms_sender' => $sms_sender, 'message' => $message, 'sms_receiver' => $sms_receiver, 'smsc' => $smsc);
            $json = json_encode($payload);
            $url = str_replace('{SANDBOX_PAYLOAD}', urlencode($json), $url);
            $url = str_replace('{SANDBOX_DATETIME}', urlencode($sms_datetime), $url);
            $url = str_replace('{SANDBOX_SENDER}', urlencode($sms_sender), $url);
            $url = str_replace('{SANDBOX_MESSAGE}', urlencode($message), $url);
            $url = str_replace('{SANDBOX_RECEIVER}', urlencode($sms_receiver), $url);
            $url = str_replace('{SANDBOX_SMSC}', urlencode($smsc), $url);
            _log("sandbox forward to URL start url:[" . $url . "]", 3, 'incoming recvsms_intercept_after');
            $response = @file_get_contents($url);
            _log("sandbox forward to URL end response:[" . $response . "]", 3, 'incoming recvsms_intercept_after');
        }
    }
    // flag the hook if is_routed
    if ($is_routed) {
        $ret['param']['feature'] = 'incoming';
        if ($settings['leave_copy_sandbox']) {
            $ret['param']['status'] = 0;
        } else {
            $ret['param']['status'] = 1;
        }
        $ret['param']['uid'] = 1;
        $ret['modified'] = TRUE;
    }
    return $ret;
}
Example #2
0
/**
 * Intercept on after-process stage for incoming SMS and forward it to selected user's inbox
 *
 * @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 $feature feature
 *        managed to hook current incoming SMS
 * @param $status recvsms()
 *        status, 0 or FALSE for unhandled
 * @param $uid keyword
 *        owner
 * @return array $ret
 */
function incoming_hook_recvsms_intercept_after($sms_datetime, $sms_sender, $message, $sms_receiver, $feature, $status, $uid, $smsc)
{
    global $core_config;
    $ret = array();
    $users = array();
    $is_routed = FALSE;
    if (!$status) {
        // get settings
        $settings = incoming_settings_get();
        // get post rules
        $post_rules = incoming_post_rules_get();
        // sandbox match receiver number and sender ID
        if (!$is_routed) {
            // route sandbox if receiver number matched with default sender ID of users
            if ($post_rules['match_sender_id']) {
                $s = array();
                if ($settings['match_all_sender_id']) {
                    // get all approved sender ID
                    $s = sender_id_getall();
                } else {
                    $data = user_search($sms_receiver, 'sender');
                    foreach ($data as $user) {
                        // get default sender ID
                        if ($user['sender']) {
                            $s[] = $user['sender'];
                            // in case an error occured where multiple users own the same sender ID
                            break;
                        }
                    }
                }
                // start matching
                foreach ($s as $sender_id) {
                    if ($sender_id && $sms_receiver && $sender_id == $sms_receiver) {
                        unset($usernames);
                        unset($username);
                        if ($settings['match_all_sender_id']) {
                            // get $username who owns $sender_id
                            $uids = sender_id_owner($sender_id);
                            foreach ($uids as $uid) {
                                $usernames[] = user_uid2username($uid);
                            }
                        } else {
                            $usernames[] = $user['username'];
                        }
                        array_unique($usernames);
                        foreach ($usernames as $username) {
                            if ($username) {
                                _log("sandbox match sender start u:" . $username . " dt:" . $sms_datetime . " s:" . $sms_sender . " r:" . $sms_receiver . " m:[" . $message . "]", 3, 'incoming recvsms_intercept_after');
                                recvsms_inbox_add($sms_datetime, $sms_sender, $username, $message, $sms_receiver);
                                _log("sandbox match sender end u:" . $username, 3, 'incoming recvsms_intercept_after');
                                $is_routed = TRUE;
                                // single match only
                                // break;
                            }
                        }
                    }
                }
            }
        }
        // sandbox prefix
        if (!$is_routed) {
            // route sandbox by adding a prefix to message and re-enter it to recvsms()
            /**
             * if ($post_rules['insert_prefix'] && trim($message)) {
             * $message = $post_rules['insert_prefix'] .
             *
             *
             * ' ' . trim($message);
             * _log("sandbox add prefix start keyword:" . $post_rules['insert_prefix'] . " dt:" . $sms_datetime . " s:" . $sms_sender . " r:" . $sms_receiver . " m:" . $message, 3, 'incoming recvsms_intercept_after');
             * recvsms($sms_datetime, $sms_sender, $message, $sms_receiver, $smsc);
             * _log("sandbox add prefix end keyword:" . $post_rules['insert_prefix'], 3, 'incoming recvsms_intercept_after');
             * $is_routed = TRUE;
             * }
             */
        }
        // sandbox forward to users
        if (!$is_routed) {
            foreach ($post_rules['forward_to'] as $uid) {
                $c_username = user_uid2username($uid);
                if ($c_username) {
                    $users[] = $c_username;
                }
            }
            // route sandbox to users inbox
            foreach ($users as $username) {
                _log("sandbox to user start u:" . $username . " dt:" . $sms_datetime . " s:" . $sms_sender . " r:" . $sms_receiver . " m:[" . $message . "]", 3, 'incoming recvsms_intercept_after');
                recvsms_inbox_add($sms_datetime, $sms_sender, $username, $message, $sms_receiver);
                _log("sandbox to user end u:" . $username, 3, 'incoming recvsms_intercept_after');
                $is_routed = TRUE;
            }
        }
        // flag the hook if is_routed
        if ($is_routed) {
            $ret['param']['feature'] = 'incoming';
            if ($settings['leave_copy_sandbox']) {
                $ret['param']['status'] = 0;
            } else {
                $ret['param']['status'] = 1;
            }
            $ret['param']['uid'] = 1;
            $ret['modified'] = TRUE;
        }
    }
    return $ret;
}