public function getServerInfo()
 {
     if (!Request::ajax()) {
         die;
     }
     $this->setServers();
     $cp = new TMFColorParser();
     $output = array();
     foreach ($this->config['servers'] as $server) {
         $client = new MadRemote();
         if ($client->connect($server['ip'], $server['port'])) {
             $client->Authenticate($server['user'], $server['pass']);
             $version = $client->GetVersion();
             $srvdata = $client->GetServerOptions();
             $players = $client->GetPlayerList(300, 0);
             $cmap = $client->GetCurrentMapInfo();
             $nmap = $client->GetNextMapInfo();
             if (!empty($version) && !empty($srvdata)) {
                 $out = array();
                 $env = $version['TitleId'];
                 $link = 'maniaplanet://‪#‎join‬=' . $server['login'] . '@' . $env;
                 $name = $srvdata['Name'];
                 $playermax = $srvdata['CurrentMaxPlayers'];
                 $spectatormax = $srvdata['CurrentMaxSpectators'];
                 if (!empty($players)) {
                     $pc = 0;
                     $sc = 0;
                     foreach ($players as $player) {
                         if ($player['IsSpectator'] == 1) {
                             $sc++;
                         } else {
                             $pc++;
                         }
                     }
                     $pm = $pc . ' / ' . $playermax;
                     $sm = $sc . ' / ' . $spectatormax;
                 } else {
                     $pm = '0 / ' . $playermax;
                     $sm = '0 / ' . $spectatormax;
                 }
                 $out['name'] = $cp->toHTML($name);
                 $out['players'] = $pm;
                 $out['spectators'] = $sm;
                 $out['login'] = $server['login'];
                 $out['env'] = $env;
                 $out['cmap'] = $cp->toHTML($cmap['Name']);
                 $out['nmap'] = $cp->toHTML($nmap['Name']);
                 array_push($output, $out);
             }
         }
         $client->close();
     }
     return Response::json($output);
 }
    $login = $trackmania->getLogin();
    $player = $trackmania->getPlayer();
} catch (\TrackMania\WebServices\Exception $e) {
    var_dump($e->getHTTPStatusCode(), $e->getHTTPStatusMessage(), $e->getMessage());
    $login = null;
    $player = null;
}
/**
 * Instanciates the Guestbook class to we can handle data
 */
$guestbook = new Guestbook();
/**
 * Instanciates the color parser
 * @see http://fish.stabb.de/index.php5?page=3&subpage=127
 */
$tmfcolorparser = new TMFColorParser('#ffffff');
/**
 * Looks for different actions
 */
if ($login && array_key_exists('post', $_POST)) {
    $guestbook->createPost($login, $_POST['post']);
} elseif ($login && $login == ADMIN_LOGIN && array_key_exists('delete', $_GET)) {
    $guestbook->deletePost($_GET['delete']);
}
/**
 * Get the page
 */
if (array_key_exists('page', $_GET)) {
    $page = abs((int) $_GET['page']);
} else {
    $page = 1;
 public function getStatsTotal()
 {
     if (!Request::ajax()) {
         die;
     }
     $this->checkRuns();
     $cp = new TMFColorParser();
     $pls = array();
     foreach ($this->runs as $run) {
         $this->getMaps($run);
         $maps = $this->maps;
         $norectime = 720000;
         $mapinfo = array();
         $n = 0;
         foreach ($maps as $map) {
             $results = DB::select('select MapId, Uid, Name from dsc_uaseco_maps where Uid = ?', array($map));
             if (!empty($results)) {
                 foreach ($results as $res) {
                     $mapinfo[$n]['MapId'] = $res->MapId;
                     $mapinfo[$n]['Uid'] = $res->Uid;
                     $mapinfo[$n]['Name'] = $cp->toHTML($res->Name);
                 }
                 $n++;
             }
         }
         $this->array_sort_by_column($mapinfo, 'MapId');
         $playerinfo = array();
         $n = 0;
         foreach ($mapinfo as $map) {
             $results = DB::select('select PlayerId from dsc_uaseco_records where MapId = ?', array($map['MapId']));
             if (!empty($results)) {
                 foreach ($results as $res) {
                     if (!$this->in_array_r($res->PlayerId, $playerinfo)) {
                         $results2 = DB::select('select PlayerId, Login, Nickname, Nation from dsc_uaseco_players where PlayerId = ?', array($res->PlayerId));
                         if (!empty($results2)) {
                             foreach ($results2 as $res2) {
                                 $lgn = $res2->Login;
                                 $playerinfo[$lgn]['PlayerId'] = $res2->PlayerId;
                                 $playerinfo[$lgn]['Login'] = $res2->Login;
                                 $playerinfo[$lgn]['Nickname'] = $cp->toHTML($res2->Nickname);
                                 $playerinfo[$lgn]['Nation'] = $res2->Nation;
                             }
                             $n++;
                         }
                     }
                 }
             }
         }
         $this->array_sort_by_column($playerinfo, 'PlayerId');
         foreach ($playerinfo as &$player) {
             $totaltime = 0;
             $check = false;
             foreach ($mapinfo as $map) {
                 $results = DB::select('select MapId, PlayerId, Date, Score, Checkpoints from dsc_uaseco_records where MapId = ? and PlayerId = ?', array($map['MapId'], $player['PlayerId']));
                 if (!empty($results)) {
                     foreach ($results as $res) {
                         $totaltime += (int) $res->Score;
                     }
                 } else {
                     $check = true;
                     $totaltime += (int) $norectime;
                 }
             }
             if (!$check) {
                 $player['totaltimeclean'] = $totaltime;
             } else {
                 $player['totaltimeclean'] = $totaltime;
             }
         }
         $this->array_sort_by_column($playerinfo, 'totaltimeclean');
         $playerinfo = array_map("unserialize", array_unique(array_map("serialize", $playerinfo)));
         $i = 100;
         $playerout = array();
         foreach ($playerinfo as &$player) {
             if ($i > 0) {
                 $player['points'] = $i;
                 array_push($playerout, $player);
                 $i--;
             }
         }
         $pls[$run] = $playerout;
     }
     $total = array();
     $i = 0;
     foreach ($pls as $run) {
         foreach ($run as $key => $plyr) {
             if (isset($total[$plyr['Login']])) {
                 if (isset($total[$plyr['Login']]['points'])) {
                     $points = $plyr['points'];
                     if (isset($total[$plyr['Login']]['points'])) {
                         $total[$plyr['Login']]['points'] += $points;
                     } else {
                         $total[$plyr['Login']]['points'] = $points;
                     }
                 } else {
                     $total[$plyr['Login']]['points'] = 0;
                 }
             } else {
                 $total[$plyr['Login']] = $plyr;
             }
             if (!isset($total[$plyr['Login']]['runs'])) {
                 $total[$plyr['Login']]['runs'] = array();
             }
             if (!array_key_exists($i, $total[$plyr['Login']]['runs'])) {
                 if (isset($points)) {
                     $total[$plyr['Login']]['runs'][$i] = $points;
                 } else {
                     $total[$plyr['Login']]['runs'][$i] = $total[$plyr['Login']]['points'];
                 }
             }
         }
         $i++;
     }
     $this->array_sort_by_column($total, 'points', SORT_DESC);
     for ($i = 0; $i < count($pls); $i++) {
         foreach ($total as &$player) {
             if (!isset($player['runs'][$i])) {
                 $player['runs'][$i] = 0;
             }
             ksort($player['runs']);
         }
     }
     return Response::json($total);
 }