This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/core/page.php

366 lines
10 KiB
PHP
Raw Normal View History

<?php
2009-07-19 07:38:13 +00:00
/**
2009-07-21 03:18:40 +00:00
* \page themes Themes
2015-08-23 15:09:52 +00:00
*
2009-07-21 03:18:40 +00:00
* Each extension has a theme with a specific name -- eg. the extension Setup
* which is stored in ext/setup/main.php will have a theme called SetupTheme
* stored in ext/setup/theme.php. If you want to customise it, create a class
* in the file themes/mytheme/setup.theme.php called CustomSetupTheme which
* extends SetupTheme and overrides some of its methods.
2015-08-23 15:09:52 +00:00
*
2009-07-21 03:18:40 +00:00
* Generally an extension should only deal with processing data; whenever it
2010-01-12 15:01:34 +00:00
* wants to display something, it should pass the data to be displayed to the
* theme object, and the theme will add the data into the global $page
* structure.
*
* A page should make sure that all the data it outputs is free from dangerous
* data by using html_escape(), url_escape(), or int_escape() as appropriate.
*
* Because some HTML can be placed anywhere according to the theme, coming up
* with the correct way to link to a page can be hard -- thus we have the
* make_link() function, which will take a path like "post/list" and turn it
* into a full and correct link, eg /myboard/post/list, /foo/index.php?q=post/list,
* etc depending on how things are set up. This should always be used to link
* to pages rather than hardcoding a path.
*
* Various other common functions are available as part of the Themelet class.
2009-07-19 07:38:13 +00:00
*/
2009-07-21 03:18:40 +00:00
2009-07-19 07:38:13 +00:00
/**
2014-04-29 05:33:03 +00:00
* Class Page
*
2009-07-19 07:38:13 +00:00
* A data structure for holding all the bits of data that make up a page.
*
* The various extensions all add whatever they want to this structure,
2014-04-29 05:33:03 +00:00
* then Layout turns it into HTML.
2009-07-19 07:38:13 +00:00
*/
class Page {
2010-01-12 15:01:34 +00:00
/** @name Overall */
//@{
/** @var string */
public $mode = "page";
/** @var string */
public $type = "text/html; charset=utf-8";
2009-07-19 07:38:13 +00:00
/**
2010-01-12 15:01:34 +00:00
* Set what this page should do; "page", "data", or "redirect".
2009-07-19 07:38:13 +00:00
*/
2017-09-19 17:55:43 +00:00
public function set_mode(string $mode) {
$this->mode = $mode;
}
2009-01-04 19:18:37 +00:00
2009-07-19 07:38:13 +00:00
/**
* Set the page's MIME type.
2009-07-19 07:38:13 +00:00
*/
2017-09-19 17:55:43 +00:00
public function set_type(string $type) {
$this->type = $type;
}
2009-01-04 19:18:37 +00:00
2010-01-12 15:01:34 +00:00
//@}
// ==============================================
2010-01-12 15:01:34 +00:00
/** @name "data" mode */
//@{
2015-08-23 15:09:52 +00:00
/** @var string; public only for unit test */
public $data = "";
2015-08-23 15:09:52 +00:00
/** @var string; public only for unit test */
public $filename = null;
2009-07-19 07:38:13 +00:00
/**
* Set the raw data to be sent.
2009-07-19 07:38:13 +00:00
*/
2017-09-19 17:55:43 +00:00
public function set_data(string $data) {
$this->data = $data;
}
2009-01-04 19:18:37 +00:00
2009-07-19 07:38:13 +00:00
/**
* Set the recommended download filename.
2009-07-19 07:38:13 +00:00
*/
2017-09-19 17:55:43 +00:00
public function set_filename(string $filename) {
$this->filename = $filename;
}
2010-01-12 15:01:34 +00:00
//@}
// ==============================================
2010-01-12 15:01:34 +00:00
/** @name "redirect" mode */
//@{
/** @var string */
private $redirect = "";
2009-07-19 07:38:13 +00:00
/**
2010-01-12 15:01:34 +00:00
* Set the URL to redirect to (remember to use make_link() if linking
* to a page in the same site).
2009-07-19 07:38:13 +00:00
*/
2017-09-19 17:55:43 +00:00
public function set_redirect(string $redirect) {
$this->redirect = $redirect;
}
2010-01-12 15:01:34 +00:00
//@}
// ==============================================
2010-01-12 15:01:34 +00:00
/** @name "page" mode */
//@{
2015-08-02 18:07:11 +00:00
/** @var int */
public $code = 200;
/** @var string */
public $title = "";
/** @var string */
public $heading = "";
/** @var string */
public $subheading = "";
/** @var string */
public $quicknav = "";
/** @var string[] */
public $html_headers = array();
/** @var string[] */
public $http_headers = array();
2015-08-03 13:32:46 +00:00
/** @var string[][] */
public $cookies = array();
/** @var Block[] */
public $blocks = array();
2015-08-02 18:07:11 +00:00
/**
* Set the HTTP status code
*/
2019-05-28 16:31:20 +00:00
public function set_code(int $code): void {
2015-08-02 18:07:11 +00:00
$this->code = $code;
}
2019-05-28 16:31:20 +00:00
public function set_title(string $title): void {
$this->title = $title;
}
2019-05-28 16:31:20 +00:00
public function set_heading(string $heading): void {
$this->heading = $heading;
}
2019-05-28 16:31:20 +00:00
public function set_subheading(string $subheading): void {
$this->subheading = $subheading;
}
2009-07-19 07:38:13 +00:00
/**
* Add a line to the HTML head section.
2009-07-19 07:38:13 +00:00
*/
2019-05-28 16:31:20 +00:00
public function add_html_header(string $line, int $position=50): void {
while(isset($this->html_headers[$position])) $position++;
$this->html_headers[$position] = $line;
}
/**
* Add a http header to be sent to the client.
*/
2019-05-28 16:31:20 +00:00
public function add_http_header(string $line, int $position=50): void {
while(isset($this->http_headers[$position])) $position++;
$this->http_headers[$position] = $line;
}
2015-08-03 13:32:46 +00:00
/**
* 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.
*/
2019-05-28 16:31:20 +00:00
public function add_cookie(string $name, string $value, int $time, string $path): void {
2015-08-03 13:32:46 +00:00
$full_name = COOKIE_PREFIX."_".$name;
$this->cookies[] = array($full_name, $value, $time, $path);
}
2019-05-28 16:31:20 +00:00
public function get_cookie(string $name): ?string {
2015-08-03 13:32:46 +00:00
$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.
*/
2017-09-19 17:55:43 +00:00
public function get_all_html_headers(): string {
$data = '';
2016-06-18 11:25:54 +00:00
ksort($this->html_headers);
foreach ($this->html_headers as $line) {
2016-06-18 11:25:54 +00:00
$data .= "\t\t" . $line . "\n";
}
return $data;
}
2015-08-23 15:09:52 +00:00
/**
* Removes all currently set HTML headers (Be careful..).
*/
2019-05-28 16:31:20 +00:00
public function delete_all_html_headers(): void {
$this->html_headers = array();
}
2009-07-19 07:38:13 +00:00
/**
* Add a Block of data to the page.
2009-07-19 07:38:13 +00:00
*/
2010-01-12 15:01:34 +00:00
public function add_block(Block $block) {
$this->blocks[] = $block;
}
2010-01-12 15:01:34 +00:00
//@}
// ==============================================
2009-01-04 19:18:37 +00:00
2009-07-19 07:38:13 +00:00
/**
* Display the page according to the mode and data given.
2009-07-19 07:38:13 +00:00
*/
2019-05-28 16:31:20 +00:00
public function display(): void {
global $page, $user;
2015-08-23 15:09:52 +00:00
header("HTTP/1.0 {$this->code} Shimmie");
header("Content-type: ".$this->type);
header("X-Powered-By: SCore-".SCORE_VERSION);
if (!headers_sent()) {
2015-08-03 13:32:46 +00:00
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.";
}
switch($this->mode) {
case "page":
if(CACHE_HTTP) {
2012-02-07 10:17:37 +00:00
header("Vary: Cookie, Accept-Encoding");
if($user->is_anonymous() && $_SERVER["REQUEST_METHOD"] == "GET") {
header("Cache-control: public, max-age=600");
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 600) . ' GMT');
}
else {
#header("Cache-control: private, max-age=0");
header("Cache-control: no-cache");
header('Expires: ' . gmdate('D, d M Y H:i:s', time() - 600) . ' GMT');
}
}
#else {
# header("Cache-control: no-cache");
# header('Expires: ' . gmdate('D, d M Y H:i:s', time() - 600) . ' GMT');
#}
2016-07-03 11:07:54 +00:00
if($this->get_cookie("flash_message") !== null) {
2015-08-03 13:32:46 +00:00
$this->add_cookie("flash_message", "", -1, "/");
}
usort($this->blocks, "blockcmp");
$this->add_auto_html_headers();
$layout = new Layout();
$layout->display_page($page);
break;
case "data":
2009-12-26 01:00:38 +00:00
header("Content-Length: ".strlen($this->data));
if(!is_null($this->filename)) {
header('Content-Disposition: attachment; filename='.$this->filename);
}
print $this->data;
break;
case "redirect":
header('Location: '.$this->redirect);
2012-01-13 01:30:26 +00:00
print 'You should be redirected to <a href="'.$this->redirect.'">'.$this->redirect.'</a>';
break;
default:
print "Invalid page mode";
break;
}
}
2015-08-23 15:09:52 +00:00
/**
* This function grabs all the CSS and JavaScript files sprinkled throughout Shimmie's folders,
* concatenates them together into two large files (one for CSS and one for JS) and then stores
* them in the /cache/ directory for serving to the user.
2015-08-23 15:09:52 +00:00
*
* Why do this? Two reasons:
* 1. Reduces the number of files the user's browser needs to download.
* 2. Allows these cached files to be compressed/minified by the admin.
2015-08-23 15:09:52 +00:00
*
* TODO: This should really be configurable somehow...
*/
2019-05-28 16:31:20 +00:00
public function add_auto_html_headers(): void {
global $config;
$data_href = get_base_href();
$theme_name = $config->get_string('theme', 'default');
$this->add_html_header("<script type='text/javascript'>base_href = '$data_href';</script>", 40);
2018-11-11 17:38:32 +00:00
# static handler will map these to themes/foo/static/bar.ico or ext/handle_static/static/bar.ico
$this->add_html_header("<link rel='icon' type='image/x-icon' href='$data_href/favicon.ico'>", 41);
$this->add_html_header("<link rel='apple-touch-icon' href='$data_href/apple-touch-icon.png'>", 42);
//We use $config_latest to make sure cache is reset if config is ever updated.
$config_latest = 0;
foreach(zglob("data/config/*") as $conf) {
$config_latest = max($config_latest, filemtime($conf));
}
/*** Generate CSS cache files ***/
$css_latest = $config_latest;
$css_files = array_merge(
zglob("ext/{".ENABLED_EXTS."}/style.css"),
2018-11-10 12:02:48 +00:00
zglob("themes/$theme_name/style.css")
);
foreach($css_files as $css) {
$css_latest = max($css_latest, filemtime($css));
}
$css_md5 = md5(serialize($css_files));
$css_cache_file = data_path("cache/style/{$theme_name}.{$css_latest}.{$css_md5}.css");
if(!file_exists($css_cache_file)) {
$css_data = "";
foreach($css_files as $file) {
$file_data = file_get_contents($file);
$pattern = '/url[\s]*\([\s]*["\']?([^"\'\)]+)["\']?[\s]*\)/';
$replace = 'url("../../../'.dirname($file).'/$1")';
$file_data = preg_replace($pattern, $replace, $file_data);
$css_data .= $file_data . "\n";
}
2012-03-22 12:15:03 +00:00
file_put_contents($css_cache_file, $css_data);
}
2018-11-11 17:41:28 +00:00
$this->add_html_header("<link rel='stylesheet' href='$data_href/$css_cache_file' type='text/css'>", 43);
/*** Generate JS cache files ***/
$js_latest = $config_latest;
$js_files = array_merge(
[
"vendor/bower-asset/jquery/dist/jquery.min.js",
"vendor/bower-asset/jquery-timeago/jquery.timeago.js",
"vendor/bower-asset/tablesorter/jquery.tablesorter.min.js",
"vendor/bower-asset/js-cookie/src/js.cookie.js",
2018-11-11 17:38:32 +00:00
"ext/handle_static/modernizr-3.3.1.custom.js",
],
zglob("ext/{".ENABLED_EXTS."}/script.js"),
zglob("themes/$theme_name/script.js")
);
foreach($js_files as $js) {
$js_latest = max($js_latest, filemtime($js));
2012-03-09 22:27:12 +00:00
}
$js_md5 = md5(serialize($js_files));
$js_cache_file = data_path("cache/script/{$theme_name}.{$js_latest}.{$js_md5}.js");
if(!file_exists($js_cache_file)) {
$js_data = "";
foreach($js_files as $file) {
$js_data .= file_get_contents($file) . "\n";
}
2012-03-22 12:15:03 +00:00
file_put_contents($js_cache_file, $js_data);
}
2018-11-11 17:41:28 +00:00
$this->add_html_header("<script src='$data_href/$js_cache_file' type='text/javascript'></script>", 44);
}
}