/**
* Inserts a user into the database.
*
* @return array
*/
function insert_user()
{
global $db, $cache, $plugins;
// Yes, validating is required.
if(!$this->get_validated())
{
die("The user needs to be validated before inserting it into the DB.");
}
if(count($this->get_errors()) > 0)
{
die("The user is not valid.");
}
$user = &$this->data;
$array = array('postnum', 'threadnum', 'avatar', 'avatartype', 'additionalgroups', 'displaygroup', 'icq', 'aim', 'yahoo', 'skype', 'google', 'bday', 'signature', 'style', 'dateformat', 'timeformat', 'notepad');
foreach($array as $value)
{
if(!isset($user[$value]))
{
$user[$value] = '';
}
}
$this->user_insert_data = array(
"username" => $db->escape_string($user['username']),
"password" => $user['password'],
"salt" => $user['salt'],
"loginkey" => $user['loginkey'],
"email" => $db->escape_string($user['email']),
"postnum" => (int)$user['postnum'],
"threadnum" => (int)$user['threadnum'],
"avatar" => $db->escape_string($user['avatar']),
"avatartype" => $db->escape_string($user['avatartype']),
"usergroup" => (int)$user['usergroup'],
"additionalgroups" => $db->escape_string($user['additionalgroups']),
"displaygroup" => (int)$user['displaygroup'],
"usertitle" => $db->escape_string(htmlspecialchars_uni($user['usertitle'])),
"regdate" => (int)$user['regdate'],
"lastactive" => (int)$user['lastactive'],
"lastvisit" => (int)$user['lastvisit'],
"website" => $db->escape_string($user['website']),
"icq" => (int)$user['icq'],
"aim" => $db->escape_string($user['aim']),
"yahoo" => $db->escape_string($user['yahoo']),
"skype" => $db->escape_string($user['skype']),
"google" => $db->escape_string($user['google']),
"birthday" => $user['bday'],
"signature" => $db->escape_string($user['signature']),
"allownotices" => (int)$user['options']['allownotices'],
"hideemail" => (int)$user['options']['hideemail'],
"subscriptionmethod" => (int)$user['options']['subscriptionmethod'],
"receivepms" => (int)$user['options']['receivepms'],
"receivefrombuddy" => (int)$user['options']['receivefrombuddy'],
"pmnotice" => (int)$user['options']['pmnotice'],
"pmnotify" => (int)$user['options']['pmnotify'],
"showimages" => (int)$user['options']['showimages'],
"showvideos" => (int)$user['options']['showvideos'],
"showsigs" => (int)$user['options']['showsigs'],
"showavatars" => (int)$user['options']['showavatars'],
"showquickreply" => (int)$user['options']['showquickreply'],
"showredirect" => (int)$user['options']['showredirect'],
"tpp" => (int)$user['options']['tpp'],
"ppp" => (int)$user['options']['ppp'],
"invisible" => (int)$user['options']['invisible'],
"style" => (int)$user['style'],
"timezone" => $db->escape_string($user['timezone']),
"dstcorrection" => (int)$user['options']['dstcorrection'],
"threadmode" => $user['options']['threadmode'],
"daysprune" => (int)$user['options']['daysprune'],
"dateformat" => $db->escape_string($user['dateformat']),
"timeformat" => $db->escape_string($user['timeformat']),
"regip" => $db->escape_binary($user['regip']),
"language" => $db->escape_string($user['language']),
"showcodebuttons" => (int)$user['options']['showcodebuttons'],
"sourceeditor" => (int)$user['options']['sourceeditor'],
"buddyrequestspm" => (int)$user['options']['buddyrequestspm'],
"buddyrequestsauto" => (int)$user['options']['buddyrequestsauto'],
"away" => (int)$user['away']['away'],
"awaydate" => (int)$user['away']['date'],
"returndate" => $user['away']['returndate'],
"awayreason" => $db->escape_string($user['away']['awayreason']),
"notepad" => $db->escape_string($user['notepad']),
"referrer" => (int)$user['referrer_uid'],
"referrals" => 0,
"buddylist" => '',
"ignorelist" => '',
"pmfolders" => '',
"notepad" => '',
"warningpoints" => 0,
"moderateposts" => 0,
"moderationtime" => 0,
"suspendposting" => 0,
"suspensiontime" => 0,
"coppauser" => (int)$user['coppa_user'],
"classicpostbit" => (int)$user['options']['classicpostbit'],
"usernotes" => ''
);
if($user['options']['dstcorrection'] == 1)
{
$this->user_insert_data['dst'] = 1;
}
else if($user['options']['dstcorrection'] == 0)
{
$this->user_insert_data['dst'] = 0;
}
$plugins->run_hooks("datahandler_user_insert", $this);
$this->uid = $db->insert_query("users", $this->user_insert_data);
$user['user_fields']['ufid'] = $this->uid;
$pfcache = $cache->read('profilefields');
if(is_array($pfcache))
{
foreach($pfcache as $profile_field)
{
if(array_key_exists("fid{$profile_field['fid']}", $user['user_fields']))
{
continue;
}
$user['user_fields']["fid{$profile_field['fid']}"] = '';
}
}
$db->insert_query("userfields", $user['user_fields'], false);
if($this->user_insert_data['referrer'] != 0)
{
$db->write_query("
UPDATE ".TABLE_PREFIX."users
SET referrals=referrals+1
WHERE uid='{$this->user_insert_data['referrer']}'
");
}
// Update forum stats
update_stats(array('numusers' => '+1'));
if((int)$user['usergroup'] == 5)
{
$cache->update_awaitingactivation();
}
$this->return_values = array(
"uid" => $this->uid,
"username" => $user['username'],
"loginkey" => $user['loginkey'],
"email" => $user['email'],
"password" => $user['password'],
"usergroup" => $user['usergroup']
);
$plugins->run_hooks("datahandler_user_insert_end", $this);
return $this->return_values;
}