diff --git a/php/.htaccess b/php/.htaccess index 6de0dd3..25fa5dc 100644 --- a/php/.htaccess +++ b/php/.htaccess @@ -2,8 +2,7 @@ Order Allow,Deny Deny from all - - Order Allow,Deny - Deny from all - -AcceptPathInfo Off +Options +FollowSymLinks +RewriteEngine On +RewriteCond %{REQUEST_URI} !^.+/?(css|js|fonts|img)/ +RewriteRule ^(.*)$ app.php [NC,L] diff --git a/php/about.php b/php/about.php deleted file mode 100644 index 18ddddc..0000000 --- a/php/about.php +++ /dev/null @@ -1,6 +0,0 @@ -setTitle('About'); - -$container->render('about.html.php'); diff --git a/php/config/.htaccess b/php/config/.htaccess new file mode 100644 index 0000000..8d2f256 --- /dev/null +++ b/php/config/.htaccess @@ -0,0 +1 @@ +deny from all diff --git a/php/includes/config.php b/php/config/config.php similarity index 56% rename from php/includes/config.php rename to php/config/config.php index 37c565a..88bf18b 100644 --- a/php/includes/config.php +++ b/php/config/config.php @@ -15,7 +15,22 @@ $config['libLocation'] = "includes/"; */ $config['viewLocation'] = "views/"; +/** + * Config location + */ +$config['configLocation'] = "config/"; + +/** + * Controllers location + */ +$config['controllerLocation'] = "controllers/"; + +/** + * Prefix for all routes (eg '/trivia') + */ +$config['baseRoute'] = ''; + /** * Default page after logging in */ -$config['defaultLoginRedirectPage'] = "index.php"; +$config['defaultLoginRedirectPage'] = "/"; diff --git a/php/config/routes.php b/php/config/routes.php new file mode 100644 index 0000000..375d2f5 --- /dev/null +++ b/php/config/routes.php @@ -0,0 +1,23 @@ +"/","target"=>'Index:index', "args"=>array("name"=>"home")); + +// About +$routes[] = array("pattern"=>"/about", "target"=>"About:about", "args"=>array("name"=>"about")); + +// Stats +$routes[] = array("pattern"=>"/stats", "target"=>"Stats:stats", "args"=>array("name"=>"stats")); +$routes[] = array("pattern"=>"/top/:timespan", "target"=>"Stats:top", "args"=>array("name"=>"top")); + +// Players +$routes[] = array("pattern"=>"/search", "target"=>"User:search", "args"=>array("name"=>"search")); +$routes[] = array("pattern"=>"/profile/:username", "target"=>"User:profile", "args"=>array("name"=>"profile", "filters" => array("username" => "([^\s/]+)"))); + +// Reports +$routes[] = array("pattern"=>"/reports", "target"=>"Reports:list", "args"=>array("name"=>"reports")); + +// Login/Logout +$routes[] = array("pattern"=>"/login", "target"=>"Login:login", "args"=>array("name"=>"login")); +$routes[] = array("pattern"=>"/logout", "target"=>"Login:logout", "args"=>array("name"=>"logout")); diff --git a/php/controllers/.htaccess b/php/controllers/.htaccess new file mode 100644 index 0000000..8d2f256 --- /dev/null +++ b/php/controllers/.htaccess @@ -0,0 +1 @@ +deny from all diff --git a/php/controllers/AboutController.php b/php/controllers/AboutController.php new file mode 100644 index 0000000..f7be20e --- /dev/null +++ b/php/controllers/AboutController.php @@ -0,0 +1,8 @@ +setTitle('About'); + $this->render('about.html.php'); + } +} diff --git a/php/controllers/IndexController.php b/php/controllers/IndexController.php new file mode 100644 index 0000000..0d7c9c1 --- /dev/null +++ b/php/controllers/IndexController.php @@ -0,0 +1,28 @@ +container; + $storage = $container->getStorage(); + + $errors = array(); + $result = array(); + try { + $result = $storage->getRecentAskedQuestions(); + } catch(StorageSchemaException $e) { + $errors[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $errors[] = "Error: Database is not available"; + } + $storage->close(); + + $values = array(); + + $values['result'] = $result; + $values['errors'] = $errors; + + $container->setTitle('Home'); + + $container->render('home.html.php', $values); + } +} diff --git a/php/controllers/LoginController.php b/php/controllers/LoginController.php new file mode 100644 index 0000000..8bbb94e --- /dev/null +++ b/php/controllers/LoginController.php @@ -0,0 +1,69 @@ +container->getConfig()['defaultLoginRedirectPage']; + if(array_key_exists('lastPage', $_SESSION)) { + $this->redirect($_SESSION('lastPage')); + } else { + $this->redirect($defaultPage); + } + } + + public function loginAction() { + $login = $this->container->getLogin(); + + if($login->isLoggedIn()) { + $this->doRedirect(); + } + + $errors = array(); + if($_SERVER['REQUEST_METHOD'] === 'POST') { + $username = $this->getPostVariable("username"); + $password = $this->getPostVariable("password"); + + if(empty($username)) { + $errors['username'] = "Please enter a Username."; + } + if(empty($password)) { + $errors['password'] = "Please enter a Password."; + } + if(!empty($username) && !empty($password)) { + if($login->login($username, $password)) { + $this->container->setNotice("Success! You have been successfully logged in."); + $this->doRedirect(); + } else { + $errors['login'] = "Invalid credentials. Please check your username and password, and try again."; + } + } + } + + $values = array(); + $values['errors'] = $errors; + + $this->container->setTitle("Login"); + $this->container->render("login.html.php", $values); + } + + public function logoutAction() { + $login = $this->container->getLogin(); + + $login->logout(); + + // force a new session, for the notice + $this->container->login = new Login($this->container->storage); + + $this->container->setNotice("Success! You have been successfully logged out."); + $this->redirect($this->container->getConfig()['defaultLoginRedirectPage']); + } +} diff --git a/php/controllers/ReportsController.php b/php/controllers/ReportsController.php new file mode 100644 index 0000000..865ecc3 --- /dev/null +++ b/php/controllers/ReportsController.php @@ -0,0 +1,103 @@ +container; + $storage = $container->getStorage(); + + $reportPage = $this->getPageVariable('rp'); + $editPage = $this->getPageVariable('ep'); + $newPage = $this->getPageVariable('np'); + $deletePage = $this->getPageVariable('dp'); + + $maxResults = 5; + + $reportResultCount = 0; + $reportResult = array(); + $reportErrors = array(); + try { + $reportResult = $storage->getTopReports($reportPage, $maxResults); + $reportResultCount = $storage->getCountReports(); + } catch(StorageSchemaException $e) { + $reportErrors[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $reportErrors[] = "Error: Database is not available"; + } + + $editResultCount = 0; + $editResult = array(); + $editErrors = array(); + try { + $editResult = $storage->getTopEdits($editPage, $maxResults); + $editResultCount = $storage->getCountEdits(); + } catch(StorageSchemaException $e) { + $editErrors[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $editErrors[] = "Error: Database is not available"; + } + + $newResultCount = 0; + $newResult = array(); + $newErrors = array(); + try { + $newResult = $storage->getTopNewQuestions($newPage, $maxResults); + $newResultCount = $storage->getCountNewQuestions(); + } catch(StorageSchemaException $e) { + $newErrors[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $newErrors[] = "Error: Database is not available"; + } + + $deleteResultCount = 0; + $deleteResult = array(); + $deleteErrors = array(); + try { + $deleteResult = $storage->getTopDeletions($deletePage, $maxResults); + $deleteResultCount = $storage->getCountDeletions(); + } catch(StorageSchemaException $e) { + $deleteErrors[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $deleteErrors[] = "Error: Database is not available"; + } + + $values = array(); + $values['maxResults'] = $maxResults; + + $values['reportResultCount'] = $reportResultCount; + $values['reportResult'] = $reportResult; + $values['reportErrors'] = $reportErrors; + $values['reportPage'] = $reportPage; + + $values['editResultCount'] = $editResultCount; + $values['editResult'] = $editResult; + $values['editErrors'] = $editErrors; + $values['editPage'] = $editPage; + + $values['newResultCount'] = $newResultCount; + $values['newResult'] = $newResult; + $values['newErrors'] = $newErrors; + $values['newPage'] = $newPage; + + $values['deleteResultCount'] = $deleteResultCount; + $values['deleteResult'] = $deleteResult; + $values['deleteErrors'] = $deleteErrors; + $values['deletePage'] = $deletePage; + + $container->setTitle('Reports'); + + $container->render('reports.html.php', $values); + } +} diff --git a/php/controllers/StatsController.php b/php/controllers/StatsController.php new file mode 100644 index 0000000..4b451b7 --- /dev/null +++ b/php/controllers/StatsController.php @@ -0,0 +1,126 @@ +container; + $storage = $container->getStorage(); + + $dayResult = array(); + $dayErrors = array(); + try { + $dayResult = $storage->getDayTopScores(1, 10); + } catch(StorageSchemaException $e) { + $dayErrors[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $dayErrors[] = "Error: Database is not available"; + } + + $weekResult = array(); + $weekErrors = array(); + try { + $weekResult = $storage->getWeekTopScores(1, 10); + } catch(StorageSchemaException $e) { + $weekErrors[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $weekErrors[] = "Error: Database is not available"; + } + + $monthResult = array(); + $monthErrors = array(); + try { + $monthResult = $storage->getMonthTopScores(1, 10); + } catch(StorageSchemaException $e) { + $monthErrors[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $monthErrors[] = "Error: Database is not available"; + } + + $yearResult = array(); + $yearErrors = array(); + try { + $yearResult = $storage->getYearTopScores(1, 10); + } catch(StorageSchemaException $e) { + $yearErrors[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $yearErrors[] = "Error: Database is not available"; + } + + $values = array(); + $values['dayResult'] = $dayResult; + $values['dayErrors'] = $dayErrors; + $values['weekResult'] = $weekResult; + $values['weekErrors'] = $weekErrors; + $values['monthResult'] = $monthResult; + $values['monthErrors'] = $monthErrors; + $values['yearResult'] = $yearResult; + $values['yearErrors'] = $yearErrors; + + $container->setTitle('Stats'); + + $container->render('stats.html.php', $values); + + } + + public function topAction($args) { + $container = $this->container; + $storage = $container->getStorage(); + + $timespans = array('day'=>'Day', 'week'=>'Week', 'month'=>'Month', 'year'=>'Year'); + $timespan = 'day'; + $timeDesc = 'Day'; + if(array_key_exists('timespan', $args)) { + if(array_key_exists(strtolower($args['timespan']), $timespans)) { + $timespan = strtolower($args['timespan']); + $timeDesc = $timespans[$timespan]; + } + } + + if(array_key_exists('page', $_GET)) { + $page = $_GET['page']; + } + if(!isset($page)) { + $page = 1; + } + if($page < 1) { + $page = 1; + } + + $maxResults = 20; + + $resultCount = 0; + $result = array(); + $errors = array(); + try { + if ($timespan == 'week') { + $result = $storage->getWeekTopScores($page, $maxResults); + $resultCount = $storage->getCountWeekTopScores(); + } else if ($timespan == 'month') { + $result = $storage->getMonthTopScores($page, $maxResults); + $resultCount = $storage->getCountMonthTopScores(); + } else if ($timespan == 'year') { + $result = $storage->getYearTopScores($page, $maxResults); + $resultCount = $storage->getCountYearTopScores(); + } else { + $result = $storage->getDayTopScores($page, $maxResults); + $resultCount = $storage->getCountDayTopScores(); + } + } catch(StorageSchemaException $e) { + $errors[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $errors[] = "Error: Database is not available"; + } + + $values = array(); + $values['result'] = $result; + $values['resultCount'] = $resultCount; + $values['maxResults'] = $maxResults; + $values['page'] = $page; + $values['timespan'] = $timespan; + $values['timeDesc'] = $timeDesc; + $values['errors'] = $errors; + + $container->setTitle('Top Scores for ' . $timeDesc); + + $container->render('top.html.php', $values); + } +} diff --git a/php/controllers/UserController.php b/php/controllers/UserController.php new file mode 100644 index 0000000..88eabf4 --- /dev/null +++ b/php/controllers/UserController.php @@ -0,0 +1,183 @@ +container; + $storage = $container->getStorage(); + + $username = ''; + $usernameCanonical = ''; + + if(array_key_exists('username', $_GET)) { + // Convert username to lowercase in irc + $username = $_GET['username']; + $ircLowerSymbols = array("\\"=>"|", "["=>"{", "]"=>"}", "~"=>"^"); + $usernameCanonical = strtr($username, $ircLowerSymbols); + $usernameCanonical = strtolower($usernameCanonical); + } + + if(array_key_exists('page', $_GET)) { + $page = $_GET['page']; + } + if(!isset($page)) { + $page = 1; + } + if($page < 1) { + $page = 1; + } + + $maxResults = 10; + $usersCount = 0; + $users = array(); + $errors = array(); + + try { + $users = $storage->getUserLikeUsernameCanonical($usernameCanonical, $page, $maxResults); + $usersCount = $storage->getCountUserLikeUsernameCanonical($usernameCanonical); + } catch(StorageSchemaException $e) { + $errors[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $errors[] = "Error: Database is not available"; + } + + $storage->close(); + + // Redirect to profile if only 1 result found + if(count($users) == 1) { + $container->redirect($container->router->generate('profile', array("username"=>$users[0]['username']))); + } + + $values = array(); + $values['username'] = $username; + $values['usernameCanonical'] = $usernameCanonical; + $values['page'] = $page; + $values['maxResults'] = $maxResults; + $values['usersCount'] = $usersCount; + $values['users'] = $users; + $values['errors'] = $errors; + + $container->setTitle('Players'); + + $container->render('user.html.php', $values); + } + + public function profileAction($args) { + $container = $this->container; + $storage = $container->getStorage(); + + $username = ''; + $usernameCanonical = ''; + + if(array_key_exists('username', $args)) { + $username = $args['username']; + } + + $ircLowerSymbols = array("\\"=>"|", "["=>"{", "]"=>"}", "~"=>"^"); + $usernameCanonical = strtr($username, $ircLowerSymbols); + $usernameCanonical = strtolower($usernameCanonical); + + $lastSeen = 'Never'; + + $result = array(); + $result['usrname'] = "Not found"; + $result['count'] = 0; + $result['score'] = 0; + $result['points'] = 0; + $result['q_asked'] = 0; + $result['num_e'] = 0; + $result['num_e_accepted'] = 0; + $result['num_q'] = 0; + $result['num_q_accepted'] = 0; + $result['num_r'] = 0; + $result['highest_streak'] = 0; + + $userProfile = $result; + $errors = array(); + + if ($username != '') { + try { + $profileResult = $storage->getUserProfileInformation($usernameCanonical); + if(count($profileResult) > 0) { + if(array_key_exists('usrname', $profileResult[0])) { + if(!is_null($profileResult[0]['usrname'])) { + $userProfile = $profileResult[0]; + } + } + } + $lastSeenQuery = $storage->getTimeSinceLastPlayed($usernameCanonical); + if(count($lastSeenQuery) > 0) { + if(array_key_exists('last_updated', $lastSeenQuery[0])) { + if(!is_null($lastSeenQuery[0]['last_updated'])) { + $lastSeenSeconds = strtotime("now") - $lastSeenQuery[0]['last_updated']; + $lastSeenObj = $this->secondsToTime($lastSeenSeconds); + $lastSeen = ''; + if($lastSeenObj["d"] > 0) { + $lastSeen .= $lastSeenObj["d"] . ' days '; + } + if($lastSeenObj["h"] > 0) { + $lastSeen .= $lastSeenObj["h"] . ' hours '; + } + if($lastSeenObj["m"] > 0) { + $lastSeen .= $lastSeenObj["m"] . ' mins '; + } + if($lastSeenObj["s"] > 0) { + $lastSeen .= $lastSeenObj["s"] . ' secs'; + } + if($lastSeen != '') { + $lastSeen .= ' ago'; + } + } + } + } + } catch(StorageSchemaException $e) { + $errors[] = "Error: Database schema is not queryable"; + } catch(StorageConnectionException $e) { + $errors[] = "Error: Database is not available"; + } + $storage->close(); + } + + $values = array(); + + $values['userProfile'] = $userProfile; + $values['username'] = $username; + $values['usernameCanonical'] = $usernameCanonical; + $values['errors'] = $errors; + $values['lastSeen'] = $lastSeen; + + $container->setTitle($username); + + $container->render('profile.html.php', $values); + } + + protected function secondsToTime($inputSeconds) { + $secondsInAMinute = 60; + $secondsInAnHour = 60 * $secondsInAMinute; + $secondsInADay = 24 * $secondsInAnHour; + + // extract days + $days = floor($inputSeconds / $secondsInADay); + + // extract hours + $hourSeconds = $inputSeconds % $secondsInADay; + $hours = floor($hourSeconds / $secondsInAnHour); + + // extract minutes + $minuteSeconds = $hourSeconds % $secondsInAnHour; + $minutes = floor($minuteSeconds / $secondsInAMinute); + + // extract the remaining seconds + $remainingSeconds = $minuteSeconds % $secondsInAMinute; + $seconds = ceil($remainingSeconds); + + // return the final array + $obj = array( + 'd' => (int) $days, + 'h' => (int) $hours, + 'm' => (int) $minutes, + 's' => (int) $seconds, + ); + return $obj; + } + +} diff --git a/php/css/triviatime.css b/php/css/triviatime.css index be743c3..9da89d1 100644 --- a/php/css/triviatime.css +++ b/php/css/triviatime.css @@ -21,6 +21,12 @@ body { font-weight: 200; line-height: 16px; margin-bottom: 10px; + margin-right:0px; + margin-left:0px; +} +.padded-row { + margin-top: 60px; + margin-bottom: 60px; } .breakable { word-wrap: break-word; @@ -32,6 +38,38 @@ body { background-color: #E0E0E0; cursor: pointer; } +.form-signin { + max-width: 300px; + padding: 19px 29px 29px; + margin: 0 auto 20px; + background-color: #fff; + border: 1px solid #e5e5e5; + -webkit-border-radius: 5px; + -moz-border-radius: 5px; + border-radius: 5px; + -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05); + -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05); + box-shadow: 0 1px 2px rgba(0,0,0,.05); +} +.form-signin .form-signin-heading, +.form-signin .checkbox { + margin-bottom: 10px; +} +.form-signin input[type="text"], +.form-signin input[type="password"] { + font-size: 16px; + height: auto; + margin-bottom: 15px; + padding: 7px 9px; +} +@media (max-width: 980px) { + /* Enable use of floated navbar text */ + .navbar-text.pull-right { + float: none; + padding-left: 5px; + padding-right: 5px; + } +} @media screen and (max-height: 600px) { .modal-body { max-height: 340px; diff --git a/php/fonts/glyphicons-halflings-regular.eot b/php/fonts/glyphicons-halflings-regular.eot new file mode 100644 index 0000000..423bd5d Binary files /dev/null and b/php/fonts/glyphicons-halflings-regular.eot differ diff --git a/php/fonts/glyphicons-halflings-regular.svg b/php/fonts/glyphicons-halflings-regular.svg new file mode 100644 index 0000000..4469488 --- /dev/null +++ b/php/fonts/glyphicons-halflings-regular.svg @@ -0,0 +1,229 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/php/fonts/glyphicons-halflings-regular.ttf b/php/fonts/glyphicons-halflings-regular.ttf new file mode 100644 index 0000000..a498ef4 Binary files /dev/null and b/php/fonts/glyphicons-halflings-regular.ttf differ diff --git a/php/fonts/glyphicons-halflings-regular.woff b/php/fonts/glyphicons-halflings-regular.woff new file mode 100644 index 0000000..d83c539 Binary files /dev/null and b/php/fonts/glyphicons-halflings-regular.woff differ diff --git a/php/includes/Bootstrap.php b/php/includes/Bootstrap.php index 3512b78..d3e21de 100644 --- a/php/includes/Bootstrap.php +++ b/php/includes/Bootstrap.php @@ -5,16 +5,38 @@ class Bootstrap public $config; public $login; public $renderer; + public $router; - public function __construct($config) { + public function __construct($config, $routes) { $this->config = $config; $this->storage = new Storage($this->config['dbLocation']); $this->login = new Login($this->storage); $this->renderer = new Renderer($config, $this); + $this->router = new Router(); + $this->loadRoutes($routes); $this->setCurrentPage(basename($_SERVER['PHP_SELF'])); } + public function loadRoutes($routes) { + $this->router->setBasePath($routes['base']); + foreach($routes['routes'] as $route) { + $pattern = ''; + $target = ''; + $args = array(); + if(array_key_exists('pattern', $route)) { + $pattern = $route['pattern']; + } + if(array_key_exists('target', $route)) { + $target = $route['target']; + } + if(array_key_exists('args', $route)) { + $args = $route['args']; + } + $this->router->map($pattern, $target, $args); + } + } + public function render($page, $values=array()) { $this->renderer->render($page, $values); } @@ -56,4 +78,19 @@ class Bootstrap public function getConfig() { return $this->config; } + + public function set404() { + header("HTTP/1.0 404 Not Found"); + } + + public function render404() { + $this->set404(); + $this->render('404.html.php'); + die(); + } + + public function redirect($page) { + header('Location: ' . $page); + die(); + } } diff --git a/php/includes/Renderer.php b/php/includes/Renderer.php index 66db798..8ce7b3f 100644 --- a/php/includes/Renderer.php +++ b/php/includes/Renderer.php @@ -11,7 +11,7 @@ class Renderer $this->container = $container; } - public function render($page, $values) { + public function render($page, $values, $useTemplate=true) { $viewVars = array(); $viewVars['title'] = $this->title; if($this->title != '') { @@ -20,10 +20,14 @@ class Renderer $viewVars['currentPage'] = $this->currentPage; $container = $this->container; - - include($this->config['viewLocation'] . 'header.html.php'); - include($this->config['viewLocation'] . $page); - include($this->config['viewLocation'] . 'footer.html.php'); + + if($useTemplate) { + include($this->config['viewLocation'] . 'header.html.php'); + include($this->config['viewLocation'] . $page); + include($this->config['viewLocation'] . 'footer.html.php'); + } else { + include($this->config['viewLocation'] . $page); + } } public function setTitle($title) { diff --git a/php/includes/autoload.php b/php/includes/autoload.php index 4628e0e..bfdecf0 100644 --- a/php/includes/autoload.php +++ b/php/includes/autoload.php @@ -1,14 +1,13 @@ getStorage(); - -$errors = array(); -$result = array(); -try { - $result = $storage->getRecentAskedQuestions(); -} catch(StorageSchemaException $e) { - $errors[] = "Error: Database schema is not queryable"; -} catch(StorageConnectionException $e) { - $errors[] = "Error: Database is not available"; -} -$storage->close(); - -$values = array(); - -$values['result'] = $result; -$values['errors'] = $errors; - -$container->setTitle('Home'); - -$container->render('home.html.php', $values); diff --git a/php/login.php b/php/login.php deleted file mode 100644 index f78a287..0000000 --- a/php/login.php +++ /dev/null @@ -1,60 +0,0 @@ -getConfig()['defaultLoginRedirectPage']; - if(array_key_exists('lastPage', $_SESSION)) { - redirect($_SESSION('lastPage')); - } else { - redirect($defaultPage); - } -} - -$login = $container->getLogin(); - -if($login->isLoggedIn()) { - doRedirect(); -} - -$errors = array(); -if($_SERVER['REQUEST_METHOD'] === 'POST') { - $username = getPostVariable("username"); - $password = getPostVariable("password"); - - if(empty($username)) { - $errors['username'] = "Please enter a Username."; - } - if(empty($password)) { - $errors['password'] = "Please enter a Password."; - } - if(!empty($username) && !empty($password)) { - if($login->login($username, $password)) { - $container->setNotice("Success! You have been successfully logged in."); - doRedirect(); - } else { - $errors['login'] = "Invalid credentials. Please check your username and password, and try again."; - } - } -} - -$values = array(); -$values['errors'] = $errors; - -$container->setTitle("Login"); -$container->render("login.html.php", $values); diff --git a/php/logout.php b/php/logout.php deleted file mode 100644 index b5ef3be..0000000 --- a/php/logout.php +++ /dev/null @@ -1,17 +0,0 @@ -getLogin(); - -$login->logout(); - -// force a new session, for the notice -$container->login = new Login(); - -$container->setNotice("Success! You have been successfully logged out."); -redirect($container->getConfig()['defaultLoginRedirectPage']); diff --git a/php/profile.php b/php/profile.php deleted file mode 100644 index e601f5f..0000000 --- a/php/profile.php +++ /dev/null @@ -1,122 +0,0 @@ -getStorage(); - -$username = ''; -$usernameCanonical = ''; - -if(array_key_exists('username', $_GET)) { - // Convert username to lowercase in irc - $username = $_GET['username']; - $ircLowerSymbols = array("\\"=>"|", "["=>"{", "]"=>"}", "~"=>"^"); - $usernameCanonical = strtr($username, $ircLowerSymbols); - $usernameCanonical = strtolower($usernameCanonical); -} - -$lastSeen = 'Never'; - -function emptyResult() { - $result = array(); - $result['usrname'] = "Not found"; - $result['count'] = 0; - $result['score'] = 0; - $result['points'] = 0; - $result['q_asked'] = 0; - $result['num_e'] = 0; - $result['num_e_accepted'] = 0; - $result['num_q'] = 0; - $result['num_q_accepted'] = 0; - $result['num_r'] = 0; - $result['highest_streak'] = 0; - return $result; -} - -function secondsToTime($inputSeconds) { - - $secondsInAMinute = 60; - $secondsInAnHour = 60 * $secondsInAMinute; - $secondsInADay = 24 * $secondsInAnHour; - - // extract days - $days = floor($inputSeconds / $secondsInADay); - - // extract hours - $hourSeconds = $inputSeconds % $secondsInADay; - $hours = floor($hourSeconds / $secondsInAnHour); - - // extract minutes - $minuteSeconds = $hourSeconds % $secondsInAnHour; - $minutes = floor($minuteSeconds / $secondsInAMinute); - - // extract the remaining seconds - $remainingSeconds = $minuteSeconds % $secondsInAMinute; - $seconds = ceil($remainingSeconds); - - // return the final array - $obj = array( - 'd' => (int) $days, - 'h' => (int) $hours, - 'm' => (int) $minutes, - 's' => (int) $seconds, - ); - return $obj; -} - -$userProfile = emptyResult(); -$errors = array(); - -if ($username != '') { - try { - $profileResult = $storage->getUserProfileInformation($usernameCanonical); - if(count($profileResult) > 0) { - if(array_key_exists('usrname', $profileResult[0])) { - if(!is_null($profileResult[0]['usrname'])) { - $userProfile = $profileResult[0]; - } - } - } - $lastSeenQuery = $storage->getTimeSinceLastPlayed($usernameCanonical); - if(count($lastSeenQuery) > 0) { - if(array_key_exists('last_updated', $lastSeenQuery[0])) { - if(!is_null($lastSeenQuery[0]['last_updated'])) { - $lastSeenSeconds = strtotime("now") - $lastSeenQuery[0]['last_updated']; - $lastSeenObj = secondsToTime($lastSeenSeconds); - $lastSeen = ''; - if($lastSeenObj["d"] > 0) { - $lastSeen .= $lastSeenObj["d"] . ' days '; - } - if($lastSeenObj["h"] > 0) { - $lastSeen .= $lastSeenObj["h"] . ' hours '; - } - if($lastSeenObj["m"] > 0) { - $lastSeen .= $lastSeenObj["m"] . ' mins '; - } - if($lastSeenObj["s"] > 0) { - $lastSeen .= $lastSeenObj["s"] . ' secs'; - } - if($lastSeen != '') { - $lastSeen .= ' ago'; - } - } - } - } - } catch(StorageSchemaException $e) { - $errors[] = "Error: Database schema is not queryable"; - } catch(StorageConnectionException $e) { - $errors[] = "Error: Database is not available"; - } - $storage->close(); -} - -$values = array(); - -$values['userProfile'] = $userProfile; -$values['username'] = $username; -$values['usernameCanonical'] = $usernameCanonical; -$values['errors'] = $errors; -$values['lastSeen'] = $lastSeen; - -$container->setTitle($username); - -$container->render('profile.html.php', $values); diff --git a/php/reports.php b/php/reports.php deleted file mode 100644 index 52cbab8..0000000 --- a/php/reports.php +++ /dev/null @@ -1,99 +0,0 @@ -getStorage(); - -function getPageVariable($name) { - if(array_key_exists($name, $_GET)) { - $page = $_GET[$name]; - } - if(!isset($page)) { - $page = 1; - } - if($page < 1) { - $page = 1; - } - return $page; -} - -$reportPage = getPageVariable('rp'); -$editPage = getPageVariable('ep'); -$newPage = getPageVariable('np'); -$deletePage = getPageVariable('dp'); - -$maxResults = 5; - -$reportResultCount = 0; -$reportResult = array(); -$reportErrors = array(); -try { - $reportResult = $storage->getTopReports($reportPage, $maxResults); - $reportResultCount = $storage->getCountReports(); -} catch(StorageSchemaException $e) { - $reportErrors[] = "Error: Database schema is not queryable"; -} catch(StorageConnectionException $e) { - $reportErrors[] = "Error: Database is not available"; -} - -$editResultCount = 0; -$editResult = array(); -$editErrors = array(); -try { - $editResult = $storage->getTopEdits($editPage, $maxResults); - $editResultCount = $storage->getCountEdits(); -} catch(StorageSchemaException $e) { - $editErrors[] = "Error: Database schema is not queryable"; -} catch(StorageConnectionException $e) { - $editErrors[] = "Error: Database is not available"; -} - -$newResultCount = 0; -$newResult = array(); -$newErrors = array(); -try { - $newResult = $storage->getTopNewQuestions($newPage, $maxResults); - $newResultCount = $storage->getCountNewQuestions(); -} catch(StorageSchemaException $e) { - $newErrors[] = "Error: Database schema is not queryable"; -} catch(StorageConnectionException $e) { - $newErrors[] = "Error: Database is not available"; -} - -$deleteResultCount = 0; -$deleteResult = array(); -$deleteErrors = array(); -try { - $deleteResult = $storage->getTopDeletions($deletePage, $maxResults); - $deleteResultCount = $storage->getCountDeletions(); -} catch(StorageSchemaException $e) { - $deleteErrors[] = "Error: Database schema is not queryable"; -} catch(StorageConnectionException $e) { - $deleteErrors[] = "Error: Database is not available"; -} - -$values = array(); -$values['maxResults'] = $maxResults; - -$values['reportResultCount'] = $reportResultCount; -$values['reportResult'] = $reportResult; -$values['reportErrors'] = $reportErrors; -$values['reportPage'] = $reportPage; - -$values['editResultCount'] = $editResultCount; -$values['editResult'] = $editResult; -$values['editErrors'] = $editErrors; -$values['editPage'] = $editPage; - -$values['newResultCount'] = $newResultCount; -$values['newResult'] = $newResult; -$values['newErrors'] = $newErrors; -$values['newPage'] = $newPage; - -$values['deleteResultCount'] = $deleteResultCount; -$values['deleteResult'] = $deleteResult; -$values['deleteErrors'] = $deleteErrors; -$values['deletePage'] = $deletePage; - -$container->setTitle('Reports'); - -$container->render('reports.html.php', $values); diff --git a/php/stats.php b/php/stats.php deleted file mode 100644 index 1025571..0000000 --- a/php/stats.php +++ /dev/null @@ -1,58 +0,0 @@ -getStorage(); - -$dayResult = array(); -$dayErrors = array(); -try { - $dayResult = $storage->getDayTopScores(1, 10); -} catch(StorageSchemaException $e) { - $dayErrors[] = "Error: Database schema is not queryable"; -} catch(StorageConnectionException $e) { - $dayErrors[] = "Error: Database is not available"; -} - -$weekResult = array(); -$weekErrors = array(); -try { - $weekResult = $storage->getWeekTopScores(1, 10); -} catch(StorageSchemaException $e) { - $weekErrors[] = "Error: Database schema is not queryable"; -} catch(StorageConnectionException $e) { - $weekErrors[] = "Error: Database is not available"; -} - -$monthResult = array(); -$monthErrors = array(); -try { - $monthResult = $storage->getMonthTopScores(1, 10); -} catch(StorageSchemaException $e) { - $monthErrors[] = "Error: Database schema is not queryable"; -} catch(StorageConnectionException $e) { - $monthErrors[] = "Error: Database is not available"; -} - -$yearResult = array(); -$yearErrors = array(); -try { - $yearResult = $storage->getYearTopScores(1, 10); -} catch(StorageSchemaException $e) { - $yearErrors[] = "Error: Database schema is not queryable"; -} catch(StorageConnectionException $e) { - $yearErrors[] = "Error: Database is not available"; -} - -$values = array(); -$values['dayResult'] = $dayResult; -$values['dayErrors'] = $dayErrors; -$values['weekResult'] = $weekResult; -$values['weekErrors'] = $weekErrors; -$values['monthResult'] = $monthResult; -$values['monthErrors'] = $monthErrors; -$values['yearResult'] = $yearResult; -$values['yearErrors'] = $yearErrors; - -$container->setTitle('Stats'); - -$container->render('stats.html.php', $values); diff --git a/php/top.php b/php/top.php deleted file mode 100644 index 504602d..0000000 --- a/php/top.php +++ /dev/null @@ -1,62 +0,0 @@ -getStorage(); - -$timespans = array('d'=>'Day', 'w'=>'Week', 'm'=>'Month', 'y'=>'Year'); -$timespan = 'd'; -$timeDesc = 'Day'; -if(array_key_exists('t', $_GET)) { - if(array_key_exists(strtolower($_GET['t']), $timespans)) { - $timespan = strtolower($_GET['t']); - $timeDesc = $timespans[$timespan]; - } -} - -if(array_key_exists('page', $_GET)) { - $page = $_GET['page']; -} -if(!isset($page)) { - $page = 1; -} -if($page < 1) { - $page = 1; -} - -$maxResults = 20; - -$resultCount = 0; -$result = array(); -$errors = array(); -try { - if ($timespan == 'w') { - $result = $storage->getWeekTopScores($page, $maxResults); - $resultCount = $storage->getCountWeekTopScores(); - } else if ($timespan == 'm') { - $result = $storage->getMonthTopScores($page, $maxResults); - $resultCount = $storage->getCountMonthTopScores(); - } else if ($timespan == 'y') { - $result = $storage->getYearTopScores($page, $maxResults); - $resultCount = $storage->getCountYearTopScores(); - } else { - $result = $storage->getDayTopScores($page, $maxResults); - $resultCount = $storage->getCountDayTopScores(); - } -} catch(StorageSchemaException $e) { - $errors[] = "Error: Database schema is not queryable"; -} catch(StorageConnectionException $e) { - $errors[] = "Error: Database is not available"; -} - -$values = array(); -$values['result'] = $result; -$values['resultCount'] = $resultCount; -$values['maxResults'] = $maxResults; -$values['page'] = $page; -$values['timespan'] = $timespan; -$values['timeDesc'] = $timeDesc; -$values['errors'] = $errors; - -$container->setTitle('Top Scores for ' . $timeDesc); - -$container->render('top.html.php', $values); diff --git a/php/user.php b/php/user.php deleted file mode 100644 index 83245b0..0000000 --- a/php/user.php +++ /dev/null @@ -1,59 +0,0 @@ -getStorage(); - -$username = ''; -$usernameCanonical = ''; - -if(array_key_exists('username', $_GET)) { - // Convert username to lowercase in irc - $username = $_GET['username']; - $ircLowerSymbols = array("\\"=>"|", "["=>"{", "]"=>"}", "~"=>"^"); - $usernameCanonical = strtr($username, $ircLowerSymbols); - $usernameCanonical = strtolower($usernameCanonical); -} - -if(array_key_exists('page', $_GET)) { - $page = $_GET['page']; -} -if(!isset($page)) { - $page = 1; -} -if($page < 1) { - $page = 1; -} - -$maxResults = 10; -$usersCount = 0; -$users = array(); -$errors = array(); - -try { - $users = $storage->getUserLikeUsernameCanonical($usernameCanonical, $page, $maxResults); - $usersCount = $storage->getCountUserLikeUsernameCanonical($usernameCanonical); -} catch(StorageSchemaException $e) { - $errors[] = "Error: Database schema is not queryable"; -} catch(StorageConnectionException $e) { - $errors[] = "Error: Database is not available"; -} - -$storage->close(); - -// Redirect to profile if only 1 result found -if(count($users) == 1) { - header('Location: profile.php?username=' . rawurlencode($users[0]['username'])); - die(); -} - -$values = array(); -$values['username'] = $username; -$values['usernameCanonical'] = $usernameCanonical; -$values['page'] = $page; -$values['maxResults'] = $maxResults; -$values['usersCount'] = $usersCount; -$values['users'] = $users; -$values['errors'] = $errors; - -$container->setTitle('Players'); - -$container->render('user.html.php', $values); diff --git a/php/views/footer.html.php b/php/views/footer.html.php index 6846021..798459a 100644 --- a/php/views/footer.html.php +++ b/php/views/footer.html.php @@ -5,8 +5,8 @@ - - + + diff --git a/php/views/header.html.php b/php/views/header.html.php index 0ff1c93..ccf0fb6 100644 --- a/php/views/header.html.php +++ b/php/views/header.html.php @@ -7,9 +7,9 @@ - - - + + +