2010-02-26 00:14:24 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2016-05-16 12:18:20 +02:00
|
|
|
* EGroupware - ImportExport
|
2010-02-26 00:14:24 +01:00
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package importexport
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
* @copyright Nathan Gray
|
|
|
|
* @version $Id: class.importexport_import_ui.inc.php 27222 2009-06-08 16:21:14Z ralfbecker $
|
|
|
|
*/
|
|
|
|
|
2016-05-10 00:49:20 +02:00
|
|
|
use EGroupware\Api;
|
|
|
|
use EGroupware\Api\Framework;
|
|
|
|
use EGroupware\Api\Vfs;
|
|
|
|
use EGroupware\Api\Etemplate;
|
2010-02-26 00:14:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* userinterface for admins to schedule imports or exports using async services
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
class importexport_schedule_ui
|
|
|
|
{
|
|
|
|
public $public_functions = array(
|
|
|
|
'index' => true,
|
|
|
|
'edit' => true,
|
|
|
|
);
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
protected static $template;
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-17 17:03:30 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$this->template = new Etemplate();
|
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-17 17:03:30 +02:00
|
|
|
public function index($content = array())
|
|
|
|
{
|
2016-05-26 11:40:23 +02:00
|
|
|
$async = new Api\Asyncservice();
|
2016-05-16 12:18:20 +02:00
|
|
|
if(is_array($content['scheduled']))
|
|
|
|
{
|
|
|
|
foreach($content['scheduled'] as $row)
|
2011-12-09 17:25:17 +01:00
|
|
|
{
|
2016-05-17 17:03:30 +02:00
|
|
|
if($row['delete'])
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$key = urldecode(key($row['delete']));
|
2016-05-26 11:40:23 +02:00
|
|
|
$async->cancel_timer($key);
|
2011-12-09 17:25:17 +01:00
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
}
|
2016-05-26 11:40:23 +02:00
|
|
|
$async_list = $async->read('importexport%');
|
2016-05-16 12:18:20 +02:00
|
|
|
$data = array();
|
2016-05-17 17:03:30 +02:00
|
|
|
if(is_array($async_list))
|
|
|
|
{
|
|
|
|
foreach($async_list as $id => $async)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
foreach(array('errors', 'warnings', 'result') as $messages)
|
|
|
|
{
|
|
|
|
if(is_array($async['data'][$messages]))
|
2011-12-15 17:56:52 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$list = array();
|
|
|
|
foreach($async['data'][$messages] as $target => $message)
|
2011-12-15 17:56:52 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$list[] = array(
|
|
|
|
'target' => (is_numeric($target) ? '' : $target),
|
|
|
|
'message' => implode("\n", (array)$message)
|
|
|
|
);
|
2011-12-15 17:56:52 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
$async['data'][$messages] = $list;
|
2011-12-15 17:56:52 +01:00
|
|
|
}
|
2010-03-02 23:33:47 +01:00
|
|
|
}
|
2016-05-17 17:03:30 +02:00
|
|
|
if(is_numeric($async['data']['record_count']))
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$async['data']['record_count'] = lang('%1 records processed', $async['data']['record_count']);
|
|
|
|
}
|
|
|
|
$data['scheduled'][] = array_merge($async['data'], array(
|
|
|
|
'id' => urlencode($id),
|
|
|
|
'next' => Api\DateTime::server2user($async['next']),
|
|
|
|
'times' => str_replace("\n", '', print_r($async['times'], true)),
|
|
|
|
'last_run' => $async['data']['last_run'] ? Api\DateTime::server2user($async['data']['last_run']) : ''
|
|
|
|
));
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
array_unshift($data['scheduled'], false);
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
$sel_options = self::get_select_options($data);
|
|
|
|
$this->template->read('importexport.schedule_index');
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$GLOBALS['egw_info']['flags']['app_header'] = lang('Schedule import / export');
|
|
|
|
$this->template->exec('importexport.importexport_schedule_ui.index', $data, $sel_options);
|
|
|
|
}
|
2011-06-02 18:50:41 +02:00
|
|
|
|
2016-05-17 17:03:30 +02:00
|
|
|
public function edit($content = array())
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$id = $_GET['id'] ? urldecode($_GET['id']) : $content['id'];
|
|
|
|
$definition_id = $_GET['definition'];
|
|
|
|
$async = new Api\Asyncservice();
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
unset($content['id']);
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$data = $content;
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// Deal with incoming
|
2016-05-17 17:03:30 +02:00
|
|
|
if($content['save'] && self::check_target($content) === true)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
unset($content['save']);
|
|
|
|
$async->cancel_timer($id);
|
|
|
|
$id = self::generate_id($content);
|
|
|
|
$schedule = $content['schedule'];
|
|
|
|
// Async sometimes changes minutes to an array - keep what user typed
|
|
|
|
$content['min'] = $schedule['min'];
|
|
|
|
unset($content['schedule']);
|
|
|
|
|
|
|
|
// Remove any left blank
|
2016-05-17 17:03:30 +02:00
|
|
|
foreach($schedule as $key => &$value)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
if($value == '') unset($schedule[$key]);
|
|
|
|
}
|
|
|
|
$result = $async->set_timer(
|
|
|
|
$schedule,
|
|
|
|
$id,
|
|
|
|
'importexport.importexport_schedule_ui.exec',
|
|
|
|
$content
|
|
|
|
);
|
2016-05-17 17:03:30 +02:00
|
|
|
if($result)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
Framework::refresh_opener('', 'admin',$id,'update','admin');
|
|
|
|
Framework::window_close();
|
2016-05-17 17:03:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['message'] = lang('Unable to schedule');
|
|
|
|
unset($id);
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-17 17:03:30 +02:00
|
|
|
if($id)
|
|
|
|
{
|
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$preserve['id'] = $id;
|
|
|
|
$async = $async->read($id);
|
2016-05-17 17:03:30 +02:00
|
|
|
if(is_array($async[$id]['data']))
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data += $async[$id]['data'];
|
|
|
|
$data['schedule'] = $async[$id]['times'];
|
|
|
|
unset($data['times']);
|
2011-12-07 21:05:02 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// Async sometimes changes minutes to an array - show user what they typed
|
2016-05-17 17:03:30 +02:00
|
|
|
if(is_array($data['schedule']['min']))
|
|
|
|
{
|
|
|
|
$data['schedule']['min'] = $data['min'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['message'] = lang('Schedule not found');
|
|
|
|
}
|
2016-05-17 17:03:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['type'] = $content['type'] ? $content['type'] : 'import';
|
|
|
|
|
2016-05-17 17:03:30 +02:00
|
|
|
if((int)$definition_id)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$bo = new importexport_definitions_bo();
|
|
|
|
$definition = $bo->read($definition_id);
|
2016-05-17 17:03:30 +02:00
|
|
|
if($definition['definition_id'])
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['type'] = $definition['type'];
|
|
|
|
$data['appname'] = $definition['application'];
|
|
|
|
$data['plugin'] = $definition['plugin'];
|
|
|
|
$data['definition'] = $definition['name'];
|
2011-06-02 18:50:41 +02:00
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-17 17:03:30 +02:00
|
|
|
if($data['target'] && $data['type'])
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$file_check = self::check_target($data);
|
2016-05-17 17:03:30 +02:00
|
|
|
if($file_check !== true)
|
|
|
|
{
|
|
|
|
$data['message'] .= ($data['message'] ? "\n" . $file_check : $file_check);
|
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['no_delete_files'] = $data['type'] != 'import';
|
2012-02-01 21:49:27 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// Current server time for nice message
|
|
|
|
$data['current_time'] = time();
|
2012-02-01 21:49:27 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$sel_options = self::get_select_options($data);
|
|
|
|
Framework::includeJS('.','importexport','importexport');
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$GLOBALS['egw_info']['flags']['app_header'] = lang('Schedule import / export');
|
|
|
|
$this->template->read('importexport.schedule_edit');
|
|
|
|
$this->template->exec('importexport.importexport_schedule_ui.edit', $data, $sel_options, array(), $preserve, 2);
|
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
/**
|
|
|
|
* Get options for select boxes
|
|
|
|
*/
|
2016-05-17 17:03:30 +02:00
|
|
|
public static function get_select_options(Array $data)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$options = array(
|
|
|
|
'type' => array(
|
|
|
|
'import' => lang('import'),
|
|
|
|
'export' => lang('export')
|
|
|
|
)
|
|
|
|
);
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
(array)$apps = importexport_helper_functions::get_apps($data['type'] ? $data['type'] : 'all');
|
2016-05-17 17:03:30 +02:00
|
|
|
if(count($apps))
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$options['appname'] = array('' => lang('Select one')) + array_combine($apps,$apps);
|
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$plugins = importexport_helper_functions::get_plugins($data['appname'] ? $data['appname'] : 'all', $data['type']);
|
2016-05-17 17:03:30 +02:00
|
|
|
if(is_array($plugins))
|
|
|
|
{
|
|
|
|
foreach($plugins as $types)
|
|
|
|
{
|
|
|
|
if(!is_array($types[$data['type']]))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach($types[$data['type']] as $key => $title)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$options['plugin'][$key] = $title;
|
2012-03-13 11:22:15 +01:00
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
}
|
2011-04-11 17:45:29 +02:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$options['definition'] = array();
|
|
|
|
|
|
|
|
// If the query isn't started with something, bodefinitions won't load the definitions
|
|
|
|
$query = array();
|
|
|
|
$query['type'] = $data['type'];
|
|
|
|
$query['application'] = $data['application'];
|
|
|
|
$query['plugin'] = $data['plugin'];
|
|
|
|
$definitions = new importexport_definitions_bo($query);
|
2016-05-17 17:03:30 +02:00
|
|
|
foreach ((array)$definitions->get_definitions() as $identifier)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$definition = new importexport_definition($identifier);
|
2016-05-17 17:03:30 +02:00
|
|
|
}
|
|
|
|
catch (Exception $e)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
unset($e);
|
|
|
|
// permission error
|
|
|
|
continue;
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-17 17:03:30 +02:00
|
|
|
if (($title = $definition->get_title()))
|
|
|
|
{
|
2018-12-05 21:25:34 +01:00
|
|
|
$options['definition'][] = array(
|
|
|
|
'value' => $definition->get_identifier(),
|
|
|
|
'label' => $title
|
|
|
|
);
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
unset($definition);
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
unset($definitions);
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
return $options;
|
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
/**
|
|
|
|
* Generate a async key
|
|
|
|
*/
|
2016-05-17 17:03:30 +02:00
|
|
|
public static function generate_id($data)
|
|
|
|
{
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$query = array(
|
|
|
|
'name' => $data['definition']
|
|
|
|
);
|
2011-04-11 17:45:29 +02:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$definitions = new importexport_definitions_bo($query);
|
|
|
|
$definition_list = ((array)$definitions->get_definitions());
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$id = 'importexport.'.$definition_list[0].'.'.$data['target'];
|
|
|
|
return $id;
|
|
|
|
}
|
2011-12-06 01:44:07 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
/**
|
|
|
|
* Check that the target is valid for the type (readable or writable)
|
|
|
|
* and that they're not trying to write directly to the filesystem
|
|
|
|
*
|
|
|
|
* $data should contain target & type
|
|
|
|
*/
|
2021-06-09 23:23:25 +02:00
|
|
|
public static function check_target(Array &$data) {
|
2016-05-16 12:18:20 +02:00
|
|
|
$scheme = parse_url($data['target'], PHP_URL_SCHEME);
|
2021-06-09 23:23:25 +02:00
|
|
|
if($scheme == 'file')
|
2016-05-17 17:03:30 +02:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
return 'Direct file access not allowed';
|
|
|
|
}
|
2021-06-09 23:23:25 +02:00
|
|
|
else if ($scheme == '')
|
|
|
|
{
|
|
|
|
$data['target'] = Vfs::PREFIX.$data['target'];
|
|
|
|
return static::check_target($data);
|
|
|
|
}
|
2016-05-17 17:03:30 +02:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
if($scheme == Vfs::SCHEME && !in_array(Vfs::SCHEME, stream_get_wrappers())) {
|
|
|
|
stream_wrapper_register(Vfs::SCHEME, 'vfs_stream_wrapper', STREAM_IS_URL);
|
2010-10-19 19:29:11 +02:00
|
|
|
}
|
2021-06-09 23:23:25 +02:00
|
|
|
else if (!in_array($scheme, stream_get_wrappers()))
|
|
|
|
{
|
|
|
|
return lang("Unable to access files with '%1'",$scheme);
|
|
|
|
}
|
2010-10-19 19:29:11 +02:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
if ($data['type'] == 'import' && ($scheme == Vfs::SCHEME && !Vfs::is_readable($data['target'])))
|
2012-02-14 21:29:33 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
return lang('%1 is not readable',$data['target']);
|
|
|
|
}
|
|
|
|
elseif ($data['type'] == 'import' && in_array($scheme, array('http','https')))
|
|
|
|
{
|
|
|
|
// Not supported by is_readable, try headers...
|
|
|
|
stream_context_set_default(array('http'=>array(
|
|
|
|
'method' => 'HEAD',
|
|
|
|
'ignore_errors' => 1
|
|
|
|
)));
|
|
|
|
$headers = get_headers($data['target'],1);
|
|
|
|
|
|
|
|
// Reset...
|
|
|
|
stream_context_set_default(array('http'=>array(
|
|
|
|
'method' => 'GET',
|
|
|
|
'ignore_errors' => 0
|
|
|
|
)));
|
|
|
|
// Response code has an integer key, but redirects may add more responses
|
|
|
|
for($i = 0; $i < count($headers); $i++)
|
2012-02-14 21:29:33 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
if(!$headers[$i]) break;
|
|
|
|
if(strpos($headers[$i],'200') !== false) return true;
|
2012-02-14 21:29:33 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
return lang('%1 is not readable',$data['target']);
|
|
|
|
}
|
2016-05-17 17:03:30 +02:00
|
|
|
elseif ($data['type'] == 'export' && !self::is__writable($data['target']))
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
return lang('%1 is not writable',$data['target']);
|
|
|
|
}
|
2012-02-14 21:29:33 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
return true;
|
|
|
|
}
|
2011-12-07 18:18:08 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
/**
|
|
|
|
* Writable that checks the folder too, in case the file does not exist yet
|
|
|
|
* http://ca3.php.net/manual/en/function.is-writable.php#73596
|
|
|
|
*
|
|
|
|
* @param path Path to check
|
|
|
|
*/
|
2016-05-17 17:03:30 +02:00
|
|
|
private static function is__writable($path)
|
|
|
|
{
|
2021-03-21 21:13:07 +01:00
|
|
|
if ($path[strlen($path)-1] === '/')
|
2016-05-17 17:03:30 +02:00
|
|
|
{
|
|
|
|
// recursively return a temporary file path
|
2016-05-16 12:18:20 +02:00
|
|
|
return self::is__writable($path.uniqid(mt_rand()).'.tmp');
|
2016-05-17 17:03:30 +02:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
else if (is_dir($path))
|
2016-05-17 17:03:30 +02:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
return self::is__writable($path.'/'.uniqid(mt_rand()).'.tmp');
|
2016-05-17 17:03:30 +02:00
|
|
|
}
|
2016-05-26 11:40:23 +02:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// check tmp file for read/write capabilities
|
|
|
|
$rm = file_exists($path);
|
|
|
|
$f = @fopen($path, 'a');
|
|
|
|
|
|
|
|
if ($f===false)
|
2016-05-17 17:03:30 +02:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
return false;
|
2016-05-17 17:03:30 +02:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
|
|
|
|
fclose($f);
|
2016-05-17 17:03:30 +02:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
if (!$rm)
|
2016-05-17 17:03:30 +02:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
@unlink($path);
|
2016-05-17 17:03:30 +02:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-13 11:22:15 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
/**
|
|
|
|
* Execute a scheduled import or export
|
|
|
|
*/
|
|
|
|
public static function exec($data)
|
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
|
|
|
|
$data['record_count'] = 0;
|
|
|
|
unset($data['errors']);
|
|
|
|
unset($data['warnings']);
|
|
|
|
unset($data['result']);
|
2011-12-07 18:18:08 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
if($data['lock'])
|
|
|
|
{
|
|
|
|
// Lock expires
|
|
|
|
if($data['lock'] < time())
|
|
|
|
{
|
|
|
|
unset($data['lock']);
|
|
|
|
$data['warnings'][][] = lang('Lock expired on previous run');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Still running
|
|
|
|
ob_end_flush();
|
2011-12-07 21:05:02 +01:00
|
|
|
return;
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['last_run'] = time();
|
2011-12-07 18:18:08 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// Lock job for an hour to prevent multiples overlapping
|
2021-06-09 23:23:25 +02:00
|
|
|
// $data['lock'] = time() + 3600;
|
2016-05-16 12:18:20 +02:00
|
|
|
self::update_job($data, true);
|
|
|
|
|
|
|
|
// check file
|
|
|
|
$file_check = self::check_target($data);
|
2016-05-17 17:03:30 +02:00
|
|
|
if($file_check !== true)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['errors'] = array($file_check=>'');
|
|
|
|
// Update job with results
|
|
|
|
self::update_job($data);
|
|
|
|
|
|
|
|
error_log('importexport_schedule: ' . date('c') . ": $file_check \n");
|
|
|
|
error_log(ob_get_flush());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$definition = new importexport_definition($data['definition']);
|
2016-05-17 17:03:30 +02:00
|
|
|
if( $definition->get_identifier() < 1 )
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['errors'] = array('Definition not found!');
|
|
|
|
// Update job with results
|
|
|
|
self::update_job($data);
|
|
|
|
|
|
|
|
error_log('importexport_schedule: ' . date('c') . ": Definition not found! \n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$GLOBALS['egw_info']['flags']['currentapp'] = $definition->application;
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$po = new $definition->plugin;
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$type = $data['type'];
|
2010-02-26 00:14:24 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
if(is_dir($data['target']))
|
|
|
|
{
|
|
|
|
if($data['type'] == 'import')
|
2011-12-06 01:44:07 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$targets = array();
|
|
|
|
foreach(scandir($data['target']) as $target)
|
2011-12-06 01:44:07 +01:00
|
|
|
{
|
2016-05-17 17:03:30 +02:00
|
|
|
if ($target == '.' || $target == '..')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
$target = $data['target'].(substr($data['target'],-1) == '/' ? '' : '/').$target;
|
2012-03-13 11:22:15 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// Check modification time, make sure it's not currently being written
|
|
|
|
// Skip files modified in the last 10 seconds
|
|
|
|
$mod_time = filemtime($target);
|
|
|
|
if($mod_time >= time() - 10)
|
2011-12-06 01:44:07 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['result'][$target] = lang('Skipped');
|
|
|
|
continue;
|
2011-12-06 01:44:07 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
$targets[$mod_time.$target] = $target;
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
if($targets)
|
2011-12-12 18:23:24 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
ksort($targets);
|
2011-12-12 18:23:24 +01:00
|
|
|
}
|
2011-12-06 01:44:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
// Create a unique file for export
|
|
|
|
$targets = array($data['target'].uniqid($definition->name).'.'.$po->get_filesuffix());
|
2010-03-02 23:33:47 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$targets = array($data['target']);
|
|
|
|
}
|
2010-03-02 23:33:47 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
if($type == 'export')
|
|
|
|
{
|
|
|
|
// Set to export all or filter, if set
|
|
|
|
$selection = array('selection' => 'all');
|
|
|
|
if($definition->filter)
|
2012-04-18 19:48:08 +02:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$fields = importexport_helper_functions::get_filter_fields($definition->application, $po);
|
|
|
|
$selection = array('selection' => 'filter');
|
|
|
|
$filters = array();
|
|
|
|
foreach($definition->filter as $field => $value)
|
2012-12-31 21:36:28 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
// Handle multiple values
|
2016-05-17 17:03:30 +02:00
|
|
|
if(!is_array($value) && strpos($value,',') !== false)
|
|
|
|
{
|
|
|
|
$value = explode(',',$value);
|
|
|
|
}
|
2012-12-31 21:36:28 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$filters[$field] = $value;
|
2012-12-31 21:36:28 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// Process relative dates into the current absolute date
|
|
|
|
if($filters[$field] && strpos($fields[$field]['type'],'date') === 0)
|
|
|
|
{
|
|
|
|
$filters[$field] = importexport_helper_functions::date_rel2abs($value);
|
2012-12-31 21:36:28 +01:00
|
|
|
}
|
2013-01-29 19:22:46 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
// Update filter to use current absolute dates
|
|
|
|
$definition->filter = $filters;
|
2012-04-18 19:48:08 +02:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
if(!is_array($definition->plugin_options))
|
2011-12-06 01:44:07 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$definition->plugin_options = array();
|
|
|
|
}
|
|
|
|
$definition->plugin_options = array_merge($definition->plugin_options, $selection);
|
|
|
|
}
|
2019-05-10 21:33:04 +02:00
|
|
|
// Set some automatic admin history data, if the plugin wants it
|
|
|
|
$definition->plugin_options = array_merge($definition->plugin_options, array('admin_cmd' => array(
|
|
|
|
'comment' => lang('schedule import / export') . "\n" . $definition->get_title() . "\n" . $target
|
|
|
|
)));
|
2016-05-16 12:18:20 +02:00
|
|
|
|
|
|
|
foreach($targets as $target)
|
|
|
|
{
|
|
|
|
// Update lock timeout
|
|
|
|
$data['lock'] = time() + 3600;
|
|
|
|
self::update_job($data, true);
|
|
|
|
|
|
|
|
$resource = null;
|
2016-05-17 17:03:30 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (($resource = @fopen( $target, $data['type'] == 'import' ? 'rb' : 'wb' )))
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$result = $po->$type( $resource, $definition );
|
|
|
|
|
2011-12-06 01:44:07 +01:00
|
|
|
fclose($resource);
|
2016-05-17 17:03:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
error_log('importexport_schedule: ' . date('c') . ": File $target not readable! \n");
|
|
|
|
$data['errors'][$target][] = lang('%1 is not readable',$target);
|
2010-03-03 17:28:45 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
}
|
|
|
|
catch (Exception $i_ex)
|
|
|
|
{
|
|
|
|
fclose($resource);
|
|
|
|
$data['errors'][$target][] = $i_ex->getMessage();
|
|
|
|
}
|
2012-03-13 11:22:15 +01:00
|
|
|
|
2011-12-06 01:44:07 +01:00
|
|
|
|
2016-05-17 17:03:30 +02:00
|
|
|
if(method_exists($po, 'get_warnings') && $po->get_warnings())
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$buffer = 'importexport_schedule: ' . date('c') . ": Import warnings:\n#\tWarning\n";
|
2016-05-17 17:03:30 +02:00
|
|
|
foreach($po->get_warnings() as $record => $msg)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['warnings'][$target][] = "#$record: $msg";
|
2021-11-22 08:41:01 +01:00
|
|
|
$buffer .= "$record\t$msg\n";
|
2011-12-15 17:56:52 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
error_log($buffer);
|
|
|
|
}
|
2016-05-17 17:03:30 +02:00
|
|
|
if(method_exists($po, 'get_errors') && $po->get_errors())
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$buffer = 'importexport_schedule: ' . date('c') . ": Import errors:\n#\tError\n";
|
2016-05-17 17:03:30 +02:00
|
|
|
foreach($po->get_errors() as $record => $error)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['errors'][$target][] = "#$record: $error";
|
2021-11-22 08:41:01 +01:00
|
|
|
$buffer .= "$record\t$error\n";
|
2011-12-06 01:44:07 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
error_log($buffer);
|
|
|
|
}
|
2011-12-06 01:44:07 +01:00
|
|
|
|
2016-05-17 17:03:30 +02:00
|
|
|
if($po instanceof importexport_iface_import_plugin)
|
|
|
|
{
|
|
|
|
if(is_numeric($result))
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['record_count'] += $result;
|
|
|
|
$data['result'][$target][] = lang('%1 records processed', $result);
|
|
|
|
}
|
|
|
|
$data['result'][$target] = array();
|
2016-05-17 17:03:30 +02:00
|
|
|
foreach($po->get_results() as $action => $count)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['result'][$target][] = lang($action) . ": $count";
|
|
|
|
}
|
2016-05-17 17:03:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
if($result instanceof importexport_iface_export_record)
|
|
|
|
{
|
|
|
|
$data['record_count'] += $result->get_num_of_records();
|
|
|
|
$data['result'][$target][] = lang('%1 records processed', $result->get_num_of_records());
|
2010-03-03 17:28:45 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
}
|
2010-03-02 23:33:47 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// Delete file?
|
|
|
|
if($data['delete_files'] && $type == 'import' && !$data['errors'])
|
|
|
|
{
|
|
|
|
foreach($targets as $target)
|
2011-12-06 01:44:07 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
if(unlink($target))
|
2011-12-06 01:44:07 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$data['result'][$target][] .= "\n..." . lang('deleted');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$data['errors'][$target][] .= "\n..." . lang('Unable to delete');
|
2011-12-06 01:44:07 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
}
|
2011-12-06 01:44:07 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// Run time in minutes
|
|
|
|
$data['run_time'] = round((time() - $data['last_run']) / 60,1);
|
2012-02-14 21:29:33 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// Clear lock
|
|
|
|
$data['lock'] = 0;
|
2011-12-13 21:00:40 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// Update job with results
|
|
|
|
self::update_job($data);
|
2011-12-07 18:18:08 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
$contents = ob_get_contents();
|
2011-12-09 19:44:29 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// Log to error log
|
|
|
|
if($contents)
|
|
|
|
{
|
|
|
|
error_log('importexport_schedule: ' . date('c') . ": \n".$contents);
|
|
|
|
}
|
2011-12-09 19:44:29 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
ob_end_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the async job with current status, and send a notification
|
|
|
|
* to user if there were any errors.
|
|
|
|
*/
|
2016-05-17 17:03:30 +02:00
|
|
|
private static function update_job($data, $no_notification = false)
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$id = self::generate_id($data);
|
|
|
|
$async = new Api\Asyncservice();
|
|
|
|
$jobs = $async->read($id);
|
|
|
|
$job = $jobs[$id];
|
2016-05-26 11:40:23 +02:00
|
|
|
|
2016-05-17 17:03:30 +02:00
|
|
|
if(is_array($job))
|
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$async->cancel_timer($id);
|
|
|
|
$result = $async->set_timer(
|
2016-06-09 16:19:41 +02:00
|
|
|
$job['times'],
|
2016-05-16 12:18:20 +02:00
|
|
|
$id,
|
|
|
|
'importexport.importexport_schedule_ui.exec',
|
|
|
|
$data
|
|
|
|
);
|
2011-12-07 18:18:08 +01:00
|
|
|
}
|
2016-05-17 17:03:30 +02:00
|
|
|
if($no_notification)
|
|
|
|
{
|
|
|
|
return $result;
|
|
|
|
}
|
2011-12-07 18:18:08 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
// Send notification to user
|
|
|
|
if($data['warnings'] || $data['errors'])
|
|
|
|
{
|
|
|
|
$notify = new notifications();
|
|
|
|
$notify->set_sender($data['account_id']);
|
|
|
|
$notify->add_receiver($data['account_id']);
|
|
|
|
$notify->set_subject(lang('Schedule import | export'). ' ' . lang('errors'));
|
|
|
|
$contents = '';
|
2011-12-09 19:44:29 +01:00
|
|
|
|
2016-05-16 12:18:20 +02:00
|
|
|
if($data['warnings'])
|
2011-12-09 19:44:29 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$contents .= lang($data['type']) . ' ' . lang('Warnings') . ' ' . Api\DateTime::to() . ':';
|
|
|
|
foreach($data['warnings'] as $target => $message)
|
2011-12-09 19:44:29 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$contents .= "\n". (is_numeric($target) ? '' : $target."\n");
|
|
|
|
$contents .= is_array($message) ? implode("\n",$message) : $message;
|
2011-12-15 18:01:01 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
$contents .= "\n";
|
|
|
|
}
|
|
|
|
if($data['errors'])
|
|
|
|
{
|
|
|
|
$contents .= lang($data['type']) . ' ' . lang('Errors') . ' ' . Api\DateTime::to() . ':';
|
|
|
|
foreach($data['errors'] as $target => $errors)
|
2011-12-15 18:01:01 +01:00
|
|
|
{
|
2016-05-16 12:18:20 +02:00
|
|
|
$contents .= "\n". (is_numeric($target) ? '' : $target."\n");
|
|
|
|
$contents .= is_array($errors) ? implode("\n",$errors) : $errors;
|
2011-12-09 19:44:29 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
$contents .= "\n";
|
2011-12-09 19:44:29 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
$notify->set_message($contents);
|
|
|
|
$notify->send();
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
return $result;
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2016-05-16 12:18:20 +02:00
|
|
|
}
|