2008-10-26 13:13:01 +01:00
#!/usr/bin/php -qC
2006-09-19 08:59:55 +02:00
< ? php
/**
* Admin - Command line interface
*
* @ link http :// www . egroupware . org
* @ package admin
* @ author Ralf Becker < RalfBecker - AT - outdoor - training . de >
2016-03-28 20:51:38 +02:00
* @ copyright ( c ) 2006 - 16 by Ralf Becker < RalfBecker - AT - outdoor - training . de >
2006-09-19 08:59:55 +02:00
* @ license http :// opensource . org / licenses / gpl - license . php GPL - GNU General Public License
* @ version $Id $
*/
2016-04-27 12:34:57 +02:00
use EGroupware\Api ;
2016-04-29 15:02:10 +02:00
use EGroupware\Api\Framework ;
use EGroupware\Api\Vfs ;
2016-04-27 12:34:57 +02:00
2006-09-19 08:59:55 +02:00
chdir ( dirname ( __FILE__ )); // to enable our relative pathes to work
2013-09-10 19:29:58 +02:00
if ( php_sapi_name () !== 'cli' ) // security precaution: forbit calling admin-cli as web-page
2006-09-19 08:59:55 +02:00
{
die ( '<h1>admin-cli.php must NOT be called as web-page --> exiting !!!</h1>' );
}
2016-10-31 14:51:58 +01:00
elseif ( $_SERVER [ 'argc' ] <= 1 || $_SERVER [ 'argc' ] == 2 && in_array ( $_SERVER [ 'argv' ][ 1 ], array ( '-h' , '--help' )))
2006-09-19 08:59:55 +02:00
{
2016-10-31 14:51:58 +01:00
usage ();
2006-09-19 08:59:55 +02:00
}
else
{
2016-10-31 14:51:58 +01:00
$arguments = $_SERVER [ 'argv' ];
array_shift ( $arguments );
$action = array_shift ( $arguments );
2006-09-19 08:59:55 +02:00
}
2007-11-22 01:57:12 +01:00
2012-08-09 10:16:17 +02:00
// allow to specify instance by using a username with appended @domain-name
2007-11-22 01:57:12 +01:00
$arg0s = explode ( ',' , @ array_shift ( $arguments ));
2016-10-31 14:51:58 +01:00
@ list ( $user , $domain ) = explode ( '@' , $arg0s [ 0 ] . '@' );
2012-08-09 10:16:17 +02:00
load_egw ( $user , $arg0s [ 1 ], $domain );
2006-09-19 08:59:55 +02:00
switch ( $action )
{
2007-07-13 10:55:07 +02:00
case '--edit-user' :
return do_edit_user ( $arg0s );
2008-10-26 13:13:01 +01:00
2010-08-20 22:28:59 +02:00
case '--add-user' : // like --edit-account, but always runs addaccount hook
return do_edit_user ( $arg0s , true );
2016-10-31 14:51:58 +01:00
case '--edit-alias' :
case '--edit-forward' :
case '--edit-quota' :
return do_edit_mail ( substr ( $action , 7 ), $arg0s );
2007-11-07 14:50:35 +01:00
case '--change-pw' :
return do_change_pw ( $arg0s );
2006-09-19 08:59:55 +02:00
case '--delete-user' :
2007-07-13 10:55:07 +02:00
return do_delete_account ( $arg0s [ 2 ], $arg0s [ 3 ]);
2008-10-26 13:13:01 +01:00
2007-07-13 10:55:07 +02:00
case '--edit-group' :
return do_edit_group ( $arg0s );
2008-10-26 13:13:01 +01:00
2007-07-13 10:55:07 +02:00
case '--delete-group' :
2007-11-27 04:20:28 +01:00
return do_delete_account ( $arg0s [ 2 ], 0 , false );
2008-10-26 13:13:01 +01:00
2007-07-13 10:55:07 +02:00
case '--allow-app' :
case '--deny-app' :
return do_account_app ( $arg0s , $action == '--allow-app' );
2008-10-26 13:13:01 +01:00
2007-04-28 13:51:46 +02:00
case '--change-account-id' :
return do_change_account_id ( $arg0s );
2008-10-26 13:13:01 +01:00
2016-03-28 20:51:38 +02:00
/* ToDo : get this working again
2007-11-22 01:57:12 +01:00
case '--subscribe-other' :
return do_subscribe_other ( $arg0s [ 2 ], $arg0s [ 3 ]);
2016-03-28 20:51:38 +02:00
*/
2007-05-08 15:36:17 +02:00
case '--check-acl' ;
return do_check_acl ();
2008-10-26 13:13:01 +01:00
2007-12-05 03:27:49 +01:00
case '--show-header' ;
2007-12-07 01:07:08 +01:00
return run_command ( new setup_cmd_showheader ( $arg0s [ 2 ]));
2008-10-26 13:13:01 +01:00
2007-07-13 10:55:07 +02:00
case '--exit-codes' :
return list_exit_codes ();
2006-09-19 08:59:55 +02:00
default :
2012-08-08 18:25:03 +02:00
// we allow to call admin_cmd classes directly, if they define the constant SETUP_CLI_CALLABLE
if ( substr ( $action , 0 , 2 ) == '--' && class_exists ( $class = str_replace ( '-' , '_' , substr ( $action , 2 ))) &&
is_subclass_of ( $class , 'admin_cmd' ) && @ constant ( $class . '::SETUP_CLI_CALLABLE' ))
{
$args = array ();
$args [ 'domain' ] = array_shift ( $arg0s ); // domain must be first argument, to ensure right domain get's selected in header-include
foreach ( $arg0s as $arg )
{
list ( $name , $value ) = explode ( '=' , $arg , 2 );
if ( property_exists ( 'admin_cmd' , $name )) // dont allow to overwrite admin_cmd properties
{
2016-04-29 15:02:10 +02:00
throw new Api\Exception\WrongUserinput ( lang ( " Invalid argument '%1' !!! " , $arg ), 90 );
2012-08-08 18:25:03 +02:00
}
if ( substr ( $name , - 1 ) == ']' ) // allow 1-dim. arrays
{
list ( $name , $sub ) = explode ( '[' , substr ( $name , 0 , - 1 ), 2 );
$args [ $name ][ $sub ] = $value ;
}
else
{
$args [ $name ] = $value ;
}
}
return run_command ( new $class ( $args ));
}
2006-09-19 08:59:55 +02:00
usage ( $action );
break ;
}
exit ( 0 );
2007-11-22 01:57:12 +01:00
/**
* run a command object , after checking for additional arguments : sheduled , requested or comment
2008-10-26 13:13:01 +01:00
*
2007-11-27 04:20:28 +01:00
* Does not return ! Echos success or error messsage and exits with either 0 ( success ) or the numerical error - code
2007-11-22 01:57:12 +01:00
*
* @ param admin_cmd $cmd
*/
2007-11-27 04:20:28 +01:00
function run_command ( admin_cmd $cmd )
2007-11-22 01:57:12 +01:00
{
2016-04-27 12:34:57 +02:00
global $arguments , $user , $arg0s , $domain ;
2008-10-26 13:13:01 +01:00
2007-11-27 04:20:28 +01:00
$skip_checks = false ;
2007-11-22 01:57:12 +01:00
while ( $arguments && ( $extra = array_shift ( $arguments )))
{
switch ( $extra )
{
2007-11-27 04:20:28 +01:00
case '--schedule' : // schedule the command instead of running it directly
2007-11-22 01:57:12 +01:00
$time = admin_cmd :: parse_date ( array_shift ( $arguments ));
break ;
2008-10-26 13:13:01 +01:00
2007-11-22 01:57:12 +01:00
case '--requested' : // note who requested to run the command
$cmd -> requested = 0 ;
$cmd -> requested_email = array_shift ( $arguments );
break ;
2008-10-26 13:13:01 +01:00
2007-11-22 01:57:12 +01:00
case '--comment' : // note a comment
$cmd -> comment = array_shift ( $arguments );
break ;
2008-10-26 13:13:01 +01:00
2007-11-23 21:04:26 +01:00
case '--remote' : // run the command on a remote install
$cmd -> remote_id = admin_cmd :: parse_remote ( array_shift ( $arguments ));
break ;
2008-10-26 13:13:01 +01:00
2013-06-12 18:57:44 +02:00
case '--skip-checks' : // do not yet run the checks for scheduled local commands
2007-11-27 04:20:28 +01:00
$skip_checks = true ;
break ;
2008-10-26 13:13:01 +01:00
2016-10-31 14:51:58 +01:00
case '--try-run' : // only run checks
2013-06-12 18:57:44 +02:00
$dry_run = true ;
break ;
2007-12-05 03:27:49 +01:00
case '--header-access' :
if ( $cmd instanceof setup_cmd )
{
list ( $user , $pw ) = explode ( ',' , array_shift ( $arguments ), 2 );
$cmd -> set_header_secret ( $user , $pw );
}
break ;
2007-11-27 04:20:28 +01:00
2007-11-22 01:57:12 +01:00
default :
//fail(99,lang('Unknown option %1',$extra);
echo lang ( 'Unknown option %1' , $extra ) . " \n \n " ;
usage ( '' , 99 );
break ;
}
}
2013-06-12 18:57:44 +02:00
if ( $dry_run && $skip_checks )
{
2016-10-31 14:51:58 +01:00
echo lang ( 'You can NOT use --try-run together with --skip-checks!' ) . " \n \n " ;
2013-06-12 18:57:44 +02:00
usage ( '' , 99 );
}
2007-11-23 21:04:26 +01:00
//_debug_array($cmd);
2013-06-12 18:57:44 +02:00
try {
print_r ( $cmd -> run ( $time , true , $skip_checks , $dry_run ));
2016-04-27 12:34:57 +02:00
// cli can NOT clear instance cache of APC(u), as cli uses different shared memory then webserver
// --> we use a webservice call to clear cache (might fail if no domain in specified in webserver_url or on command line)
if ( ! $dry_run )
{
$url = $GLOBALS [ 'egw_info' ][ 'server' ][ 'webserver_url' ] . '/json.php?menuaction=admin.admin_hooks.ajax_clear_cache' ;
if ( $url [ 0 ] == '/' ) $url = 'http://' . ( ! empty ( $domain ) && $domain != 'default' ? $domain : 'localhost' ) . $url ;
2016-04-29 15:02:10 +02:00
$data = file_get_contents ( $url , false , Framework :: proxy_context ( $user , $arg0s [ 1 ]));
2016-04-27 12:34:57 +02:00
error_log ( " file_get_contents(' $url ') returned " . array2string ( $data ));
if ( $data && strpos ( $data , '"success"' ) !== false )
{
error_log ( 'Instance cache cleared.' );
}
else
{
error_log ( 'You might need to clear the cache for changes to be visiable: Admin >> Clear cache!' );
}
}
2013-06-12 18:57:44 +02:00
}
2016-04-29 15:02:10 +02:00
catch ( Api\Exception\WrongUserinput $e ) {
2013-06-12 18:57:44 +02:00
echo " \n " . $e -> getMessage () . " \n \n " ;
exit ( $e -> getCode ());
}
2007-12-06 09:00:41 +01:00
echo " \n " ;
2007-11-27 04:20:28 +01:00
exit ( 0 );
2007-11-22 01:57:12 +01:00
}
2006-09-19 08:59:55 +02:00
/**
2007-07-13 10:55:07 +02:00
* callback to authenticate with the user / pw specified on the commandline
2008-10-26 13:13:01 +01:00
*
2006-09-19 08:59:55 +02:00
* @ param array & $account account_info with keys 'login' , 'passwd' and optional 'passwd_type'
* @ return boolean / string true if we allow the access and account is set , a sessionid or false otherwise
*/
function user_pass_from_argv ( & $account )
{
$account = array (
'login' => $GLOBALS [ 'arg0s' ][ 0 ],
'passwd' => $GLOBALS [ 'arg0s' ][ 1 ],
'passwd_type' => 'text' ,
);
//print_r($account);
if ( ! ( $sessionid = $GLOBALS [ 'egw' ] -> session -> create ( $account )))
{
2007-07-13 10:55:07 +02:00
//fail(1,lang("Wrong admin-account or -password !!!"));
echo lang ( " Wrong admin-account or -password !!! " ) . " \n \n " ;
2006-09-19 08:59:55 +02:00
usage ( '' , 1 );
}
if ( ! $GLOBALS [ 'egw_info' ][ 'user' ][ 'apps' ][ 'admin' ]) // will be tested by the header too, but whould give html error-message
{
2007-07-13 10:55:07 +02:00
//fail(2,lang("Permission denied !!!"));
echo lang ( " Permission denied !!! " ) . " \n \n " ;
2006-09-19 08:59:55 +02:00
usage ( '' , 2 );
}
return $sessionid ;
}
2012-08-09 10:16:17 +02:00
/**
* Start the eGW session , exits on wrong credintials
*
* @ param string $user
* @ param string $passwd
* @ param string $domain
*/
function load_egw ( $user , $passwd , $domain = 'default' )
{
//echo "load_egw($user,$passwd,$domain)\n";
$_REQUEST [ 'domain' ] = $domain ;
$GLOBALS [ 'egw_login_data' ] = array (
'login' => $user ,
'passwd' => $passwd ,
'passwd_type' => 'text' ,
);
if ( ini_get ( 'session.save_handler' ) == 'files' && ! is_writable ( ini_get ( 'session.save_path' )) && is_dir ( '/tmp' ) && is_writable ( '/tmp' ))
{
ini_set ( 'session.save_path' , '/tmp' ); // regular users may have no rights to apache's session dir
}
$GLOBALS [ 'egw_info' ] = array (
'flags' => array (
'currentapp' => 'admin' ,
'noheader' => true ,
'autocreate_session_callback' => 'user_pass_from_argv' ,
'no_exception_handler' => 'cli' ,
)
);
if ( substr ( $user , 0 , 5 ) != 'root_' )
{
include ( '../header.inc.php' );
}
else
{
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'currentapp' ] = 'login' ;
include ( '../header.inc.php' );
if ( $user == 'root_' . $GLOBALS [ 'egw_info' ][ 'server' ][ 'header_admin_user' ] &&
_check_pw ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'header_admin_password' ], $passwd ) ||
$user == 'root_' . $GLOBALS [ 'egw_domain' ][ $_GET [ 'domain' ]][ 'config_user' ] &&
_check_pw ( $GLOBALS [ 'egw_domain' ][ $_GET [ 'domain' ]][ 'config_passwd' ], $passwd ))
{
echo " \n Root access granted! \n " ;
2016-04-29 15:02:10 +02:00
Vfs :: $is_root = true ;
2012-08-09 10:16:17 +02:00
}
else
{
die ( " Unknown user or password! \n " );
}
}
}
/**
* Check password against a md5 hash or cleartext password
*
* @ param string $hash_or_cleartext
* @ param string $pw
* @ return boolean
*/
function _check_pw ( $hash_or_cleartext , $pw )
{
//echo "_check_pw($hash_or_cleartext,$pw) md5=".md5($pw)."\n";
if ( preg_match ( '/^[0-9a-f]{32}$/' , $hash_or_cleartext ))
{
return $hash_or_cleartext == md5 ( $pw );
}
return $hash_or_cleartext == $pw ;
}
2006-09-19 08:59:55 +02:00
/**
* Give a usage message and exit
*
2016-03-28 20:51:38 +02:00
* @ param string $action = null
* @ param int $ret = 0 exit - code
2006-09-19 08:59:55 +02:00
*/
function usage ( $action = null , $ret = 0 )
{
2016-03-28 20:51:38 +02:00
unset ( $action );
2006-09-19 08:59:55 +02:00
$cmd = basename ( $_SERVER [ 'argv' ][ 0 ]);
2016-10-31 14:51:58 +01:00
echo " Usage: $cmd --command admin-account[@domain],admin-password,options,... [--schedule { YYYY-mm-dd|+1 week|+5 days}] [--requested 'Name <email>'] [--comment 'comment ...'] [--remote { id|name}] [--skip-checks] [--try-run] \n \n " ;
2013-06-12 18:57:44 +02:00
echo " \n \t --skip-checks \t do NOT run checks \n " ;
2016-10-31 14:51:58 +01:00
echo " \t --try-run \t only run checks \n " ;
2008-10-26 13:13:01 +01:00
2012-08-09 10:16:17 +02:00
echo " \t Alternativly you can also use a setup user and password by prefixing it with 'root_', eg. 'root_admin' for setup user 'admin'. \n \n " ;
2009-05-19 08:36:06 +02:00
echo " --edit-user admin-account[@domain],admin-password,account[=new-account-name],first-name,last-name,password,email,expires { never(default)|YYYY-MM-DD|already},can-change-pw { yes(default)|no},anon-user { yes|no(default)},primary-group { Default(default)|...}[,groups,...][,homedirectory,loginshell] \n " ;
2013-06-12 18:57:44 +02:00
echo " Edit or add a user to EGroupware. If you specify groups, they *replace* the exiting memberships! homedirectory+loginshell are supported only for LDAP and must start with a slash! \n " ;
2007-11-07 14:50:35 +01:00
echo " --change-pw admin-account[@domain],admin-password,account,password \n " ;
echo " Change/set the password for a given user \n " ;
2006-09-19 08:59:55 +02:00
echo " --delete-user admin-account[@domain],admin-password,account-to-delete[,account-to-move-data] \n " ;
2013-06-12 18:57:44 +02:00
echo " Deletes a user from EGroupware. It's data can be moved to an other user or it get deleted too. \n " ;
2007-07-13 10:55:07 +02:00
echo " --edit-group admin-account[@domain],admin-password,group[=new-group-name],email[,members,...] \n " ;
2013-06-12 18:57:44 +02:00
echo " Edit or add a group to EGroupware. If you specify members, they *replace* the exiting members! \n " ;
2007-07-13 10:55:07 +02:00
echo " --delete-group admin-account[@domain],admin-password,group-to-delete \n " ;
2013-06-12 18:57:44 +02:00
echo " Deletes a group from EGroupware. \n " ;
2007-07-13 10:55:07 +02:00
echo " --allow-app admin-account[@domain],admin-password,account,application,... \n " ;
echo " --deny-app admin-account[@domain],admin-password,account,application,... \n " ;
echo " Give or deny an account (user or group specified by account name or id) run rights for the given applications. \n " ;
2007-04-28 13:51:46 +02:00
echo " --change-account-id admin-account[@domain],admin-password,from1,to1[...,fromN,toN] \n " ;
echo " Changes one or more account_id's in the database (make a backup before!). \n " ;
2007-05-08 15:36:17 +02:00
echo " --check-acl admin-account[@domain],admin-password \n " ;
2007-07-13 10:55:07 +02:00
echo " Deletes ACL entries of not longer existing accounts (make a database backup before! --> setup-cli.php). \n " ;
2012-08-09 10:16:17 +02:00
echo " --admin-cmd-check-cats admin-account[@domain],admin-password \n " ;
echo " Deletes categories of not longer existing accounts. \n " ;
2016-10-31 15:41:52 +01:00
echo " --edit-alias admin-account[@domain],admin-password,account[=acc_id],create-identity(yes,no/default),[+/-]alias1,... \n " ;
echo " --edit-forward admin-account[@domain],admin-password,account[=acc_id],mode(forwardOnly),[+/-]forward1,... \n " ;
2016-10-31 14:51:58 +01:00
echo " --edit-quota admin-account[@domain],admin-password,account[=acc_id],quota(mb) \n " ;
echo " Edit mail account of EGroupware managed mail-server for a given user and optional acc_id (can't be scheduled or try-run) \n " ;
2007-07-13 10:55:07 +02:00
echo " --exit-codes admin-account[@domain],admin-password \n " ;
echo " List all exit codes of the command line interface \n " ;
2008-10-26 13:13:01 +01:00
exit ( $ret );
2006-09-19 08:59:55 +02:00
}
2016-10-31 14:51:58 +01:00
/**
* Edit mail account of EGroupware managed mail - server
*
* @ param string $type " alias " , " forward " , " quota "
2016-10-31 15:41:52 +01:00
* @ param array $arg0s admin - account [ @ domain ], admin - password , account [ = acc_id ], ...
* - alias : create - identity ( yes , no / default ),[ +/- ] alias1 , ... aliasN
* - forward : mode ( forwardOnly ),[ +/- ] forward1 , ... forwardN
2016-10-31 14:51:58 +01:00
* - quota : quota ( mb )
* @ return int 0 on success
*/
2016-10-31 15:41:52 +01:00
function do_edit_mail ( $type , array $arg0s )
2016-10-31 14:51:58 +01:00
{
2016-10-31 15:41:52 +01:00
array_shift ( $arg0s ); // admin-account
array_shift ( $arg0s ); // admin-pw
list ( $account , $acc_id ) = explode ( '=' , array_shift ( $arg0s ));
2016-10-31 14:51:58 +01:00
$account_id = is_numeric ( $account ) ? ( int ) $account : $GLOBALS [ 'egw' ] -> accounts -> name2id ( $account );
if ( ! $GLOBALS [ 'egw' ] -> accounts -> exists ( $account_id ) && ! ( $account_id = $GLOBALS [ 'egw' ] -> accounts -> name2id ( $account )))
{
echo " Unknown user-account ' $account '! \n " ;
exit ( 1 );
}
$found = 0 ;
foreach ( $acc_id ? array ( Api\Mail\Account :: read ( $acc_id , $account_id )) :
Api\Mail\Account :: search ( $account_id , false ) as $account )
{
2016-11-15 14:22:28 +01:00
if ( ! isset ( $acc_id ) && ! Api\Mail\Account :: is_multiple ( $account )) continue ; // no need to waste time on personal accounts
2016-10-31 14:51:58 +01:00
2016-10-31 15:41:52 +01:00
$args = $arg0s ;
2016-10-31 14:51:58 +01:00
try {
if ( ! ( $data = $account -> getUserData ( $account_id )))
{
continue ; // not a managed mail-server
}
switch ( $type )
{
case 'alias' :
$create_identity = strtolower ( array_shift ( $args )) === 'yes' ;
2016-11-14 13:42:22 +01:00
$delete_identity = $args [ 0 ][ 0 ] == '-' ;
2016-10-31 15:41:52 +01:00
array_modify ( $data [ 'mailAlternateAddress' ], $args );
2016-10-31 14:51:58 +01:00
break ;
case 'forward' :
$data [ 'deliveryMode' ] = strtolower ( array_shift ( $args )) === 'forwardonly' ? Api\Mail\Smtp :: FORWARD_ONLY : '' ;
2016-10-31 15:41:52 +01:00
array_modify ( $data [ 'mailForwardingAddress' ], $args );
2016-10-31 14:51:58 +01:00
break ;
case 'quota' :
$data [ 'quotaLimit' ] = int ( $args [ 0 ]);
break ;
}
$account -> saveUserData ( $account_id , $data );
echo " Data in mail-account (acc_id= $account->acc_id ) updated. \n " ;
++ $found ;
// create identities for all aliases
if ( $type == 'alias' && $create_identity && $args )
{
// check if user allready has an identity created for given aliases
2016-11-14 13:42:22 +01:00
foreach ( Api\Mail\Account :: identities ( $account , false , 'ident_email' , $account_id ) as $ident_id => $email )
2016-10-31 14:51:58 +01:00
{
2016-10-31 15:56:41 +01:00
if (( $key = array_search ( $email , $args )) !== false )
2016-10-31 14:51:58 +01:00
{
2016-11-15 14:22:28 +01:00
// delete identities, if "-" is used and email of identity matches given ones and is not standard identity
if ( $delete_identity && $ident_id != $account -> ident_id )
{
Api\Mail\Account :: delete_identity ( $ident_id );
}
2016-10-31 14:51:58 +01:00
unset ( $args [ $key ]);
}
}
// create not existing identities by copying standard identity plus alias as email
foreach ( $args as $email )
{
$identity = $account -> params ;
unset ( $identity [ 'ident_id' ]);
unset ( $identity [ 'ident_name' ]);
$identity [ 'ident_email' ] = $email ;
$identity [ 'account_id' ] = $account_id ; // make this a personal identity for $account_id
Api\Mail\Account :: save_identity ( $identity );
}
if ( $args ) echo " Identity(s) for " . implode ( ', ' , $args ) . " created. \n " ;
}
}
catch ( \Exception $e ) {
_egw_log_exception ( $e );
echo $e -> getMessage () . " \n " ;
}
}
if ( ! $found )
{
echo " No mailserver managed by this EGroupware instance! \n " ;
exit ( 2 );
}
exit ( 0 );
}
2016-10-31 15:41:52 +01:00
/**
* Set , add or remove from array depending on $mod [ 0 ][ 0 ] being '+' , '-' or something else ( set )
*
* @ param array & $arr
2016-10-31 15:51:02 +01:00
* @ param array & $mod eg . [ " +some-alias@egroupware.org " , " other-alias@egroupware.org " ] will add all given alias to $arr
* on return optional +/- prefix has been removed
2016-10-31 15:41:52 +01:00
* @ return array
*/
2016-11-10 09:51:37 +01:00
function array_modify ( & $arr , array & $mod )
2016-10-31 15:41:52 +01:00
{
2016-11-10 09:51:37 +01:00
if ( ! is_array ( $arr )) $arr = array ();
2016-10-31 15:41:52 +01:00
switch ( $mod [ 0 ][ 0 ])
{
case '-' :
2016-10-31 15:46:56 +01:00
$mod [ 0 ] = substr ( $mod [ 0 ], 1 );
2016-11-14 13:42:22 +01:00
$arr = array_values ( array_unique ( array_diff ( $arr , $mod )));
2016-10-31 15:41:52 +01:00
break ;
case '+' ;
2016-10-31 15:46:56 +01:00
$mod [ 0 ] = substr ( $mod [ 0 ], 1 );
2016-11-14 13:42:22 +01:00
$arr = array_values ( array_unique ( array_merge ( $arr , $mod )));
2016-10-31 15:41:52 +01:00
break ;
default :
2016-11-14 13:42:22 +01:00
$arr = array_values ( array_unique ( $mod ));
2016-10-31 15:41:52 +01:00
}
return $arr ;
}
2007-07-13 10:55:07 +02:00
/**
* Give or deny an account ( user or group specified by account name or id ) run rights for the given applications .
*
* @ param array $args admin - account [ @ domain ], admin - password , account , application , ...
* @ param boolean $allow true = allow , false = deny
* @ return int 0 on success
*/
function do_account_app ( $args , $allow )
2007-05-08 15:36:17 +02:00
{
2007-07-13 10:55:07 +02:00
array_shift ( $args ); // admin-account
array_shift ( $args ); // admin-pw
$account = array_shift ( $args );
2008-10-26 13:13:01 +01:00
2007-11-22 01:57:12 +01:00
include_once ( EGW_INCLUDE_ROOT . '/admin/inc/class.admin_cmd_account_app.inc.php' );
2007-11-27 04:20:28 +01:00
run_command ( new admin_cmd_account_app ( $allow , $account , $args ));
2007-07-13 10:55:07 +02:00
}
/**
2013-06-12 18:57:44 +02:00
* Edit or add a group to EGroupware . If you specify members , they * replace * the exiting member !
2007-07-13 10:55:07 +02:00
* 1 : 2 : 3 : 4 : 5 :
* @ param array $args admin - account [ @ domain ], admin - password , group [ = new - group - name ], email [, members , ... ]
*/
function do_edit_group ( $args )
{
array_shift ( $args ); // admin-account
array_shift ( $args ); // admin-pw
list ( $account , $new_account_name ) = explode ( '=' , array_shift ( $args )); // account[=new-account-name]
2008-10-26 13:13:01 +01:00
2007-11-27 04:20:28 +01:00
$data = array (
'account_lid' => $new_account_name ,
'account_email' => array_shift ( $args ),
'account_members' => $args ,
);
try {
admin_cmd :: parse_account ( $account , false );
2007-07-13 10:55:07 +02:00
2016-03-28 20:51:38 +02:00
foreach ( $data as & $value ) // existing account --> empty values mean dont change, not set them empty!
2007-11-27 04:20:28 +01:00
{
if (( string ) $value === '' ) $value = null ;
}
2007-07-13 10:55:07 +02:00
}
2007-11-27 04:20:28 +01:00
catch ( Exception $e ) { // new group
2016-03-28 20:51:38 +02:00
unset ( $e ); // not used
2007-11-27 04:20:28 +01:00
$data [ 'account_lid' ] = $account ;
$account = false ;
2016-03-28 20:51:38 +02:00
}
2007-11-27 04:20:28 +01:00
run_command ( new admin_cmd_edit_group ( $account , $data ));
2007-07-13 10:55:07 +02:00
}
2007-11-07 14:50:35 +01:00
/**
* Change / Set Password for a given user
2008-10-26 13:13:01 +01:00
* 1 : 2 : 3 : 4 :
2007-11-07 14:50:35 +01:00
* @ param array $args admin - account [ @ domain ], admin - password , account , password
*/
function do_change_pw ( $args )
{
array_shift ( $args ); // admin-account
array_shift ( $args ); // admin-pw
2007-11-27 04:20:28 +01:00
$account = array_shift ( $args ); // account
$password = array_shift ( $args ); // pw
2008-10-26 13:13:01 +01:00
2007-11-27 04:20:28 +01:00
run_command ( new admin_cmd_change_pw ( $account , $password ));
2007-11-07 14:50:35 +01:00
}
2007-07-13 10:55:07 +02:00
/**
2013-06-12 18:57:44 +02:00
* Edit or add a user to EGroupware . If you specify groups , they * replace * the exiting memberships !
2007-07-13 10:55:07 +02:00
* 1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9 : 10 : 11 : 12
2009-05-19 08:36:06 +02:00
* @ param array $args admin - account [ @ domain ], admin - password , account [ = new - account - name ], first - name , last - name , password , email , expires { never ( default ) | YYYY - MM - DD | already }, can - change - pw { true ( default ) | false }, anon - user { true | false ( default )}, primary - group { Default ( default ) |... }[, groups , ... ][, homedirectory , loginshell ]
2016-03-28 20:51:38 +02:00
* @ param boolean $run_addaccount_hook = null default run hook depending on account existence , true = allways run addaccount hook
2007-07-13 10:55:07 +02:00
*/
2010-08-20 22:28:59 +02:00
function do_edit_user ( $args , $run_addaccount_hook = null )
2007-07-13 10:55:07 +02:00
{
array_shift ( $args ); // admin-account
array_shift ( $args ); // admin-pw
list ( $account , $new_account_name ) = explode ( '=' , array_shift ( $args )); // account[=new-account-name]
2008-10-26 13:13:01 +01:00
2009-05-19 08:36:06 +02:00
$data = array ();
// do we need to support ldap only attributes: homedirectory and loginshell
if (( $GLOBALS [ 'egw_info' ][ 'server' ][ 'account_repository' ] == 'ldap' ||
empty ( $GLOBALS [ 'egw_info' ][ 'server' ][ 'account_repository' ]) && $GLOBALS [ 'egw_info' ][ 'server' ][ 'auth_type' ] == 'ldap' ) &&
$GLOBALS [ 'egw_info' ][ 'server' ][ 'ldap_extra_attributes' ] && count ( $args ) > 9 && // 9 = primary group
( $last_arg = array_pop ( $dummy = $args )) && $last_arg [ 0 ] == '/' ) // last argument start with a slash
{
$data [ 'loginshell' ] = array_pop ( $args );
$data [ 'homedirectory' ] = array_pop ( $args );
}
$data += array (
2007-11-27 04:20:28 +01:00
'account_lid' => $new_account_name ,
'account_firstname' => array_shift ( $args ),
'account_lastname' => array_shift ( $args ),
'account_passwd' => array_shift ( $args ),
'account_email' => array_shift ( $args ),
'account_expires' => array_shift ( $args ),
'changepassword' => array_shift ( $args ),
'anonymous' => array_shift ( $args ),
'account_primary_group' => array_shift ( $args ),
'account_groups' => $args ,
);
try {
admin_cmd :: parse_account ( $account , true );
2007-07-13 10:55:07 +02:00
2016-03-28 20:51:38 +02:00
foreach ( $data as & $value ) // existing account --> empty values mean dont change, not set them empty!
2007-07-13 10:55:07 +02:00
{
2007-11-27 04:20:28 +01:00
if (( string ) $value === '' ) $value = null ;
2007-07-13 10:55:07 +02:00
}
}
2007-11-27 04:20:28 +01:00
catch ( Exception $e ) { // new account
2016-03-28 20:51:38 +02:00
unset ( $e ); // not used
2007-11-27 04:20:28 +01:00
$data [ 'account_lid' ] = $account ;
$account = false ;
2016-03-28 20:51:38 +02:00
}
2010-08-20 22:28:59 +02:00
run_command ( new admin_cmd_edit_user ( $account , $data , null , $run_addaccount_hook ));
2007-05-08 15:36:17 +02:00
}
2006-09-19 08:59:55 +02:00
/**
2007-07-13 10:55:07 +02:00
* Delete a given acount from eGW
2006-09-19 08:59:55 +02:00
*
2007-07-13 10:55:07 +02:00
* @ param int / string $account account - name of - id
2016-03-28 20:51:38 +02:00
* @ param int / string $new_user = 0 for users only : account to move the entries too
* @ param boolean $is_user = true are we called for a user or group
2006-09-19 08:59:55 +02:00
* @ return int 0 on success , 2 - 4 otherwise ( see source )
*/
2007-11-27 04:20:28 +01:00
function do_delete_account ( $account , $new_user = 0 , $is_user = true )
2006-09-19 08:59:55 +02:00
{
2007-11-27 04:20:28 +01:00
run_command ( new admin_cmd_delete_account ( $account , $new_user , $is_user ));
2006-09-19 08:59:55 +02:00
}
2007-04-28 13:51:46 +02:00
2007-07-13 10:55:07 +02:00
/**
* Deletes ACL entries of not longer existing accounts
2008-10-26 13:13:01 +01:00
*
2007-07-13 10:55:07 +02:00
* @ return int 0 allways
*/
function do_check_acl ()
{
2007-11-27 04:20:28 +01:00
run_command ( new admin_cmd_check_acl ());
2007-07-13 10:55:07 +02:00
}
2007-04-28 13:51:46 +02:00
2007-07-13 10:55:07 +02:00
/**
* Changes one or more account_id ' s in the database ( make a backup before ! ) .
*
* @ param array $args admin - account [ @ domain ], admin - password , from1 , to1 [ ... , fromN , toN ]
* @ return int 0 on success
*/
2007-04-28 13:51:46 +02:00
function do_change_account_id ( $args )
{
2007-04-28 14:25:44 +02:00
if ( count ( $args ) < 4 ) usage (); // 4 means at least user,pw,from1,to1
2008-10-26 13:13:01 +01:00
2007-04-28 13:51:46 +02:00
$ids2change = array ();
for ( $n = 2 ; $n < count ( $args ); $n += 2 )
{
$from = ( int ) $args [ $n ];
$to = ( int ) $args [ $n + 1 ];
$ids2change [ $from ] = $to ;
}
2007-11-27 04:20:28 +01:00
run_command ( new admin_cmd_change_account_id ( $ids2change ));
2007-04-28 13:51:46 +02:00
}
2007-07-13 10:55:07 +02:00
/**
* List all exit codes used by the command line interface
*
2008-10-26 13:13:01 +01:00
* The list is generated by " greping " this file for calls to the fail () function .
2007-07-13 10:55:07 +02:00
* Calls to fail () have to be in one line , to be recogniced !
2008-10-26 13:13:01 +01:00
*
2007-12-06 09:00:41 +01:00
* @ ToDo adapt it to the exceptions
2007-07-13 10:55:07 +02:00
*/
function list_exit_codes ()
{
error_reporting ( error_reporting () & ~ E_NOTICE );
$codes = array ( 'Ok' );
2016-03-28 20:51:38 +02:00
foreach ( file ( __FILE__ ) as $line )
2007-07-13 10:55:07 +02:00
{
2016-03-28 20:51:38 +02:00
$matches = null ;
2007-07-13 10:55:07 +02:00
if ( preg_match ( '/fail\(([0-9]+),(.*)\);/' , $line , $matches ))
{
//echo "Line $n: $matches[1]: $matches[2]\n";
@ eval ( '$codes[' . $matches [ 1 ] . '] = ' . $matches [ 2 ] . ';' );
}
}
ksort ( $codes , SORT_NUMERIC );
foreach ( $codes as $num => $msg )
{
echo $num . " \t " . str_replace ( " \n " , " \n \t " , $msg ) . " \n " ;
}
}
2007-11-22 01:57:12 +01:00
/**
* Read the IMAP ACLs
*
* @ param array $args admin - account [ @ domain ], admin - password , accout_lid [, pw ]
* @ return int 0 on success
*/
function do_subscribe_other ( $account_lid , $pw = null )
{
2016-03-28 20:51:38 +02:00
unset ( $account_lid , $pw );
/* ToDo : this cant work , not even in 14. x
2007-11-22 01:57:12 +01:00
if ( ! ( $account_id = $GLOBALS [ 'egw' ] -> accounts -> name2id ( $account_lid )))
{
2016-04-29 15:02:10 +02:00
throw new Api\Exception\WrongUserinput ( lang ( " Unknown account: %1 !!! " , $account_lid ), 15 );
2007-11-22 01:57:12 +01:00
}
$GLOBALS [ 'egw_info' ][ 'user' ] = array (
'account_id' => $account_id ,
'account_lid' => $account_lid ,
'passwd' => $pw ,
);
2010-08-20 22:28:59 +02:00
$emailadmin = new emailadmin_bo ();
2007-11-22 01:57:12 +01:00
$user_profile = $emailadmin -> getUserProfile ( 'felamimail' );
unset ( $emailadmin );
2008-10-26 13:13:01 +01:00
2007-11-22 01:57:12 +01:00
$icServer = new cyrusimap ();
//$icServer =& $user_profile->ic_server[0];
//print_r($icServer);
2008-10-26 13:13:01 +01:00
2007-11-22 01:57:12 +01:00
$icServer -> openConnection ( ! $pw );
2008-10-26 13:13:01 +01:00
2007-11-22 01:57:12 +01:00
$delimiter = $icServer -> getHierarchyDelimiter ();
$mailboxes = $icServer -> getMailboxes ();
//print_r($mailboxes);
2008-10-26 13:13:01 +01:00
2007-11-22 01:57:12 +01:00
$own_mbox = 'user' . $delimiter . $account_lid ;
2008-10-26 13:13:01 +01:00
2007-11-22 01:57:12 +01:00
foreach ( $mailboxes as $n => $mailbox )
{
// if ($n < 1) continue;
if ( substr ( $mailbox , 0 , 5 ) != 'user' . $delimiter || substr ( $mailbox , 0 , strlen ( $own_mbox )) == $own_mbox ) continue ;
if ( ! $pw ) $mailbox = str_replace ( 'INBOX' , 'user' . $delimiter . $account_lid , $mailbox );
2016-03-28 20:51:38 +02:00
//$rights = $icServer->getACL($mailbox);
//echo "getACL($mailbox)\n";
//foreach($rights as $data)
//{
// echo $data['USER'].' '.$data['RIGHTS']."\n";
//}
2007-11-22 01:57:12 +01:00
echo " subscribing $mailbox for $account_lid\n " ;
//$icServer->subscribeMailbox($mailbox);
//exit;
2016-03-28 20:51:38 +02:00
} */
2007-12-05 03:27:49 +01:00
}