set cookies on Page object

This commit is contained in:
Shish 2015-08-03 14:32:46 +01:00
parent eb246ef1ee
commit 83435e3266
11 changed files with 59 additions and 57 deletions

View file

@ -132,6 +132,9 @@ class Page {
/** @var string[] */
public $http_headers = array();
/** @var string[][] */
public $cookies = array();
/** @var Block[] */
public $blocks = array();
@ -187,6 +190,31 @@ class Page {
$this->http_headers[$position] = $line;
}
/**
* The counterpart for get_cookie, this works like php's
* setcookie method, but prepends the site-wide cookie prefix to
* the $name argument before doing anything.
*
* @param string $name
* @param string $value
* @param int $time
* @param string $path
*/
public function add_cookie($name, $value, $time, $path) {
$full_name = COOKIE_PREFIX."_".$name;
$this->cookies[] = array($full_name, $value, $time, $path);
}
public function get_cookie(/*string*/ $name) {
$full_name = COOKIE_PREFIX."_".$name;
if(isset($_COOKIE[$full_name])) {
return $_COOKIE[$full_name];
}
else {
return null;
}
}
/**
* Get all the HTML headers that are currently set and return as a string.
* @return string
@ -228,7 +256,12 @@ class Page {
header("X-Powered-By: SCore-".SCORE_VERSION);
if (!headers_sent()) {
foreach($this->http_headers as $head){ header($head); }
foreach($this->http_headers as $head) {
header($head);
}
foreach($this->cookies as $c) {
setcookie($c[0], $c[1], $c[2], $c[3]);
}
} else {
print "Error: Headers have already been sent to the client.";
}
@ -252,6 +285,9 @@ 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")) {
$this->add_cookie("flash_message", "", -1, "/");
}
usort($this->blocks, "blockcmp");
$this->add_auto_html_headers();
$layout = new Layout();

View file

@ -777,35 +777,6 @@ function get_session_ip(Config $config) {
return $addr;
}
/**
* similar to $_COOKIE[$name], but $name has the site-wide cookie
* prefix prepended to it, eg username -> shm_username, to prevent
* conflicts from multiple installs within one domain.
*/
function get_prefixed_cookie(/*string*/ $name) {
$full_name = COOKIE_PREFIX."_".$name;
if(isset($_COOKIE[$full_name])) {
return $_COOKIE[$full_name];
}
else {
return null;
}
}
/**
* The counterpart for get_prefixed_cookie, this works like php's
* setcookie method, but prepends the site-wide cookie prefix to
* the $name argument before doing anything.
*
* @param string $name
* @param string $value
* @param int $time
* @param string $path
*/
function set_prefixed_cookie($name, $value, $time, $path) {
$full_name = COOKIE_PREFIX."_".$name;
setcookie($full_name, $value, $time, $path);
}
/**
* Set (or extend) a flash-message cookie.
@ -820,13 +791,14 @@ function set_prefixed_cookie($name, $value, $time, $path) {
* @param string $type
*/
function flash_message(/*string*/ $text, /*string*/ $type="info") {
$current = get_prefixed_cookie("flash_message");
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
set_prefixed_cookie("flash_message", $text, time()+60, "/");
$page->add_cookie("flash_message", $text, time()+60, "/");
}
/**
@ -1628,10 +1600,10 @@ function _decaret($str) {
* @return User
*/
function _get_user() {
global $config;
global $config, $page;
$user = null;
if(get_prefixed_cookie("user") && get_prefixed_cookie("session")) {
$tmp_user = User::by_session(get_prefixed_cookie("user"), get_prefixed_cookie("session"));
if($page->get_cookie("user") && $page->get_cookie("session")) {
$tmp_user = User::by_session($page->get_cookie("user"), $page->get_cookie("session"));
if(!is_null($tmp_user)) {
$user = $tmp_user;
}

View file

@ -562,7 +562,7 @@ class CommentList extends Extension {
* @throws CommentPostingException
*/
private function add_comment_wrapper(/*int*/ $image_id, User $user, /*string*/ $comment) {
global $database, $config;
global $database, $config, $page;
if(!$user->can("bypass_comment_checks")) {
// will raise an exception if anything is wrong
@ -571,7 +571,7 @@ class CommentList extends Extension {
// all checks passed
if($user->is_anonymous()) {
set_prefixed_cookie("nocache", "Anonymous Commenter", time()+60*60*24, "/");
$page->add_cookie("nocache", "Anonymous Commenter", time()+60*60*24, "/");
}
$database->Execute(
"INSERT INTO comments(image_id, owner_id, owner_ip, posted, comment) ".
@ -585,7 +585,7 @@ class CommentList extends Extension {
}
private function comment_checks(/*int*/ $image_id, User $user, /*string*/ $comment) {
global $config;
global $config, $page;
// basic sanity checks
if(!$user->can("create_comment")) {
@ -606,7 +606,7 @@ class CommentList extends Extension {
throw new CommentPostingException("Comment too repetitive~");
}
else if($user->is_anonymous() && !$this->hash_match()) {
set_prefixed_cookie("nocache", "Anonymous Commenter", time()+60*60*24, "/");
$page->add_cookie("nocache", "Anonymous Commenter", time()+60*60*24, "/");
throw new CommentPostingException(
"Comment submission form is out of date; refresh the ".
"comment form to show you aren't a spammer~");

View file

@ -392,7 +392,7 @@ class Pools extends Extension {
$order_by = "";
$order = get_prefixed_cookie("ui-order-pool");
$order = $page->get_cookie("ui-order-pool");
if($order == "created" || is_null($order)){
$order_by = "ORDER BY p.date DESC";
}elseif($order == "updated"){

View file

@ -165,11 +165,11 @@ class UserPage extends Extension {
$this->theme->display_user_list($page, User::by_list(0), $user);
}
else if($event->get_arg(0) == "logout") {
set_prefixed_cookie("session", "", time()+60*60*24*$config->get_int('login_memory'), "/");
$page->add_cookie("session", "", time()+60*60*24*$config->get_int('login_memory'), "/");
if(CACHE_HTTP || SPEED_HAX) {
# to keep as few versions of content as possible,
# make cookies all-or-nothing
set_prefixed_cookie("user", "", time()+60*60*24*$config->get_int('login_memory'), "/");
$page->add_cookie("user", "", time()+60*60*24*$config->get_int('login_memory'), "/");
}
log_info("user", "Logged out");
$page->set_mode("redirect");
@ -476,14 +476,14 @@ class UserPage extends Extension {
* @param string $pass
*/
private function set_login_cookie(/*string*/ $name, /*string*/ $pass) {
global $config;
global $config, $page;
$addr = get_session_ip($config);
$hash = User::by_name($name)->passhash;
set_prefixed_cookie("user", $name,
$page->add_cookie("user", $name,
time()+60*60*24*365, '/');
set_prefixed_cookie("session", md5($hash.$addr),
$page->add_cookie("session", md5($hash.$addr),
time()+60*60*24*$config->get_int('login_memory'), '/');
}
//}}}

View file

@ -189,11 +189,10 @@ class Layout {
$withleft = "noleft";
}
$flash = get_prefixed_cookie("flash_message");
$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>";
set_prefixed_cookie("flash_message", "", -1, "/");
}
print <<<EOD

View file

@ -215,11 +215,10 @@ class Layout {
$withleft = "noleft";
}
$flash = get_prefixed_cookie("flash_message");
$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>";
set_prefixed_cookie("flash_message", "", -1, "/");
}
print <<<EOD

View file

@ -49,11 +49,10 @@ class Layout {
$wrapper = ' style="height: 3em; overflow: auto;"';
}
$flash = get_prefixed_cookie("flash_message");
$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>";
set_prefixed_cookie("flash_message", "", -1, "/");
}
print <<<EOD

View file

@ -55,11 +55,10 @@ class Layout {
$withleft = "";
}
$flash = get_prefixed_cookie("flash_message");
$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>";
set_prefixed_cookie("flash_message", "", -1, "/");
}
print <<<EOD

View file

@ -167,11 +167,10 @@ class Layout {
$main_block_html = "<article>{$main_block_html}</article>";
}
$flash = get_prefixed_cookie("flash_message");
$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>";
set_prefixed_cookie("flash_message", "", -1, "/");
}
print <<<EOD

View file

@ -57,11 +57,10 @@ class Layout {
}
*/
$flash = get_prefixed_cookie("flash_message");
$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>";
set_prefixed_cookie("flash_message", "", -1, "/");
}
print <<<EOD