public function prepare()
 {
     $this->template = "player_stats";
     $this->tab = "player";
     //backpack::update_schema();
     if (is_id64($this->request[0])) {
         $player_id = $this->request[0];
     } else {
         $player_id = get_id64($this->request[0]);
         if (!$player_id) {
             die("404");
         }
     }
     $player = new player($player_id);
     die("302");
 }
    public function prepare()
    {
        global $db, $user, $settings;
        // auth check
        $auth = $db->query_first("SELECT mp.type FROM  tf2stats_map_to_player mp LEFT JOIN tf2_maps m ON m.id = mp.map_id\r\n\t\t\t\t\t\tWHERE mp.player_id = %s AND m.name = %s", array($user->id(), $this->request[0]));
        if (!in_array($auth['type'], array('M', 'A', 'C'))) {
            page::error("Little man", "You are no match for me!");
        }
        // handle file removals
        if ($this->request[1] == 'delimg') {
            $db->query("DELETE FROM tf2stats_map_images WHERE image = %s", array($this->request[2]));
            $this->params['success'] = "Deleted " . $this->request[2];
        }
        // update
        if ($_REQUEST['update']) {
            if ($_REQUEST['filesize'] && !is_numeric($_REQUEST['filesize'])) {
                $this->params['error'] = 'Filesize must be numeric. Do not append "MB".';
            } elseif ($_REQUEST['url'] && !filter_var($_REQUEST['url'], FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)) {
                $this->params['error'] = 'Download URL is not valid.';
            } else {
                $i = $db->query_first("SELECT m.id, m.official FROM tf2_maps m WHERE m.name = %s", array($this->request[0]));
                if (IsUserAdmin()) {
                    $Official = (int) (isset($_REQUEST['official']) && $_REQUEST['official'] == 'official');
                    cache::log("Changing official status for " . $i['id'] . " - old: " . $i['official'] . " - new: " . $Official);
                    if ($i['official'] != $Official) {
                        $db->query("UPDATE tf2_maps SET official = %s WHERE id = %s", array($Official, $i['id']));
                    }
                }
                $db->query("INSERT INTO tf2stats_managed_maps (player_id, map_id, edit_time, description, file_size, download_url) VALUES(%s, %s, %s, %s, %s, %s)\r\n\t\t\t\t\t\t\tON DUPLICATE KEY UPDATE edit_time=%s, description=%s, file_size=%s, download_url = %s", array($user->id(), $i['id'], time(), $_REQUEST['description'], $_REQUEST['filesize'], $_REQUEST['url'], time(), $_REQUEST['description'], $_REQUEST['filesize'], $_REQUEST['url']));
            }
        }
        // map info
        $map_info = $db->query_first("SELECT m.name, m.id, m.official, mp.description, mp.file_size, mp.download_url, mp.edit_time, p.name as player_name FROM  tf2stats_managed_maps mp \r\n\t\t\t\t\t\tLEFT JOIN tf2_maps m ON m.id = mp.map_id\r\n\t\t\t\t\t\tLEFT JOIN tf2_players p on mp.player_id = p.id\r\n\t\t\t\t\t\tWHERE m.name = %s\r\n\t\t\t\t\t\tORDER BY edit_time DESC\r\n\t\t\t\t\t\tLIMIT 1", array($this->request[0]));
        if ($map_info) {
            $this->params['old'] = true;
        } else {
            $map_info = $db->query_first("SELECT m.name,  m.id FROM tf2_maps m WHERE m.name = %s", array($this->request[0]));
        }
        $this->params['map_info'] = $map_info;
        // handle adding authors.
        if ($this->request[1] == 'addauthor') {
            if ($this->request['search']) {
                $player_id = $this->request['search'];
                if (!is_id64($player_id)) {
                    $player_id = get_id64($this->request['search']);
                }
                $player = new player($player_id);
                if ($player->id()) {
                    $db->query("INSERT INTO tf2stats_map_to_player(player_id, map_id, type) VALUES(%s, %s, %s)", array($player->id(), $map_info['id'], 'A'));
                    $this->params['success'] = $player_id . ' has been added to the author list.';
                } else {
                    $this->params['error'] = "Could not find a player by '" . $_REQUEST['search'] . "'. Please refine your search.";
                }
            } else {
                $this->template = "manage_map_author";
                $this->title = sprintf("Adding author for %s", htmlspecialchars($map_info['name']));
                return;
            }
        }
        if ($this->request[1] == 'delauthor') {
            $id = $this->request[2];
            $db->query("DELETE FROM tf2stats_map_to_player WHERE player_id=%s AND map_id = %s", array($id, $map_info['id']));
            $this->params['success'] = "Deleted author";
        }
        // handle file uploads.
        if ($this->request[1] == 'upload') {
            $this->template = "manage_map_upload";
            $this->title = sprintf("Upload image for %s", htmlspecialchars($map_info['name']));
            $this->params['allowed_images'] = implode(', ', $settings['upload']['allowed_images']);
            if ($_FILES['image']) {
                if (!$_FILES['image']['tmp_name']) {
                    $this->params['error'] = 'Upload failed. (This usually happens when you try to upload a file larger than 1MB!)';
                    return;
                }
                // check extension.
                $ext = end(explode(".", strtolower($_FILES['image']['name'])));
                if (!in_array($ext, $settings['upload']['allowed_images'])) {
                    $this->params['error'] = 'Unsupported file extension ' . $ext . '. Please convert your image to one of these formats: ' . implode(', ', $settings['upload']['allowed_images']);
                    return;
                }
                // rename if already exists
                $filename = sprintf("%s_%s", $map_info['id'], str_replace(array('(', ')', ' '), '_', basename($_FILES['image']['name'])));
                $target_path = $settings['upload']['folder']['maps'] . $filename;
                while (file_exists($target_path)) {
                    $filename = md5(time() . rand()) . '.' . $ext;
                    $target_path = $settings['upload']['folder']['maps'] . $filename;
                }
                //var_dump($target_path);
                if (filesize($_FILES['image']['tmp_name']) > 2097152) {
                    $this->params['error'] = 'Uploaded file cannot exceed 1MB.';
                    return;
                }
                if (move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
                    $db->query("INSERT INTO tf2stats_map_images (map_id, player_id, image) VALUES(%s, %s, %s)", array($map_info['id'], $user->id(), $filename));
                    $this->params['success'] = basename($_FILES['image']['name']) . ' uploaded successfully.';
                } else {
                    echo $_FILES['image']['tmp_name'];
                    echo $target_path;
                    $this->params['error'] = 'Unknown error. Please nag FireSlash until he fixes it.';
                }
            }
            return;
        }
        // tinyMCE setup
        $this->head .= '<script type="text/javascript" src="/static/js/tiny_mce/jquery.tinymce.js"></script>
		<script type="text/javascript">
	$().ready(function() {
		$(\'textarea.tinymce\').tinymce({
			// Location of TinyMCE script
			script_url : "/static/js/tiny_mce/tiny_mce.js",

			theme : "advanced",
			mode : "none",
			plugins : "bbcode",
			theme_advanced_buttons1 : "bold,italic,underline,undo,redo,link,unlink,image,forecolor,styleselect,removeformat,cleanup,code",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_styles : "Code=codeStyle;Quote=quoteStyle",
			content_css : "css/bbcode.css",
			entity_encoding : "raw",
			add_unload_trigger : false,
			remove_linebreaks : false,
			inline_styles : false,
			convert_fonts_to_spans : false,
			apply_source_formatting : false
			
		});
	});
