<?php
/*
* Copyright (c) 2009-2010 ATMAIL. All rights reserved
* See http://atmail.com/license.php for license agreement
*/

/**
 * Add a user
 *
 * Arguments:
 * php ldap-add-user.php [username] [password] [domain]
 *
 * @author : Ben Duncan
 */

ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '/usr/local/atmail/webmail/library');
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '/usr/local/atmail/webmail/application/models');
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '/usr/local/atmail/webmail/');

//Error reporting
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_startup_errors', 0);
ini_set('display_errors', 0);

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

// remove php time limit
set_time_limit(0);

mb_internal_encoding('UTF-8');
iconv_set_encoding("internal_encoding", "UTF-8");//setting gave grief at some point, i suspect it was surrounding Lucene, but needed for ZF mime header decoding. this doesnt work, possible because conflicts with php.ini setting
iconv_set_encoding("input_encoding", "BASE-64");
iconv_set_encoding("output_encoding", "UTF-8");

//Site application root and paths
//all references to directories have / appended to eliminate posibility of direcory reference composition resulting in '/....' as in root 
//i.e. when composing a directory string, all components dont/cant/wont start with a / eliminating the possibility if a resulting string referring to OS root
define('APP_ROOT', dirname(__FILE__) . '' . DIRECTORY_SEPARATOR);

//Setup Zend Loader
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
//$loader->registerNamespace('App_'); //for model namespacing
$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);



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

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

//Load general functions
Zend_Loader::loadFile('Atmail/General.php', null, true);
Zend_Loader::loadFile('Atmail/Exception.php', null, true);
Zend_Loader::loadFile('users.php', null, true);

// Get database configuration    
$dbConfig = new Zend_Config_Ini('config/dbconfig.ini', 'production');
Zend_Registry::set('dbConfig', $dbConfig);
$dbAdapter = dbAdapterFactory($dbConfig);
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);

$writer = new Zend_Log_Writer_Stream('/dev/null');
$log = new Atmail_Log($writer);
$logBasePath = APP_ROOT . 'log' . DIRECTORY_SEPARATOR;
Zend_Registry::set('log', $log);

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

$users = $dbAdapter->query("select Account, Password from UserSession")->fetchAll();

print $siteBaseUrl;
foreach($users as $user)
{
	if(!users::isExternal($user['Account']))
	{
		list($username, $domain) = explode("@", $user['Account']);
		$password = $user['Password'];
		fwrite(STDOUT, "http://" . $_SERVER['argv'][1] . "/mail/auth/processlogin?emailName=" . $username . "&password=" . $password . "&emailDomain=" . $domain . "&requestedServer=localhost\n");
	}
}
