How to delete user and node, drupal 7? - php

I have created a module which adds a new node(with user info) if a user is added. Now I want to implement a user_delete hook that deletes the node if the user is deleted. I don't know how to implement this.
This code adds a new node for new user:
function user_profile_user_insert(&$edit, $account, $category) {
$node = new stdClass();
$node->type = 'members';
node_object_prepare($node);
//dvm($account);
$node->title = $account->name;
$user_info = user_load($account->uid);
$node->field_user["und"][0]["uid"] = $account->uid;
$path = $account->name;
$node->path = array('alias' => $path);
node_save($node);
}

try this
function node_delete($nid) {
node_delete_multiple(array($nid));
}

for user you can use
user_delete where you pass the $uid as the parameter
as for nodes there is node_delete you pass you pass the $nid as the parameter.
also deleting nodes based on user id there is this function node_user_delete

Until I solved the user reg spam problem with the stop_spam_reg module, I routinely purged the spam accounts like this:
/devel/php
$query = db_select('users', 'u') ->fields('u', array('uid'));
$result = $query->execute();
foreach ($result as $record) {
if( $record->uid>1){ //replace 1 with the latest valid uid you want to keep
user_delete($record->uid);
}
}
Be careful to leave user 1 or you'll have bigger issues than spam users :)

Related

PHP: Script failing after PDO query

During my create user process I make a few queries to various database's to get the new user setup. This script has been working fine for about a year and a half, but now something is off.
So the first thing I do is I check to see if a user exists with the credentials being submitted. I've thoroughly tested the check and I'm confident my issue isn't there.
If that check comes back false then the script continues to create the user.
public function registerUser() {
parse_str($_SERVER['QUERY_STRING'], $data);
$data = (object) $data;
$check = json_decode($this->checkUserExists($data->email));
if ($check->res) {
$res = new \stdClass();
$res->res = false;
$res->user_status = $check->user_status;
$res->msg = 'User exists.';
echo json_encode($res);
}
if (!$check->res) {
$this->createUser($data);
}
}
The problem arises after all the queries have been completed, the script does not seem to want to run the if statement at the bottom. I marked it with comment characters so it's easier to find, but I included the entire function for clarity, maybe I'm doing something that is causing the issue.
I tried invoking an error manually at various points during the script. And I am able to trigger an error all the way down to the bottom of the script.
private function createUser($data) {
$Crypt = new CryptController();
$AuthSelect = new AuthController();
$Time = new TimeController();
$remote_address = new RemoteAddressController();
$Session = new SessionController();
$AuthInsert = new AuthModel_Insert();
$hashed_password = $Crypt->create_hash($data->password);
$data->password = '';
$AuthData = json_decode($AuthSelect->getAuth());
$system_auth_id = $AuthData->system_auth_id;
$user_id = $Crypt->get_uuid();
$user_auth_id = $Crypt->get_uuid();
$user_createddate = $Time->time();
$user_updateddate = $Time->time();
$user_lastupdateddate = $Time->time();
$agent_ip = $remote_address->getIpAddress();
$userData = $this->createUserObject(
$user_id,
$user_auth_id,
$system_auth_id,
$hashed_password,
$user_createddate,
$user_updateddate,
$user_lastupdateddate,
$data
);
$agentData = $this->createAgentObject(
$user_id,
$agent_ip,
$data
);
//////////////////////////////////////////
$create_user = $AuthInsert->createNewUser(
$userData
);
$create_user_agent = $this->setUserAgent(
$agentData
);
$sessionKeyData = new \stdClass();
$sessionKeyData->user_id = $user_id;
$sessionKeyData->user_auth_id = $user_auth_id;
$sessionKeyData->system_auth_id = $system_auth_id;
$sessionKeyData->agent_id = $create_user_agent->agent->agent_id;
$set_session_key = $Session->setSessionKey(
$sessionKeyData
);
$send_activation_email = $this->createUserActivation(
$userData
);
if (
$create_user &&
$create_user_agent->res &&
$set_session_key->res &&
$send_activation_email->res) {
$res = new \stdClass();
$res->res = true;
$res->msg = 'New user successfully created.';
echo json_encode($res);
} else {
$res = new \stdClass();
$res->res = false;
$res->msg = 'Error: User creation process incomplete.';
echo json_encode($res);
}
//////////////////////////////////////////
trigger_error("Invoked Error: ",E_USER_ERROR);
}
The queries themselves go through just fine, all the tables are populated just fine. The issue is that after that happens the script doesn't finish. It seems to end the createUser() function and return to the registerUser() function at which point the user will exist so it will return false and echo that back to the client.
In my testing it seems my issue might be at the bottom with that if statement. But I've tested each of those queries individually and they do return the desired booleans to get the true condition. But, even the false condition doesn't go through which should return 'Error: User creation process incomplete.'. That doesn't happen either.
I'm hoping someone sees something I'm missing because I've been stuck on this problem for too long. I appreciate any guidance that might lead me to an answer. Thanks in advance.
Just for clarification the message I'm getting back is $res->msg = 'User exists.'; which comes from registeruser(). The message I'm expecting back is $res->msg = 'New user successfully created.'; which should come from createUser().

