qa-db-users.php 9.78 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-db-users.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Database-level access to user management tables (if not using single sign-on)


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}


	function qa_db_calc_passcheck($password, $salt)
/*
	Return the expected value for the passcheck column given the $password and password $salt
*/
	{
Gideon Greenspan committed
38
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
39

Gideon Greenspan committed
40 41
		return sha1(substr($salt, 0, 8).$password.substr($salt, 8));
	}
Scott Vivian committed
42

Gideon Greenspan committed
43 44 45 46 47 48 49

	function qa_db_user_create($email, $password, $handle, $level, $ip)
/*
	Create a new user in the database with $email, $password, $handle, privilege $level, and $ip address
*/
	{
		require_once QA_INCLUDE_DIR.'qa-util-string.php';
Scott Vivian committed
50

Gideon Greenspan committed
51
		$salt=isset($password) ? qa_random_alphanum(16) : null;
Scott Vivian committed
52

Gideon Greenspan committed
53 54 55 56 57
		qa_db_query_sub(
			'INSERT INTO ^users (created, createip, email, passsalt, passcheck, level, handle, loggedin, loginip) '.
			'VALUES (NOW(), COALESCE(INET_ATON($), 0), $, $, UNHEX($), #, $, NOW(), COALESCE(INET_ATON($), 0))',
			$ip, $email, $salt, isset($password) ? qa_db_calc_passcheck($password, $salt) : null, (int)$level, $handle, $ip
		);
Scott Vivian committed
58

Gideon Greenspan committed
59 60 61
		return qa_db_last_insert_id();
	}

Scott Vivian committed
62

Gideon Greenspan committed
63 64 65 66 67 68 69 70 71
	function qa_db_user_delete($userid)
/*
	Delete user $userid from the database, along with everything they have ever done (to the extent that it's possible)
*/
	{
		qa_db_query_sub('UPDATE ^posts SET lastuserid=NULL WHERE lastuserid=$', $userid);
		qa_db_query_sub('DELETE FROM ^userpoints WHERE userid=$', $userid);
		qa_db_query_sub('DELETE FROM ^blobs WHERE blobid=(SELECT avatarblobid FROM ^users WHERE userid=$)', $userid);
		qa_db_query_sub('DELETE FROM ^users WHERE userid=$', $userid);
Scott Vivian committed
72

Gideon Greenspan committed
73 74 75 76 77 78 79 80 81 82 83 84
		// All the queries below should be superfluous due to foreign key constraints, but just in case the user switched to MyISAM.
		// Note also that private messages to/from that user are kept since we don't have all the keys we need to delete efficiently.

		qa_db_query_sub('UPDATE ^posts SET userid=NULL WHERE userid=$', $userid);
		qa_db_query_sub('DELETE FROM ^userlogins WHERE userid=$', $userid);
		qa_db_query_sub('DELETE FROM ^userprofile WHERE userid=$', $userid);
		qa_db_query_sub('DELETE FROM ^userfavorites WHERE userid=$', $userid);
		qa_db_query_sub('DELETE FROM ^userevents WHERE userid=$', $userid);
		qa_db_query_sub('DELETE FROM ^uservotes WHERE userid=$', $userid);
		qa_db_query_sub('DELETE FROM ^userlimits WHERE userid=$', $userid);
	}

Scott Vivian committed
85

Gideon Greenspan committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	function qa_db_user_find_by_email($email)
/*
	Return the ids of all users in the database which match $email (should be one or none)
*/
	{
		return qa_db_read_all_values(qa_db_query_sub(
			'SELECT userid FROM ^users WHERE email=$',
			$email
		));
	}


	function qa_db_user_find_by_handle($handle)
/*
	Return the ids of all users in the database which match $handle (=username), should be one or none
*/
	{
		return qa_db_read_all_values(qa_db_query_sub(
			'SELECT userid FROM ^users WHERE handle=$',
			$handle
		));
	}
Scott Vivian committed
108 109


Gideon Greenspan committed
110 111 112 113 114 115 116 117 118 119 120 121 122
	function qa_db_user_get_userid_handles($userids)
/*
	Return an array mapping mapping each userid in $userids that can be found to that user's handle
*/
	{
		if (count($userids))
			return qa_db_read_all_assoc(qa_db_query_sub(
				'SELECT userid, handle FROM ^users WHERE userid IN (#)',
				$userids
			), 'userid', 'handle');
		else
			return array();
	}
