set cookies on Page object
This commit is contained in:
parent
eb246ef1ee
commit
83435e3266
11 changed files with 59 additions and 57 deletions
|
@ -132,6 +132,9 @@ class Page {
|
||||||
/** @var string[] */
|
/** @var string[] */
|
||||||
public $http_headers = array();
|
public $http_headers = array();
|
||||||
|
|
||||||
|
/** @var string[][] */
|
||||||
|
public $cookies = array();
|
||||||
|
|
||||||
/** @var Block[] */
|
/** @var Block[] */
|
||||||
public $blocks = array();
|
public $blocks = array();
|
||||||
|
|
||||||
|
@ -187,6 +190,31 @@ class Page {
|
||||||
$this->http_headers[$position] = $line;
|
$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.
|
* Get all the HTML headers that are currently set and return as a string.
|
||||||
* @return string
|
* @return string
|
||||||
|
@ -228,7 +256,12 @@ class Page {
|
||||||
header("X-Powered-By: SCore-".SCORE_VERSION);
|
header("X-Powered-By: SCore-".SCORE_VERSION);
|
||||||
|
|
||||||
if (!headers_sent()) {
|
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 {
|
} else {
|
||||||
print "Error: Headers have already been sent to the client.";
|
print "Error: Headers have already been sent to the client.";
|
||||||
}
|
}
|
||||||
|
@ -252,6 +285,9 @@ class Page {
|
||||||
# header("Cache-control: no-cache");
|
# header("Cache-control: no-cache");
|
||||||
# header('Expires: ' . gmdate('D, d M Y H:i:s', time() - 600) . ' GMT');
|
# 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");
|
usort($this->blocks, "blockcmp");
|
||||||
$this->add_auto_html_headers();
|
$this->add_auto_html_headers();
|
||||||
$layout = new Layout();
|
$layout = new Layout();
|
||||||
|
|
|
@ -777,35 +777,6 @@ function get_session_ip(Config $config) {
|
||||||
return $addr;
|
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.
|
* Set (or extend) a flash-message cookie.
|
||||||
|
@ -820,13 +791,14 @@ function set_prefixed_cookie($name, $value, $time, $path) {
|
||||||
* @param string $type
|
* @param string $type
|
||||||
*/
|
*/
|
||||||
function flash_message(/*string*/ $text, /*string*/ $type="info") {
|
function flash_message(/*string*/ $text, /*string*/ $type="info") {
|
||||||
$current = get_prefixed_cookie("flash_message");
|
global $page;
|
||||||
|
$current = $page->get_cookie("flash_message");
|
||||||
if($current) {
|
if($current) {
|
||||||
$text = $current . "\n" . $text;
|
$text = $current . "\n" . $text;
|
||||||
}
|
}
|
||||||
# the message should be viewed pretty much immediately,
|
# the message should be viewed pretty much immediately,
|
||||||
# so 60s timeout should be more than enough
|
# 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
|
* @return User
|
||||||
*/
|
*/
|
||||||
function _get_user() {
|
function _get_user() {
|
||||||
global $config;
|
global $config, $page;
|
||||||
$user = null;
|
$user = null;
|
||||||
if(get_prefixed_cookie("user") && get_prefixed_cookie("session")) {
|
if($page->get_cookie("user") && $page->get_cookie("session")) {
|
||||||
$tmp_user = User::by_session(get_prefixed_cookie("user"), get_prefixed_cookie("session"));
|
$tmp_user = User::by_session($page->get_cookie("user"), $page->get_cookie("session"));
|
||||||
if(!is_null($tmp_user)) {
|
if(!is_null($tmp_user)) {
|
||||||
$user = $tmp_user;
|
$user = $tmp_user;
|
||||||
}
|
}
|
||||||
|
|
|
@ -562,7 +562,7 @@ class CommentList extends Extension {
|
||||||
* @throws CommentPostingException
|
* @throws CommentPostingException
|
||||||
*/
|
*/
|
||||||
private function add_comment_wrapper(/*int*/ $image_id, User $user, /*string*/ $comment) {
|
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")) {
|
if(!$user->can("bypass_comment_checks")) {
|
||||||
// will raise an exception if anything is wrong
|
// will raise an exception if anything is wrong
|
||||||
|
@ -571,7 +571,7 @@ class CommentList extends Extension {
|
||||||
|
|
||||||
// all checks passed
|
// all checks passed
|
||||||
if($user->is_anonymous()) {
|
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(
|
$database->Execute(
|
||||||
"INSERT INTO comments(image_id, owner_id, owner_ip, posted, comment) ".
|
"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) {
|
private function comment_checks(/*int*/ $image_id, User $user, /*string*/ $comment) {
|
||||||
global $config;
|
global $config, $page;
|
||||||
|
|
||||||
// basic sanity checks
|
// basic sanity checks
|
||||||
if(!$user->can("create_comment")) {
|
if(!$user->can("create_comment")) {
|
||||||
|
@ -606,7 +606,7 @@ class CommentList extends Extension {
|
||||||
throw new CommentPostingException("Comment too repetitive~");
|
throw new CommentPostingException("Comment too repetitive~");
|
||||||
}
|
}
|
||||||
else if($user->is_anonymous() && !$this->hash_match()) {
|
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(
|
throw new CommentPostingException(
|
||||||
"Comment submission form is out of date; refresh the ".
|
"Comment submission form is out of date; refresh the ".
|
||||||
"comment form to show you aren't a spammer~");
|
"comment form to show you aren't a spammer~");
|
||||||
|
|
|
@ -392,7 +392,7 @@ class Pools extends Extension {
|
||||||
|
|
||||||
|
|
||||||
$order_by = "";
|
$order_by = "";
|
||||||
$order = get_prefixed_cookie("ui-order-pool");
|
$order = $page->get_cookie("ui-order-pool");
|
||||||
if($order == "created" || is_null($order)){
|
if($order == "created" || is_null($order)){
|
||||||
$order_by = "ORDER BY p.date DESC";
|
$order_by = "ORDER BY p.date DESC";
|
||||||
}elseif($order == "updated"){
|
}elseif($order == "updated"){
|
||||||
|
|
|
@ -165,11 +165,11 @@ class UserPage extends Extension {
|
||||||
$this->theme->display_user_list($page, User::by_list(0), $user);
|
$this->theme->display_user_list($page, User::by_list(0), $user);
|
||||||
}
|
}
|
||||||
else if($event->get_arg(0) == "logout") {
|
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) {
|
if(CACHE_HTTP || SPEED_HAX) {
|
||||||
# to keep as few versions of content as possible,
|
# to keep as few versions of content as possible,
|
||||||
# make cookies all-or-nothing
|
# 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");
|
log_info("user", "Logged out");
|
||||||
$page->set_mode("redirect");
|
$page->set_mode("redirect");
|
||||||
|
@ -476,14 +476,14 @@ class UserPage extends Extension {
|
||||||
* @param string $pass
|
* @param string $pass
|
||||||
*/
|
*/
|
||||||
private function set_login_cookie(/*string*/ $name, /*string*/ $pass) {
|
private function set_login_cookie(/*string*/ $name, /*string*/ $pass) {
|
||||||
global $config;
|
global $config, $page;
|
||||||
|
|
||||||
$addr = get_session_ip($config);
|
$addr = get_session_ip($config);
|
||||||
$hash = User::by_name($name)->passhash;
|
$hash = User::by_name($name)->passhash;
|
||||||
|
|
||||||
set_prefixed_cookie("user", $name,
|
$page->add_cookie("user", $name,
|
||||||
time()+60*60*24*365, '/');
|
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'), '/');
|
time()+60*60*24*$config->get_int('login_memory'), '/');
|
||||||
}
|
}
|
||||||
//}}}
|
//}}}
|
||||||
|
|
|
@ -189,11 +189,10 @@ class Layout {
|
||||||
$withleft = "noleft";
|
$withleft = "noleft";
|
||||||
}
|
}
|
||||||
|
|
||||||
$flash = get_prefixed_cookie("flash_message");
|
$flash = $page->get_cookie("flash_message");
|
||||||
$flash_html = "";
|
$flash_html = "";
|
||||||
if($flash) {
|
if($flash) {
|
||||||
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
|
$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
|
print <<<EOD
|
||||||
|
|
|
@ -215,11 +215,10 @@ class Layout {
|
||||||
$withleft = "noleft";
|
$withleft = "noleft";
|
||||||
}
|
}
|
||||||
|
|
||||||
$flash = get_prefixed_cookie("flash_message");
|
$flash = $page->get_cookie("flash_message");
|
||||||
$flash_html = "";
|
$flash_html = "";
|
||||||
if($flash) {
|
if($flash) {
|
||||||
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
|
$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
|
print <<<EOD
|
||||||
|
|
|
@ -49,11 +49,10 @@ class Layout {
|
||||||
$wrapper = ' style="height: 3em; overflow: auto;"';
|
$wrapper = ' style="height: 3em; overflow: auto;"';
|
||||||
}
|
}
|
||||||
|
|
||||||
$flash = get_prefixed_cookie("flash_message");
|
$flash = $page->get_cookie("flash_message");
|
||||||
$flash_html = "";
|
$flash_html = "";
|
||||||
if($flash) {
|
if($flash) {
|
||||||
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
|
$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
|
print <<<EOD
|
||||||
|
|
|
@ -55,11 +55,10 @@ class Layout {
|
||||||
$withleft = "";
|
$withleft = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
$flash = get_prefixed_cookie("flash_message");
|
$flash = $page->get_cookie("flash_message");
|
||||||
$flash_html = "";
|
$flash_html = "";
|
||||||
if($flash) {
|
if($flash) {
|
||||||
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
|
$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
|
print <<<EOD
|
||||||
|
|
|
@ -167,11 +167,10 @@ class Layout {
|
||||||
$main_block_html = "<article>{$main_block_html}</article>";
|
$main_block_html = "<article>{$main_block_html}</article>";
|
||||||
}
|
}
|
||||||
|
|
||||||
$flash = get_prefixed_cookie("flash_message");
|
$flash = $page->get_cookie("flash_message");
|
||||||
$flash_html = "";
|
$flash_html = "";
|
||||||
if($flash) {
|
if($flash) {
|
||||||
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
|
$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
|
print <<<EOD
|
||||||
|
|
|
@ -57,11 +57,10 @@ class Layout {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$flash = get_prefixed_cookie("flash_message");
|
$flash = $page->get_cookie("flash_message");
|
||||||
$flash_html = "";
|
$flash_html = "";
|
||||||
if($flash) {
|
if($flash) {
|
||||||
$flash_html = "<b id='flash'>".nl2br(html_escape($flash))." <a href='#' onclick=\"\$('#flash').hide(); return false;\">[X]</a></b>";
|
$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
|
print <<<EOD
|
||||||
|
|
Reference in a new issue