Implement multiple on duplicated user found policies
This commit is contained in:
parent
368ccf82ff
commit
cd0d9abea8
1 changed files with 105 additions and 36 deletions
|
@ -29,6 +29,8 @@ function print_usage()
|
|||
print "-u / --user User name\n";
|
||||
print "-m / --mailhost Mailhost (optional)\n";
|
||||
print "-o / --output Output file\n";
|
||||
print "-l / --last-login-on-duplicated Use last login user on duplicated\n";
|
||||
print "-a / --all-on-duplicated Retreive all user contacts on duplicated\n";
|
||||
print "-v / --verbose Enable verbose mode\n";
|
||||
print "-d / --debug Enable debug mode\n";
|
||||
}
|
||||
|
@ -47,7 +49,7 @@ function progress_update($pos, $max)
|
|||
|
||||
class rcmail_export extends rcmail_utils {
|
||||
|
||||
function get_userid($user,$mailhost=null) {
|
||||
function get_userid($user,$mailhost=null,$onDuplicatedPolicy='error') {
|
||||
$db = self::db();
|
||||
$sql = "SELECT user_id, mail_host, last_login FROM " . $db->table_name('users', true) . " WHERE username=?";
|
||||
$sql_params = array($user);
|
||||
|
@ -63,12 +65,41 @@ class rcmail_export extends rcmail_utils {
|
|||
log_msg('FATAL',"User $user not found !");
|
||||
}
|
||||
elseif ($db -> num_rows($sql_result)>1) {
|
||||
log_msg("DEBUG","On duplicated user policy : $onDuplicatedPolicy");
|
||||
switch ($onDuplicatedPolicy) {
|
||||
case 'last-login':
|
||||
log_msg('INFO', "More thant one user found for username $user : take the last connected one.");
|
||||
$user_id = false;
|
||||
$mailhost = false;
|
||||
$last_connected="1900-01-01 00:00:00";
|
||||
while($sql_arr = $db->fetch_assoc($sql_result)) {
|
||||
log_msg('DEBUG', " - User : $user / ID : ".$sql_arr['user_id']." / Mailhost : ".$sql_arr['mail_host']." / Last login : ".$sql_arr['last_login']);
|
||||
if ( strnatcmp($last_connected, $sql_arr['last_login']) <= 0 ) {
|
||||
$last_connected=$sql_arr['last_login'];
|
||||
$user_id = $sql_arr['user_id'];
|
||||
}
|
||||
}
|
||||
log_msg('INFO', "Last connected one is ID $user_id / Mailhost : $mailhost / Last login : $last_connected");
|
||||
return $user_id;
|
||||
break;
|
||||
case 'all':
|
||||
log_msg('INFO', "More thant one user found for username $user : take all of them.");
|
||||
$user_ids = array();
|
||||
while($sql_arr = $db->fetch_assoc($sql_result)) {
|
||||
log_msg('DEBUG', " - User : $user / ID : ".$sql_arr['user_id']." / Mailhost : ".$sql_arr['mail_host']." / Last login : ".$sql_arr['last_login']);
|
||||
$user_ids[] = $sql_arr['user_id'];
|
||||
}
|
||||
return $user_ids;
|
||||
break;
|
||||
case 'error':
|
||||
default:
|
||||
log_msg('FATAL', "More thant one user found for username $user. You need to specify mailhost !");
|
||||
log_msg('INFO', "Users found :");
|
||||
while($sql_arr = $db->fetch_assoc($sql_result)) {
|
||||
log_msg('INFO', " - User : $user / ID : ".$sql_arr['user_id']." / Mailhost : ".$sql_arr['mail_host']." / Last login : ".$sql_arr['last_login']);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
$sql_arr = $db->fetch_assoc($sql_result);
|
||||
return $sql_arr['user_id'];
|
||||
|
@ -77,35 +108,44 @@ class rcmail_export extends rcmail_utils {
|
|||
return false;
|
||||
}
|
||||
|
||||
function get_user_address_book($user, $mailhost = null) {
|
||||
$userid=$this -> get_userid($user, $mailhost);
|
||||
if ($userid !== false) {
|
||||
log_msg('DEBUG', "User $user ID : $userid");
|
||||
$contacts = new rcube_contacts(self::db(), $userid);
|
||||
return $contacts;
|
||||
}
|
||||
function get_user_address_book($user, $mailhost = null, $onDuplicatedPolicy = 'error') {
|
||||
$userids=$this -> get_userid($user, $mailhost, $onDuplicatedPolicy);
|
||||
if ($userid === false)
|
||||
return false;
|
||||
if (!is_array($userids))
|
||||
$userids = array($userids);
|
||||
$address_books = array();
|
||||
foreach ($userids as $userid) {
|
||||
log_msg('DEBUG', "User $user ID : $userid");
|
||||
$address_books[] = new rcube_contacts(self::db(), $userid);
|
||||
}
|
||||
return $address_books;
|
||||
}
|
||||
|
||||
function export_user_contacts($user, $mailhost = null, $out = null) {
|
||||
$CONTACTS = $this->get_user_address_book($user, $mailhost);
|
||||
if ($CONTACTS === false) {
|
||||
function export_user_contacts($user, $mailhost = null, $out = null, $onDuplicatedPolicy = 'error') {
|
||||
$address_books = $this->get_user_address_book($user, $mailhost, $onDuplicatedPolicy);
|
||||
if ($address_books === false) {
|
||||
log_msg('WARNING', "No address book found for user $user");
|
||||
return false;
|
||||
}
|
||||
|
||||
$CONTACTS->set_page(1);
|
||||
$CONTACTS->set_pagesize(99999);
|
||||
$result = $CONTACTS->list_records(null, 0, true);
|
||||
|
||||
if (!$out) {
|
||||
$out = STDOUT;
|
||||
}
|
||||
|
||||
log_msg('DEBUG', count($address_books). " address book(s) found for user $user.");
|
||||
|
||||
$total_count=0;
|
||||
for($idx=0; $idx < count($address_books); $idx++) {
|
||||
log_msg('DEBUG', "Export contact of address book #$idx of user $user.");
|
||||
$address_books[$idx]->set_page(1);
|
||||
$address_books[$idx]->set_pagesize(99999);
|
||||
$result = $address_books[$idx]->list_records(null, 0, true);
|
||||
|
||||
$count=0;
|
||||
while ($result && ($row = $result->next())) {
|
||||
if ($CONTACTS) {
|
||||
$this -> prepare_for_export($row, $CONTACTS);
|
||||
if ($address_books[$idx]) {
|
||||
$this -> prepare_for_export($row, $address_books[$idx]);
|
||||
}
|
||||
|
||||
// fix folding and end-of-line chars
|
||||
|
@ -114,7 +154,11 @@ class rcmail_export extends rcmail_utils {
|
|||
fwrite($out, rcube_vcard::rfc2425_fold($row['vcard']) . rcube_vcard::$eol);
|
||||
$count++;
|
||||
}
|
||||
log_msg("INFO","$count contact(s) found in address book of user $user");
|
||||
log_msg("INFO","$count contact(s) found in address book #$idx of user $user");
|
||||
$total_count += $count;
|
||||
}
|
||||
log_msg("INFO","$total_count contact(s) found in total for user $user");
|
||||
return True;
|
||||
}
|
||||
|
||||
function prepare_for_export(&$record, $source = null) {
|
||||
|
@ -165,7 +209,16 @@ class rcmail_export extends rcmail_utils {
|
|||
}
|
||||
|
||||
// get arguments
|
||||
$opts = array('u' => 'user', 'm' => 'mailhost', 'o' => 'output', 'd' => 'debug', 'v' => 'verbose', 'h' => 'help');
|
||||
$opts = array(
|
||||
'u' => 'user',
|
||||
'm' => 'mailhost',
|
||||
'o' => 'output',
|
||||
'l' => 'last-login-on-duplicated',
|
||||
'a' => 'all-on-duplicated',
|
||||
'd' => 'debug',
|
||||
'v' => 'verbose',
|
||||
'h' => 'help',
|
||||
);
|
||||
$args = rcube_utils::get_opt($opts);
|
||||
|
||||
if ($_SERVER['argv'][1] == 'help' || isset($args['help'])) {
|
||||
|
@ -214,9 +267,25 @@ if (!empty($args['output'])) {
|
|||
}
|
||||
}
|
||||
|
||||
if (isset($args['last-login-on-duplicated'])) {
|
||||
$onDuplicatedPolicy = 'last-login';
|
||||
}
|
||||
elseif (isset($args['all-on-duplicated'])) {
|
||||
$onDuplicatedPolicy = 'all';
|
||||
}
|
||||
else {
|
||||
$onDuplicatedPolicy = 'error';
|
||||
}
|
||||
log_msg("DEBUG","On duplicated user policy : $onDuplicatedPolicy");
|
||||
|
||||
$export = new rcmail_export();
|
||||
|
||||
$export -> export_user_contacts( $args['user'], $args['mailhost'], $out);
|
||||
$result = $export -> export_user_contacts(
|
||||
$args['user'],
|
||||
$args['mailhost'],
|
||||
$out,
|
||||
$onDuplicatedPolicy
|
||||
);
|
||||
|
||||
if ($out)
|
||||
fclose($out);
|
||||
|
|
Loading…
Reference in a new issue