controllers: fix base url to omit scheme for when we connect through a https gateway
[pharext/pharext.org] / app / Controller / Github.php
1 <?php
2
3 namespace app\Controller;
4
5 use app\Controller;
6 use app\Github\API;
7 use app\Model\Account;
8 use app\Model\Owner;
9 use app\Model\Token;
10 use app\Session;
11 use app\Web;
12 use http\Cookie;
13 use http\Header;
14 use http\QueryString;
15
16 abstract class Github implements Controller
17 {
18 /**
19 * @var \app\web
20 */
21 protected $app;
22
23 /**
24 * @var API
25 */
26 protected $github;
27
28 /**
29 * @var Session
30 */
31 protected $session;
32
33 function __construct(Web $app, API $github, Session $session) {
34 $this->app = $app;
35 $this->github = $github;
36 $this->session = $session;
37 $this->app->getView()->addData(compact("session") + [
38 "location" => "github",
39 "title" => "Github"
40 ]);
41 $this->app->getView()->registerFunction("check", [$github, "checkRepoHook"]);
42
43 if (($header = $this->app->getRequest()->getHeader("Cache-Control", Header::class))) {
44 $params = $header->getParams();
45 if (!empty($params["no-cache"])) {
46 $this->github->setMaxAge(0);
47 } elseif (!empty($params["max-age"])) {
48 $this->github->setMaxAge($params["max-age"]["value"]);
49 }
50 }
51 }
52
53 protected function login(Account $account, Token $token, Owner $owner) {
54 $auth = new Cookie;
55 $auth->setCookie("account", $account->account->get());
56 $auth->setFlags(Cookie::SECURE | Cookie::HTTPONLY);
57 $auth->setPath($this->app->getBaseUrl()->path);
58 $auth->setMaxAge(60*60*24);
59 $this->app->getResponse()->setCookie($auth);
60
61 $this->github->setToken($token->token->get());
62 $this->session->account = $account->account->get();
63 $this->session->github = (object) $owner->export();
64 }
65
66 protected function checkToken() {
67 if ($this->github->hasToken()) {
68 return true;
69 }
70 $this->app->redirect($this->app->getBaseUrl()->mod([
71 "scheme" => null,
72 "path" => "github/signin",
73 "query" => new QueryString(["returnto" => $this->session->current])
74 ]));
75 return false;
76 }
77 }