summaryrefslogtreecommitdiff
path: root/forum/utils/forums.php
blob: c9c48b523fdf72aa0895068a14cad7a383099f3f (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php /* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

include_once($UTIL_DIR . "/convert.php");
include_once($UTIL_DIR . "/threads.php");


class Forum {
	public $fid;
	public $name;
	private $newStuff;
  private $numPosts;
  private $latestPost;

	public function setNewStuff($newStuff, $threads)
	{
    $this->numPosts = sizeof($threads->threads);
    $this->latestPost = "";
    $t = $threads->getLatestThread();
    if($t) {
      $this->latestPost = $t->name;
    }
    
		$this->newStuff = $newStuff;
	}

	public function show()
	{
    $newcls = " forum_nonew";
    if($this->newStuff) $newcls = " forum_new";

		echo "    <div class=\"forum\">\n";
		echo "      <span class=\"forum_icon".$newcls."\"></span>\n";
		echo "      <a class=\"forum_title\" href=\"?fid=" . $this->fid . "\">" . $this->name . "</a>\n";
    echo "      <span class=\"forum_latestpost\">".$this->latestPost."</span>\n";
    echo "      <span class=\"forum_numberofthreads\">".sprintf("%d", $this->numPosts)." threads</span>\n";
		echo "    </div>\n";
	}

	public function Forum($fid, $name)
	{
		$this->fid = $fid;
		$this->name = $name;
	}
}

class Forums {

	private $file;
	public $forums = array();
	
	public function add($forum) {
		global $FORUMS_DIR;
		if(!file_exists($FORUMS_DIR . "/". $forum->fid)) {
			mkdir($FORUMS_DIR . "/". $forum->fid);
		}
		$key = $forum->fid;
		$this->forums[$key] = $forum;
	}
	
	public function write()
	{
		$fp = fopen($this->file, "w");

		$block = TRUE;
		flock($fp, LOCK_EX, $block); // do an exclusive lock

		fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

		fwrite($fp, "<forums>\n");
		foreach($this->forums as $forum) {
			fwrite($fp, "  <forum fid=\"" . $forum->fid . "\"\n");
			fwrite($fp, "         name=\"" . htmlspecialchars($forum->name, ENT_QUOTES, "UTF-8") . "\"/>\n");
		}
		fwrite($fp, "</forums>\n");

		fclose($fp);
	}

	public function getForum($fid)
	{
		$forum = $this->forums[$fid]; 
		return $forum;
	}

	public function show()
	{
    echo "<div class=\"forums\">\n";
		foreach($this->forums as $forum) {
			$forum->show();
		}
    echo "</div>\n";
	}

	private function read()
	{
		global $FORUMS_DIR;

		$dom = new DomDocument;
		$dom->preserveWhiteSpace = FALSE;
		$dom->load($this->file);
		$forums = $dom->getElementsByTagName('forum');

		foreach($forums as $f) {
			$forum = new Forum($f->getAttribute('fid'),
												 $f->getAttribute('name'));

			$this->add($forum);

			$threads = new Threads($FORUMS_DIR . "/" . $f->getAttribute('fid'));
			$forum->setNewStuff($threads->newStuff(), $threads);
		}
	}

	public function Forums($file)
	{
		$this->file = $file;
		if(file_exists($file)) $this->read();
		else {
			if(!file_exists(dirname($file))) {
				if(!mkdir(dirname($file))) {
					echo"Could not create directory: " . dirname($file);
					die();
				}
			} else {
				if(!is_dir(dirname($file))) {
					echo dirname($file) . " exists but is not a directory";
					die();
				} else {
					if(!is_readable(dirname($file)) || !is_writeable(dirname($file)) || !is_executable(dirname($file))) {
						echo dirname($file) . " exists but does not have the correct permissions. (r/w/x)";
						die();
					}
				}
			}
		}
	}

}
?>