flash_message -> page->flash, with no cookies

This commit is contained in:
Shish 2019-12-15 19:47:18 +00:00
parent 8740d83686
commit 70db0ce5bd
29 changed files with 93 additions and 115 deletions

View file

@ -686,18 +686,18 @@ class Image
*/
public function set_tags(array $unfiltered_tags): void
{
global $cache, $database;
global $cache, $database, $page;
$unfiltered_tags = array_unique($unfiltered_tags);
$tags = [];
foreach ($unfiltered_tags as $tag) {
if (mb_strlen($tag, 'UTF-8') > 255) {
flash_message("Can't set a tag longer than 255 characters");
$page->flash("Can't set a tag longer than 255 characters");
continue;
}
if (startsWith($tag, "-")) {
flash_message("Can't set a tag which starts with a minus");
$page->flash("Can't set a tag which starts with a minus");
continue;
}

View file

@ -136,12 +136,13 @@ class Tag
public static function sanitize_array(array $tags): array
{
global $page;
$tag_array = [];
foreach ($tags as $tag) {
try {
$tag = Tag::sanitize($tag);
} catch (Exception $e) {
flash_message($e->getMessage());
$page->flash($e->getMessage());
continue;
}

View file

@ -19,6 +19,7 @@ define("SCORE_LOG_NOTSET", 0);
*/
function log_msg(string $section, int $priority, string $message, ?string $flash=null, $args=[])
{
global $page;
send_event(new LogEvent($section, $priority, $message, $args));
$threshold = defined("CLI_LOG_LEVEL") ? CLI_LOG_LEVEL : 0;
@ -26,7 +27,7 @@ function log_msg(string $section, int $priority, string $message, ?string $flash
print date("c")." $section: $message\n";
}
if (!is_null($flash)) {
flash_message($flash);
$page->flash($flash);
}
}

View file

@ -157,6 +157,9 @@ class Page
/** @var Block[] */
public $blocks = [];
/** @var string[] */
public $flash = [];
/**
* Set the HTTP status code
*/
@ -180,6 +183,11 @@ class Page
$this->subheading = $subheading;
}
public function flash(string $message): void
{
$this->flash[] = $message;
}
/**
* Add a line to the HTML head section.
*/
@ -263,6 +271,10 @@ class Page
{
global $page, $user;
if (@$_GET["flash"]) {
$this->flash[] = $_GET['flash'];
}
header("HTTP/1.0 {$this->code} Shimmie");
header("Content-type: " . $this->type);
header("X-Powered-By: SCore-" . SCORE_VERSION);
@ -295,9 +307,6 @@ class Page
# header("Cache-control: no-cache");
# header('Expires: ' . gmdate('D, d M Y H:i:s', time() - 600) . ' GMT');
#}
if ($this->get_cookie("flash_message") !== null) {
$this->add_cookie("flash_message", "", -1, "/");
}
usort($this->blocks, "blockcmp");
$pnbe = new PageNavBuildingEvent();
send_event($pnbe);
@ -426,6 +435,10 @@ class Page
}
break;
case PageMode::REDIRECT:
if ($this->flash) {
$this->redirect .= (strpos($this->redirect, "?") === false) ? "?" : "&";
$this->redirect .= "flash=" . url_escape(implode("\n", $this->flash));
}
header('Location: ' . $this->redirect);
print 'You should be redirected to <a href="' . $this->redirect . '">' . $this->redirect . '</a>';
break;

View file

@ -132,27 +132,6 @@ function get_session_ip(Config $config): string
}
/**
* Set (or extend) a flash-message cookie.
*
* This can optionally be done at the same time as saving a log message with log_*()
*
* Generally one should flash a message in onPageRequest and log a message wherever
* the action actually takes place (eg onWhateverElse) - but much of the time, actions
* are taken from within onPageRequest...
*/
function flash_message(string $text, string $type="info"): void
{
global $page;
$current = $page->get_cookie("flash_message");
if ($current) {
$text = $current . "\n" . $text;
}
# the message should be viewed pretty much immediately,
# so 60s timeout should be more than enough
$page->add_cookie("flash_message", $text, time()+60, "/");
}
/**
* A shorthand way to send a TextFormattingEvent and get the results.
*/

View file

@ -210,7 +210,7 @@ class Approval extends Extension
public function onBulkAction(BulkActionEvent $event)
{
global $user;
global $page, $user;
switch ($event->action) {
case "bulk_approve_image":
@ -220,7 +220,7 @@ class Approval extends Extension
self::approve_image($image->id);
$total++;
}
flash_message("Approved $total items");
$page->flash("Approved $total items");
}
break;
case "bulk_disapprove_image":
@ -230,7 +230,7 @@ class Approval extends Extension
self::disapprove_image($image->id);
$total++;
}
flash_message("Disapproved $total items");
$page->flash("Disapproved $total items");
}
break;
}

