fix type hint (WIP)

This commit is contained in:
Jean-Christian Paul Denis 2023-05-12 09:06:14 +02:00
parent c34a8478c6
commit edbc2e5b8c
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
2 changed files with 121 additions and 110 deletions

View File

@ -274,7 +274,7 @@ class ActivityBehaviors
public static function blogUpdate(Cursor $cur, string $blog_id): void public static function blogUpdate(Cursor $cur, string $blog_id): void
{ {
$logs = [(string) dcCore::app()->auth?->getInfo('user_cn')]; $logs = [self::str(dcCore::app()->auth?->getInfo('user_cn'))];
ActivityReport::instance()->addLog('blog', 'update', $logs); ActivityReport::instance()->addLog('blog', 'update', $logs);
} }
@ -283,30 +283,28 @@ class ActivityBehaviors
if (dcCore::app()->url->type != '404') { if (dcCore::app()->url->type != '404') {
return; return;
} }
$logs = [(string) dcCore::app()->blog?->url . $_SERVER['QUERY_STRING']]; $logs = [self::str(dcCore::app()->blog?->url) . $_SERVER['QUERY_STRING']];
ActivityReport::instance()->addLog('blog', 'p404', $logs); ActivityReport::instance()->addLog('blog', 'p404', $logs);
} }
public static function postCreate(Cursor $cur, int $post_id): void public static function postCreate(Cursor $cur, int $post_id): void
{ {
$type = $cur->getField('post_type') ?? 'post'; $post_url = dcCore::app()->blog?->getPostURL('', self::str($cur->getField('post_dt')), self::str($cur->getField('post_title')), $post_id);
$post_url = dcCore::app()->blog?->getPostURL('', $cur->getField('post_dt'), $cur->getField('post_title'), $post_id);
$logs = [ $logs = [
(string) $cur->getField('post_title'), self::str($cur->getField('post_title')),
(string) dcCore::app()->auth?->getInfo('user_cn'), self::str(dcCore::app()->auth?->getInfo('user_cn')),
(string) dcCore::app()->blog?->url . dcCore::app()->url->getBase($type) . '/' . $post_url, self::str(dcCore::app()->blog?->url) . dcCore::app()->url->getBase(self::str($cur->getField('post_type'))) . '/' . $post_url,
]; ];
ActivityReport::instance()->addLog('post', 'create', $logs); ActivityReport::instance()->addLog('post', 'create', $logs);
} }
public static function postUpdate(Cursor $cur, int $post_id): void public static function postUpdate(Cursor $cur, int $post_id): void
{ {
$type = $cur->getField('post_type') ?? 'post'; $post_url = dcCore::app()->blog?->getPostURL('', self::str($cur->getField('post_dt')), self::str($cur->getField('post_title')), $post_id);
$post_url = dcCore::app()->blog?->getPostURL('', $cur->getField('post_dt'), $cur->getField('post_title'), $post_id);
$logs = [ $logs = [
(string) $cur->getField('post_title'), self::str($cur->getField('post_title')),
(string) dcCore::app()->auth?->getInfo('user_cn'), self::str(dcCore::app()->auth?->getInfo('user_cn')),
(string) dcCore::app()->blog?->url . dcCore::app()->url->getBase($type) . '/' . $post_url, self::str(dcCore::app()->blog?->url) . dcCore::app()->url->getBase(self::str($cur->getField('post_type'))) . '/' . $post_url,
]; ];
ActivityReport::instance()->addLog('post', 'update', $logs); ActivityReport::instance()->addLog('post', 'update', $logs);
} }
@ -318,8 +316,8 @@ class ActivityBehaviors
return; return;
} }
$logs = [ $logs = [
(string) $posts->f('post_title'), self::str($posts->f('post_title')),
(string) dcCore::app()->auth?->getInfo('user_cn'), self::str(dcCore::app()->auth?->getInfo('user_cn')),
]; ];
ActivityReport::instance()->addLog('post', 'delete', $logs); ActivityReport::instance()->addLog('post', 'delete', $logs);
} }
@ -349,10 +347,10 @@ class ActivityBehaviors
} }
$logs = [ $logs = [
(string) $cur->getField('comment_author'), self::str($cur->getField('comment_author')),
(string) $posts->f('post_title'), self::str($posts->f('post_title')),
(string) dcCore::app()->blog?->url . dcCore::app()->url->getBase((string) $posts->f('post_type')) . self::str(dcCore::app()->blog?->url) . dcCore::app()->url->getBase(self::str($posts->f('post_type'))) .
'/' . $posts->f('post_url') . '#c' . $cur->getField('comment_id'), '/' . self::str($posts->f('post_url')) . '#c' . self::str($cur->getField('comment_id')),
]; ];
ActivityReport::instance()->addLog('comment', 'create', $logs); ActivityReport::instance()->addLog('comment', 'create', $logs);
} }
@ -367,10 +365,10 @@ class ActivityBehaviors
} }
$logs = [ $logs = [
(string) dcCore::app()->auth?->getInfo('user_cn'), self::str(dcCore::app()->auth?->getInfo('user_cn')),
(string) $posts->f('post_title'), self::str($posts->f('post_title')),
(string) dcCore::app()->blog?->url . dcCore::app()->url->getBase((string) $posts->f('post_type')) . self::str(dcCore::app()->blog?->url) . dcCore::app()->url->getBase(self::str($posts->f('post_type'))) .
'/' . $posts->f('post_url') . '#c' . $old->f('comment_id'), '/' . self::str($posts->f('post_url')) . '#c' . self::str($old->f('comment_id')),
]; ];
ActivityReport::instance()->addLog('comment', 'update', $logs); ActivityReport::instance()->addLog('comment', 'update', $logs);
} }
@ -389,11 +387,11 @@ class ActivityBehaviors
} }
$logs = [ $logs = [
(string) $cur->getField('comment_author'), self::str($cur->getField('comment_author')),
(string) $cur->getField('comment_site'), self::str($cur->getField('comment_site')),
(string) $posts->f('post_title'), self::str($posts->f('post_title')),
(string) dcCore::app()->blog?->url . dcCore::app()->url->getBase($posts->f('post_type')) . self::str(dcCore::app()->blog?->url) . dcCore::app()->url->getBase(self::str($posts->f('post_type'))) .
'/' . $posts->f('post_url'), '/' . self::str($posts->f('post_url')),
]; ];
ActivityReport::instance()->addLog('comment', 'trackback', $logs); ActivityReport::instance()->addLog('comment', 'trackback', $logs);
} }
@ -401,9 +399,9 @@ class ActivityBehaviors
public static function categoryCreate(Cursor $cur, int $cat_id): void public static function categoryCreate(Cursor $cur, int $cat_id): void
{ {
$logs = [ $logs = [
(string) $cur->getField('cat_title'), self::str($cur->getField('cat_title')),
(string) dcCore::app()->auth?->getInfo('user_cn'), self::str(dcCore::app()->auth?->getInfo('user_cn')),
(string) dcCore::app()->blog?->url . dcCore::app()->url->getBase('category') . '/' . $cur->getField('cat_url'), self::str(dcCore::app()->blog?->url) . dcCore::app()->url->getBase('category') . '/' . self::str($cur->getField('cat_url')),
]; ];
ActivityReport::instance()->addLog('category', 'create', $logs); ActivityReport::instance()->addLog('category', 'create', $logs);
} }
@ -411,9 +409,9 @@ class ActivityBehaviors
public static function categoryUpdate(Cursor $cur, int $cat_id): void public static function categoryUpdate(Cursor $cur, int $cat_id): void
{ {
$logs = [ $logs = [
(string) $cur->getField('cat_title'), self::str($cur->getField('cat_title')),
(string) dcCore::app()->auth?->getInfo('user_cn'), self::str(dcCore::app()->auth?->getInfo('user_cn')),
(string) dcCore::app()->blog?->url . dcCore::app()->url->getBase('category') . '/' . $cur->getField('cat_url'), self::str(dcCore::app()->blog?->url) . dcCore::app()->url->getBase('category') . '/' . self::str($cur->getField('cat_url')),
]; ];
ActivityReport::instance()->addLog('category', 'update', $logs); ActivityReport::instance()->addLog('category', 'update', $logs);
} }
@ -421,14 +419,14 @@ class ActivityBehaviors
public static function userCreate(Cursor $cur, string $user_id): void public static function userCreate(Cursor $cur, string $user_id): void
{ {
$user_cn = dcUtils::getUserCN( $user_cn = dcUtils::getUserCN(
$cur->getField('user_id'), self::str($cur->getField('user_id')),
$cur->getField('user_name'), self::str($cur->getField('user_name')),
$cur->getField('user_firstname'), self::str($cur->getField('user_firstname')),
$cur->getField('user_displayname') self::str($cur->getField('user_displayname'))
); );
$logs = [ $logs = [
(string) $user_cn, self::str($user_cn),
(string) dcCore::app()->auth?->getInfo('user_cn'), self::str(dcCore::app()->auth?->getInfo('user_cn')),
]; ];
ActivityReport::instance()->addLog('user', 'create', $logs); ActivityReport::instance()->addLog('user', 'create', $logs);
} }
@ -436,14 +434,14 @@ class ActivityBehaviors
public static function userUpdate(Cursor $cur, string $user_id): void public static function userUpdate(Cursor $cur, string $user_id): void
{ {
$user_cn = dcUtils::getUserCN( $user_cn = dcUtils::getUserCN(
$cur->getField('user_id'), self::str($cur->getField('user_id')),
$cur->getField('user_name'), self::str($cur->getField('user_name')),
$cur->getField('user_firstname'), self::str($cur->getField('user_firstname')),
$cur->getField('user_displayname') self::str($cur->getField('user_displayname'))
); );
$logs = [ $logs = [
(string) $user_cn, self::str($user_cn),
(string) dcCore::app()->auth?->getInfo('user_cn'), self::str(dcCore::app()->auth?->getInfo('user_cn')),
]; ];
ActivityReport::instance()->addLog('user', 'update', $logs); ActivityReport::instance()->addLog('user', 'update', $logs);
} }
@ -460,13 +458,13 @@ class ActivityBehaviors
return; return;
} }
$user_cn = dcUtils::getUserCN( $user_cn = dcUtils::getUserCN(
$user->f('user_id'), self::str($user->f('user_id')),
$user->f('user_name'), self::str($user->f('user_name')),
$user->f('user_firstname'), self::str($user->f('user_firstname')),
$user->f('user_displayname') self::str($user->f('user_displayname'))
); );
$logs = [ $logs = [
(string) $user_cn, self::str($user_cn),
]; ];
ActivityReport::instance()->addLog('user', 'preference', $logs); ActivityReport::instance()->addLog('user', 'preference', $logs);
} }
@ -475,15 +473,27 @@ class ActivityBehaviors
{ {
$users = dcCore::app()->getUser($user_id); $users = dcCore::app()->getUser($user_id);
$user_cn = dcUtils::getUserCN( $user_cn = dcUtils::getUserCN(
$users->f('user_id'), self::str($users->f('user_id')),
$users->f('user_name'), self::str($users->f('user_name')),
$users->f('user_firstname'), self::str($users->f('user_firstname')),
$users->f('user_displayname') self::str($users->f('user_displayname'))
); );
$logs = [ $logs = [
(string) $user_cn, self::str($user_cn),
(string) dcCore::app()->auth?->getInfo('user_cn'), self::str(dcCore::app()->auth?->getInfo('user_cn')),
]; ];
ActivityReport::instance()->addLog('user', 'delete', $logs); ActivityReport::instance()->addLog('user', 'delete', $logs);
} }
/**
* Type cast.
*
* @param mixed $field The field to check
*
* @return string The string field
*/
private static function str(mixed $field): string
{
return is_string($field) || is_numeric($field) ? (string) $field : 'unknown';
}
} }

