<?php
namespace ScummVM;

/**
 * Development only
 * Don't re-route static file requests to index.php
 * And change directory context to public_html
 *
 * When DEV_SERVER is true a different Redis database is chosen
 * It's true when running using PHP built-in server or
 * if the DEV_SERVER environment variable is set to 1
 */
if (isset($_SERVER['SERVER_SOFTWARE']) &&
    \preg_match("/PHP [\d\.]+ Development Server/",$_SERVER['SERVER_SOFTWARE'])) {
  define('DEV_SERVER', true);
  chdir('public_html');
  if (\preg_match('/\.(?:png|jpg|jpeg|gif|css|js|svg)/', $_SERVER["REQUEST_URI"])) {
    return false;
  }
} else if (getenv('DEV_SERVER') === "1") {
  define('DEV_SERVER', true);
} else {
  define('DEV_SERVER', false);
}

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../orm/config.php';
require_once __DIR__ . '/../include/Constants.php';

/**
 * Multilingual support
 */
global $lang, $available_languages;
$languages = array_slice(scandir(DIR_DATA),2);
$available_languages = [];
foreach ($languages as $l) {
    if (!\is_dir(DIR_DATA . "/$l")) {
      continue;
    }
    $available_languages[$l] = \locale_get_display_name($l, $l);
}

// Backwards compatibility for lang query param & cookie
// TODO: Remove this eventually
$oldLangs = [
  "en_US" => "en",
  "el_GR" => "el",
  "es_ES" => "es",
  "fr_FR" => "fr",
  "he_IL" => "he",
  "it_IT" => "it",
  "pt_BR" => "pt-BR",
  "pt_PT" => "pt-PT",
  "ru_RU" => "ru"
];
if (!empty($_GET['lang'])) {
  $lang = $_GET['lang'];
  $uri = \preg_replace("/[?&]lang=$lang/i", "", $_SERVER['REQUEST_URI']);
  if (array_key_exists($lang, $available_languages)) {
    header("Location: " . "/$lang" . $uri);
  } elseif (array_key_exists($lang, $oldLangs)) {
    header("Location: /" . $oldLangs[$lang] . $uri);
  }
} elseif (!empty($_COOKIE['lang'])) {
  $lang = $_COOKIE['lang'];
  $cookie_options = [
    'expires' => time()-86400,
    'path' => '/',
    'domain' => $_SERVER['HTTP_HOST'],
    'secure' => true,
    'samesite' => 'None'
  ];
  if (\strpos($_SERVER['REQUEST_URI'], "/$lang/") === false) {
    if (array_key_exists($lang, $available_languages)) {
      header("Location: " . "/$lang" . $_SERVER['REQUEST_URI']);
    } elseif (array_key_exists($lang, $oldLangs)) {
      header("Location: /" . $oldLangs[$lang] . $_SERVER['REQUEST_URI']);
    }
  }
  setcookie("lang", "", $cookie_options);
}

$langs = join("|", array_keys($available_languages));
$langMatches = [];

if (\preg_match("/^\/($langs)(\/|$)/i", $_SERVER['REQUEST_URI'], $langMatches)) {
    $lang = $langMatches[1];
}

if (!array_key_exists($lang, $available_languages)) {
    $lang = DEFAULT_LOCALE;
}

/* Time zone to use for news items etc. */
date_default_timezone_set("UTC");

// set_include_path(get_include_path() . PATH_SEPARATOR . DIR_INCLUDE);
error_reporting(E_ALL ^ E_NOTICE);  // disable notices

if (!is_writeable(SMARTY_DIR_COMPILE)) {
    print "Smarty compile dir (" . SMARTY_DIR_COMPILE . ") isn't writeable!<br>\n";
    die(1);
}

/* Exception handling. */
set_exception_handler(array('ScummVM\ExceptionHandler', 'handleException'));

