← Back
mainpublic_html/index.php
<?php
require __DIR__ . '/../app/helpers.php';
require __DIR__ . '/../app/db.php';
require __DIR__ . '/../app/auth.php';
require __DIR__ . '/../app/permissions.php';

$cfg = require __DIR__ . '/../app/config.php';

spl_autoload_register(function($class) {
  $paths = [
    __DIR__ . '/../app/controllers/' . $class . '.php',
    __DIR__ . '/../app/services/' . $class . '.php',
    __DIR__ . '/../app/' . $class . '.php',
  ];
  foreach ($paths as $p) if (is_file($p)) { require $p; return; }
});

$router = new Router();

$auth = new AuthController();
$explore = new ExploreController();
$repo = new RepoController($cfg);
$browse = new BrowseController($cfg);
$commit = new CommitController($cfg);
$branch = new BranchController($cfg);
$collab = new CollaboratorController();
$pull = new PullController($cfg);
$issue = new IssueController();

$router->get('/', fn()=> redirect('/explore'));

// Auth
$router->get('/login', fn()=> $auth->showLogin());
$router->post('/login', fn()=> $auth->login());
$router->get('/register', fn()=> $auth->showRegister());
$router->post('/register', fn()=> $auth->register());
$router->get('/logout', fn()=> $auth->logout());

// Explore
$router->get('/explore', fn()=> $explore->explore());

// Repos (requires login)
$router->get('/repos', fn()=> $repo->list());
$router->get('/repos/new', fn()=> $repo->showCreate());
$router->post('/repos/new', fn()=> $repo->create());

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?: '/';
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';

try {
  // Repo home
  if (preg_match('#^/r/(\d+)$#', $path, $m) && $method==='GET') {
    $repo->show((int)$m[1]); exit;
  }

  // Browse
  if (preg_match('#^/r/(\d+)/browse$#', $path, $m) && $method==='GET') {
    $browse->browse((int)$m[1]); exit;
  }
  if (preg_match('#^/r/(\d+)/file$#', $path, $m) && $method==='GET') {
    $browse->file((int)$m[1]); exit;
  }

  // ZIP commit
  if (preg_match('#^/r/(\d+)/commit-zip$#', $path, $m) && $method==='GET') {
    $commit->showZip((int)$m[1]); exit;
  }
  if (preg_match('#^/r/(\d+)/commit-zip$#', $path, $m) && $method==='POST') {
    $commit->commitZip((int)$m[1]); exit;
  }

  // Branches
  if (preg_match('#^/r/(\d+)/branches$#', $path, $m) && $method==='GET') {
    $branch->show((int)$m[1]); exit;
  }
  if (preg_match('#^/r/(\d+)/branches$#', $path, $m) && $method==='POST') {
    $branch->create((int)$m[1]); exit;
  }

  // Collaborators
  if (preg_match('#^/r/(\d+)/collaborators$#', $path, $m) && $method==='GET') {
    $collab->show((int)$m[1]); exit;
  }
  if (preg_match('#^/r/(\d+)/collaborators$#', $path, $m) && $method==='POST') {
    $collab->add((int)$m[1]); exit;
  }
  if (preg_match('#^/r/(\d+)/collaborators/(\d+)/delete$#', $path, $m) && $method==='POST') {
    $collab->remove((int)$m[1], (int)$m[2]); exit;
  }

  // Pull Requests
  if (preg_match('#^/r/(\d+)/pulls$#', $path, $m) && $method==='GET') {
    $pull->list((int)$m[1]); exit;
  }
  if (preg_match('#^/r/(\d+)/pulls/new$#', $path, $m) && $method==='GET') {
    $pull->showCreate((int)$m[1]); exit;
  }
  if (preg_match('#^/r/(\d+)/pulls/new$#', $path, $m) && $method==='POST') {
    $pull->create((int)$m[1]); exit;
  }
  if (preg_match('#^/r/(\d+)/pulls/(\d+)$#', $path, $m) && $method==='GET') {
    $pull->show((int)$m[1], (int)$m[2]); exit;
  }
  if (preg_match('#^/r/(\d+)/pulls/(\d+)/merge$#', $path, $m) && $method==='POST') {
    $pull->merge((int)$m[1], (int)$m[2]); exit;
  }

  // Issues
  if (preg_match('#^/r/(\d+)/issues$#', $path, $m) && $method==='GET') {
    $issue->list((int)$m[1]); exit;
  }
  if (preg_match('#^/r/(\d+)/issues/new$#', $path, $m) && $method==='GET') {
    $issue->showCreate((int)$m[1]); exit;
  }
  if (preg_match('#^/r/(\d+)/issues/new$#', $path, $m) && $method==='POST') {
    $issue->create((int)$m[1]); exit;
  }
  if (preg_match('#^/r/(\d+)/issues/(\d+)$#', $path, $m) && $method==='GET') {
    $issue->show((int)$m[1], (int)$m[2]); exit;
  }
  if (preg_match('#^/r/(\d+)/issues/(\d+)/comment$#', $path, $m) && $method==='POST') {
    $issue->comment((int)$m[1], (int)$m[2]); exit;
  }
  if (preg_match('#^/r/(\d+)/issues/(\d+)/toggle$#', $path, $m) && $method==='POST') {
    $issue->toggleState((int)$m[1], (int)$m[2]); exit;
  }

  $router->dispatch($path, $method);

} catch (Throwable $e) {
  http_response_code(500);
  flash_set('err', $e->getMessage());
  echo "<pre>" . e($e->getMessage()) . "</pre>";
}