function simplerate_hook_rate_deduct($smslog_id) { global $core_config; logger_print("enter smslog_id:" . $smslog_id, 2, "simplerate deduct"); $db_query = "SELECT p_dst,p_footer,p_msg,uid,unicode FROM " . _DB_PREF_ . "_tblSMSOutgoing WHERE smslog_id='{$smslog_id}'"; $db_result = dba_query($db_query); if ($db_row = dba_fetch_array($db_result)) { $p_dst = $db_row['p_dst']; $p_msg = $db_row['p_msg']; $p_footer = $db_row['p_footer']; $uid = $db_row['uid']; $unicode = $db_row['unicode']; if ($p_dst && $p_msg && $uid) { // get charge $p_msg_len = strlen($p_msg) + strlen($p_footer); list($count, $rate, $charge) = rate_getcharges($p_msg_len, $unicode, $p_dst); // sender's $username = user_uid2username($uid); $credit = rate_getusercredit($username); $balance = $credit - $charge; // parent's when sender is a subuser $parent_uid = user_getparentbyuid($uid); if ($parent_uid) { $username_parent = user_uid2username($parent_uid); $credit_parent = rate_getusercredit($username_parent); $balance_parent = $credit_parent - $charge; } // if sender have parent then deduct parent first if ($parent_uid) { if (!rate_setusercredit($parent_uid, $balance_parent)) { return FALSE; } logger_print("parent uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id . " msglen:" . $p_msg_len . " count:" . $count . " rate:" . $rate . " charge:" . $charge . " credit_parent:" . $credit_parent . " balance_parent:" . $balance_parent, 2, "simplerate deduct"); } if (rate_setusercredit($uid, $balance)) { logger_print("user uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id . " msglen:" . $p_msg_len . " count:" . $count . " rate:" . $rate . " charge:" . $charge . " credit:" . $credit . " balance:" . $balance, 2, "simplerate deduct"); if (billing_post($smslog_id, $rate, $credit, $count, $charge)) { logger_print("deduct successful uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate deduct"); return TRUE; } else { logger_print("deduct failed uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate deduct"); return FALSE; } } } } return FALSE; }
/** * Send SMS * * @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 * @return array array($status, $sms_to, $smslog_id, $queue, $counts) */ function sendsms($username, $sms_to, $message, $sms_type = 'text', $unicode = 0, $smsc = '', $nofooter = false, $sms_footer = '', $sms_sender = '', $sms_schedule = '') { global $core_config, $user_config; // get user data $user = $user_config; if ($username && $user['username'] != $username) { $user = user_getdatabyusername($username); } if (!is_array($sms_to)) { $sms_to = explode(',', $sms_to); } $uid = $user['uid']; // discard if banned if (user_banned_get($uid)) { logger_print("user banned, exit immediately uid:" . $uid, 2, "sendsms"); return array(FALSE, '', '', '', ''); } // SMS sender ID $sms_sender = core_sanitize_sender($sms_sender); $sms_sender = $sms_sender && sender_id_isvalid($username, $sms_sender) ? $sms_sender : sendsms_get_sender($username); // SMS footer $sms_footer = core_sanitize_footer($sms_footer); $sms_footer = $sms_footer ? $sms_footer : $user['footer']; if ($nofooter) { $sms_footer = ''; } // a hack to remove \r from \r\n // the issue begins with ENTER being \r\n and detected as 2 chars // and since the javascript message counter can't detect it as 2 chars // thus the message length counts is inaccurate $message = str_replace("\r\n", "\n", $message); // just to make sure its length, we need to stripslashes message before enter other procedures $sms_sender = stripslashes($sms_sender); $message = stripslashes($message); $sms_footer = stripslashes($sms_footer); // fixme anton - fix #71 but not sure whats the correct solution for this // $max_length = ( $unicode ? $user['opt']['max_sms_length_unicode'] : $user['opt']['max_sms_length'] ); $max_length = $user['opt']['max_sms_length']; if (strlen($message) > $max_length) { $message = substr($message, 0, $max_length); } $sms_msg = $message; logger_print("start uid:" . $uid . " sender_id:[" . $sms_sender . "] smsc:[" . $smsc . "]", 2, "sendsms"); // add a space infront of footer if exists $c_sms_footer = trim($sms_footer) ? ' ' . trim($sms_footer) : ''; logger_print("maxlen:" . $max_length . " footerlen:" . strlen($c_sms_footer) . " footer:[" . $c_sms_footer . "] msglen:" . strlen($sms_msg) . " message:[" . $sms_msg . "]", 3, "sendsms"); // create a queue $queue_code = sendsms_queue_create($sms_sender, $sms_footer, $sms_msg, $uid, 0, $sms_type, $unicode, $sms_schedule, $smsc); if (!$queue_code) { // when unable to create a queue then immediately returns FALSE, no point to continue logger_print("fail to finalize queue creation, exit immediately", 2, "sendsms"); return array(FALSE, '', '', '', ''); } if (is_array($sms_to)) { $array_sms_to = $sms_to; } else { $array_sms_to = explode(',', $sms_to); } // get manipulated and valid destination numbers $all_sms_to = array(); for ($i = 0; $i < count($array_sms_to); $i++) { if ($c_sms_to = sendsms_getvalidnumber(trim($array_sms_to[$i]))) { $c_sms_to = sendsms_manipulate_prefix(trim($c_sms_to), $user); $all_sms_to[] = $c_sms_to; } } // remove double entries $all_sms_to = array_unique($all_sms_to); // calculate total sms and charges $total_count = 0; $total_charges = 0; foreach ($all_sms_to as $c_sms_to) { list($count, $rate, $charge) = rate_getcharges(strlen($message . $c_sms_footer), $unicode, $c_sms_to); $total_count += $count; $total_charges += $charge; } _log('dst:' . count($all_sms_to) . ' sms_count:' . $total_count . ' total_charges:' . $total_charges, 2, 'sendsms'); // sender's $credit = rate_getusercredit($user['username']); $balance = $credit - $total_charges; // parent's when sender is a subuser $parent_uid = user_getparentbyuid($user['uid']); if ($parent_uid) { $username_parent = user_uid2username($parent_uid); $credit_parent = rate_getusercredit($username_parent); $balance_parent = $credit_parent - $total_charges; } // default returns for ($i = 0; $i < count($all_sms_to); $i++) { $ok[$i] = FALSE; $to[$i] = $all_sms_to[$i]; $smslog_id[$i] = 0; $queue[$i] = $queue_code; $counts[$i] = $count; } if ($parent_uid) { if (!($balance_parent >= 0)) { _log('failed parent do not have enough credit. credit:' . $credit_parent . ' dst:' . count($all_sms_to) . ' sms_count:' . $total_count . ' total_charges:' . $total_charges, 2, 'sendsms'); return array($ok, $to, $smslog_id, $queue, $counts); } } else { if (!($balance >= 0)) { _log('failed user do not have enough credit. credit:' . $credit_parent . ' dst:' . count($all_sms_to) . ' sms_count:' . $total_count . ' total_charges:' . $total_charges, 2, 'sendsms'); return array($ok, $to, $smslog_id, $queue, $counts); } } $queue_count = 0; $sms_count = 0; $failed_queue_count = 0; $failed_sms_count = 0; for ($i = 0; $i < count($all_sms_to); $i++) { $c_sms_to = $all_sms_to[$i]; if ($smslog_id[$i] = sendsms_queue_push($queue_code, $c_sms_to)) { $ok[$i] = TRUE; $queue_count++; $sms_count = $sms_count + $count; } else { $ok[$i] = FALSE; $failed_queue_count++; $failed_sms_count++; } $to[$i] = $c_sms_to; $queue[$i] = $queue_code; $counts[$i] = $count; } if (sendsms_queue_update($queue_code, array('flag' => '0', 'sms_count' => $sms_count))) { logger_print("end queue_code:" . $queue_code . " queue_count:" . $queue_count . " sms_count:" . $sms_count . " failed_queue:" . $failed_queue_count . " failed_sms:" . $failed_sms_count, 2, "sendsms"); } else { logger_print("fail to prepare queue, exit immediately queue_code:" . $queue_code, 2, "sendsms"); return array(FALSE, '', '', $queue_code, ''); } if (!$core_config['issendsmsd']) { unset($ok); unset($to); unset($queue); unset($counts); logger_print("sendsmsd off immediately process queue_code:" . $queue_code, 2, "sendsms"); list($ok, $to, $smslog_id, $queue, $counts) = sendsmsd($queue_code); } return array($ok, $to, $smslog_id, $queue, $counts); }
function simplerate_hook_rate_deduct($smslog_id) { global $core_config; logger_print("enter smslog_id:" . $smslog_id, 2, "simplerate deduct"); $db_query = "SELECT p_dst,p_footer,p_msg,uid,unicode FROM " . _DB_PREF_ . "_tblSMSOutgoing WHERE smslog_id='{$smslog_id}'"; $db_result = dba_query($db_query); if ($db_row = dba_fetch_array($db_result)) { $p_dst = $db_row['p_dst']; $p_msg = $db_row['p_msg']; $p_footer = $db_row['p_footer']; $uid = $db_row['uid']; $unicode = $db_row['unicode']; if ($p_dst && $p_msg && $uid) { // get charge $p_msg_len = strlen($p_msg) + strlen($p_footer); list($count, $rate, $charge) = rate_getcharges($uid, $p_msg_len, $unicode, $p_dst); // sender's $username = user_uid2username($uid); $credit = rate_getusercredit($username); $balance = $credit - $charge; // parent's when sender is a subuser $parent_uid = user_getparentbyuid($uid); if ($parent_uid) { $username_parent = user_uid2username($parent_uid); $credit_parent = rate_getusercredit($username_parent); $balance_parent = $credit_parent - $charge; } // if sender have parent then deduct parent first if ($parent_uid) { if (!rate_setusercredit($parent_uid, $balance_parent)) { return FALSE; } logger_print("parent uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id . " msglen:" . $p_msg_len . " count:" . $count . " rate:" . $rate . " charge:" . $charge . " credit_parent:" . $credit_parent . " balance_parent:" . $balance_parent, 2, "simplerate deduct"); } if (rate_setusercredit($uid, $balance)) { logger_print("user uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id . " msglen:" . $p_msg_len . " count:" . $count . " rate:" . $rate . " charge:" . $charge . " credit:" . $credit . " balance:" . $balance, 2, "simplerate deduct"); if (billing_post($smslog_id, $rate, $credit, $count, $charge)) { logger_print("deduct successful uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate deduct"); // if balance under credit lowest limit and never been notified then notify admins, parent_uid and uid $credit_lowest_limit = (double) $core_config['main']['credit_lowest_limit']; _log('credit_lowest_limit:' . $credit_lowest_limit . ' balance:' . $balance . ' charge:' . $charge, 3, 'simplerate deduct'); $reg = registry_search($uid, 'feature', 'credit', 'lowest_limit_notif'); $notified = $reg['feature']['credit']['lowest_limit_notif'] ? TRUE : FALSE; if ($charge && $balance && $credit_lowest_limit && $balance <= $credit_lowest_limit && !$notified) { // set notified registry_update($uid, 'feature', 'credit', array('lowest_limit_notif' => TRUE)); // notif admins $admins = user_getallwithstatus(2); foreach ($admins as $admin) { $credit_message_to_admins = sprintf(_('Username %s with account ID %d has reached lowest credit limit of %s'), $username, $uid, $credit_lowest_limit); recvsms_inbox_add(core_get_datetime(), _SYSTEM_SENDER_ID_, $admin['username'], $credit_message_to_admins); } // notif parent_uid if exists if ($parent_uid && $username_parent) { $credit_message_to_parent = sprintf(_('Your subuser with username %s and account ID %d has reached lowest credit limit of %s'), $username, $uid, $credit_lowest_limit); recvsms_inbox_add(core_get_datetime(), _SYSTEM_SENDER_ID_, $username_parent, $credit_message_to_parent); } // notif uid $sender_username = $username_parent ? $username_parent : _SYSTEM_SENDER_ID_; $credit_message_to_self = sprintf(_('You have reached lowest credit limit of %s'), $credit_lowest_limit); recvsms_inbox_add(core_get_datetime(), $sender_username, $username, $credit_message_to_self); _log('sent notification credit_lowest_limit:' . $credit_lowest_limit, 3, 'simplerate deduct'); } return TRUE; } else { logger_print("deduct failed uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate deduct"); return FALSE; } } else { logger_print("rate deduct failed due to unable to save to db uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate deduct"); } } else { logger_print("rate deduct failed due to empty data uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate deduct"); } } else { logger_print("rate deduct failed due to missing data uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id, 3, "simplerate deduct"); } return FALSE; }