summaryrefslogtreecommitdiff
path: root/forum/utils/users.php
blob: ee04b92a0d5bef19ee80e41eb3426c001088ec6b (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
<?php

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

class User {
	public $uid;
	public $gid;
	public $enabled;
	public $username;
	public $password;
	public $name;
	public $avatar;
	public $email;
	public $notified;
	public $signature;

	public function checkPassword($password)
	{
		return $this->password == sha1(md5($password));
	}

	public function write($fp)
	{
		fwrite($fp, "  <user enabled=\"" . $this->enabled . "\"\n");
		fwrite($fp, "        uid=\"" . $this->uid . "\"\n");
		fwrite($fp, "        gid=\"" . $this->gid . "\"\n");
		fwrite($fp, "        notified=\"" . $this->notified . "\"\n");
		fwrite($fp, "        username=\"" . htmlspecialchars($this->username, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "        password=\"" . $this->password . "\"\n");
		fwrite($fp, "        name=\"" . htmlspecialchars($this->name, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "        avatar=\"" . htmlspecialchars($this->avatar, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "        email=\"" . htmlspecialchars($this->email, ENT_QUOTES, "UTF-8") . "\">");
		fwrite($fp, htmlspecialchars($this->signature, ENT_QUOTES, "UTF-8"));
		fwrite($fp, "</user>\n");
	}

	public function User($enabled, $uid, $gid, $username, $password, $name, $email, $avatar, $signature, $notified)
	{
		$this->enabled = $enabled;
		$this->gid = $gid;
		$this->uid = $uid;
		$this->username = $username;
		$this->password = $password;
		$this->email = $email;
		$this->name = $name;
		$this->avatar = $avatar;
		$this->signature = $signature;
		if($notified == "") $notified = 0;
		$this->notified = $notified;
	}
}

class Users {

	private $file;
	public $users = array();
	
	public function add($user) {
		$key = $user->uid;
		$this->users[$key] = $user;
	}
	
	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, "<users>\n");
		foreach($this->users as $user) {
			$user->write($fp);
		}
		fwrite($fp, "</users>\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 getUserID($username)
	{
		foreach($this->users as $user) {
			if($user->username == $username) return $user->uid;
		}
		return -1;
	}

	public function getUser($uid)
	{
		$user = $this->users[$uid]; 
		return $user;
	}

	private function read()
	{
		$dom = new DomDocument;
		$dom->preserveWhiteSpace = FALSE;
		$dom->load($this->file);
		$users = $dom->getElementsByTagName('user');

		foreach ($users as $u) {
			$user = new User($u->getAttribute('enabled'),
											 $u->getAttribute('uid'),
											 $u->getAttribute('gid'),
											 $u->getAttribute('username'),
											 $u->getAttribute('password'),
											 $u->getAttribute('name'),
											 $u->getAttribute('email'),
											 $u->getAttribute('avatar'),
											 $u->textContent,
											 $u->getAttribute('notified'));

			$this->add($user);
		}

	}

	public function Users($file)
	{
		$this->file = $file;
		if(file_exists($file)) $this->read();
		else {
			// Create default users.xml file.
			$user = new User(true, 0, 0, "admin", "8456c0209aaa9ca04730a3160caf6e5e6b5ea389", "Administrator", "", "", 0);
			$this->add($user);
			$this->write();
		}
	}

}
?>