summaryrefslogtreecommitdiff
path: root/utils/user.php
blob: 508f83d9524e253d6333923265fd4729723f63ce (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
<?php

include_once("convert.php");

class User {
	public $userid;
	public $password;
	public $users;
	public $news;
	public $events;
	public $guestbook;
	public $gallery;
	public $user;
	public $newsletter;
	public $pressrelease;

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

	public function setPassword($oldpassword, $password1, $password2) {
		if($this->checkPassword($oldpassword) == false) { // Doublecheck the validity of the user.
			echo "<p>Current password is incorrect!</p>\n";
			return 1;
		}

		if($password1 != $password2) { // Check if passowrds match.
			echo "<p>Passwords do not match!</p>\n";
			return 1;
		}

		echo "<p>Password updated successfully!</p>\n";

		$this->password = sha1(md5($password1));
		return 0;
	}

	public function User($userid, $password, $users, $news, $events, $guestbook, $gallery, $user, $newsletter, $pressrelease)
	{
		$this->userid = $userid;
		$this->password = $password;
		$this->users = $users;
		$this->news = $news;
		$this->events = $events;
		$this->guestbook = $guestbook;
		$this->gallery = $gallery;
		$this->user = $user;
		$this->newsletter = $newsletter;
		$this->pressrelease = $pressrelease;
	}
}

class Users {

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

		fwrite($fp, "<users>\n");
		foreach($this->users as $user) {
			fwrite($fp, "  <user userid=\"" .
						 htmlspecialchars($user->userid, ENT_QUOTES, "UTF-8") . "\"\n");
			fwrite($fp, "        password=\"" . $user->password . "\"\n");
			fwrite($fp, "        users=\"" . $user->users . "\"\n");
			fwrite($fp, "        news=\"" . $user->news . "\"\n");
			fwrite($fp, "        events=\"" . $user->events . "\"\n");
			fwrite($fp, "        guestbook=\"" . $user->guestbook . "\"\n");
			fwrite($fp, "        gallery=\"" . $user->gallery . "\"\n");
			fwrite($fp, "        user=\"" . $user->user . "\"\n");
			fwrite($fp, "        newsletter=\"" . $user->newsletter . "\"\n");
			fwrite($fp, "        pressrelease=\"" . $user->pressrelease . "\">\n");
			fwrite($fp, "  </user>\n");
		}
		fwrite($fp, "</users>\n");

		fclose($fp);
	}

	public function deleteUser($userid)
	{
		if($userid != "admin") {
			if($this->users[$userid]) {
				unset($this->users[$userid]); 
				$this->write();
			} else {
				echo "<p>ERROR: User! <em>".$userid."</em> does not exist!</p>\n";
				return false;
			}
		} else {
			echo "<p>ERROR: You cannot delete the admin user!</p>\n";
			return false;
		}
		return true;
	}

	public function findUser($userid)
	{
		$user = $this->users[$userid]; 
		return $user;
	}
	
	public function useridList()
	{
		$useridlist = array();
		foreach($this->users as $user) {
			array_push($useridlist, $user->userid);
		}
		return $useridlist;
	}

	private function read()
	{

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

		foreach ($params as $param) {
			$user = new User($param->getAttribute('userid'),
					 $param->getAttribute('password'),
					 $param->getAttribute('users'),
					 $param->getAttribute('news'),
					 $param->getAttribute('events'),
					 $param->getAttribute('guestbook'),
					 $param->getAttribute('gallery'),
					 $param->getAttribute('user'),
					 $param->getAttribute('newsletter'),
					 $param->getAttribute('pressrelease'));
			$this->add($user);
		}
		
	}

	public function Users($file)
	{
		$this->file =  $file;
		$this->read();
	}

}
?>