add oembed provider for photos

This commit is contained in:
redmatrix 2016-01-31 15:55:27 -08:00
parent 1f87fef968
commit a341c889b7
7 changed files with 110 additions and 9 deletions

View file

@ -48,7 +48,7 @@ require_once('include/AccessList.php');
define ( 'PLATFORM_NAME', 'hubzilla' );
define ( 'RED_VERSION', trim(file_get_contents('version.inc')));
define ( 'STD_VERSION', '1.1.3' );
define ( 'STD_VERSION', '1.1.4' );
define ( 'ZOT_REVISION', 1 );
define ( 'DB_UPDATE_VERSION', 1161 );

View file

@ -78,7 +78,6 @@ function oembed_fetch_url($embedurl){
else {
// try oembed autodiscovery
$redirects = 0;
$result = z_fetch_url($embedurl, false, $redirects, array('timeout' => 15, 'accept_content' => "text/*", 'novalidate' => true ));
if($result['success'])
$html_text = $result['body'];
@ -88,8 +87,8 @@ function oembed_fetch_url($embedurl){
if ($dom){
$xpath = new DOMXPath($dom);
$attr = "oembed";
$xattr = oe_build_xpath("class","oembed");
$entries = $xpath->query("//link[@type='application/json+oembed']");
foreach($entries as $e){
$href = $e->getAttributeNode("href")->nodeValue;
@ -173,6 +172,10 @@ function oembed_format_object($j){
$ret.="<br>";
}; break;
case "link": {
if($j->thumbnail_url) {
$ret = '<a href="' . $embedurl . '" ><img src="' . $j->thumbnail_url . '" alt="thumbnail" /></a><br /><br />';
}
//$ret = "<a href='".$embedurl."'>".$j->title."</a>";
}; break;
case "rich": {

View file

@ -181,7 +181,7 @@ function permissions_sql($owner_id, $remote_observer = null) {
*/
else {
$observer = (($remote_observer) ? $remote_observer : get_observer_hash());
$observer = ((! is_null($remote_observer)) ? $remote_observer : get_observer_hash());
if($observer) {
$groups = init_groups_visitor($observer);

View file

@ -289,9 +289,12 @@ if($a->module_loaded) {
*/
if(function_exists($a->module . '_init')) {
call_hooks($a->module . '_mod_init', $placeholder);
$func = $a->module . '_init';
$func($a);
$arr = array('init' => true, 'replace' => false);
call_hooks($a->module . '_mod_init', $arr);
if(! $arr['replace']) {
$func = $a->module . '_init';
$func($a);
}
}
/**
@ -333,7 +336,7 @@ if($a->module_loaded) {
if(($_SERVER['REQUEST_METHOD'] === 'POST') && (! $a->error)
&& (function_exists($a->module . '_post'))
&& (! x($_POST, 'auth-params'))) {
&& (! x($_POST, 'auth-params'))) {
call_hooks($a->module . '_mod_post', $_POST);
$func = $a->module . '_post';
$func($a);

93
mod/oep.php Normal file
View file

@ -0,0 +1,93 @@
<?php
// oembed provider
function oep_init(&$a) {
$url = $_REQUEST['url'];
if(! $url)
http_status_exit(404, 'Not found');
$maxwidth = $_REQUEST['maxwidth'];
$maxheight = $_REQUEST['maxheight'];
$format = $_REQUEST['format'];
if($format && $format !== 'json')
http_status_exit(501, 'Not implemented');
if(fnmatch('*/photos/*/album/*',$url))
$arr = oep_album_reply($_REQUEST);
elseif(fnmatch('*/photos/*',$url))
$arr = oep_photo_reply($_REQUEST);
if($arr) {
header('Content-Type: application/json+oembed');
echo json_encode($arr);
killme();
}
http_status_exit(404,'Not found');
}
function oep_album_reply() {
}
function oep_photo_reply($args) {
$ret = array();
$url = $args['url'];
$maxwidth = intval($args['maxwidth']);
$maxheight = intval($args['maxheight']);
if(preg_match('|//(.*?)/(.*?)/(.*?)/image/|',$url,$matches)) {
$chn = $matches[3];
$res = basename($url);
}
if(! ($chn && $res))
return;
$c = q("select * from channel where channel_address = '%s' limit 1",
dbesc($chn)
);
if(! $c)
return;
$sql_extra = permissions_sql($c[0]['channel_id']);
$r = q("select height, width, scale, resource_id from photo where uid = %d and resource_id = '%s' $sql_extra order by scale asc",
intval($c[0]['channel_id']),
dbesc($res)
);
if($r) {
foreach($r as $rr) {
$foundres = false;
if($maxheight && $rr['height'] > $maxheight)
continue;
if($maxwidth && $rr['width'] > $maxwidth)
continue;
$foundres = true;
break;
}
if($foundres) {
$ret['type'] = 'link';
$ret['thumbnail_url'] = z_root() . '/photo/' . '/' . $rr['resource_id'] . '-' . $rr['scale'];
$ret['thumbnail_width'] = $rr['width'];
$ret['thumbnail_height'] = $rr['height'];
}
}
return $ret;
}

View file

@ -804,6 +804,8 @@ function photos_content(&$a) {
if($datatype === 'image') {
$a->page['htmlhead'] .= "\r\n" . '<link rel="alternate" type="application/json+oembed" href="' . z_root() . '/oep?f=&url=' . urlencode(z_root() . '/' . $a->cmd) . '" title="oembed" />' . "\r\n";
// fetch image, item containing image, then comments
$ph = q("SELECT id,aid,uid,xchan,resource_id,created,edited,title,`description`,album,filename,`type`,height,width,`size`,scale,photo_usage,is_nsfw,allow_cid,allow_gid,deny_cid,deny_gid FROM `photo` WHERE `uid` = %d AND `resource_id` = '%s'

View file

@ -1 +1 @@
2016-01-29.1293H
2016-01-31.1295H