summaryrefslogtreecommitdiff
path: root/utils/pages.php
blob: 0ab52a87663b655e92a54d30a6e33f6bd19efdde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php

include_once("convert.php");
include_once($UTIL_DIR . "/markdown.php");
include_once($UTIL_DIR . "/modules.php");

class Page {
	public $title;
	public $content;

	public function write($fp)
	{
		fwrite($fp, "  <page title=\"" .
					 htmlspecialchars($this->title, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "        content=\"" .
					 htmlspecialchars($this->content, ENT_QUOTES, "UTF-8") . "\">\n");
		fwrite($fp, "  </page>\n");
	}

	public function show()
	{
		global $DATA_DIR, $modules;

		$str = Markdown($this->content);
		
		if(preg_match_all("/\[\[([\?,a-zA-Z0-9_]+)\]\]/", $str, $res)) {

			$modulecodes = array_unique($res[1]);
			foreach($modulecodes as $modulecode) {
				$m = explode("?", $modulecode);
				$module = $m[0];
				$params = explode(",", $m[1]);
				if($modules[$module]) {
					$modulestr = $modules[$module]->run($params);
				} else {
					$modulestr = "<p><strong>CMS ERROR: Could not find module: [[" . $modulecode . "]]</strong></p>";
				}

				$str = str_replace("[[" . $modulecode . "]]", $modulestr, $str);
				
			}
		}

		echo $str;
	}

	public function Page($title, $content)
	{
		$this->title = $title;
		$this->content = $content;
	}
}

class Pages {

	private $file;
	private $pages = array();

	public function getPage($title)
	{
		return $this->pages[$title];
	}

	public function add($page) {
		$key = $page->title;
		$this->pages[$key] = $page;
	}
	
	public function write()
	{
		$fp = fopen($this->file, "w");
		fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

		fwrite($fp, "<pages>\n");
		foreach($this->pages as $page) {
			$page->write($fp);
		}
		fwrite($fp, "</pages>\n");

		fclose($fp);
	}
	
	private function read()
	{
		$dom = new DomDocument;
		$dom->preserveWhiteSpace = TRUE;
		$dom->load($this->file);
		$pages = $dom->getElementsByTagName('page');

		foreach ($pages as $p) {
			$page = new Page($p->getAttribute('title'),
											 $p->textContent);
			$this->add($page);
		}
	}

	public function Pages($file)
	{
		$this->file =  $file;
		if(file_exists($file)) $this->read();
	}

}
?>