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.class.php

214 lines
5.1 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
*
* 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.
*
* 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,
* 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 */
//@{
/** @private */
var $mode = "page";
2010-01-12 15:01:34 +00:00
/** @private */
var $type = "text/html";
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
*/
public function set_mode($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
*/
public function set_type($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 */
//@{
2010-01-12 15:01:34 +00:00
/** @private */
var $data = "";
2010-01-12 15:01:34 +00:00
/** @private */
var $filename = null;
2009-07-19 07:38:13 +00:00
/**
2010-01-12 15:01:34 +00:00
* Set the raw data to be sent
2009-07-19 07:38:13 +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
/**
2010-01-12 15:01:34 +00:00
* Set the recommended download filename
2009-07-19 07:38:13 +00:00
*/
public function set_filename($filename) {
$this->filename = $filename;
}
2010-01-12 15:01:34 +00:00
//@}
// ==============================================
2010-01-12 15:01:34 +00:00
/** @name "redirect" mode */
//@{
2010-01-12 15:01:34 +00:00
/** @private */
var $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
*/
public function set_redirect($redirect) {
$this->redirect = $redirect;
}
2010-01-12 15:01:34 +00:00
//@}
// ==============================================
2010-01-12 15:01:34 +00:00
/** @name "page" mode */
//@{
2010-01-12 15:01:34 +00:00
/** @privatesection */
var $title = "";
var $heading = "";
var $subheading = "";
var $quicknav = "";
var $html_headers = array();
var $blocks = array();
2010-01-12 15:01:34 +00:00
/** @publicsection */
2009-07-19 07:38:13 +00:00
/**
2010-01-12 15:01:34 +00:00
* Set the window title
2009-07-19 07:38:13 +00:00
*/
public function set_title($title) {
$this->title = $title;
}
2009-07-19 07:38:13 +00:00
/**
2010-01-12 15:01:34 +00:00
* Set the main heading
2009-07-19 07:38:13 +00:00
*/
public function set_heading($heading) {
$this->heading = $heading;
}
2009-07-19 07:38:13 +00:00
/**
2010-01-12 15:01:34 +00:00
* Set the sub heading
2009-07-19 07:38:13 +00:00
*/
public function set_subheading($subheading) {
$this->subheading = $subheading;
}
2009-07-19 07:38:13 +00:00
/**
2010-01-12 15:01:34 +00:00
* Add a line to the HTML head section
2009-07-19 07:38:13 +00:00
*/
public function add_html_header($line, $position=50) {
while(isset($this->html_headers[$position])) $position++;
$this->html_headers[$position] = $line;
}
2009-07-19 07:38:13 +00:00
/**
2010-01-12 15:01:34 +00:00
* Add a Block of data
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
/**
2010-01-12 15:01:34 +00:00
* Display the page according to the mode and data given
2009-07-19 07:38:13 +00:00
*/
public function display() {
global $page;
header("Content-type: {$this->type}");
2009-01-04 16:24:06 +00:00
header("X-Powered-By: SCore-".SCORE_VERSION);
switch($this->mode) {
case "page":
header("Cache-control: no-cache");
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}");
print "You should be redirected to <a href='{$this->redirect}'>{$this->redirect}</a>";
break;
default:
print "Invalid page mode";
break;
}
}
protected function add_auto_html_headers() {
$data_href = get_base_href();
2010-07-30 12:39:11 +00:00
2010-04-26 02:53:05 +00:00
foreach(glob("lib/*.css") as $css) {
$this->add_html_header("<link rel='stylesheet' href='$data_href/$css' type='text/css'>");
2010-04-26 02:53:05 +00:00
}
$css_files = glob("ext/*/style.css");
if($css_files) {
foreach($css_files as $css_file) {
$this->add_html_header("<link rel='stylesheet' href='$data_href/$css_file' type='text/css'>");
}
}
2010-07-30 12:39:11 +00:00
foreach(glob("lib/*.js") as $js) {
$this->add_html_header("<script src='$data_href/$js' type='text/javascript'></script>");
2010-07-30 12:39:11 +00:00
}
$js_files = glob("ext/*/script.js");
if($js_files) {
foreach($js_files as $js_file) {
$this->add_html_header("<script src='$data_href/$js_file' type='text/javascript'></script>");
}
}
}
}
?>