<?php
/*
* Copyright (c) 2009-2010 ATMAIL. All rights reserved
* See http://atmail.com/license.php for license agreement
*/
/**
 * Delete users from csv file
 *
 * csv format:
 * username
 *
 * @author : Ben Duncan
 */

ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '../../library');
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '../../library/Pear/');
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '../../application/models');
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '../../');

require_once('Mail/RFC822.php');

// Lets give the script some ram!
ini_set('memory_limit', -1);

// remove php time limit
set_time_limit(0);

if ($_SERVER['argc'] < 3) {
    die("Usage: php delete-users-csv.php [/path/to/users.csv] [admin-username]\n");    
}

$file = $_SERVER['argv'][1];

if (!$fh = fopen($file, 'r')) {
    die("Could not open $file\n");    
}


require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
//$loader->registerNamespace('App_'); //for model namespacing
$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);
Zend_Loader::loadFile('Atmail/General.php', null, true);
Zend_Loader::loadFile('Atmail/Exception.php', null, true);


$siteBaseUrl = substr($_SERVER["SCRIPT_NAME"],0,strpos($_SERVER["SCRIPT_NAME"],'index.php'));

Zend_Registry::set( 'siteBaseUrl', $siteBaseUrl );

// Get database configuration    
$dbConfig = new Zend_Config_Ini('config/dbconfig.ini', 'production');
Zend_Registry::set('dbConfig', $dbConfig);
$dbAdapter = Zend_Db::Factory($dbConfig->database);
$dbAdapter->query("SET NAMES 'utf8'");
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter', $dbAdapter); 

//Get main configuration from database
$config = new Atmail_Config_Mysql($dbConfig, true);

//$config->global['cacheEnabled'] = false;
$config->global['allowLocalPop3'] = true; //for testing/debugging
$config->global['allowLocalImap'] = true; //for testing/debugging 
Zend_Registry::set('config', $config);

$locale = new Zend_Locale();
Zend_Registry::set('Zend_Locale', $locale);

$log = new Atmail_Log();
$log->addPriority('atmail', 10); 
$log->addPriority('sysadmin', 20); 
$log->addPriority('imap', 30); 
$log->addPriority('firebug', 40); 

$logBasePath = APP_ROOT . 'log' . DIRECTORY_SEPARATOR;
Zend_Registry::set('log', $log);

unset($config);
unset($settings);

require_once('../../application/models/api.php');
$_SERVER['PHP_AUTH_USER'] = $_SERVER['argv'][2];
$api = new api( array('directApi' => 1) );

while (false !== $user = fgetcsv($fh, 10000, ",")) {
    // Skip blank lines
    if (is_null($user[1]))
        continue;

    // Trim whitespace from start/end of values
    $user = array_map('trim', $user);

    // Skip if migrate file is invalid
    if ( !$user[1] )
        continue;

    // Add user
    $userData = array();
    $userData['name'] = $user[1];

    $res = array();

	try {
		echo "Going to delete " . $userData['name'] . ' ';
		
	    //$res = $api->userDel($userData);		
	} catch( Exception $e ) {
		//print_r($e);
        fwrite(STDOUT, "ERROR: " . $e->getMessage() . "\n");		
	}
 
   /*
   if ($res['status']) {
        fwrite(STDOUT, "OK\n");
    }
   */

}

fwrite(STDOUT, "DONE\n");
