inc/users.inc.php
author rejo
Thu, 03 Jan 2008 20:27:47 +0000
changeset 60 6ffacb626294
parent 58 78558a77131e
child 65 ce1c4d5e1576
permissions -rwxr-xr-x
[feladat @ 107] Fixing ticket:17 (zone listing and count in user management screen incomplete).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     1
<?
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     2
47
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
     3
/*  PowerAdmin, a friendly web-based admin tool for PowerDNS.
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
     4
 *  See <https://rejo.zenger.nl/poweradmin> for more details.
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
     5
 *
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
     6
 *  Copyright 2007, 2008  Rejo Zenger <rejo@zenger.nl>
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
     7
 *
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
     8
 *  This program is free software: you can redistribute it and/or modify
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
     9
 *  it under the terms of the GNU General Public License as published by
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
    10
 *  the Free Software Foundation, either version 3 of the License, or
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
    11
 *  (at your option) any later version.
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
    12
 *
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
    13
 *  This program is distributed in the hope that it will be useful,
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
    16
 *  GNU General Public License for more details.
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
    17
 *
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
    18
 *  You should have received a copy of the GNU General Public License
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
    19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
ae140472d97c [feladat @ 94]
rejo
parents: 36
diff changeset
    20
 */
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    21
58
78558a77131e [feladat @ 105]
rejo
parents: 55
diff changeset
    22
