/** * Delete those users from the database which corresponds to the given condition or to the given ids array * Note: the delete cascade arrays are handled! * * @param string the name of this class * Note: This is required until min phpversion will be 5.3. Since PHP 5.3 we can use static::function_name to achieve late static bindings * @param string where condition * @param array object ids * @return mixed # of rows affected or false if error */ static function db_delete_where($class_name, $sql_where, $object_ids = NULL, $params = NULL) { global $DB; $DB->begin(); if (!empty($sql_where)) { $object_ids = $DB->get_col('SELECT user_ID FROM T_users WHERE ' . $sql_where); } if (!$object_ids) { // There is no user to delete $DB->commit(); return; } // Delete orphan threads per users $result = delete_orphan_threads($object_ids); // Remove deleted user(s) from posts where it was as last edit user $user_ids = implode(',', $object_ids); $result = $result && $DB->query('UPDATE T_items__item SET post_lastedit_user_ID = NULL WHERE post_lastedit_user_ID IN ( ' . $user_ids . ' )') !== false; $result = $result && $DB->query('UPDATE T_items__version SET iver_edit_user_ID = NULL WHERE iver_edit_user_ID IN ( ' . $user_ids . ' )') !== false; // Remove this user from links where it was as last edit user $result = $result && $DB->query('UPDATE T_links SET link_lastedit_user_ID = NULL WHERE link_lastedit_user_ID IN ( ' . $user_ids . ' )') !== false; if ($result) { // Delete the user(s) with all of the cascade objects $params['use_transaction'] = false; // no need to start a new transaction $result = parent::db_delete_where($class_name, $sql_where, $object_ids, $params); } if ($result !== false) { // delete the users' media folders recursively, for all deleted users $FileRootCache =& get_FileRootCache(); foreach ($object_ids as $user_ID) { if ($root_directory = $FileRootCache->get_root_dir('user', $user_ID)) { // Delete the folder only when it is detected: rmdir_r($root_directory); } } } $result !== false ? $DB->commit() : $DB->rollback(); return $result; }
/** * Delete user and dependencies from database * * Includes WAY TOO MANY requests because we try to be compatible with MySQL 3.23, bleh! * * @param Log Log object where output gets added (by reference). */ function dbdelete(&$Log) { global $DB, $Plugins; if ($this->ID == 0) { debug_die('Non persistant object cannot be deleted!'); } $deltype = param('deltype', 'string', ''); // spammer $DB->begin(); if ($deltype == 'spammer') { // If we delete user as spammer we should delete private messaged of this user $this->delete_messages(); } else { // If we delete user as not spammer we keep his comments as from anonymous user // Transform registered user comments to unregistered: $ret = $DB->query('UPDATE T_comments SET comment_author_ID = NULL, comment_author = ' . $DB->quote($this->get('preferredname')) . ', comment_author_email = ' . $DB->quote($this->get('email')) . ', comment_author_url = ' . $DB->quote($this->get('url')) . ' WHERE comment_author_ID = ' . $this->ID); if (is_a($Log, 'log')) { $Log->add('Transforming user\'s comments to unregistered comments... ' . sprintf('(%d rows)', $ret), 'note'); } } // Get list of posts that are going to be deleted (3.23) $post_list = implode(',', $DB->get_col(' SELECT post_ID FROM T_items__item WHERE post_creator_user_ID = ' . $this->ID)); if (!empty($post_list)) { // Delete comments $ret = $DB->query("DELETE FROM T_comments\n\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE comment_post_ID IN ({$post_list})"); if (is_a($Log, 'log')) { $Log->add(sprintf('Deleted %d comments on user\'s posts.', $ret), 'note'); } // Delete post extracats $ret = $DB->query("DELETE FROM T_postcats\n\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE postcat_post_ID IN ({$post_list})"); if (is_a($Log, 'log')) { $Log->add(sprintf('Deleted %d extracats of user\'s posts\'.', $ret)); // TODO: geeky wording. } // Posts will we auto-deleted by parent method } else { // no posts if (is_a($Log, 'log')) { $Log->add('No posts to delete.', 'note'); } } // Get list of sessions that are going to be deleted $sessions_SQL = new SQL(); $sessions_SQL->SELECT('sess_ID'); $sessions_SQL->FROM('T_sessions'); $sessions_SQL->WHERE('sess_user_ID = ' . $this->ID); $sessions_list = $DB->get_col($sessions_SQL->get()); if (!empty($sessions_list)) { // Delete all hit logs of this user $DB->query('DELETE FROM T_hitlog WHERE hit_sess_ID IN ( ' . $DB->quote($sessions_list) . ' )'); } // delete user involved ophan threads delete_orphan_threads($this->ID); // Remove this user from posts where it was as last edit user $DB->query('UPDATE T_items__item SET post_lastedit_user_ID = NULL WHERE post_lastedit_user_ID = ' . $this->ID); $DB->query('UPDATE T_items__version SET iver_edit_user_ID = NULL WHERE iver_edit_user_ID = ' . $this->ID); // Remove this user from links where it was as last edit user $DB->query('UPDATE T_links SET link_lastedit_user_ID = NULL WHERE link_lastedit_user_ID = ' . $this->ID); // remember ID, because parent method resets it to 0 $old_ID = $this->ID; $old_email = $this->get('email'); // Delete main object: if (!parent::dbdelete()) { $DB->rollback(); $Log->add('User has not been deleted.', 'error'); return false; } // user was deleted, also delete this user's media folder recursively $FileRootCache =& get_FileRootCache(); $root_directory = $FileRootCache->get_root_dir('user', $old_ID); rmdir_r($root_directory); if ($deltype == 'spammer') { // User was deleted as spammer, we should mark email of this user as 'Spammer' load_class('tools/model/_emailblocked.class.php', 'EmailBlocked'); $EmailBlockedCache =& get_EmailBlockedCache(); $EmailBlocked =& $EmailBlockedCache->get_by_name($old_email, false, false); if (!$EmailBlocked) { // Create new record in the T_email_blocked table $EmailBlocked = new EmailBlocked(); $EmailBlocked->set('address', $old_email); } if (!empty($EmailBlocked)) { // Save status of an email address $EmailBlocked->set('status', 'spammer'); $EmailBlocked->dbsave(); } } $DB->commit(); if (is_a($Log, 'log')) { $Log->add('Deleted User.', 'note'); } // Notify plugins: $this->ID = $old_ID; $Plugins->trigger_event('AfterUserDelete', $params = array('User' => &$this)); $this->ID = 0; return true; }