6bb5c7f05cfdc9458e867c4403bf1e4f5b7d50b4
[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->setupCheck();
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 $rep = new Repository();
29 $rep->setConfig($config);
30 try {
31     $img = $rep->getImage($options);
32     header('HTTP/1.0 302 Found');
33     header('Location: ' . $img->getUrl());
34 } catch (\Exception $e) {
35     //FIXME: handle 404s and so properly
36     header('HTTP/1.0 500 Internal Server error');
37     header('Content-type: text/plain');
38     echo $e->getMessage() . "\n";
39     exit(2);
40 }
41 ?>