streams/Code/Module/Manage.php

231 lines
9.3 KiB
PHP
Raw Normal View History

2016-04-19 03:38:38 +00:00
<?php
2021-12-03 03:01:39 +00:00
2022-02-16 04:08:28 +00:00
namespace Code\Module;
2016-04-19 03:38:38 +00:00
2019-05-23 23:37:56 +00:00
use App;
2022-02-16 04:08:28 +00:00
use Code\Web\Controller;
use Code\Lib\PConfig;
use Code\Lib\ServiceClass;
use Code\Lib\Channel;
use Code\Lib\Navbar;
use Code\Render\Theme;
2022-02-12 20:43:29 +00:00
2022-01-25 01:26:12 +00:00
2019-05-23 23:37:56 +00:00
require_once('include/security.php');
2021-12-02 23:02:31 +00:00
class Manage extends Controller
{
public function get()
{
if ((!get_account_id()) || ($_SESSION['delegate'])) {
notice(t('Permission denied.') . EOL);
2022-09-04 00:01:52 +00:00
return '';
2021-12-02 23:02:31 +00:00
}
2023-04-27 11:06:01 +00:00
$channel = App::get_channel();
2021-12-02 23:02:31 +00:00
2022-01-25 04:16:38 +00:00
Navbar::set_selected('Manage');
2021-12-02 23:02:31 +00:00
$change_channel = ((argc() > 1) ? intval(argv(1)) : 0);
if (argc() > 2) {
if (argv(2) === 'default') {
2021-12-03 03:01:39 +00:00
$r = q(
"select channel_id from channel where channel_id = %d and channel_account_id = %d limit 1",
2021-12-02 23:02:31 +00:00
intval($change_channel),
intval(get_account_id())
);
if ($r) {
2021-12-03 03:01:39 +00:00
q(
"update account set account_default_channel = %d where account_id = %d",
2021-12-02 23:02:31 +00:00
intval($change_channel),
intval(get_account_id())
);
}
goaway(z_root() . '/manage');
} elseif (argv(2) === 'menu') {
2022-09-04 01:35:50 +00:00
$state = intval(PConfig::Get($change_channel, 'system', 'include_in_menu', 0));
PConfig::Set($change_channel, 'system', 'include_in_menu', 1 - $state);
2021-12-02 23:02:31 +00:00
goaway(z_root() . '/manage');
}
}
if ($change_channel) {
$r = change_channel($change_channel);
if ((argc() > 2) && !(argv(2) === 'default')) {
goaway(z_root() . '/' . implode('/', array_slice(App::$argv, 2))); // Go to whatever is after /manage/, but with the new channel
} elseif ($r && $r['channel_startpage']) {
goaway(z_root() . '/' . $r['channel_startpage']); // If nothing extra is specified, go to the default page
}
goaway(z_root());
}
$channels = null;
2021-12-03 03:01:39 +00:00
$r = q(
"select channel.*, xchan.* from channel left join xchan on channel.channel_hash = xchan.xchan_hash where channel.channel_account_id = %d and channel_removed = 0 order by channel_name ",
2021-12-02 23:02:31 +00:00
intval(get_account_id())
);
$account = App::get_account();
if ($r && count($r)) {
2022-01-25 01:26:12 +00:00
$channels = ((is_site_admin()) ? array_merge([Channel::get_system()], $r) : $r);
2021-12-02 23:02:31 +00:00
for ($x = 0; $x < count($channels); $x++) {
$channels[$x]['link'] = 'manage/' . intval($channels[$x]['channel_id']);
2022-09-04 01:35:50 +00:00
$channels[$x]['include_in_menu'] = intval(PConfig::Get($channels[$x]['channel_id'], 'system', 'include_in_menu', 0));
2021-12-02 23:02:31 +00:00
$channels[$x]['default'] = (($channels[$x]['channel_id'] == $account['account_default_channel']) ? "1" : '');
$channels[$x]['default_links'] = '1';
$channels[$x]['collections_label'] = t('Collection');
$channels[$x]['forum_label'] = t('Group');
$channels[$x]['msg_system'] = t('System | Site Channel');
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
$c = q(
"SELECT id, item_wall FROM item
WHERE item_unseen = 1 and uid = %d " . item_normal(),
2021-12-02 23:02:31 +00:00
intval($channels[$x]['channel_id'])
);
if ($c) {
foreach ($c as $it) {
if (intval($it['item_wall'])) {
$channels[$x]['home']++;
} else {
$channels[$x]['network']++;
}
}
}
2021-12-03 03:01:39 +00:00
$intr = q(
"SELECT COUNT(abook.abook_id) AS total FROM abook left join xchan on abook.abook_xchan = xchan.xchan_hash where abook_channel = %d and abook_pending = 1 and abook_self = 0 and abook_ignored = 0 and xchan_deleted = 0 and xchan_orphan = 0 ",
2021-12-02 23:02:31 +00:00
intval($channels[$x]['channel_id'])
);
if ($intr) {
$channels[$x]['intros'] = intval($intr[0]['total']);
}
2021-12-03 03:01:39 +00:00
$events = q(
"SELECT etype, dtstart, adjust FROM event
WHERE event.uid = %d AND dtstart < '%s' AND dtstart > '%s' and dismissed = 0
ORDER BY dtstart ASC ",
2021-12-02 23:02:31 +00:00
intval($channels[$x]['channel_id']),
dbesc(datetime_convert('UTC', date_default_timezone_get(), 'now + 7 days')),
dbesc(datetime_convert('UTC', date_default_timezone_get(), 'now - 1 days'))
);
if ($events) {
$channels[$x]['all_events'] = count($events);
if ($channels[$x]['all_events']) {
$str_now = datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y-m-d');
foreach ($events as $e) {
$bd = false;
if ($e['etype'] === 'birthday') {
$channels[$x]['birthdays']++;
$bd = true;
} else {
$channels[$x]['events']++;
}
if (datetime_convert('UTC', ((intval($e['adjust'])) ? date_default_timezone_get() : 'UTC'), $e['dtstart'], 'Y-m-d') === $str_now) {
$channels[$x]['all_events_today']++;
if ($bd) {
$channels[$x]['birthdays_today']++;
} else {
$channels[$x]['events_today']++;
}
}
}
}
}
}
}
2021-12-03 03:01:39 +00:00
$r = q(
"select count(channel_id) as total from channel where channel_account_id = %d and channel_removed = 0",
2021-12-02 23:02:31 +00:00
intval(get_account_id())
);
2022-01-25 01:26:12 +00:00
$limit = ServiceClass::account_fetch(get_account_id(), 'total_identities');
2021-12-02 23:02:31 +00:00
if ($limit !== false) {
$channel_usage_message = sprintf(t("You have created %1$.0f of %2$.0f allowed channels."), $r[0]['total'], $limit);
} else {
$channel_usage_message = '';
}
$create = ['new_channel', t('Create a new channel'), t('Create New')];
$delegates = null;
if (local_channel()) {
2021-12-03 03:01:39 +00:00
$delegates = q(
"select * from abook left join xchan on abook_xchan = xchan_hash where
2018-05-26 22:12:02 +00:00
abook_channel = %d and abook_xchan in ( select xchan from abconfig where chan = %d and cat = 'system' and k = 'their_perms' and v like '%s' )",
2021-12-02 23:02:31 +00:00
intval(local_channel()),
intval(local_channel()),
dbesc('%delegate%')
);
2023-04-27 11:06:01 +00:00
$links = q("select * from linkid where ident = '%s' and sigtype = %s",
dbesc($channel['channel_hash']),
intval(IDLINK_RELME)
);
$linkid_str = ids_to_querystr($links,'link', true);
if ($linkid_str) {
2023-05-02 05:28:35 +00:00
$linkedIdentities = q("select * from xchan where (xchan_hash in ($linkid_str) and xchan_network = 'activitypub')
or (xchan_url in ($linkid_str) and xchan_network in ('zot6','nomad')) ");
2023-04-27 11:06:01 +00:00
}
2021-12-02 23:02:31 +00:00
}
if ($delegates) {
for ($x = 0; $x < count($delegates); $x++) {
$delegates[$x]['link'] = 'magic?f=&bdest=' . bin2hex($delegates[$x]['xchan_url'])
. '&delegate=' . urlencode($delegates[$x]['xchan_addr']);
$delegates[$x]['channel_name'] = $delegates[$x]['xchan_name'];
$delegates[$x]['delegate'] = 1;
$delegates[$x]['collections_label'] = t('Collection');
$delegates[$x]['forum_label'] = t('Group');
}
} else {
$delegates = null;
}
2023-05-02 05:28:35 +00:00
if ($linkedIdentities) {
for ($x = 0; $x < count($linkedIdentities); $x++) {
$linkedIdentities[$x]['link'] = zid($linkedIdentities[$x]['xchan_url']);
$linkedIdentities[$x]['channel_name'] = $linkedIdentities[$x]['xchan_name'];
$linkedIdentities[$x]['delegate'] = 2;
$linkedIdentities[$x]['collections_label'] = t('Collection');
}
} else {
$linkedIdentities = null;
}
2022-02-12 20:43:29 +00:00
return replace_macros(Theme::get_template('channels.tpl'), [
2021-12-02 23:02:31 +00:00
'$header' => t('Channels'),
'$msg_selected' => t('Current Channel'),
2023-04-27 11:06:01 +00:00
'$msg_linked' => t('Linked Identities'),
2021-12-02 23:02:31 +00:00
'$selected' => local_channel(),
'$desc' => t('Switch to one of your channels by selecting it.'),
'$msg_default' => t('Default Login Channel'),
'$msg_make_default' => t('Make Default'),
'$msg_include' => t('Add to menu'),
'$msg_no_include' => t('Add to menu'),
'$create' => $create,
'$all_channels' => $channels,
'$mail_format' => t('%d new messages'),
'$intros_format' => t('%d new introductions'),
'$channel_usage_message' => $channel_usage_message,
2023-05-02 05:28:35 +00:00
'$remote_desc' => t('Linked Identity'),
2021-12-02 23:02:31 +00:00
'$delegated_desc' => t('Delegated Channel'),
2023-04-27 11:06:01 +00:00
'$delegates' => $delegates,
'$links' => $linkedIdentities,
2021-12-02 23:02:31 +00:00
]);
}
2016-04-19 03:38:38 +00:00
}