/* Page mapping. */
$pages = array(
    'compatibility'                         => '\ScummVM\Pages\CompatibilityPage',
    'compatibility/[cId:version]'           => '\ScummVM\Pages\CompatibilityPage',
    'compatibility/[cId:version]/[:game]'   => '\ScummVM\Pages\CompatibilityPage',
    'contact'                               => '\ScummVM\Pages\SimplePage',
    'credits'                               => '\ScummVM\Pages\SimplePage',
    'demos'                                 => '\ScummVM\Pages\DemosPage',
    'demos/director'                        => '\ScummVM\Pages\DirectorDemosPage',
    'documentation'                         => 'https://docs.scummvm.org/',
    'downloads'                             => '\ScummVM\Pages\DownloadsPage',
    'dumper-companion'                      => '\ScummVM\Pages\StaticPage',
    'games'                                 => '\ScummVM\Pages\GamesPage',
    'faq'                                   => 'https://docs.scummvm.org/en/latest/help/faq.html',
    'feeds'                                 => '\ScummVM\Pages\FeedsPage',
    'feeds/[a:type]'                        => '\ScummVM\Pages\FeedsPage',
    'links'                                 => '\ScummVM\Pages\LinksPage',
    ''                                      => '\ScummVM\Pages\NewsPage',
    'news'                                  => '\ScummVM\Pages\NewsPage',
    'news/[a:date]'                         => '\ScummVM\Pages\NewsPage',
    'press'                                 => '\ScummVM\Pages\SimplePage',
    'press/[a:article]'                     => '\ScummVM\Pages\ArticlePage',
    'screenshots'                           => '\ScummVM\Pages\ScreenshotsPage',
    'screenshots/[a:category]'              => '\ScummVM\Pages\ScreenshotsPage',
    'screenshots/[a:category]/[:game]'      => '\ScummVM\Pages\ScreenshotsPage',
    'sponsors'                              => '\ScummVM\Pages\SimplePage',
);

$router = new \AltoRouter();

// Custom match for Compatibility ID.
$router->addMatchTypes([
  'cId' => "dev|[\d\.]+([rc\d]+)?",
  'lang' => \join("|", array_keys(array_change_key_case($available_languages, CASE_LOWER)))
]);

foreach ($pages as $key => $value) {
    $router->map('GET', "/[lang:lang]?/{$key}/?", $value, $key);
}

$match = $router->match(strtolower($_SERVER['REQUEST_URI']));

if ($match) {
    if ($match['target'] === '\ScummVM\Pages\SimplePage' || $match['target'] === '\ScummVM\Pages\StaticPage') {
      $page = new $match['target']($match['name']);
    } else if (strpos($match['target'],"http") === 0) {
      header("Location: {$match['target']}");
      return;
    } else {
      $page = new $match['target']();
    }
    return $page->index($match['params']);
} else {
  $page = new \ScummVM\Pages\NewsPage();
  return $page->index(array());
}


[ICO]NameLast modifiedSizeDescription

