summaryrefslogtreecommitdiffstats
path: root/inc/util.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/util.inc.php')
-rw-r--r--inc/util.inc.php37
1 files changed, 35 insertions, 2 deletions
diff --git a/inc/util.inc.php b/inc/util.inc.php
index 9bcfdf13..ace879f4 100644
--- a/inc/util.inc.php
+++ b/inc/util.inc.php
@@ -150,15 +150,21 @@ SADFACE;
* Redirects the user via a '302 Moved' header.
* An active session will be saved, any messages that haven't
* been displayed yet will be appended to the redirect.
- * @param string $location Location to redirect to. "false" to redirect to same URL (useful after POSTs)
+ * @param string|false $location Location to redirect to. "false" to redirect to same URL (useful after POSTs)
+ * @param bool $preferRedirectPost if true, use the value from $_POST['redirect'] instead of $location
*/
- public static function redirect($location = false)
+ public static function redirect($location = false, $preferRedirectPost = false)
{
if ($location === false) {
$location = preg_replace('/(&|\?)message\[\]\=[^&]*/', '\1', $_SERVER['REQUEST_URI']);
}
Session::save();
$messages = Message::toRequest();
+ if ($preferRedirectPost
+ && ($redirect = Request::post('redirect', false, 'string')) !== false
+ && !preg_match(',^(\w+\:|//),', $redirect) /* no uri scheme, no server */) {
+ $location = $redirect;
+ }
if (!empty($messages)) {
if (strpos($location, '?') === false) {
$location .= '?' . $messages;
@@ -462,4 +468,31 @@ SADFACE;
);
}
+ /**
+ * Transform timestamp to easily readable string.
+ * The format depends on how far the timestamp lies in the past.
+ * @param int $ts unix timestamp
+ * @return string human readable representation
+ */
+ public static function prettyTime($ts)
+ {
+ settype($ts, 'int');
+ if ($ts === 0)
+ return '???';
+ static $TODAY = false, $ETODAY = false, $YESTERDAY = false, $YEAR = false;
+ if (!$ETODAY) $ETODAY = strtotime('today 23:59:59');
+ if ($ts > $ETODAY) // TODO: Do we need strings for future too?
+ return date('d.m.Y H:i', $ts);
+ if (!$TODAY) $TODAY = strtotime('today 0:00');
+ if ($ts >= $TODAY)
+ return Dictionary::translate('lang_today') . ' ' . date('H:i', $ts);
+ if (!$YESTERDAY) $YESTERDAY = strtotime('yesterday 0:00');
+ if ($ts >= $YESTERDAY)
+ return Dictionary::translate('lang_yesterday') . ' ' . date('H:i', $ts);
+ if (!$YEAR) $YEAR = strtotime('this year 1/1');
+ if ($ts >= $YEAR)
+ return date('d.m. H:i', $ts);
+ return date('d.m.Y', $ts);
+ }
+
}