diff --git a/admin/admin-cli.php b/admin/admin-cli.php index d6ceb3d959..1ac967cd69 100755 --- a/admin/admin-cli.php +++ b/admin/admin-cli.php @@ -6,7 +6,7 @@ * @link http://www.egroupware.org * @package admin * @author Ralf Becker - * @copyright (c) 2006-18 by Ralf Becker + * @copyright (c) 2006-19 by Ralf Becker * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License */ @@ -24,6 +24,11 @@ elseif ($_SERVER['argc'] <= 1 || $_SERVER['argc'] == 2 && in_array($_SERVER['arg { usage(); } +elseif ($_SERVER['argv'][1] == '--exit-codes') +{ + list_exit_codes(); + exit(0); +} else { $arguments = $_SERVER['argv']; @@ -34,7 +39,7 @@ else // allow to specify instance by using a username with appended @domain-name $arg0s = explode(',',@array_shift($arguments)); @list($user,$domain) = explode('@',$arg0s[0].'@'); -load_egw($user,$arg0s[1],$domain); +load_egw($user, @$arg0s[1], $domain); switch($action) { @@ -78,9 +83,6 @@ switch($action) case '--show-header'; return run_command(new setup_cmd_showheader($arg0s[2])); - case '--exit-codes': - return list_exit_codes(); - default: // 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))) && @@ -107,7 +109,7 @@ switch($action) } return run_command(new $class($args)); } - usage($action); + usage($action, 1); break; } exit(0); @@ -170,7 +172,7 @@ function run_command(admin_cmd $cmd) } if ($dry_run && $skip_checks) { - echo lang('You can NOT use --try-run together with --skip-checks!')."\n\n"; + echo lang('You can NOT use --dry-run together with --skip-checks!')."\n\n"; usage('', 99); } //_debug_array($cmd); @@ -195,7 +197,7 @@ function run_command(admin_cmd $cmd) } } } - catch (Api\Exception\WrongUserinput $e) { + catch (Api\Exception $e) { echo "\n".$e->getMessage()."\n\n"; exit($e->getCode()); } @@ -632,29 +634,56 @@ function do_change_account_id($args) /** * List all exit codes used by the command line interface * - * The list is generated by "greping" this file for calls to the fail() function. - * Calls to fail() have to be in one line, to be recogniced! - * - * @ToDo adapt it to the exceptions + * The list is generated by "greping" this file for thrown exceptions. + * Exceptions have to be in one line, to be recogniced! */ function list_exit_codes() { error_reporting(error_reporting() & ~E_NOTICE); - $codes = array('Ok'); - foreach(file(__FILE__) as $line) + if (!function_exists('lang')) { - $matches = null; - if (preg_match('/fail\(([0-9]+),(.*)\);/',$line,$matches)) + function lang($str) { - //echo "Line $n: $matches[1]: $matches[2]\n"; - @eval('$codes['.$matches[1].'] = '.$matches[2].';'); + return $str; } } - ksort($codes,SORT_NUMERIC); - foreach($codes as $num => $msg) + + $codes = array(); + $files = array('admin-cli.php'); + foreach(scandir(__DIR__.'/inc') as $file) { - echo $num."\t".str_replace("\n","\n\t",$msg)."\n"; + if (substr($file,0,strlen('class.admin_cmd')) == 'class.admin_cmd') + { + $files[] = 'inc/'.$file; + } + } + foreach($files as $file) + { + $content = file_get_contents(__DIR__.'/'.$file); + + $matches = null; + if (preg_match_all('/throw new (Api\\\\Exception[\\\\a-z_]*)\((.*),\\s*([0-9]+)\);/mi',$content,$matches)) + { + //echo $file.":\n"; print_r($matches); + foreach($matches[3] as $key => $code) + { + $src = preg_replace('/(self::)?\$[a-z_>-]+/i', "''", $matches[2][$key]); // gives fatal error otherwise + @eval('$src = '.$src.';'); + + if (!empty($src) && (!isset($codes[$code]) || !in_array($src, $codes[$code]))) + { + //if (isset($codes[$code])) echo "$file redefines #$code: ".implode(', ', $codes[$code])."\n"; + $codes[$code][] = $src; + } + } + } + } + $codes[0] = 'Ok'; + ksort($codes, SORT_NUMERIC); + foreach($codes as $num => $msgs) + { + echo $num."\t".str_replace("\n","\n\t", implode(', ', (array)$msgs))."\n"; } } diff --git a/admin/inc/class.admin_cmd.inc.php b/admin/inc/class.admin_cmd.inc.php index 9de1739447..359dfbc537 100644 --- a/admin/inc/class.admin_cmd.inc.php +++ b/admin/inc/class.admin_cmd.inc.php @@ -473,7 +473,7 @@ abstract class admin_cmd if (!(class_exists($class = 'EGroupware\\'.$data['type']) || // namespaced class class_exists($class = $data['type'])) || $data['type'] == 'admin_cmd') { - throw new Api\Exception\WrongParameter(lang('Unknown command %1!',$class),0); + throw new Api\Exception\WrongParameter(lang('Unknown command %1!',$class), 10); } $cmd = new $class($data); @@ -481,7 +481,7 @@ abstract class admin_cmd { return $cmd; } - throw new Api\Exception\WrongParameter(lang('%1 is no command!',$class),0); + throw new Api\Exception\WrongParameter(lang('%1 is no command!',$class), 10); } /** @@ -793,8 +793,8 @@ abstract class admin_cmd * @param string|int $account account_id or account_lid * @param boolean $allow_only_user =null true=only user, false=only groups, default both * @return int/array account_id - * @throws Api\Exception\WrongUserinput(lang("Unknown account: %1 !!!",$account),15); - * @throws Api\Exception\WrongUserinput(lang("Wrong account type: %1 is NO %2 !!!",$account,$allow_only_user?lang('user'):lang('group')),15); + * @throws Api\Exception\WrongUserinput(lang("Unknown account: %1 !!!",$account), 15); + * @throws Api\Exception\WrongUserinput(lang("Wrong account type: %1 is NO %2 !!!",$account,$allow_only_user?lang('user'):lang('group')), 16); */ static function parse_account($account,$allow_only_user=null) { @@ -803,11 +803,11 @@ abstract class admin_cmd if (!($type = admin_cmd::$accounts->exists($account)) || !is_numeric($id=$account) && !($id = admin_cmd::$accounts->name2id($account))) { - throw new Api\Exception\WrongUserinput(lang("Unknown account: %1 !!!",$account),15); + throw new Api\Exception\WrongUserinput(lang("Unknown account: %1 !!!",$account), 15); } if (!is_null($allow_only_user) && $allow_only_user !== ($type == 1)) { - throw new Api\Exception\WrongUserinput(lang("Wrong account type: %1 is NO %2 !!!",$account,$allow_only_user?lang('user'):lang('group')),15); + throw new Api\Exception\WrongUserinput(lang("Wrong account type: %1 is NO %2 !!!",$account,$allow_only_user?lang('user'):lang('group')), 16); } if ($type == 2 && $id > 0) $id = -$id; // groups use negative id's internally, fix it, if user given the wrong sign @@ -820,8 +820,8 @@ abstract class admin_cmd * @param string|int|array $accounts array or comma-separated account_id's or account_lid's * @param boolean $allow_only_user =null true=only user, false=only groups, default both * @return array of account_id's or null if none specified - * @throws Api\Exception\WrongUserinput(lang("Unknown account: %1 !!!",$account),15); - * @throws Api\Exception\WrongUserinput(lang("Wrong account type: %1 is NO %2 !!!",$account,$allow_only?lang('user'):lang('group')),15); + * @throws Api\Exception\WrongUserinput(lang("Unknown account: %1 !!!",$account), 15); + * @throws Api\Exception\WrongUserinput(lang("Wrong account type: %1 is NO %2 !!!",$account,$allow_only?lang('user'):lang('group')), 16); */ static function parse_accounts($accounts,$allow_only_user=null) { diff --git a/admin/inc/class.admin_cmd_edit_group.inc.php b/admin/inc/class.admin_cmd_edit_group.inc.php index 4465efae9f..033d960e9d 100644 --- a/admin/inc/class.admin_cmd_edit_group.inc.php +++ b/admin/inc/class.admin_cmd_edit_group.inc.php @@ -63,16 +63,16 @@ class admin_cmd_edit_group extends admin_cmd ); if (!$data['account_lid'] && (!$this->account || !is_null($data['account_lid']))) { - throw new Api\Exception\WrongUserinput(lang('You must enter a group name.'),9); + throw new Api\Exception\WrongUserinput(lang('You must enter a group name.'), 17); } if (!is_null($data['account_lid']) && ($id = admin_cmd::$accounts->name2id($data['account_lid'],'account_lid','g')) && $id !== $data['account_id']) { - throw new Api\Exception\WrongUserinput(lang('That loginid has already been taken'),999); + throw new Api\Exception\WrongUserinput(lang('That loginid has already been taken'), 11); } if (!$data['account_members'] && !$this->account) { - throw new Api\Exception\WrongUserinput(lang('You must select at least one group member.'),9); + throw new Api\Exception\WrongUserinput(lang('You must select at least one group member.'), 18); } if ($data['account_members']) { diff --git a/admin/inc/class.admin_cmd_edit_user.inc.php b/admin/inc/class.admin_cmd_edit_user.inc.php index 0b42ff764f..c9e0cdf778 100644 --- a/admin/inc/class.admin_cmd_edit_user.inc.php +++ b/admin/inc/class.admin_cmd_edit_user.inc.php @@ -80,16 +80,16 @@ class admin_cmd_edit_user extends admin_cmd_change_pw } if (!$data['account_lastname'] && (!$this->account || !is_null($data['account_lastname']))) { - throw new Api\Exception\WrongUserinput(lang('You must enter a lastname'),9); + throw new Api\Exception\WrongUserinput(lang('You must enter a lastname'), 13); } if (!is_null($data['account_lid']) && ($id = admin_cmd::$accounts->name2id($data['account_lid'],'account_lid','u')) && (string)$id !== (string)$data['account_id']) { - throw new Api\Exception\WrongUserinput(lang('That loginid has already been taken'),999); + throw new Api\Exception\WrongUserinput(lang('That loginid has already been taken'), 11); } if (isset($data['account_passwd_2']) && $data['account_passwd'] != $data['account_passwd_2']) { - throw new Api\Exception\WrongUserinput(lang('The two passwords are not the same'),0); + throw new Api\Exception\WrongUserinput(lang('The two passwords are not the same'), 12); } $expires = self::_parse_expired($data['account_expires'],(boolean)$this->account); if ($expires === 0) // deactivated