View file

@ -116,13 +116,13 @@ class BulkActions extends Extension
public function onBulkAction(BulkActionEvent $event)
{
global $user;
global $page, $user;
switch ($event->action) {
case "bulk_delete":
if ($user->can(Permissions::DELETE_IMAGE)) {
$i = $this->delete_items($event->items);
flash_message("Deleted $i items");
$page->flash("Deleted $i items");
}
break;
case "bulk_tag":
@ -137,7 +137,7 @@ class BulkActions extends Extension
}
$i= $this->tag_items($event->items, $tags, $replace);
flash_message("Tagged $i items");
$page->flash("Tagged $i items");
}
break;
case "bulk_source":
@ -147,7 +147,7 @@ class BulkActions extends Extension
if ($user->can(Permissions::BULK_EDIT_IMAGE_SOURCE)) {
$source = $_POST['bulk_source'];
$i = $this->set_source($event->items, $source);
flash_message("Set source for $i items");
$page->flash("Set source for $i items");
}
break;
}
@ -215,6 +215,7 @@ class BulkActions extends Extension
private function delete_items(iterable $items): int
{
global $page;
$total = 0;
foreach ($items as $image) {
try {
@ -227,7 +228,7 @@ class BulkActions extends Extension
send_event(new ImageDeletionEvent($image));
$total++;
} catch (Exception $e) {
flash_message("Error while removing {$image->id}: " . $e->getMessage(), "error");
$page->flash("Error while removing {$image->id}: " . $e->getMessage(), "error");
}
}
return $total;
@ -275,13 +276,14 @@ class BulkActions extends Extension
private function set_source(iterable $items, String $source): int
{
global $page;
$total = 0;
foreach ($items as $image) {
try {
send_event(new SourceSetEvent($image, $source));
$total++;
} catch (Exception $e) {
flash_message("Error while setting source for {$image->id}: " . $e->getMessage(), "error");
$page->flash("Error while setting source for {$image->id}: " . $e->getMessage(), "error");
}
}
return $total;

View file

@ -203,7 +203,7 @@ class CommentList extends Extension
// FIXME: post, not args
if ($event->count_args() === 3) {
send_event(new CommentDeletionEvent($event->get_arg(1)));
flash_message("Deleted comment");
$page->flash("Deleted comment");
$page->set_mode(PageMode::REDIRECT);
if (!empty($_SERVER['HTTP_REFERER'])) {
$page->set_redirect($_SERVER['HTTP_REFERER']);
@ -232,7 +232,7 @@ class CommentList extends Extension
foreach ($comment_ids as $cid) {
send_event(new CommentDeletionEvent($cid));
}
flash_message("Deleted $num comments");
$page->flash("Deleted $num comments");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("admin"));

View file

@ -107,6 +107,7 @@ class CronUploader extends Extension
private function restage_folder(string $folder)
{
global $page;
if (empty($folder)) {
throw new Exception("folder empty");
}
@ -122,7 +123,7 @@ class CronUploader extends Extension
$results = get_dir_contents($queue_dir);
if (count($results) > 0) {
flash_message("Queue folder must be empty to re-stage", "error");
$page->flash("Queue folder must be empty to re-stage", "error");
return;
}
@ -130,9 +131,9 @@ class CronUploader extends Extension
if (count($results) == 0) {
if (rmdir($stage_dir)===false) {
flash_message("Nothing to stage from $folder, cannot remove folder");
$page->flash("Nothing to stage from $folder, cannot remove folder");
} else {
flash_message("Nothing to stage from $folder, removing folder");
$page->flash("Nothing to stage from $folder, removing folder");
}
return;
}
@ -144,15 +145,16 @@ class CronUploader extends Extension
rename($original_path, $new_path);
}
flash_message("Re-staged $folder to queue");
$page->flash("Re-staged $folder to queue");
rmdir($stage_dir);
}
private function clear_folder($folder)
{
global $page;
$path = join_path(CronUploaderConfig::get_dir(), $folder);
deltree($path);
flash_message("Cleared $path");
$page->flash("Cleared $path");
}

View file

@ -165,7 +165,7 @@ class Favorites extends Extension
public function onBulkAction(BulkActionEvent $event)
{
global $user;
global $page, $user;
switch ($event->action) {
case "bulk_favorite":
@ -175,7 +175,7 @@ class Favorites extends Extension
send_event(new FavoriteSetEvent($image->id, $user, true));
$total++;
}
flash_message("Added $total items to favorites");
$page->flash("Added $total items to favorites");
}
break;
case "bulk_unfavorite":
@ -185,7 +185,7 @@ class Favorites extends Extension
send_event(new FavoriteSetEvent($image->id, $user, false));
$total++;
}
flash_message("Removed $total items from favorites");
$page->flash("Removed $total items from favorites");
}
break;
}

