inc/users.inc.php
author peter
Sun, 06 Jan 2008 11:49:17 +0000
changeset 67 2f8c29fc5e2e
parent 65 ce1c4d5e1576
child 71 e1b918eaf69a
permissions -rwxr-xr-x
[feladat @ 114] Patch for ticket 19. Because of the use of the quote function, empty strings where converted to NULL as MDB2_PORTABILITY_EMPTY_TO_NULL is set by default. This problem also applied to the active state of an user.
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;
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
    32
	$add = '';
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    33
 	if(is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    34
 	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    35
                 //When a user id is given, it is excluded from the userlist returned.
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
    36
                 $add = " WHERE users.id!=".$db->quote($id);
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    37
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    38
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    39
	// Make a huge query.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    40
	$sqlq = "SELECT users.id AS id,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    41
		users.username AS username,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    42
		users.fullname AS fullname,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    43
		users.email AS email,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    44
		users.description AS description,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    45
		users.level AS level,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    46
		users.active AS active,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    47
		count(zones.owner) AS aantal FROM users
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    48
		LEFT JOIN zones ON users.id=zones.owner$add
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    49
		GROUP BY
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    50
			users.id,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    51
			users.username,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    52
			users.fullname,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    53
			users.email,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    54
			users.description,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    55
			users.level,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    56
			users.active
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    57
		ORDER BY
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
    58
			users.fullname";
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    59
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    60
	// Execute the huge query.
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
    61
	$db->setLimit($rowstart, $rowamount);
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    62
	$result = $db->query($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    63
	$ret = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    64
	$retcount = 0;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    65
	while ($r = $result->fetchRow())
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    66
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    67
		$ret[] = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    68
		 "id"                    =>              $r["id"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    69
		 "username"              =>              $r["username"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    70
		 "fullname"              =>              $r["fullname"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    71
		 "email"                 =>              $r["email"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    72
		 "description"           =>              $r["description"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    73
		 "level"                 =>              $r["level"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    74
		 "active"                =>              $r["active"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    75
		 "numdomains"            =>              $r["aantal"]
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    76
		);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    77
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    78
	return $ret;
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
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    83
 * Check if the given $userid is connected to a valid user.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    84
 * return values: true if user exists, false if users doesnt exist.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    85
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    86
 function is_valid_user($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    87
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    88
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    89
	if(is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    90
	{
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
    91
		$result = $db->query("SELECT id FROM users WHERE id=".$db->quote($id));
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    92
		if ($result->numRows() == 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    93
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    94
			return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    95
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    96
		else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    97
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    98
			return false;
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
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   105
 * Gives a textdescribed value of the given levelid
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   106
 * return values: the text associated with the level
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   107
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   108
function leveldescription($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   109
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   110
	switch($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   111
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   112
		case 1:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   113
			global $NAME_LEVEL_1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   114
			return $NAME_LEVEL_1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   115
			break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   116
		case 5:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   117
			global $NAME_LEVEL_5;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   118
			return $NAME_LEVEL_5;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   119
			break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   120
		case 10:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   121
			global $NAME_LEVEL_10;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   122
			return $NAME_LEVEL_10;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   123
			break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   124
		default:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   125
			return "Unknown";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   126
			break;
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
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   132
 * Checks if a given username exists in the database.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   133
 * return values: true if exists, false if not.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   134
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   135
function user_exists($user)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   136
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   137
	global $db;
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   138
	$result = $db->query("SELECT id FROM users WHERE username=".$db->quote($user));
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   139
	if ($result->numRows() == 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   140
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   141
                 return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   142
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   143
	elseif($result->numRows() == 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   144
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   145
        	return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   146
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   147
        else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   148
        {
4
55ed92aa7cf5 [feladat @ 5]
rejo
parents: 1
diff changeset
   149
        	error(ERR_UNKNOWN);
1
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
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   155
 * Get all user info for the given user in an array.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   156
 * return values: the database style array with the information about the user.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   157
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   158
function get_user_info($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   159
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   160
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   161
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   162
	{
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   163
		$result = $db->query("SELECT id, username, fullname, email, description, level, active from users where id=".$db->quote($id));
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   164
		$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   165
		return $r;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   166
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   167
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   168
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   169
		error(sprintf(ERR_INV_ARGC,"get_user_info", "you gave illegal arguments: $id"));
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
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   175
 * Delete a user from the system
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   176
 * return values: true if user doesnt exist.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   177
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   178
function delete_user($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   179
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   180
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   181
	if (!level(10))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   182
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   183
		error(ERR_LEVEL_10);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   184
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   185
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   186
	{
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   187
        	$db->query("DELETE FROM users WHERE id=".$db->quote($id));
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   188
        	$db->query("DELETE FROM zones WHERE owner=".$db->quote($id));
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   189
        	return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   190
        	// No need to check the affected rows. If the affected rows would be 0,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   191
        	// the user isnt in the dbase, just as we want.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   192
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   193
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   194
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   195
		error(ERR_INV_ARG);
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
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   201
 * Adds a user to the system.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   202
 * return values: true if succesfully added.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   203
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   204
function add_user($user, $password, $fullname, $email, $level, $description, $active)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   205
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   206
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   207
	if (!level(10))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   208
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   209
		error(ERR_LEVEL_10);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   210
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   211
	if (!user_exists($user))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   212
	{
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   213
		if (!is_valid_email($email)) 
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   214
		{
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   215
			error(ERR_INV_EMAIL);
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   216
		}
67
2f8c29fc5e2e [feladat @ 114]
peter
parents: 65
diff changeset
   217
		if ($active != 1) {
2f8c29fc5e2e [feladat @ 114]
peter
parents: 65
diff changeset
   218
			$active = 0;
2f8c29fc5e2e [feladat @ 114]
peter
parents: 65
diff changeset
   219
		}
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   220
		$db->query("INSERT INTO users (username, password, fullname, email, description, level, active) VALUES (".$db->quote($user).", '" . md5($password) . "', ".$db->quote($fullname).", ".$db->quote($email).", ".$db->quote($description).", ".$db->quote($level).", ".$db->quote($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
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   241
	if (!is_valid_email($email)) 
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   242
	{
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   243
		error(ERR_INV_EMAIL);
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   244
	}
67
2f8c29fc5e2e [feladat @ 114]
peter
parents: 65
diff changeset
   245
	if ($active != 1) {
2f8c29fc5e2e [feladat @ 114]
peter
parents: 65
diff changeset
   246
		$active = 0;
2f8c29fc5e2e [feladat @ 114]
peter
parents: 65
diff changeset
   247
	}
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   248
	$sqlquery = "UPDATE users set username=".$db->quote($user).", fullname=".$db->quote($fullname).", email=".$db->quote($email).", level=".$db->quote($level).", description=".$db->quote($description).", active=".$db->quote($active);
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   249
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   250
	if($password != "")
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   251
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   252
		$sqlquery .= ", password= '" . md5($password) . "' ";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   253
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   254
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   255
	$sqlquery .= " WHERE id=".$db->quote($id) ;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   256
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   257
  	// Search the username that right now goes with this ID.
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   258
	$result = $db->query("SELECT username from users where id=".$db->quote($id));
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   259
	$r = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   260
	$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   261
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   262
  	// If the found username with this ID is the given username with the command.. execute.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   263
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   264
	if($r["username"] == $user)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   265
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   266
		$db->query($sqlquery);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   267
  		return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   268
  	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   269
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   270
  	// Its not.. so the user wants to change.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   271
  	// Find if there is an id that has the wished username.
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   272
  	$otheruser = $db->query("SELECT id from users where username=".$db->query($user));
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   273
  	if($otheruser->numRows() > 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   274
  	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   275
  		error(ERR_USER_EXIST);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   276
  	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   277
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   278
  	// Its fine it seems.. :)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   279
  	// Lets execute it.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   280
  	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   281
  	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   282
		$db->query($sqlquery);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   283
		return true;
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
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   288
 * Change the pass of the user.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   289
 * The user is automatically logged out after the pass change.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   290
 * return values: none.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   291
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   292
function change_user_pass($currentpass, $newpass, $newpass2)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   293
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   294
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   295
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   296
	// Check if the passwords are equal.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   297
	if($newpass != $newpass2)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   298
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   299
		error(ERR_USER_MATCH_NEW_PASS);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   300
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   301
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   302
	// Retrieve the users password.
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   303
	$result = $db->query("SELECT password, id FROM users WHERE username=".$db->quote($_SESSION["userlogin"]));
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   304
	$rinfo = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   305
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   306
	// Check the current password versus the database password and execute the update.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   307
	if(md5($currentpass) == $rinfo["password"])
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   308
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   309
		$sqlquery = "update users set password='" . md5($newpass) . "' where id='" . $rinfo["id"] . "'";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   310
		$db->query($sqlquery);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   311
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   312
		// Logout the user.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   313
		logout("Pass changed please re-login");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   314
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   315
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   316
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   317
		error(ERR_USER_WRONG_CURRENT_PASS);
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
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   323
 * Get a fullname when you have a userid.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   324
 * return values: gives the fullname from a userid.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   325
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   326
function get_fullname_from_userid($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   327
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   328
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   329
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   330
	{
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   331
		$result = $db->query("SELECT fullname FROM users WHERE id=".$db->quote($id));
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   332
		$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   333
		return $r["fullname"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   334
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   335
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   336
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   337
		error(ERR_INV_ARG);
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
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   343
 * Get a fullname when you have a userid.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   344
 * return values: gives the fullname from a userid.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   345
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   346
function get_owner_from_id($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   347
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   348
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   349
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   350
	{
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   351
		$result = $db->query("SELECT fullname FROM users WHERE id=".$db->quote($id));
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   352
		if ($result->numRows() == 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   353
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   354
			$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   355
			return $r["fullname"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   356
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   357
		else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   358
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   359
			error(ERR_USER_NOT_EXIST);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   360
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   361
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   362
	error(ERR_INV_ARG);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   363
}
26
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   364
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   365
/**
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   366
 * get_owners_from_domainid
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   367
 *
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   368
 * @todo also fetch the subowners
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   369
 * @param $id integer the id of the domain
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   370
 * @return String the list of owners for this domain
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   371
 */
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   372
function get_owners_from_domainid($id) {
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   373
      
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   374
      global $db;
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   375
      if (is_numeric($id))
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   376
      {
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 58
diff changeset
   377
              $result = $db->query("SELECT users.id, users.fullname FROM users, zones WHERE zones.domain_id=".$db->quote($id)." AND zones.owner=users.id ORDER by fullname");
26
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   378
              if ($result->numRows() == 0)
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   379
              {
36
4ec5ab29f634 [feladat @ 83]
rejo
parents: 26
diff changeset
   380
		      return "";
4ec5ab29f634 [feladat @ 83]
rejo
parents: 26
diff changeset
   381
              } 
4ec5ab29f634 [feladat @ 83]
rejo
parents: 26
diff changeset
   382
	      else 
4ec5ab29f634 [feladat @ 83]
rejo
parents: 26
diff changeset
   383
	      {
26
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   384
                      $names = array();
36
4ec5ab29f634 [feladat @ 83]
rejo
parents: 26
diff changeset
   385
                      while ($r = $result->fetchRow()) 
4ec5ab29f634 [feladat @ 83]
rejo
parents: 26
diff changeset
   386
		      {
26
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   387
                              $names[] = $r['fullname'];
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   388
                      }
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   389
                      return implode(', ', $names);
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   390
              }
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   391
      }
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   392
      error(ERR_INV_ARG);
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   393
}
5d63f1e71d6e [feladat @ 73]
rejo
parents: 8
diff changeset
   394
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   395
?>