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

/*
* Usage : inserts a calendar event into a6 caldav.
*/

define('APP_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
set_include_path(APP_ROOT . "../../application/models/" . PATH_SEPARATOR . APP_ROOT . "../../library/" . PATH_SEPARATOR . APP_ROOT . "../../" . PATH_SEPARATOR . APP_ROOT . "../../library/Atmail/CalDav" . PATH_SEPARATOR . "library/" . PATH_SEPARATOR . get_include_path());

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

// Lets give the script some ram!
// remove php time limit
ini_set('memory_limit', -1);
set_time_limit(0);

mb_internal_encoding('UTF-8');
iconv_set_encoding("internal_encoding", "UTF-8");
iconv_set_encoding("input_encoding", "BASE-64");
iconv_set_encoding("output_encoding", "UTF-8");

require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$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/RenderHelpers.php', null, true);
Zend_Loader::loadFile('Atmail/Exception.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);
Zend_Registry::set('config', $config);
$locale = new Zend_Locale();
Zend_Registry::set('Zend_Locale', $locale);
session_name('ATMAIL6');

// Load the CalDavUrl from the DB
$cal_url = $config->defaultUserSettings['CalDavUrl'];

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

include 'application/models/calendar.php';

$calname 		= $argv[1];
$user 			= $argv[2];
$password 		= $argv[3];
$color 			= $argv[4];
$UserFrom=$UserTo=$user;

// caldav glue	
$entry_username = $UserFrom;
$entry_password = $password;
$url = $cal_url;

// ASSUME : that the user exists, the user import will have run already.
$_calendar = new calendar(array('URL' => $url, 'Account' => $entry_username, 'Password' => $entry_password, 'Calendar' => "calendar", 'home', 'largeInstall' => 0));
$calendar = $_calendar->make_calendar($entry_username, $calname);
echo $calendar->url . "\n";


