streams/Code/Module/Layouts.php

220 lines
6.7 KiB
PHP
Raw Normal View History

2016-04-19 03:38:38 +00:00
<?php
2022-02-16 04:08:28 +00:00
namespace Code\Module;
2019-04-17 23:54:06 +00:00
use App;
2022-02-16 04:08:28 +00:00
use Code\Web\Controller;
use Code\Lib\Libprofile;
use Code\Lib\Channel;
use Code\Render\Theme;
2022-02-12 20:43:29 +00:00
2022-01-25 01:26:12 +00:00
2019-04-17 23:54:06 +00:00
2016-04-19 03:38:38 +00:00
require_once('include/conversation.php');
2021-12-02 23:02:31 +00:00
class Layouts extends Controller
{
2016-04-19 03:38:38 +00:00
2021-12-02 23:02:31 +00:00
public function init()
{
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
if (argc() > 1 && argv(1) === 'sys' && is_site_admin()) {
2022-01-25 01:26:12 +00:00
$sys = Channel::get_system();
2021-12-02 23:02:31 +00:00
if ($sys && intval($sys['channel_id'])) {
App::$is_sys = true;
}
}
2016-04-30 20:26:45 +00:00
2021-12-03 03:01:39 +00:00
if (argc() > 1) {
2021-12-02 23:02:31 +00:00
$which = argv(1);
2021-12-03 03:01:39 +00:00
} else {
2021-12-02 23:02:31 +00:00
return;
2021-12-03 03:01:39 +00:00
}
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
Libprofile::load($which);
}
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
public function get()
{
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
if (!App::$profile) {
notice(t('Requested profile is not available.') . EOL);
App::$error = 404;
2022-09-04 01:35:50 +00:00
return '';
2021-12-02 23:02:31 +00:00
}
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
$which = argv(1);
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
$_SESSION['return_url'] = App::$query_string;
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
$uid = local_channel();
$owner = 0;
$channel = null;
$observer = App::get_observer();
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
$channel = App::get_channel();
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
if (App::$is_sys && is_site_admin()) {
2022-01-25 01:26:12 +00:00
$sys = Channel::get_system();
2021-12-02 23:02:31 +00:00
if ($sys && intval($sys['channel_id'])) {
$uid = $owner = intval($sys['channel_id']);
$channel = $sys;
$observer = $sys;
}
}
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
if (!$owner) {
// Figure out who the page owner is.
2021-12-03 03:01:39 +00:00
$r = q(
"select channel_id from channel where channel_address = '%s'",
2021-12-02 23:02:31 +00:00
dbesc($which)
);
if ($r) {
$owner = intval($r[0]['channel_id']);
}
}
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
$ob_hash = (($observer) ? $observer['xchan_hash'] : '');
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
$perms = get_all_perms($owner, $ob_hash);
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
if (!$perms['write_pages']) {
notice(t('Permission denied.') . EOL);
2022-09-04 01:35:50 +00:00
return '';
2021-12-02 23:02:31 +00:00
}
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
// Block design features from visitors
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
if ((!$uid) || ($uid != $owner)) {
notice(t('Permission denied.') . EOL);
2022-09-04 01:35:50 +00:00
return '';
2021-12-02 23:02:31 +00:00
}
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
// Get the observer, check their permissions
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
$ob_hash = (($observer) ? $observer['xchan_hash'] : '');
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
$perms = get_all_perms($owner, $ob_hash);
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
if (!$perms['write_pages']) {
notice(t('Permission denied.') . EOL);
2022-09-04 01:35:50 +00:00
return '';
2021-12-02 23:02:31 +00:00
}
2016-04-30 20:26:45 +00:00
2021-12-02 23:02:31 +00:00
// This feature is not exposed in redbasic ui since it is not clear why one would want to
// download a json encoded pdl file - we dont have a possibility to import it.
// Use the buildin share/install feature instead.
2021-12-02 23:02:31 +00:00
if ((argc() > 3) && (argv(2) === 'share') && (argv(3))) {
2021-12-03 03:01:39 +00:00
$r = q(
"select iconfig.v, iconfig.k, mimetype, title, body from iconfig
left join item on item.id = iconfig.iid
where uid = %d and mid = '%s' and iconfig.cat = 'system' and iconfig.k = 'PDL' order by iconfig.v asc",
2021-12-02 23:02:31 +00:00
intval($owner),
dbesc(argv(3))
);
if ($r) {
header('Content-type: application/x-hubzilla-layout');
header('Content-Disposition: attachment; filename="' . $r[0]['sid'] . '.pdl"');
echo json_encode($r);
killme();
}
}
// Create a status editor (for now - we'll need a WYSIWYG eventually) to create pages
// Nickname is set to the observers xchan, and profile_uid to the owners.
// This lets you post pages at other people's channels.
2022-09-04 01:35:50 +00:00
$x = [
2021-12-02 23:02:31 +00:00
'webpage' => ITEM_TYPE_PDL,
'is_owner' => true,
'nickname' => App::$profile['channel_address'],
'showacl' => false,
'hide_voting' => true,
'hide_future' => true,
'hide_expire' => true,
'hide_location' => true,
'hide_weblink' => true,
'hide_attach' => true,
'hide_preview' => true,
'disable_comments' => true,
'ptlabel' => t('Layout Name'),
'profile_uid' => intval($owner),
'expanded' => true,
'placeholdertitle' => t('Layout Description (Optional)'),
'novoting' => true,
'bbco_autocomplete' => 'comanche'
2022-09-04 01:35:50 +00:00
];
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
if ($_REQUEST['title']) {
2021-12-02 23:02:31 +00:00
$x['title'] = $_REQUEST['title'];
2021-12-03 03:01:39 +00:00
}
if ($_REQUEST['body']) {
2021-12-02 23:02:31 +00:00
$x['body'] = $_REQUEST['body'];
2021-12-03 03:01:39 +00:00
}
if ($_REQUEST['pagetitle']) {
2021-12-02 23:02:31 +00:00
$x['pagetitle'] = $_REQUEST['pagetitle'];
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$editor = status_editor($x);
2021-12-03 03:01:39 +00:00
$r = q(
"select iconfig.iid, iconfig.v, mid, title, body, mimetype, created, edited, item_type from iconfig
left join item on iconfig.iid = item.id
where uid = %d and iconfig.cat = 'system' and iconfig.k = 'PDL' and item_type = %d order by item.created desc",
2021-12-02 23:02:31 +00:00
intval($owner),
intval(ITEM_TYPE_PDL)
);
$pages = null;
if ($r) {
$pages = [];
foreach ($r as $rr) {
2022-09-04 01:35:50 +00:00
$element_arr = [
2021-12-02 23:02:31 +00:00
'type' => 'layout',
'title' => $rr['title'],
'body' => $rr['body'],
'created' => $rr['created'],
'edited' => $rr['edited'],
'mimetype' => $rr['mimetype'],
'pagetitle' => urldecode($rr['v']),
'mid' => $rr['mid']
2022-09-04 01:35:50 +00:00
];
$pages[$rr['iid']][] = [
2021-12-02 23:02:31 +00:00
'url' => $rr['iid'],
'title' => urldecode($rr['v']),
'descr' => $rr['title'],
'mid' => $rr['mid'],
'created' => $rr['created'],
'edited' => $rr['edited'],
'bb_element' => '[element]' . base64url_encode(json_encode($element_arr)) . '[/element]'
2022-09-04 01:35:50 +00:00
];
2021-12-02 23:02:31 +00:00
}
}
//Build the base URL for edit links
$url = z_root() . '/editlayout/' . $which;
2022-09-04 01:35:50 +00:00
$o = replace_macros(Theme::get_template('layoutlist.tpl'), [
2021-12-02 23:02:31 +00:00
'$title' => t('Layouts'),
'$create' => t('Create'),
'$help' => '', // array('text' => t('Help'), 'url' => 'help/comanche', 'title' => t('Comanche page description language help')),
'$editor' => $editor,
'$baseurl' => $url,
'$name' => t('Layout Name'),
'$descr' => t('Layout Description'),
'$created' => t('Created'),
'$edited' => t('Edited'),
'$edit' => t('Edit'),
'$share' => t('Share'),
'$download' => t('Download PDL file'),
'$pages' => $pages,
'$channel' => $which,
'$view' => t('View'),
2022-09-04 01:35:50 +00:00
]);
2021-12-02 23:02:31 +00:00
return $o;
}
2016-04-19 03:38:38 +00:00
}