function main()
 {
     // Only registered users can go beyond this point
     if (!$this->user->data['is_registered']) {
         if ($this->user->data['is_bot']) {
             redirect(append_sid("{$this->phpbb_root_path}index.{$this->phpEx}"));
         }
         login_box('', $this->user->lang['LOGIN_INFO']);
     }
     $adm_points = $this->request->variable('adm_points', false);
     $u_id = $this->request->variable('user_id', 0);
     $post_id = $this->request->variable('post_id', 0);
     if (empty($u_id)) {
         $message = $this->user->lang['EDIT_NO_ID_SPECIFIED'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank_edit')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
         trigger_error($message);
     }
     $user_id = $u_id;
     add_form_key('bank_edit');
     if ($adm_points != false && ($this->auth->acl_get('a_') || $this->auth->acl_get('m_chg_bank'))) {
         $this->template->assign_block_vars('administer_bank', array());
         $submit = isset($_POST['submit']) ? true : false;
         if ($submit) {
             if (!check_form_key('bank_edit')) {
                 trigger_error('FORM_INVALID');
             }
             $new_points = round($this->request->variable('points', 0.0), 2);
             $this->functions_points->set_bank($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 = $this->db->sql_build_query('SELECT', $sql_array);
             $result = $this->db->sql_query($sql);
             $points_user = $this->db->sql_fetchrow($result);
             // Add logs
             $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_MOD_BANK', false, array($points_user['username']));
             $message = $post_id ? sprintf($this->user->lang['EDIT_P_RETURN_POST'], '<a href="' . append_sid("{$this->phpbb_root_path}viewtopic.{$this->phpEx}", "p=" . $post_id) . '">', '</a>') : sprintf($this->user->lang['EDIT_P_RETURN_INDEX'], '<a href="' . append_sid("{$this->phpbb_root_path}index.{$this->phpEx}") . '">', '</a>');
             trigger_error(sprintf($this->user->lang['EDIT_POINTS_SET'], $this->config['points_name']) . $message);
         } else {
             $sql_array = array('SELECT' => 'u.user_id, u.username, u.user_points, u.user_colour, b.holding', 'FROM' => array(USERS_TABLE => 'u'), 'LEFT_JOIN' => array(array('FROM' => array($this->points_bank_table => 'b'), 'ON' => 'u.user_id = b.user_id')), 'WHERE' => 'u.user_id = ' . (int) $u_id);
             $sql = $this->db->sql_build_query('SELECT', $sql_array);
             $result = $this->db->sql_query($sql);
             $row = $this->db->sql_fetchrow($result);
             if (empty($u_id)) {
                 $message = $this->user->lang['EDIT_USER_NOT_EXIST'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank_edit')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                 trigger_error($message);
             }
             $hidden_fields = build_hidden_fields(array('user_id' => $u_id, 'post_id' => $post_id));
             $this->template->assign_vars(array('USER_NAME' => get_username_string('full', $u_id, $row['username'], $row['user_colour']), 'BANK_POINTS' => sprintf($this->functions_points->number_format_points($row['holding'])), 'POINTS_NAME' => $this->config['points_name'], 'CURRENT_VALUE' => $row['holding'], 'L_POINTS_MODIFY' => sprintf($this->user->lang['EDIT_BANK_MODIFY'], $this->config['points_name']), 'L_P_BANK_TITLE' => sprintf($this->user->lang['EDIT_P_BANK_TITLE'], $this->config['points_name']), 'L_USERNAME' => $this->user->lang['USERNAME'], 'L_SET_AMOUNT' => $this->user->lang['EDIT_SET_AMOUNT'], 'U_USER_LINK' => append_sid("{$this->phpbb_root_path}memberlist.{$this->phpEx}", "mode=viewprofile&amp;u=" . $u_id), 'S_ACTION' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank_edit', 'adm_points' => '1')), 'S_HIDDEN_FIELDS' => $hidden_fields));
         }
     }
     // Generate the page
     page_header($this->user->lang['EDIT_POINTS_ADMIN']);
     // Generate the page template
     $this->template->set_filenames(array('body' => 'points/points_bank_edit.html'));
     page_footer();
 }
    public function handle_ultimatepoints_list()
    {
        // UPlist disabled
        if (!$this->config['points_enable']) {
            trigger_error($this->user->lang['POINTS_LIST_DISABLE'], E_USER_NOTICE);
        }
        // Get some values
        $sql_array = array('SELECT' => '*', 'FROM' => array($this->points_values_table => 'v'));
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query($sql);
        $points_values = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        // Generate donors list
        $start = $this->request->variable('start', 0);
        $limit = $points_values['number_show_per_page'];
        $sql = 'SELECT *
			FROM ' . USERS_TABLE . '
			WHERE user_points > 0
			ORDER BY user_points DESC';
        $result = $this->db->sql_query_limit($sql, $limit, $start);
        while ($row = $this->db->sql_fetchrow($result)) {
            $this->template->assign_block_vars('ultimatelist', array('USERNAME' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']), 'POINT' => sprintf($this->functions_points->number_format_points($row['user_points']))));
        }
        $this->db->sql_freeresult($result);
        // Generate pagination
        $sql = 'SELECT COUNT(user_points) AS ultimatepoints_total
			FROM ' . USERS_TABLE . '
			WHERE user_points > 0';
        $result = $this->db->sql_query($sql);
        $ultimatepoints_total = (int) $this->db->sql_fetchfield('ultimatepoints_total');
        //Start pagination
        $pagination_url = $this->helper->route('dmzx_ultimatepoints_list_controller');
        $this->pagination->generate_template_pagination($pagination_url, 'pagination', 'start', $ultimatepoints_total, $limit, $start);
        $this->template->assign_vars(array('TOTAL_ULTIMATEPOINTS_LIST' => $ultimatepoints_total == 1 ? $this->user->lang['POINTS_LIST_USER'] : sprintf($this->user->lang['POINTS_LIST_USERS'], $ultimatepoints_total), 'POINTSLIST' => $this->config['points_name']));
        // Output the page
        page_header($this->user->lang['POINTS_LIST_TOTAL']);
        $this->template->set_filenames(array('body' => 'points/points_list.html'));
        page_footer();
    }
    function main()
    {
        $sql = 'SELECT *
				FROM ' . $this->points_values_table;
        $result = $this->db->sql_query($sql);
        $points_values = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        // Add part to bar
        $this->template->assign_block_vars('navlinks', array('U_VIEW_FORUM' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'info')), 'FORUM_NAME' => sprintf($this->user->lang['POINTS_INFO'], $this->config['points_name'])));
        // Read out all the need values
        $info_attach = $points_values['points_per_attach'] == 0 ? sprintf($this->user->lang['INFO_NO_POINTS'], $this->config['points_name']) : sprintf($this->functions_points->number_format_points($points_values['points_per_attach']) . '&nbsp;' . $this->config['points_name']);
        $info_addtional_attach = $points_values['points_per_attach_file'] == 0 ? sprintf($this->user->lang['INFO_NO_POINTS'], $this->config['points_name']) : sprintf($this->functions_points->number_format_points($points_values['points_per_attach_file']) . '&nbsp;' . $this->config['points_name']);
        $info_poll = $points_values['points_per_poll'] == 0 ? sprintf($this->user->lang['INFO_NO_POINTS'], $this->config['points_name']) : sprintf($this->functions_points->number_format_points($points_values['points_per_poll']) . '&nbsp;' . $this->config['points_name']);
        $info_poll_option = $points_values['points_per_poll_option'] == 0 ? sprintf($this->user->lang['INFO_NO_POINTS'], $this->config['points_name']) : sprintf($this->functions_points->number_format_points($points_values['points_per_poll_option']) . '&nbsp;' . $this->config['points_name']);
        $info_topic_word = $points_values['points_per_topic_word'] == 0 ? sprintf($this->user->lang['INFO_NO_POINTS'], $this->config['points_name']) : sprintf($this->functions_points->number_format_points($points_values['points_per_topic_word']) . '&nbsp;' . $this->config['points_name']);
        $info_topic_character = $points_values['points_per_topic_character'] == 0 ? sprintf($this->user->lang['INFO_NO_POINTS'], $this->config['points_name']) : sprintf($this->functions_points->number_format_points($points_values['points_per_topic_character']) . '&nbsp;' . $this->config['points_name']);
        $info_post_word = $points_values['points_per_post_word'] == 0 ? sprintf($this->user->lang['INFO_NO_POINTS'], $this->config['points_name']) : sprintf($this->functions_points->number_format_points($points_values['points_per_post_word']) . '&nbsp;' . $this->config['points_name']);
        $info_post_character = $points_values['points_per_post_character'] == 0 ? sprintf($this->user->lang['INFO_NO_POINTS'], $this->config['points_name']) : sprintf($this->functions_points->number_format_points($points_values['points_per_post_character']) . '&nbsp;' . $this->config['points_name']);
        $info_cost_warning = $points_values['points_per_warn'] == 0 ? sprintf($this->user->lang['INFO_NO_COST'], $this->config['points_name']) : sprintf($this->functions_points->number_format_points($points_values['points_per_warn']) . '&nbsp;' . $this->config['points_name']);
        $info_reg_bonus = $points_values['reg_points_bonus'] == 0 ? sprintf($this->user->lang['INFO_NO_POINTS'], $this->config['points_name']) : sprintf($this->functions_points->number_format_points($points_values['reg_points_bonus']) . '&nbsp;' . $this->config['points_name']);
        $info_points_bonus = $points_values['points_bonus_chance'] == 0 ? sprintf($this->user->lang['INFO_NO_POINTS'], $this->config['points_name']) : sprintf($this->user->lang['INFO_BONUS_CHANCE_EXPLAIN'], $this->functions_points->number_format_points($points_values['points_bonus_chance']), $this->functions_points->number_format_points($points_values['points_bonus_min']), $this->functions_points->number_format_points($points_values['points_bonus_max']), $this->config['points_name']);
        $this->template->assign_vars(array('USER_POINTS' => sprintf($this->functions_points->number_format_points($this->user->data['user_points'])), 'POINTS_NAME' => $this->config['points_name'], 'LOTTERY_NAME' => $points_values['lottery_name'], 'BANK_NAME' => $points_values['bank_name'], 'POINTS_INFO_DESCRIPTION' => sprintf($this->user->lang['POINTS_INFO_DESCRIPTION'], $this->config['points_name']), 'INFO_ATTACH' => $info_attach, 'INFO_ADD_ATTACH' => $info_addtional_attach, 'INFO_POLL' => $info_poll, 'INFO_POLL_OPTION' => $info_poll_option, 'INFO_TOPIC_WORD' => $info_topic_word, 'INFO_TOPIC_CHARACTER' => $info_topic_character, 'INFO_POST_WORD' => $info_post_word, 'INFO_POST_CHARACTER' => $info_post_character, 'INFO_COST_WARNING' => $info_cost_warning, 'INFO_REG_BONUS' => $info_reg_bonus, 'INFO_POINTS_BONUS' => $info_points_bonus, 'U_TRANSFER_USER' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')), 'U_LOGS' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs')), 'U_LOTTERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')), 'U_BANK' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')), 'U_ROBBERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')), 'U_INFO' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'info')), 'U_USE_TRANSFER' => $this->auth->acl_get('u_use_transfer'), 'U_USE_LOGS' => $this->auth->acl_get('u_use_logs'), 'U_USE_LOTTERY' => $this->auth->acl_get('u_use_lottery'), 'U_USE_BANK' => $this->auth->acl_get('u_use_bank'), 'U_USE_ROBBERY' => $this->auth->acl_get('u_use_robbery')));
        // Generate the page
        page_header($this->user->lang['POINTS_INFO']);
        // Generate the page template
        $this->template->set_filenames(array('body' => 'points/points_info.html'));
        page_footer();
    }
    function main($checked_user)
    {
        // Get all values
        $sql = 'SELECT *
				FROM ' . $this->points_values_table;
        $result = $this->db->sql_query($sql);
        $points_values = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        // Get all point config names and config values
        $sql = 'SELECT config_name, config_value
				FROM ' . $this->points_config_table;
        $result = $this->db->sql_query($sql);
        while ($row = $this->db->sql_fetchrow($result)) {
            $points_config[$row['config_name']] = $row['config_value'];
        }
        $this->db->sql_freeresult($result);
        // Set some variables
        $start = $this->request->variable('start', 0);
        $number = $points_values['number_show_per_page'];
        add_form_key('lottery_tickets');
        // Check, if lottery is enabled
        if (!$points_config['lottery_enable']) {
            $message = $this->user->lang['LOTTERY_DISABLED'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller') . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
        }
        // Check, if user is allowed to use the lottery
        if (!$this->auth->acl_get('u_use_lottery')) {
            $message = $this->user->lang['NOT_AUTHORISED'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller') . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
        }
        // Add part to bar
        $this->template->assign_block_vars('navlinks', array('U_VIEW_FORUM' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')), 'FORUM_NAME' => $points_values['lottery_name']));
        // Add lottery base amount in description
        $this->template->assign_vars(array('L_LOTTERY_BASE_AMOUNT' => sprintf($this->user->lang['LOTTERY_DESCRIPTION'], sprintf($this->functions_points->number_format_points($points_values['lottery_base_amount'])), $this->config['points_name'])));
        // Recheck, if lottery was run, for those boards only having one user per day and which don't call the index page first
        if ($points_values['lottery_draw_period'] != 0 && time() > $points_values['lottery_last_draw_time'] + $points_values['lottery_draw_period']) {
            $this->functions_points->run_lottery();
        }
        // Check, if user has purchased tickets
        if ($this->request->variable('purchase_ticket', false) && $this->user->data['user_id'] != ANONYMOUS) {
            if (!check_form_key('lottery_tickets')) {
                trigger_error('FORM_INVALID');
            }
            // How many tickets have been bought?
            $total_tickets_bought = $this->request->variable('total_tickets', 0);
            // Check, if user already bought tickets
            $sql_array = array('SELECT' => 'COUNT(ticket_id) AS number_of_tickets', 'FROM' => array($this->points_lottery_tickets_table => 't'), 'WHERE' => 'user_id = ' . (int) $this->user->data['user_id']);
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
            $result = $this->db->sql_query($sql);
            $number_tickets = $this->db->sql_fetchfield('number_of_tickets');
            $this->db->sql_freeresult($result);
            // Check, if the user tries to buy more tickets than allowed
            if ($total_tickets_bought > $points_values['lottery_max_tickets']) {
                $message = sprintf($this->user->lang['LOTTERY_MAX_TICKETS_REACH'], $points_values['lottery_max_tickets']) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Check in user try to buy negative tickets
            if ($total_tickets_bought <= 0) {
                $message = $this->user->lang['LOTTERY_NEGATIVE_TICKETS'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Check, if the already bought tickets and the new request are higher than the max set number of tickets
            if ($number_tickets + $total_tickets_bought > $points_values['lottery_max_tickets']) {
                $message = sprintf($this->user->lang['LOTTERY_MAX_TICKETS_LEFT'], $points_values['lottery_max_tickets'] - $number_tickets) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Check, if the user sent an empty value
            if (!$total_tickets_bought) {
                $message = $this->user->lang['LOTTERY_INVALID_INPUT'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Check. if lottery is enabled
            if ($points_config['lottery_enable'] != 0 && $points_values['lottery_ticket_cost'] != 0) {
                // Grab users total cash
                $sql_array = array('SELECT' => '*', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $this->user->data['user_id']);
                $sql = $this->db->sql_build_query('SELECT', $sql_array);
                $result = $this->db->sql_query($sql);
                $purchaser = $this->db->sql_fetchrow($result);
                $this->db->sql_freeresult($result);
                // Check, if the user has enough cash to buy tickets
                if ($points_values['lottery_ticket_cost'] * $total_tickets_bought > $purchaser['user_points']) {
                    $message = $this->user->lang['LOTTERY_LACK_FUNDS'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')) . '">&laquo; ' . $this->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' => $this->user->data['user_id']);
            }
            $this->db->sql_multi_insert($this->points_lottery_tickets_table, $sql_insert_ary);
            // Check again, if lottery is enabled
            if ($points_config['lottery_enable'] != 0) {
                // Deduct cost
                $viewer_cash = $purchaser['user_points'] - $points_values['lottery_ticket_cost'] * $total_tickets_bought;
                $this->functions_points->set_points($this->user->data['user_id'], $viewer_cash);
                // Update jackpot
                $this->functions_points->set_points_values('lottery_jackpot', $points_values['lottery_jackpot'] + $points_values['lottery_ticket_cost'] * $total_tickets_bought);
            }
            $message = $this->user->lang['LOTTERY_TICKET_PURCHASED'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
            $this->template->assign_vars(array('U_ACTION' => $this->u_action));
        }
        // Display main page
        $history_mode = $this->request->variable('history', '');
        if ($history_mode) {
            // If no one has ever won, why bother doing anything else?
            if ($points_values['points_winners_total'] = 0) {
                $message = $this->user->lang['LOTTERY_NO_WINNERS'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            $total_wins = $points_values['points_winners_total'];
            // Check, if no entries returned, only self search would turn up empty at this point
            if ($history_mode == 'ego') {
                $sql_array = array('SELECT' => 'COUNT(id) AS viewer_history', 'FROM' => array($this->points_lottery_history_table => 'h'), 'WHERE' => 'user_id = ' . (int) $this->user->data['user_id']);
                $sql = $this->db->sql_build_query('SELECT', $sql_array);
                $result = $this->db->sql_query($sql);
                $total_wins = (int) $this->db->sql_fetchfield('viewer_history');
                $this->db->sql_freeresult($result);
                if ($total_wins == 0) {
                    $message = $this->user->lang['LOTTERY_NEVER_WON'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                    trigger_error($message);
                }
                $this->template->assign_vars(array('U_VIEW_HISTORY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery', 'history' => 'all')), 'U_TRANSFER_USER' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')), 'U_LOGS' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs')), 'U_LOTTERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')), 'U_BANK' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')), 'U_ROBBERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery'))));
            }
            // Check, if user is viewing all or own entries
            if ($history_mode == 'all') {
                $sql_array = array('SELECT' => 'COUNT(id) AS total_entries', 'FROM' => array($this->points_lottery_history_table => 'h'));
                $sql = $this->db->sql_build_query('SELECT', $sql_array);
                $result = $this->db->sql_query($sql);
                $total_entries = (int) $this->db->sql_fetchfield('total_entries');
                $this->db->sql_freeresult($result);
                $sql_array = array('SELECT' => 'h.*, u.*', 'FROM' => array($this->points_lottery_history_table => 'h'), 'LEFT_JOIN' => array(array('FROM' => array(USERS_TABLE => 'u'), 'ON' => 'h.user_id = u.user_id')), 'ORDER_BY' => 'time DESC');
            } else {
                $sql_array = array('SELECT' => 'COUNT(id) AS total_entries', 'FROM' => array($this->points_lottery_history_table => 'h'), 'WHERE' => 'user_id = ' . (int) $this->user->data['user_id']);
                $sql = $this->db->sql_build_query('SELECT', $sql_array);
                $result = $this->db->sql_query($sql);
                $total_entries = (int) $this->db->sql_fetchfield('total_entries');
                $this->db->sql_freeresult($result);
                $sql_array = array('SELECT' => 'h.*, u.*', 'FROM' => array($this->points_lottery_history_table => 'h'), 'LEFT_JOIN' => array(array('FROM' => array(USERS_TABLE => 'u'), 'ON' => 'h.user_id = u.user_id')), 'WHERE' => 'h.user_id = ' . (int) $this->user->data['user_id'], 'ORDER_BY' => 'time DESC');
            }
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
            $result = $this->db->sql_query_limit($sql, $number, $start);
            $row_color = $start;
            while ($row = $this->db->sql_fetchrow($result)) {
                $row_color++;
                // Check, if winner is user
                if ($row['user_id'] != 0) {
                    $history_member = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']);
                } else {
                    $history_member = $this->user->lang['LOTTERY_NO_WINNER'];
                }
                $this->template->assign_block_vars('history_row', array('NUMBER' => $row_color, 'U_WINNER_PROFILE' => $history_member, 'WINNER_PROFILE' => $history_member, 'USERNAME' => $row['username'], 'WINNINGS' => sprintf($this->functions_points->number_format_points($row['amount'])), 'DATE' => $this->user->format_date($row['time']), 'ROW_COLOR' => $row_color));
                $this->template->assign_vars(array('U_VIEW_HISTORY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery', 'history' => 'all')), 'U_VIEW_SELF_HISTORY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery', 'history' => 'ego')), 'U_INFO' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'info')), 'U_TRANSFER_USER' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')), 'U_LOGS' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs')), 'U_LOTTERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')), 'U_BANK' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')), 'U_ROBBERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery'))));
            }
            //Start pagination
            $this->pagination->generate_template_pagination($this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery', 'history' => $history_mode)), 'pagination', 'start', $total_entries, $number, $start);
            // Viewing a history page
            $this->template->assign_vars(array('CASH_NAME' => $this->config['points_name'], 'PAGINATION' => $this->user->lang('POINTS_LOG_COUNT', $total_entries), 'LOTTERY_NAME' => $points_values['lottery_name'], 'BANK_NAME' => $points_values['bank_name'], 'S_VIEW_HISTORY' => true, 'U_BACK_TO_LOTTERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')), 'U_VIEW_SELF_HISTORY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery', 'history' => 'ego')), 'U_TRANSFER_USER' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')), 'U_LOGS' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs')), 'U_LOTTERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')), 'U_BANK' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')), 'U_ROBBERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery'))));
        } else {
            // Show main lottery page
            $viewer_total_tickets = '';
            if ($this->user->data['user_id'] != ANONYMOUS) {
                //Select total tickets viewer owns
                $sql_array = array('SELECT' => 'COUNT(ticket_id) AS num_tickets', 'FROM' => array($this->points_lottery_tickets_table => 'h'), 'WHERE' => 'user_id = ' . (int) $this->user->data['user_id']);
                $sql = $this->db->sql_build_query('SELECT', $sql_array);
                $result = $this->db->sql_query($sql);
                $viewer_total_tickets = (int) $this->db->sql_fetchfield('num_tickets');
                $this->db->sql_freeresult($result);
            }
            // User color selection
            $sql_array = array('SELECT' => 'user_id, username, user_colour', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $points_values['lottery_prev_winner_id']);
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
            $result = $this->db->sql_query($sql);
            $row = $this->db->sql_fetchrow($result);
            if ($row == null) {
                $username_colored = $this->user->lang['LOTTERY_NO_WINNER'];
            } else {
                $username_colored = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']);
            }
            // Check, if previous winner is a user
            if ($points_values['lottery_prev_winner_id'] != 0) {
                $link_member = append_sid("{$this->phpbb_root_path}memberlist.{$this->phpEx}", "mode=viewprofile&amp;u=" . $points_values['lottery_prev_winner_id']);
            } else {
                $link_member = '';
            }
            // Select the total number of tickets
            $sql_array = array('SELECT' => 'COUNT(ticket_id) AS no_of_tickets', 'FROM' => array($this->points_lottery_tickets_table => 't'));
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
            $result = $this->db->sql_query($sql);
            $row = $this->db->sql_fetchrow($result);
            $no_of_tickets = $row['no_of_tickets'];
            $this->db->sql_freeresult($result);
            // Select the total number of players
            $sql_array = array('SELECT' => 'user_id', 'FROM' => array($this->points_lottery_tickets_table => 't'));
            $sql = $this->db->sql_build_query('SELECT_DISTINCT', $sql_array);
            $result = $this->db->sql_query($sql);
            $no_of_players = 0;
            while ($row = $this->db->sql_fetchrow($result)) {
                $no_of_players += 1;
            }
            $this->db->sql_freeresult($result);
            $this->template->assign_vars(array('JACKPOT' => sprintf($this->functions_points->number_format_points($points_values['lottery_jackpot']), $this->config['points_name']), 'POINTS_NAME' => $this->config['points_name'], 'TICKET_COST' => sprintf($this->functions_points->number_format_points($points_values['lottery_ticket_cost'])), 'PREVIOUS_WINNER' => $username_colored, 'NEXT_DRAWING' => $this->user->format_date($points_values['lottery_last_draw_time'] + $points_values['lottery_draw_period'], false, true), 'LOTTERY_NAME' => $points_values['lottery_name'], 'BANK_NAME' => $points_values['bank_name'], 'VIEWER_TICKETS_TOTAL' => $viewer_total_tickets, 'LOTTERY_TICKETS' => $no_of_tickets, 'LOTTERY_PLAYERS' => $no_of_players, 'MAX_TICKETS' => $points_values['lottery_max_tickets'], 'S_PURCHASE_SINGLE' => $viewer_total_tickets == 0 && $points_config['lottery_multi_ticket_enable'] == 0 && $points_config['lottery_enable'] == 1 ? true : false, 'S_PURCHASE_MULTI' => $viewer_total_tickets < $points_values['lottery_max_tickets'] && $points_config['lottery_multi_ticket_enable'] == 1 && $points_config['lottery_enable'] == 1 ? true : false, 'S_MULTI_TICKETS' => $points_config['lottery_multi_ticket_enable'] == 1 ? true : false, 'S_LOTTERY_ENABLE' => $points_config['lottery_enable'] == 1 ? true : false, 'S_DRAWING_ENABLED' => $points_values['lottery_draw_period'] ? true : false, 'U_PREVIOUS_WINNER' => $link_member, 'U_VIEW_HISTORY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery', 'history' => 'all')), 'U_VIEW_SELF_HISTORY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery', 'history' => 'ego')), 'U_TRANSFER_USER' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')), 'U_LOGS' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs')), 'U_LOTTERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')), 'U_BANK' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')), 'U_ROBBERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')), 'U_INFO' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'info')), 'U_USE_TRANSFER' => $this->auth->acl_get('u_use_transfer'), 'U_USE_LOGS' => $this->auth->acl_get('u_use_logs'), 'U_USE_LOTTERY' => $this->auth->acl_get('u_use_lottery'), 'U_USE_BANK' => $this->auth->acl_get('u_use_bank'), 'U_USE_ROBBERY' => $this->auth->acl_get('u_use_robbery'), 'USER_POINTS' => sprintf($this->functions_points->number_format_points($checked_user['user_points']))));
        }
        // Generate the page header
        page_header($points_values['lottery_name']);
        // Generate the page template
        $this->template->set_filenames(array('body' => 'points/points_lottery.html'));
        page_footer();
    }
    function main($checked_user)
    {
        // Get all values
        $sql = 'SELECT *
				FROM ' . $this->points_values_table;
        $result = $this->db->sql_query($sql);
        $points_values = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        // Check if user is allowed to use the logs
        if (!$this->auth->acl_get('u_use_logs')) {
            $message = $this->user->lang['NOT_AUTHORISED'] . '<br /><br /><a href="' . append_sid("{$this->phpbb_root_path}ultimatepoints") . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
        }
        // Add part to bar
        $this->template->assign_block_vars('navlinks', array('U_VIEW_FORUM' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs')), 'FORUM_NAME' => sprintf($this->user->lang['LOGS_TITLE'], $this->config['points_name'])));
        // Preparing the sort order
        $start = $this->request->variable('start', 0);
        $number = $points_values['number_show_per_page'];
        $sort_days = $this->request->variable('st', 0);
        $sort_key = $this->request->variable('sk', 'date');
        $sort_dir = $this->request->variable('sd', 'd');
        $limit_days = array(0 => $this->user->lang['ALL_POSTS'], 1 => $this->user->lang['1_DAY'], 7 => $this->user->lang['7_DAYS'], 14 => $this->user->lang['2_WEEKS'], 30 => $this->user->lang['1_MONTH'], 90 => $this->user->lang['3_MONTHS'], 180 => $this->user->lang['6_MONTHS'], 365 => $this->user->lang['1_YEAR']);
        $sort_by_text = array('date' => $this->user->lang['LOGS_SORT_DATE'], 'to' => $this->user->lang['LOGS_SORT_TONAME'], 'from' => $this->user->lang['LOGS_SORT_FROMNAME'], 'comment' => $this->user->lang['LOGS_SORT_COMMENT']);
        $sort_by_sql = array('date' => 'point_date', 'to' => 'point_recv', 'from' => 'point_send', 'comment' => 'point_comment');
        $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
        gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
        $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . ($sort_dir == 'd' ? 'DESC' : 'ASC');
        // The different log types
        $types = array(0 => '--', 1 => $this->user->lang['LOGS_RECV'], 2 => $this->user->lang['LOGS_SENT'], 3 => $this->user->lang['LOGS_ROBBERY_WON'], 4 => $this->user->lang['LOGS_ROBBERY_LOST']);
        // Grab the total amount of logs for this user
        $sql_array = array('SELECT' => 'COUNT(*) AS total', 'FROM' => array($this->points_log_table => 'l'), 'WHERE' => 'point_send = ' . (int) $this->user->data['user_id'] . '
				OR point_recv = ' . (int) $this->user->data['user_id']);
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query($sql);
        $max = (int) $this->db->sql_fetchfield('total');
        // Grab the actual logs based on all account movements
        $sql_array = array('SELECT' => '*', 'FROM' => array($this->points_log_table => 'l'), 'WHERE' => 'point_send = ' . (int) $this->user->data['user_id'] . '
				OR point_recv = ' . (int) $this->user->data['user_id'], 'ORDER_BY' => $sql_sort_order);
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query_limit($sql, $number, $start);
        // Start looping all the logs
        while ($row = $this->db->sql_fetchrow($result)) {
            switch ($row['point_type']) {
                case 1:
                    //Transfer
                    $transfer_user = $row['point_send'] == $checked_user['user_id'] ? $row['point_recv'] : $row['point_send'];
                    $sql_array = array('SELECT' => '*', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $transfer_user);
                    $sql = $this->db->sql_build_query('SELECT', $sql_array);
                    $result1 = $this->db->sql_query($sql);
                    $opponent = $this->db->sql_fetchrow($result1);
                    $this->db->sql_freeresult($result1);
                    if ($row['point_send'] == $checked_user['user_id']) {
                        $who = get_username_string('full', $checked_user['user_id'], $checked_user['username'], $checked_user['user_colour']) . "<br />(" . $this->functions_points->number_format_points($row['point_sendold']) . "->" . $this->functions_points->number_format_points($row['point_sendold'] - $row['point_amount']) . ")";
                        $to = get_username_string('full', $opponent['user_id'], $opponent['username'], $opponent['user_colour']) . "<br />(" . $this->functions_points->number_format_points($row['point_recvold']) . "->" . $this->functions_points->number_format_points($row['point_recvold'] + (100 - $points_values['transfer_fee']) / 100 * $row['point_amount']) . ")";
                        $rows = 2;
                    } else {
                        $to = get_username_string('full', $checked_user['user_id'], $checked_user['username'], $checked_user['user_colour']) . "<br />(" . $this->functions_points->number_format_points($row['point_recvold']) . "->" . $this->functions_points->number_format_points($row['point_recvold'] + (100 - $points_values['transfer_fee']) / 100 * $row['point_amount']) . ")";
                        $who = get_username_string('full', $opponent['user_id'], $opponent['username'], $opponent['user_colour']) . "<br />(" . $this->functions_points->number_format_points($row['point_sendold']) . "->" . $this->functions_points->number_format_points($row['point_sendold'] - $row['point_amount']) . ")";
                        $rows = 1;
                    }
                    $who .= " (-" . $this->functions_points->number_format_points($row['point_amount']) . ")";
                    $to .= " (+" . $this->functions_points->number_format_points((100 - $points_values['transfer_fee']) / 100 * $row['point_amount']) . ")";
                    break;
                case 2:
                    //Locked
                    $who = get_username_string('full', $opponent['user_id'], $opponent['username'], $opponent['user_colour']);
                    $to = "--";
                    break;
                case 3:
                    //Robbery
                    $transfer_user = $row['point_send'] == $checked_user['user_id'] ? $row['point_recv'] : $row['point_send'];
                    $sql_array = array('SELECT' => '*', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $transfer_user);
                    $sql = $this->db->sql_build_query('SELECT', $sql_array);
                    $result1 = $this->db->sql_query($sql);
                    $opponent = $this->db->sql_fetchrow($result1);
                    $this->db->sql_freeresult($result1);
                    if ($row['point_send'] == $checked_user['user_id']) {
                        $who = get_username_string('full', $checked_user['user_id'], $checked_user['username'], $checked_user['user_colour']) . "<br />(" . $this->functions_points->number_format_points($row['point_sendold']) . "->" . $this->functions_points->number_format_points($row['point_sendold'] + $row['point_amount']) . ")";
                        $to = get_username_string('full', $opponent['user_id'], $opponent['username'], $opponent['user_colour']) . "<br />(" . $this->functions_points->number_format_points($row['point_recvold']) . "->" . $this->functions_points->number_format_points($row['point_recvold'] - $row['point_amount']) . ")";
                        $rows = 3;
                    } else {
                        $to = get_username_string('full', $checked_user['user_id'], $checked_user['username'], $checked_user['user_colour']) . "<br />(" . $this->functions_points->number_format_points($row['point_recvold']) . "->" . $this->functions_points->number_format_points($row['point_recvold'] - $row['point_amount']) . ")";
                        $who = get_username_string('full', $opponent['user_id'], $opponent['username'], $opponent['user_colour']) . "<br />(" . $this->functions_points->number_format_points($row['point_sendold']) . "->" . $this->functions_points->number_format_points($row['point_sendold'] + $row['point_amount']) . ")";
                        $rows = 4;
                    }
                    $who .= " (+" . $this->functions_points->number_format_points($row['point_amount']) . ")";
                    $to .= " (-" . $this->functions_points->number_format_points($row['point_amount']) . ")";
                    break;
            }
            // Add the items to the template
            $this->template->assign_block_vars('logs', array('DATE' => $this->user->format_date($row['point_date']), 'COMMENT' => nl2br($row['point_comment']), 'TYPE' => $types[$rows], 'ROW' => $rows, 'WHO' => $who, 'TO' => $to));
        }
        $this->db->sql_freeresult($result);
        //Start pagination
        $this->pagination->generate_template_pagination($this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs', 'sk' => $sort_key, 'sd' => $sort_dir)), 'pagination', 'start', $max, $number, $start);
        // Generate the page template
        $this->template->assign_vars(array('PAGINATION' => $this->user->lang('POINTS_LOG_COUNT', $max), 'LOTTERY_NAME' => $points_values['lottery_name'], 'BANK_NAME' => $points_values['bank_name'], 'S_LOGS_ACTION' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs')), 'S_SELECT_SORT_DIR' => $s_sort_dir, 'S_SELECT_SORT_KEY' => $s_sort_key, 'U_TRANSFER_USER' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')), 'U_LOGS' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs')), 'U_LOTTERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')), 'U_BANK' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')), 'U_ROBBERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')), 'U_INFO' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'info')), 'U_USE_TRANSFER' => $this->auth->acl_get('u_use_transfer'), 'U_USE_LOGS' => $this->auth->acl_get('u_use_logs'), 'U_USE_LOTTERY' => $this->auth->acl_get('u_use_lottery'), 'U_USE_BANK' => $this->auth->acl_get('u_use_bank'), 'U_USE_ROBBERY' => $this->auth->acl_get('u_use_robbery')));
        // Generate the page header
        page_header(sprintf($this->user->lang['LOGS_TITLE'], $checked_user['username']));
        // Generate the page template
        $this->template->set_filenames(array('body' => 'points/points_logs.html'));
        page_footer();
    }
    function main($checked_user)
    {
        // Get all values
        $sql = 'SELECT *
				FROM ' . $this->points_values_table;
        $result = $this->db->sql_query($sql);
        $points_values = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        // Select user's bank holding
        $sql_array = array('SELECT' => '*', 'FROM' => array($this->points_bank_table => 'b'), 'WHERE' => 'user_id = ' . (int) $this->user->data['user_id']);
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query($sql);
        $row = $this->db->sql_fetchrow($result);
        // Select user's lottery tickets
        $viewer_total_tickets = '';
        if ($this->user->data['user_id'] != ANONYMOUS) {
            $sql_array = array('SELECT' => 'COUNT(ticket_id) AS num_tickets', 'FROM' => array($this->points_lottery_tickets_table => 't'), 'WHERE' => 'user_id = ' . (int) $this->user->data['user_id']);
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
            $result = $this->db->sql_query($sql);
            $viewer_total_tickets = (int) $this->db->sql_fetchfield('num_tickets');
            $this->db->sql_freeresult($result);
        }
        // Generate the page header
        page_header(sprintf($this->user->lang['POINTS_TITLE_MAIN'], $this->config['points_name']));
        $user_name = get_username_string('full', $checked_user['user_id'], $checked_user['username'], $checked_user['user_colour']);
        // Generate some language stuff, dependig on the fact, if user has a bank account or not
        if ($row['user_id'] != $this->user->data['user_id'] || $row['holding'] < 1) {
            $this->template->assign_vars(array('L_MAIN_ON_HAND' => sprintf($this->user->lang['MAIN_ON_HAND'], $this->functions_points->number_format_points($checked_user['user_points']), $this->config['points_name']), 'L_MAIN_HELLO_USERNAME' => sprintf($this->user->lang['MAIN_HELLO_USERNAME'], $user_name), 'L_MAIN_LOTTERY_TICKETS' => sprintf($this->user->lang['MAIN_LOTTERY_TICKETS'], $viewer_total_tickets)));
        } else {
            $this->template->assign_vars(array('L_MAIN_ON_HAND' => $this->auth->acl_get('u_use_points') ? sprintf($this->user->lang['MAIN_ON_HAND'], $this->functions_points->number_format_points($checked_user['user_points']), $this->config['points_name']) : '', 'L_MAIN_HELLO_USERNAME' => sprintf($this->user->lang['MAIN_HELLO_USERNAME'], $user_name), 'L_MAIN_LOTTERY_TICKETS' => $this->auth->acl_get('u_use_lottery') ? sprintf($this->user->lang['MAIN_LOTTERY_TICKETS'], $viewer_total_tickets) : ''));
            if ($this->auth->acl_get('u_use_bank')) {
                $this->template->assign_block_vars('has_bank_account', array('L_MAIN_BANK_HAVE' => sprintf($this->user->lang['MAIN_BANK_HAVE'], $this->functions_points->number_format_points($row['holding']), $this->config['points_name'])));
            }
        }
        // Generate richest users
        $limit = $points_values['number_show_top_points'];
        $sql_array = array('SELECT' => 'user_id, username, user_colour, user_points', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_points > 0', 'ORDER_BY' => 'user_points DESC, username_clean ASC');
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query_limit($sql, $limit);
        while ($row = $this->db->sql_fetchrow($result)) {
            $this->template->assign_block_vars('points', array('USERNAME' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']), 'POINT' => sprintf($this->functions_points->number_format_points($row['user_points']))));
        }
        $this->db->sql_freeresult($result);
        // Richest Banker
        $sql_array = array('SELECT' => 'b.user_id, b.holding, u.user_id, u.username, u.user_colour', 'FROM' => array($this->points_bank_table => 'b'), 'LEFT_JOIN' => array(array('FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = b.user_id')), 'ORDER_BY' => 'b.holding DESC');
        // Build the query...
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        // Run the query...
        $result = $this->db->sql_query_limit($sql, 5);
        // only get 5 richest..
        // rb_ is Richest Banker
        while ($rb_row = $this->db->sql_fetchrow($result)) {
            $rb_username = get_username_string('full', $rb_row['user_id'], $rb_row['username'], $rb_row['user_colour']);
            if ($rb_row['holding'] != 0.0) {
                $this->template->assign_block_vars('richest_banker', array('USER' => $rb_username, 'HOLDING' => $rb_row['holding']));
            }
        }
        $this->db->sql_freeresult($result);
        // Free the results
        // Most Donations Given
        $sql_array = array('SELECT' => 'lt.point_send, u.user_id, u.username, u.user_colour, SUM(lt.point_amount) AS total_donated', 'FROM' => array($this->points_log_table => 'lt'), 'LEFT_JOIN' => array(array('FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = lt.point_send')), 'GROUP_BY' => 'lt.point_send', 'ORDER_BY' => 'total_donated DESC');
        // Build the query...
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        // Run the query...
        $result = $this->db->sql_query_limit($sql, 5);
        // only get 5 most generous users..
        // md_ is Most Donated
        while ($md_row = $this->db->sql_fetchrow($result)) {
            $md_username = get_username_string('full', $md_row['user_id'], $md_row['username'], $md_row['user_colour']);
            $this->template->assign_block_vars('most_donated', array('USER' => $md_username, 'DONATED' => $md_row['total_donated']));
        }
        $this->db->sql_freeresult($result);
        // Free the results
        // Most Lotteries Won
        $sql_array = array('SELECT' => 'lh.user_id, COUNT(lh.id) AS total_won, SUM(lh.amount) AS total_prize, u.user_id, u.username, u.user_colour', 'FROM' => array($this->points_lottery_history_table => 'lh'), 'LEFT_JOIN' => array(array('FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = lh.user_id')), 'GROUP_BY' => 'lh.user_id', 'ORDER_BY' => 'total_won DESC');
        // Build the query...
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        // Run the query...
        $result = $this->db->sql_query_limit($sql, 5);
        // only get 5 luckiest users..
        // lw_ is Lotteries Won
        while ($lw_row = $this->db->sql_fetchrow($result)) {
            if ($lw_row['user_id'] != 0) {
                $lw_username = get_username_string('full', $lw_row['user_id'], $lw_row['username'], $lw_row['user_colour']);
                $this->template->assign_block_vars('lotteries_won', array('USER' => $lw_username, 'TOTAL_WON' => $lw_row['total_won'], 'TOTAL_PRIZE' => $lw_row['total_prize']));
            }
        }
        $this->db->sql_freeresult($result);
        // Free the results
        $this->template->assign_vars(array('LOTTERY_NAME' => $points_values['lottery_name'], 'BANK_NAME' => $points_values['bank_name'], 'S_DISPLAY_INDEX' => $points_values['number_show_top_points'] > 0 ? true : false, 'U_TRANSFER_USER' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')), 'U_LOGS' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs')), 'U_LOTTERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')), 'U_BANK' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')), 'U_ROBBERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')), 'U_INFO' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'info')), 'U_USE_TRANSFER' => $this->auth->acl_get('u_use_transfer'), 'U_USE_LOGS' => $this->auth->acl_get('u_use_logs'), 'U_USE_LOTTERY' => $this->auth->acl_get('u_use_lottery'), 'U_USE_BANK' => $this->auth->acl_get('u_use_bank'), 'U_USE_ROBBERY' => $this->auth->acl_get('u_use_robbery')));
        // Generate the page template
        $this->template->set_filenames(array('body' => 'points/points_main.html'));
        page_footer();
    }
    function main($checked_user)
    {
        // Get all point config names and config values
        $sql = 'SELECT config_name, config_value
				FROM ' . $this->points_config_table;
        $result = $this->db->sql_query($sql);
        while ($row = $this->db->sql_fetchrow($result)) {
            $points_config[$row['config_name']] = $row['config_value'];
        }
        $this->db->sql_freeresult($result);
        // Grab transfer fee
        $sql = 'SELECT transfer_fee
				FROM ' . $this->points_values_table;
        $result = $this->db->sql_query($sql);
        $transfer_fee = $this->db->sql_fetchfield('transfer_fee');
        $this->db->sql_freeresult($result);
        // Grab the variables
        $message = $this->request->variable('comment', '', true);
        $adm_points = $this->request->variable('adm_points', false);
        $transfer_id = $this->request->variable('i', 0);
        $post_id = $this->request->variable('post_id', 0);
        add_form_key('transfer_points');
        // Check, if transferring is allowed
        if (!$points_config['transfer_enable']) {
            $message = $this->user->lang['TRANSFER_REASON_TRANSFER'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller') . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
        }
        // Add part to bar
        $this->template->assign_block_vars('navlinks', array('U_VIEW_FORUM' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')), 'FORUM_NAME' => sprintf($this->user->lang['TRANSFER_TITLE'], $this->config['points_name'])));
        $submit = isset($_POST['submit']) ? true : false;
        if ($submit) {
            if (!check_form_key('transfer_points')) {
                trigger_error('FORM_INVALID');
            }
            // Get variables for transferring
            $am = round($this->request->variable('amount', 0.0), 2);
            $comment = $this->request->variable('comment', '', true);
            // Check, if the sender has enough cash
            if ($this->user->data['user_points'] < $am) {
                $message = sprintf($this->user->lang['TRANSFER_REASON_MINPOINTS'], $this->config['points_name']) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Check, if the amount is 0 or below
            if ($am <= 0) {
                $message = sprintf($this->user->lang['TRANSFER_REASON_UNDERZERO'], $this->config['points_name']) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Check, if the user is trying to send to himself
            if ($this->user->data['user_id'] == $checked_user['user_id']) {
                $message = sprintf($this->user->lang['TRANSFER_REASON_YOURSELF'], $this->config['points_name']) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Add cash to receiver
            $amount = (100 - $transfer_fee) / 100 * $am;
            // Deduct the transfer fee
            $this->functions_points->add_points($checked_user['user_id'], $amount);
            // Remove cash from sender
            $this->functions_points->substract_points($this->user->data['user_id'], $am);
            // Get current time for logs
            $current_time = time();
            // Add transfer information to the log
            $text = utf8_normalize_nfc($message);
            $sql = 'INSERT INTO ' . $this->points_log_table . ' ' . $this->db->sql_build_array('INSERT', array('point_send' => (int) $this->user->data['user_id'], 'point_recv' => (int) $checked_user['user_id'], 'point_amount' => $am, 'point_sendold' => $this->user->data['user_points'], 'point_recvold' => $checked_user['user_points'], 'point_comment' => $text, 'point_type' => '1', 'point_date' => $current_time));
            $this->db->sql_query($sql);
            // Send pm to user
            if (!$points_config['transfer_pm_enable'] == 0 && $checked_user['user_allow_pm'] == 1) {
                // Select the user data for the PM
                $sql_array = array('SELECT' => '*', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $checked_user['user_id']);
                $sql = $this->db->sql_build_query('SELECT', $sql_array);
                $result = $this->db->sql_query($sql);
                $user_row = $this->db->sql_fetchrow($result);
                $this->db->sql_freeresult($result);
                $points_name = $this->config['points_name'];
                $comment = $this->db->sql_escape($comment);
                $pm_subject = utf8_normalize_nfc(sprintf($this->user->lang['TRANSFER_PM_SUBJECT']));
                $pm_text = utf8_normalize_nfc(sprintf($this->user->lang['TRANSFER_PM_BODY'], $amount, $points_name, $text));
                $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($checked_user['user_id'] => 'to')), 'from_user_id' => $this->user->data['user_id'], 'from_username' => $this->user->data['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);
            }
            $message = sprintf($this->user->lang['TRANSFER_REASON_TRANSUCC'], $this->functions_points->number_format_points($am), $this->config['points_name'], $checked_user['username']) . '<br /><br />' . ($post_id ? sprintf($this->user->lang['EDIT_P_RETURN_POST'], '<a href="' . append_sid("{$this->phpbb_root_path}viewtopic.{$this->phpEx}", "p=" . $post_id) . '">', '</a>') : sprintf($this->user->lang['EDIT_P_RETURN_INDEX'], '<a href="' . append_sid("{$this->phpbb_root_path}index.{$this->phpEx}") . '">', '</a>'));
            trigger_error($message);
            $this->template->assign_vars(array('U_ACTION' => $this->u_action));
        }
        $username_full = get_username_string('full', $checked_user['user_id'], $checked_user['username'], $checked_user['user_colour']);
        $this->template->assign_vars(array('L_TRANSFER_DESCRIPTION' => sprintf($this->user->lang['TRANSFER_DESCRIPTION'], $this->config['points_name']), 'POINTS_NAME' => $this->config['points_name'], 'POINTS_COMMENTS' => $points_config['comments_enable'] ? true : false, 'TRANSFER_FEE' => $transfer_fee, 'U_TRANSFER_NAME' => sprintf($this->user->lang['TRANSFER_TO_NAME'], $username_full, $this->config['points_name']), 'S_ALLOW_SEND_PM' => $this->auth->acl_get('u_sendpm')));
        // Generate the page
        page_header(sprintf($this->user->lang['TRANSFER_TITLE'], $this->config['points_name']));
        // Generate the page template
        $this->template->set_filenames(array('body' => 'points/points_transfer.html'));
        page_footer();
    }
    public function submit_post_end($event)
    {
        $points_config = $this->cache->get('points_config');
        $points_values = $this->cache->get('points_values');
        if ($this->config['points_enable']) {
            $data = $event['data'];
            $mode = $event['mode'];
            $poll = $event['poll'];
            $post_id = (int) $data['post_id'];
            $topic_id = (int) $data['topic_id'];
            $forum_id = (int) $data['forum_id'];
            $user_id = (int) $this->user->data['user_id'];
            // Send the user_id away to check for a bonus increment
            $this->functions_points->random_bonus_increment($user_id);
            /**
             * Grab our message and strip it clean.
             * This means removing all BBCode,
             * and removing text inside code and quote blocks
             */
            $message = $this->functions_points->strip_text($data['message']);
            // Set default values
            $topic_word = $topic_char = $forum_topic = 0;
            $post_word = $post_char = $forum_post = 0;
            $difference = $total_points = $prev_points = 0;
            $has_attach = $per_attach = 0;
            $total_attachments = $points_attach = 0;
            $has_poll = $per_poll = $points_poll = $total_poll_options = 0;
            // We grab global points increment
            $topic_word = $points_values['points_per_topic_word'];
            // Points per word in a topic
            $topic_char = $points_values['points_per_topic_character'];
            // Points per character in a topic
            $post_word = $points_values['points_per_post_word'];
            // Points per word in a post (reply)
            $post_char = $points_values['points_per_post_character'];
            // Points per word in a post (reply)
            $has_attach = $points_values['points_per_attach'];
            // Points for having attachments in your post
            $per_attach = $points_values['points_per_attach_file'];
            // Points per attachment in your post
            $has_poll = $points_values['points_per_poll'];
            // Points for having a poll in your topic
            $per_poll = $points_values['points_per_poll_option'];
            // Points per poll option in your topic
            // We grab forum specific points increment
            $sql = 'SELECT forum_peredit, forum_perpost, forum_pertopic, forum_cost_topic, forum_cost_post
					FROM ' . FORUMS_TABLE . '
					WHERE forum_id = ' . (int) $forum_id;
            $result = $this->db->sql_query($sql);
            $forum = $this->db->sql_fetchrow($result);
            $this->db->sql_freeresult($result);
            // First we check if we have to pay for new topics/post
            if ($mode == 'post' && $forum['forum_cost_topic'] > 0 && $this->auth->acl_get('f_pay_topic', (int) $forum_id)) {
                $this->functions_points->substract_points((int) $user_id, $forum['forum_cost_topic']);
            } else {
                if (($mode == 'reply' || $mode == 'quote') && $forum['forum_cost_post'] > 0 && $this->auth->acl_get('f_pay_post', (int) $forum_id)) {
                    $this->functions_points->substract_points((int) $user_id, $forum['forum_cost_post']);
                }
            }
            // We grab some specific message data
            $sizeof_msg = sizeof(explode(' ', $message));
            // Amount of words
            $chars_msg = utf8_strlen($message);
            // Amount of characters
            // Check if the post has attachment, if so calculate attachment points
            if (!empty($data['attachment_data'])) {
                $total_attachments = sizeof($data['attachment_data']);
                $points_attach = $total_attachments * $per_attach + $has_attach;
            }
            // Check if the post has a poll, if so calculate poll points
            if (!empty($poll['poll_options'])) {
                $total_poll_options = sizeof($poll['poll_options']);
                $points_poll = $total_poll_options * $per_poll + $has_poll;
            }
            // If it's a new topic
            if ($mode == 'post' && $forum['forum_pertopic'] > 0) {
                // We calculate the total points
                $words_points = $topic_word * $sizeof_msg;
                $chars_points = $topic_char * $chars_msg;
                $total_points = $words_points + $chars_points + $forum['forum_pertopic'] + $points_attach + $points_poll;
                // We add the total points
                $this->functions_points->add_points($user_id, $total_points);
                // Add to the user
                $this->functions_points->add_points_to_table($post_id, $total_points, 'topic', $total_attachments, $total_poll_options);
                // Add to the post table
            } else {
                if (($mode == 'reply' || $mode == 'quote') && $forum['forum_perpost'] > 0) {
                    // We calculate the total points
                    $words_points = $post_word * $sizeof_msg;
                    $chars_points = $post_char * $chars_msg;
                    $total_points = $words_points + $chars_points + $forum['forum_perpost'] + $points_attach;
                    // We add the total points
                    $this->functions_points->add_points($user_id, $total_points);
                    // Add to the user
                    $this->functions_points->add_points_to_table($post_id, $total_points, 'post', $total_attachments, 0);
                    // Add to the post table
                } else {
                    if (($mode == 'edit_topic' || $mode == 'edit_first_post') && $forum['forum_peredit'] > 0) {
                        // We calculate the total points
                        $words_points = $topic_word * $sizeof_msg;
                        $chars_points = $topic_char * $chars_msg;
                        $total_points = $words_points + $chars_points + $forum['forum_peredit'] + $points_attach + $points_poll;
                        // We grab previously received points amount
                        $sql = 'SELECT points_topic_received
						FROM ' . POSTS_TABLE . '
						WHERE post_id = ' . (int) $post_id;
                        $result = $this->db->sql_query($sql);
                        $prev_points = $this->db->sql_fetchfield('points_topic_received');
                        $this->db->sql_freeresult($result);
                        // We calculate the difference
                        $difference = $total_points - $prev_points;
                        // We add the difference, only if it's positive, cause we're generous :-)
                        if ($difference > 0) {
                            $this->functions_points->add_points($user_id, $difference);
                            // Add to the user
                            $this->functions_points->add_points_to_table($post_id, $total_points, 'topic', $total_attachments, $total_poll_options);
                            // Update to the post table
                        } else {
                            return;
                            // "AM I NOT MERCIFUL??" - Caesar Commodus (Gladiator [2000])
                        }
                    } else {
                        if (($mode == 'edit' || $mode == 'edit_last_post') && $forum['forum_peredit'] > 0) {
                            // We calculate the total points
                            $words_points = $post_word * $sizeof_msg;
                            $chars_points = $post_char * $chars_msg;
                            $total_points = $words_points + $chars_points + $forum_['forum_peredit'] + $points_attach;
                            // We grab previously received points amount
                            $sql = 'SELECT points_post_received
						FROM ' . POSTS_TABLE . '
						WHERE post_id = ' . (int) $post_id;
                            $result = $this->db->sql_query($sql);
                            $prev_points = $this->db->sql_fetchfield('points_post_received');
                            $this->db->sql_freeresult($result);
                            // We calculate the difference
                            $difference = $total_points - $prev_points;
                            // We add the difference, only if it's positive, cause we're generous :-)
                            if ($difference > 0) {
                                $this->functions_points->add_points($user_id, $difference);
                                // Add to the user
                                $this->functions_points->add_points_to_table($post_id, $total_points, 'post', $total_attachments, 0);
                                // Update to the post table
                            } else {
                                return;
                                // "AM I NOT MERCIFUL??" - Caesar Commodus (Gladiator [2000])
                            }
                        } else {
                            // We do nothing..
                            return;
                            // The only thing necessary for the triumph of evil, is for good men to do nothing. - Edmund Burke
                        }
                    }
                }
            }
        } else {
            return;
        }
    }
예제 #9
0
    public function handle_ultimatepoints()
    {
        include $this->phpbb_root_path . 'includes/functions_user.' . $this->phpEx;
        include $this->phpbb_root_path . 'includes/functions_module.' . $this->phpEx;
        include $this->phpbb_root_path . 'includes/functions_display.' . $this->phpEx;
        include $this->phpbb_root_path . 'includes/functions_privmsgs.' . $this->phpEx;
        $mode = $this->request->variable('mode', '');
        // Read out config data
        $sql_array = array('SELECT' => 'config_name, config_value', 'FROM' => array($this->points_config_table => 'c'));
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query($sql);
        while ($row = $this->db->sql_fetchrow($result)) {
            $points_config[$row['config_name']] = $row['config_value'];
        }
        $this->db->sql_freeresult($result);
        // Read out config values
        $sql = 'SELECT *
				FROM ' . $this->points_values_table;
        $result = $this->db->sql_query($sql);
        $points_values = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        // Exclude Bots
        if ($this->user->data['is_bot']) {
            redirect(append_sid("{$this->phpbb_root_path}index.{$this->phpEx}"));
        }
        // Check if you are locked or not
        if (!$this->auth->acl_get('u_use_points')) {
            trigger_error('NOT_AUTHORISED');
        }
        // Get user's information
        $check_user = $this->request->variable('i', 0);
        $check_user = $check_user == 0 ? $this->user->data['user_id'] : $check_user;
        $sql_array = array('SELECT' => '*', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'u.user_id = ' . (int) $check_user);
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query($sql);
        $checked_user = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        if (!$checked_user) {
            trigger_error('POINTS_NO_USER');
        }
        // Ultimate Points Version
        $version = $this->config['ultimate_points_version'];
        // Check if points system is enabled
        if (!$this->config['points_enable']) {
            trigger_error($points_config['points_disablemsg']);
        }
        // Add the base entry into the Nav Bar at top
        $this->template->assign_block_vars('navlinks', array('U_VIEW_FORUM' => $this->helper->route('dmzx_ultimatepoints_controller'), 'FORUM_NAME' => sprintf($this->user->lang['POINTS_TITLE_MAIN'], $this->config['points_name'])));
        $this->template->assign_vars(array_change_key_case($checked_user, CASE_UPPER));
        $user_name = get_username_string('full', $this->user->data['user_id'], $this->user->data['username'], $this->user->data['user_colour'], $this->user->data['username']);
        $this->template->assign_vars(array_merge(array_change_key_case($points_config, CASE_UPPER), array('USER_POINTS' => $this->functions_points->number_format_points($this->user->data['user_points']), 'U_USE_POINTS' => $this->auth->acl_get('u_use_points'), 'U_CHG_POINTS' => $this->auth->acl_get('m_chg_points'), 'POINT_VERS' => $version, 'U_USE_TRANSFER' => $this->auth->acl_get('u_use_transfer'), 'U_USE_LOGS' => $this->auth->acl_get('u_use_logs'), 'U_USE_LOTTERY' => $this->auth->acl_get('u_use_lottery'), 'U_USE_BANK' => $this->auth->acl_get('u_use_bank'), 'U_USE_ROBBERY' => $this->auth->acl_get('u_use_robbery'))));
        switch ($mode) {
            case 'transfer_user':
                $this->points_transfer_user->main($checked_user);
                break;
            case 'logs':
                $this->points_logs->main($checked_user);
                break;
            case 'lottery':
                $this->points_lottery->main($checked_user);
                break;
            case 'transfer':
                $this->points_transfer->main($checked_user);
                break;
            case 'robbery':
                $this->points_robbery->main($checked_user);
                break;
            case 'points_edit':
                $this->points_points_edit->main();
                break;
            case 'bank':
                $this->points_bank->main();
                break;
            case 'bank_edit':
                $this->points_bank_edit->main();
                break;
            case 'info':
                $this->points_info->main();
                break;
            default:
                $this->points_main->main($checked_user);
                break;
        }
    }
    function main($checked_user)
    {
        // Get all values
        $sql = 'SELECT *
				FROM ' . $this->points_values_table;
        $result = $this->db->sql_query($sql);
        $points_values = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        // Get all point config names and config values
        $sql = 'SELECT config_name, config_value
				FROM ' . $this->points_config_table;
        $result = $this->db->sql_query($sql);
        while ($row = $this->db->sql_fetchrow($result)) {
            $points_config[$row['config_name']] = $row['config_value'];
        }
        $this->db->sql_freeresult($result);
        // Check, if user is allowed to use the robbery
        if (!$this->auth->acl_get('u_use_robbery')) {
            $message = $this->user->lang['NOT_AUTHORISED'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller') . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
        }
        // Check, if robbery is enabled
        if (!$points_config['robbery_enable']) {
            $message = $this->user->lang['ROBBERY_DISABLED'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller') . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
        }
        // Add part to bar
        $this->template->assign_block_vars('navlinks', array('U_VIEW_FORUM' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')), 'FORUM_NAME' => sprintf($this->user->lang['POINTS_ROBBERY'], $this->config['points_name'])));
        // Read out cash of current user
        $pointsa = $this->user->data['user_points'];
        // Check key
        add_form_key('robbery_attack');
        $submit = isset($_POST['submit']) ? true : false;
        if ($submit) {
            if (!check_form_key('robbery_attack')) {
                trigger_error('FORM_INVALID');
            }
            // Add all required informations
            $username = utf8_normalize_nfc($this->request->variable('username', '', true));
            $attacked_amount = round($this->request->variable('attacked_amount', 0.0), 2);
            if ($attacked_amount <= 0) {
                $message = $this->user->lang['ROBBERY_TOO_SMALL_AMOUNT'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Check, if user has entered the name of the user to be robbed
            if (empty($username)) {
                $message = $this->user->lang['ROBBERY_NO_ID_SPECIFIED'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Check, if user tries to rob himself
            if ($this->user->data['username_clean'] == utf8_clean_string($username)) {
                $message = $this->user->lang['ROBBERY_SELF'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Check, if user is trying to rob to much cash
            if ($points_values['robbery_loose'] != 0) {
                if ($this->user->data['user_points'] < $attacked_amount / 100 * $points_values['robbery_loose']) {
                    $message = $this->user->lang['ROBBERY_TO_MUCH'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                    trigger_error($message);
                }
            }
            // Select the user_id of user to be robbed
            $sql_array = array('SELECT' => 'user_id', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'username_clean = "' . $this->db->sql_escape(utf8_clean_string($username)) . '"');
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
            $result = $this->db->sql_query($sql);
            $user_id = (int) $this->db->sql_fetchfield('user_id');
            $this->db->sql_freeresult($result);
            // If no matching user id is found
            if (!$user_id) {
                $message = $this->user->lang['POINTS_NO_USER'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // If the robbed user doesn't have enough cash
            $sql_array = array('SELECT' => 'user_points', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $user_id);
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
            $result = $this->db->sql_query($sql);
            $pointsa = $this->db->sql_fetchfield('user_points');
            $this->db->sql_freeresult($result);
            if ($attacked_amount > $pointsa) {
                $message = $this->user->lang['ROBBERY_TO_MUCH_FROM_USER'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Check, if user tries to rob more than x % of users cash
            if ($points_values['robbery_max_rob'] != 0) {
                if ($attacked_amount > $pointsa / 100 * $points_values['robbery_max_rob']) {
                    $message = sprintf($this->user->lang['ROBBERY_MAX_ROB'], $points_values['robbery_max_rob']) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                    trigger_error($message);
                }
            }
            // Get some info about the robbed user
            $user_namepoints = get_username_string('full', $checked_user['user_id'], $checked_user['username'], $checked_user['user_colour']);
            // Genarate a random number
            $rand_base = $points_values['robbery_chance'];
            $rand_value = rand(0, 100);
            // If robbery was successful and notification is enabled, send notification
            if ($rand_value <= $rand_base) {
                $this->functions_points->add_points($this->user->data['user_id'], $attacked_amount);
                $this->functions_points->substract_points($user_id, $attacked_amount);
                // Add robbery to the log
                $sql = 'INSERT INTO ' . $this->points_log_table . ' ' . $this->db->sql_build_array('INSERT', array('point_send' => (int) $this->user->data['user_id'], 'point_recv' => $user_id, 'point_amount' => $attacked_amount, 'point_sendold' => $this->user->data['user_points'], 'point_recvold' => $pointsa, 'point_comment' => '', 'point_type' => '3', 'point_date' => time()));
                $this->db->sql_query($sql);
                if ($points_config['robbery_notify']) {
                    // Increase our notification sent counter
                    $this->config->increment('points_notification_id', 1);
                    // Store the notification data we will use in an array
                    $data = array('points_notify_id' => (int) $this->config['points_notification_id'], 'points_notify_msg' => sprintf($this->user->lang['NOTIFICATION_ROBBERY_SUCCES'], $attacked_amount, $this->config['points_name']), 'sender' => (int) $this->user->data['user_id'], 'receiver' => (int) $user_id, 'mode' => 'robbery');
                    // Create the notification
                    $this->notification_manager->add_notifications('dmzx.ultimatepoints.notification.type.points', $data);
                }
                $message = $this->user->lang['ROBBERY_SUCCESFUL'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            } else {
                if ($points_values['robbery_loose'] != 0) {
                    $lose = $attacked_amount / 100 * $points_values['robbery_loose'];
                    $this->functions_points->substract_points($this->user->data['user_id'], $lose);
                    if ($points_config['robbery_notify']) {
                        // Increase our notification sent counter
                        $this->config->increment('points_notification_id', 1);
                        // Store the notification data we will use in an array
                        $data = array('points_notify_id' => (int) $this->config['points_notification_id'], 'points_notify_msg' => $this->user->lang['NOTIFICATION_ROBBERY_FAILED'], 'sender' => (int) $this->user->data['user_id'], 'receiver' => (int) $user_id, 'mode' => 'robbery');
                        // Create the notification
                        $this->notification_manager->add_notifications('dmzx.ultimatepoints.notification.type.points', $data);
                    }
                    $message = $this->user->lang['ROBBERY_BAD'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                    trigger_error($message);
                }
            }
            $this->template->assign_vars(array('USER_NAME' => get_username_string('full', $checked_user['user_id'], $points_config['username'], $points_config['user_colour']), 'U_ACTION' => $this->u_action, 'S_HIDDEN_FIELDS' => $hidden_fields));
        }
        $this->template->assign_vars(array('USER_POINTS' => sprintf($this->functions_points->number_format_points($pointsa)), 'POINTS_NAME' => $this->config['points_name'], 'LOTTERY_NAME' => $points_values['lottery_name'], 'BANK_NAME' => $points_values['bank_name'], 'L_ROBBERY_CHANCE' => sprintf($this->user->lang['ROBBERY_CHANCE'], $this->functions_points->number_format_points($points_values['robbery_max_rob']), $this->functions_points->number_format_points($points_values['robbery_chance'])), 'L_ROBBERY_AMOUNTLOSE' => sprintf($this->user->lang['ROBBERY_AMOUNTLOSE'], $this->functions_points->number_format_points($points_values['robbery_loose'])), 'U_FIND_USERNAME' => append_sid("{$this->phpbb_root_path}memberlist.{$this->phpEx}", "mode=searchuser&amp;form=post&amp;field=username"), 'U_TRANSFER_USER' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')), 'U_LOGS' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs')), 'U_LOTTERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')), 'U_BANK' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')), 'U_ROBBERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')), 'U_INFO' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'info')), 'U_USE_TRANSFER' => $this->auth->acl_get('u_use_transfer'), 'U_USE_LOGS' => $this->auth->acl_get('u_use_logs'), 'U_USE_LOTTERY' => $this->auth->acl_get('u_use_lottery'), 'U_USE_BANK' => $this->auth->acl_get('u_use_bank'), 'U_USE_ROBBERY' => $this->auth->acl_get('u_use_robbery')));
        // Generate the page
        page_header($this->user->lang['POINTS_ROBBERY']);
        // Generate the page template
        $this->template->set_filenames(array('body' => 'points/points_robbery.html'));
        page_footer();
    }
 public function display_forumpoints()
 {
     // Grab some vars
     $action = $this->request->variable('action', '');
     $id = $this->request->variable('id', 0);
     $mode = $this->request->variable('mode', '');
     // Read out config data
     $sql_array = array('SELECT' => 'config_name, config_value', 'FROM' => array($this->points_config_table => 'c'));
     $sql = $this->db->sql_build_query('SELECT', $sql_array);
     $result = $this->db->sql_query($sql);
     while ($row = $this->db->sql_fetchrow($result)) {
         $points_config[$row['config_name']] = $row['config_value'];
     }
     $this->db->sql_freeresult($result);
     $this->template->assign_vars(array_change_key_case($points_config, CASE_UPPER));
     // Read out config data
     $sql_array = array('SELECT' => 'config_name, config_value', 'FROM' => array($this->points_config_table => 'c'));
     $sql = $this->db->sql_build_query('SELECT', $sql_array);
     $result = $this->db->sql_query($sql);
     while ($row = $this->db->sql_fetchrow($result)) {
         $points_config[$row['config_name']] = $row['config_value'];
     }
     $this->db->sql_freeresult($result);
     $this->template->assign_vars(array_change_key_case($points_config, CASE_UPPER));
     // Read out values data
     $sql_array = array('SELECT' => '*', 'FROM' => array($this->points_values_table => 'v'));
     $sql = $this->db->sql_build_query('SELECT', $sql_array);
     $result = $this->db->sql_query($sql);
     $points_values = $this->db->sql_fetchrow($result);
     $this->db->sql_freeresult($result);
     // Form key
     add_form_key('acp_points');
     $this->template->assign_vars(array('BASE' => $this->u_action));
     $submit = $this->request->variable('submit', '');
     $forum_data = $errors = array();
     $set_point_values = $this->request->variable('action_point_values', '');
     // Update forum points values
     if ($set_point_values) {
         if (confirm_box(true)) {
             $forum_topic = round($this->request->variable('forum_topic', 0.0), 2);
             $forum_post = round($this->request->variable('forum_post', 0.0), 2);
             $forum_edit = round($this->request->variable('forum_edit', 0.0), 2);
             $forum_cost = round($this->request->variable('forum_cost', 0.0), 2);
             $forum_cost_t = round($this->request->variable('forum_cost_topic', 0.0), 2);
             $forum_cost_p = round($this->request->variable('forum_cost_post', 0.0), 2);
             // Update values in phpbb_points_values
             $this->functions_points->set_points_values('forum_topic', $forum_topic);
             $this->functions_points->set_points_values('forum_post', $forum_post);
             $this->functions_points->set_points_values('forum_edit', $forum_edit);
             $this->functions_points->set_points_values('forum_cost', $forum_cost);
             $this->functions_points->set_points_values('forum_cost_topic', $forum_cost_t);
             $this->functions_points->set_points_values('forum_cost_post', $forum_cost_p);
             // Update all forum points
             $data = array('forum_pertopic' => $forum_topic, 'forum_perpost' => $forum_post, 'forum_peredit' => $forum_edit, 'forum_cost' => $forum_cost, 'forum_cost_topic' => $forum_cost_t, 'forum_cost_post' => $forum_cost_p);
             $sql = 'UPDATE ' . FORUMS_TABLE . ' SET ' . $this->db->sql_build_array('UPDATE', $data);
             $this->db->sql_query($sql);
             // Add logs
             $this->log->add('admin', $this->user->data['user_id'], $this->user->data['user_ip'], 'LOG_MOD_POINTS_FORUM');
             trigger_error($this->user->lang['FORUM_POINT_SETTINGS_UPDATED'] . adm_back_link($this->u_action));
         } else {
             $s_hidden_fields = build_hidden_fields(array('forum_topic' => $this->request->variable('forum_topic', 0.0), 'forum_post' => $this->request->variable('forum_post', 0.0), 'forum_edit' => $this->request->variable('forum_edit', 0.0), 'forum_cost' => $this->request->variable('forum_cost', 0.0), 'forum_cost_topic' => $this->request->variable('forum_cost_topic', 0.0), 'forum_cost_post' => $this->request->variable('forum_cost_post', 0.0), 'mode' => $mode, 'action' => $action, 'action_point_values' => true));
             confirm_box(false, 'FORUM_POINT_UPDATE', $s_hidden_fields);
         }
     }
     $this->template->assign_vars(array('FORUM_POINTS_NAME' => $this->config['points_name'], 'FORUM_TOPIC' => $points_values['forum_topic'], 'FORUM_POST' => $points_values['forum_post'], 'FORUM_EDIT' => $points_values['forum_edit'], 'FORUM_COST' => $points_values['forum_cost'], 'FORUM_COST_TOPIC' => $points_values['forum_cost_topic'], 'FORUM_COST_POST' => $points_values['forum_cost_post'], 'S_FORUMPOINTS' => true, 'U_ACTION' => $this->u_action));
 }
    function main()
    {
        // Get all values
        $sql = 'SELECT *
				FROM ' . $this->points_values_table;
        $result = $this->db->sql_query($sql);
        $points_values = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        // Check Points Config Table if Bank is Enabled
        $sql = 'SELECT config_value
				FROM ' . $this->points_config_table . '
				WHERE config_name = "bank_enable"';
        $result = $this->db->sql_query($sql);
        $is_bank_enabled = $this->db->sql_fetchfield('config_value');
        $this->db->sql_freeresult($result);
        // Check if bank is enabled
        if (1 > $points_values['bank_pay_period']) {
            $message = $this->user->lang['BANK_ERROR_PAYOUTTIME_SHORT'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
        }
        if ($is_bank_enabled != 1) {
            $message = $this->user->lang['BANK_DISABLED'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller') . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
        }
        if (!$this->auth->acl_get('u_use_bank')) {
            $message = $this->user->lang['NOT_AUTHORISED'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller') . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
        }
        $withdrawtotal_check = '';
        // Add part to bar
        $this->template->assign_block_vars('navlinks', array('U_VIEW_FORUM' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')), 'FORUM_NAME' => $points_values['bank_name']));
        // Check, if it's time to pay users
        $time = time();
        if ($time - $points_values['bank_last_restocked'] > $points_values['bank_pay_period']) {
            $this->functions_points->set_points_values('bank_last_restocked', $time);
            // Pay the users
            $sql = 'UPDATE ' . $this->points_bank_table . '
					SET holding = holding + round((holding / 100) * ' . $points_values['bank_interest'] . ')
					WHERE holding < ' . $points_values['bank_interestcut'] . '
						OR ' . $points_values['bank_interestcut'] . ' = 0';
            $this->db->sql_query($sql);
            // Mantain the bank costs
            if ($points_values['bank_cost'] != '0') {
                $sql = 'UPDATE ' . $this->points_bank_table . '
						SET holding = holding - ' . $points_values['bank_cost'] . '
						WHERE holding >= ' . $points_values['bank_cost'] . '';
                $this->db->sql_query($sql);
            }
            // Increase our notification sent counter
            $this->config->increment('points_notification_id', 1);
            $data = array('points_notify_id' => (int) $this->config['points_notification_id'], 'points_notify_msg' => $this->user->lang['NOTIFICATION_BANK_PAYOUT'], 'sender' => $this->user->data['user_id'], 'receiver' => (int) $this->user->data['user_id'], 'mode' => 'bank');
            // Send the notification
            $this->notification_manager->add_notifications('dmzx.ultimatepoints.notification.type.points', $data);
            $sql_array = array('SELECT' => 'username', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $this->user->data['user_id']);
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
            $result = $this->db->sql_query($sql);
            $points_user = $this->db->sql_fetchrow($result);
            // Add logs
            $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_MOD_POINTS_BANK_PAYS', false, array($points_user['username']));
        }
        $sql_array = array('SELECT' => '*', 'FROM' => array($this->points_bank_table => 'u'), 'WHERE' => 'user_id = ' . (int) $this->user->data['user_id']);
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query($sql);
        $row = $this->db->sql_fetchrow($result);
        $action = $this->request->variable('action', '');
        add_form_key('bank_action');
        // Default bank info page
        if (empty($action)) {
            $this->template->set_filenames(array('body' => 'points/points_bank.html'));
            if (!isset($row['holding']) && $this->user->data['user_id'] > 0 && $this->user->data['username'] != ANONYMOUS) {
                $this->template->assign_block_vars('no_account', array('USER_NO_ACCOUNT' => sprintf($this->user->lang['BANK_USER_NO_ACCOUNT'], $points_values['bank_name']), 'OPEN_ACCOUNT' => sprintf($this->user->lang['BANK_OPEN_ACCOUNT'], '<a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank', 'action' => 'createaccount')) . '" title="' . $this->user->lang['BANK_OPEN_ACCOUNT'] . '!">', '</a>')));
            } else {
                if ($this->user->data['user_id'] > 0 && $this->user->data['username'] != ANONYMOUS) {
                    $this->template->assign_block_vars('has_account', array());
                }
            }
            $sql_array = array('SELECT' => 'SUM(holding) AS total_holding, count(user_id) AS total_users', 'FROM' => array($this->points_bank_table => 'u'), 'WHERE' => 'id > 0');
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
            $result = $this->db->sql_query($sql);
            $b_row = $this->db->sql_fetchrow($result);
            $bankholdings = $b_row['total_holding'] ? $b_row['total_holding'] : 0;
            $bankusers = $b_row['total_users'];
            $withdrawtotal = $row['fees'] == 'on' ? $row['holding'] - round($row['holding'] / 100 * $points_values['bank_fees']) : $row['holding'];
            if ($row['fees'] == 'on' && $this->user->lang['BANK_WITHDRAW_RATE']) {
                $this->template->assign_block_vars('switch_withdraw_fees', array());
            }
            if ($points_values['bank_min_withdraw']) {
                $this->template->assign_block_vars('switch_min_with', array());
            }
            if ($points_values['bank_min_deposit']) {
                $this->template->assign_block_vars('switch_min_depo', array());
            }
            $banklocation = ' -> <a href="' . $this->helper->route('dmzx_ultimatepoints_controller') . '" class="nav">' . $points_values['bank_name'] . '</a>';
            $title = $points_values['bank_name'] . '; ' . (!is_numeric($row['holding']) ? $this->user->lang['BANK_ACCOUNT_OPENING'] : $this->user->lang['BANK_DEPOSIT_WITHDRAW'] . ' ' . $this->config['points_name']);
            page_header($points_values['bank_name']);
            $bank_enable = $is_bank_enabled;
            $this->template->assign_vars(array('BANK_NAME' => $points_values['bank_name'], 'BANKLOCATION' => $banklocation, 'BANK_OPENED' => $this->user->format_date($bank_enable), 'BANK_HOLDINGS' => sprintf($this->functions_points->number_format_points($bankholdings)), 'BANK_ACCOUNTS' => $bankusers, 'BANK_FEES' => $points_values['bank_fees'], 'BANK_INTEREST' => $points_values['bank_interest'], 'BANK_MIN_WITH' => sprintf($this->functions_points->number_format_points($points_values['bank_min_withdraw'])), 'BANK_MIN_DEPO' => sprintf($this->functions_points->number_format_points($points_values['bank_min_deposit'])), 'BANK_MAX_HOLD' => sprintf($this->functions_points->number_format_points($points_values['bank_interestcut'])), 'BANK_TITLE' => $title, 'POINTS_NAME' => $this->config['points_name'], 'USER_BALANCE' => sprintf($this->functions_points->number_format_points($row['holding'])), 'USER_GOLD' => $this->user->data['user_points'], 'USER_WITHDRAW' => sprintf(number_format($withdrawtotal, 2, '.', '')), 'U_WITHDRAW' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank', 'action' => 'withdraw')), 'U_DEPOSIT' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank', 'action' => 'deposit'))));
        } else {
            if ($action == 'createaccount') {
                if (!$this->user->data['is_registered']) {
                    login_box();
                }
                $this->template->set_filenames(array('body' => 'points/points_bank.html'));
                if (is_numeric($row['holding'])) {
                    trigger_error(' ' . $this->user->lang['YES_ACCOUNT'] . '!<br /><br />' . sprintf($this->user->lang['BANK_BACK_TO_BANK'], '<a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')) . '">', '</a>') . sprintf('<br />' . $this->user->lang['BANK_BACK_TO_INDEX'], '<a href="' . append_sid("{$this->phpbb_root_path}index.{$this->phpEx}") . '">', '</a>'));
                } else {
                    $sql = 'INSERT INTO ' . $this->points_bank_table . ' ' . $this->db->sql_build_array('INSERT', array('user_id' => (int) $this->user->data['user_id'], 'opentime' => time(), 'fees' => 'on'));
                    $this->db->sql_query($sql);
                    trigger_error(' ' . $this->user->lang['BANK_WELCOME_BANK'] . ' ' . $points_values['bank_name'] . '! <br />' . $this->user->lang['BANK_START_BALANCE'] . '<br />' . $this->user->lang['BANK_YOUR_ACCOUNT'] . '!<br /><br />' . sprintf($this->user->lang['BANK_BACK_TO_BANK'], '<a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')) . '">', '</a>') . sprintf('<br />' . $this->user->lang['BANK_BACK_TO_INDEX'], '<a href="' . append_sid("{$this->phpbb_root_path}index.{$this->phpEx}") . '">', '</a>'));
                }
            } else {
                if ($action == 'deposit') {
                    if (!check_form_key('bank_action')) {
                        trigger_error('FORM_INVALID');
                    }
                    $deposit = round($this->request->variable('deposit', 0.0), 2);
                    if (!$this->user->data['is_registered']) {
                        login_box();
                    }
                    if ($deposit < $points_values['bank_min_deposit']) {
                        $message = sprintf($this->user->lang['BANK_DEPOSIT_SMALL_AMOUNT'], $points_values['bank_min_deposit'], $this->config['points_name']) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                        trigger_error($message);
                    } else {
                        if ($deposit < 1) {
                            $message = $this->user->lang['BANK_ERROR_DEPOSIT'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                            trigger_error($message);
                        } else {
                            if ($deposit > $this->user->data['user_points']) {
                                $message = sprintf($this->user->lang['BANK_ERROR_NOT_ENOUGH_DEPOSIT'], $this->config['points_name']) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                                trigger_error($message);
                            }
                        }
                    }
                    $this->functions_points->substract_points($this->user->data['user_id'], $deposit);
                    $sql_array = array('SELECT' => 'holding, totaldeposit', 'FROM' => array($this->points_bank_table => 'b'), 'WHERE' => 'user_id = ' . (int) $this->user->data['user_id']);
                    $sql = $this->db->sql_build_query('SELECT', $sql_array);
                    $result = $this->db->sql_query($sql);
                    $user_bank = $this->db->sql_fetchrow($result);
                    $user_holding = $user_bank['holding'];
                    $user_totaldeposit = $user_bank['totaldeposit'];
                    $this->db->sql_freeresult($result);
                    $data = array('holding' => $user_holding + $deposit, 'totaldeposit' => $user_totaldeposit + $deposit);
                    $sql = 'UPDATE ' . $this->points_bank_table . '
					SET ' . $this->db->sql_build_array('UPDATE', $data) . '
					WHERE user_id = ' . (int) $this->user->data['user_id'];
                    $this->db->sql_query($sql);
                    trigger_error(' ' . $this->user->lang['BANK_HAVE_DEPOSIT'] . ' ' . sprintf($this->functions_points->number_format_points($deposit)) . ' ' . $this->config['points_name'] . ' ' . $this->user->lang['BANK_TO_ACCOUNT'] . '<br />' . $this->user->lang['BANK_NEW_BALANCE'] . ' ' . sprintf($this->functions_points->number_format_points($row['holding'] + $deposit)) . '.<br />' . $this->user->lang['BANK_LEAVE_WITH'] . ' ' . sprintf($this->functions_points->number_format_points($this->user->data['user_points'] - $deposit)) . ' ' . $this->config['points_name'] . ' ' . $this->user->lang['BANK_ON_HAND'] . '.<br /><br />' . sprintf($this->user->lang['BANK_BACK_TO_BANK'], '<a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')) . '">', '</a>') . sprintf('<br />' . $this->user->lang['BANK_BACK_TO_INDEX'], '<a href="' . append_sid("{$this->phpbb_root_path}index.{$this->phpEx}") . '">', '</a>'));
                } else {
                    if ($action == 'withdraw') {
                        if (!check_form_key('bank_action')) {
                            trigger_error('FORM_INVALID');
                        }
                        $withdraw = round($this->request->variable('withdraw', 0.0), 2);
                        if (!$this->user->data['is_registered']) {
                            login_box();
                        }
                        if ($withdraw < $points_values['bank_min_withdraw']) {
                            $message = sprintf($this->user->lang['BANK_WITHDRAW_SMALL_AMOUNT'], $points_values['bank_min_withdraw'], $this->config['points_name']) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                            trigger_error($message);
                        } else {
                            if ($withdraw < 1) {
                                $message = $this->user->lang['BANK_ERROR_WITHDRAW'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                                trigger_error($message);
                            }
                        }
                        if ($row['fees'] == 'on') {
                            $withdrawtotal_check = $row['fees'] == 'on' ? $row['holding'] - round($row['holding'] / 100 * $points_values['bank_fees']) : $row['holding'];
                            $fees = round($row['holding'] / 100 * $points_values['bank_fees']);
                            if ($withdraw == $withdrawtotal_check) {
                                $withdrawtotal = $withdraw + $fees;
                            } else {
                                $withdrawtotal = round($withdraw / 100 * $points_values['bank_fees']) + $withdraw;
                            }
                        } else {
                            $withdrawtotal = 0;
                        }
                        if ($row['holding'] < $withdrawtotal) {
                            $message = sprintf($this->user->lang['BANK_ERROR_NOT_ENOUGH_WITHDRAW'], $this->config['points_name']) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                            trigger_error($message);
                        }
                        // Transfer points to users cash account
                        $this->functions_points->add_points($this->user->data['user_id'], $withdraw);
                        // Update users bank account
                        $sql_array = array('SELECT' => 'holding, totalwithdrew', 'FROM' => array($this->points_bank_table => 'b'), 'WHERE' => 'user_id = ' . (int) $this->user->data['user_id']);
                        $sql = $this->db->sql_build_query('SELECT', $sql_array);
                        $result = $this->db->sql_query($sql);
                        $user_bank = $this->db->sql_fetchrow($result);
                        $user_holding = $user_bank['holding'];
                        $user_totalwithdrew = $user_bank['totalwithdrew'];
                        $this->db->sql_freeresult($result);
                        $data = array('holding' => $user_holding - $withdrawtotal, 'totalwithdrew' => $user_totalwithdrew + $withdraw);
                        $sql = 'UPDATE ' . $this->points_bank_table . '
				SET ' . $this->db->sql_build_array('UPDATE', $data) . '
				WHERE user_id = ' . (int) $this->user->data['user_id'];
                        $this->db->sql_query($sql);
                        trigger_error(' ' . $this->user->lang['BANK_HAVE_WITHDRAW'] . ' ' . sprintf($this->functions_points->number_format_points($withdraw)) . ' ' . $this->config['points_name'] . ' ' . $this->user->lang['BANK_FROM_ACCOUNT'] . '. <br />' . $this->user->lang['BANK_NEW_BALANCE'] . ' ' . sprintf($this->functions_points->number_format_points($row['holding'] - $withdrawtotal)) . ' ' . $this->config['points_name'] . '.<br />' . $this->user->lang['BANK_NOW_HAVE'] . ' ' . sprintf($this->functions_points->number_format_points($this->user->data['user_points'] + $withdraw)) . ' ' . $this->config['points_name'] . ' ' . $this->user->lang['BANK_ON_HAND'] . '.<br /><br />' . sprintf($this->user->lang['BANK_BACK_TO_BANK'], '<a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')) . '">', '</a>') . sprintf('<br />' . $this->user->lang['BANK_BACK_TO_INDEX'], '<a href="' . append_sid("{$this->phpbb_root_path}index.{$this->phpEx}") . '">', '</a>'));
                    } else {
                        redirect($this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')));
                    }
                }
            }
        }
        // Generate most rich banker to show
        $limit = $points_values['number_show_top_points'];
        $sql_array = array('SELECT' => 'u.user_id, u.username, u.user_colour, b.*', 'FROM' => array(USERS_TABLE => 'u'), 'LEFT_JOIN' => array(array('FROM' => array($this->points_bank_table => 'b'), 'ON' => 'u.user_id = b.user_id')), 'WHERE' => 'b.holding > 0', 'ORDER_BY' => 'b.holding DESC, u.username ASC');
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query_limit($sql, $limit);
        while ($row = $this->db->sql_fetchrow($result)) {
            $this->template->assign_block_vars('bank', array('USERNAME' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']), 'POINT' => sprintf($this->functions_points->number_format_points($row['holding']))));
        }
        $this->db->sql_freeresult($result);
        // Generate the time format
        function time_format($secs, $filter = false)
        {
            global $user;
            $output = '';
            $filter = $filter ? explode('|', strtolower($filter)) : false;
            $time_array = array('year' => 60 * 60 * 24 * 365, 'month' => 60 * 60 * 24 * 30, 'week' => 60 * 60 * 24 * 7, 'day' => 60 * 60 * 24, 'hour' => 60 * 60, 'minute' => 60, 'second' => 0);
            foreach ($time_array as $key => $value) {
                if ($filter && !in_array($key, $filter)) {
                    continue;
                }
                $item = $value ? intval(intval($secs) / $value) : intval($secs);
                if ($item > 0) {
                    $secs = $secs - $item * $value;
                    $output .= ' ' . $item . ' ' . ($item > 1 ? $user->lang['TIME_' . strtoupper($key) . 'S'] : $user->lang['TIME_' . strtoupper($key)]);
                }
            }
            return $output;
        }
        $this->template->assign_vars(array('BANK_INTEREST_PERIOD' => time_format($points_values['bank_pay_period']), 'BANK_COST' => sprintf($this->functions_points->number_format_points($points_values['bank_cost'])), 'LOTTERY_NAME' => $points_values['lottery_name'], 'BANK_NAME' => $points_values['bank_name'], 'BANK_NOBODY_IN_BANK' => sprintf($this->user->lang['BANK_NOBODY_IN_BANK'], $this->config['points_name'], $points_values['bank_name']), 'S_DISPLAY_INDEX' => $points_values['number_show_top_points'] > 0 ? true : false, 'L_BANK_DESCRIPTION' => sprintf($this->user->lang['BANK_DESCRIPTION'], $this->config['points_name']), 'U_TRANSFER_USER' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')), 'U_LOGS' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs')), 'U_LOTTERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')), 'U_BANK' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')), 'U_ROBBERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')), 'U_INFO' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'info')), 'U_USE_TRANSFER' => $this->auth->acl_get('u_use_transfer'), 'U_USE_LOGS' => $this->auth->acl_get('u_use_logs'), 'U_USE_LOTTERY' => $this->auth->acl_get('u_use_lottery'), 'U_USE_BANK' => $this->auth->acl_get('u_use_bank'), 'U_USE_ROBBERY' => $this->auth->acl_get('u_use_robbery')));
        page_footer();
    }
    function main($checked_user)
    {
        add_form_key('transfer_user');
        // Get all point config names and config values
        $sql = 'SELECT config_name, config_value
				FROM ' . $this->points_config_table;
        $result = $this->db->sql_query($sql);
        while ($row = $this->db->sql_fetchrow($result)) {
            $points_config[$row['config_name']] = $row['config_value'];
        }
        $this->db->sql_freeresult($result);
        // Get all values
        $sql = 'SELECT *
				FROM ' . $this->points_values_table;
        $result = $this->db->sql_query($sql);
        $points_values = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        // Grab the message variable
        $message = $this->request->variable('comment', '', true);
        // Check, if transferring is allowed
        if (!$points_config['transfer_enable']) {
            $message = $this->user->lang['TRANSFER_REASON_TRANSFER'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller') . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
        }
        // Check, if user is allowed to use the transfer module
        if (!$this->auth->acl_get('u_use_transfer')) {
            $message = $this->user->lang['NOT_AUTHORISED'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller') . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
        }
        // Add part to bar
        $this->template->assign_block_vars('navlinks', array('U_VIEW_FORUM' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')), 'FORUM_NAME' => sprintf($this->user->lang['TRANSFER_TITLE'], $this->config['points_name'])));
        $submit = isset($_POST['submit']) ? true : false;
        if ($submit) {
            if (!check_form_key('transfer_user')) {
                trigger_error('FORM_INVALID');
            }
            // Grab needed variables for the transfer
            $am = round($this->request->variable('amount', 0.0), 2);
            $comment = $this->request->variable('comment', '', true);
            $username1 = $this->request->variable('username', '', true);
            $username = strtolower($username1);
            // Select the user data to transfer to
            $sql_array = array('SELECT' => '*', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'username_clean = "' . $this->db->sql_escape(utf8_clean_string($username)) . '"');
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
            $result = $this->db->sql_query($sql);
            $transfer_user = $this->db->sql_fetchrow($result);
            $this->db->sql_freeresult($result);
            if ($transfer_user == null) {
                $message = $this->user->lang['TRANSFER_NO_USER_RETURN'] . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Select the old user_points from user_id to transfer to
            $sql_array = array('SELECT' => 'user_points', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $transfer_user['user_id']);
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
            $result = $this->db->sql_query($sql);
            $transfer_user_old_points = (int) $this->db->sql_fetchfield('user_points');
            $this->db->sql_freeresult($result);
            // Check, if the sender has enough cash
            if ($this->user->data['user_points'] < $am) {
                $message = sprintf($this->user->lang['TRANSFER_REASON_MINPOINTS'], $this->config['points_name']) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Check, if the amount is 0 or below
            if ($am <= 0) {
                $message = sprintf($this->user->lang['TRANSFER_REASON_UNDERZERO'], $this->config['points_name']) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Check, if user is trying to send to himself
            if ($this->user->data['user_id'] == $transfer_user['user_id']) {
                $message = sprintf($this->user->lang['TRANSFER_REASON_YOURSELF'], $this->config['points_name']) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
                trigger_error($message);
            }
            // Add cash to receiver
            $amount = (100 - $points_values['transfer_fee']) / 100 * $am;
            // Deduct transfer fee
            $this->functions_points->add_points($transfer_user['user_id'], $amount);
            // Remove cash from sender
            $this->functions_points->substract_points($this->user->data['user_id'], $am);
            // Get current time for log
            $current_time = time();
            // Add transferring information to the log
            $text = utf8_normalize_nfc($message);
            $sql = 'INSERT INTO ' . $this->points_log_table . ' ' . $this->db->sql_build_array('INSERT', array('point_send' => (int) $this->user->data['user_id'], 'point_recv' => (int) $transfer_user['user_id'], 'point_amount' => $am, 'point_sendold' => $this->user->data['user_points'], 'point_recvold' => $transfer_user_old_points, 'point_comment' => $text, 'point_type' => '1', 'point_date' => $current_time));
            $this->db->sql_query($sql);
            // Send pm to receiver, if PM is enabled
            if (!$points_config['transfer_pm_enable'] == 0 && $transfer_user['user_allow_pm']) {
                $points_name = $this->config['points_name'];
                $comment = $this->db->sql_escape($comment);
                $pm_subject = utf8_normalize_nfc(sprintf($this->user->lang['TRANSFER_PM_SUBJECT']));
                $pm_text = utf8_normalize_nfc(sprintf($this->user->lang['TRANSFER_PM_BODY'], $amount, $points_name, $text));
                $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($transfer_user['user_id'] => 'to')), 'from_user_id' => $this->user->data['user_id'], 'from_username' => $this->user->data['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);
            }
            // Change $username back to regular username
            $sql_array = array('SELECT' => 'username', 'FROM' => array(USERS_TABLE => 'u'), 'WHERE' => 'user_id = ' . (int) $transfer_user['user_id']);
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
            $result = $this->db->sql_query($sql);
            $show_user = $this->db->sql_fetchfield('username');
            $this->db->sql_freeresult($result);
            // Show the successful transfer message
            $message = sprintf($this->user->lang['TRANSFER_REASON_TRANSUCC'], $this->functions_points->number_format_points($am), $this->config['points_name'], $show_user) . '<br /><br /><a href="' . $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')) . '">&laquo; ' . $this->user->lang['BACK_TO_PREV'] . '</a>';
            trigger_error($message);
            $this->template->assign_vars(array('U_ACTION' => $this->u_action));
        }
        $this->template->assign_vars(array('USER_POINTS' => sprintf($this->functions_points->number_format_points($checked_user['user_points'])), 'POINTS_NAME' => $this->config['points_name'], 'POINTS_COMMENTS' => $points_config['comments_enable'] ? true : false, 'TRANSFER_FEE' => $points_values['transfer_fee'], 'LOTTERY_NAME' => $points_values['lottery_name'], 'BANK_NAME' => $points_values['bank_name'], 'L_TRANSFER_DESCRIPTION' => sprintf($this->user->lang['TRANSFER_DESCRIPTION'], $this->config['points_name']), 'U_TRANSFER_USER' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'transfer_user')), 'U_LOGS' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'logs')), 'U_LOTTERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'lottery')), 'U_BANK' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'bank')), 'U_ROBBERY' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'robbery')), 'U_INFO' => $this->helper->route('dmzx_ultimatepoints_controller', array('mode' => 'info')), 'U_FIND_USERNAME' => append_sid("{$this->phpbb_root_path}memberlist.{$this->phpEx}", "mode=searchuser&amp;form=post&amp;field=username"), 'U_USE_TRANSFER' => $this->auth->acl_get('u_use_transfer'), 'U_USE_LOGS' => $this->auth->acl_get('u_use_logs'), 'U_USE_LOTTERY' => $this->auth->acl_get('u_use_lottery'), 'U_USE_BANK' => $this->auth->acl_get('u_use_bank'), 'U_USE_ROBBERY' => $this->auth->acl_get('u_use_robbery'), 'S_ALLOW_SEND_PM' => $this->auth->acl_get('u_sendpm')));
        // Generate the page
        page_header(sprintf($this->user->lang['TRANSFER_TITLE'], $this->config['points_name']));
        // Generate the page template
        $this->template->set_filenames(array('body' => 'points/points_transfer_user.html'));
        page_footer();
    }