PHP getElementById not working

So I'm trying to write a short function using PHP to check whether a server (or the back up) is available. The service provides two servers to use, and a page within the server that simply has "OK" in an element with id "server_status". I basically took their code that they provided and adjusted it so that it provides the kind of output I need. I want to get an array of true or false (depending on whether one of the sites is available), and the correct page if it is. Right now the output every time is (false, "e404.html"), which is what I set it up to output if no conditions are met. Here is my code:
function checkURL() {
$servers = array('tpeweb.paybox.com', // primary URL
'tpeweb1.paybox.com'); // backup URL
foreach($servers as $server){
$doc = new DOMDocument();
$doc->loadHTMLFile('https://'.$server.'/load.html');
$server_status = "";
$element = $doc->getElementById('server_status');
if($element){
$server_status = $element->textContent;
}
if($server_status == "OK"){
// Server is up and services are available
return array(true, 'https://'.$server.'/cgi/MYchoix_pagepaiement.cgi');
}
}
return array(false, 'e404.html');
}
Doing some output testing, it appears that I'm loading the document into $doc, but it doesn't fill $element. I'm new to PHP so I'm not quite sure what is wrong.
EDIT:
This is the original code that the service provided to make this check, I adjusted it because I needed to be able to actually output the link to use:
<?php
$servers = array('urlserver.paybox.com', // primary URL
'urlserver1.paybox.com'); // backup URL
$serverOK = "";
foreach($servers as $server){
$doc = new DOMDocument();
$doc->loadHTMLFile('https://'.$server.'/load.html');
$server_status = "";
$element = $doc->getElementById('server_status');
if($element){
$server_status = $element->textContent;
}
if($server_status == "OK"){
// Server is up and services are available
$serverOK = $server;
break;
}
// else : Server is up but services are not available .
}
if(!$serverOK){
die("Error : no server found");
}
?>
//echo 'Connecting to https://'.$server.'/cgi/MYchoix_pagepaiement.cgi';
Thanks,
Adrian
Does your html file have a doctype declared?
from http://php.net/manual/en/domdocument.getelementbyid.php
For this function to work, you will need either to set some ID attributes with DOMElement::setIdAttribute or a DTD which defines an attribute to be of type ID.
It should be sufficient to include <!DOCTYPE html> at the very top of your html files, and set
$doc->validateOnParse = true; before calling the getElementByID function.

Howto disable/hide Navigation-Tab 'Status' in phpMyAdmin4 multi-user environment

I need to disable the Navigation-Tab 'Status' in a phpMyAdmin4 multi-user environment. This post explains how to do it but it seems as it is for previous versions of phpMyAdmin.
As explained in the above link there should be a file named 'server_links.inc.php'. Under 'usr/share/phpmyadmin/libraries/' there does not exist such file... These are my server_* files:
server_bin_log.lib.php
server_collations.lib.php
server_common.inc.php
server_common.lib.php
server_databases.lib.php
server_engines.lib.php
server_plugins.lib.php
server_privileges.lib.php
server_status.lib.php
server_status_advisor.lib.php
server_status_monitor.lib.php
server_status_queries.lib.php
server_status_variables.lib.php
server_user_groups.lib.php
server_users.lib.php
server_variables.lib.php
In which file is the tab-navigation stored?
How can I disable/hide the 'Status'-Tab?
How can I restrict access for SQL Queries like 'SHOW STATUS' after hiding the 'Status'-Tab?
If you want to change the menu tabs without touching the code, the user groups feature will do just that; see http://docs.phpmyadmin.net/en/latest/config.html?highlight=usergroup#cfg_Servers_usergroups.
You can can also change the code, in libraries/Menu.class.php.
This is my working code for hiding the tab 'Status' in /phpmyadmin/libraries/Menu.class.php:
if ($is_superuser) {
$tabs['status']['icon'] = 's_status.png';
$tabs['status']['link'] = 'server_status.php';
$tabs['status']['text'] = __('Status');
$tabs['status']['active'] = in_array(
basename($GLOBALS['PMA_PHP_SELF']),
array(
'server_status.php',
'server_status_advisor.php',
'server_status_monitor.php',
'server_status_queries.php',
'server_status_variables.php'
)
);
}
This is my working code in /phpmyadmin/server_status.php to deny access anyway if not superuser:
if (!$GLOBALS['dbi']->isSuperuser()) {
$msg = PMA_Message::error(__('No Privileges'));
$msg->display();
exit;
}
Note that the other files belonging to server_status.php should also contain the superuser check, so that no one can access these files directly:
'server_status_advisor.php',
'server_status_monitor.php',
'server_status_queries.php',
'server_status_variables.php'.
for newest phpmyadmin, you can edit at
libraries/classes/menu.php
refer to function _getMenu()
if (strlen((string) $this->_table) > 0) {
$tabs = $this->_getTableTabs();
$url_params['db'] = $this->_db;
$url_params['table'] = $this->_table;
$level = 'table';
} elseif (strlen($this->_db) > 0) {
$tabs = $this->_getDbTabs();
$url_params['db'] = $this->_db;
$level = 'db';
} else {
$tabs = $this->_getServerTabs();
$level = 'server';
}
and then you can modify menu you wanted to, let say edit menu table tabs, then edit function _getTableTabs. eg. you wanted to make menu export hide permanently then comment this code bellow
$tabs['export']['icon'] = 'b_tblexport';
$tabs['export']['link'] = 'tbl_export.php';
$tabs['export']['args']['single_table'] = 'true';
$tabs['export']['text'] = __('Export');
PS. this solution only for permanently hide