Scott Vivian committed
123

Gideon Greenspan committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137

	function qa_db_user_get_handle_userids($handles)
/*
	Return an array mapping mapping each handle in $handle that can be found to that user's userid
*/
	{
		if (count($handles))
			return qa_db_read_all_assoc(qa_db_query_sub(
				'SELECT handle, userid FROM ^users WHERE handle IN ($)',
				$handles
			), 'handle', 'userid');
		else
			return array();
	}
Scott Vivian committed
138

Gideon Greenspan committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

	function qa_db_user_set($userid, $field, $value)
/*
	Set $field of $userid to $value in the database users table
*/
	{
		qa_db_query_sub(
			'UPDATE ^users SET '.qa_db_escape_string($field).'=$ WHERE userid=$',
			$value, $userid
		);
	}


	function qa_db_user_set_password($userid, $password)
/*
	Set the password of $userid to $password, and reset their salt at the same time
*/
	{
Gideon Greenspan committed
157
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
158

Gideon Greenspan committed
159
		require_once QA_INCLUDE_DIR.'qa-util-string.php';
Scott Vivian committed
160

Gideon Greenspan committed
161 162 163 164 165 166 167
		$salt=qa_random_alphanum(16);

		qa_db_query_sub(
			'UPDATE ^users SET passsalt=$, passcheck=UNHEX($) WHERE userid=$',
			$salt, qa_db_calc_passcheck($password, $salt), $userid
		);
	}
Scott Vivian committed
168

Gideon Greenspan committed
169 170 171 172 173 174 175 176 177 178 179 180

	function qa_db_user_set_flag($userid, $flag, $set)
/*
	Switch on the $flag bit of the flags column for $userid if $set is true, or switch off otherwise
*/
	{
		qa_db_query_sub(
			'UPDATE ^users SET flags=flags'.($set ? '|' : '&~').'# WHERE userid=$',
			$flag, $userid
		);
	}

Scott Vivian committed
181

Gideon Greenspan committed
182 183 184 185 186
	function qa_db_user_rand_emailcode()
/*
	Return a random string to be used for a user's emailcode column
*/
	{
Gideon Greenspan committed
187
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
188

Gideon Greenspan committed
189
		require_once QA_INCLUDE_DIR.'qa-util-string.php';
Scott Vivian committed
190

Gideon Greenspan committed
191 192
		return qa_random_alphanum(8);
	}
Scott Vivian committed
193

Gideon Greenspan committed
194 195 196 197 198 199

	function qa_db_user_rand_sessioncode()
/*
	Return a random string to be used for a user's sessioncode column (for browser session cookies)
*/
	{
Gideon Greenspan committed
200
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
201

Gideon Greenspan committed
202
		require_once QA_INCLUDE_DIR.'qa-util-string.php';
Scott Vivian committed
203

Gideon Greenspan committed
204 205 206
		return qa_random_alphanum(8);
	}

Scott Vivian committed
207

Gideon Greenspan committed
208 209 210 211 212 213 214 215 216 217 218
	function qa_db_user_profile_set($userid, $field, $value)
/*
	Set a row in the database user profile table to store $value for $field for $userid
*/
	{
		qa_db_query_sub(
			'REPLACE ^userprofile (title, content, userid) VALUES ($, $, $)',
			$field, $value, $userid
		);
	}

Scott Vivian committed
219

Gideon Greenspan committed
220 221 222 223 224 225 226 227 228 229
	function qa_db_user_logged_in($userid, $ip)
/*
	Note in the database that $userid just logged in from $ip address
*/
	{
		qa_db_query_sub(
			'UPDATE ^users SET loggedin=NOW(), loginip=COALESCE(INET_ATON($), 0) WHERE userid=$',
			$ip, $userid
		);
	}
Scott Vivian committed
230

Gideon Greenspan committed
231 232 233 234 235 236 237 238 239 240 241

	function qa_db_user_written($userid, $ip)
/*
	Note in the database that $userid just performed a write operation from $ip address
*/
	{
		qa_db_query_sub(
			'UPDATE ^users SET written=NOW(), writeip=COALESCE(INET_ATON($), 0) WHERE userid=$',
			$ip, $userid
		);
	}
