Add oEmbed support
authorChristian Weiske <cweiske@cweiske.de>
Mon, 2 Feb 2015 20:34:22 +0000 (21:34 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 2 Feb 2015 20:34:22 +0000 (21:34 +0100)
README.rst
data/templates/display.htm
data/templates/oembed.htm [new file with mode: 0644]
src/phorkie/Repository.php
www/oembed.php [new file with mode: 0644]
www/www-header.php

index 889f474f4cfe88065a20d43a529e25dcd65194c5..fc97dd7e28dbe997172912fb8c8bd772186feb23 100644 (file)
@@ -22,7 +22,7 @@ Features
   - add new files
   - delete existing files
   - replace file with upload
-- embedding of pastes in your blog
+- embedding of pastes in your blog via oEmbed
 - multiple files in one paste
   - option to edit single files in a multi-file paste
 - syntax highlighting with GeSHi
index 0e5f0366f7bbc447004490c92002ea968c7c50d8..16e1b0871cfe5476abdf6f9259c0979cf848af73 100644 (file)
@@ -3,6 +3,8 @@
 
 {% block meta %}
 <link rel="meta" title="DOAP" type="application/rdf+xml" href="{{repo.getLink('doap')}}"/>
+  <link rel="alternate" title="oEmbed" type="application/json+oembed" href="{{repo.getLink('oembed-json', null, true)}}" />
+  <link rel="alternate" title="oEmbed" type="application/xml+oembed" href="{{repo.getLink('oembed-xml', null, true)}}" />
 {% if repo.getCloneURL(true) %}
   <link rel="vcs-git" href="{{repo.getCloneURL(true)}}" title="{{repo.getTitle}}"/>
 {% endif %}
diff --git a/data/templates/oembed.htm b/data/templates/oembed.htm
new file mode 100644 (file)
index 0000000..1a1eb7a
--- /dev/null
@@ -0,0 +1,7 @@
+<!-- embedding all files of {{repo.getLink('display', null, true)}} -->
+<link rel="stylesheet" href="{{htmlhelper.fullUrl('css/embed.css')}}"/>
+<div class="phork" id="{{repo.id}}">
+{% for file in repo.getFiles %}
+    {% include 'embed-part-file.htm' %}
+{% endfor %}
+</div>
index f9b248dcf91ea2b1c5a8bf572879bbd1a0f5b85e..8cf5ff62d2fff510a50b0ad0abc0fa8389e0bb45 100644 (file)
@@ -337,6 +337,12 @@ class Repository
             $link = $this->id . '/delete/confirm';
         } else if ($type == 'embed') {
             $link = $this->id . '/embed';
+        } else if ($type == 'oembed-json') {
+            $link = 'oembed.php?format=json&url='
+                . urlencode($this->getLink('display', null, true));
+        } else if ($type == 'oembed-xml') {
+            $link = 'oembed.php?format=xml&url='
+                . urlencode($this->getLink('display', null, true));
         } else if ($type == 'remotefork') {
             return 'web+fork:' . $this->getLink('display', null, true);
         } else if ($type == 'revision') {
diff --git a/www/oembed.php b/www/oembed.php
new file mode 100644 (file)
index 0000000..8672658
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+namespace phorkie;
+/**
+ * Embed a paste via oEmbed
+ *
+ * @link http://www.oembed.com/
+ */
+$reqWritePermissions = false;
+require_once 'www-header.php';
+
+if (!isset($_GET['url'])) {
+    header('HTTP/1.0 400 Bad Request');
+    echo "url parameter missing\n";
+    exit(1);
+}
+
+if (!isset($_GET['format'])) {
+    $format = 'json';
+} else if ($_GET['format'] != 'json' && $_GET['format'] != 'xml') {
+    header('HTTP/1.0 400 Bad Request');
+    echo "Invalid format parameter\n";
+    exit(1);
+} else {
+    $format = $_GET['format'];
+}
+
+if (!isset($_GET['maxwidth'])) {
+    $maxWidth = 900;
+} else {
+    $maxWidth = (int) $_GET['maxwidth'];
+    if ($maxWidth <= 100) {
+        header('HTTP/1.0 400 Bad Request');
+        echo "maxwidth parameter too small\n";
+        exit(1);
+    }
+}
+
+if (!isset($_GET['maxheight'])) {
+    $maxHeight = 900;
+} else {
+    $maxHeight = (int) $_GET['maxheight'];
+    if ($maxHeight <= 100) {
+        header('HTTP/1.0 400 Bad Request');
+        echo "maxheight parameter too small\n";
+        exit(1);
+    }
+}
+
+
+$parts = explode('/', $_GET['url']);
+$id = end($parts);
+
+$repo = new Repository();
+$repo->loadById($id);
+
+if ($format == 'json') {
+    $data = new \stdClass();
+} else {
+    $data = new \SimpleXMLElement(
+        '<?xml version="1.0" encoding="utf-8" standalone="yes"?>'
+        . '<oembed/>'
+    );
+}
+$data->type = 'rich';
+$data->version = '1.0';
+
+$data->provider_name = 'phorkie';
+$data->provider_url = Tools::fullUrl();
+
+$data->title = $repo->getTitle();
+$author = $repo->getOwner();
+$data->author_name = $author['name'];
+$data->cache_age = 86400;
+
+$data->width  = $maxWidth;
+$data->height = $maxHeight;
+
+$data->html = render('oembed', array('repo' => $repo), true);
+
+if ($format == 'json') {
+    header('Content-type: application/json');
+    echo json_encode($data) . "\n";
+} else {
+    header('Content-type: text/xml');
+    echo $data->asXML();;
+}
+?>
index 833fb8a56479a461382dba5983cfe6cfc9495551..859d797a094c45486b3edcb07ab3571bbe7ced78 100644 (file)
@@ -91,7 +91,7 @@ if (!isset($noSecurityCheck) || $noSecurityCheck !== true) {
     require __DIR__ . '/www-security.php';
 }
 
-function render($tplname, $vars = array())
+function render($tplname, $vars = array(), $return = false)
 {
     $vars['baseurl'] = '/';
     if (!empty($GLOBALS['phorkie']['cfg']['baseurl'])) {
@@ -117,8 +117,14 @@ function render($tplname, $vars = array())
     $vars['suggestSetupCheck'] = $GLOBALS['phorkie']['suggestSetupCheck'];
 
     $template = $GLOBALS['twig']->loadTemplate($tplname . '.htm');
-    echo $template->render($vars);
+
+    if ($return) {
+        return $template->render($vars);
+    } else {
+        echo $template->render($vars);
+    }
 }
+
 function redirect($target)
 {
     header('Location: ' . $target);