</script>
		';
        // map info
        $this->template = "manage_map";
        require_once 'classes/map.php';
        $m = new map($this->request[0]);
        $this->params['images'] = $m->get_images('xy165');
        $this->params['has_images'] = $this->params['images'];
        $this->params['can_set_official'] = IsUserAdmin();
        // associated peoples
        $db->query("SELECT p.id, p.name, mp.type from tf2stats_map_to_player mp LEFT JOIN tf2_players p ON mp.player_id = p.id WHERE mp.map_id = %s", array($map_info['id']));
        while ($row = $db->fetch_array()) {
            $row['del_link'] = sprintf('/manage_map/%s/delauthor/%s/', $this->request[0], $row['id']);
            $p[] = $row;
        }
        $this->title = sprintf("Managing %s", htmlspecialchars($this->request[0]));
        $this->params['people'] = $p;
    }
 public function prepare()
 {
     global $METRICS, $CLASS_METRICS, $CLASSES, $CLASS_SLOTS, $VALVE_EMPLOYEES;
     $this->template = "player_stats";
     $this->tab = "player";
     //backpack::update_schema();
     if (is_id64($this->request[0])) {
         $player_id = $this->request[0];
     } else {
         $player_id = get_id64($this->request[0]);
         if (!$player_id) {
             page::error("I will eat your search results, I'll eat them up!", "Thats right nancy boy, I've got better things\r\n\t\t\t\tto do than look for some sissy boy stats page, there's a war going on! The only path to victory is through\r\n\t\t\t\tpain and bloodshed. So man up girly boy, and put the right thing in the box next time.", array('image' => "soldier"));
         }
     }
     $player = new player($player_id, true);
     $player->preload_stats(true);
     $backpack = new backpack($player, true);
     cache::multi_run();
     $stats = $player->get_stats();
     if ($player->info['custom_url']) {
         $this->canonical = sprintf('http://tf2stats.net/player/%s/', $player->info['custom_url']);
     } else {
         $this->canonical = sprintf('http://tf2stats.net/player/%s/', $player->id64());
     }
     //if($player_id != "76561198003273729")
     $player->log_view();
     foreach ($CLASSES as $c) {
         // Build sorted class array
         $classes[$stats[$c]['accum']['iplaytime']] = $c;
         foreach ($METRICS as $m => $l) {
             // Determine peak class values
             if ($stats[$c]['max'][$m] > $stats['all']['max'][$m]['value']) {
                 $stats['all']['max'][$m] = array('value' => $stats[$c]['max'][$m], 'class' => $c);
             }
             if ($stats[$c]['accum'][$m] > $stats['all']['best'][$m]) {
                 $stats['all']['best'][$m] = $stats[$c]['accum'][$m];
             }
             $stats['all']['accum'][$m] += $stats[$c]['accum'][$m];
         }
     }
     krsort($classes);
     $this->params['classes'] = $classes;
     $this->params['metrics'] = $METRICS;
     $this->params['class_metrics'] = $CLASS_METRICS;
     $this->params['backpack'] = $backpack;
     $this->params['equipped'] = $backpack->equipped;
     $this->params['player'] = $player;
     $this->params['stats'] = $stats;
     $this->params['slots'] = $CLASS_SLOTS;
     //var_dump($backpack->equipped['soldier']['primary']);
     //var_dump($backpack->equipped);
     //var_dump($backpack->items);
     //echo decbin(1082130432);
     //$up = unpack('f',1082130432);
     //var_dump($up);
     for ($x = 1; $x <= 1050; $x++) {
         $fullbp[$x] = false;
     }
     $invalids = array_slice($fullbp, 1000, 50);
     $ix = 0;
     $maxpage = 300;
     if (is_array($backpack->items)) {
         foreach ($backpack->items as $i) {
             unset($tail);
             if ($fullbp[$i['position']] || !$i['position']) {
                 $invalids[$ix++] = $i;
             } else {
                 $fullbp[$i['position']] = $i;
             }
             if ($i['position'] > $maxpage) {
                 $maxpage = $i['position'];
             }
         }
     }
     for ($x = 0; $x < $maxpage; $x += 50) {
         $bpp[] = array('items' => array_slice($fullbp, $x, 50));
     }
     if ($ix > 0) {
         $bpp[] = array('items' => $invalids);
     }
     $this->params['bpp'] = $bpp;
     $this->title = htmlspecialchars($player->info['name']);
     $this->params['player_name'] = $this->title;
     if ($player_id === '76561197972495328') {
         $this->params['player_name'] .= ' — <span style="color:#4d8bd0">TF2Stats Creator</span>';
     } else {
         if ($player_id === '76561197972494985') {
             $this->params['player_name'] = '<a href="//xpaw.ru" target="_blank" style="text-decoration:none">' . $this->title . '</a> — <span style="color:#d0514d">TF2Stats Maintainer</span>';
         }
     }
     $this->params['valve_employee'] = in_array($player_id, $VALVE_EMPLOYEES);
 }