[PARENTDIR]Parent Directory  -  
[DIR]access/2023-12-30 15:39 -  
[DIR]adl/2024-01-14 22:23 -  
[DIR]agi/2024-01-14 22:23 -  
[DIR]agos/2023-12-30 15:39 -  
[DIR]ags/2024-02-05 12:35 -  
[DIR]asylum/2024-04-14 16:50 -  
[DIR]bagel/2024-12-22 20:41 -  
[DIR]bbvs/2022-02-20 02:32 -  
[DIR]bladerunner/2022-02-20 02:32 -  
[DIR]buried/2023-12-30 15:39 -  
[DIR]cge/2022-02-20 02:32 -  
[DIR]cge2/2022-02-20 02:32 -  
[DIR]chewy/2023-12-30 15:39 -  
[DIR]cine/2023-12-30 15:39 -  
[DIR]composer/2022-02-20 02:32 -  
[TXT]create_thumbs.sh2022-12-10 23:31 109  
[DIR]cruise/2022-02-20 02:32 -  
[DIR]cryomni3d/2022-02-20 02:32 -  
[DIR]darkseed/2024-12-22 20:41 -  
[DIR]dgds/2024-12-22 20:41 -  
[DIR]director/2024-01-14 22:23 -  
[DIR]draci/2022-02-20 02:32 -  
[DIR]dragons/2022-06-08 23:29 -  
[DIR]drascula/2022-02-20 02:32 -  
[DIR]dreamweb/2022-02-20 02:32 -  
[DIR]efh/2024-01-02 16:56 -  
[DIR]freescape/2024-11-13 21:11 -  
[DIR]glk/2024-01-14 22:23 -  
[DIR]gnap/2022-02-20 02:32 -  
[DIR]gob/2023-07-17 16:45 -  
[DIR]griffon/2024-01-02 16:56 -  
[DIR]grim/2022-02-20 02:32 -  
[DIR]groovie/2022-02-20 02:32 -  
[DIR]hdb/2022-02-20 02:32 -  
[DIR]hopkins/2022-02-20 02:32 -  
[DIR]hugo/2023-12-30 15:39 -  
[DIR]hypno/2023-12-30 15:39 -  
[DIR]illusions/2022-06-08 23:17 -  
[DIR]kingdom/2024-01-02 16:56 -  
[DIR]kyra/2022-02-20 02:32 -  
[DIR]lab/2022-02-20 02:32 -  
[DIR]lure/2022-02-20 02:32 -  
[DIR]m4/2023-12-30 15:39 -  
[DIR]made/2022-02-20 02:32 -  
[DIR]mads/2022-02-20 02:32 -  
[TXT]mkthumb.sh2022-12-10 23:31 160  
[DIR]mm/2024-01-14 22:23 -  
[DIR]mohawk/2024-01-04 01:43 -  
[DIR]mortevielle/2022-02-20 02:32 -  
[DIR]mtropolis/2024-01-14 22:23 -  
[DIR]myst3/2022-02-20 02:32 -  
[DIR]nancy/2023-11-10 16:48 -  
[DIR]neverhood/2021-04-03 19:11 -  
[DIR]ngi/2022-02-20 02:32 -  
[DIR]parallaction/2022-02-20 02:32 -  
[DIR]pegasus/2022-02-20 02:32 -  
[DIR]petka/2024-01-14 22:23 -  
[DIR]pink/2023-12-30 15:39 -  
[DIR]plumbers/2022-02-20 02:32 -  
[DIR]prince/2024-01-04 01:43 -  
[DIR]private/2023-12-30 15:39 -  
[DIR]queen/2022-02-20 02:32 -  
[DIR]saga/2022-02-20 02:32 -  
[DIR]saga2/2024-01-04 01:43 -  
[DIR]sci/2024-01-14 22:23 -  
[DIR]scumm/2024-12-22 20:41 -  
[DIR]sherlock/2022-02-20 02:32 -  
[DIR]sky/2022-02-20 02:32 -  
[DIR]stark/2022-02-20 02:32 -  
[DIR]supernova/2023-12-30 15:39 -  
[DIR]sword1/2022-02-20 02:32 -  
[DIR]sword2/2022-02-20 02:32 -  
[DIR]sword25/2022-02-20 02:32 -  
[DIR]teenagent/2022-02-20 02:32 -  
[DIR]tetraedge/2023-12-30 15:39 -  
[DIR]tinsel/2022-02-20 02:32 -  
[DIR]titanic/2022-02-20 02:32 -  
[DIR]toltecs/2022-02-20 02:32 -  
[DIR]tony/2022-02-20 02:32 -  
[DIR]toon/2022-02-20 02:32 -  
[DIR]touche/2022-02-20 02:32 -  
[DIR]trecision/2022-04-28 02:26 -  
[DIR]tsage/2022-02-20 02:32 -  
[DIR]tucker/2022-02-20 02:32 -  
[DIR]twine/2022-02-20 02:32 -  
[DIR]twp/2024-12-22 20:41 -  
[DIR]ultima/2023-12-30 15:39 -  
[DIR]vcruise/2023-12-30 15:39 -  
[DIR]voyeur/2023-12-30 15:39 -  
[DIR]wintermute/2022-02-20 02:32 -  
[DIR]zvision/2022-02-20 02:32 -  

<?php
namespace ScummVM;

/**
 * Development only
 * Don't re-route static file requests to index.php
 * And change directory context to public_html
 *
 * When DEV_SERVER is true a different Redis database is chosen
 * It's true when running using PHP built-in server or
 * if the DEV_SERVER environment variable is set to 1
 */
if (isset($_SERVER['SERVER_SOFTWARE']) &&
    \preg_match("/PHP [\d\.]+ Development Server/",$_SERVER['SERVER_SOFTWARE'])) {
  define('DEV_SERVER', true);
  chdir('public_html');
  if (\preg_match('/\.(?:png|jpg|jpeg|gif|css|js|svg)/', $_SERVER["REQUEST_URI"])) {
    return false;
  }
} else if (getenv('DEV_SERVER') === "1") {
  define('DEV_SERVER', true);
} else {
  define('DEV_SERVER', false);
}

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../orm/config.php';
require_once __DIR__ . '/../include/Constants.php';

/**
 * Multilingual support
 */
global $lang, $available_languages;
$languages = array_slice(scandir(DIR_DATA),2);
$available_languages = [];
foreach ($languages as $l) {
    if (!\is_dir(DIR_DATA . "/$l")) {
      continue;
    }
    $available_languages[$l] = \locale_get_display_name($l, $l);
}

