Created
September 27, 2011 02:49
-
-
Save smaida/1244193 to your computer and use it in GitHub Desktop.
ExpressionEngine Delete Member Account Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function cancel_account() | |
{ | |
$this->member_id = $this->EE->TMPL->fetch_param('member_id'); | |
$this->return = $this->EE->TMPL->fetch_param('return', 'site_index'); | |
$this->EE->lang->loadfile('login'); | |
/* ------------------------------------- | |
/* No sneakiness - we'll do this in case the site administrator | |
/* has foolishly turned off secure forms and some monkey is | |
/* trying to delete their account from an off-site form or | |
/* after logging out. | |
/* -------------------------------------*/ | |
if ($this->EE->session->userdata['member_id'] == 0 OR $this->EE->session->userdata['can_delete_self'] !== 'y') | |
{ | |
return $this->EE->output->show_user_error('general', $this->EE->lang->line('not_authorized')); | |
} | |
/** ------------------------------------- | |
/** If the user is a SuperAdmin, then no deletion | |
/** -------------------------------------*/ | |
if ($this->EE->session->userdata['group_id'] == 1 || $this->EE->session->userdata['group_id'] == 6) | |
{ | |
return $this->EE->output->show_user_error('general', $this->EE->lang->line('cannot_delete_super_admin')); | |
} | |
/** ---------------------------------------- | |
/** Is IP and User Agent required for login? Then, same here. | |
/** ----------------------------------------*/ | |
if ($this->EE->config->item('require_ip_for_login') == 'y') | |
{ | |
if ($this->EE->session->userdata['ip_address'] == '' OR $this->EE->session->userdata['user_agent'] == '') | |
{ | |
return $this->EE->output->show_user_error('general', $this->EE->lang->line('unauthorized_request')); | |
} | |
} | |
/** ---------------------------------------- | |
/** Check password lockout status | |
/** ----------------------------------------*/ | |
if ($this->EE->session->check_password_lockout($this->EE->session->userdata['username']) === TRUE) | |
{ | |
return $this->EE->output->show_user_error('general', str_replace("%x", $this->EE->config->item('password_lockout_interval'), $this->EE->lang->line('password_lockout_in_effect'))); | |
} | |
/** ---------------------------------------- | |
/** The logged in user is only allowed to delete their own account | |
/** ----------------------------------------*/ | |
if ($this->member_id != $this->EE->session->userdata('member_id')) | |
{ | |
$this->EE->output->show_message('error', 'You cannot cancel this account.'); | |
} | |
/** ---------------------------------------- | |
/** Cancel account start hook | |
/** ----------------------------------------*/ | |
$this->EE->extensions->call('vc_cancel_account_start', $this->member_id); | |
if ($this->EE->extensions->end_script === TRUE) return; | |
/** ------------------------------------- | |
/** No turning back, get to deletin'! | |
/** -------------------------------------*/ | |
$this->EE->db->query("DELETE FROM exp_members WHERE member_id = '{$this->member_id}'"); | |
$this->EE->db->query("DELETE FROM exp_member_data WHERE member_id = '{$this->member_id}'"); | |
$this->EE->db->query("DELETE FROM exp_member_homepage WHERE member_id = '{$this->member_id}'"); | |
$message_query = $this->EE->db->query("SELECT DISTINCT recipient_id FROM exp_message_copies WHERE sender_id = '{$this->member_id}' AND message_read = 'n'"); | |
$this->EE->db->query("DELETE FROM exp_message_copies WHERE sender_id = '{$this->member_id}'"); | |
$this->EE->db->query("DELETE FROM exp_message_data WHERE sender_id = '{$this->member_id}'"); | |
$this->EE->db->query("DELETE FROM exp_message_folders WHERE member_id = '{$this->member_id}'"); | |
$this->EE->db->query("DELETE FROM exp_message_listed WHERE member_id = '{$this->member_id}'"); | |
if ($message_query->num_rows() > 0) | |
{ | |
foreach($message_query->result_array() as $row) | |
{ | |
$count_query = $this->EE->db->query("SELECT COUNT(*) AS count FROM exp_message_copies WHERE recipient_id = '".$row['recipient_id']."' AND message_read = 'n'"); | |
$this->EE->db->query($this->EE->db->update_string('exp_members', array('private_messages' => $count_query->row('count') ), "member_id = '".$row['recipient_id']."'")); | |
} | |
} | |
/** ---------------------------------------- | |
/** Kill friendships | |
/** ----------------------------------------*/ | |
if (is_file(PATH_THIRD.'friends/ext.friends'.EXT)) | |
{ | |
require_once PATH_THIRD.'friends/ext.friends'.EXT; | |
Friends_extension_base::delete_non_existent_members(); | |
} | |
/** ---------------------------------------- | |
/** Kill favorites | |
/** ----------------------------------------*/ | |
if (is_file(PATH_THIRD.'favorites/ext.favorites'.EXT)) | |
{ | |
require_once PATH_THIRD.'favorites/ext.favorites'.EXT; | |
$favorites = new Favorites_extension_base; | |
$favorites->delete_members(); | |
} | |
/** ---------------------------------------- | |
/** Kill ratings | |
/** ----------------------------------------*/ | |
if (is_file(PATH_THIRD.'rating/act.rating'.EXT)) | |
{ | |
require_once PATH_THIRD.'rating/act.rating'.EXT; | |
$this->EE->db->delete('ratings', array('rating_author_id' => $this->member_id)); | |
$this->EE->db->where('reply_author_id', $this->member_id); | |
$this->EE->db->update('ratings', array('reply_author_id' => NULL, 'reply_content' => NULL, 'reply_date' => NULL)); | |
$ratings = new Rating_actions(); | |
$ratings->actions()->update_channel_stats(array(1,2,4,5,6,7)); | |
} | |
/** ------------------------------------- | |
/** Va-poo-rize Channel Entries and Comments | |
/** -------------------------------------*/ | |
// Reassign business entries | |
$data = array( | |
'status' => 'open', | |
'author_id' => $this->vc_user | |
); | |
$this->EE->db->where('author_id', $this->member_id); | |
$this->EE->db->where_in('channel_id', $this->business_channels); | |
$this->EE->db->update('channel_titles', $data); | |
// Kill data | |
$entry_ids = array(); | |
$channel_ids = array(); | |
$recount_ids = array(); | |
// Find Entry IDs and Channel IDs, then delete | |
$query = $this->EE->db->select("entry_id, channel_id") | |
->from("channel_titles") | |
->where("author_id", $this->member_id) | |
->where_not_in("channel_id", $this->business_channels) | |
->get(); | |
if ($query->num_rows() > 0) | |
{ | |
foreach ($query->result_array() as $row) | |
{ | |
$entry_ids[] = $row['entry_id']; | |
$channel_ids[] = $row['channel_id']; | |
} | |
$this->EE->db->query("DELETE FROM exp_channel_titles WHERE entry_id IN ('".implode("','", $entry_ids)."')"); | |
$this->EE->db->query("DELETE FROM exp_channel_data WHERE entry_id IN ('".implode("','", $entry_ids)."')"); | |
} | |
// Find the affected entries AND channel ids for author's comments | |
$query = $this->EE->db->query("SELECT DISTINCT(entry_id), channel_id FROM exp_comments WHERE author_id = '{$this->member_id}'"); | |
if ($query->num_rows() > 0) | |
{ | |
foreach ($query->result_array() as $row) | |
{ | |
$recount_ids[] = $row['entry_id']; | |
$channel_ids[] = $row['channel_id']; | |
} | |
$recount_ids = array_diff($recount_ids, $entry_ids); | |
} | |
// Delete comments by member | |
$this->EE->db->query("DELETE FROM exp_comments WHERE author_id = '{$this->member_id}'"); | |
// Update stats on channel entries that were NOT deleted AND had comments by author | |
if (count($recount_ids) > 0) | |
{ | |
foreach (array_unique($recount_ids) as $entry_id) | |
{ | |
$query = $this->EE->db->query("SELECT MAX(comment_date) AS max_date FROM exp_comments WHERE status = 'o' AND entry_id = '".$this->EE->db->escape_str($entry_id)."'"); | |
$comment_date = ($query->num_rows() == 0 OR ! is_numeric($query->row('max_date') )) ? 0 : $query->row('max_date') ; | |
$query = $this->EE->db->query("SELECT COUNT(*) AS count FROM exp_comments WHERE entry_id = '{$entry_id}' AND status = 'o'"); | |
$this->EE->db->query("UPDATE exp_channel_titles SET comment_total = '".$this->EE->db->escape_str($query->row('count') )."', recent_comment_date = '$comment_date' WHERE entry_id = '{$entry_id}'"); | |
} | |
} | |
if (count($channel_ids) > 0) | |
{ | |
foreach (array_unique($channel_ids) as $channel_id) | |
{ | |
$this->EE->stats->update_channel_stats($channel_id); | |
$this->EE->stats->update_comment_stats($channel_id); | |
} | |
} | |
/** ------------------------------------- | |
/** Trash the Session and cookies | |
/** -------------------------------------*/ | |
$this->EE->db->query("DELETE FROM exp_online_users WHERE site_id = '".$this->EE->db->escape_str($this->EE->config->item('site_id'))."' AND ip_address = '".$this->EE->input->ip_address()."' AND member_id = '{$this->member_id}'"); | |
$this->EE->db->query("DELETE FROM exp_sessions WHERE session_id = '".$this->EE->session->userdata['session_id']."'"); | |
$this->EE->functions->set_cookie($this->EE->session->c_uniqueid); | |
$this->EE->functions->set_cookie($this->EE->session->c_password); | |
$this->EE->functions->set_cookie($this->EE->session->c_session); | |
$this->EE->functions->set_cookie($this->EE->session->c_expire); | |
$this->EE->functions->set_cookie($this->EE->session->c_anon); | |
$this->EE->functions->set_cookie('read_topics'); | |
$this->EE->functions->set_cookie('tracker'); | |
/** ------------------------------------- | |
/** Update | |
/** -------------------------------------*/ | |
$this->EE->stats->update_member_stats(); | |
/** ------------------------------------- | |
/** Cancel account end hook | |
/** -------------------------------------*/ | |
$this->EE->extensions->call('vc_cancel_account_end', $this->member_id); | |
if ($this->EE->extensions->end_script === TRUE) return; | |
/** ------------------------------------- | |
/** Get out of here | |
/** -------------------------------------*/ | |
$this->EE->functions->redirect($this->EE->functions->create_url('?ACT=17')); | |
$this->_redirect($this->return); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment