From 215f3889924973ad138cfce309744cb1abf38cf0 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 23 Sep 2020 13:04:26 +0300 Subject: [PATCH] move timestamp-related stuff to a separate class --- classes/counters.php | 2 +- classes/digest.php | 4 +- classes/feeds.php | 14 ++--- classes/handler/public.php | 2 +- classes/pref/feeds.php | 8 +-- classes/pref/filters.php | 2 +- classes/pref/prefs.php | 4 +- classes/pref/system.php | 7 +-- classes/pref/users.php | 8 +-- classes/rpc.php | 2 - include/functions.php | 108 +++++++--------------------------- plugins/af_psql_trgm/init.php | 4 +- 12 files changed, 47 insertions(+), 118 deletions(-) diff --git a/classes/counters.php b/classes/counters.php index 4d37e407b..be634c52a 100644 --- a/classes/counters.php +++ b/classes/counters.php @@ -112,7 +112,7 @@ class Counters { $id = $line["id"]; $last_error = htmlspecialchars($line["last_error"]); - $last_updated = make_local_datetime($line['last_updated'], false); + $last_updated = TimeHelper::make_local_datetime($line['last_updated'], false); if (Feeds::feedHasIcon($id)) { $has_img = filemtime(Feeds::getIconFile($id)); diff --git a/classes/digest.php b/classes/digest.php index 9101b52f4..5128b4186 100644 --- a/classes/digest.php +++ b/classes/digest.php @@ -97,7 +97,7 @@ class Digest $tpl_t->readTemplateFromFile("digest_template.txt"); $user_tz_string = get_pref('USER_TIMEZONE', $user_id); - $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string); + $local_ts = TimeHelper::convert_timestamp(time(), 'UTC', $user_tz_string); $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts)); $tpl->setVariable('CUR_TIME', date('G:i', $local_ts)); @@ -159,7 +159,7 @@ class Digest array_push($affected_ids, $line["ref_id"]); - $updated = make_local_datetime($line['last_updated'], false, + $updated = TimeHelper::make_local_datetime($line['last_updated'], false, $user_id); if (get_pref('ENABLE_FEED_CATS', $user_id)) { diff --git a/classes/feeds.php b/classes/feeds.php index bb5456577..fdc2de640 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -211,7 +211,7 @@ class Feeds extends Handler_Protected { $feed_site_url = $qfh_ret[2]; $last_error = $qfh_ret[3]; $last_updated = strpos($qfh_ret[4], '1970-') === false ? - make_local_datetime($qfh_ret[4], false) : __("Never"); + TimeHelper::make_local_datetime($qfh_ret[4], false) : __("Never"); $highlight_words = $qfh_ret[5]; $reply['first_id'] = $qfh_ret[6]; $reply['is_vfeed'] = $qfh_ret[7]; @@ -343,12 +343,12 @@ class Feeds extends Handler_Protected { } } - $line["updated_long"] = make_local_datetime($line["updated"],true); - $line["updated"] = make_local_datetime($line["updated"], false, false, false, true); + $line["updated_long"] = TimeHelper::make_local_datetime($line["updated"],true); + $line["updated"] = TimeHelper::make_local_datetime($line["updated"], false, false, false, true); $line['imported'] = T_sprintf("Imported at %s", - make_local_datetime($line["date_entered"], false)); + TimeHelper::make_local_datetime($line["date_entered"], false)); if ($line["tag_cache"]) $tags = explode(",", $line["tag_cache"]); @@ -426,7 +426,7 @@ class Feeds extends Handler_Protected { $sth->execute([$_SESSION['uid']]); $row = $sth->fetch(); - $last_updated = make_local_datetime($row["last_updated"], false); + $last_updated = TimeHelper::make_local_datetime($row["last_updated"], false); $reply['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated); @@ -580,7 +580,7 @@ class Feeds extends Handler_Protected { $sth->execute([$_SESSION['uid']]); $row = $sth->fetch(); - $last_updated = make_local_datetime($row["last_updated"], false); + $last_updated = TimeHelper::make_local_datetime($row["last_updated"], false); $reply['headlines']['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated); @@ -2239,7 +2239,7 @@ class Feeds extends Handler_Protected { $user_tz_string = get_pref('USER_TIMEZONE', $_SESSION['uid']); $orig_ts = strtotime(substr($k, 1)); - $k = date("Y-m-d", convert_timestamp($orig_ts, $user_tz_string, 'UTC')); + $k = date("Y-m-d", TimeHelper::convert_timestamp($orig_ts, $user_tz_string, 'UTC')); //$k = date("Y-m-d", strtotime(substr($k, 1))); diff --git a/classes/handler/public.php b/classes/handler/public.php index 9671b18b2..4bd9c06f9 100755 --- a/classes/handler/public.php +++ b/classes/handler/public.php @@ -414,7 +414,7 @@ class Handler_Public extends Handler { $rv .= "
"; # row //$entry_author = $line["author"] ? " - " . $line["author"] : ""; - $parsed_updated = make_local_datetime($line["updated"], true, + $parsed_updated = TimeHelper::make_local_datetime($line["updated"], true, $owner_uid, true); $rv .= "
".$line['author']."
"; diff --git a/classes/pref/feeds.php b/classes/pref/feeds.php index d7220f9ae..8b9099007 100755 --- a/classes/pref/feeds.php +++ b/classes/pref/feeds.php @@ -101,7 +101,7 @@ class Pref_Feeds extends Handler_Protected { $feed['unread'] = -1; $feed['error'] = $feed_line['last_error']; $feed['icon'] = Feeds::getFeedIcon($feed_line['id']); - $feed['param'] = make_local_datetime( + $feed['param'] = TimeHelper::make_local_datetime( $feed_line['last_updated'], true); $feed['updates_disabled'] = (int)($feed_line['update_interval'] < 0); @@ -268,7 +268,7 @@ class Pref_Feeds extends Handler_Protected { $feed['checkbox'] = false; $feed['error'] = $feed_line['last_error']; $feed['icon'] = Feeds::getFeedIcon($feed_line['id']); - $feed['param'] = make_local_datetime( + $feed['param'] = TimeHelper::make_local_datetime( $feed_line['last_updated'], true); $feed['unread'] = -1; $feed['type'] = 'feed'; @@ -303,7 +303,7 @@ class Pref_Feeds extends Handler_Protected { $feed['checkbox'] = false; $feed['error'] = $feed_line['last_error']; $feed['icon'] = Feeds::getFeedIcon($feed_line['id']); - $feed['param'] = make_local_datetime( + $feed['param'] = TimeHelper::make_local_datetime( $feed_line['last_updated'], true); $feed['unread'] = -1; $feed['type'] = 'feed'; @@ -1478,7 +1478,7 @@ class Pref_Feeds extends Handler_Protected { htmlspecialchars($line["title"]).""; print ""; - print make_local_datetime($line['last_article'], false); + print TimeHelper::make_local_datetime($line['last_article'], false); print ""; print ""; diff --git a/classes/pref/filters.php b/classes/pref/filters.php index 6121e4c14..1113f251e 100755 --- a/classes/pref/filters.php +++ b/classes/pref/filters.php @@ -302,7 +302,7 @@ class Pref_Filters extends Handler_Protected { $filter['name'] = $name[0]; $filter['param'] = $name[1]; $filter['checkbox'] = false; - $filter['last_triggered'] = $line["last_triggered"] ? make_local_datetime($line["last_triggered"], false) : null; + $filter['last_triggered'] = $line["last_triggered"] ? TimeHelper::make_local_datetime($line["last_triggered"], false) : null; $filter['enabled'] = sql_bool_to_bool($line["enabled"]); $filter['rules'] = $this->getfilterrules_list($line['id']); diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php index f825454dd..d7b486cbb 100644 --- a/classes/pref/prefs.php +++ b/classes/pref/prefs.php @@ -1320,11 +1320,11 @@ class Pref_Prefs extends Handler_Protected { print "" . htmlspecialchars($row["title"]) . ""; print ""; - print make_local_datetime($row['created'], false); + print TimeHelper::make_local_datetime($row['created'], false); print ""; print ""; - print make_local_datetime($row['last_used'], false); + print TimeHelper::make_local_datetime($row['last_used'], false); print ""; print ""; diff --git a/classes/pref/system.php b/classes/pref/system.php index d0f8a8273..7e9aa44a1 100644 --- a/classes/pref/system.php +++ b/classes/pref/system.php @@ -26,7 +26,7 @@ class Pref_System extends Handler_Protected { function index() { print "
"; - print "
report ".__('Event Log')."\">"; if (LOG_DESTINATION == "sql") { @@ -66,8 +66,7 @@ class Pref_System extends Handler_Protected { print "" . $line["login"] . ""; print "" . - make_local_datetime( - $line["created_at"], false) . ""; + TimeHelper::make_local_datetime($line["created_at"], false) . ""; print ""; } @@ -81,7 +80,7 @@ class Pref_System extends Handler_Protected { print "
"; - print "
info ".__('PHP Information')."\">"; ob_start(); diff --git a/classes/pref/users.php b/classes/pref/users.php index fd307f7ec..5ec7aa2e6 100644 --- a/classes/pref/users.php +++ b/classes/pref/users.php @@ -137,10 +137,10 @@ class Pref_Users extends Handler_Protected { if ($row = $sth->fetch()) { print ""; - $last_login = make_local_datetime( + $last_login = TimeHelper::make_local_datetime( $row["last_login"], true); - $created = make_local_datetime( + $created = TimeHelper::make_local_datetime( $row["created"], true); $stored_articles = $row["stored_articles"]; @@ -399,8 +399,8 @@ class Pref_Users extends Handler_Protected { print ""; $line["login"] = htmlspecialchars($line["login"]); - $line["created"] = make_local_datetime($line["created"], false); - $line["last_login"] = make_local_datetime($line["last_login"], false); + $line["created"] = TimeHelper::make_local_datetime($line["created"], false); + $line["last_login"] = TimeHelper::make_local_datetime($line["last_login"], false); print ""; diff --git a/classes/rpc.php b/classes/rpc.php index b5cbbbcea..59a119ed7 100755 --- a/classes/rpc.php +++ b/classes/rpc.php @@ -647,8 +647,6 @@ class RPC extends Handler_Protected { $params["php_platform"] = PHP_OS; $params["php_version"] = PHP_VERSION; - $params["sanity_checksum"] = sha1(file_get_contents("include/sanity_check.php")); - $pdo = Db::pdo(); $sth = $pdo->prepare("SELECT MAX(id) AS mid, COUNT(*) AS nf FROM diff --git a/include/functions.php b/include/functions.php index 506d426b1..cf3362e71 100644 --- a/include/functions.php +++ b/include/functions.php @@ -168,26 +168,46 @@ Debug::log($msg); } + // @deprecated + function getFeedUnread($feed, $is_cat = false) { + return Feeds::getFeedArticles($feed, $is_cat, true, $_SESSION["uid"]); + } + + // @deprecated function sanitize($str, $force_remove_images = false, $owner = false, $site_url = false, $highlight_words = false, $article_id = false) { return Sanitizer::sanitize($str, $force_remove_images, $owner, $site_url, $highlight_words, $article_id); } + // @deprecated function fetch_file_contents($params) { return UrlHelper::fetch($params); } + // @deprecated function rewrite_relative_url($url, $rel_url) { return UrlHelper::rewrite_relative($url, $rel_url); } + // @deprecated function validate_url($url) { return UrlHelper::validate($url); } + // @deprecated function authenticate_user($login, $password, $check_only = false, $service = false) { return UserHelper::authenticate($login, $password, $check_only, $service); } + // @deprecated + function smart_date_time($timestamp, $tz_offset = 0, $owner_uid = false, $eta_min = false) { + return TimeHelper::smart_date_time($timestamp, $tz_offset, $owner_uid, $eta_min); + } + + // @deprecated + function make_local_datetime($timestamp, $long, $owner_uid = false, $no_smart_dt = false, $eta_min = false) { + return TimeHelper::make_local_datetime($timestamp, $long, $owner_uid, $no_smart_dt, $eta_min); + } + /* end compat shims */ function get_ssl_certificate_id() { @@ -271,90 +291,6 @@ } } - function convert_timestamp($timestamp, $source_tz, $dest_tz) { - - try { - $source_tz = new DateTimeZone($source_tz); - } catch (Exception $e) { - $source_tz = new DateTimeZone('UTC'); - } - - try { - $dest_tz = new DateTimeZone($dest_tz); - } catch (Exception $e) { - $dest_tz = new DateTimeZone('UTC'); - } - - $dt = new DateTime(date('Y-m-d H:i:s', $timestamp), $source_tz); - return $dt->format('U') + $dest_tz->getOffset($dt); - } - - function make_local_datetime($timestamp, $long, $owner_uid = false, - $no_smart_dt = false, $eta_min = false) { - - if (!$owner_uid) $owner_uid = $_SESSION['uid']; - if (!$timestamp) $timestamp = '1970-01-01 0:00'; - - global $utc_tz; - global $user_tz; - - if (!$utc_tz) $utc_tz = new DateTimeZone('UTC'); - - $timestamp = substr($timestamp, 0, 19); - - # We store date in UTC internally - $dt = new DateTime($timestamp, $utc_tz); - - $user_tz_string = get_pref('USER_TIMEZONE', $owner_uid); - - if ($user_tz_string != 'Automatic') { - - try { - if (!$user_tz) $user_tz = new DateTimeZone($user_tz_string); - } catch (Exception $e) { - $user_tz = $utc_tz; - } - - $tz_offset = $user_tz->getOffset($dt); - } else { - $tz_offset = (int) -$_SESSION["clientTzOffset"]; - } - - $user_timestamp = $dt->format('U') + $tz_offset; - - if (!$no_smart_dt) { - return smart_date_time($user_timestamp, - $tz_offset, $owner_uid, $eta_min); - } else { - if ($long) - $format = get_pref('LONG_DATE_FORMAT', $owner_uid); - else - $format = get_pref('SHORT_DATE_FORMAT', $owner_uid); - - return date($format, $user_timestamp); - } - } - - function smart_date_time($timestamp, $tz_offset = 0, $owner_uid = false, $eta_min = false) { - if (!$owner_uid) $owner_uid = $_SESSION['uid']; - - if ($eta_min && time() + $tz_offset - $timestamp < 3600) { - return T_sprintf("%d min", date("i", time() + $tz_offset - $timestamp)); - } else if (date("Y.m.d", $timestamp) == date("Y.m.d", time() + $tz_offset)) { - $format = get_pref('SHORT_DATE_FORMAT', $owner_uid); - if (strpos((strtolower($format)), "a") === false) - return date("G:i", $timestamp); - else - return date("g:i a", $timestamp); - } else if (date("Y", $timestamp) == date("Y", time() + $tz_offset)) { - $format = get_pref('SHORT_DATE_FORMAT', $owner_uid); - return date($format, $timestamp); - } else { - $format = get_pref('LONG_DATE_FORMAT', $owner_uid); - return date($format, $timestamp); - } - } - function sql_bool_to_bool($s) { return $s && ($s !== "f" && $s !== "false"); //no-op for PDO, backwards compat for legacy layer } @@ -441,10 +377,6 @@ } } - function getFeedUnread($feed, $is_cat = false) { - return Feeds::getFeedArticles($feed, $is_cat, true, $_SESSION["uid"]); - } - function checkbox_to_sql_bool($val) { return ($val == "on") ? 1 : 0; } diff --git a/plugins/af_psql_trgm/init.php b/plugins/af_psql_trgm/init.php index b6aea67fb..20e3981ce 100644 --- a/plugins/af_psql_trgm/init.php +++ b/plugins/af_psql_trgm/init.php @@ -98,7 +98,7 @@ class Af_Psql_Trgm extends Plugin { print ""; - print "
" . smart_date_time(strtotime($line["updated"])) . "
"; + print "
" . TimeHelper::smart_date_time(strtotime($line["updated"])) . "
"; print ""; } @@ -123,7 +123,7 @@ class Af_Psql_Trgm extends Plugin { function hook_prefs_tab($args) { if ($args != "prefFeeds") return; - print "
extension ".__('Mark similar articles as read')."\">"; if (DB_TYPE != "pgsql") {