1#!/usr/bin/php -q
2<?php
3/**
4 * EGroupware - importexport
5 *
6 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
7 * @package importexport
8 * @link http://www.egroupware.org
9 * @author Cornelius Weiss <nelius@cwtech.de>
10 * @version $Id$
11 */
12
13use EGroupware\Api;
14
15$path_to_egroupware = realpath(dirname(__FILE__).'/..');
16
17$usage = "usage:
18		--definition <name of definition>    Name of definition
19		--file <name of file>                File to import / for export
20		--user <eGW username>                eGroupWare username for action
21		--password <password for user>       users password
22		--domain <domain name>               eGroupWare domain
23		--dry-run                            no real action, just console output
24		\n";
25
26if (php_sapi_name() != 'cli') {
27	die('This script only runs form command line');
28}
29
30if (ini_get('session.save_handler') == 'files' && !is_writable(ini_get('session.save_path')) && is_dir('/tmp') && is_writable('/tmp'))
31{
32	ini_set('session.save_path','/tmp');	// regular users may have no rights to apache's session dir
33}
34
35// Include PEAR::Console_Getopt
36require_once dirname(__DIR__).'/vendor/autoload.php';
37
38// Define exit codes for errors
39define('HEADER_NOT_FOUND',9);
40define('NO_ARGS',10);
41define('INVALID_OPTION',11);
42
43// Reading the incoming arguments - same as $argv
44$args = Console_Getopt::readPHPArgv();
45
46// Make sure we got them (for non CLI binaries)
47if (PEAR::isError($args)) {
48   fwrite(STDERR,"importexport_cli: ".$args->getMessage()."\n".$usage);
49   exit(NO_ARGS);
50}
51
52// Short options
53$short_opts = 'f:d:';
54
55// Long options
56$long_opts = array(
57   'definition=',
58   'file=',
59   'user=',
60   'password=',
61   'domain=',
62   'dry-run',
63   );
64
65// Convert the arguments to options - check for the first argument
66if ( realpath($_SERVER['argv'][0]) == __FILE__ ) {
67   $options = Console_Getopt::getOpt($args,$short_opts,$long_opts);
68} else {
69   $options = Console_Getopt::getOpt2($args,$short_opts,$long_opts);
70}
71
72// Check the options are valid
73if (PEAR::isError($options)) {
74   fwrite(STDERR,"importexport_cli: ".$options->getMessage()."\n".$usage."\n");
75   exit(INVALID_OPTION);
76}
77
78$domain = 'default';
79$dryrun = false;
80foreach ($options[0] as $option) {
81	switch ($option[0]) {
82		case '--file' :
83			$file = $option[1];
84			break;
85		case '--definition' :
86			$definition = $option[1];
87			break;
88		case '--domain' :
89			$domain = $option[1];
90			break;
91		case '--user' :
92			$user = $option[1];
93			break;
94		case '--password' :
95			$password = $option[1];
96			break;
97		case '--dry-run' :
98			$dryrun = true;
99			break;
100		default :
101			fwrite (STDERR,$usage."\n");
102			exit(INVALID_OPTION);
103	}
104}
105// check file
106if ( !$user || !$password ) {
107	fwrite(STDERR,'importexport_cli: You have to supply a username / password'."\n".$usage);
108	exit(INVALID_OPTION);
109}
110
111// Can't change domain once header is loaded
112$_REQUEST['domain'] = $domain;
113
114$GLOBALS['egw_info']['flags'] = array(
115	'disable_Template_class' => True,
116	'noheader'  => True,
117	'nonavbar' => True,
118	'currentapp' => 'importexport',
119	'autocreate_session_callback' => 'import_export_access',
120	'login' => $user . '@' . $domain,
121	'passwd' => $password,
122);
123if (!is_readable($path_to_egroupware.'/header.inc.php'))
124{
125	fwrite(STDERR,"importexport.php: Could not find '$path_to_egroupware/header.inc.php', exiting !!!\n");
126	exit(HEADER_NOT_FOUND);
127}
128include($path_to_egroupware.'/header.inc.php');
129
130// check file
131if (!is_readable($file))
132{
133	fwrite(STDERR,"importexport_cli: ". $file. ' is not readable'."\n");
134	exit(INVALID_OPTION);
135}
136
137$definition = new importexport_definition($definition);
138if( $definition->get_identifier() < 1 ) {
139	fwrite(STDERR,"importexport_cli: Definition not found! \n");
140	exit(INVALID_OPTION);
141}
142
143$GLOBALS['egw_info']['flags']['currentapp'] = $definition->application;
144
145$plugin_options = $definition->plugin_options;
146$plugin_options['dry_run'] = $dryrun;
147$definition->plugin_options = $plugin_options;
148$type = $definition->type;
149
150$po = new $definition->plugin;
151
152$resource = fopen( $file, 'r' );
153$po->$type( $resource, $definition );
154
155exit();
156
157function import_export_access(&$account)
158{
159	$account['login'] = $GLOBALS['egw_info']['flags']['login'];
160	$account['passwd'] = $GLOBALS['egw_info']['flags']['passwd'];
161	$account['passwd_type'] = 'text';
162	return true;
163}
164