View file

@ -90,11 +90,11 @@ class ImageBan extends Extension
if ($hash) {
send_event(new AddImageHashBanEvent($hash, $reason));
flash_message("Image ban added");
$page->flash("Image ban added");
if ($image) {
send_event(new ImageDeletionEvent($image));
flash_message("Image deleted");
$page->flash("Image deleted");
}
$page->set_mode(PageMode::REDIRECT);
@ -104,7 +104,7 @@ class ImageBan extends Extension
$user->ensure_authed();
$input = validate_input(["d_hash"=>"string"]);
send_event(new RemoveImageHashBanEvent($input['d_hash']));
flash_message("Image ban removed");
$page->flash("Image ban removed");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect($_SERVER['HTTP_REFERER']);
} elseif ($event->get_arg(0) == "list") {

View file

@ -93,6 +93,8 @@ class IPBan extends Extension
{
global $cache, $config, $database, $page, $user, $_shm_user_classes;
$d = @$_GET['DEBUG'];
// Get lists of banned IPs and banned networks
$ips = $cache->get("ip_bans");
$networks = $cache->get("network_bans");
@ -130,6 +132,12 @@ class IPBan extends Extension
}
}
if ($d) {
print($remote);
print("\n");
print($active_ban_id);
print("\n");
}
// If an active ban is found, act on it
if (!is_null($active_ban_id)) {
$row = $database->get_row("SELECT * FROM bans WHERE id=:id", ["id"=>$active_ban_id]);
@ -181,14 +189,14 @@ class IPBan extends Extension
$user->ensure_authed();
$input = validate_input(["c_ip"=>"string", "c_mode"=>"string", "c_reason"=>"string", "c_expires"=>"optional,date"]);
send_event(new AddIPBanEvent($input['c_ip'], $input['c_mode'], $input['c_reason'], $input['c_expires']));
flash_message("Ban for {$input['c_ip']} added");
$page->flash("Ban for {$input['c_ip']} added");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("ip_ban/list"));
} elseif ($event->get_arg(0) == "delete") {
$user->ensure_authed();
$input = validate_input(["d_id"=>"int"]);
send_event(new RemoveIPBanEvent($input['d_id']));
flash_message("Ban removed");
$page->flash("Ban removed");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("ip_ban/list"));
} elseif ($event->get_arg(0) == "list") {

View file

@ -157,7 +157,7 @@ class Media extends Extension
public function onBulkAction(BulkActionEvent $event)
{
global $user;
global $page, $user;
switch ($event->action) {
case "bulk_media_rescan":
@ -172,7 +172,7 @@ class Media extends Extension
$failed++;
}
}
flash_message("Scanned media properties for $total items, failed for $failed");
$page->flash("Scanned media properties for $total items, failed for $failed");
}
break;
}

View file