// Backwards compatibility for lang query param & cookie
// TODO: Remove this eventually
$oldLangs = [
  "en_US" => "en",
  "el_GR" => "el",
  "es_ES" => "es",
  "fr_FR" => "fr",
  "he_IL" => "he",
  "it_IT" => "it",
  "pt_BR" => "pt-BR",
  "pt_PT" => "pt-PT",
  "ru_RU" => "ru"
];
if (!empty($_GET['lang'])) {
  $lang = $_GET['lang'];
  $uri = \preg_replace("/[?&]lang=$lang/i", "", $_SERVER['REQUEST_URI']);
  if (array_key_exists($lang, $available_languages)) {
    header("Location: " . "/$lang" . $uri);
  } elseif (array_key_exists($lang, $oldLangs)) {
    header("Location: /" . $oldLangs[$lang] . $uri);
  }
} elseif (!empty($_COOKIE['lang'])) {
  $lang = $_COOKIE['lang'];
  $cookie_options = [
    'expires' => time()-86400,
    'path' => '/',
    'domain' => $_SERVER['HTTP_HOST'],
    'secure' => true,
    'samesite' => 'None'
  ];
  if (\strpos($_SERVER['REQUEST_URI'], "/$lang/") === false) {
    if (array_key_exists($lang, $available_languages)) {
      header("Location: " . "/$lang" . $_SERVER['REQUEST_URI']);
    } elseif (array_key_exists($lang, $oldLangs)) {
      header("Location: /" . $oldLangs[$lang] . $_SERVER['REQUEST_URI']);
    }
  }
  setcookie("lang", "", $cookie_options);
}

$langs = join("|", array_keys($available_languages));
$langMatches = [];

if (\preg_match("/^\/($langs)(\/|$)/i", $_SERVER['REQUEST_URI'], $langMatches)) {
    $lang = $langMatches[1];
}

if (!array_key_exists($lang, $available_languages)) {
    $lang = DEFAULT_LOCALE;
}

/* Time zone to use for news items etc. */
date_default_timezone_set("UTC");

// set_include_path(get_include_path() . PATH_SEPARATOR . DIR_INCLUDE);
error_reporting(E_ALL ^ E_NOTICE);  // disable notices

if (!is_writeable(SMARTY_DIR_COMPILE)) {
    print "Smarty compile dir (" . SMARTY_DIR_COMPILE . ") isn't writeable!<br>\n";
    die(1);
}

/* Exception handling. */
set_exception_handler(array('ScummVM\ExceptionHandler', 'handleException'));

/* Page mapping. */
$pages = array(
    'compatibility'                         => '\ScummVM\Pages\CompatibilityPage',
    'compatibility/[cId:version]'           => '\ScummVM\Pages\CompatibilityPage',
    'compatibility/[cId:version]/[:game]'   => '\ScummVM\Pages\CompatibilityPage',
    'contact'                               => '\ScummVM\Pages\SimplePage',
    'credits'                               => '\ScummVM\Pages\SimplePage',
    'demos'                                 => '\ScummVM\Pages\DemosPage',
    'demos/director'                        => '\ScummVM\Pages\DirectorDemosPage',
    'documentation'                         => 'https://docs.scummvm.org/',
    'downloads'                             => '\ScummVM\Pages\DownloadsPage',
    'dumper-companion'                      => '\ScummVM\Pages\StaticPage',
    'games'                                 => '\ScummVM\Pages\GamesPage',
    'faq'                                   => 'https://docs.scummvm.org/en/latest/help/faq.html',
    'feeds'                                 => '\ScummVM\Pages\FeedsPage',
    'feeds/[a:type]'                        => '\ScummVM\Pages\FeedsPage',
    'links'                                 => '\ScummVM\Pages\LinksPage',
    ''                                      => '\ScummVM\Pages\NewsPage',
    'news'                                  => '\ScummVM\Pages\NewsPage',
    'news/[a:date]'                         => '\ScummVM\Pages\NewsPage',
    'press'                                 => '\ScummVM\Pages\SimplePage',
    'press/[a:article]'                     => '\ScummVM\Pages\ArticlePage',
    'screenshots'                           => '\ScummVM\Pages\ScreenshotsPage',
    'screenshots/[a:category]'              => '\ScummVM\Pages\ScreenshotsPage',
    'screenshots/[a:category]/[:game]'      => '\ScummVM\Pages\ScreenshotsPage',
    'sponsors'                              => '\ScummVM\Pages\SimplePage',
);

$router = new \AltoRouter();

// Custom match for Compatibility ID.
$router->addMatchTypes([
  'cId' => "dev|[\d\.]+([rc\d]+)?",
  'lang' => \join("|", array_keys(array_change_key_case($available_languages, CASE_LOWER)))
]);

foreach ($pages as $key => $value) {
    $router->map('GET', "/[lang:lang]?/{$key}/?", $value, $key);
}

$match = $router->match(strtolower($_SERVER['REQUEST_URI']));

if ($match) {
    if ($match['target'] === '\ScummVM\Pages\SimplePage' || $match['target'] === '\ScummVM\Pages\StaticPage') {
      $page = new $match['target']($match['name']);
    } else if (strpos($match['target'],"http") === 0) {
      header("Location: {$match['target']}");
      return;
    } else {
      $page = new $match['target']();
    }
    return $page->index($match['params']);
} else {
  $page = new \ScummVM\Pages\NewsPage();
  return $page->index(array());
}