require_once("inc/toolkit.inc.php");
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    23
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    24
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    25
 * Retrieve all users.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    26
 * Its to show_users therefore the odd name. Has to be changed.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    27
 * return values: an array with all users in it.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    28
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    29
function show_users($id='',$rowstart=0,$rowamount=9999999)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    30
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    31
 	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    32
 	if(is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    33
 	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    34
                 //When a user id is given, it is excluded from the userlist returned.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    35
                 $add = " WHERE users.id!=$id";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    36
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    37
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    38
	// Make a huge query.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    39
	$sqlq = "SELECT users.id AS id,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    40
		users.username AS username,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    41
		users.fullname AS fullname,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    42
		users.email AS email,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    43
		users.description AS description,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    44
		users.level AS level,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    45
		users.active AS active,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    46
		count(zones.owner) AS aantal FROM users
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    47
		LEFT JOIN zones ON users.id=zones.owner$add
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    48
		GROUP BY
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    49
			users.id,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    50
			users.username,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    51
			users.fullname,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    52
			users.email,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    53
			users.description,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    54
			users.level,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    55
			users.active
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    56
		ORDER BY
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    57
			users.fullname
55
a885f557678f [feladat @ 102]
peter
parents: 47
diff changeset
    58
	 	LIMIT $rowamount OFFSET $rowstart";
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    59
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    60
	// Execute the huge query.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    61
	$result = $db->query($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    62
	$ret = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    63
	$retcount = 0;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    64
	while ($r = $result->fetchRow())
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    65
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    66
		$ret[] = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    67
		 "id"                    =>              $r["id"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    68
		 "username"              =>              $r["username"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    69
		 "fullname"              =>              $r["fullname"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    70
		 "email"                 =>              $r["email"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    71
		 "description"           =>              $r["description"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    72
		 "level"                 =>              $r["level"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    73
		 "active"                =>              $r["active"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    74
		 "numdomains"            =>              $r["aantal"]
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    75
		);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    76
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    77
	return $ret;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    78
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    79
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    80
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    81
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    82
 * Check if the given $userid is connected to a valid user.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    83
 * return values: true if user exists, false if users doesnt exist.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    84
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    85
 function is_valid_user($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    86
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    87
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    88
	if(is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    89
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    90
		$result = $db->query("SELECT id FROM users WHERE id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    91
		if ($result->numRows() == 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    92
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    93
			return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    94
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    95
		else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    96
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    97
			return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    98
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    99
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   100
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   101
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   102
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   103
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   104
 * Gives a textdescribed value of the given levelid
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   105
 * return values: the text associated with the level
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   106
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   107
function leveldescription($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   108
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   109
	switch($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   110
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   111
		case 1:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   112
			global $NAME_LEVEL_1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   113
			return $NAME_LEVEL_1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   114
			break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   115
		case 5:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   116
			global $NAME_LEVEL_5;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   117
			return $NAME_LEVEL_5;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   118
			break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   119
		case 10:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   120
			global $NAME_LEVEL_10;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   121
			return $NAME_LEVEL_10;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   122
			break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   123
		default:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   124
			return "Unknown";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   125
			break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   126
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   127
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   128
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   129
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   130
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   131
 * Checks if a given username exists in the database.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   132
 * return values: true if exists, false if not.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   133
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   134
function user_exists($user)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   135
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   136
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   137
	$result = $db->query("SELECT id FROM users WHERE username='$user'");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   138
	if ($result->numRows() == 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   139
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   140
                 return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   141
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   142
	elseif($result->numRows() == 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   143
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   144
        	return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   145
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   146
        else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   147
        {
4
55ed92aa7cf5 [feladat @ 5]
rejo
parents: 1
diff changeset
   148
        	error(ERR_UNKNOWN);
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   149
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   150
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   151
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   152
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   153
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   154
 * Get all user info for the given user in an array.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   155
 * return values: the database style array with the information about the user.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   156
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   157
function get_user_info($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   158
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   159
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   160
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   161
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   162
		$result = $db->query("SELECT id, username, fullname, email, description, level, active from users where id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   163
		$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   164
		return $r;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   165
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   166
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   167
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   168
		error(sprintf(ERR_INV_ARGC,"get_user_info", "you gave illegal arguments: $id"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   169
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   170
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   171
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   172
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   173
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   174
 * Delete a user from the system
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   175
 * return values: true if user doesnt exist.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   176
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   177
function delete_user($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   178
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   179
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   180
	if (!level(10))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   181
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   182
		error(ERR_LEVEL_10);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   183
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   184
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   185
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   186
        	$db->query("DELETE FROM users WHERE id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   187
        	$db->query("DELETE FROM zones WHERE owner=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   188
        	return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   189
        	// No need to check the affected rows. If the affected rows would be 0,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   190
        	// the user isnt in the dbase, just as we want.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   191
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   192
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   193
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   194
		error(ERR_INV_ARG);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   195
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   196
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   197
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   198
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   199
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   200
 * Adds a user to the system.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   201
 * return values: true if succesfully added.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   202
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   203
function add_user($user, $password, $fullname, $email, $level, $description, $active)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   204
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   205
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   206
	if (!level(10))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   207
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   208
		error(ERR_LEVEL_10);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   209
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   210
	if (!user_exists($user))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   211
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   212
		// Might have to be changed.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   213
		// TODO probably.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   214
		$description = mysql_escape_string($description);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   215
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   216
		// Clean up the fullname
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   217
		$fullname = mysql_escape_string($fullname);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   218
		is_valid_email($email);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   219
8
47dd15d8bb8c [feladat @ 20]
rejo
parents: 4
diff changeset
   220
		$db->query("INSERT INTO users (username, password, fullname, email, description, level, active) VALUES ('$user', '" . md5($password) . "', '$fullname', '$email', '$description', '$level', '$active')");
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   221
		return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   222
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   223
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   224
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   225
		error(ERR_USER_EXISTS);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   226
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   227
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   228
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   229
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   230
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   231
 * Edit the information of an user.. sloppy implementation with too many queries.. (2) :)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   232
 * return values: true if succesful
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   233
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   234
function edit_user($id, $user, $fullname, $email, $level, $description, $active, $password)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   235
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   236
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   237
	if(!level(10)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   238
		error(ERR_LEVEL_10);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   239
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   240
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   241
  	// Might have to be changed.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   242
  	// TODO
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   243
	$description = mysql_escape_string($description);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   244
	$fullname = mysql_escape_string($fullname);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   245
	is_valid_email($email);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   246
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   247
	$sqlquery = "UPDATE users set username='$user', fullname='$fullname', email='$email', level=$level, description='$description', active=$active ";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   248
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   249
	if($password != "")
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   250
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   251
		$sqlquery .= ", password= '" . md5($password) . "' ";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   252
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   253
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   254
	$sqlquery .= "where id=$id" ;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   255
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   256
  	// Search the username that right now goes with this ID.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   257
	$result = $db->query("SELECT username from users where id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   258
	$r = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   259
	$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   260
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   261
  	// If the found username with this ID is the given username with the command.. execute.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   262
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   263
	if($r["username"] == $user)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   264
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   265
		$db->query($sqlquery);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   266
  		return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   267
  	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   268
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   269
  	// Its not.. so the user wants to change.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   270
  	// Find if there is an id that has the wished username.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   271
  	$otheruser = $db->query("SELECT id from users where username='$user'");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   272
  	if($otheruser->numRows() > 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   273
  	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   274
  		error(ERR_USER_EXIST);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   275
  	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   276
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   277
  	// Its fine it seems.. :)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   278
  	// Lets execute it.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   279
  	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   280
  	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   281
		$db->query($sqlquery);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   282
		return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   283
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   284
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   285
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   286
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   287
 * Change the pass of the user.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   288
 * The user is automatically logged out after the pass change.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   289
 * return values: none.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   290
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   291
function change_user_pass($currentpass, $newpass, $newpass2)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   292
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   293
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   294
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   295
	// Check if the passwords are equal.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   296
	if($newpass != $newpass2)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   297
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   298
		error(ERR_USER_MATCH_NEW_PASS);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   299
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   300
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   301
	// Retrieve the users password.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   302
	$result = $db->query("SELECT password, id FROM users WHERE username='". $_SESSION["userlogin"]  ."'");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   303
	$rinfo = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   304
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   305
	// Check the current password versus the database password and execute the update.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   306
	if(md5($currentpass) == $rinfo["password"])
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   307
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   308
		$sqlquery = "update users set password='" . md5($newpass) . "' where id='" . $rinfo["id"] . "'";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   309
		$db->query($sqlquery);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   310
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   311
		// Logout the user.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   312
		logout("Pass changed please re-login");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   313
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   314
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   315
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   316
		error(ERR_USER_WRONG_CURRENT_PASS);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   317
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   318
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   319
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   320
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   321
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   322
 * Get a fullname when you have a userid.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   323
 * return values: gives the fullname from a userid.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   324
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   325
function get_fullname_from_userid($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   326
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   327
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   328
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   329
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   330
		$result = $db->query("SELECT fullname FROM users WHERE id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   331
		$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   332
		return $r["fullname"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   333
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   334
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   335
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   336
		error(ERR_INV_ARG);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   337
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   338
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   339
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   340
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   341
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   342
 * Get a fullname when you have a userid.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   343
 * return values: gives the fullname from a userid.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   344
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   345
function get_owner_from_id($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   346
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   347
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   348
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   349
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   350
		$result = $db->query("SELECT fullname FROM users WHERE id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   351
		if ($result->numRows() == 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   352
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   353
			$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   354
			return $r["fullname"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   355
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   356
		else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   357
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   358
			error(ERR_USER_NOT_EXIST);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   359
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   360
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   361
	error(ERR_INV_ARG);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   362
}
26
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   363
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   364
/**
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   365
 * get_owners_from_domainid
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   366
 *
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   367
 * @todo also fetch the subowners
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   368
 * @param $id integer the id of the domain
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   369
 * @return String the list of owners for this domain
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   370
 */
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   371
function get_owners_from_domainid($id) {
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   372
      
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   373
      global $db;
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   374
      if (is_numeric($id))
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   375
      {
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   376
              $result = $db->query("SELECT users.id, users.fullname FROM users, zones WHERE zones.domain_id=$id AND zones.owner=users.id ORDER by fullname");
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   377
              if ($result->numRows() == 0)
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   378
              {
36
4ec5ab29f634 [feladat @ 83]
rejo
parents: 26
diff changeset
   379
		      return "";
4ec5ab29f634 [feladat @ 83]
rejo
parents: 26
diff changeset
   380
              } 
4ec5ab29f634 [feladat @ 83]
rejo
parents: 26
diff changeset
   381
	      else 
4ec5ab29f634 [feladat @ 83]
rejo
parents: 26
diff changeset
   382
	      {
26
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   383
                      $names = array();
36
4ec5ab29f634 [feladat @ 83]
rejo
parents: 26
diff changeset
   384
                      while ($r = $result->fetchRow()) 
4ec5ab29f634 [feladat @ 83]
rejo
parents: 26
diff changeset
   385
		      {
26
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   386
                              $names[] = $r['fullname'];
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   387
                      }
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   388
                      return implode(', ', $names);
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   389
              }
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   390
      }
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   391
      error(ERR_INV_ARG);
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   392
}
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   393
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   394
?>