Move off sourceforge
[phorkie.git] / www / oembed.php
1 <?php
2 namespace phorkie;
3 /**
4  * Embed a paste via oEmbed
5  *
6  * @link http://www.oembed.com/
7  */
8 $reqWritePermissions = false;
9 require_once 'www-header.php';
10
11 if (!isset($_GET['url'])) {
12     header('HTTP/1.0 400 Bad Request');
13     echo "url parameter missing\n";
14     exit(1);
15 }
16
17 if (!isset($_GET['format'])) {
18     $format = 'json';
19 } else if ($_GET['format'] != 'json' && $_GET['format'] != 'xml') {
20     header('HTTP/1.0 400 Bad Request');
21     echo "Invalid format parameter\n";
22     exit(1);
23 } else {
24     $format = $_GET['format'];
25 }
26
27 if (!isset($_GET['maxwidth'])) {
28     $maxWidth = 900;
29 } else {
30     $maxWidth = (int) $_GET['maxwidth'];
31     if ($maxWidth <= 100) {
32         header('HTTP/1.0 400 Bad Request');
33         echo "maxwidth parameter too small\n";
34         exit(1);
35     }
36 }
37
38 if (!isset($_GET['maxheight'])) {
39     $maxHeight = 900;
40 } else {
41     $maxHeight = (int) $_GET['maxheight'];
42     if ($maxHeight <= 100) {
43         header('HTTP/1.0 400 Bad Request');
44         echo "maxheight parameter too small\n";
45         exit(1);
46     }
47 }
48
49
50 $parts = explode('/', $_GET['url']);
51 $id = end($parts);
52
53 $repo = new Repository();
54 $repo->loadById($id);
55
56 if ($format == 'json') {
57     $data = new \stdClass();
58 } else {
59     $data = new \SimpleXMLElement(
60         '<?xml version="1.0" encoding="utf-8" standalone="yes"?>'
61         . '<oembed/>'
62     );
63 }
64 $data->type = 'rich';
65 $data->version = '1.0';
66
67 $data->provider_name = 'phorkie';
68 $data->provider_url = Tools::fullUrl();
69
70 $data->title = $repo->getTitle();
71 $author = $repo->getOwner();
72 $data->author_name = $author['name'];
73 $data->cache_age = 86400;
74
75 $data->width  = $maxWidth;
76 $data->height = $maxHeight;
77
78 $data->html = render('oembed', array('repo' => $repo), true);
79
80 if ($format == 'json') {
81     header('Content-type: application/json');
82     echo json_encode($data) . "\n";
83 } else {
84     header('Content-type: text/xml');
85     echo $data->asXML();;
86 }
87 ?>