<?php

/**
 *
 * @category        admin
 * @package         pages
 * @author          Ryan Djurovich (2004-2009), WebsiteBaker Project
 * @copyright       2009-2012, WebsiteBaker Org. e.V.
 * @link			http://www.websitebaker2.org/
 * @license         http://www.gnu.org/licenses/gpl.html
 * @platform        WebsiteBaker 2.8.x
 * @requirements    PHP 5.2.2 and higher
 * @version         $Id: delete.php 2070 2014-01-03 01:21:42Z darkviper $
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/users/delete.php $
 * @lastmodified    $Date: 2014-01-03 02:21:42 +0100 (Fri, 03 Jan 2014) $
 *
 */

/* -------------------------------------------------------- */
// Must include code to stop this file being accessed directly
if(!defined('WB_URL')) {
	require_once(dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
	throw new IllegalFileException();
}
/* -------------------------------------------------------- */
function delete_user($admin, &$aActionRequest)
{
	$database = WbDatabase::getInstance();
	$mLang = Translate::getInstance();
    $aUserID = array();
    $bRetVal = false;

    $action = 'default';
    $action = (isset($aActionRequest['delete']) ? 'delete' : $action );
    $action = (isset($aActionRequest['delete_outdated']) ? 'delete_outdated' : $action );

	switch($action) :
		case 'delete': // delete the user
    	    if(isset($aActionRequest['user_id'])) {
    			if(!is_array($aActionRequest['user_id'])) {
    		        $aUserID[] = $aActionRequest['user_id'];
    		    } else {
    		        $aUserID = $aActionRequest['user_id'];
    		    }
    	    } 
    		break;
		case 'delete_outdated': // delete Users awaiting activation
            if(isset($aActionRequest['activation_user_id'])) {
        		if(!is_array($aActionRequest['activation_user_id'])) {
        	        $aUserID[] = $aActionRequest['activation_user_id'];
        	    } else {
        	        $aUserID = $aActionRequest['activation_user_id'];
        	    }
            }
    		break;
		default: // show userlist with empty modify mask
	endswitch; // end of switch
    
//    if(isset($aActionRequest['activation_user_id'])) {
//		if(!is_array($aActionRequest['activation_user_id'])) {
//	
//	        $aUserID[] = $aActionRequest['activation_user_id'];
//	    } else {
//	        $aUserID = $aActionRequest['activation_user_id'];
//	    }
//    } else {
//	    if(isset($aActionRequest['user_id'])) {
//			if(!is_array($aActionRequest['user_id'])) {
//		
//		        $aUserID[] = $aActionRequest['user_id'];
//		    } else {
//		        $aUserID = $aActionRequest['user_id'];
//		    }
//	    } 
//    } 
    

    foreach ( $aUserID AS $key => $value)
    {
        switch ($_SERVER['REQUEST_METHOD']) :
			case 'GET': // insert/update user
                $_GET['user_id'] =$aUserID[$key];
				break;
			default: // show userlist with empty modify mask
                $_POST['user_id'] =$aUserID[$key];
		endswitch; // end of switch
		$user_id = intval($admin->checkIDKEY('user_id', 0, $_SERVER['REQUEST_METHOD']));

		// Check if user id is a valid number and doesnt equal 1
		if($user_id == 0){
			msgQueue::add($mLang->MESSAGE_GENERIC_FORGOT_OPTIONS );
            return $bRetVal;
        }

		if( ($user_id < 2 ) )
		{
			// if($admin_header) { $admin->print_header(); }
			msgQueue::add($mLang->MESSAGE_GENERIC_SECURITY_ACCESS );
            return $bRetVal;
		}

		if( ($msg = msgQueue::getError()) == '')
		{
			$sql  = 'SELECT `active` FROM `'.TABLE_PREFIX.'users` '.
                    'WHERE `user_id` = '.$user_id;
            if( ($iDeleteUser = $database->get_one($sql)) != null ) {
                if($iDeleteUser) {
    				// Deactivate the user
        			$sql  = 'UPDATE `'.TABLE_PREFIX.'users` SET '.
                            '`active` = 0 '.
                            'WHERE `user_id` = '.$user_id;
                    if( $database->query($sql) ) {
                        msgQueue::add($mLang->TEXT_USERS_MARKED_DELETED, true);
                    }
                } else {


        			$sql  = 'DELETE FROM `'.TABLE_PREFIX.'users` '.
                            'WHERE `user_id` = '.$user_id;
                    if( $database->query($sql) ) {
                        msgQueue::add($mLang->MESSAGE_USERS_DELETED, true);
                    }
                }
                $bRetVal = true;
            }
            if($database->is_error()) {
                msgQueue::add( implode('<br />',explode(';',$database->get_error())) );
                $bRetVal = false;
           }
		}
    }
    if(isset($aActionRequest['clearmsg'])) { msgQueue::clear();  }
    return $bRetVal;
}

if(!isset($aActionRequest)) {
    $requestMethod = '_'.strtoupper($_SERVER['REQUEST_METHOD']);
    $aActionRequest = (isset(${$requestMethod})) ? ${$requestMethod} : null;
    $aActionRequest['clearmsg'] = true;
}
