From 884da4b664107a8507cf6bcbbe722f8e3d4304dd Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Thu, 15 Sep 2011 07:06:33 +0000 Subject: [PATCH] "modernized" importexport_cli a bit (removed uneccessary stuff) and added PEAR Console_Getopt dependency --- importexport/importexport_cli.php | 312 +++++++++++++++--------------- importexport/setup/setup.inc.php | 6 + 2 files changed, 158 insertions(+), 160 deletions(-) diff --git a/importexport/importexport_cli.php b/importexport/importexport_cli.php index df8373f4f2..ee9f4afa36 100755 --- a/importexport/importexport_cli.php +++ b/importexport/importexport_cli.php @@ -1,167 +1,159 @@ #!/usr/bin/php -q - * @version $Id$ - */ - - $path_to_egroupware = realpath(dirname(__FILE__).'/..'); - - $usage = "usage: - --definition Name of definition - --file File to import / for export - --user eGroupWare username for action - --password users password - --domain eGroupWare domain - --dry-run no real action, just console output - \n"; - - if (php_sapi_name() != 'cli') { - die('This script only runs form command line'); - } - - // Include PEAR::Console_Getopt - require_once 'Console/Getopt.php'; - - // Define exit codes for errors - define('HEADER_NOT_FOUND',9); - define('NO_ARGS',10); - define('INVALID_OPTION',11); - - // Reading the incoming arguments - same as $argv - $args = Console_Getopt::readPHPArgv(); - - // Make sure we got them (for non CLI binaries) - if (PEAR::isError($args)) { - fwrite(STDERR,"importexport_cli: ".$args->getMessage()."\n".$usage); - exit(NO_ARGS); - } - - // Short options - $short_opts = 'f:d:'; - - // Long options - $long_opts = array( - 'definition=', - 'file=', - 'user=', - 'password=', - 'domain=', - 'dry-run', - ); - - // Convert the arguments to options - check for the first argument - if ( realpath($_SERVER['argv'][0]) == __FILE__ ) { - $options = Console_Getopt::getOpt($args,$short_opts,$long_opts); - } else { - $options = Console_Getopt::getOpt2($args,$short_opts,$long_opts); - } - - // Check the options are valid - if (PEAR::isError($options)) { - fwrite(STDERR,"importexport_cli: ".$options->getMessage()."\n".$usage."\n"); - exit(INVALID_OPTION); - } - - $domain = 'default'; - $dryrun = false; - foreach ($options[0] as $option) { - switch ($option[0]) { - case '--file' : - $file = $option[1]; - break; - case '--definition' : - $definition = $option[1]; - break; - case '--domain' : - $domain = $option[1]; - break; - case '--user' : - $user = $option[1]; - break; - case '--password' : - $password = $option[1]; - break; - case '--dry-run' : - $dryrun = true; - break; - default : - fwrite (STDERR,$usage."\n"); - exit(INVALID_OPTION); - } - } - // check file - if ( !$user || !$password ) { - fwrite(STDERR,'importexport_cli: You have to supply a username / password'."\n".$usage); - exit(INVALID_OPTION); - } - - // Can't change domain once header is loaded - $_REQUEST['domain'] = $domain; +/** + * EGroupware - importexport + * + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License + * @package importexport + * @link http://www.egroupware.org + * @author Cornelius Weiss + * @version $Id$ + */ - $GLOBALS['egw_info']['flags'] = array( - 'disable_Template_class' => True, - 'noheader' => True, - 'nonavbar' => True, - 'currentapp' => 'importexport', - 'autocreate_session_callback' => 'import_export_access', - 'login' => $user . '@' . $domain, - 'passwd' => $password, - 'noapi' => True, - ); - if (!is_readable($path_to_egroupware.'/header.inc.php')) - { - fwrite(STDERR,"importexport.php: Could not find '$path_to_egroupware/header.inc.php', exiting !!!\n"); - exit(HEADER_NOT_FOUND); - } - include($path_to_egroupware.'/header.inc.php'); - unset($GLOBALS['egw_info']['flags']['noapi']); +$path_to_egroupware = realpath(dirname(__FILE__).'/..'); - // check domain - $db_type = $GLOBALS['egw_domain'][$domain]['db_type']; - if (!isset($GLOBALS['egw_domain'][$domain]) || empty($db_type)) - { - fwrite(STDERR,"importexport_cli: ". $domain. ' is not a valid domain name'."\n"); - exit(INVALID_OPTION); - } - $GLOBALS['egw_info']['server']['sessions_type'] = 'db'; // no php4-sessions availible for cgi - - include(EGW_API_INC.'/functions.inc.php'); - - // check file - if (!is_readable($file)) - { - fwrite(STDERR,"importexport_cli: ". $file. ' is not readable'."\n"); - exit(INVALID_OPTION); - } +$usage = "usage: + --definition Name of definition + --file File to import / for export + --user eGroupWare username for action + --password users password + --domain eGroupWare domain + --dry-run no real action, just console output + \n"; - $definition = new importexport_definition($definition); - if( $definition->get_identifier() < 1 ) { - fwrite(STDERR,"importexport_cli: Definition not found! \n"); - exit(INVALID_OPTION); - } - - $GLOBALS['egw_info']['flags']['currentapp'] = $definition->application; - - $definition->plugin_options['dry_run'] = $dryrun; - $type = $definition->type; - - $po = new $definition->plugin; - - $resource = fopen( $file, 'r' ); - $po->$type( $resource, $definition ); +if (php_sapi_name() != 'cli') { + die('This script only runs form command line'); +} - $GLOBALS['egw']->common->phpgw_exit(); - - function import_export_access(&$account) - { - $account['login'] = $GLOBALS['egw_info']['flags']['login']; - $account['passwd'] = $GLOBALS['egw_info']['flags']['passwd']; - $account['passwd_type'] = 'text'; - return true; +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 +} + +// Include PEAR::Console_Getopt +require_once 'Console/Getopt.php'; + +// Define exit codes for errors +define('HEADER_NOT_FOUND',9); +define('NO_ARGS',10); +define('INVALID_OPTION',11); + +// Reading the incoming arguments - same as $argv +$args = Console_Getopt::readPHPArgv(); + +// Make sure we got them (for non CLI binaries) +if (PEAR::isError($args)) { + fwrite(STDERR,"importexport_cli: ".$args->getMessage()."\n".$usage); + exit(NO_ARGS); +} + +// Short options +$short_opts = 'f:d:'; + +// Long options +$long_opts = array( + 'definition=', + 'file=', + 'user=', + 'password=', + 'domain=', + 'dry-run', + ); + +// Convert the arguments to options - check for the first argument +if ( realpath($_SERVER['argv'][0]) == __FILE__ ) { + $options = Console_Getopt::getOpt($args,$short_opts,$long_opts); +} else { + $options = Console_Getopt::getOpt2($args,$short_opts,$long_opts); +} + +// Check the options are valid +if (PEAR::isError($options)) { + fwrite(STDERR,"importexport_cli: ".$options->getMessage()."\n".$usage."\n"); + exit(INVALID_OPTION); +} + +$domain = 'default'; +$dryrun = false; +foreach ($options[0] as $option) { + switch ($option[0]) { + case '--file' : + $file = $option[1]; + break; + case '--definition' : + $definition = $option[1]; + break; + case '--domain' : + $domain = $option[1]; + break; + case '--user' : + $user = $option[1]; + break; + case '--password' : + $password = $option[1]; + break; + case '--dry-run' : + $dryrun = true; + break; + default : + fwrite (STDERR,$usage."\n"); + exit(INVALID_OPTION); } +} +// check file +if ( !$user || !$password ) { + fwrite(STDERR,'importexport_cli: You have to supply a username / password'."\n".$usage); + exit(INVALID_OPTION); +} + +// Can't change domain once header is loaded +$_REQUEST['domain'] = $domain; + +$GLOBALS['egw_info']['flags'] = array( + 'disable_Template_class' => True, + 'noheader' => True, + 'nonavbar' => True, + 'currentapp' => 'importexport', + 'autocreate_session_callback' => 'import_export_access', + 'login' => $user . '@' . $domain, + 'passwd' => $password, +); +if (!is_readable($path_to_egroupware.'/header.inc.php')) +{ + fwrite(STDERR,"importexport.php: Could not find '$path_to_egroupware/header.inc.php', exiting !!!\n"); + exit(HEADER_NOT_FOUND); +} +include($path_to_egroupware.'/header.inc.php'); + +// check file +if (!is_readable($file)) +{ + fwrite(STDERR,"importexport_cli: ". $file. ' is not readable'."\n"); + exit(INVALID_OPTION); +} + +$definition = new importexport_definition($definition); +if( $definition->get_identifier() < 1 ) { + fwrite(STDERR,"importexport_cli: Definition not found! \n"); + exit(INVALID_OPTION); +} + +$GLOBALS['egw_info']['flags']['currentapp'] = $definition->application; + +$definition->plugin_options['dry_run'] = $dryrun; +$type = $definition->type; + +$po = new $definition->plugin; + +$resource = fopen( $file, 'r' ); +$po->$type( $resource, $definition ); + +common::egw_exit(); + +function import_export_access(&$account) +{ + $account['login'] = $GLOBALS['egw_info']['flags']['login']; + $account['passwd'] = $GLOBALS['egw_info']['flags']['passwd']; + $account['passwd_type'] = 'text'; + return true; +} diff --git a/importexport/setup/setup.inc.php b/importexport/setup/setup.inc.php index a8d9c6f343..539251976e 100644 --- a/importexport/setup/setup.inc.php +++ b/importexport/setup/setup.inc.php @@ -49,5 +49,11 @@ $setup_info['importexport']['check_install'] = array( 'dom' => array( 'func' => 'extension_check', ), + '' => array( + 'func' => 'pear_check', + ), + 'Console_Getopt' => array( + 'func' => 'pear_check', + ), );