<?php require_once 'Views.php'; require_once 'RepositoryList.php'; require_once 'Git.php'; require_once 'GitDiff.php'; require_once 'DiffPage.php'; class Router { private $repositories = []; private $git; public function __construct(string $reposPath) { $this->git = new Git($reposPath); $list = new RepositoryList($reposPath); $list->eachRepository(function($repo) { $this->repositories[] = $repo; }); } public function route(): Page { $reqRepo = $_GET['repo'] ?? ''; $action = $_GET['action'] ?? 'home'; $hash = $this->sanitizePath($_GET['hash'] ?? ''); $currentRepo = null; $decoded = urldecode($reqRepo); foreach ($this->repositories as $repo) { if ($repo['safe_name'] === $reqRepo || $repo['name'] === $decoded) { $currentRepo = $repo; break; } } if (!$currentRepo) { return new HomePage($this->repositories, $this->git); } $this->git->setRepository($currentRepo['path']); if ($action === 'raw') { return new RawPage($this->git, $hash); } if ($action === 'commit') { return new DiffPage($this->repositories, $currentRepo, $this->git, $hash); } if ($action === 'commits') { return new CommitsPage($this->repositories, $currentRepo, $this->git, $hash); } return new FilePage($this->repositories, $currentRepo, $this->git, $hash); } private function sanitizePath($path) { $path = str_replace(['..', '\\', "\0"], ['', '/', ''], $path); return preg_replace('/[^a-zA-Z0-9_\-\.\/]/', '', $path); } }