summaryrefslogtreecommitdiff
path: root/forum/utils/threads.php
blob: 0c4ef32fbca60516177594c6e598f2c399730125 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php

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

/**
 * <?xml version="1.0" encoding="UTF-8"?>
 * <thread tid="1" name="Thread1">
 *   .
 *   .
 *   .
 * </thread>
 */

class Thread {
	public $tid;
	public $name;
	public $lastseen = array();
	public $lastpost;

	public function show()
	{
		global $fid, $current_user;
		$jumptonew = "";
		echo "    <div class=\"thread\">\n";
		if($this->lastseen[$current_user->uid] < $this->lastpost) {
			echo "  <div class=\"new\"></div>";
			$jumptonew = "#firstunread";
		} else {
			echo "      <div class=\"nonew\"></div>\n";
		}
		echo "      <a href=\"?fid=" . $fid . "&amp;tid=" .	$this->tid . $jumptonew."\">" . $this->name . "</a>\n";
		echo "    </div>\n";
	}

	private function loadLastSeen($lastseen)
	{
		if($lastseen == "") return;
		$list = explode(",", $lastseen);
		foreach($list as $l) {
			$pair = explode("=", $l);
			if($pair[0] != "" && $pair[1] != "") {
				$this->lastseen[$pair[0]] = $pair[1];
			} 
		}
	}

	public function Thread($tid, $name, $lastpost, $lastseen)
	{
		$this->tid = $tid;
		$this->name = $name;
		$this->lastpost = $lastpost;
		$this->loadLastSeen($lastseen);
	}
}

class Threads {

	private $dir;
	public $threads = array();
	
	public function add($thread) {
		//		$key = $thread->name;
		//		$key = sprintf("%d-%d", $thread->lastpost, $thread->tid);
		//		$key = sprintf("%d", $thread->lastpost);
		$key = $thread->lastpost . "-" . $thread->tid;//name;
		//		echo "[" . $key . "]";
		$this->threads[$key] = $thread;
	}
	
	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, "<members>\n");
		foreach($this->members as $member) {
			fwrite($fp, "  <member id=\"" .
						 htmlspecialchars($member->id, ENT_QUOTES, "UTF-8") . "\"\n");
			fwrite($fp, "          name=\"" . 
						 htmlspecialchars($member->name, ENT_QUOTES, "UTF-8") . "\"\n");
			fwrite($fp, "          description=\"" . 
						 htmlspecialchars($member->description, ENT_QUOTES, "UTF-8") . "\"\n");
			fwrite($fp, "          image=\"" . 
						 htmlspecialchars($member->image, ENT_QUOTES, "UTF-8") . "\">\n");


			fwrite($fp, "  </member>\n");
		}
		fwrite($fp, "</members>\n");

		fclose($fp);
		*/
	}

	/*
	public function deleteForumUser($id)
	{
		if($this->members[$id]) {
			unset($this->members[$id]); 
			//			$this->write();
		} else {
			echo "<p>ERROR: User! <em>".$id."</em> does not exist!</p>\n";
			return false;
		}
		return true;
	}
	*/

	public function getThread($tid)
	{
		$thread = $this->threads[$tid]; 
		return $thread;
	}

	public function show()
	{
		foreach($this->threads as $thread) {
			$thread->show();
		}
	}

	public function newStuff()
	{
		global $current_user;

		foreach($this->threads as $thread) {
			if($thread->lastseen[$current_user->uid] < $thread->lastpost) return true;
		}

		return false;
	}

	private function read()
	{
		$dh = opendir($this->dir);
		while($file = readdir($dh)) {
			if($file == "." || $file == "..") continue;
			$dom = new DomDocument;
			$dom->preserveWhiteSpace = FALSE;
			$dom->load($this->dir . "/" . $file);
			$threads = $dom->getElementsByTagName('thread');
			
			foreach($threads as $f) {
				$thread = new Thread($f->getAttribute('tid'),
														 $f->getAttribute('name'),
														 $f->getAttribute('lastpost'),
														 $f->getAttribute('lastseen'));

				$this->add($thread);
			}
		}
	}

	public function Threads($dir)
	{
		$this->dir = $dir;
		$this->read();
		krsort($this->threads);
	}

}
?>