Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* utils> */
- function console_log( $data ){
- echo '<script>';
- echo 'console.log('. json_encode( $data ) .')';
- echo '</script>';
- }
- /* <utils */
- /* cookie> */
- $favoriteLeagues = @json_decode($_COOKIE["FAVORITE_LEAGUES"]);
- $preFavoriteFixtures = @json_decode($_COOKIE["FAVORITE_FIXTURES"]);
- $preFavoriteTeams = @json_decode($_COOKIE["FAVORITE_TEAMS"]);
- $favoriteFixtures = $preFavoriteFixtures ? $preFavoriteFixtures : [];
- $favoriteTeams = $preFavoriteTeams ? $preFavoriteTeams : [];
- /* <cookie */
- /* default top leagues> */
- if (!$favoriteLeagues) {
- $favoriteLeagues = array(2, 5, 8, 72, 82, 301, 384, 564, 732, 1328);
- setcookie('FAVORITE_LEAGUES', json_encode($favoriteLeagues), null, "/");
- }
- /* <default top leagues */
- if(@strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine') !== false) {
- $debug = false;
- } else {
- $debug = true;
- }
- require_once 'vendor/autoload.php';
- Twig_Autoloader::register();
- $loader = new Twig_Loader_Filesystem('src/templates');
- if($debug) {
- if(isset($_REQUEST['api_debug'])) {
- $api_base = 'http://35.195.34.69';
- } else {
- $api_base = 'https://api-v1-dot-scorengas.appspot.com';
- }
- $twig_config = [
- 'cache' => false,
- 'debug' => true,
- 'auto_reload' => true
- ];
- } else {
- if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
- $auth_string = substr($_SERVER['HTTP_AUTHORIZATION'], 6); // removing 'Basic ' from the string
- $auth_string = base64_decode($auth_string); // decoding string with user:password
- list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', $auth_string);
- }
- if(@$_SERVER['PHP_AUTH_USER'] != 'test' or @$_SERVER['PHP_AUTH_PW'] != 'test') {
- header('WWW-Authenticate: Basic realm="Closed"');
- header('HTTP/1.0 401 Unauthorized');
- exit;
- }
- $api_base = 'https://api-v1.scorenga.co.uk';
- $twig_config = [
- // 'cache' => 'cache/twig'
- 'cache' => false
- ];
- }
- $twig = new Twig_Environment($loader, $twig_config);
- /* php consts> */
- $twig->addGlobal('API_BASE', $api_base);
- $twig->addGlobal('DEBUG', $debug);
- $twig->addGlobal('FAVORITE_LEAGUES', $favoriteLeagues);
- $twig->addGlobal('FAVORITE_FIXTURES_COUNT', count($favoriteFixtures));
- $twig->addGlobal('FAVORITE_TEAMS_COUNT', count($favoriteTeams));
- $twig->addGlobal('SERVER_DATE_TIME', 'test');
- $twig->addGlobal('regions', json_decode(file_get_contents('data/regions.json')));
- /* <php consts */
- if($left_regions_block = @file_get_contents('cache/blocks/left_regions.html')) {
- $twig->addGlobal('left_regions_block', $left_regions_block);
- }
- class Router {
- static $_routes = [];
- static $_404_route = null;
- static function add_route($pattern, $callback) {
- $pattern = '#^' . str_replace('/', '\/', $pattern) . '$#';
- self::$_routes[$pattern] = $callback;
- }
- static function add_404($callback) {
- self::$_404_route = $callback;
- }
- static function run($url) {
- foreach(self::$_routes as $pattern => $callback) {
- if (preg_match($pattern, $url, $params)) {
- array_shift($params);
- return call_user_func_array($callback, array_values($params));
- }
- }
- if(self::$_404_route) {
- return call_user_func(self::$_404_route);
- }
- return http_response_code(404);
- }
- static function render($template, $context=[]) {
- global $twig;
- $twig->loadTemplate($template)->display($context);
- }
- static function api_get($endpoint, $params=[]) {
- global $api_base;
- $context_options=array(
- "ssl"=>array(
- "verify_peer"=>false,
- "verify_peer_name"=>false,
- ),
- );
- $context = stream_context_create($context_options);
- return json_decode(
- file_get_contents("$api_base/$endpoint?" . implode('&', $params), null, $context)
- );
- }
- }
- Router::add_route('/', function() {
- Router::render('pages/index.html');
- });
- Router::add_route('/match/(\d+)(?:|/(h2h))', function($fixture_id, $h2h=null) {
- $url = "fixtures/$fixture_id";
- $params = (array) Router::api_get($url);
- $params['is_h2h'] = true;
- Router::render('pages/match.html', $params);
- });
- Router::add_route('/team/[^/]+/(\d+)(|/results|/fixtures|/squad)', function($team_id, $section=null) {
- Router::render('pages/team.html', [
- 'team' => ['id' => $team_id]
- ]);
- });
- Router::add_route('/player/[^/]+/(\d+)', function($player_id) {
- Router::render('pages/player.html', [
- 'player' => ['id' => $player_id],
- ]);
- });
- Router::add_route('/league/[^/]+/[^/]+/(\d+)(|/|/results|/fixtures|/standings|/standings/(\d+)|/teams|/archive)', function($season_id, $section=null, $stage_id=null) {
- $SECTIONS_MAP = [
- '' => 'overview',
- '/' => 'overview',
- '/results' => 'results',
- '/fixtures' => 'upcoming',
- '/standings' => 'standings',
- '/teams' => 'teams',
- '/archive' => 'seasons'
- ];
- $part = $SECTIONS_MAP[$section];
- $url = "leagues/?part=$part&season_id=$season_id";
- if($stage_id) {
- $url .= "&stage_id=$stage_id";
- }
- Router::render('pages/league.html', [
- 'data' => (array) Router::api_get($url),
- 'tab' => $part
- ]);
- });
- Router::add_route('/standings/(\d+)(?:|/(\d+))', function($season_id, $stage_id=null) {
- Router::render('pages/standing.html', [
- 'season' => ['id' => $season_id],
- 'stage' => ['id' => $stage_id]
- ]);
- });
- Router::add_route('/top500/(draws|yellow-cards|over-under|referees|transfers)', function($section) {
- Router::render('pages/standing.html', [
- 'top' => ['type' => $section]
- ]);
- });
- /* error */
- Router::add_route('/error+(?:|/(.*))', function() {
- Router::render('pages/simple/500.html');
- });
- /* политика куки */
- Router::add_route('/privacy-policy+(?:|/(.*))', function() {
- Router::render('pages/simple/privacy_policy.html', [
- 'HIDE_LEFT_BLOCK' => true,
- 'HIDE_RIGHT_BLOCK' => true
- ]);
- });
- /* terms_and_conditions */
- Router::add_route('/terms-and-conditions+(?:|/(.*))', function() {
- Router::render('pages/simple/terms_and_conditions.html', [
- 'HIDE_LEFT_BLOCK' => true,
- 'HIDE_RIGHT_BLOCK' => true
- ]);
- });
- /* cookie_policy */
- Router::add_route('/cookie-policy+(?:|/(.*))', function() {
- Router::render('pages/simple/cookie_policy.html', [
- 'HIDE_LEFT_BLOCK' => true,
- 'HIDE_RIGHT_BLOCK' => true
- ]);
- });
- /* contacts */
- Router::add_route('/contacts+(?:|/(.*))', function() {
- Router::render('pages/simple/contacts.html', [
- 'HIDE_LEFT_BLOCK' => true,
- 'HIDE_RIGHT_BLOCK' => true
- ]);
- });
- /* wildcard */
- Router::add_route('/[^/]+(?:|/(.*))', function() {
- Router::render('pages/simple/404.html');
- });
- Router::run(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
Add Comment
Please, Sign In to add comment