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'); ...@@ -23,8 +23,8 @@ define('UNIQUEID', 'account');
// server connection settings // server connection settings
define('SERVER', 'file:///var/run/zarafa'); define('SERVER', 'file:///var/run/zarafa');
define('USERNAME', 'ahall'); define('USERNAME', '');
define('PASSWORD', 'ahall'); define('PASSWORD', '');
define('CERTIFICATE', null); define('CERTIFICATE', null);
define('CERTIFICATE_PASSWORD', null); define('CERTIFICATE_PASSWORD', null);
......
...@@ -24,20 +24,20 @@ include_once(SYNC_CONFIG); ...@@ -24,20 +24,20 @@ include_once(SYNC_CONFIG);
if (! GabSyncCLI::SureWhatToDo()) { if (! GabSyncCLI::SureWhatToDo()) {
// show error message if available // show error message if available
if (GabSyncCLI::GetErrorMessage()) if (GabSyncCLI::GetErrorMessage())
fwrite(STDERR, GabSyncCLI::GetErrorMessage() . "\n\n"); fwrite(STDERR, GabSyncCLI::GetErrorMessage() . PHP_EOL.PHP_EOL);
echo GabSyncCLI::UsageInstructions(); echo GabSyncCLI::UsageInstructions();
exit(1); exit(1);
} }
else if (!GabSyncCLI::SetupSyncher()) { else if (!GabSyncCLI::SetupSyncher()) {
fwrite(STDERR, GabSyncCLI::GetErrorMessage() . "\n"); fwrite(STDERR, GabSyncCLI::GetErrorMessage() . PHP_EOL);
exit(1); exit(1);
} }
GabSyncCLI::RunCommand(); GabSyncCLI::RunCommand();
} }
catch (Exception $ex) { catch (Exception $ex) {
die(get_class($ex) . ": ". $ex->getMessage() . "\n"); die(get_class($ex) . ": ". $ex->getMessage() . PHP_EOL);
} }
...@@ -64,15 +64,18 @@ class GabSyncCLI { ...@@ -64,15 +64,18 @@ class GabSyncCLI {
* @access public * @access public
*/ */
static public function UsageInstructions() { static public function UsageInstructions() {
return "Usage:\n\tgab-sync.php -a ACTION [options]\n\n" . return "Usage:" .PHP_EOL.
"Parameters:\n\t-a simulate | sync | sync-one | clear-all | delete-all\n\t[-u] UNIQUE-ID\n\n" . "\tgab-sync.php -a ACTION [options]" .PHP_EOL.PHP_EOL.
"Actions:\n" . "Parameters:" .PHP_EOL.
"\tsimulate\t\t Simulates the GAB synchronization and prints out statistics and configuration suggestions.\n" . "\t-a simulate | sync | sync-one | clear-all | delete-all" .PHP_EOL.
"\tsync\t\t\t Synchronizes all data from the GAB to the global folder, clearing all existing data if configuration changed!\n" . "\t[-u] UNIQUE-ID" .PHP_EOL.PHP_EOL.
"\tsync-one -u UNIQUE-ID\t Tries do find the entry with UNIQUE-ID and updates the chunk this entry belongs to.\n" . "Actions:" .PHP_EOL.
"\tclear-all\t\t Removes all data from the global folder.\n" . "\tsimulate\t\t Simulates the GAB synchronization and prints out statistics and configuration suggestions." .PHP_EOL.
"\tdelete-all\t\t Like clear-all but also deletes the global folder.\n" . "\tsync\t\t\t Synchronizes all data from the GAB to the global folder, clearing all existing data if configuration changed!" .PHP_EOL.
"\n"; "\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;
} }
/** /**
...@@ -204,7 +207,7 @@ class GabSyncCLI { ...@@ -204,7 +207,7 @@ class GabSyncCLI {
* @access public * @access public
*/ */
static public function RunCommand() { static public function RunCommand() {
echo "\n"; echo PHP_EOL;
switch(self::$command) { switch(self::$command) {
case self::COMMAND_SIMULATE: case self::COMMAND_SIMULATE:
self::$syncher->Simulate(); self::$syncher->Simulate();
...@@ -224,7 +227,7 @@ class GabSyncCLI { ...@@ -224,7 +227,7 @@ class GabSyncCLI {
if ( $confirm === 'y' || $confirm === 'yes') if ( $confirm === 'y' || $confirm === 'yes')
self::$syncher->ClearAll(); self::$syncher->ClearAll();
else else
echo "Aborted!\n"; echo "Aborted!".PHP_EOL;
break; break;
case self::COMMAND_DELETEALL: case self::COMMAND_DELETEALL:
...@@ -233,10 +236,10 @@ class GabSyncCLI { ...@@ -233,10 +236,10 @@ class GabSyncCLI {
if ( $confirm === 'y' || $confirm === 'yes') if ( $confirm === 'y' || $confirm === 'yes')
self::$syncher->DeleteAll(); self::$syncher->DeleteAll();
else else
echo "Aborted!\n"; echo "Aborted!".PHP_EOL;
break; break;
} }
echo "\n"; echo PHP_EOL;
} }
} }
\ No newline at end of file
...@@ -27,7 +27,7 @@ abstract class Syncher { ...@@ -27,7 +27,7 @@ abstract class Syncher {
* Simulates the synchronization, showing statistics but without touching any data. * Simulates the synchronization, showing statistics but without touching any data.
*/ */
public function Simulate() { 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); $this->Sync(false);
} }
...@@ -159,7 +159,7 @@ abstract class Syncher { ...@@ -159,7 +159,7 @@ abstract class Syncher {
public function ClearAll() { public function ClearAll() {
$folderid = $this->GetHiddenFolderId(); $folderid = $this->GetHiddenFolderId();
if (!$folderid) { if (!$folderid) {
$this->Log("Could not locate folder. Aborting.", true); $this->Terminate("Could not locate folder. Aborting.");
} }
$status = $this->ClearFolderContents($folderid); $status = $this->ClearFolderContents($folderid);
...@@ -178,7 +178,7 @@ abstract class Syncher { ...@@ -178,7 +178,7 @@ abstract class Syncher {
public function DeleteAll() { public function DeleteAll() {
$folderid = $this->GetHiddenFolderId(); $folderid = $this->GetHiddenFolderId();
if (!$folderid) { if (!$folderid) {
$this->Log("Could not locate folder. Aborting.", true); $this->Terminate("Could not locate folder. Aborting.");
} }
$emptystatus = $this->ClearFolderContents($folderid); $emptystatus = $this->ClearFolderContents($folderid);
if ($emptystatus) { if ($emptystatus) {
...@@ -193,20 +193,27 @@ abstract class Syncher { ...@@ -193,20 +193,27 @@ abstract class Syncher {
* Logs a message to the command line. * Logs a message to the command line.
* *
* @param string $msg the message * @param string $msg the message
* @param boolean $error if set to true, message will be printed to STDERR and the script terminates.
* *
* @access protected * @access protected
* @return void * @return void
*/ */
protected function Log($msg, $error = false) { protected function Log($msg, $error = false) {
if ($error) { 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); fwrite(STDERR, $msg);
echo "\n\n"; echo PHP_EOL.PHP_EOL;
exit(1); exit(1);
} }
else
echo $msg . "\n";
}
/** /**
* Gets the ID of the hidden folder. If $doCreate is true (or not set) the * Gets the ID of the hidden folder. If $doCreate is true (or not set) the
......
...@@ -35,7 +35,7 @@ class Zarafa extends Syncher { ...@@ -35,7 +35,7 @@ class Zarafa extends Syncher {
parent::__construct(); parent::__construct();
$this->session = mapi_logon_zarafa(USERNAME, PASSWORD, SERVER, CERTIFICATE, CERTIFICATE_PASSWORD, 0, self::VERSION, self::NAME. " ". self::VERSION); $this->session = mapi_logon_zarafa(USERNAME, PASSWORD, SERVER, CERTIFICATE, CERTIFICATE_PASSWORD, 0, self::VERSION, self::NAME. " ". self::VERSION);
if (mapi_last_hresult()) { 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->mainUser = USERNAME;
$this->store = $this->openMessageStore(HIDDEN_FOLDERSTORE); $this->store = $this->openMessageStore(HIDDEN_FOLDERSTORE);
...@@ -66,7 +66,7 @@ class Zarafa extends Syncher { ...@@ -66,7 +66,7 @@ class Zarafa extends Syncher {
// mapi_folder_createfolder() fails if a folder with this name already exists -> MAPI_E_COLLISION // mapi_folder_createfolder() fails if a folder with this name already exists -> MAPI_E_COLLISION
$newfolder = mapi_folder_createfolder($parentfolder, HIDDEN_FOLDERNAME, ""); $newfolder = mapi_folder_createfolder($parentfolder, HIDDEN_FOLDERNAME, "");
if (mapi_last_hresult()) 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 // TODO: set PR_HIDDEN
mapi_setprops($newfolder, array(PR_CONTAINER_CLASS => "IPF.Appointment")); mapi_setprops($newfolder, array(PR_CONTAINER_CLASS => "IPF.Appointment"));
...@@ -78,7 +78,7 @@ class Zarafa extends Syncher { ...@@ -78,7 +78,7 @@ class Zarafa extends Syncher {
return $sourcekey; return $sourcekey;
} }
else 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; return false;
} }
...@@ -95,11 +95,11 @@ class Zarafa extends Syncher { ...@@ -95,11 +95,11 @@ class Zarafa extends Syncher {
$folderentryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($folderid)); $folderentryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($folderid));
if (mapi_last_hresult()) 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); mapi_folder_deletefolder($parentfolder, $folderentryid);
if (mapi_last_hresult()) 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; return true;
} }
...@@ -143,7 +143,7 @@ class Zarafa extends Syncher { ...@@ -143,7 +143,7 @@ class Zarafa extends Syncher {
$flags = 0; $flags = 0;
mapi_folder_emptyfolder($folder, $flags); mapi_folder_emptyfolder($folder, $flags);
if (mapi_last_hresult()) 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; return true;
} }
...@@ -161,7 +161,7 @@ class Zarafa extends Syncher { ...@@ -161,7 +161,7 @@ class Zarafa extends Syncher {
$table = mapi_folder_getcontentstable($folder); $table = mapi_folder_getcontentstable($folder);
if (!$table) 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 = array("location" =>"PT_STRING8:PSETID_Appointment:0x8208");
$mapiprops = getPropIdsFromStrings($this->store, $this->mapiprops); $mapiprops = getPropIdsFromStrings($this->store, $this->mapiprops);
...@@ -206,7 +206,7 @@ class Zarafa extends Syncher { ...@@ -206,7 +206,7 @@ class Zarafa extends Syncher {
$addrbook = mapi_openaddressbook($this->session); $addrbook = mapi_openaddressbook($this->session);
$result = mapi_last_hresult(); $result = mapi_last_hresult();
if ($result) 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) if ($addrbook)
$ab_entryid = mapi_ab_getdefaultdir($addrbook); $ab_entryid = mapi_ab_getdefaultdir($addrbook);
...@@ -216,7 +216,7 @@ class Zarafa extends Syncher { ...@@ -216,7 +216,7 @@ class Zarafa extends Syncher {
$table = mapi_folder_getcontentstable($ab_dir); $table = mapi_folder_getcontentstable($ab_dir);
if (!$table) 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 // restrict the table if we should only return one
if ($uniqueId) { if ($uniqueId) {
...@@ -225,10 +225,10 @@ class Zarafa extends Syncher { ...@@ -225,10 +225,10 @@ class Zarafa extends Syncher {
mapi_table_restrict($table, $restriction); mapi_table_restrict($table, $restriction);
$querycnt = mapi_table_getrowcount($table); $querycnt = mapi_table_getrowcount($table);
if ($querycnt == 0) { 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) { 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 { ...@@ -483,11 +483,11 @@ class Zarafa extends Syncher {
} }
if (!$parentfentryid) 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); $parentfolder = mapi_msgstore_openentry($this->store, $parentfentryid);
if (!$parentfolder) 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; $this->folderCache[$rootId] = $parentfolder;
} }
...@@ -505,7 +505,7 @@ class Zarafa extends Syncher { ...@@ -505,7 +505,7 @@ class Zarafa extends Syncher {
if (!isset($this->folderCache[$folderid])) { if (!isset($this->folderCache[$folderid])) {
$folderentryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($folderid)); $folderentryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($folderid));
if (!$folderentryid) 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); $this->folderCache[$folderid] = mapi_msgstore_openentry($this->store, $folderentryid);
} }
...@@ -532,7 +532,7 @@ class Zarafa extends Syncher { ...@@ -532,7 +532,7 @@ class Zarafa extends Syncher {
break; break;
} }
if (!$prop) { 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; return $prop;
} }
...@@ -550,11 +550,11 @@ class Zarafa extends Syncher { ...@@ -550,11 +550,11 @@ class Zarafa extends Syncher {
$stream = mapi_openproperty($message, $prop, IID_IStream, 0, 0); $stream = mapi_openproperty($message, $prop, IID_IStream, 0, 0);
$ret = mapi_last_hresult(); $ret = mapi_last_hresult();
if ($ret == MAPI_E_NOT_FOUND) { 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 ""; return "";
} }
elseif ($ret) { 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 ""; return "";
} }
$data = ""; $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