15463e43359cdd3140343dcc49b9eaf758ea518a
[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->setConfig($config);
21     $options->parse($_GET);
22 } catch (\InvalidArgumentException $e) {
23     header('HTTP/1.0 400 Bad Request');
24     header('Content-type: text/plain');
25     echo $e->getMessage() . "\n";
26     exit(1);
27 }
28
29 $auth = new Authenticator();
30 try {
31     $auth->authenticate($config);
32 } catch (\Exception $e) {
33     header('HTTP/1.0 401 Unauthorized');
34     header('Content-type: text/plain');
35     echo $e->getMessage() . "\n";
36     exit(1);
37 }
38
39 $rep = new Repository();
40 $rep->setConfig($config);
41 try {
42     $img = $rep->getImage($options);
43     if ($config->redirect) {
44         header('HTTP/1.0 302 Found');
45         header('Location: ' . $img->getUrl());
46     } else {
47         header('Content-type: ' . $img->getMimeType());
48         readfile($img->getPath());
49     }
50 } catch (\Exception $e) {
51     //FIXME: handle 404s and so properly
52     header('HTTP/1.0 500 Internal Server error');
53     header('Content-type: text/plain');
54     echo $e->getMessage() . "\n";
55     exit(2);
56 }
57 ?>