Snippets

Jelmer van der Linde oEmbed provider

Created by Jelmer van der Linde
<?php

/**
 * This little script implements the [oembed format](https://oembed.com) to
 * make it easy for us to make pages and viewers we create embeddable in
 * Wordpress.
 * Just paste the following code in the head of the HTML to allow it to be
 * embedded:
 *
 *     <link rel="alternate" type="application/json+oembed"
 *           href="https://server1.geodienst.xyz/oembed.php"
 *           title="Straatnamen Groningen oEmbed Profile">
 *
 * Made by Jelmer van der Linde, 2018
 */

function abort($message, $code = 500) {
	header('Status: 500 Internal Server Error');
	die($message);
}

function get_page_title($url) {
	$html = file_get_contents($url);

	if (!preg_match('{<title>(.+?)</title>}i', $html, $match))
		return null;

	return html_entity_decode($match[1]);
}

function get_iframe_html($url, $width = null, $height = null)
{
	$attributes = [
		'sandbox' => 'allow-scripts',
		'security' => 'restricted',
		'src' => $url,
		'frameborder' => 0,
		'marginwidth' => 0,
		'marginheight' => 0
	];

	if ($width)
		$attributes['width'] = $width;

	if ($height)
		$attributes['height'] = $height;

	$html_attributes = '';

	foreach ($attributes as $name => $value)
		$html_attributes .= sprintf(' %s="%s"', $name, htmlspecialchars($value, ENT_QUOTES));

	return sprintf('<iframe%s></iframe>', $html_attributes);
}

function main() {
	if (!isset($_GET['url']))
		abort('url parameter missing');

	$title = get_page_title($_GET['url']);

	$html = get_iframe_html($_GET['url'],
		isset($_GET['maxwidth']) ? (int) $_GET['maxwidth'] : null,
		isset($_GET['maxheight']) ? (int) $_GET['maxheight'] : null);

	header('Content-Type: application/json');
	echo json_encode([
		'version' => '1.0',
		'type' => 'iframe',
		'provider_name' => 'Geodienst oEmbed provider',
		'provider_url' => 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'],
		'author_name' => 'Geodienst Rijksuniversiteit Groningen',
		'author_url' => 'https://www.geodienst.xyz/',
		'title' => $title,
		'type' => 'rich',
		'html' => $html
	]);
}

main();

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.