streams/Zotlabs/Module/Embedphotos.php

214 lines
5.8 KiB
PHP
Raw Normal View History

<?php
namespace Zotlabs\Module;
2019-06-08 23:53:05 +00:00
use App;
use Zotlabs\Web\Controller;
2019-06-08 23:53:05 +00:00
require_once('include/attach.php');
require_once('include/photos.php');
2019-06-08 23:53:05 +00:00
class Embedphotos extends Controller {
/**
*
* This is the POST destination for the embedphotos button
*
*/
function post() {
if (argc() > 1 && argv(1) === 'album') {
// API: /embedphotos/album
$name = (x($_POST,'name') ? $_POST['name'] : null );
2019-06-08 23:53:05 +00:00
if (! $name) {
json_return_and_die(array('errormsg' => 'Error retrieving album', 'status' => false));
}
2019-06-08 23:53:05 +00:00
$album = $this->embedphotos_widget_album(array('channel_id' => local_channel(), 'album' => $name));
json_return_and_die(array('status' => true, 'content' => $album));
}
2019-06-08 23:53:05 +00:00
if (argc() > 1 && argv(1) === 'albumlist') {
// API: /embedphotos/albumlist
2019-06-08 23:53:05 +00:00
$album_list = $this->embedphotos_album_list();
json_return_and_die(array('status' => true, 'albumlist' => $album_list));
}
2019-06-08 23:53:05 +00:00
if (argc() > 1 && argv(1) === 'photolink') {
// API: /embedphotos/photolink
$href = (x($_POST,'href') ? $_POST['href'] : null );
2019-06-08 23:53:05 +00:00
if (! $href) {
json_return_and_die(array('errormsg' => 'Error retrieving link ' . $href, 'status' => false));
}
$resource_id = array_pop(explode("/", $href));
$x = self::photolink($resource_id);
2019-06-08 23:53:05 +00:00
if ($x) {
json_return_and_die(array('status' => true, 'photolink' => $x, 'resource_id' => $resource_id));
2019-06-08 23:53:05 +00:00
}
json_return_and_die(array('errormsg' => 'Error retrieving resource ' . $resource_id, 'status' => false));
}
}
protected static function photolink($resource) {
2019-06-08 23:53:05 +00:00
$channel = App::get_channel();
$output = EMPTY_STR;
2019-06-08 23:53:05 +00:00
if ($channel) {
$resolution = ((feature_enabled($channel['channel_id'],'large_photos')) ? 1 : 2);
$r = q("select mimetype, height, width from photo where resource_id = '%s' and $resolution = %d and uid = %d limit 1",
dbesc($resource),
intval($resolution),
intval($channel['channel_id'])
);
2019-06-08 23:53:05 +00:00
if (! $r) {
return $output;
2019-06-08 23:53:05 +00:00
}
if ($r[0]['mimetype'] === 'image/jpeg') {
$ext = '.jpg';
2019-06-08 23:53:05 +00:00
}
elseif ($r[0]['mimetype'] === 'image/png') {
$ext = '.png';
2019-06-08 23:53:05 +00:00
}
elseif ($r[0]['mimetype'] === 'image/gif') {
$ext = '.gif';
2019-06-08 23:53:05 +00:00
}
else {
$ext = EMPTY_STR;
2019-06-08 23:53:05 +00:00
}
$output = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $resource . ']' .
'[zmg=' . $r[0]['width'] . 'x' . $r[0]['height'] . ']' . z_root() . '/photo/' . $resource . '-' . $resolution . $ext . '[/zmg][/zrl]';
return $output;
}
}
/**
* Copied from include/widgets.php::widget_album() with a modification to get the profile_uid from
* the input array as in widget_item()
*
* @param array $args
* @return string with HTML
*/
2019-06-08 23:53:05 +00:00
function embedphotos_widget_album($args) {
$channel_id = 0;
2019-06-08 23:53:05 +00:00
if (array_key_exists('channel_id', $args)) {
$channel_id = $args['channel'];
$channel = channelx_by_n($channel_id);
}
if (! $channel_id) {
return '';
2019-06-08 23:53:05 +00:00
}
$owner_uid = $channel_id;
require_once('include/security.php');
$sql_extra = permissions_sql($channel_id);
2019-06-08 23:53:05 +00:00
if (! perm_is_allowed($channel_id,get_observer_hash(),'view_storage'))
return '';
2019-06-08 23:53:05 +00:00
if ($args['album']) {
$album = (($args['album'] === '/') ? '' : $args['album']);
2019-06-08 23:53:05 +00:00
}
if ($args['title']) {
$title = $args['title'];
2019-06-08 23:53:05 +00:00
}
/**
* This may return incorrect permissions if you have multiple directories of the same name.
* It is a limitation of the photo table using a name for a photo album instead of a folder hash
*/
2019-06-08 23:53:05 +00:00
if ($album) {
$x = q("select hash from attach where filename = '%s' and uid = %d limit 1",
dbesc($album),
intval($owner_uid)
);
2019-06-08 23:53:05 +00:00
if ($x) {
$y = attach_can_view_folder($owner_uid,get_observer_hash(),$x[0]['hash']);
2019-06-08 23:53:05 +00:00
if (! $y) {
return '';
2019-06-08 23:53:05 +00:00
}
}
}
$order = 'DESC';
$r = q("SELECT p.resource_id, p.id, p.filename, p.mimetype, p.imgscale, p.description, p.created FROM photo p INNER JOIN
2019-06-08 23:53:05 +00:00
(SELECT resource_id, max(imgscale) imgscale FROM photo WHERE uid = %d AND album = '%s' AND imgscale <= 4
AND photo_usage IN ( %d, %d ) $sql_extra GROUP BY resource_id) ph
ON (p.resource_id = ph.resource_id AND p.imgscale = ph.imgscale)
ORDER BY created $order",
intval($owner_uid),
dbesc($album),
intval(PHOTO_NORMAL),
intval(PHOTO_PROFILE)
);
2019-06-08 23:53:05 +00:00
$photos = [];
if ($r) {
$twist = 'rotright';
2019-06-08 23:53:05 +00:00
foreach ($r as $rr) {
if ($twist == 'rotright') {
$twist = 'rotleft';
2019-06-08 23:53:05 +00:00
}
else {
$twist = 'rotright';
2019-06-08 23:53:05 +00:00
}
$ext = $phototypes[$rr['mimetype']];
$imgalt_e = $rr['filename'];
$desc_e = $rr['description'];
2019-06-08 23:53:05 +00:00
$imagelink = (z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $rr['resource_id']
. (($_GET['order'] === 'posted') ? '?f=&order=posted' : ''));
2019-06-08 23:53:05 +00:00
$photos[] = [
'id' => $rr['id'],
'twist' => ' ' . $twist . rand(2,4),
'link' => $imagelink,
'title' => t('View Photo'),
'src' => z_root() . '/photo/' . $rr['resource_id'] . '-' . $rr['imgscale'] . '.' .$ext,
'alt' => $imgalt_e,
'desc' => $desc_e,
'ext' => $ext,
'hash' => $rr['resource_id'],
'unknown' => t('Unknown')
];
}
}
2019-06-08 23:53:05 +00:00
$o .= replace_macros(get_markup_template('photo_album.tpl'), [
'$photos' => $photos,
'$album' => (($title) ? $title : $album),
'$album_id' => rand(),
'$album_edit' => array(t('Edit Album'), $album_edit),
'$can_post' => false,
'$upload' => [ t('Upload'), z_root() . '/photos/' . $channel['channel_address'] . '/upload/' . bin2hex($album) ],
'$order' => false,
'$upload_form' => $upload_form,
'$no_fullscreen_btn' => true
2019-06-08 23:53:05 +00:00
]);
return $o;
}
2019-06-08 23:53:05 +00:00
function embedphotos_album_list() {
$p = photos_albums_list(App::get_channel(),App::get_observer());
if ($p['success']) {
return $p['albums'];
2019-06-08 23:53:05 +00:00
}
else {
return null;
}
}
}