Commit 0413ad83 authored by Sebastian Kummer's avatar Sebastian Kummer

Merge pull request #564 in ZP/z-push from...

Merge pull request #564 in ZP/z-push from feature/ZP-1165-tools-z-push-admin-could-have-an to develop

* commit 'b8c4b3e9':
  ZP-1165 Remove device command honours --days-old parameter.
  ZP-1165 Added days-ago as an alias for days-old option.
  ZP-1165 Added days-old option for lastsync action.
parents 42642206 b8c4b3e9
...@@ -28,6 +28,11 @@ class ZPushAdmin { ...@@ -28,6 +28,11 @@ class ZPushAdmin {
* //TODO resync of a foldertype for all users (e.g. Appointment) * //TODO resync of a foldertype for all users (e.g. Appointment)
*/ */
const STATUS_SUCCESS = 0;
const STATUS_DEVICE_SYNCED_AFTER_DAYSOLD = 1;
public static $status = self::STATUS_SUCCESS;
/** /**
* List devices known to Z-Push. * List devices known to Z-Push.
* If no user is given, all devices are listed * If no user is given, all devices are listed
...@@ -219,11 +224,13 @@ class ZPushAdmin { ...@@ -219,11 +224,13 @@ class ZPushAdmin {
* *
* @param string $user (opt) user of the device * @param string $user (opt) user of the device
* @param string $devid (opt) device id which should be removed * @param string $devid (opt) device id which should be removed
* @param int $daysOld (opt) devices which haven't synced for $daysOld days
* @param int $time (opt) unix timestamp to use with $daysOld
* *
* @return boolean * @return boolean
* @access public * @access public
*/ */
static public function RemoveDevice($user = false, $devid = false) { static public function RemoveDevice($user = false, $devid = false, $daysOld = false, $time = false) {
if ($user === false && $devid === false) if ($user === false && $devid === false)
return false; return false;
...@@ -232,7 +239,7 @@ class ZPushAdmin { ...@@ -232,7 +239,7 @@ class ZPushAdmin {
$devicesIds = ZPush::GetStateMachine()->GetAllDevices($user); $devicesIds = ZPush::GetStateMachine()->GetAllDevices($user);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::RemoveDevice(): all '%d' devices for user '%s' found to be removed", count($devicesIds), $user)); ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::RemoveDevice(): all '%d' devices for user '%s' found to be removed", count($devicesIds), $user));
foreach ($devicesIds as $deviceid) { foreach ($devicesIds as $deviceid) {
if (!self::RemoveDevice($user, $deviceid)) { if (!self::RemoveDevice($user, $deviceid, $daysOld, $time)) {
ZLog::Write(LOGLEVEL_ERROR, sprintf("ZPushAdmin::RemoveDevice(): removing devices failed for device '%s' of user '%s'. Aborting", $deviceid, $user)); ZLog::Write(LOGLEVEL_ERROR, sprintf("ZPushAdmin::RemoveDevice(): removing devices failed for device '%s' of user '%s'. Aborting", $deviceid, $user));
return false; return false;
} }
...@@ -243,7 +250,7 @@ class ZPushAdmin { ...@@ -243,7 +250,7 @@ class ZPushAdmin {
$users = self::ListUsers($devid); $users = self::ListUsers($devid);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::RemoveDevice(): device '%d' is used by '%d' users and will be removed", $devid, count($users))); ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::RemoveDevice(): device '%d' is used by '%d' users and will be removed", $devid, count($users)));
foreach ($users as $aUser) { foreach ($users as $aUser) {
if (!self::RemoveDevice($aUser, $devid)) { if (!self::RemoveDevice($aUser, $devid, $daysOld, $time)) {
ZLog::Write(LOGLEVEL_ERROR, sprintf("ZPushAdmin::RemoveDevice(): removing user '%s' from device '%s' failed. Aborting", $aUser, $devid)); ZLog::Write(LOGLEVEL_ERROR, sprintf("ZPushAdmin::RemoveDevice(): removing user '%s' from device '%s' failed. Aborting", $aUser, $devid));
return false; return false;
} }
...@@ -260,6 +267,21 @@ class ZPushAdmin { ...@@ -260,6 +267,21 @@ class ZPushAdmin {
$device->SetData($devicedata, false); $device->SetData($devicedata, false);
if (!isset($devicedata->devices)) if (!isset($devicedata->devices))
throw new StateInvalidException("No devicedata stored in ASDevice"); throw new StateInvalidException("No devicedata stored in ASDevice");
if ($daysOld) {
if (!$time) {
$time = time();
}
$lastSynced = floor(($time - $device->getLastupdatetime()) / 86400);
if ($daysOld > $lastSynced) {
ZLog::Write(LOGLEVEL_INFO,
sprintf("ZPushAdmin::RemoveDevice(): device '%s' of user '%s' synced %d day(s) ago but only devices which synced more than %d days ago will be removed. Skipping.",
$devid, $user, $lastSynced, $daysOld));
self::$status = self::STATUS_DEVICE_SYNCED_AFTER_DAYSOLD;
return true;
}
}
$devices = $devicedata->devices; $devices = $devicedata->devices;
} }
catch (StateNotFoundException $e) { catch (StateNotFoundException $e) {
......
...@@ -92,6 +92,7 @@ class ZPushAdminCLI { ...@@ -92,6 +92,7 @@ class ZPushAdminCLI {
static private $device = false; static private $device = false;
static private $type = false; static private $type = false;
static private $errormessage; static private $errormessage;
static private $daysold = false;
/** /**
* Returns usage instructions * Returns usage instructions
...@@ -102,7 +103,8 @@ class ZPushAdminCLI { ...@@ -102,7 +103,8 @@ class ZPushAdminCLI {
static public function UsageInstructions() { static public function UsageInstructions() {
return "Usage:\n\tz-push-admin.php -a ACTION [options]\n\n" . return "Usage:\n\tz-push-admin.php -a ACTION [options]\n\n" .
"Parameters:\n\t-a list/lastsync/wipe/remove/resync/clearloop/fixstates\n\t[-u] username\n\t[-d] deviceid\n" . "Parameters:\n\t-a list/lastsync/wipe/remove/resync/clearloop/fixstates\n\t[-u] username\n\t[-d] deviceid\n" .
"\t[-t] type\tthe following types are available: '".self::TYPE_OPTION_EMAIL."', '".self::TYPE_OPTION_CALENDAR."', '".self::TYPE_OPTION_CONTACT."', '".self::TYPE_OPTION_TASK."', '".self::TYPE_OPTION_NOTE."', '".self::TYPE_OPTION_HIERARCHY."' of '".self::TYPE_OPTION_GAB."' (for KOE) or a folder id.\n\n" . "\t[-t] type\tthe following types are available: '".self::TYPE_OPTION_EMAIL."', '".self::TYPE_OPTION_CALENDAR."', '".self::TYPE_OPTION_CONTACT."', '".self::TYPE_OPTION_TASK."', '".self::TYPE_OPTION_NOTE."', '".self::TYPE_OPTION_HIERARCHY."' of '".self::TYPE_OPTION_GAB."' (for KOE) or a folder id.\n" .
"\t[--days-old] n\tshow or remove profiles older than n days with lastsync or remove. n must be a positive integer.\n\n".
"Actions:\n" . "Actions:\n" .
"\tlist\t\t\t\t\t Lists all devices and synchronized users\n" . "\tlist\t\t\t\t\t Lists all devices and synchronized users\n" .
"\tlist -u USER\t\t\t\t Lists all devices of user USER\n" . "\tlist -u USER\t\t\t\t Lists all devices of user USER\n" .
...@@ -150,7 +152,7 @@ class ZPushAdminCLI { ...@@ -150,7 +152,7 @@ class ZPushAdminCLI {
if (self::$errormessage) if (self::$errormessage)
return; return;
$options = getopt("u:d:a:t:"); $options = getopt("u:d:a:t:", array('user:', 'device:', 'action:', 'type:', 'days-old:', 'days-ago:'));
// get 'user' // get 'user'
if (isset($options['u']) && !empty($options['u'])) if (isset($options['u']) && !empty($options['u']))
...@@ -177,6 +179,19 @@ class ZPushAdminCLI { ...@@ -177,6 +179,19 @@ class ZPushAdminCLI {
elseif (isset($options['type']) && !empty($options['type'])) elseif (isset($options['type']) && !empty($options['type']))
self::$type = strtolower(trim($options['type'])); self::$type = strtolower(trim($options['type']));
if (isset($options['days-ago']) && !empty($options['days-ago'])) {
$options['days-old'] = $options['days-ago'];
}
if (isset($options['days-old']) && !empty($options['days-old'])) {
if (!is_numeric($options['days-old']) || $options['days-old'] < 0) {
self::$errormessage = "--days-old parameter must be a positive integer\n";
self::$command = null;
return;
}
self::$daysold = trim($options['days-old']);
}
// if type is set, it must be one of known types or a 44 or 48 byte long folder id // if type is set, it must be one of known types or a 44 or 48 byte long folder id
if (self::$type !== false) { if (self::$type !== false) {
if (self::$type !== self::TYPE_OPTION_EMAIL && if (self::$type !== self::TYPE_OPTION_EMAIL &&
...@@ -438,17 +453,22 @@ class ZPushAdminCLI { ...@@ -438,17 +453,22 @@ class ZPushAdminCLI {
echo "\tno devices found\n"; echo "\tno devices found\n";
else { else {
echo "All known devices and users and their last synchronization time\n\n"; echo "All known devices and users and their last synchronization time\n\n";
echo str_pad("Device id", 36) . str_pad("Synchronized user", 31) . str_pad("Last sync time", 20) . "Short Ids\n"; echo str_pad("Device id", 36) . str_pad("Synchronized user", 31) . str_pad("Last sync time", 33) . "Short Ids\n";
echo "-----------------------------------------------------------------------------------------------------\n"; echo "------------------------------------------------------------------------------------------------------------------\n";
} }
$now = time();
foreach ($devicelist as $deviceId) { foreach ($devicelist as $deviceId) {
$users = ZPushAdmin::ListUsers($deviceId); $users = ZPushAdmin::ListUsers($deviceId);
foreach ($users as $user) { foreach ($users as $user) {
$device = ZPushAdmin::GetDeviceDetails($deviceId, $user); $device = ZPushAdmin::GetDeviceDetails($deviceId, $user);
$lastsync = $device->GetLastSyncTime() ? strftime("%Y-%m-%d %H:%M", $device->GetLastSyncTime()) : "never"; $daysOld = floor(($now - $device->GetLastSyncTime()) / 86400);
if (self::$daysold > $daysOld) {
continue;
}
$lastsync = $device->GetLastSyncTime() ? strftime("%Y-%m-%d %H:%M", $device->GetLastSyncTime()) . ' (' . str_pad($daysOld, 3, ' ', STR_PAD_LEFT) . ' days ago)' : "never";
$hasShortFolderIds = $device->HasFolderIdMapping() ? "Yes":"No"; $hasShortFolderIds = $device->HasFolderIdMapping() ? "Yes":"No";
echo str_pad($deviceId, 36) . str_pad($user, 30) . " " . str_pad($lastsync, 20) . $hasShortFolderIds . "\n"; echo str_pad($deviceId, 36) . str_pad($user, 30) . " " . str_pad($lastsync, 33) . $hasShortFolderIds . "\n";
} }
} }
} }
...@@ -505,20 +525,24 @@ class ZPushAdminCLI { ...@@ -505,20 +525,24 @@ class ZPushAdminCLI {
} }
/** /**
* Command "Remove device" * Command "Remove device".
* Remove a device of that user from the device list * Removes a device of that user from the device list.
* *
* @return * @return
* @access public * @access public
*/ */
static public function CommandRemoveDevice() { static public function CommandRemoveDevice() {
$stat = ZPushAdmin::RemoveDevice(self::$user, self::$device); $stat = ZPushAdmin::RemoveDevice(self::$user, self::$device, self::$daysold, time());
if (self::$user === false) if (self::$user === false)
echo sprintf("State data of device '%s' removed: %s", self::$device, ($stat)?'OK':ZLog::GetLastMessage(LOGLEVEL_ERROR)). "\n"; echo sprintf("State data of device '%s' removed: %s", self::$device, ($stat)?'OK':ZLog::GetLastMessage(LOGLEVEL_ERROR)). "\n";
elseif (self::$device === false) elseif (self::$device === false)
echo sprintf("State data of all devices of user '%s' removed: %s", self::$user, ($stat)?'OK':ZLog::GetLastMessage(LOGLEVEL_ERROR)). "\n"; echo sprintf("State data of all devices of user '%s' removed: %s", self::$user, ($stat)?'OK':ZLog::GetLastMessage(LOGLEVEL_ERROR)). "\n";
else else
echo sprintf("State data of device '%s' of user '%s' removed: %s", self::$device, self::$user, ($stat)?'OK':ZLog::GetLastMessage(LOGLEVEL_ERROR)). "\n"; echo sprintf("State data of device '%s' of user '%s' removed: %s", self::$device, self::$user, ($stat)?'OK':ZLog::GetLastMessage(LOGLEVEL_ERROR)). "\n";
if (ZPushAdmin::$status == ZPushAdmin::STATUS_DEVICE_SYNCED_AFTER_DAYSOLD) {
print("Some devices might not have been removed because of --days-old parameter. Check Z-Push log file for more details.\n");
}
} }
/** /**
......
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