sergeuniquite

Untitled

Apr 27th, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.82 KB | None | 0 0
  1. <?php
  2.     /* utils> */
  3.     function console_log( $data ){
  4.         echo '<script>';
  5.         echo 'console.log('. json_encode( $data ) .')';
  6.         echo '</script>';
  7.     }
  8.     /* <utils */
  9.  
  10.     /* cookie> */
  11.     $favoriteLeagues = @json_decode($_COOKIE["FAVORITE_LEAGUES"]);
  12.     $preFavoriteFixtures = @json_decode($_COOKIE["FAVORITE_FIXTURES"]);
  13.     $preFavoriteTeams = @json_decode($_COOKIE["FAVORITE_TEAMS"]);
  14.     $favoriteFixtures = $preFavoriteFixtures ? $preFavoriteFixtures : [];
  15.     $favoriteTeams = $preFavoriteTeams ? $preFavoriteTeams : [];
  16.     /* <cookie */
  17.  
  18.     /* default top leagues> */
  19.     if (!$favoriteLeagues) {
  20.         $favoriteLeagues = array(2, 5, 8, 72, 82, 301, 384, 564, 732, 1328);
  21.         setcookie('FAVORITE_LEAGUES', json_encode($favoriteLeagues), null, "/");
  22.     }
  23.     /* <default top leagues */
  24.  
  25.     if(@strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine') !== false) {
  26.         $debug = false;
  27.     } else {
  28.         $debug = true;
  29.     }
  30.  
  31.     require_once 'vendor/autoload.php';
  32.  
  33.     Twig_Autoloader::register();
  34.     $loader = new Twig_Loader_Filesystem('src/templates');
  35.  
  36.     if($debug) {
  37.         if(isset($_REQUEST['api_debug'])) {
  38.             $api_base = 'http://35.195.34.69';
  39.         } else {
  40.             $api_base = 'https://api-v1-dot-scorengas.appspot.com';
  41.         }
  42.         $twig_config = [
  43.             'cache' => false,
  44.             'debug' => true,
  45.             'auto_reload' => true
  46.         ];
  47.     } else {
  48.         if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
  49.             $auth_string = substr($_SERVER['HTTP_AUTHORIZATION'], 6);            // removing 'Basic ' from the string
  50.             $auth_string = base64_decode($auth_string);                                   // decoding string with user:password
  51.             list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', $auth_string);
  52.         }
  53.         if(@$_SERVER['PHP_AUTH_USER'] != 'test' or @$_SERVER['PHP_AUTH_PW'] != 'test') {
  54.             header('WWW-Authenticate: Basic realm="Closed"');
  55.             header('HTTP/1.0 401 Unauthorized');
  56.             exit;
  57.         }
  58.         $api_base = 'https://api-v1.scorenga.co.uk';
  59.         $twig_config = [
  60. //            'cache' => 'cache/twig'
  61.             'cache' => false
  62.         ];
  63.     }
  64.  
  65.     $twig = new Twig_Environment($loader, $twig_config);
  66.     /* php consts> */
  67.     $twig->addGlobal('API_BASE', $api_base);
  68.     $twig->addGlobal('DEBUG', $debug);
  69.     $twig->addGlobal('FAVORITE_LEAGUES', $favoriteLeagues);
  70.     $twig->addGlobal('FAVORITE_FIXTURES_COUNT', count($favoriteFixtures));
  71.     $twig->addGlobal('FAVORITE_TEAMS_COUNT', count($favoriteTeams));
  72.     $twig->addGlobal('SERVER_DATE_TIME', 'test');
  73.     $twig->addGlobal('regions', json_decode(file_get_contents('data/regions.json')));
  74.     /* <php consts */
  75.  
  76.     if($left_regions_block = @file_get_contents('cache/blocks/left_regions.html')) {
  77.         $twig->addGlobal('left_regions_block', $left_regions_block);
  78.     }
  79.  
  80.     class Router {
  81.         static $_routes = [];
  82.         static $_404_route = null;
  83.  
  84.         static function add_route($pattern, $callback) {
  85.             $pattern = '#^' . str_replace('/', '\/', $pattern) . '$#';
  86.             self::$_routes[$pattern] = $callback;
  87.         }
  88.  
  89.         static function add_404($callback) {
  90.             self::$_404_route = $callback;
  91.         }
  92.  
  93.         static function run($url) {
  94.             foreach(self::$_routes as $pattern => $callback) {
  95.                 if (preg_match($pattern, $url, $params)) {
  96.                     array_shift($params);
  97.                     return call_user_func_array($callback, array_values($params));
  98.                 }
  99.             }
  100.             if(self::$_404_route) {
  101.                 return call_user_func(self::$_404_route);
  102.             }
  103.             return http_response_code(404);
  104.         }
  105.  
  106.         static function render($template, $context=[]) {
  107.             global $twig;
  108.             $twig->loadTemplate($template)->display($context);
  109.         }
  110.  
  111.         static function api_get($endpoint, $params=[]) {
  112.             global $api_base;
  113.             $context_options=array(
  114.                 "ssl"=>array(
  115.                     "verify_peer"=>false,
  116.                     "verify_peer_name"=>false,
  117.                 ),
  118.             );
  119.             $context = stream_context_create($context_options);
  120.             return json_decode(
  121.                 file_get_contents("$api_base/$endpoint?" . implode('&', $params), null, $context)
  122.             );
  123.         }
  124.     }
  125.  
  126.     Router::add_route('/', function() {
  127.         Router::render('pages/index.html');
  128.     });
  129.  
  130.     Router::add_route('/match/(\d+)(?:|/(h2h))', function($fixture_id, $h2h=null) {
  131.         $url = "fixtures/$fixture_id";
  132.         $params = (array) Router::api_get($url);
  133.         $params['is_h2h'] = true;
  134.         Router::render('pages/match.html', $params);
  135.     });
  136.  
  137.     Router::add_route('/team/[^/]+/(\d+)(|/results|/fixtures|/squad)', function($team_id, $section=null) {
  138.         Router::render('pages/team.html', [
  139.             'team' => ['id' => $team_id]
  140.         ]);
  141.     });
  142.  
  143.     Router::add_route('/player/[^/]+/(\d+)', function($player_id) {
  144.         Router::render('pages/player.html', [
  145.             'player' => ['id' => $player_id],
  146.         ]);
  147.     });
  148.  
  149.     Router::add_route('/league/[^/]+/[^/]+/(\d+)(|/|/results|/fixtures|/standings|/standings/(\d+)|/teams|/archive)', function($season_id, $section=null, $stage_id=null) {
  150.         $SECTIONS_MAP = [
  151.             '' => 'overview',
  152.             '/' => 'overview',
  153.             '/results' => 'results',
  154.             '/fixtures' => 'upcoming',
  155.             '/standings' => 'standings',
  156.             '/teams' => 'teams',
  157.             '/archive' => 'seasons'
  158.         ];
  159.         $part = $SECTIONS_MAP[$section];
  160.         $url = "leagues/?part=$part&season_id=$season_id";
  161.         if($stage_id) {
  162.             $url .= "&stage_id=$stage_id";
  163.         }
  164.         Router::render('pages/league.html', [
  165.             'data' => (array) Router::api_get($url),
  166.             'tab' => $part
  167.         ]);
  168.     });
  169.  
  170.     Router::add_route('/standings/(\d+)(?:|/(\d+))', function($season_id, $stage_id=null) {
  171.         Router::render('pages/standing.html', [
  172.             'season' => ['id' => $season_id],
  173.             'stage' => ['id' => $stage_id]
  174.         ]);
  175.     });
  176.  
  177.     Router::add_route('/top500/(draws|yellow-cards|over-under|referees|transfers)', function($section) {
  178.         Router::render('pages/standing.html', [
  179.             'top' => ['type' => $section]
  180.         ]);
  181.     });
  182.  
  183.     /* error */
  184.     Router::add_route('/error+(?:|/(.*))', function() {
  185.         Router::render('pages/simple/500.html');
  186.     });
  187.  
  188.     /* политика куки */
  189.     Router::add_route('/privacy-policy+(?:|/(.*))', function() {
  190.         Router::render('pages/simple/privacy_policy.html', [
  191.             'HIDE_LEFT_BLOCK' => true,
  192.             'HIDE_RIGHT_BLOCK' => true
  193.         ]);
  194.     });
  195.  
  196.     /* terms_and_conditions */
  197.     Router::add_route('/terms-and-conditions+(?:|/(.*))', function() {
  198.         Router::render('pages/simple/terms_and_conditions.html', [
  199.             'HIDE_LEFT_BLOCK' => true,
  200.             'HIDE_RIGHT_BLOCK' => true
  201.         ]);
  202.     });
  203.  
  204.     /* cookie_policy */
  205.     Router::add_route('/cookie-policy+(?:|/(.*))', function() {
  206.         Router::render('pages/simple/cookie_policy.html', [
  207.             'HIDE_LEFT_BLOCK' => true,
  208.             'HIDE_RIGHT_BLOCK' => true
  209.         ]);
  210.     });
  211.  
  212.     /* contacts */
  213.     Router::add_route('/contacts+(?:|/(.*))', function() {
  214.         Router::render('pages/simple/contacts.html', [
  215.             'HIDE_LEFT_BLOCK' => true,
  216.             'HIDE_RIGHT_BLOCK' => true
  217.         ]);
  218.     });
  219.  
  220.     /* wildcard */
  221.     Router::add_route('/[^/]+(?:|/(.*))', function() {
  222.         Router::render('pages/simple/404.html');
  223.     });
  224.  
  225.     Router::run(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
Add Comment
Please, Sign In to add comment