Scott Vivian committed
242 243


Gideon Greenspan committed
244 245 246 247 248 249 250 251 252 253 254
	function qa_db_user_login_add($userid, $source, $identifier)
/*
	Add an external login in the database for $source and $identifier for user $userid
*/
	{
		qa_db_query_sub(
			'INSERT INTO ^userlogins (userid, source, identifier, identifiermd5) '.
			'VALUES ($, $, $, UNHEX($))',
			$userid, $source, $identifier, md5($identifier)
		);
	}
Scott Vivian committed
255

Gideon Greenspan committed
256 257 258 259 260 261 262 263 264 265 266 267

	function qa_db_user_login_find($source, $identifier)
/*
	Return some information about the user with external login $source and $identifier in the database, if a match is found
*/
	{
		return qa_db_read_all_assoc(qa_db_query_sub(
			'SELECT ^userlogins.userid, handle, email FROM ^userlogins LEFT JOIN ^users ON ^userlogins.userid=^users.userid '.
			'WHERE source=$ AND identifiermd5=UNHEX($) AND identifier=$',
			$source, md5($identifier), $identifier
		));
	}
Scott Vivian committed
268 269


Gideon Greenspan committed
270 271 272 273 274 275
	function qa_db_user_login_sync($sync)
/*
	Lock all tables if $sync is true, otherwise unlock them. Used to synchronize creation of external login mappings.
*/
	{
		if ($sync) { // need to lock all tables since any could be used by a plugin's event module
Gideon Greenspan committed
276
			$tables=qa_db_list_tables();
Gideon Greenspan committed
277 278 279 280

			$locks=array();
			foreach ($tables as $table)
				$locks[]=$table.' WRITE';
Scott Vivian committed
281

Gideon Greenspan committed
282 283 284 285 286
			qa_db_query_sub('LOCK TABLES '.implode(', ', $locks));

		} else
			qa_db_query_sub('UNLOCK TABLES');
	}
Scott Vivian committed
287 288


Gideon Greenspan committed
289
	function qa_db_user_levels_set($userid, $userlevels)
Gideon Greenspan committed
290 291 292 293
/*
	Reset the full set of context-specific (currently, per category) user levels for user $userid to $userlevels, where
	$userlevels is an array of arrays, the inner arrays containing items 'entitytype', 'entityid' and 'level'.
*/
Gideon Greenspan committed
294 295 296 297 298
	{
		qa_db_query_sub(
			'DELETE FROM ^userlevels WHERE userid=$',
			$userid
		);
Scott Vivian committed
299

Gideon Greenspan committed
300 301 302 303 304 305
		foreach ($userlevels as $userlevel)
			qa_db_query_sub(
				'REPLACE ^userlevels (userid, entitytype, entityid, level) VALUES ($, $, #, #)',
				$userid, $userlevel['entitytype'], $userlevel['entityid'], $userlevel['level']
			);
	}
Scott Vivian committed
306 307


Gideon Greenspan committed
308 309 310 311 312 313 314 315 316 317
	function qa_db_users_get_mailing_next($lastuserid, $count)
/*
	Get the information required for sending a mailing to the next $count users with userids greater than $lastuserid
*/
	{
		return qa_db_read_all_assoc(qa_db_query_sub(
			'SELECT userid, email, handle, emailcode, flags FROM ^users WHERE userid># ORDER BY userid LIMIT #',
			$lastuserid, $count
		));
	}
Scott Vivian committed
318 319


Gideon Greenspan committed
320
	function qa_db_uapprovecount_update()
Gideon Greenspan committed
321 322 323
/*
	Update the cached count of the number of users who are awaiting approval after registration
*/
Gideon Greenspan committed
324 325 326 327 328 329 330
	{
		if ( qa_should_update_counts() && !QA_FINAL_EXTERNAL_USERS )
			qa_db_query_sub(
				"REPLACE ^options (title, content) SELECT 'cache_uapprovecount', COUNT(*) FROM ^users WHERE level<# AND NOT (flags&#)",
				QA_USER_LEVEL_APPROVED, QA_USER_FLAGS_USER_BLOCKED
			);
	}
Gideon Greenspan committed
331 332 333 334 335


/*
	Omit PHP closing tag to help avoid accidental output
*/