<?php
/* error_reporting(E_ALL); */
/* ini_set('display_errors', 1); */

require_once __DIR__.DIRECTORY_SEPARATOR.'/autoload.php';

use Site\Lib\Route;

use Site\Controller\Atom;
use Site\Controller\Fediverse;
use Site\Controller\Home;
use Site\Controller\Notfound;
use Site\Controller\Page;
use Site\Controller\User;

define('ROOT', realpath(__DIR__));

include(ROOT.'/config/config.php');
include(ROOT.'/util.php');

$routes = [
  Route::add('GET', 'blog/{page}', Home::class.'@article'),
  Route::add('GET', 'about', Page::class.'@about'),
  Route::add('GET', 'monero', Page::class.'@monero'),
  Route::add('GET', 'secret', Page::class.'@secret'),
];

if (ACTIVITYPUB_ENABLED) {
  $routes[] = Route::add('GET', '.well-known/webfinger', Home::class.'@apfinger');
  $routes[] = Route::add('GET', 'ap/following', Fediverse::class.'@apfollowing');
  $routes[] = Route::add('GET', 'ap/followers', Fediverse::class.'@apfollowers');
  $routes[] = Route::add('GET', 'ap/outbox', Fediverse::class.'@apoutbox');
  $routes[] = Route::add('GET', 'ap/activities/create/{uuid}',
                         Fediverse::class.'@apactivity');
  $routes[] = Route::add('POST', 'ap/inbox', Fediverse::class.'@apinbox');
  $routes[] = Route::add('GET', 'ap/actor', Fediverse::class.'@apactor');
}

if (AUTH_ENABLED) {
  $routes[] = Route::add('POST', 'login', User::class.'@login');
  $routes[] = Route::add('GET', 'login', User::class.'@login');
  $routes[] = Route::add('GET', 'logout', User::class.'@logout');
}

/* if (RSS_ENABLED) {} */

if (ATOM_ENABLED) {
  $routes[] = Route::add('GET', 'blog.atom', Atom::class.'@feed');
}

$routes[] = Route::add('GET', '', Home::class.'@show');

Route::init($routes);
Route::setFallback([new Notfound(), 'show']);

$uri = urldecode($_SERVER['REQUEST_URI']);

// .xhtmlで終わるURLのリダイレクト処理
if (preg_match('/(.+)\.xhtml$/', $uri, $matches)) {
  // 末尾の.xhtmlを取り除く
  $newUri = $matches[1];
  header('HTTP/1.1 301 Moved Permanently');
  header('Location: ' . $newUri);
  exit();
}

$uri = urldecode($_SERVER['REQUEST_URI']);
if ($uri == '/index.php' || $uri == '/index.html') {
  header('Location: /');
  exit();
}

Route::dispatch($uri);
