From a8edf6c9123d0f45ead885bfee1388eb86296b0d Mon Sep 17 00:00:00 2001 From: Sarjuuk Date: Thu, 17 Mar 2022 10:14:58 +0100 Subject: [PATCH] POST/GET * unify accessing &_GET and &_POST data * properly calc and display diffTime --- includes/community.class.php | 17 +++++------ includes/defines.php | 2 +- includes/utilities.php | 59 +++++++++++++++++++++++++++++------- pages/account.php | 4 +-- pages/screenshot.php | 4 +-- pages/utility.php | 26 ++++++++-------- 6 files changed, 74 insertions(+), 38 deletions(-) diff --git a/includes/community.class.php b/includes/community.class.php index 90dcf6fd..77340567 100644 --- a/includes/community.class.php +++ b/includes/community.class.php @@ -55,7 +55,7 @@ class CommunityContent '; private static string $ssQuery = ' - SELECT s.id, a.displayName AS user, s.date, s.width, s.height, s.caption, IF(s.status & ?d, 1, 0) AS "sticky", s.type, s.typeId + SELECT s.id AS ARRAY_KEY, s.id, a.displayName AS user, s.date, s.width, s.height, s.caption, IF(s.status & ?d, 1, 0) AS "sticky", s.type, s.typeId FROM ?_screenshots s LEFT JOIN ?_account a ON s.userIdOwner = a.id WHERE {s.userIdOwner = ?d AND }{s.type = ? AND }{s.typeId = ? AND }s.status & ?d AND (s.status & ?d) = 0 @@ -64,7 +64,7 @@ class CommunityContent '; private static string $viQuery = ' - SELECT v.id, a.displayName AS user, v.date, v.videoId, v.caption, IF(v.status & ?d, 1, 0) AS "sticky", v.type, v.typeId + SELECT v.id AS ARRAY_KEY, v.id, a.displayName AS user, v.date, v.videoId, v.caption, IF(v.status & ?d, 1, 0) AS "sticky", v.type, v.typeId FROM ?_videos v LEFT JOIN ?_account a ON v.userIdOwner = a.id WHERE {v.userIdOwner = ?d AND }{v.type = ? AND }{v.typeId = ? AND }v.status & ?d AND (v.status & ?d) = 0 @@ -78,7 +78,6 @@ class CommunityContent c.body AS preview, c.date, c.replyTo AS commentid, - UNIX_TIMESTAMP() - c.date AS elapsed, IF(c.flags & ?d, 1, 0) AS deleted, IF(c.type <> 0, c.type, c2.type) AS type, IF(c.typeId <> 0, c.typeId, c2.typeId) AS typeId, @@ -151,7 +150,7 @@ class CommunityContent } } - public static function getCommentPreviews(array $params = [], int &$nFound = 0) : array + public static function getCommentPreviews(array $params = [], int &$nFound = 0, bool $dateFmt = true) : array { /* purged:0, <- doesnt seem to be used anymore @@ -184,7 +183,7 @@ class CommunityContent $c['subject'] = self::$subjCache[$c['type']][$c['typeId']]; // format date - $c['date'] = date(Util::$dateFormatInternal, $c['date']); + $c['date'] = $dateFmt ? date(Util::$dateFormatInternal, $c['date']) : intVal($c['date']); // remove commentid if not looking for replies if (empty($params['replies'])) @@ -418,7 +417,7 @@ class CommunityContent return $comments; } - public static function getVideos(int $typeOrUser = 0, int $typeId = 0, int &$nFound = 0) : array + public static function getVideos(int $typeOrUser = 0, int $typeId = 0, int &$nFound = 0, bool $dateFmt = true) : array { $videos = DB::Aowow()->selectPage($nFound, self::$viQuery, CC_FLAG_STICKY, @@ -450,7 +449,7 @@ class CommunityContent $v['subject'] = Lang::user('removed'); } - $v['date'] = date(Util::$dateFormatInternal, $v['date']); + $v['date'] = $dateFmt ? date(Util::$dateFormatInternal, $v['date']) : intVal($v['date']); $v['videoType'] = 1; // always youtube if (!$v['sticky']) @@ -463,7 +462,7 @@ class CommunityContent return $videos; } - public static function getScreenshots(int $typeOrUser = 0, int $typeId = 0, int &$nFound = 0) : array + public static function getScreenshots(int $typeOrUser = 0, int $typeId = 0, int &$nFound = 0, bool $dateFmt = true) : array { $screenshots = DB::Aowow()->selectPage($nFound, self::$ssQuery, CC_FLAG_STICKY, @@ -495,7 +494,7 @@ class CommunityContent $s['subject'] = Lang::user('removed'); } - $s['date'] = date(Util::$dateFormatInternal, $s['date']); + $s['date'] = $dateFmt ? date(Util::$dateFormatInternal, $s['date']) : intVal($s['date']); if (!$s['sticky']) unset($s['sticky']); diff --git a/includes/defines.php b/includes/defines.php index 263f09c0..52684d27 100644 --- a/includes/defines.php +++ b/includes/defines.php @@ -9,7 +9,7 @@ if (!defined('AOWOW_REVISION')) define('E_AOWOW', E_ALL & ~(E_DEPRECATED | E_USER_DEPRECATED | E_STRICT)); define('JSON_AOWOW_POWER', JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); -define('FILTER_FLAG_STRIP_AOWOW', FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_STRIP_BACKTICK); +define('FILTER_FLAG_STRIP_AOWOW', FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK); define('MIME_TYPE_TEXT', 'Content-Type: text/plain; charset=utf-8'); define('MIME_TYPE_XML', 'Content-Type: text/xml; charset=utf-8'); diff --git a/includes/utilities.php b/includes/utilities.php index 610b999e..f049e0f0 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -25,24 +25,43 @@ trait TrRequestData if ($this->filtered) return; + // php bug? If INPUT_X is empty, filter_input_array returns null/fails + // only really relevant for INPUT_POST + // manuall set everything null in this case + if (isset($this->_post) && gettype($this->_post) == 'array') - $this->_post = filter_input_array(INPUT_POST, $this->_post); + { + if ($_POST) + $this->_post = filter_input_array(INPUT_POST, $this->_post); + else + $this->_post = array_fill_keys(array_keys($this->_post), null); + } if (isset($this->_get) && gettype($this->_get) == 'array') - $this->_get = filter_input_array(INPUT_GET, $this->_get); + { + if ($_GET) + $this->_get = filter_input_array(INPUT_GET, $this->_get); + else + $this->_get = array_fill_keys(array_keys($this->_get), null); + } if (isset($this->_cookie) && gettype($this->_cookie) == 'array') - $this->_cookie = filter_input_array(INPUT_COOKIE, $this->_cookie); + { + if ($_COOKIE) + $this->_cookie = filter_input_array(INPUT_COOKIE, $this->_cookie); + else + $this->_cookie = array_fill_keys(array_keys($this->_cookie), null); + } $this->filtered = true; } - protected static function checkEmptySet(string $val) : bool + private static function checkEmptySet(string $val) : bool { return $val === ''; // parameter is expected to be empty } - protected static function checkInt(string $val) : int + public static function checkInt(string $val) : int { if (preg_match('/^-?\d+$/', $val)) return intVal($val); @@ -50,7 +69,7 @@ trait TrRequestData return 0; } - protected static function checkLocale(string $val) : int + private static function checkLocale(string $val) : int { if (preg_match('/^'.implode('|', array_keys(array_filter(Util::$localeStrings))).'$/', $val)) return intVal($val); @@ -58,7 +77,7 @@ trait TrRequestData return -1; } - protected static function checkDomain(string $val) : string + private static function checkDomain(string $val) : string { if (preg_match('/^'.implode('|', array_filter(Util::$subDomains)).'$/i', $val)) return strtolower($val); @@ -66,7 +85,7 @@ trait TrRequestData return ''; } - protected static function checkIdList(string $val) : array + private static function checkIdList(string $val) : array { if (preg_match('/^-?\d+(,-?\d+)*$/', $val)) return array_map('intVal', explode(',', $val)); @@ -74,7 +93,7 @@ trait TrRequestData return []; } - protected static function checkIntArray(string $val) : array + private static function checkIntArray(string $val) : array { if (preg_match('/^-?\d+(:-?\d+)*$/', $val)) return array_map('intVal', explode(':', $val)); @@ -82,7 +101,7 @@ trait TrRequestData return []; } - protected static function checkIdListUnsigned(string $val) : array + private static function checkIdListUnsigned(string $val) : array { if (preg_match('/\d+(,\d+)*/', $val)) return array_map('intVal', explode(',', $val)); @@ -90,7 +109,7 @@ trait TrRequestData return []; } - protected static function checkFulltext(string $val) : string + private static function checkFulltext(string $val) : string { // trim non-printable chars return preg_replace('/[\p{Cf}\p{Co}\p{Cs}\p{Cn}]/ui', '', $val); @@ -654,6 +673,24 @@ class Util } } + public static function formatTimeDiff(int $sec) : string + { + $delta = time() - $sec; + + [, $s, $m, $h, $d] = self::parseTime($delta * 1000); + + if ($delta > (1 * MONTH)) // use absolute + return date(Lang::main('dateFmtLong'), $sec); + else if ($delta > (2 * DAY)) // days ago + return Lang::main('timeAgo', [$d . ' ' . Lang::timeUnits('pl', 3)]); + else if ($h) // hours, minutes ago + return Lang::main('timeAgo', [$h . ' ' . Lang::timeUnits('ab', 4) . ' ' . $m . ' ' . Lang::timeUnits('ab', 5)]); + else if ($m) // minutes, seconds ago + return Lang::main('timeAgo', [$m . ' ' . Lang::timeUnits('ab', 5) . ' ' . $m . ' ' . Lang::timeUnits('ab', 6)]); + else // seconds ago + return Lang::main('timeAgo', [$s . ' ' . Lang::timeUnits($s == 1 ? 'sg' : 'pl', 6)]); + } + // pageText for Books (Item or GO) and questText public static function parseHtmlText(string $text, bool $markdown = false) : string { diff --git a/pages/account.php b/pages/account.php index c6710298..a910efc4 100644 --- a/pages/account.php +++ b/pages/account.php @@ -37,7 +37,7 @@ class AccountPage extends GenericPage 'password' => ['filter' => FILTER_UNSAFE_RAW], 'c_password' => ['filter' => FILTER_UNSAFE_RAW], 'token' => ['filter' => FILTER_UNSAFE_RAW], - 'remember_me' => ['filter' => FILTER_CALLBACK, 'options' => 'GenericPage::rememberCallback'], + 'remember_me' => ['filter' => FILTER_CALLBACK, 'options' => 'AccountPage::rememberCallback'], 'email' => ['filter' => FILTER_SANITIZE_EMAIL] ); @@ -59,7 +59,7 @@ class AccountPage extends GenericPage } } - protected function rememberCallback($val) + protected static function rememberCallback($val) { return $val == 'yes' ? $val : null; } diff --git a/pages/screenshot.php b/pages/screenshot.php index 16cc0b86..f26d2bd5 100644 --- a/pages/screenshot.php +++ b/pages/screenshot.php @@ -28,8 +28,8 @@ class ScreenshotPage extends GenericPage protected $imgHash = ''; protected $_post = array( - 'coords' => ['filter' => FILTER_CALLBACK, 'options' => 'AjaxHandler::checkIdListUnsigned'], - 'screenshotalt' => ['filter' => FILTER_UNSAFE_RAW] + 'coords' => ['filter' => FILTER_CALLBACK, 'options' => 'GenericPage::checkIdListUnsigned'], + 'screenshotalt' => ['filter' => FILTER_UNSAFE_RAW, 'flags' => FILTER_FLAG_STRIP_AOWOW] ); public function __construct($pageCall, $pageParam) diff --git a/pages/utility.php b/pages/utility.php index 93e107ed..8f639859 100644 --- a/pages/utility.php +++ b/pages/utility.php @@ -75,7 +75,7 @@ class UtilityPage extends GenericPage header('Location: ?'.Util::$typeStrings[$type].'='.$typeId, true, 302); die(); case 'latest-comments': // rss - $data = CommunityContent::getCommentPreviews(); + $data = CommunityContent::getCommentPreviews(dateFmt: false); if ($this->rss) { @@ -85,19 +85,19 @@ class UtilityPage extends GenericPage $this->feedData[] = array( 'title' => [true, [], Util::ucFirst(Lang::game(Util::$typeStrings[$d['type']])).Lang::main('colon').htmlentities($d['subject'])], 'link' => [false, [], HOST_URL.'/?go-to-comment&id='.$d['id']], - 'description' => [true, [], htmlentities($d['preview'])."

".sprintf(Lang::main('byUserTimeAgo'), $d['user'], Util::formatTime($d['elapsed'] * 1000, true))], - 'pubDate' => [false, [], date(DATE_RSS, time() - $d['elapsed'])], + 'description' => [true, [], htmlentities($d['preview'])."

".Lang::main('byUser', [$d['user'], '']) . Util::formatTimeDiff($d['date'], true)], + 'pubDate' => [false, [], date(DATE_RSS, $d['date'])], 'guid' => [false, [], HOST_URL.'/?go-to-comment&id='.$d['id']] // 'domain' => [false, [], null] ); } } else - $this->lvTabs[] = ['commentpreview', ['data' => $data]]; + $this->lvTabs[] = ['commentpreview', ['data' => array_values($data)]]; break; case 'latest-screenshots': // rss - $data = CommunityContent::getScreenshots(); + $data = CommunityContent::getScreenshots(dateFmt: false); if ($this->rss) { @@ -106,14 +106,14 @@ class UtilityPage extends GenericPage $desc = ''; if ($d['caption']) $desc .= '
'.$d['caption']; - $desc .= "

".sprintf(Lang::main('byUserTimeAgo'), $d['user'], Util::formatTime($d['elapsed'] * 1000, true)); + $desc .= "

".Lang::main('byUser', [$d['user'], '']) . Util::formatTimeDiff($d['date'], true); // enclosure/length => filesize('static/uploads/screenshots/thumb/'.$d['id'].'.jpg') .. always set to this placeholder value though $this->feedData[] = array( 'title' => [true, [], Util::ucFirst(Lang::game(Util::$typeStrings[$d['type']])).Lang::main('colon').htmlentities($d['subject'])], 'link' => [false, [], HOST_URL.'/?'.Util::$typeStrings[$d['type']].'='.$d['typeId'].'#screenshots:id='.$d['id']], 'description' => [true, [], $desc], - 'pubDate' => [false, [], date(DATE_RSS, time() - $d['elapsed'])], + 'pubDate' => [false, [], date(DATE_RSS, $d['date'])], 'enclosure' => [false, ['url' => STATIC_URL.'/uploads/screenshots/thumb/'.$d['id'].'.jpg', 'length' => 12345, 'type' => 'image/jpeg'], null], 'guid' => [false, [], HOST_URL.'/?'.Util::$typeStrings[$d['type']].'='.$d['typeId'].'#screenshots:id='.$d['id']], // 'domain' => [false, [], live|ptr] @@ -121,11 +121,11 @@ class UtilityPage extends GenericPage } } else - $this->lvTabs[] = ['screenshot', ['data' => $data]]; + $this->lvTabs[] = ['screenshot', ['data' => array_values($data)]]; break; case 'latest-videos': // rss - $data = CommunityContent::getVideos(); + $data = CommunityContent::getVideos(dateFmt: false); if ($this->rss) { @@ -134,14 +134,14 @@ class UtilityPage extends GenericPage $desc = ''; if ($d['caption']) $desc .= '
'.$d['caption']; - $desc .= "

".sprintf(Lang::main('byUserTimeAgo'), $d['user'], Util::formatTime($d['elapsed'] * 1000, true)); + $desc .= "

".Lang::main('byUser', [$d['user'], '']) . Util::formatTimeDiff($d['date'], true); // is enclosure/length .. is this even relevant..? $this->feedData[] = array( - 'title' => [true, [], Util::ucFirst(Lang::game(Util::$typeStrings[$d['type']])).Lang::main('colon').htmlentities($row['subject'])], + 'title' => [true, [], Util::ucFirst(Lang::game(Util::$typeStrings[$d['type']])).Lang::main('colon').htmlentities($d['subject'])], 'link' => [false, [], HOST_URL.'/?'.Util::$typeStrings[$d['type']].'='.$d['typeId'].'#videos:id='.$d['id']], 'description' => [true, [], $desc], - 'pubDate' => [false, [], date(DATE_RSS, time() - $row['elapsed'])], + 'pubDate' => [false, [], date(DATE_RSS, $d['date'])], 'enclosure' => [false, ['url' => '//i3.ytimg.com/vi/'.$d['videoId'].'/default.jpg', 'length' => 12345, 'type' => 'image/jpeg'], null], 'guid' => [false, [], HOST_URL.'/?'.Util::$typeStrings[$d['type']].'='.$d['typeId'].'#videos:id='.$d['id']], // 'domain' => [false, [], live|ptr] @@ -149,7 +149,7 @@ class UtilityPage extends GenericPage } } else - $this->lvTabs[] = ['video', ['data' => $data]]; + $this->lvTabs[] = ['video', ['data' => array_values($data)]]; break; case 'latest-articles': // rss