streams/Code/Module/Admin.php

194 lines
5.5 KiB
PHP
Raw Normal View History

<?php
2021-12-03 03:01:39 +00:00
/**
2022-02-16 04:08:28 +00:00
* @file Code/Module/Admin.php
* @brief Hubzilla's admin controller.
*
* Controller for the /admin/ area.
*/
2022-02-16 04:08:28 +00:00
namespace Code\Module;
2022-02-16 04:08:28 +00:00
use Code\Web\Controller;
use Code\Web\SubModule;
use Code\Lib\Config;
use Code\Lib\Navbar;
use Code\Lib\Addon;
use Code\Render\Theme;
2022-02-12 20:43:29 +00:00
2022-09-02 23:45:58 +00:00
/**
* @brief Admin area.
*
*/
2021-12-02 23:02:31 +00:00
class Admin extends Controller
{
private $sm = null;
public function __construct()
{
$this->sm = new SubModule();
}
public function init()
{
logger('admin_init', LOGGER_DEBUG);
if (!is_site_admin()) {
logger('admin denied.');
return;
}
if (argc() > 1) {
$this->sm->call('init');
}
2022-10-10 11:07:54 +00:00
if ($_GET['upgrade']) {
$cmd = 'util/udall';
if (is_writable('upgrade.log') || is_writable('.')) {
$cmd .= ' > upgrade.log';
}
exec($cmd);
2022-10-10 11:07:54 +00:00
goaway(z_root() . '/admin');
}
2021-12-02 23:02:31 +00:00
}
public function post()
{
logger('admin_post', LOGGER_DEBUG);
if (!is_site_admin()) {
logger('admin denied.');
return;
}
if (argc() > 1) {
$this->sm->call('post');
}
// goaway(z_root() . '/admin' );
}
/**
* @return string
*/
public function get()
{
logger('admin_content', LOGGER_DEBUG);
if (!is_site_admin()) {
logger('admin denied.');
return login(false);
}
/*
* Page content
*/
2022-01-25 04:16:38 +00:00
Navbar::set_selected('Admin');
2021-12-02 23:02:31 +00:00
$o = '';
if (argc() > 1) {
$o = $this->sm->call('get');
if ($o === false) {
notice(t('Item not found.'));
}
} else {
$o = $this->admin_page_summary();
}
if (is_ajax()) {
echo $o;
killme();
} else {
return $o;
}
2022-09-04 00:20:42 +00:00
return '';
2021-12-02 23:02:31 +00:00
}
/**
* @brief Returns content for Admin Summary Page.
*
* @return string HTML from parsed admin_summary.tpl
*/
public function admin_page_summary()
{
// list total user accounts, expirations etc.
$accounts = [];
2021-12-03 03:01:39 +00:00
$r = q(
"SELECT COUNT(CASE WHEN account_id > 0 THEN 1 ELSE NULL END) AS total, COUNT(CASE WHEN account_expires > %s THEN 1 ELSE NULL END) AS expiring, COUNT(CASE WHEN account_expires < %s AND account_expires > '%s' THEN 1 ELSE NULL END) AS expired, COUNT(CASE WHEN (account_flags & %d)>0 THEN 1 ELSE NULL END) AS blocked FROM account",
2021-12-02 23:02:31 +00:00
db_utcnow(),
db_utcnow(),
dbesc(NULL_DATE),
intval(ACCOUNT_BLOCKED)
);
if ($r) {
$accounts['total'] = ['label' => t('Accounts'), 'val' => $r[0]['total']];
$accounts['blocked'] = ['label' => t('Blocked accounts'), 'val' => $r[0]['blocked']];
$accounts['expired'] = ['label' => t('Expired accounts'), 'val' => $r[0]['expired']];
$accounts['expiring'] = ['label' => t('Expiring accounts'), 'val' => $r[0]['expiring']];
}
// pending registrations
2021-12-03 03:01:39 +00:00
$pdg = q(
"SELECT account.*, register.hash from account left join register on account_id = register.uid where (account_flags & %d ) > 0 ",
2021-12-02 23:02:31 +00:00
intval(ACCOUNT_PENDING)
);
$pending = (($pdg) ? count($pdg) : 0);
// available channels, primary and clones
$channels = [];
$r = q("SELECT COUNT(*) AS total, COUNT(CASE WHEN channel_primary = 1 THEN 1 ELSE NULL END) AS main, COUNT(CASE WHEN channel_primary = 0 THEN 1 ELSE NULL END) AS clones FROM channel WHERE channel_removed = 0 and channel_system = 0");
if ($r) {
$channels['total'] = ['label' => t('Channels'), 'val' => $r[0]['total']];
$channels['main'] = ['label' => t('Primary'), 'val' => $r[0]['main']];
$channels['clones'] = ['label' => t('Clones'), 'val' => $r[0]['clones']];
}
// We can do better, but this is a quick queue status
$r = q("SELECT COUNT(outq_delivered) AS total FROM outq WHERE outq_delivered = 0");
$queue = (($r) ? $r[0]['total'] : 0);
$queues = ['label' => t('Message queues'), 'queue' => $queue];
$plugins = Addon::list_installed();
2021-12-02 23:02:31 +00:00
// Could be extended to provide also other alerts to the admin
$alertmsg = '';
2022-09-02 23:45:58 +00:00
$vrelease = get_repository_version('release');
$vdev = get_repository_version('dev');
2024-01-08 20:58:16 +00:00
$upgrade = ((version_compare(STD_VERSION, $vrelease) < 0) ? t('Software updates are available.') : '');
2022-10-10 11:07:54 +00:00
$git_cmd = exec('which git');
2022-02-12 20:43:29 +00:00
$t = Theme::get_template('admin_summary.tpl');
2021-12-02 23:02:31 +00:00
return replace_macros($t, [
'$title' => t('Administration'),
'$page' => t('Summary'),
'$adminalertmsg' => $alertmsg,
'$queues' => $queues,
'$accounts' => [t('Registered accounts'), $accounts],
'$pending' => [t('Pending registrations'), $pending],
'$channels' => [t('Registered channels'), $channels],
'$plugins' => (($plugins) ? [t('Active addons'), $plugins] : EMPTY_STR),
'$version' => [t('Version'), STD_VERSION],
'$vmaster' => [t('Repository version (release)'), $vrelease],
'$vdev' => [t('Repository version (dev)'), $vdev],
'$upgrade' => $upgrade,
2022-10-10 11:07:54 +00:00
'$update' => t('Update'),
'$can_upgrade' => is_dir('.git') && $git_cmd,
2021-12-02 23:02:31 +00:00
'$build' => Config::Get('system', 'db_version')
]);
}
}