implement authentication
[phancap.git] / www / get.php
1 <?php
2 namespace phancap;
3 /**
4  * Get a screenshot for a website.
5  */
6 header('HTTP/1.0 500 Internal Server Error');
7
8 if (file_exists(__DIR__ . '/../src/phancap/Autoloader.php')) {
9     include_once __DIR__ . '/../src/phancap/Autoloader.php';
10     Autoloader::register();
11 } else {
12     include_once 'phancap/Autoloader.php';
13 }
14
15 $config = new Config();
16 $config->load();
17
18 $options = new Options();
19 try {
20     $options->parse($_GET);
21 } catch (\InvalidArgumentException $e) {
22     header('HTTP/1.0 400 Bad Request');
23     header('Content-type: text/plain');
24     echo $e->getMessage() . "\n";
25     exit(1);
26 }
27
28 $auth = new Authenticator();
29 try {
30     $auth->authenticate($config);
31 } catch (\Exception $e) {
32     header('HTTP/1.0 401 Unauthorized');
33     header('Content-type: text/plain');
34     echo $e->getMessage() . "\n";
35     exit(1);
36 }
37
38 $rep = new Repository();
39 $rep->setConfig($config);
40 try {
41     $img = $rep->getImage($options);
42     header('HTTP/1.0 302 Found');
43     header('Location: ' . $img->getUrl());
44 } catch (\Exception $e) {
45     //FIXME: handle 404s and so properly
46     header('HTTP/1.0 500 Internal Server error');
47     header('Content-type: text/plain');
48     echo $e->getMessage() . "\n";
49     exit(2);
50 }
51 ?>