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