function main($id, $mode)
 {
     global $template, $user, $db, $config, $phpEx, $phpbb_root_path, $ultimate_points, $points_config, $points_values, $auth, $check_auth;
     $adm_points = request_var('adm_points', false);
     $u_id = request_var('user_id', 0);
     $post_id = request_var('post_id', 0);
     add_form_key('points_edit');
     if (empty($u_id)) {
         $message = $user->lang['EDIT_NO_ID_SPECIFIED'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.{$phpEx}", "mode=points_edit") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
         trigger_error($message);
     }
     if ($adm_points != false && ($auth->acl_get('a_points') || $auth->acl_get('m_chg_points'))) {
         $template->assign_block_vars('administer_points', array());
         if (isset($_POST['submit'])) {
             if (!check_form_key('points_edit')) {
                 trigger_error('FORM_INVALID');
             }
             $new_points = round(request_var('points', 0.0), 2);
             set_points($u_id, $new_points);
             $sql_array = array('SELECT' => 'user_id, username, user_points, user_colour', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $u_id);
             $sql = $db->sql_build_query('SELECT', $sql_array);
             $result = $db->sql_query($sql);
             $points_user = $db->sql_fetchrow($result);
             add_log('admin', 'LOG_MOD_POINTS', $points_user['username']);
             $message = $post_id ? sprintf($user->lang['EDIT_P_RETURN_POST'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.{$phpEx}", "p=" . $post_id) . '">', '</a>') : sprintf($user->lang['EDIT_P_RETURN_INDEX'], '<a href="' . append_sid("{$phpbb_root_path}index.{$phpEx}") . '">', '</a>');
             trigger_error(sprintf($user->lang['EDIT_POINTS_SET'], $config['points_name']) . $message);
         } else {
             $sql_array = array('SELECT' => 'user_id, username, user_points, user_colour', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $u_id);
             $sql = $db->sql_build_query('SELECT', $sql_array);
             $result = $db->sql_query($sql);
             $row = $db->sql_fetchrow($result);
             if (empty($u_id)) {
                 $message = $user->lang['EDIT_USER_NOT_EXIST'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.{$phpEx}", "mode=points_edit") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
                 trigger_error($message);
             }
             $hidden_fields = build_hidden_fields(array('user_id' => $u_id, 'post_id' => $post_id));
             $template->assign_vars(array('USER_NAME' => get_username_string('full', $u_id, $row['username'], $row['user_colour']), 'POINTS_OF_USER' => sprintf(number_format_points($row['user_points'])), 'POINTS_NAME' => $config['points_name'], 'CURRENT_VALUE' => $row['user_points'], 'L_POINTS_MODIFY' => sprintf($user->lang['EDIT_POINTS_MODIFY'], $config['points_name']), 'L_P_POINTS_TITLE' => sprintf($user->lang['EDIT_P_POINTS_TITLE'], $config['points_name']), 'L_USERNAME' => $user->lang['USERNAME'], 'S_ACTION' => append_sid("{$phpbb_root_path}points.{$phpEx}", "mode=points_edit&amp;adm_points=1"), 'S_HIDDEN_FIELDS' => $hidden_fields, 'U_USER_LINK' => append_sid("{$phpbb_root_path}memberlist.{$phpEx}", "mode=viewprofile&amp;u=" . $u_id)));
         }
     } else {
         $message = $user->lang['NOT_AUTHORISED'];
         trigger_error($message);
     }
     // Generate the page
     page_header($user->lang['EDIT_POINTS_ADMIN']);
     // Generate the page template
     $template->set_filenames(array('body' => 'points/points_points_edit.html'));
     page_footer();
 }
/**
* Run Lottery
*/
function run_lottery()
{
    global $db, $config, $user, $table_prefix, $phpbb_root_path, $phpEx;
    define('POINTS_LOTTERY_HISTORY_TABLE', $table_prefix . 'points_lottery_history');
    define('POINTS_VALUES_TABLE', $table_prefix . 'points_values');
    define('POINTS_LOTTERY_TICKETS_TABLE', $table_prefix . 'points_lottery_tickets');
    $current_time = time();
    // Count number of tickets
    $sql_array = array('SELECT' => 'COUNT(ticket_id) AS num_tickets', 'FROM' => array(POINTS_LOTTERY_TICKETS_TABLE => 'l'));
    $sql = $db->sql_build_query('SELECT', $sql_array);
    $result = $db->sql_query($sql);
    $total_tickets = (int) $db->sql_fetchfield('num_tickets');
    $db->sql_freeresult($result);
    // Select a random user from tickets table
    switch ($db->sql_layer) {
        case 'postgres':
            $order_by = 'RANDOM()';
            break;
        case 'mssql':
        case 'mssql_odbc':
            $order_by = 'NEWID()';
            break;
        default:
            $order_by = 'RAND()';
            break;
    }
    $sql_array = array('SELECT' => '*', 'FROM' => array(POINTS_LOTTERY_TICKETS_TABLE => 'l'), 'ORDER_BY' => $order_by);
    $sql = $db->sql_build_query('SELECT', $sql_array);
    $result = $db->sql_query_limit($sql, 1);
    $random_user_by_tickets = (int) $db->sql_fetchfield('user_id');
    $db->sql_freeresult($result);
    if ($total_tickets > 0) {
        // Genarate a random number
        $rand_base = $points_values['lottery_chance'];
        $rand_value = rand(0, 100);
        // Decide, if the user really wins
        if ($rand_value <= $rand_base) {
            $winning_number = $random_user_by_tickets;
            // Select a winner from ticket table
            $sql_array = array('SELECT' => '*', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $winning_number);
            $sql = $db->sql_build_query('SELECT', $sql_array);
            $result = $db->sql_query($sql);
            $winner = $db->sql_fetchrow($result);
            $db->sql_freeresult($result);
            // Check if lottery is enabled and prepare winner informations
            if ($points_config['lottery_enable'] != 0) {
                // Select the receiver language
                $winner['user_lang'] = file_exists($phpbb_root_path . 'language/' . $winner['user_lang'] . "/mods/points.{$phpEx}") ? $winner['user_lang'] : $config['default_lang'];
                // load receivers language
                include $phpbb_root_path . 'language/' . basename($winner['user_lang']) . "/mods/points.{$phpEx}";
                $winnings_update = $winner['user_points'] + $points_values['lottery_jackpot'];
                set_points($winner['user_id'], $winnings_update);
                $winner_notification = sprintf(number_format_points($points_values['lottery_jackpot'])) . ' ' . $config['points_name'] . ' ';
                $winner_deposit = $lang['LOTTERY_PM_CASH_ENABLED'];
                $amount_won = $points_values['lottery_jackpot'];
            } else {
                $winner_notification = '';
                $winner_deposit = '';
                $amount_won = '';
            }
            // Update previous winner information
            $sql = 'UPDATE ' . POINTS_VALUES_TABLE . ' 
					SET lottery_prev_winner = "' . $db->sql_escape($winner['username']) . '",
						lottery_prev_winner_id = ' . intval($winner['user_id']);
            $db->sql_query($sql);
            // Check, if user wants to be informed by PM
            if ($winner['user_allow_pm'] == 1) {
                $sql_array = array('SELECT' => '*', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $points_values['lottery_pm_from']);
                $sql = $db->sql_build_query('SELECT', $sql_array);
                $result = $db->sql_query($sql);
                $pm_sender = $db->sql_fetchrow($result);
                $db->sql_freeresult($result);
                // Notify the lucky winner by PM
                $pm_subject = utf8_normalize_nfc($lang['LOTTERY_PM_SUBJECT']);
                $pm_text = utf8_normalize_nfc(sprintf($lang['LOTTERY_PM_BODY'], $winner_notification, $winner_deposit));
                $poll = $uid = $bitfield = $options = '';
                generate_text_for_storage($pm_subject, $uid, $bitfield, $options, false, false, false);
                generate_text_for_storage($pm_text, $uid, $bitfield, $options, true, true, true);
                $pm_data = array('address_list' => array('u' => array($winner['user_id'] => 'to')), 'from_user_id' => $points_values['lottery_pm_from'] == 0 ? $winner['user_id'] : $pm_sender['user_id'], 'from_username' => $points_values['lottery_pm_from'] == 0 ? $user->lang['LOTTERY_PM_COMMISION'] : $pm_sender['username'], 'icon_id' => 0, 'from_user_ip' => '', 'enable_bbcode' => true, 'enable_smilies' => true, 'enable_urls' => true, 'enable_sig' => true, 'message' => $pm_text, 'bbcode_bitfield' => $bitfield, 'bbcode_uid' => $uid);
                submit_pm('post', $pm_subject, $pm_data, false);
            }
            // Add new winner to lottery history
            $sql = 'INSERT INTO ' . POINTS_LOTTERY_HISTORY_TABLE . ' ' . $db->sql_build_array('INSERT', array('user_id' => (int) $winner['user_id'], 'user_name' => $winner['username'], 'time' => $current_time, 'amount' => $points_values['lottery_jackpot']));
            $db->sql_query($sql);
            // Update winners total
            $sql = 'UPDATE ' . POINTS_VALUES_TABLE . ' SET lottery_winners_total = lottery_winners_total + 1';
            $db->sql_query($sql);
            // Reset jackpot
            $sql = 'UPDATE ' . POINTS_VALUES_TABLE . ' SET lottery_jackpot = lottery_base_amount';
            $db->sql_query($sql);
        } else {
            $sql = 'UPDATE ' . POINTS_VALUES_TABLE . ' SET lottery_jackpot = lottery_jackpot + lottery_base_amount';
            $db->sql_query($sql);
            $no_winner = 0;
            $sql = 'INSERT INTO ' . POINTS_LOTTERY_HISTORY_TABLE . ' ' . $db->sql_build_array('INSERT', array('user_id' => 0, 'user_name' => $no_winner, 'time' => $current_time, 'amount' => 0));
            $db->sql_query($sql);
            // Update previous winner information
            $sql = 'UPDATE ' . POINTS_VALUES_TABLE . ' SET lottery_prev_winner = "' . $no_winner . '"';
            $db->sql_query($sql);
            $sql = 'UPDATE ' . POINTS_VALUES_TABLE . ' SET lottery_prev_winner_id = 0';
            $db->sql_query($sql);
        }
    }
    // Reset lottery
    // Delete all tickets
    $sql = 'DELETE FROM ' . POINTS_LOTTERY_TICKETS_TABLE;
    $db->sql_query($sql);
    // Reset last draw time
    $check_time = $points_values['lottery_last_draw_time'] + $points_values['lottery_draw_period'];
    $current_time = time();
    if ($current_time > $check_time) {
        //this will *near* infinite loop if the check_time is significantly far behind the current time!
        while ($check_time < $current_time) {
            $check_time = $check_time + $points_values['lottery_draw_period'];
            $check_time++;
        }
        if ($check_time > $current_time) {
            $check_time = $check_time - $points_values['lottery_draw_period'];
            $sql = 'UPDATE ' . POINTS_VALUES_TABLE . ' SET lottery_last_draw_time = ' . $check_time;
            $db->sql_query($sql);
        }
    } else {
        $sql = 'UPDATE ' . POINTS_VALUES_TABLE . ' SET lottery_last_draw_time = lottery_last_draw_time + lottery_draw_period';
        $db->sql_query($sql);
    }
}
        // Check, if the user has enough cash to buy tickets
        if ($points_values['lottery_ticket_cost'] * $total_tickets_bought > $purchaser['user_points']) {
            $message = $user->lang['LOTTERY_LACK_FUNDS'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points_lottery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
        }
    }
    // Loop through total purchased tickets and create insert array
    for ($i = 0, $total_tickets_bought; $i < $total_tickets_bought; $i++) {
        $sql_insert_ary[] = array('user_id' => $user->data['user_id']);
    }
    $db->sql_multi_insert(POINTS_LOTTERY_TICKETS_TABLE, $sql_insert_ary);
    // Check again, if lottery is enablled
    if ($points_config['lottery_enable'] != 0) {
        // Deduct cost
        $viewer_cash = $purchaser['user_points'] - $points_values['lottery_ticket_cost'] * $total_tickets_bought;
        set_points($user->data['user_id'], $viewer_cash);
        // Update jackpot
        $sql = 'UPDATE ' . POINTS_VALUES_TABLE . ' SET lottery_jackpot = lottery_jackpot + (lottery_ticket_cost * ' . $total_tickets_bought . ')';
        $db->sql_query($sql);
    }
    $message = $user->lang['LOTTERY_TICKET_PURCHASED'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points_lottery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
    trigger_error($message);
    $template->assign_vars(array('U_ACTION' => $this->u_action));
}
// Display main page
$history_mode = $name;
if ($history_mode == 'points_lottery_history_self' || $history_mode == 'points_lottery_history') {
    // If no one has ever won, why bother doing anything else?
    if ($points_values['points_winners_total'] = 0) {
        $message = $user->lang['LOTTERY_NO_WINNERS'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points_lottery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
        trigger_error($message);