Commit f444c0c1 authored by Sebastian Kummer's avatar Sebastian Kummer

ZO-28 Replace newlines by php constant, add Terminate() to log and exit,

remove default username+password from the config file.
parent 3d27b16c
......@@ -23,8 +23,8 @@ define('UNIQUEID', 'account');
// server connection settings
define('SERVER', 'file:///var/run/zarafa');
define('USERNAME', 'ahall');
define('PASSWORD', 'ahall');
define('USERNAME', '');
define('PASSWORD', '');
define('CERTIFICATE', null);
define('CERTIFICATE_PASSWORD', null);
......
......@@ -24,20 +24,20 @@ include_once(SYNC_CONFIG);
if (! GabSyncCLI::SureWhatToDo()) {
// show error message if available
if (GabSyncCLI::GetErrorMessage())
fwrite(STDERR, GabSyncCLI::GetErrorMessage() . "\n\n");
fwrite(STDERR, GabSyncCLI::GetErrorMessage() . PHP_EOL.PHP_EOL);
echo GabSyncCLI::UsageInstructions();
exit(1);
}
else if (!GabSyncCLI::SetupSyncher()) {
fwrite(STDERR, GabSyncCLI::GetErrorMessage() . "\n");
fwrite(STDERR, GabSyncCLI::GetErrorMessage() . PHP_EOL);
exit(1);
}
GabSyncCLI::RunCommand();
}
catch (Exception $ex) {
die(get_class($ex) . ": ". $ex->getMessage() . "\n");
die(get_class($ex) . ": ". $ex->getMessage() . PHP_EOL);
}
......@@ -64,15 +64,18 @@ class GabSyncCLI {
* @access public
*/
static public function UsageInstructions() {
return "Usage:\n\tgab-sync.php -a ACTION [options]\n\n" .
"Parameters:\n\t-a simulate | sync | sync-one | clear-all | delete-all\n\t[-u] UNIQUE-ID\n\n" .
"Actions:\n" .
"\tsimulate\t\t Simulates the GAB synchronization and prints out statistics and configuration suggestions.\n" .
"\tsync\t\t\t Synchronizes all data from the GAB to the global folder, clearing all existing data if configuration changed!\n" .
"\tsync-one -u UNIQUE-ID\t Tries do find the entry with UNIQUE-ID and updates the chunk this entry belongs to.\n" .
"\tclear-all\t\t Removes all data from the global folder.\n" .
"\tdelete-all\t\t Like clear-all but also deletes the global folder.\n" .
"\n";
return "Usage:" .PHP_EOL.
"\tgab-sync.php -a ACTION [options]" .PHP_EOL.PHP_EOL.
"Parameters:" .PHP_EOL.
"\t-a simulate | sync | sync-one | clear-all | delete-all" .PHP_EOL.
"\t[-u] UNIQUE-ID" .PHP_EOL.PHP_EOL.
"Actions:" .PHP_EOL.
"\tsimulate\t\t Simulates the GAB synchronization and prints out statistics and configuration suggestions." .PHP_EOL.
"\tsync\t\t\t Synchronizes all data from the GAB to the global folder, clearing all existing data if configuration changed!" .PHP_EOL.
"\tsync-one -u UNIQUE-ID\t Tries do find the entry with UNIQUE-ID and updates the chunk this entry belongs to." .PHP_EOL.
"\tclear-all\t\t Removes all data from the global folder." .PHP_EOL.
"\tdelete-all\t\t Like clear-all but also deletes the global folder." .PHP_EOL.
PHP_EOL;
}
/**
......@@ -137,7 +140,7 @@ class GabSyncCLI {
if (isset($options['a']) && !empty($options['a']))
$action = strtolower(trim($options['a']));
elseif (isset($options['action']) && !empty($options['action']))
$action = strtolower(trim($options['action']));
$action = strtolower(trim($options['action']));
// get a command for the requested action
switch ($action) {
......@@ -204,7 +207,7 @@ class GabSyncCLI {
* @access public
*/
static public function RunCommand() {
echo "\n";
echo PHP_EOL;
switch(self::$command) {
case self::COMMAND_SIMULATE:
self::$syncher->Simulate();
......@@ -224,7 +227,7 @@ class GabSyncCLI {
if ( $confirm === 'y' || $confirm === 'yes')
self::$syncher->ClearAll();
else
echo "Aborted!\n";
echo "Aborted!".PHP_EOL;
break;
case self::COMMAND_DELETEALL:
......@@ -233,10 +236,10 @@ class GabSyncCLI {
if ( $confirm === 'y' || $confirm === 'yes')
self::$syncher->DeleteAll();
else
echo "Aborted!\n";
echo "Aborted!".PHP_EOL;
break;
}
echo "\n";
echo PHP_EOL;
}
}
\ No newline at end of file
......@@ -27,7 +27,7 @@ abstract class Syncher {
* Simulates the synchronization, showing statistics but without touching any data.
*/
public function Simulate() {
$this->Log("Simulating the synchronization. NO DATA IS GOING TO BE WRITTEN.\n");
$this->Log("Simulating the synchronization. NO DATA IS GOING TO BE WRITTEN.".PHP_EOL);
$this->Sync(false);
}
......@@ -159,7 +159,7 @@ abstract class Syncher {
public function ClearAll() {
$folderid = $this->GetHiddenFolderId();
if (!$folderid) {
$this->Log("Could not locate folder. Aborting.", true);
$this->Terminate("Could not locate folder. Aborting.");
}
$status = $this->ClearFolderContents($folderid);
......@@ -178,7 +178,7 @@ abstract class Syncher {
public function DeleteAll() {
$folderid = $this->GetHiddenFolderId();
if (!$folderid) {
$this->Log("Could not locate folder. Aborting.", true);
$this->Terminate("Could not locate folder. Aborting.");
}
$emptystatus = $this->ClearFolderContents($folderid);
if ($emptystatus) {
......@@ -193,19 +193,26 @@ abstract class Syncher {
* Logs a message to the command line.
*
* @param string $msg the message
* @param boolean $error if set to true, message will be printed to STDERR and the script terminates.
*
* @access protected
* @return void
*/
protected function Log($msg, $error = false) {
if ($error) {
fwrite(STDERR, $msg);
echo "\n\n";
exit(1);
}
else
echo $msg . "\n";
echo $msg . PHP_EOL;
}
/**
* Writes a message to STDERR and terminates the script.
*
* @param string $msg the message
*
* @access protected
* @return void
*/
protected function Terminate($msg) {
fwrite(STDERR, $msg);
echo PHP_EOL.PHP_EOL;
exit(1);
}
/**
......
......@@ -35,7 +35,7 @@ class Zarafa extends Syncher {
parent::__construct();
$this->session = mapi_logon_zarafa(USERNAME, PASSWORD, SERVER, CERTIFICATE, CERTIFICATE_PASSWORD, 0, self::VERSION, self::NAME. " ". self::VERSION);
if (mapi_last_hresult()) {
$this->Log(sprintf("Zarafa: login failed with error code: 0x%08X", mapi_last_hresult()), true);
$this->Terminate(sprintf("Zarafa: login failed with error code: 0x%08X", mapi_last_hresult()));
}
$this->mainUser = USERNAME;
$this->store = $this->openMessageStore(HIDDEN_FOLDERSTORE);
......@@ -66,7 +66,7 @@ class Zarafa extends Syncher {
// mapi_folder_createfolder() fails if a folder with this name already exists -> MAPI_E_COLLISION
$newfolder = mapi_folder_createfolder($parentfolder, HIDDEN_FOLDERNAME, "");
if (mapi_last_hresult())
$this->Log(sprintf("Zarafa->CreateHiddenPublicFolder(): Error, mapi_folder_createfolder() failed: 0x%08X", mapi_last_hresult()), true);
$this->Terminate(sprintf("Zarafa->CreateHiddenPublicFolder(): Error, mapi_folder_createfolder() failed: 0x%08X", mapi_last_hresult()));
// TODO: set PR_HIDDEN
mapi_setprops($newfolder, array(PR_CONTAINER_CLASS => "IPF.Appointment"));
......@@ -78,7 +78,7 @@ class Zarafa extends Syncher {
return $sourcekey;
}
else
$this->Log(sprintf("Zarafa->CreateHiddenPublicFolder(): Error, folder created but PR_SOURCE_KEY not available: 0x%08X", mapi_last_hresult()), true);
$this->Terminate(sprintf("Zarafa->CreateHiddenPublicFolder(): Error, folder created but PR_SOURCE_KEY not available: 0x%08X", mapi_last_hresult()));
return false;
}
......@@ -95,11 +95,11 @@ class Zarafa extends Syncher {
$folderentryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($folderid));
if (mapi_last_hresult())
$this->Log(sprintf("Zarafa->DeletesHiddenPublicFolder(): Error, could not get PR_ENTRYID for hidden folder: 0x%08X", mapi_last_hresult()), true);
$this->Terminate(sprintf("Zarafa->DeletesHiddenPublicFolder(): Error, could not get PR_ENTRYID for hidden folder: 0x%08X", mapi_last_hresult()));
mapi_folder_deletefolder($parentfolder, $folderentryid);
if (mapi_last_hresult())
$this->Log(sprintf("Zarafa->DeletesHiddenPublicFolder(): Error, mapi_folder_deletefolder() failed: 0x%08X", mapi_last_hresult()), true);
$this->Terminate(sprintf("Zarafa->DeletesHiddenPublicFolder(): Error, mapi_folder_deletefolder() failed: 0x%08X", mapi_last_hresult()));
return true;
}
......@@ -143,7 +143,7 @@ class Zarafa extends Syncher {
$flags = 0;
mapi_folder_emptyfolder($folder, $flags);
if (mapi_last_hresult())
$this->Log("Zarafa->ClearFolderContents: Error, mapi_folder_emptyfolder() failed: 0x%08X", true);
$this->Terminate("Zarafa->ClearFolderContents: Error, mapi_folder_emptyfolder() failed: 0x%08X");
return true;
}
......@@ -161,7 +161,7 @@ class Zarafa extends Syncher {
$table = mapi_folder_getcontentstable($folder);
if (!$table)
$this->Log("Zarafa->ClearAllNotCurrentChunkType: Error, unable to read contents table.", true);
$this->Terminate("Zarafa->ClearAllNotCurrentChunkType: Error, unable to read contents table.");
$mapiprops = array("location" =>"PT_STRING8:PSETID_Appointment:0x8208");
$mapiprops = getPropIdsFromStrings($this->store, $this->mapiprops);
......@@ -206,7 +206,7 @@ class Zarafa extends Syncher {
$addrbook = mapi_openaddressbook($this->session);
$result = mapi_last_hresult();
if ($result)
$this->Log(sprintf("Zarafa->GetFullGAB: Error opening addressbook 0x%08X", $result), true);
$this->Terminate(sprintf("Zarafa->GetFullGAB: Error opening addressbook 0x%08X", $result));
if ($addrbook)
$ab_entryid = mapi_ab_getdefaultdir($addrbook);
......@@ -216,7 +216,7 @@ class Zarafa extends Syncher {
$table = mapi_folder_getcontentstable($ab_dir);
if (!$table)
$this->Log(sprintf("Zarafa->GetFullGAB: error, could not open addressbook: 0x%08X", $result), true);
$this->Terminate(sprintf("Zarafa->GetFullGAB: error, could not open addressbook: 0x%08X", $result));
// restrict the table if we should only return one
if ($uniqueId) {
......@@ -225,10 +225,10 @@ class Zarafa extends Syncher {
mapi_table_restrict($table, $restriction);
$querycnt = mapi_table_getrowcount($table);
if ($querycnt == 0) {
$this->Log(sprintf("Zarafa->GetGAB(): Single GAB entry '%s' requested but could not be found.", $uniqueId), true);
$this->Terminate(sprintf("Zarafa->GetGAB(): Single GAB entry '%s' requested but could not be found.", $uniqueId));
}
elseif ($querycnt > 1) {
$this->Log(sprintf("Zarafa->GetGAB(): Single GAB entry '%s' requested but %d entries found. Aborting.", $uniqueId, $querycnt), true);
$this->Terminate(sprintf("Zarafa->GetGAB(): Single GAB entry '%s' requested but %d entries found. Aborting.", $uniqueId, $querycnt));
}
}
......@@ -483,11 +483,11 @@ class Zarafa extends Syncher {
}
if (!$parentfentryid)
$this->Log("Zarafa->getRootFolder(): Error, unable to open parent folder (no entry id)", true);
$this->Terminate("Zarafa->getRootFolder(): Error, unable to open parent folder (no entry id)");
$parentfolder = mapi_msgstore_openentry($this->store, $parentfentryid);
if (!$parentfolder)
$this->Log("Zarafa->CreateHiddenPublicFolder(): Error, unable to open parent folder (open entry)", true);
$this->Terminate("Zarafa->CreateHiddenPublicFolder(): Error, unable to open parent folder (open entry)");
$this->folderCache[$rootId] = $parentfolder;
}
......@@ -505,7 +505,7 @@ class Zarafa extends Syncher {
if (!isset($this->folderCache[$folderid])) {
$folderentryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($folderid));
if (!$folderentryid)
$this->Log("Zarafa->getFolder: Error, unable to open folder (no entry id).", true);
$this->Terminate("Zarafa->getFolder: Error, unable to open folder (no entry id).");
$this->folderCache[$folderid] = mapi_msgstore_openentry($this->store, $folderentryid);
}
......@@ -532,7 +532,7 @@ class Zarafa extends Syncher {
break;
}
if (!$prop) {
$this->Log(sprintf("Zarafa->getPropertyForGABvalue: Could not get find a property for '%s'. Unsupported field.", $value), true);
$this->Terminate(sprintf("Zarafa->getPropertyForGABvalue: Could not get find a property for '%s'. Unsupported field.", $value));
}
return $prop;
}
......@@ -550,11 +550,11 @@ class Zarafa extends Syncher {
$stream = mapi_openproperty($message, $prop, IID_IStream, 0, 0);
$ret = mapi_last_hresult();
if ($ret == MAPI_E_NOT_FOUND) {
ZLog::Write(LOGLEVEL_DEBUG, sprintf("MAPIUtils->readPropStream: property 0x%s not found. It is either empty or not set. It will be ignored.", str_pad(dechex($prop), 8, 0, STR_PAD_LEFT)));
$this->Log(sprintf("Zarafa>readPropStream: property 0x%s not found. It is either empty or not set. It will be ignored.", str_pad(dechex($prop), 8, 0, STR_PAD_LEFT)));
return "";
}
elseif ($ret) {
ZLog::Write(LOGLEVEL_ERROR, "MAPIUtils->readPropStream error opening stream: 0X%X", $ret);
$this->Log("Zarafa->readPropStream error opening stream: 0x%08X", $ret);
return "";
}
$data = "";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment