Skip to content

Instantly share code, notes, and snippets.

@onigetoc
Last active July 23, 2016 06:40
Show Gist options
  • Save onigetoc/dfe7ade4debda2b8ceb77099a88e872e to your computer and use it in GitHub Desktop.
Save onigetoc/dfe7ade4debda2b8ceb77099a88e872e to your computer and use it in GitHub Desktop.
<?php
header('Content-Type: application/json; charset=utf-8');
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
include '../src/autoloader.php';
function get($name, $default = '')
{
if (!isset($_GET[$name])) {
return $default;
}
if ($name === 'url') {
if (!filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
return 'http://doNotTryToXSS.invalid';
}
}
return $_GET[$name];
}
if (get('url')){
try {
$info = Embed\Embed::create(get('url'));
} catch (Exception $exception) {
echo '[{"Message":"'.$exception->getMessage().'","error":"error"}]';
die();
}
$data = array();
$data['title'] = $info->title;
$data['description'] = $info->description;
$data['url'] = $info->url;
$data['type'] = $info->type;
$data['tags'] = $info->tags;
$data['images'] = $info->images;
$data['image'] = $info->image;
$data['imageWidth'] = $info->imageWidth;
$data['imageHeight'] = $info->imageHeight;
$data['code'] = $info->code;
$data['source'] = $info->source;
$data['width'] = $info->width;
$data['height'] = $info->height;
$data['aspectRatio'] = $info->aspectRatio;
$data['authorName'] = $info->authorName;
$data['authorUrl'] = $info->authorUrl;
$data['providerName'] = $info->providerName;
$data['providerUrl'] = $info->providerUrl;
$data['providerIcon'] = $info->providerIcon;
$data['providerIcons'] = $info->providerIcons;
$data['publishedDate'] = $info->publishedDate;
$data['publishedTime'] = $info->publishedTime;
$data['license'] = $info->license;
$data['linkedData'] = $info->linkedData;
echo json_encode($data);
}
?>
<?php
header('Content-Type: application/json; charset=utf-8');
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
include '../src/autoloader.php';
function get($name, $default = '')
{
if (!isset($_GET[$name])) {
return $default;
}
if ($name === 'url') {
if (!filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
return 'http://doNotTryToXSS.invalid';
}
}
return $_GET[$name];
}
//function getEscaped($name, $default = '')
//{
// return htmlspecialchars(get($name, $default), ENT_QUOTES, 'UTF-8');
//}
if (get('url')){
try {
$info = Embed\Embed::create(get('url'));
} catch (Exception $exception) {
echo '[{"Message":"'.$exception->getMessage().'","error":"error"}]';
die();
}
/******* OEMBED MORE SERVICES ********/
$pattern = "/\b(?:pastebin|ideone|plnkr|pastie|jsbin|snipplr)\.\b/i";
//$url = get('url');
$url = $_GET['url'];
if (preg_match ($pattern, $url )) {
$domain = get_domain($url);
switch ($domain) {
case "jsbin.com":
if (substr($url, -1) !== '/') $url = $url.'/';
$url = str_replace( 'https://', 'http://', $url );
$code = '<a class="jsbin-embed" href="' . $url . 'embed?html,css,js,output">JS Bin on jsbin.com</a><script src="http://static.jsbin.com/js/embed.min.js?3.37.0"></script>';
break;
case "snipplr.com":
$re = "/https?.*snipplr.com.*view\/(\w+).*/i";
$subst = '<div id="snipplr_embed_$1" class="snipplr_embed"><a target_"blank" href="http://snipplr.com/view/$1">View this snippet</a> on Snipplr</div><script type="text/javascript" src="http://snipplr.com/js/embed.js"></script><script type="text/javascript" src="http://snipplr.com/json/$1"></script>';
$result = preg_replace($re, $subst, $url);
$code = $result;
break;
case "ideone.com":
$re = "/https?.*ideone.com\/(\w+).*/i";
$subst = '<script src="http://ideone.com/e.js/$1" type="text/javascript" ></script>';
$result = preg_replace($re, $subst, $url);
$code = $result;
break;
case "plnkr.co":
$url = array_shift(explode('?', $url)); // remove ? everything after
$embed = str_replace( 'edit/', '', $url );
$embed = str_replace( 'plnkr', 'embed.plnkr', $url );
$code = '<iframe style="width: 100%; height: 400px" src="'.$embed.'" frameborder="0" allowfullscren="allowfullscren"></iframe>';
break;
case "pastie.org":
$url = str_replace( 'pastes/', '', $url );
$code = '<script src="'.$url.'.js"></script>';
break;
}
}else{
$code = $info->code;
}
$data = array();
$data['title'] = $info->title;
$data['description'] = $info->description;
$data['url'] = $info->url;
$data['type'] = $info->type;
$data['tags'] = $info->tags;
$data['images'] = $info->images;
$data['image'] = $info->image;
$data['imageWidth'] = $info->imageWidth;
$data['imageHeight'] = $info->imageHeight;
$data['code'] = $code;
$data['source'] = $info->source;
$data['width'] = $info->width;
$data['height'] = $info->height;
$data['aspectRatio'] = $info->aspectRatio;
$data['authorName'] = $info->authorName;
$data['authorUrl'] = $info->authorUrl;
$data['providerName'] = $info->providerName;
$data['providerUrl'] = $info->providerUrl;
$data['providerIcon'] = $info->providerIcon;
$data['providerIcons'] = $info->providerIcons;
$data['publishedDate'] = $info->publishedDate;
$data['publishedTime'] = $info->publishedTime;
$data['license'] = $info->license;
$data['linkedData'] = $info->linkedData;
echo json_encode($data);
}
/********** FUNCTIONS ***********/
/********** for future use ***********/
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
?>
{
"title": "Embedded Codeply",
"description": "How to use the Office UI Fabric with Codeply for styling your Office add-ins (the WEB kind!)",
"url": "http://www.codeply.com/view/eQ1OFP9WmX",
"type": "rich",
"tags": [
"html",
"css",
"javascript",
"editor",
"responsive design",
"RWD",
"responsive",
"angular",
"html5",
"bootstrap",
"responsive frameworks",
"front end"
],
"images": [
{
"value": "http://www.codeply.com/img/eQ1OFP9WmX",
"providers": [
"oembed",
"opengraph",
"html"
],
"width": 1366,
"height": 780,
"size": 1065480,
"mime": "image/png"
}
],
"image": "http://codesniff.com/Embed/demo/img/161975075792e49782edb8.79187889.jpg",
"imageWidth": 1366,
"imageHeight": 780,
"code": "<iframe src=\"https://www.codeply.com/embed/eQ1OFP9WmX\" scrolling=\"no\" frameborder=\"0\" height=\"500\" width=\"700\" allowtransparency=\"true\" style=\"width: 700px; overflow: hidden;\"></iframe>",
"source": null,
"width": 700,
"height": 500,
"aspectRatio": 71.429,
"authorName": "Skelly",
"authorUrl": "http://codeply.com/users/cskelly",
"providerName": "Codeply",
"providerUrl": "http://codeply.com",
"providerIcon": "http://www.codeply.com/favicon.ico",
"providerIcons": {
"1": {
"value": "http://www.codeply.com/favicon.ico",
"providers": [
"adapter"
],
"width": 300,
"height": 300,
"size": 90000,
"mime": "image/png"
},
"2": {
"value": "http://www.codeply.com/images/cp_300.png",
"providers": [
"html"
],
"width": 300,
"height": 300,
"size": 90000,
"mime": "image/png"
}
},
"publishedDate": null,
"publishedTime": null,
"license": null,
"linkedData": []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment