2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-07-21 03:18:40 +00:00
|
|
|
* \page themes Themes
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
2009-07-21 06:36:12 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
/**
|
|
|
|
* 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,
|
2009-07-21 06:36:12 +00:00
|
|
|
* then Layout turns it into HTML
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2009-07-21 06:36:12 +00:00
|
|
|
class Page {
|
2010-01-12 15:01:34 +00:00
|
|
|
/** @name Overall */
|
|
|
|
//@{
|
2014-04-27 19:33:57 +00:00
|
|
|
/** @var string */
|
|
|
|
public $mode = "page";
|
|
|
|
/** @var string */
|
|
|
|
public $type = "text/html; charset=utf-8";
|
2007-04-16 11:58:25 +00:00
|
|
|
|
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".
|
2014-04-27 19:33:57 +00:00
|
|
|
* @param string $mode
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
public function set_mode($mode) {
|
|
|
|
$this->mode = $mode;
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 19:33:57 +00:00
|
|
|
* Set the page's MIME type.
|
|
|
|
* @param string $type
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
public function set_type($type) {
|
|
|
|
$this->type = $type;
|
|
|
|
}
|
|
|
|
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2010-01-12 15:01:34 +00:00
|
|
|
//@}
|
2007-04-16 11:58:25 +00:00
|
|
|
// ==============================================
|
2010-01-12 15:01:34 +00:00
|
|
|
/** @name "data" mode */
|
|
|
|
//@{
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2014-04-27 19:33:57 +00:00
|
|
|
/** @var string */
|
|
|
|
private $data = "";
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $filename = null;
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 19:33:57 +00:00
|
|
|
* Set the raw data to be sent.
|
|
|
|
* @param string $data
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
public function set_data($data) {
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 19:33:57 +00:00
|
|
|
* Set the recommended download filename.
|
|
|
|
* @param string $filename
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2008-05-25 23:02:17 +00:00
|
|
|
public function set_filename($filename) {
|
|
|
|
$this->filename = $filename;
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
|
|
|
|
2010-01-12 15:01:34 +00:00
|
|
|
//@}
|
2007-04-16 11:58:25 +00:00
|
|
|
// ==============================================
|
2010-01-12 15:01:34 +00:00
|
|
|
/** @name "redirect" mode */
|
|
|
|
//@{
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2014-04-27 19:33:57 +00:00
|
|
|
/** @var string */
|
|
|
|
private $redirect = "";
|
2007-04-16 11:58:25 +00:00
|
|
|
|
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
|
2014-04-27 19:33:57 +00:00
|
|
|
* to a page in the same site).
|
|
|
|
* @param string $redirect
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
public function set_redirect($redirect) {
|
|
|
|
$this->redirect = $redirect;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-12 15:01:34 +00:00
|
|
|
//@}
|
2007-04-16 11:58:25 +00:00
|
|
|
// ==============================================
|
2010-01-12 15:01:34 +00:00
|
|
|
/** @name "page" mode */
|
|
|
|
//@{
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2014-04-27 19:33:57 +00:00
|
|
|
/** @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();
|
|
|
|
|
|
|
|
/** @var Block[] */
|
|
|
|
public $blocks = array();
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 19:33:57 +00:00
|
|
|
* Set the window title.
|
|
|
|
* @param string $title
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
public function set_title($title) {
|
|
|
|
$this->title = $title;
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 19:33:57 +00:00
|
|
|
* Set the main heading.
|
|
|
|
* @param string $heading
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
public function set_heading($heading) {
|
|
|
|
$this->heading = $heading;
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 19:33:57 +00:00
|
|
|
* Set the sub heading.
|
|
|
|
* @param string $subheading
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
public function set_subheading($subheading) {
|
|
|
|
$this->subheading = $subheading;
|
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 19:33:57 +00:00
|
|
|
* Add a line to the HTML head section.
|
|
|
|
* @param string $line
|
|
|
|
* @param int $position
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2011-08-28 04:31:30 +00:00
|
|
|
public function add_html_header($line, $position=50) {
|
|
|
|
while(isset($this->html_headers[$position])) $position++;
|
|
|
|
$this->html_headers[$position] = $line;
|
2007-05-01 12:41:44 +00:00
|
|
|
}
|
2014-04-27 19:33:57 +00:00
|
|
|
|
2011-08-30 17:14:03 +00:00
|
|
|
/**
|
|
|
|
* Add a http header to be sent to the client.
|
2014-04-27 19:33:57 +00:00
|
|
|
* @param string $line
|
|
|
|
* @param int $position
|
2011-08-30 17:14:03 +00:00
|
|
|
*/
|
2011-08-30 20:24:10 +00:00
|
|
|
public function add_http_header($line, $position=50) {
|
|
|
|
while(isset($this->http_headers[$position])) $position++;
|
|
|
|
$this->http_headers[$position] = $line;
|
2011-08-30 17:14:03 +00:00
|
|
|
}
|
2014-04-27 19:33:57 +00:00
|
|
|
|
2011-09-25 17:40:34 +00:00
|
|
|
/**
|
|
|
|
* Get all the HTML headers that are currently set and return as a string.
|
2014-04-27 19:33:57 +00:00
|
|
|
* @return string
|
2011-09-25 17:40:34 +00:00
|
|
|
*/
|
|
|
|
public function get_all_html_headers() {
|
|
|
|
$data = '';
|
|
|
|
foreach ($this->html_headers as $line) {
|
|
|
|
$data .= $line . "\n";
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-27 19:33:57 +00:00
|
|
|
* Removes all currently set HTML headers (Be careful..).
|
2011-09-25 17:40:34 +00:00
|
|
|
*/
|
|
|
|
public function delete_all_html_headers() {
|
|
|
|
$this->html_headers = array();
|
|
|
|
}
|
2014-04-27 19:33:57 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 19:33:57 +00:00
|
|
|
* Add a Block of data to the page.
|
|
|
|
* @param Block $block
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2010-01-12 15:01:34 +00:00
|
|
|
public function add_block(Block $block) {
|
2007-06-30 01:19:11 +00:00
|
|
|
$this->blocks[] = $block;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2010-01-12 15:01:34 +00:00
|
|
|
|
|
|
|
//@}
|
2007-04-16 11:58:25 +00:00
|
|
|
// ==============================================
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 19:33:57 +00:00
|
|
|
* Display the page according to the mode and data given.
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2009-05-11 14:04:33 +00:00
|
|
|
public function display() {
|
2011-12-27 19:23:37 +00:00
|
|
|
global $page, $user;
|
2011-08-30 20:24:10 +00:00
|
|
|
|
2011-09-12 01:26:23 +00:00
|
|
|
header("Content-type: ".$this->type);
|
|
|
|
header("X-Powered-By: SCore-".SCORE_VERSION);
|
2011-08-30 20:24:10 +00:00
|
|
|
|
|
|
|
if (!headers_sent()) {
|
2011-09-04 18:24:14 +00:00
|
|
|
foreach($this->http_headers as $head){ header($head); }
|
2011-08-30 20:24:10 +00:00
|
|
|
} else {
|
|
|
|
print "Error: Headers have already been sent to the client.";
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
|
|
|
switch($this->mode) {
|
|
|
|
case "page":
|
2011-12-27 19:23:37 +00:00
|
|
|
if(CACHE_HTTP) {
|
2012-02-07 10:17:37 +00:00
|
|
|
header("Vary: Cookie, Accept-Encoding");
|
2011-12-27 19:23:37 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
2012-01-26 16:53:59 +00:00
|
|
|
#else {
|
|
|
|
# header("Cache-control: no-cache");
|
|
|
|
# header('Expires: ' . gmdate('D, d M Y H:i:s', time() - 600) . ' GMT');
|
|
|
|
#}
|
2007-06-30 01:19:11 +00:00
|
|
|
usort($this->blocks, "blockcmp");
|
2011-08-28 04:31:30 +00:00
|
|
|
$this->add_auto_html_headers();
|
2007-06-30 01:19:11 +00:00
|
|
|
$layout = new Layout();
|
2009-05-11 14:04:33 +00:00
|
|
|
$layout->display_page($page);
|
2007-04-16 11:58:25 +00:00
|
|
|
break;
|
|
|
|
case "data":
|
2009-12-26 01:00:38 +00:00
|
|
|
header("Content-Length: ".strlen($this->data));
|
2008-05-25 23:02:17 +00:00
|
|
|
if(!is_null($this->filename)) {
|
|
|
|
header('Content-Disposition: attachment; filename='.$this->filename);
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
print $this->data;
|
|
|
|
break;
|
|
|
|
case "redirect":
|
2012-01-12 20:46:34 +00:00
|
|
|
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>';
|
2007-04-16 11:58:25 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
print "Invalid page mode";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-09-12 01:24:18 +00:00
|
|
|
|
2013-11-28 05:31:46 +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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* TODO: This should really be configurable somehow...
|
|
|
|
*/
|
2011-08-28 04:31:30 +00:00
|
|
|
protected function add_auto_html_headers() {
|
2012-02-12 11:29:41 +00:00
|
|
|
global $config;
|
|
|
|
|
2009-07-19 18:35:46 +00:00
|
|
|
$data_href = get_base_href();
|
2012-02-12 11:29:41 +00:00
|
|
|
$theme_name = $config->get_string('theme', 'default');
|
2012-02-07 11:33:27 +00:00
|
|
|
|
2014-04-24 03:07:10 +00:00
|
|
|
$this->add_html_header("<script type='text/javascript'>base_href = '$data_href';</script>", 40);
|
2012-02-12 11:43:26 +00:00
|
|
|
|
2012-02-22 10:14:19 +00:00
|
|
|
# 404/static handler will map these to themes/foo/bar.ico or lib/static/bar.ico
|
2014-04-24 03:07:10 +00:00
|
|
|
$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);
|
2011-09-12 01:24:18 +00:00
|
|
|
|
2012-03-21 15:04:17 +00:00
|
|
|
$css_files = array();
|
|
|
|
$css_latest = 0;
|
|
|
|
foreach(array_merge(zglob("lib/*.css"), zglob("ext/*/style.css"), zglob("themes/$theme_name/style.css")) as $css) {
|
|
|
|
$css_files[] = $css;
|
|
|
|
$css_latest = max($css_latest, filemtime($css));
|
2011-09-12 01:24:18 +00:00
|
|
|
}
|
2012-03-30 17:21:35 +00:00
|
|
|
$css_cache_file = data_path("cache/style.$css_latest.css");
|
2012-03-21 15:04:17 +00:00
|
|
|
if(!file_exists($css_cache_file)) {
|
|
|
|
$css_data = "";
|
|
|
|
foreach($css_files as $file) {
|
|
|
|
$file_data = file_get_contents($file);
|
2011-09-25 17:40:34 +00:00
|
|
|
$pattern = '/url[\s]*\([\s]*["\']?([^"\'\)]+)["\']?[\s]*\)/';
|
2012-03-21 15:04:17 +00:00
|
|
|
$replace = 'url("../../'.dirname($file).'/$1")';
|
|
|
|
$file_data = preg_replace($pattern, $replace, $file_data);
|
|
|
|
$css_data .= $file_data . "\n";
|
2012-03-18 15:59:13 +00:00
|
|
|
}
|
2012-03-22 12:15:03 +00:00
|
|
|
file_put_contents($css_cache_file, $css_data);
|
2011-09-25 17:40:34 +00:00
|
|
|
}
|
2014-04-24 03:07:10 +00:00
|
|
|
$this->add_html_header("<link rel='stylesheet' href='$data_href/$css_cache_file' type='text/css'>", 43);
|
2012-03-21 15:04:17 +00:00
|
|
|
|
|
|
|
$js_files = array();
|
|
|
|
$js_latest = 0;
|
2012-03-22 12:30:02 +00:00
|
|
|
foreach(array_merge(zglob("lib/*.js"), zglob("ext/*/script.js"), zglob("themes/$theme_name/script.js")) as $js) {
|
2012-03-21 15:04:17 +00:00
|
|
|
$js_files[] = $js;
|
|
|
|
$js_latest = max($js_latest, filemtime($js));
|
2012-03-09 22:27:12 +00:00
|
|
|
}
|
2012-03-30 17:21:35 +00:00
|
|
|
$js_cache_file = data_path("cache/script.$js_latest.js");
|
2012-03-21 15:04:17 +00:00
|
|
|
if(!file_exists($js_cache_file)) {
|
|
|
|
$js_data = "";
|
|
|
|
foreach($js_files as $file) {
|
|
|
|
$js_data .= file_get_contents($file) . "\n";
|
2011-09-25 18:00:56 +00:00
|
|
|
}
|
2012-03-22 12:15:03 +00:00
|
|
|
file_put_contents($js_cache_file, $js_data);
|
2009-07-19 18:35:46 +00:00
|
|
|
}
|
2014-04-24 03:07:10 +00:00
|
|
|
$this->add_html_header("<script src='$data_href/$js_cache_file' type='text/javascript'></script>", 44);
|
2009-07-19 18:35:46 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2012-10-16 21:58:47 +00:00
|
|
|
|
|
|
|
class MockPage extends Page {
|
|
|
|
}
|
2014-04-24 03:07:10 +00:00
|
|
|
|