@ -114,11 +114,11 @@ class NotATag extends Extension
$input = validate_input(["d_tag"=>"string"]);
$database->execute(
$database->scoreql_to_sql(
"DELETE FROM untags WHERE LOWER(tag) = LOWER(:tag)"
"DELETE FROM untags WHERE LOWER(tag) = LOWER(:tag)"
),
["tag"=>$input['d_tag']]
);
flash_message("Image ban removed");
$page->flash("Image ban removed");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect($_SERVER['HTTP_REFERER']);
} elseif ($event->get_arg(0) == "list") {

View file

@ -164,7 +164,7 @@ class PrivMsg extends Extension
$subject = $_POST["subject"];
$message = $_POST["message"];
send_event(new SendPMEvent(new PM($from_id, $_SERVER["REMOTE_ADDR"], $to_id, $subject, $message)));
flash_message("PM sent");
$page->flash("PM sent");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect($_SERVER["HTTP_REFERER"]);
}

View file

@ -357,7 +357,7 @@ class Ratings extends Extension
public function onBulkAction(BulkActionEvent $event)
{
global $user;
global $page, $user;
switch ($event->action) {
case "bulk_rate":
@ -371,7 +371,7 @@ class Ratings extends Extension
send_event(new RatingSetEvent($image, $rating));
$total++;
}
flash_message("Rating set for $total items");
$page->flash("Rating set for $total items");
}
break;
}

View file

@ -63,7 +63,7 @@ class RegenThumb extends Extension
public function onBulkAction(BulkActionEvent $event)
{
global $user;
global $page, $user;
switch ($event->action) {
case "bulk_regen":
@ -80,7 +80,7 @@ class RegenThumb extends Extension
$total++;
}
}
flash_message("Regenerated thumbnails for $total items");
$page->flash("Regenerated thumbnails for $total items");
}
break;
}
@ -93,6 +93,7 @@ class RegenThumb extends Extension
public function onAdminAction(AdminActionEvent $event)
{
global $page;
switch ($event->action) {
case "regen_thumbs":
$event->redirect = true;
@ -128,7 +129,7 @@ class RegenThumb extends Extension
break;
}
}
flash_message("Re-generated $i thumbnails");
$page->flash("Re-generated $i thumbnails");
break;
case "delete_thumbs":
$event->redirect = true;
@ -144,11 +145,11 @@ class RegenThumb extends Extension
$i++;
}
}
flash_message("Deleted $i thumbnails for ".$_POST["delete_thumb_type"]." images");
$page->flash("Deleted $i thumbnails for ".$_POST["delete_thumb_type"]." images");
} else {
$dir = "data/thumbs/";
$this->remove_dir_recursively($dir);
flash_message("Deleted all thumbnails");
$page->flash("Deleted all thumbnails");
}

View file

@ -108,7 +108,7 @@ class Rule34 extends Extension
if (preg_match_all("/([a-fA-F0-9]{32})/", $all, $matches)) {
$matches = $matches[0];
foreach ($matches as $hash) {
flash_message("Cleaning {$hash}");
$page->flash("Cleaning {$hash}");
if (strlen($hash) != 32) {
continue;
}

View file

@ -295,8 +295,7 @@ class Setup extends Extension
} elseif ($event->get_arg(0) == "save" && $user->check_auth_token()) {
send_event(new ConfigSaveEvent($config));
$config->save();
flash_message("Config saved");
$page->flash("Config saved");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("setup"));
} elseif ($event->get_arg(0) == "advanced") {

View file

@ -166,7 +166,7 @@ class TranscodeImage extends Extension
public function onBulkAction(BulkActionEvent $event)
{
global $user, $database;
global $user, $database, $page;
switch ($event->action) {
case self::ACTION_BULK_TRANSCODE:
@ -195,7 +195,7 @@ class TranscodeImage extends Extension
}
}
}
flash_message("Transcoded $total items");
$page->flash("Transcoded $total items");
}
break;
}

View file

@ -133,7 +133,7 @@ class Trash extends Extension
public function onBulkAction(BulkActionEvent $event)
{
global $user;
global $page, $user;
switch ($event->action) {
case "bulk_trash_restore":
@ -143,7 +143,7 @@ class Trash extends Extension
self::set_trash($image->id, false);
$total++;
}
flash_message("Restored $total items from trash");
$page->flash("Restored $total items from trash");
}
break;
}