View File

@ -155,24 +155,24 @@ class ActivityReport
//nope //nope
} }
if (!empty($params['activity_type'])) { if (!empty($params['activity_type']) && is_string($params['activity_type'])) {
$sql->where('E.activity_type = ' . $sql->quote($params['activity_type'])); $sql->where('E.activity_type = ' . $sql->quote($params['activity_type']));
} else { } else {
$sql->where('E.activity_type = ' . $sql->quote($this->type)); $sql->where('E.activity_type = ' . $sql->quote($this->type));
} }
if (!empty($params['blog_id'])) { if (isset($params['blog_id']) && is_null($params['blog_id'])) {
$sql->and('E.blog_id IS NOT NULL');
} elseif (!empty($params['blog_id'])) {
if (!is_array($params['blog_id'])) { if (!is_array($params['blog_id'])) {
$params['blog_id'] = [$params['blog_id']]; $params['blog_id'] = [$params['blog_id']];
} }
$sql->and('E.blog_id' . $sql->in($params['blog_id'])); $sql->and('E.blog_id' . $sql->in($params['blog_id']));
} elseif (isset($params['blog_id']) && is_null($params['blog_id'])) {
$sql->and('E.blog_id IS NOT NULL');
} else { } else {
$sql->and('E.blog_id = ' . $sql->quote((string) dcCore::app()->blog?->id)); $sql->and('E.blog_id = ' . $sql->quote((string) dcCore::app()->blog?->id));
} }
if (isset($params['activity_status'])) { if (isset($params['activity_status']) && is_numeric($params['activity_status'])) {
$sql->and('E.activity_status = ' . ((int) $params['activity_status']) . ' '); $sql->and('E.activity_status = ' . ((int) $params['activity_status']) . ' ');
} }
//$sql->and('E.activity_status = ' . self::STATUS_PENDING); //$sql->and('E.activity_status = ' . self::STATUS_PENDING);
@ -191,20 +191,17 @@ class ActivityReport
$sql->and('E.activity_action' . $sql->in($params['activity_action'])); $sql->and('E.activity_action' . $sql->in($params['activity_action']));
} }
if (isset($params['from_date_ts'])) { if (isset($params['from_date_ts']) && is_numeric($params['from_date_ts'])) {
$sql->and("E.activity_dt >= TIMESTAMP '" . date('Y-m-d H:i:s', $params['from_date_ts']) . "' "); $sql->and("E.activity_dt >= TIMESTAMP '" . date('Y-m-d H:i:s', (int) $params['from_date_ts']) . "' ");
} }
if (isset($params['to_date_ts'])) { if (isset($params['to_date_ts']) && is_numeric($params['to_date_ts'])) {
$sql->and("E.activity_dt < TIMESTAMP '" . date('Y-m-d H:i:s', $params['to_date_ts']) . "' "); $sql->and("E.activity_dt < TIMESTAMP '" . date('Y-m-d H:i:s', (int) $params['to_date_ts']) . "' ");
} }
if (!empty($params['requests'])) { if (!empty($params['requests'])) {
$or = []; $or = [];
foreach ($this->settings->requests as $group => $actions) { foreach ($this->settings->requests as $group => $actions) {
if (empty($actions)) { foreach ($actions as $action) {
continue;
}
foreach ($actions as $action => $is) {
$or[] = $sql->andGroup(['activity_group = ' . $sql->quote($group), 'activity_action = ' . $sql->quote($action)]); $or[] = $sql->andGroup(['activity_group = ' . $sql->quote($group), 'activity_action = ' . $sql->quote($action)]);
} }
} }
@ -218,7 +215,7 @@ class ActivityReport
} }
if (!$count_only) { if (!$count_only) {
if (!empty($params['order'])) { if (!empty($params['order']) && is_string($params['order'])) {
$sql->order($sql->escape($params['order'])); $sql->order($sql->escape($params['order']));
} else { } else {
$sql->order('E.activity_dt DESC'); $sql->order('E.activity_dt DESC');
@ -236,9 +233,9 @@ class ActivityReport
/** /**
* Add a log. * Add a log.
* *
* @param string $group The group * @param string $group The group
* @param string $action The action * @param string $action The action
* @param array $logs The logs values * @param array<int,string> $logs The logs values
*/ */
public function addLog(string $group, string $action, array $logs): void public function addLog(string $group, string $action, array $logs): void
{ {
@ -272,24 +269,24 @@ class ActivityReport
/** /**
* Parse log message. * Parse log message.
* *
* @param string $message The message to transform * @param string $message The message to transform
* @param array<int,string> The log to parse * @param array<int,string> $logs The log to parse
* *
* @return string The parsed message * @return string The parsed message
*/ */
public static function parseMessage(string $message, array $data): string public static function parseMessage(string $message, array $logs): string
{ {
if (!count($data)) { if (!count($logs)) {
return __('-- activity log is empty --'); return __('-- activity log is empty --');
} }
if ($data[0] == 'undefined') { if ($logs[0] == 'undefined') {
return __('-- activity message is undefined --'); return __('-- activity message is undefined --');
} }
if ((count($data) + 1) != count(explode('%s', $message))) { if ((count($logs) + 1) != count(explode('%s', $message))) {
return __('-- activity data and message missmatch --'); return __('-- activity data and message missmatch --');
} }
return vsprintf($message, $data); return vsprintf($message, $logs);
} }
/** /**
@ -305,6 +302,7 @@ class ActivityReport
$to = 0; $to = 0;
$res = $blog_name = $blog_url = $group = ''; $res = $blog_name = $blog_url = $group = '';
$tz = dcCore::app()->blog?->settings->get('system')->get('blog_timezone'); $tz = dcCore::app()->blog?->settings->get('system')->get('blog_timezone');
$tz = is_string($tz) ? $tz : 'UTC';
$dt = empty($this->settings->dateformat) ? '%Y-%m-%d %H:%M:%S' : $this->settings->dateformat; $dt = empty($this->settings->dateformat) ? '%Y-%m-%d %H:%M:%S' : $this->settings->dateformat;
$format = $this->formats->get($this->formats->has($this->settings->mailformat) ? $this->settings->mailformat : 'plain'); $format = $this->formats->get($this->formats->has($this->settings->mailformat) ? $this->settings->mailformat : 'plain');
$group_open = false; $group_open = false;
@ -397,6 +395,7 @@ class ActivityReport
// Get blogs and logs count // Get blogs and logs count
$sql = new SelectStatement(); $sql = new SelectStatement();
$sql->from(dcCore::app()->prefix . My::ACTIVITY_TABLE_NAME) $sql->from(dcCore::app()->prefix . My::ACTIVITY_TABLE_NAME)
->column('blog_id')
->where('activity_type =' . $sql->quote($this->type)) ->where('activity_type =' . $sql->quote($this->type))
->group('blog_id'); ->group('blog_id');
@ -409,33 +408,34 @@ class ActivityReport
while ($rs->fetch()) { while ($rs->fetch()) {
$ts = time(); $ts = time();
$obs = Date::str('%Y-%m-%d %H:%M:%S', $ts - (int) $this->settings->obsolete); $obs = Date::str('%Y-%m-%d %H:%M:%S', $ts - (int) $this->settings->obsolete);
if (is_string($rs->f('blog_id'))) {
$sql = new DeleteStatement();
$sql->from(dcCore::app()->prefix . My::ACTIVITY_TABLE_NAME)
->where('activity_type =' . $sql->quote($this->type))
->and('activity_dt < TIMESTAMP ' . $sql->quote($obs))
->and('blog_id = ' . $sql->quote($rs->f('blog_id')))
->delete();
$sql = new DeleteStatement(); if (dcCore::app()->con->changes()) {
$sql->from(dcCore::app()->prefix . My::ACTIVITY_TABLE_NAME) try {
->where('activity_type =' . $sql->quote($this->type)) $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . My::ACTIVITY_TABLE_NAME);
->and('activity_dt < TIMESTAMP ' . $sql->quote($obs)) dcCore::app()->con->writeLock(dcCore::app()->prefix . My::ACTIVITY_TABLE_NAME);
->and('blog_id = ' . $sql->quote($rs->f('blog_id')))
->delete();
if (dcCore::app()->con->changes()) { $cur->setField('activity_id', $this->getNextId());
try { $cur->setField('activity_type', $this->type);
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . My::ACTIVITY_TABLE_NAME); $cur->setField('blog_id', $rs->f('blog_id'));
dcCore::app()->con->writeLock(dcCore::app()->prefix . My::ACTIVITY_TABLE_NAME); $cur->setField('activity_group', My::id());
$cur->setField('activity_action', 'message');
$cur->setField('activity_logs', json_encode([__('Activity report deletes some old logs.')]));
$cur->setField('activity_dt', date('Y-m-d H:i:s'));
$cur->setField('activity_status', self::STATUS_PENDING);
$cur->setField('activity_id', $this->getNextId()); $cur->insert();
$cur->setField('activity_type', $this->type); dcCore::app()->con->unlock();
$cur->setField('blog_id', $rs->f('blog_id')); } catch (Exception $e) {
$cur->setField('activity_group', My::id()); dcCore::app()->con->unlock();
$cur->setField('activity_action', 'message'); dcCore::app()->error->add($e->getMessage());
$cur->setField('activity_logs', json_encode([__('Activity report deletes some old logs.')])); }
$cur->setField('activity_dt', date('Y-m-d H:i:s'));
$cur->setField('activity_status', self::STATUS_PENDING);
$cur->insert();
dcCore::app()->con->unlock();
} catch (Exception $e) {
dcCore::app()->con->unlock();
dcCore::app()->error->add($e->getMessage());
} }
} }
} }
@ -653,9 +653,9 @@ class ActivityReport
/** /**
* Send a report. * Send a report.
* *
* @param array $recipients The recipients * @param array<int,string> $recipients The recipients
* @param string $message The message * @param string $message The message
* @param string $mailformat The mail content format * @param string $mailformat The mail content format
* *
* @return bool The sent success * @return bool The sent success
*/ */
@ -719,8 +719,9 @@ class ActivityReport
*/ */
public function getUserCode(): string public function getUserCode(): string
{ {
$code = pack('a32', (string) dcCore::app()->auth?->userID()) . $id = is_string(dcCore::app()->auth?->userID()) ? dcCore::app()->auth->userID() : '';
pack('H*', Crypt::hmac(DC_MASTER_KEY, (string) dcCore::app()->auth?->getInfo('user_pwd'))); $pw = is_string(dcCore::app()->auth?->getInfo('user_pwd')) ? dcCore::app()->auth->getInfo('user_pwd') : '';
$code = pack('a32', $id) . pack('H*', Crypt::hmac(DC_MASTER_KEY, $pw));
return bin2hex($code); return bin2hex($code);
} }
@ -752,7 +753,7 @@ class ActivityReport
$rs = $sql->select(); $rs = $sql->select();
if (!$rs || $rs->isEmpty()) { if (!$rs || $rs->isEmpty() || !is_string($rs->f('user_pwd')) || !is_string($rs->f('user_id'))) {
return false; return false;
} }