X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=app%2FController%2FGithub.php;h=7b7bdf64d976547cee05bd7331439cfe6c102a5f;hb=efea782d7086c58432477ce2847d2be1d8b25fa6;hp=6432c868670550c0355fdaad16e7a4e9087e6e2b;hpb=728020f863c8562d9c42e926b08ba1c7a390e53d;p=pharext%2Fpharext.org diff --git a/app/Controller/Github.php b/app/Controller/Github.php index 6432c86..7b7bdf6 100644 --- a/app/Controller/Github.php +++ b/app/Controller/Github.php @@ -4,11 +4,14 @@ namespace app\Controller; use app\Controller; use app\Github\API; +use app\Model\Account; +use app\Model\Owner; +use app\Model\Token; use app\Session; use app\Web; - +use http\Cookie; +use http\Header; use http\QueryString; -use http\Url; abstract class Github implements Controller { @@ -18,15 +21,15 @@ abstract class Github implements Controller protected $app; /** - * @var \app\Github\API + * @var API */ protected $github; /** - * @var \app\Session + * @var Session */ protected $session; - + function __construct(Web $app, API $github, Session $session) { $this->app = $app; $this->github = $github; @@ -35,7 +38,29 @@ abstract class Github implements Controller "location" => "github", "title" => "Github" ]); - $this->app->getView()->registerFunction("check", [$this, "checkRepoHook"]); + $this->app->getView()->registerFunction("check", [$github, "checkRepoHook"]); + + if (($header = $this->app->getRequest()->getHeader("Cache-Control", Header::class))) { + $params = $header->getParams(); + if (!empty($params["no-cache"])) { + $this->github->setMaxAge(0); + } elseif (!empty($params["max-age"])) { + $this->github->setMaxAge($params["max-age"]["value"]); + } + } + } + + protected function login(Account $account, Token $token, Owner $owner) { + $auth = new Cookie; + $auth->setCookie("account", $account->account->get()); + $auth->setFlags(Cookie::SECURE | Cookie::HTTPONLY); + $auth->setPath($this->app->getBaseUrl()->path); + $auth->setMaxAge(60*60*24); + $this->app->getResponse()->setCookie($auth); + + $this->github->setToken($token->token->get()); + $this->session->account = $account->account->get(); + $this->session->github = (object) $owner->export(); } protected function checkToken() { @@ -48,36 +73,4 @@ abstract class Github implements Controller ])); return false; } - - /** - * Check if the pharext webhook is set for the repo and return its id - * @param object $repo - * @return int hook id - */ - function checkRepoHook($repo) { - if ($repo->hooks) { - foreach ($repo->hooks as $hook) { - if ($hook->name === "web" && $hook->config->url === $this->github->getConfig()->hook->url) { - return $hook->id; - } - } - } - return null; - } - - function createLinkGenerator($links) { - return function($which) use($links) { - if (!isset($links[$which])) { - if ($which !== "next" || !isset($links["last"])) { - return null; - } else { - $which = "last"; - } - } - $url = new Url($links[$which], null, 0); - $qry = new QueryString($url->query); - return $qry->getInt("page", 1); - }; - } - }