/** * Get user credit balance * * @param integer $uid * User ID * @return float User credit balance */ function credit_getbalance($uid) { $balance = 0; if ($username = user_uid2username($uid)) { $balance = rate_getusercredit($username); } $balance = (double) $balance; $balance = number_format($balance, 3, '.', ''); return $balance; }
function credit_hook_rate_addusercredit($uid, $amount) { global $plugin_config; $db_table = $plugin_config['credit']['db_table']; $parent_uid = user_getparentbyuid($uid); $username = user_uid2username($uid); $status = user_getfieldbyuid($uid, 'status'); $balance = (double) rate_getusercredit($username); $amount = (double) $amount; if (abs($amount) <= 0) { _log('amount cannot be zero. amount:[' . $amount . ']', 2, 'credit_hook_rate_addusercredit'); return FALSE; } // add to balance $balance = $balance + $amount; // record it $id = dba_add($db_table, array('parent_uid' => $parent_uid, 'uid' => $uid, 'username' => $username, 'status' => $status, 'create_datetime' => core_get_datetime(), 'amount' => $amount, 'balance' => $balance, 'flag_deleted' => 0)); // update user's credit if ($id) { _log('saved id:' . $id . ' parent_uid:' . $parent_uid . ' uid:' . $uid . ' username:'******' amount:' . $amount . ' balance:' . $balance, 3, 'credit_add'); if (rate_setusercredit($uid, $balance)) { _log('updated uid:' . $uid . ' credit:' . $balance, 3, 'credit_add'); return TRUE; } else { _log('fail to update uid:' . $uid . ' credit:' . $balance, 3, 'credit_add'); dba_remove($db_table, array('id' => $id)); return FALSE; } } else { _log('fail to save parent_uid:' . $parent_uid . ' uid:' . $uid . ' username:'******' amount:' . $amount . ' balance:' . $balance, 3, 'credit_add'); return FALSE; } }
switch (_OP_) { case "user_config": if ($c_user = dba_search(_DB_PREF_ . '_tblUser', '*', array('flag_deleted' => 0, 'uid' => $c_uid))) { $token = $c_user[0]['token']; $webservices_ip = $c_user[0]['webservices_ip']; $enable_webservices = $c_user[0]['enable_webservices']; $sender = core_sanitize_sender($c_user[0]['sender']); $footer = core_sanitize_footer($c_user[0]['footer']); $datetime_timezone = core_get_timezone($c_username); $fwd_to_inbox = $c_user[0]['fwd_to_inbox']; $fwd_to_email = $c_user[0]['fwd_to_email']; $fwd_to_mobile = $c_user[0]['fwd_to_mobile']; $local_length = $c_user[0]['local_length']; $replace_zero = $c_user[0]['replace_zero']; $acl_id = (int) $c_user[0]['acl_id']; $credit = rate_getusercredit($c_username); } else { $_SESSION['dialog']['info'][] = _('User does not exist') . ' (' . _('username') . ': ' . $uname . ')'; header("Location: " . _u('index.php?app=main&inc=core_user&route=user_mgmnt&op=user_list&view=' . $view)); exit; } // select enable_webservices if ($enable_webservices) { $selected_1 = 'selected'; $selected_0 = ''; } else { $selected_1 = ''; $selected_0 = 'selected'; } $option_enable_webservices = "<option value='1' " . $selected_1 . ">" . _('yes') . "</option>"; $option_enable_webservices .= "<option value='0' " . $selected_0 . ">" . _('no') . "</option>";
/** * 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); }
$name = username2name($uname); $status = username2status($uname); $sender = username2sender($uname); $footer = username2footer($uname); $timezone = username2timezone($uname); $language_module = username2lang($uname); // get language options for ($i = 0; $i < count($core_config['languagelist']); $i++) { $language = $core_config['languagelist'][$i]; if ($language == $language_module) { $selected = "selected"; } $option_language_module .= "<option value=\"{$language}\" {$selected}>{$language}</option>"; $selected = ""; } $credit = rate_getusercredit($uname); if ($err) { $content = "<p><font color='red'>{$err}</font><p>"; } if ($status == 2) { $selected_2 = "selected"; } if ($status == 3) { $selected_3 = "selected"; } $option_status = "\n\t <option value='2' {$selected_2}>" . _('Administrator') . "</option>\n\t <option value='3' {$selected_3}>" . _('Normal user') . "</option>\n\t"; $content .= "\n\t <h2>" . _('Preferences') . ": {$uname}</h2>\n\t <p>\n\t <form action='index.php?app=menu&inc=user_mgmnt&op=user_edit_save' method='post'>\n\t <input type='hidden' name='uname' value=\"{$uname}\">\n\t <table width='100%' cellpadding='1' cellspacing='2' border='0'>\n\t <tr>\n\t\t<td width='175'>" . _('Username') . " {$nd}</td><td width='5'>:</td><td><b>{$uname}</b></td>\n\t </tr>\n\t <tr>\n\t\t<td>" . _('Full name') . " {$nd}</td><td>:</td><td><input type='text' size='30' maxlength='30' name='up_name' value=\"{$name}\"></td>\n\t </tr>\t \t \n\t <tr>\n\t\t<td>" . _('Email') . " {$nd}</td><td>:</td><td><input type='text' size='30' maxlength='30' name='up_email' value=\"{$email}\"></td>\n\t </tr>\n\t <tr>\n\t\t<td>" . _('Mobile') . "</td><td>:</td><td><input type='text' size='16' maxlength='16' name='up_mobile' value=\"{$mobile}\"> (" . _('Max. 16 numeric or 11 alphanumeric characters') . ")</td>\n\t </tr>\n\t <tr>\n\t\t<td>" . _('SMS sender ID') . "</td><td>:</td><td><input type='text' size='16' maxlength='16' name='up_sender' value=\"{$sender}\"> (" . _('Max. 16 numeric or 11 alphanumeric characters') . ")</td>\n\t </tr>\n\t <tr>\n\t\t<td>" . _('SMS footer') . "</td><td>:</td><td><input type='text' size='30' maxlength='30' name='up_footer' value=\"{$footer}\"> (" . _('Max. 30 alphanumeric characters') . ")</td>\n\t </tr>\t \n\t <tr>\n\t\t<td>" . _('Timezone') . "</td><td>:</td><td><input type='text' size='5' maxlength='5' name='up_timezone' value=\"{$timezone}\"> (" . _('Eg: +0700 for Jakarta/Bangkok timezone') . ")</td>\n\t </tr>\n\t <tr>\n\t\t<td>" . _('Password') . "</td><td>:</td><td><input type='password' size='30' maxlength='30' name='up_password'> (" . _('Fill to change password for username') . " `{$uname}`)</td>\n\t </tr>\t \n\t <tr>\n\t\t<td>" . _('Credit') . "</td><td>:</td><td><input type='text' size='16' maxlength='30' name='up_credit' value=\"{$credit}\"></td>\n\t </tr>\t \n\t <tr>\n\t\t<td>" . _('User level') . "</td><td>:</td><td><select name='up_status'>{$option_status}</select></td>\n\t </tr>\n\t <tr>\n\t\t<td>" . _('Active language') . "</td><td>:</td><td><select name='up_language_module'>{$option_language_module}</select></td>\n\t </tr>\n\t </table>\t \n\t <p><input type='submit' class='button' value='" . _('Save') . "'>\n\t </form>\n\t"; echo $content; break; case "user_edit_save": $uname = $_POST['uname'];
function simplerate_hook_rate_refund($smslog_id) { $ok = false; logger_print("start smslog_id:" . $smslog_id, 3, "simplerate refund"); $db_query = "SELECT p_dst,p_msg,uid FROM " . _DB_PREF_ . "_tblSMSOutgoing WHERE p_status='2' AND 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']; $uid = $db_row['uid']; if ($p_dst && $p_msg && $uid) { if (billing_rollback($smslog_id)) { $bill = billing_getdata($smslog_id); $rate = $bill['rate']; $status = $bill['status']; logger_print("rolling smslog_id:" . $smslog_id, 3, "simplerate refund"); if ($status == '2') { // here should be added a routine to check charset encoding // utf8 devided by 140, ucs2 devided by 70 $count = ceil(strlen($p_msg) / 153); $charge = $count * $rate; $username = uid2username($uid); $credit = rate_getusercredit($username); $remaining = $credit + $charge; if (rate_setusercredit($uid, $remaining)) { logger_print("refund smslog_id:" . $smslog_id, 3, "simplerate refund"); $ok = true; } } } } } return $ok; }
function webservices_cr($c_username) { $credit = rate_getusercredit($c_username); $credit = $credit ? $credit : '0'; $ret = "OK " . $credit; return $ret; }
} } // remove user except those who still have subusers $subusers = user_getsubuserbyuid($list[$i]['uid']); if (count($subusers) > 0) { $action .= _hint(_('Please remove all subusers from this user to delete')); } else { $action .= "<a href=\"javascript: ConfirmURL('" . addslashes(_("Are you sure you want to delete user")) . " " . $list[$i]['username'] . " ?','" . _u('index.php?app=main&inc=core_user&route=user_mgmnt&op=user_del&uname=' . $list[$i]['username']) . "&view=" . $view . "')\">" . $icon_config['user_delete'] . "</a>"; } // subuser shows parent column if ($list[$i]['status'] == 4) { $isadmin = user_getfieldbyuid($list[$i]['parent_uid'], 'status') == 2 ? $icon_config['admin'] : ''; $parent_column_row = "<td>" . user_uid2username($list[$i]['parent_uid']) . " " . $isadmin . "</td>"; } $j--; $content .= "\n\t\t\t\t<tr>\n\t\t\t\t\t<td>" . core_display_datetime($list[$i]['register_datetime']) . "</td>\n\t\t\t\t\t" . $parent_column_row . "\n\t\t\t\t\t<td>" . $banned_icon . "" . $list[$i]['username'] . " </td>\n\t\t\t\t\t<td>" . $list[$i]['name'] . "</td>\n\t\t\t\t\t<td>" . $list[$i]['mobile'] . "</td>\n\t\t\t\t\t<td>" . rate_getusercredit($list[$i]['username']) . "</td>\n\t\t\t\t\t<td>" . acl_getnamebyuid($list[$i]['uid']) . "</td>\n\t\t\t\t\t<td>" . $action . "</td>\n\t\t\t\t</tr>"; } $content .= "\n\t\t\t</tbody></table>\n\t\t\t</div>\n\t\t\t<div class=pull-right>" . $nav['form'] . "</div>"; _p($content); break; case "user_add": if ($err = TRUE) { $content = _dialog(); } $add_datetime_timezone = $_REQUEST['add_datetime_timezone']; $add_datetime_timezone = $add_datetime_timezone ? $add_datetime_timezone : core_get_timezone(); // get language options $lang_list = ''; for ($i = 0; $i < count($core_config['languagelist']); $i++) { $language = $core_config['languagelist'][$i]; $c_language_title = $plugin_config[$language]['title'];
function webservices_credit_deduct($username, $amount) { $uid = user_username2uid($username); $amount = (double) $amount; if (rate_deductusercredit($uid, $amount)) { $json['status'] = 'OK'; $json['error'] = '0'; $json['amount'] = $amount; $json['balance'] = rate_getusercredit($username); } else { $json['status'] = 'ERR'; $json['error'] = '624'; } return $json; }
function simplerate_hook_rate_refund($smslog_id) { global $core_config; logger_print("start smslog_id:" . $smslog_id, 2, "simplerate refund"); $db_query = "SELECT p_dst,p_msg,uid FROM " . _DB_PREF_ . "_tblSMSOutgoing WHERE p_status='2' AND 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) { if (billing_rollback($smslog_id)) { $bill = billing_getdata($smslog_id); $credit = $bill['credit']; $charge = $bill['charge']; $status = $bill['status']; logger_print("rolling smslog_id:" . $smslog_id, 2, "simplerate refund"); if ($status == '2') { // 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 . " credit_parent:" . $credit_parent . " balance_parent:" . $balance_parent, 2, "simplerate refund"); } if (rate_setusercredit($uid, $balance)) { logger_print("user uid:" . $uid . " parent_uid:" . $parent_uid . " smslog_id:" . $smslog_id . " credit:" . $credit . " balance:" . $balance, 2, "simplerate refund"); return TRUE; } } } } } return FALSE; }
$action .= "<a href=\"" . _u('index.php?app=main&inc=core_user&route=user_config&op=user_config&uname=' . $list[$i]['username']) . "\">" . $icon_config['user_config'] . "</a>"; if ($list[$i]['uid'] != '1' || $list[$i]['uid'] != $user_config['uid']) { if (user_banned_get($list[$i]['uid'])) { // unban $action .= "<a href=\"javascript: ConfirmURL('" . addslashes(_("Are you sure you want to unban subuser")) . " " . $list[$i]['username'] . " ?','" . _u('index.php?app=main&inc=core_user&route=subuser_mgmnt&op=subuser_unban&uname=' . $list[$i]['username']) . "')\">" . $icon_config['unban'] . "</a>"; $banned_icon = $icon_config['ban']; } else { // ban $action .= "<a href=\"javascript: ConfirmURL('" . addslashes(_("Are you sure you want to ban subuser")) . " " . $list[$i]['username'] . " ?','" . _u('index.php?app=main&inc=core_user&route=subuser_mgmnt&op=subuser_ban&uname=' . $list[$i]['username']) . "')\">" . $icon_config['ban'] . "</a>"; $banned_icon = ''; } } // remove subuser $action .= "<a href=\"javascript: ConfirmURL('" . addslashes(_("Are you sure you want to delete subuser")) . " " . $list[$i]['username'] . " ?','" . _u('index.php?app=main&inc=core_user&route=subuser_mgmnt&op=subuser_del&uname=' . $list[$i]['username']) . "')\">" . $icon_config['user_delete'] . "</a>"; $j--; $content .= "\n\t\t\t\t<tr>\n\t\t\t\t\t<td>" . core_display_datetime($list[$i]['register_datetime']) . "</td>\n\t\t\t\t\t<td>" . $banned_icon . "" . $list[$i]['username'] . " </td>\n\t\t\t\t\t<td>" . $list[$i]['name'] . "</td>\n\t\t\t\t\t<td>" . $list[$i]['mobile'] . "</td>\t\n\t\t\t\t\t<td>" . rate_getusercredit($list[$i]['username']) . "</td>\t\n\t\t\t\t\t<td>{$action}</td>\n\t\t\t\t</tr>"; } $content .= "\n\t\t\t</tbody></table>\n\t\t\t</div>\n\t\t\t<div class=pull-right>" . $nav['form'] . "</div>"; _p($content); break; case "subuser_add": if ($err = $_SESSION['error_string']) { $content = "<div class=error_string>{$err}</div>"; } $add_datetime_timezone = $_REQUEST['add_datetime_timezone']; $add_datetime_timezone = $add_datetime_timezone ? $add_datetime_timezone : core_get_timezone(); // get language options $lang_list = ''; for ($i = 0; $i < count($core_config['languagelist']); $i++) { $language = $core_config['languagelist'][$i]; $c_language_title = $plugin_config[$language]['title'];