View file

@ -563,11 +563,11 @@ class UserPage extends Extension
private function change_name_wrapper(User $duser, $name)
{
global $user;
global $page, $user;
if ($user->can(Permissions::EDIT_USER_NAME) && $this->user_can_edit_user($user, $duser)) {
$duser->set_name($name);
flash_message("Username changed");
$page->flash("Username changed");
// TODO: set login cookie if user changed themselves
$this->redirect_to_user($duser);
} else {
@ -577,7 +577,7 @@ class UserPage extends Extension
private function change_password_wrapper(User $duser, string $pass1, string $pass2)
{
global $user;
global $page, $user;
if ($this->user_can_edit_user($user, $duser)) {
if ($pass1 != $pass2) {
@ -590,7 +590,7 @@ class UserPage extends Extension
$this->set_login_cookie($duser->name, $pass1);
}
flash_message("Password changed");
$page->flash("Password changed");
$this->redirect_to_user($duser);
}
}
@ -598,23 +598,23 @@ class UserPage extends Extension
private function change_email_wrapper(User $duser, string $address)
{
global $user;
global $page, $user;
if ($this->user_can_edit_user($user, $duser)) {
$duser->set_email($address);
flash_message("Email changed");
$page->flash("Email changed");
$this->redirect_to_user($duser);
}
}
private function change_class_wrapper(User $duser, string $class)
{
global $user;
global $page, $user;
if ($user->class->name == "admin") {
$duser->set_class($class);
flash_message("Class changed");
$page->flash("Class changed");
$this->redirect_to_user($duser);
}
}

View file

@ -125,11 +125,7 @@ class Layout
$withleft = "noleft";
}
$flash = $page->get_cookie("flash_message");
$flash_html = "";
if ($flash) {
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
}
$flash_html = $page->flash ? "<b id='flash'>".nl2br(html_escape(implode("\n", $flash)))."</b>" : "";
print <<<EOD
<!doctype html>

View file

@ -125,11 +125,7 @@ class Layout
$withleft = "noleft";
}
$flash = $page->get_cookie("flash_message");
$flash_html = "";
if ($flash) {
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
}
$flash_html = $page->flash ? "<b id='flash'>".nl2br(html_escape(implode("\n", $flash)))."</b>" : "";
print <<<EOD
<!doctype html>

View file

@ -45,11 +45,7 @@ class Layout
$wrapper = ' style="height: 3em; overflow: auto;"';
}
$flash = $page->get_cookie("flash_message");
$flash_html = "";
if ($flash) {
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
}
$flash_html = $page->flash ? "<b id='flash'>".nl2br(html_escape(implode("\n", $flash)))."</b>" : "";
print <<<EOD
<!doctype html>

View file

@ -50,11 +50,7 @@ class Layout
$withleft = "";
}
$flash = $page->get_cookie("flash_message");
$flash_html = "";
if ($flash) {
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
}
$flash_html = $page->flash ? "<b id='flash'>".nl2br(html_escape(implode("\n", $flash)))."</b>" : "";
print <<<EOD
<!doctype html>

View file

@ -82,11 +82,7 @@ class Layout
$main_block_html = "<article>{$main_block_html}</article>";
}
$flash = $page->get_cookie("flash_message");
$flash_html = "";
if (!empty($flash)) {
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
}
$flash_html = $page->flash ? "<b id='flash'>".nl2br(html_escape(implode("\n", $flash)))."</b>" : "";
print <<<EOD
<!doctype html>

View file

@ -92,11 +92,7 @@ class Layout
}
*/
$flash = $page->get_cookie("flash_message");
$flash_html = "";
if ($flash) {
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
}
$flash_html = $page->flash ? "<b id='flash'>".nl2br(html_escape(implode("\n", $flash)))."</b>" : "";
print <<<EOD
<!doctype html>

View file

@ -54,11 +54,7 @@ class Layout
}
*/
$flash = $page->get_cookie("flash_message");
$flash_html = "";
if ($flash) {
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
}
$flash_html = $page->flash ? "<b id='flash'>".nl2br(html_escape(implode("\n", $flash)))."</b>" : "";
print <<<EOD
<!doctype html>