Location: PHPKode > projects > ezStats for XBOX Live > ezStats2_xbl/index.php
<?php
	### Weiterleitung auf den Installer ###
	if (!file_exists('tmp/sql.php')) {
		header("Status: 301 Moved Permanently");
		header("Location:install/");
		exit; 
	}
	
	
	### Klassen laden ###
	if (true) {
		require 'tmp/sql.php';
		require 'classes/mysql.php';
		
		$sql = new MySQL(); if ($sql->debug) error_reporting (E_ALL|E_STRICT); else error_reporting (0);
		
		require 'plugins/phpQuery.php';
		require 'classes/settings.php';
		require 'classes/security.php';
		require 'classes/functions.php';
		require 'classes/template.php';
		require 'classes/player.php';
		require 'classes/stats.php';
		
		
		$func = new Functions();
		$secure = new Security();
		$settings = new Settings();
		$tpl = new Templates();
		$tpl->load_phrases("overview", true);
		$tpl->load_phrases("_xbl_admin", true);
		$tpl->load_phrases("_xbl_overview", true);
	}
	
	
	### Allgemeine Variablen setzen ###
	if (true) {
		$func->counter();                                             // Counter starten
		$preset = $func->load_settings("settings");                   // Settings laden
		$ov = $func->load_settings("overview");                       // Settings laden
		date_default_timezone_set($preset["timezone"]);               // Zeitzone setzen
		$lastupdate = date("d.m.Y - H:i", $preset["lastcheckall"]);   // Variable für das letzte Update aller Spieler
		$columns = $func->load_settings("overview");                  // Spalten die in der Übersicht angezeigt werden können/sollen
	}
	
	
	### Tabellenkopf ###
	if (true) {
		// Spalten, die anstatt eines Textes ein Icon als Bezeichnung haben
		$icons = Array(
			"GamerScore" => 0,
			"TotalPossibleGamerScore" => -20,
			"TotalEarnedAchievements" => -40,
			"TotalPossibleAchievements" => -60,
			"TotalPercentCompleted" => -80,
			"GameCount" => -100,
			"GamerScorePerGame" => -120,
			"LastGame" => -140
		);
		
		
		$thead = phpQuery::newDocumentHTML("<thead><tr></tr></thead>");
		
		// Alle Spaltenheader erstellen, die im Leaderboard aktiviert wurden
		foreach ($columns as $name => $active) {
			if (array_key_exists($name, $icons)) {
				if ($active) pq('tr')->append('<th info="'.$tpl->phrase($name).'"><div class="ov_icons" style="background-position: 0 '.$icons[$name].'px"></div></th>');
			} else {
				if ($active) pq('tr')->append('<th>'.$tpl->phrase($name).'</th>');
			}
		}
		
		// Sortierung umdrehen, wenn Spalte 1 "Position" enthält
		$sortList = pq('th')->eq(0)->text() == $tpl->phrase('position') ? "[[0,0]]" : "[[0,1]]";
	}
	
	
	### Tabellenkörper ###
	if (true) {
		$tbody = phpQuery::newDocumentHTML("<tbody></tbody>");
		$summary = Array();
		$i = 0;
		
		// Die IDs aller Spieler abrufen, die eine Spielzeit > 0 haben, nicht die ID 1 haben, sortiert nach den Punkten
		$sql->query('
			SELECT id FROM '.$sql->prefix.'player 
			WHERE id != 0 AND GamerScore != "0"
			ORDER BY GamerScore DESC
		');
		
		// Alle Spieler durchlaufen und in als Zeile in die Tabelle speichern
		while ($row = $sql->fetchAssoc()) {
			$i++;
			$td = Array();
			$stats = new Stats($row['id']);
			
			
			if ($ov['position']) {      // Position im Leaderboard
				$result = $stats->format($i, Array('number', 0, '', 'position'));
				$td['position'] = phpQuery::newDocumentHTML('<td sort="'.$result['sort'].'" info="'.$result['tip'].'">'.$result['format'].'</td>');
				
				if (!isset($summary['position'])) $summary['position'] = Array();
				$summary['position'][] = '';
			}
			
			if ($ov['name']) {          // Spielername
				$name     = $stats->get('player', 'name');
				$nickname = $stats->get('player', 'nickname');
				$clantag  = $settings->get('clantag');
				
				if ($nickname) $name = $nickname;
				
				$path = ($preset['cms'] == "standalone") ? "" : $settings->get("path");
				$cheater = $stats->get('player', 'IsCheater') ? $tpl->phrase('yes') : $tpl->phrase('no');
				
				$html = utf8_encode(htmlentities('
					<table>
						<tr>
							<td style="width:80px"><img src="'.$stats->get('player', 'AvatarTile').'" /></td>
							<td>
								<ul>
									<li>'.$tpl->phrase('Reputation').': '.$stats->get('player', 'Reputation').'</li>
									<li>'.$tpl->phrase('IsCheater').': '.$cheater.'</li>
								</ul>
							</td>
						</tr>
						<tr>
							<td colspan="2">
								<span>
									<h1>'.$clantag.$name.'</h1>
									'.$stats->get('player', 'OnlineStatus').'
								</span>
							</td>
							<td>
							</td>
						</tr>
					</table>
				'));
				$td['name'] = phpQuery::newDocumentHTML('<td class="tleft" sort="'.$name.'" menu="'.$html.'"><a href="https://live.xbox.com/en-US/Profile?gamertag='.$stats->get('player', 'name').'" target="_blank">'.$clantag.$name.'</a></td>');
				
				
				
				if (!isset($summary['name'])) $summary['name'] = Array();
				$summary['name'][] = NULL;
			}
			
			if ($ov['custom1']) {       // Custon-Wert 1
				$result = $stats->get('player', 'custom1');
				$td['custom1'] = phpQuery::newDocumentHTML('<td class="tleft" sort="'.$result.'">'.$result.'</td>');
				
				if (!isset($summary['custom1'])) $summary['custom1'] = Array();
				$summary['custom1'][] = NULL;
			}
			
			if ($ov['GamerScore']) {
				$result = $stats->get('player', 'GamerScore', Array('number', 0));
				$td['GamerScore'] = phpQuery::newDocumentHTML('<td sort="'.$result['sort'].'" info="'.$tpl->phrase('GamerScore').': '.$result['format'].'">'.$result['format'].'</td>');
				
				if (!isset($summary['GamerScore'])) $summary['GamerScore'] = Array();
				$summary['GamerScore'][] = $result['raw'];
			}
			
			if ($ov['TotalPossibleGamerScore']) {
				$result = $stats->get('player', 'TotalPossibleGamerScore', Array('number', 0));
				$td['TotalPossibleGamerScore'] = phpQuery::newDocumentHTML('<td sort="'.$result['sort'].'" info="'.$tpl->phrase('TotalPossibleGamerScore').': '.$result['format'].'">'.$result['format'].'</td>');
				
				if (!isset($summary['TotalPossibleGamerScore'])) $summary['TotalPossibleGamerScore'] = Array();
				$summary['TotalPossibleGamerScore'][] = $result['raw'];
			}
			
			if ($ov['TotalEarnedAchievements']) {
				$result = $stats->get('player', 'TotalEarnedAchievements', Array('number', 0));
				$td['TotalEarnedAchievements'] = phpQuery::newDocumentHTML('<td sort="'.$result['sort'].'" info="'.$tpl->phrase('TotalEarnedAchievements').': '.$result['format'].'">'.$result['format'].'</td>');
				
				if (!isset($summary['TotalEarnedAchievements'])) $summary['TotalEarnedAchievements'] = Array();
				$summary['TotalEarnedAchievements'][] = $result['raw'];
			}
			
			if ($ov['TotalPossibleAchievements']) {
				$result = $stats->get('player', 'TotalPossibleAchievements', Array('number', 0));
				$td['TotalPossibleAchievements'] = phpQuery::newDocumentHTML('<td sort="'.$result['sort'].'" info="'.$tpl->phrase('TotalPossibleAchievements').': '.$result['format'].'">'.$result['format'].'</td>');
				
				if (!isset($summary['TotalPossibleAchievements'])) $summary['TotalPossibleAchievements'] = Array();
				$summary['TotalPossibleAchievements'][] = $result['raw'];
			}
			
			if ($ov['TotalPercentCompleted']) {
				$possible = $stats->get('player', 'TotalPossibleAchievements');
				$result = $stats->get('player', 'TotalEarnedAchievements', Array('ratio', $possible, 0, "%"));
				$td['TotalPercentCompleted'] = phpQuery::newDocumentHTML('<td sort="'.$result['sort'].'" info="'.$tpl->phrase('TotalPercentCompleted').': '.$result['tip'].'">'.$result['format'].'</td>');
				
				if (!isset($summary['TotalPercentCompleted'])) $summary['TotalPercentCompleted'] = Array();
				$summary['TotalPercentCompleted'][] = $result['raw'];
			}
			
			if ($ov['GameCount']) {
				$result = $stats->get('player', 'GameCount', Array('number', 0));
				$td['GameCount'] = phpQuery::newDocumentHTML('<td sort="'.$result['sort'].'" info="'.$tpl->phrase('GameCount').': '.$result['format'].'">'.$result['format'].'</td>');
				
				if (!isset($summary['GameCount'])) $summary['GameCount'] = Array();
				$summary['GameCount'][] = $result['raw'];
			}
			
			if ($ov['GamerScorePerGame']) {
				$games = $stats->get('player', 'GameCount');
				$result = $stats->get('player', 'GamerScore', Array('ratio', $games, 0));
				$td['GamerScorePerGame'] = phpQuery::newDocumentHTML('<td sort="'.$result['sort'].'" info="'.$tpl->phrase('GamerScorePerGame').': '.$result['tip'].'">'.$result['format'].'</td>');
				
				if (!isset($summary['GamerScorePerGame'])) $summary['GamerScorePerGame'] = Array();
				$summary['GamerScorePerGame'][] = $result['raw'];
			}
			
			if ($ov['LastGame']) {
				$game = $stats->get('player', 'LastGame');
				
				
				$html = utf8_encode(htmlentities('
					<div style="background-image: url('.$game->artwork->large.')">
						<span>
							<h1>'.$game->title.'</h1>
							'.$tpl->phrase('GamerScore').': '.$game->gamerscore->current.'<br/>
							'.$tpl->phrase('TotalEarnedAchievements').': '.$game->achievements->current.' / '.$game->achievements->total.'<br/>
							'.$tpl->phrase('lastplayed').': '.date("d M Y", $game->lastplayed).'
						</span>
					</div>
				'));
				$td['LastGame'] = phpQuery::newDocumentHTML('<td class="tleft" sort="'.$game->id.'" menu="'.$html.'"><a href="http://www.xbox.com/de-DE/Search?q='.$game->title.'" target="_blank">'.$game->title.'</a></td>');
				
				
				
				if (!isset($summary['LastGame'])) $summary['LastGame'] = Array();
				$summary['LastGame'][] = NULL;
			}
			
			
			// Zellen der gewünschten Reihenfolge nach in eine Zeile speichern
			$tr = phpQuery::newDocumentHTML('<tr></tr>');
			foreach ($ov as $name => $show) {
				if ($show AND isset($td[$name])) pq('tr')->append($td[$name]);
			}
			
			// Zeile an den Tabellenkörper anhängen
			phpQuery::selectDocument($tbody);
			pq('tbody')->append($tr);
		}
	}
	
	
	### Tabellenfuss ###
	if (true) {
		$tfoot = phpQuery::newDocumentHTML('<tfoot></tfoot>');
		$methods = Array('ov_averages', 'ov_median', 'ov_summary');
		
		if ($sql->count()) {
			foreach ($methods as $method) {
				if ($settings->get($method)) {
					$td = Array();
					
					if ($ov['position']) {      // Position im Leaderboard
						$td['position'] = phpQuery::newDocumentHTML('<td></td>');
					}
					
					if ($ov['name']) {          // Spielername
						$td['name'] = phpQuery::newDocumentHTML('<td>'.$tpl->phrase($method).': </td>');
					}
					
					if ($ov['custom1']) {       // Custon-Wert 1
						$td['custom1'] = phpQuery::newDocumentHTML('<td></td>');
					}
					
					if ($ov['GamerScore']) {
						if ($method == 'ov_averages') {
							$result = $func->calculate_average($summary['GamerScore']);
							$result = $stats->format($result, Array('number', 0));
							$td['GamerScore'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_median') {
							$result = $func->calculate_median($summary['GamerScore']);
							$result = $stats->format($result, Array('number', 0));
							$td['GamerScore'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_summary') {
							$result = array_sum($summary['GamerScore']);
							$result = $stats->format($result, Array('number', 0, "k"));
							$td['GamerScore'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
					}
					
					if ($ov['TotalPossibleGamerScore']) {
						if ($method == 'ov_averages') {
							$result = $func->calculate_average($summary['TotalPossibleGamerScore']);
							$result = $stats->format($result, Array('number', 0));
							$td['TotalPossibleGamerScore'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_median') {
							$result = $func->calculate_median($summary['TotalPossibleGamerScore']);
							$result = $stats->format($result, Array('number', 0));
							$td['TotalPossibleGamerScore'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_summary') {
							$result = array_sum($summary['TotalPossibleGamerScore']);
							$result = $stats->format($result, Array('number', 0, "k"));
							$td['TotalPossibleGamerScore'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
					}
					
					if ($ov['TotalEarnedAchievements']) {
						if ($method == 'ov_averages') {
							$result = $func->calculate_average($summary['TotalEarnedAchievements']);
							$result = $stats->format($result, Array('number', 0));
							$td['TotalEarnedAchievements'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_median') {
							$result = $func->calculate_median($summary['TotalEarnedAchievements']);
							$result = $stats->format($result, Array('number', 0));
							$td['TotalEarnedAchievements'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_summary') {
							$result = array_sum($summary['TotalEarnedAchievements']);
							$result = $stats->format($result, Array('number', 0, "k"));
							$td['TotalEarnedAchievements'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
					}
					
					if ($ov['TotalPossibleAchievements']) {
						if ($method == 'ov_averages') {
							$result = $func->calculate_average($summary['TotalPossibleAchievements']);
							$result = $stats->format($result, Array('number', 0));
							$td['TotalPossibleAchievements'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_median') {
							$result = $func->calculate_median($summary['TotalPossibleAchievements']);
							$result = $stats->format($result, Array('number', 0));
							$td['TotalPossibleAchievements'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_summary') {
							$result = array_sum($summary['TotalPossibleAchievements']);
							$result = $stats->format($result, Array('number', 0, "k"));
							$td['TotalPossibleAchievements'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
					}
					
					if ($ov['TotalPercentCompleted']) { 
						if ($method == 'ov_averages') {
							$result = $func->calculate_average($summary['TotalPercentCompleted']);
							$result = $stats->format($result, Array('number', 0, '%'));
							$td['TotalPercentCompleted'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_median') {
							$result = $func->calculate_median($summary['TotalPercentCompleted']);
							$result = $stats->format($result, Array('number', 0, '%'));
							$td['TotalPercentCompleted'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_summary') {
							$td['TotalPercentCompleted'] = phpQuery::newDocumentHTML('<td></td>');
						}
					}
					
					if ($ov['GameCount']) {
						if ($method == 'ov_averages') {
							$result = $func->calculate_average($summary['GameCount']);
							$result = $stats->format($result, Array('number', 0));
							$td['GameCount'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_median') {
							$result = $func->calculate_median($summary['GameCount']);
							$result = $stats->format($result, Array('number', 0));
							$td['GameCount'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_summary') {
							$result = array_sum($summary['GameCount']);
							$result = $stats->format($result, Array('number', 0, "k"));
							$td['GameCount'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
					}
					
					if ($ov['GamerScorePerGame']) {
						if ($method == 'ov_averages') {
							$result = $func->calculate_average($summary['GamerScorePerGame']);
							$result = $stats->format($result, Array('number', 0));
							$td['GamerScorePerGame'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_median') {
							$result = $func->calculate_median($summary['GamerScorePerGame']);
							$result = $stats->format($result, Array('number', 0));
							$td['GamerScorePerGame'] = phpQuery::newDocumentHTML('<td info="'.$tpl->phrase($method).': '.$result['format'].'">'.$result['format'].'</td>');
						}
						if ($method == 'ov_summary') {
							$td['GamerScorePerGame'] = phpQuery::newDocumentHTML('<td></td>');
						}
					}
					
					if ($ov['LastGame']) {
						$td['LastGame'] = phpQuery::newDocumentHTML('<td></td>');
					}
					
					
					// Zellen der gewünschten Reihenfolge nach in eine Zeile speichern
					$tr = phpQuery::newDocumentHTML('<tr></tr>');
					foreach ($ov as $name => $show) {
						if ($show AND isset($td[$name])) pq('tr')->append($td[$name]);
					}
					
					// Zeile an den Tabellenfuss anhängen
					phpQuery::selectDocument($tfoot);
					pq('tfoot')->append($tr);
				}
			}
		}
	}
	
	
	### Seite erzeugen und ausgeben ###
	if (true) {
		if ($preset['cms'] == "standalone") {
			// Template laden
			eval ("\$html = \"".$tpl->template("standalone")."\";");  
			$html = phpQuery::newDocumentHTML($html);
			
			// Selfadd: Link einblenden, mit dem Spieler sich selbst adden können
			if ($preset['selfadd'] == "1") pq("#header")->prepend('<a href="admin/index.php?selfadd" class="selfadd">'.$tpl->phrase("selfadd").'</a>'); 
		} else {
			// Template laden
			eval ("\$html = \"".$tpl->template("plugin")."\";");
			$html = str_replace('#PATH#', $preset['path'], $html);
			$html = phpQuery::newDocumentHTML($html);
			
			// Selfadd: Link einblenden, mit dem Spieler sich selbst adden können
			if ($preset['selfadd'] == "1") pq("#ezStats")->prepend('<a href="'.$preset["path"].'admin/index.php?selfadd" target="_blank" class="selfadd">'.$tpl->phrase("selfadd").'</a>'); 
		}
		
		
		// Stats-Tabelle einbinden
		pq('#ezOverview')->append($thead);
		pq('#ezOverview')->append($tfoot);
		pq('#ezOverview')->append($tbody);
		
		
		// Postprocessing
		if (!$preset['ov_brand'])            pq('#ezBrand')->remove(); // Werbung entfernen
		if (!$preset['ov_infobox'])          pq('#ezAside')->remove(); // Infobox entfernen
		if ($preset['language'] == "hebrew") pq('#ezStats')->attr('dir', 'rtl'); // Schriftrichtung ändern bei RTL-Sprachen
		
		
		// Ausgabe
		header('Content-Type:text/html; charset=UTF-8');
		echo $html;
	}
?>
Return current item: ezStats for XBOX Live