Create new thread via script vbulletin

I have a script to create new thread with via scritp in vbulletin
// Create a new datamanager for posting
$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');
// Set some variable and information
$forumid = 87; // The id of the forum we are posting to
$userid = 2;
$_POST["title"] = $vinanghinguyen_title;
$_POST["content"] = $final_content; // The user id of the person posting
$title = $_POST["title"]; // The title of the thread
$pagetext = $_POST["content"]; // The content of the thread
$allowsmilie = '1'; // Are we allowing smilies in our post
$visible = '1'; // If the post visible (ie, moderated or not)
// Parse, retrieve and process the information we need to post
$foruminfo = fetch_foruminfo($forumid);
$threadinfo = array();
$user = htmlspecialchars_uni( fetch_userinfo($userid) );
$threaddm->set_info('forum', $foruminfo);
$threaddm->set_info('thread', $threadinfo);
$threaddm->setr('forumid', $forumid);
$threaddm->setr('userid', $userid);
$threaddm->setr('pagetext', $pagetext);
$threaddm->setr('title', $title);
$threaddm->set('allowsmilie', $allowsmilie);
$threaddm->set('visible', $visible);
// Lets see what happens if we save the page
$threaddm->pre_save();
if(count($threaddm->errors) < 1) {
// Basically if the page will save without errors then let do it for real this time
$threadid = $threaddm->save();
unset($threaddm);
} else {
// There was errors in the practice run, so lets display them
var_dump ($threaddm->errors);
}
/*======================================================================*\
It can create new thread with title, forumid, userid.....but it not insert tag. I want insert with this script. thank for help

Update User Meta after Registration

In wordpress, after a user registers, I am using the function below to create two pages of two different custom post types, and I then need to store a custom meta value in their user data to assist with redirects later. I've found that if I specify custom meta values during registration (on registration form), I can retrieve these values later with :
global $current_user;
get_currentuserinfo();
$theirRedirectKey = $current_user->rpr_redirect_key;
However, in the following functions.php snippet, I can't the meta value to save for retrieval later.
function after_registration($user_id){
// Get the Newly Created User ID
$the_user = get_userdata($user_id);
// Get the Newly Created User Name
$new_user_name = $the_user->user_login;
// Create a unique Tour Code Prefix from User ID
$tourPrefix = $the_user->ID;
// Check for Tour Code Key if entered into registration form
$enteredKey = $the_user->rpr_redirect_key;
if($enteredKey == ''){
//Create the first Tour Builder Page
$tourBuilder = array();
$tourBuilder['post_title'] = $new_user_name . '| Custom Educational Tour';
// Next line may not be important after hubpages are set up.
$tourBuilder['post_name'] = 'builder-' . $tourPrefix;
$tourBuilder['post_type'] = 'builder';
$tourBuilder['post_content'] = 'This is the content!';
$tourBuilder['post_author'] = $user_id;
$tourBuilder['post_status'] = 'publish';
$tour_id = wp_insert_post( $tourBuilder );
// Build hubpage
$hubpage = array();
$hubpage['post_title'] = $new_user_name . '\'s Hubpage';
// URL must be unique
$hubpage['post_name'] = $new_user_name;
$hubpage['post_type'] = 'hubpages';
$hubpage['post_author'] = $user_id;
$hubpage['post_status'] = 'publish';
$hub_id = wp_insert_post( $hubpage );
//Update User with proper redirect keys for some reason this line doesn't work.
add_user_meta($the_user, 'rpr_redirect_key', '/hubpage/' . $new_user_name, true);
}
}
add_action('user_register', 'after_registration');
Help would be much appreciated.
In the line
add_user_meta( $the_user, 'rpr_redirect_key', '/hubpage/' . $new_user_name, true);
$the_user isn't the ID. Try $the_user->ID or $user_id instead

Categories