2010-02-26 00:14:24 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* eGroupWare
|
|
|
|
*
|
|
|
|
* @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 $
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* userinterface for admins to schedule imports or exports using async services
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
class importexport_schedule_ui {
|
|
|
|
|
|
|
|
public $public_functions = array(
|
|
|
|
'index' => true,
|
|
|
|
'edit' => true,
|
|
|
|
);
|
|
|
|
|
|
|
|
protected static $template;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->template = new etemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index($content = array()) {
|
2011-12-12 18:23:24 +01:00
|
|
|
if(is_array($content['scheduled']))
|
2011-12-09 17:25:17 +01:00
|
|
|
{
|
2011-12-12 18:23:24 +01:00
|
|
|
foreach($content['scheduled'] as $row)
|
|
|
|
{
|
|
|
|
if($row['delete']) {
|
|
|
|
$key = key($row['delete']);
|
|
|
|
ExecMethod('phpgwapi.asyncservice.cancel_timer', $key);
|
|
|
|
}
|
2011-12-09 17:25:17 +01:00
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
|
|
|
$async_list = ExecMethod('phpgwapi.asyncservice.read', 'importexport%');
|
2010-03-22 16:09:26 +01:00
|
|
|
$data = array();
|
|
|
|
if(is_array($async_list)) {
|
|
|
|
foreach($async_list as $id => $async) {
|
2011-12-15 17:56:52 +01:00
|
|
|
foreach(array('errors', 'warnings', 'result') as $messages)
|
|
|
|
{
|
|
|
|
if(is_array($async['data'][$messages]))
|
|
|
|
{
|
|
|
|
$list = array();
|
|
|
|
foreach($async['data'][$messages] as $target => $message)
|
|
|
|
{
|
|
|
|
$list[] = array(
|
|
|
|
'target' => (is_numeric($target) ? '' : $target),
|
|
|
|
'message' => implode("\n", (array)$message)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$async['data'][$messages] = $list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
2010-03-22 16:09:26 +01:00
|
|
|
if(is_array($async['data']['errors'])) {
|
2011-12-07 21:05:02 +01:00
|
|
|
$processed_errors = array();
|
2011-12-06 01:44:07 +01:00
|
|
|
foreach($async['data']['errors'] as $target => $errors)
|
|
|
|
{
|
2011-12-07 21:05:02 +01:00
|
|
|
$processed_errors[] = array(
|
|
|
|
'target' => (is_numeric($target) ? '' : $target),
|
|
|
|
'error' => implode("\n", (array)$errors)
|
|
|
|
);
|
2011-12-06 01:44:07 +01:00
|
|
|
}
|
2011-12-07 21:05:02 +01:00
|
|
|
$async['data']['errors'] = $processed_errors;
|
|
|
|
}
|
|
|
|
$results = array();
|
2011-12-15 17:56:52 +01:00
|
|
|
foreach((array)$async['data']['warnings'] as $target => $message)
|
|
|
|
{
|
|
|
|
$warnings[] = array(
|
|
|
|
'target' => $target,
|
|
|
|
'result' => implode("\n",(array)$result)
|
|
|
|
);
|
|
|
|
}
|
2011-12-07 21:05:02 +01:00
|
|
|
foreach((array)$async['data']['result'] as $target => $result)
|
|
|
|
{
|
|
|
|
$results[] = array(
|
|
|
|
'target' => $target,
|
|
|
|
'result' => implode("\n",(array)$result)
|
|
|
|
);
|
|
|
|
}
|
2011-12-15 17:56:52 +01:00
|
|
|
*/
|
2011-12-07 21:05:02 +01:00
|
|
|
if($results)
|
|
|
|
{
|
|
|
|
$async['data']['result'] = $results;
|
2010-03-22 16:09:26 +01:00
|
|
|
}
|
|
|
|
if(is_numeric($async['data']['record_count'])) {
|
|
|
|
$async['data']['record_count'] = lang('%1 records processed', $async['data']['record_count']);
|
|
|
|
}
|
2011-12-07 18:18:08 +01:00
|
|
|
$data['scheduled'][] = array_merge($async['data'], array(
|
2010-03-22 16:09:26 +01:00
|
|
|
'id' => $id,
|
|
|
|
'next' => $async['next'],
|
|
|
|
'times' => str_replace("\n", '', print_r($async['times'], true)),
|
2011-12-07 18:18:08 +01:00
|
|
|
));
|
2010-03-02 23:33:47 +01:00
|
|
|
}
|
2010-03-22 16:09:26 +01:00
|
|
|
array_unshift($data['scheduled'], false);
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
|
|
|
$sel_options = self::get_select_options($data);
|
|
|
|
$this->template->read('importexport.schedule_index');
|
|
|
|
|
|
|
|
$GLOBALS['egw_info']['flags']['app_header'] = lang('Schedule import / export');
|
|
|
|
$this->template->exec('importexport.importexport_schedule_ui.index', $data, $sel_options, $readonlys, $preserve);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit($content = array()) {
|
|
|
|
$id = $_GET['id'] ? $_GET['id'] : $content['id'];
|
2011-06-02 18:50:41 +02:00
|
|
|
$definition_id = $_GET['definition'];
|
|
|
|
|
2010-02-26 00:14:24 +01:00
|
|
|
unset($content['id']);
|
|
|
|
|
|
|
|
$data = $content;
|
|
|
|
|
|
|
|
// Deal with incoming
|
|
|
|
if($content['save'] && self::check_target($content) === true) {
|
|
|
|
unset($content['save']);
|
|
|
|
ExecMethod('phpgwapi.asyncservice.cancel_timer', $id);
|
|
|
|
$id = self::generate_id($content);
|
|
|
|
$schedule = $content['schedule'];
|
2011-12-07 21:05:02 +01:00
|
|
|
// Async sometimes changes minutes to an array - keep what user typed
|
|
|
|
$content['min'] = $schedule['min'];
|
2010-02-26 00:14:24 +01:00
|
|
|
unset($content['schedule']);
|
|
|
|
|
2011-12-07 21:05:02 +01:00
|
|
|
// Remove any left blank
|
2010-02-26 00:14:24 +01:00
|
|
|
foreach($schedule as $key => &$value) {
|
2011-12-07 21:05:02 +01:00
|
|
|
if($value == '') unset($schedule[$key]);
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2011-04-11 17:45:29 +02:00
|
|
|
$result = ExecMethod2('phpgwapi.asyncservice.set_timer',
|
2010-02-26 00:14:24 +01:00
|
|
|
$schedule,
|
|
|
|
$id,
|
|
|
|
'importexport.importexport_schedule_ui.exec',
|
|
|
|
$content
|
|
|
|
);
|
|
|
|
if($result) {
|
|
|
|
$GLOBALS['egw']->js->set_onload('window.opener.location.reload(); self.close();');
|
|
|
|
} else {
|
|
|
|
$data['message'] = lang('Unable to schedule');
|
|
|
|
unset($id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($id) {
|
|
|
|
$preserve['id'] = $id;
|
|
|
|
$async = ExecMethod('phpgwapi.asyncservice.read', $id);
|
|
|
|
if(is_array($async[$id]['data'])) {
|
|
|
|
$data += $async[$id]['data'];
|
|
|
|
$data['schedule'] = $async[$id]['times'];
|
2011-12-13 21:00:40 +01:00
|
|
|
unset($data['times']);
|
2011-12-07 21:05:02 +01:00
|
|
|
|
|
|
|
// Async sometimes changes minutes to an array - show user what they typed
|
|
|
|
if(is_array($data['schedule']['min'])) $data['schedule']['min'] = $data['min'];
|
2010-02-26 00:14:24 +01:00
|
|
|
} else {
|
|
|
|
$data['message'] = lang('Schedule not found');
|
|
|
|
}
|
|
|
|
} else {
|
2010-10-19 19:29:11 +02:00
|
|
|
$data['type'] = $content['type'] ? $content['type'] : 'import';
|
2011-06-02 18:50:41 +02:00
|
|
|
|
|
|
|
if((int)$definition_id) {
|
|
|
|
$bo = new importexport_definitions_bo();
|
|
|
|
$definition = $bo->read($definition_id);
|
|
|
|
if($definition['definition_id']) {
|
|
|
|
$data['type'] = $definition['type'];
|
|
|
|
$data['appname'] = $definition['application'];
|
|
|
|
$data['plugin'] = $definition['plugin'];
|
|
|
|
$data['definition'] = $definition['name'];
|
|
|
|
}
|
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($data['target'] && $data['type']) {
|
|
|
|
$file_check = self::check_target($data);
|
|
|
|
if($file_check !== true) $data['message'] .= ($data['message'] ? "\n" . $file_check : $file_check);
|
|
|
|
}
|
|
|
|
|
2011-12-06 01:44:07 +01:00
|
|
|
$data['no_delete_files'] = $data['type'] != 'import';
|
2010-02-26 00:14:24 +01:00
|
|
|
$sel_options = self::get_select_options($data);
|
2010-09-14 23:17:29 +02:00
|
|
|
$GLOBALS['egw']->js->validate_file('.','importexport','importexport');
|
2010-02-26 00:14:24 +01: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, $readonlys, $preserve, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get options for select boxes
|
|
|
|
*/
|
|
|
|
public static function get_select_options(Array $data) {
|
|
|
|
$options = array(
|
|
|
|
'type' => array(
|
|
|
|
'import' => lang('import'),
|
|
|
|
'export' => lang('export')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2010-03-22 16:09:26 +01:00
|
|
|
(array)$apps = importexport_helper_functions::get_apps($data['type'] ? $data['type'] : 'all');
|
2010-02-26 00:14:24 +01:00
|
|
|
if(count($apps)) {
|
|
|
|
$options['appname'] = array('' => lang('Select one')) + array_combine($apps,$apps);
|
|
|
|
}
|
|
|
|
|
2011-12-06 01:44:07 +01:00
|
|
|
$plugins = importexport_helper_functions::get_plugins($data['appname'] ? $data['appname'] : 'all', $data['type']);
|
|
|
|
if(is_array($plugins)) {
|
|
|
|
foreach($plugins as $appname => $types) {
|
|
|
|
if(!is_array($types[$data['type']])) continue;
|
|
|
|
foreach($types[$data['type']] as $key => $title) {
|
2010-02-26 00:14:24 +01:00
|
|
|
$options['plugin'][$key] = $title;
|
|
|
|
}
|
2011-12-06 01:44:07 +01:00
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2011-04-11 17:45:29 +02:00
|
|
|
|
2010-02-26 00:14:24 +01:00
|
|
|
$options['definition'] = array();
|
|
|
|
|
|
|
|
if($data['file'] && !is_array($data['file'])) {
|
|
|
|
$extension = substr($data['file'], -3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the query isn't started with something, bodefinitions won't load the definitions
|
2011-03-14 21:55:42 +01:00
|
|
|
$query = array();
|
|
|
|
$query['type'] = $data['type'];
|
|
|
|
$query['application'] = $data['application'];
|
|
|
|
$query['plugin'] = $data['plugin'];
|
2010-03-22 16:09:26 +01:00
|
|
|
$definitions = new importexport_definitions_bo($query);
|
2010-02-26 00:14:24 +01:00
|
|
|
foreach ((array)$definitions->get_definitions() as $identifier) {
|
2011-02-14 16:54:28 +01:00
|
|
|
try {
|
2010-03-22 16:09:26 +01:00
|
|
|
$definition = new importexport_definition($identifier);
|
2011-02-14 16:54:28 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
// permission error
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($title = $definition->get_title()) {
|
|
|
|
$options['definition'][$title] = $title;
|
|
|
|
}
|
|
|
|
unset($definition);
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
|
|
|
unset($definitions);
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a async key
|
|
|
|
*/
|
|
|
|
public static function generate_id($data) {
|
|
|
|
|
|
|
|
$query = array(
|
|
|
|
'name' => $data['definition']
|
|
|
|
);
|
|
|
|
|
2010-03-22 16:09:26 +01:00
|
|
|
$definitions = new importexport_definitions_bo($query);
|
2010-02-26 00:14:24 +01:00
|
|
|
$definition_list = ((array)$definitions->get_definitions());
|
2011-04-11 17:45:29 +02:00
|
|
|
|
2010-03-02 23:33:47 +01:00
|
|
|
$id = 'importexport.'.$definition_list[0].'.'.$data['target'];
|
2010-02-26 00:14:24 +01:00
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get plugins via ajax
|
|
|
|
*/
|
|
|
|
public function ajax_get_plugins($type, $appname, &$response = null) {
|
|
|
|
if($response) {
|
|
|
|
$return = false;
|
|
|
|
} else {
|
|
|
|
$response = new xajaxResponse();
|
|
|
|
}
|
|
|
|
$options = self::get_select_options(array('type' => $type, 'appname'=>$appname));
|
2010-09-14 23:17:29 +02:00
|
|
|
$response->addScript("clear_options('exec[plugin]');");
|
2011-03-21 20:05:33 +01:00
|
|
|
$response->addScript("selectbox_add_option('exec[plugin]','".lang('Select...')."', '',false);");
|
2010-09-14 23:17:29 +02:00
|
|
|
if(is_array($options['plugin'])) {
|
|
|
|
foreach ($options['plugin'] as $value => $title) {
|
2010-02-26 00:14:24 +01:00
|
|
|
$response->addScript("selectbox_add_option('exec[plugin]','$title', '$value',false);");
|
|
|
|
}
|
|
|
|
}
|
2011-04-04 16:31:37 +02:00
|
|
|
if(count($options['plugin']) == 1) {
|
|
|
|
$response->assign('exec[plugin]','value',$value);
|
|
|
|
} else {
|
|
|
|
$response->addScript("xajax_doXMLHTTP('importexport.importexport_schedule_ui.ajax_get_definitions', '$appname', document.getElementById('exec[plugin]').value);");
|
|
|
|
}
|
2011-12-06 01:44:07 +01:00
|
|
|
|
2010-02-26 00:14:24 +01:00
|
|
|
return $response->getXML();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get definitions via ajax
|
|
|
|
*/
|
|
|
|
public function ajax_get_definitions($appname, $plugin) {
|
|
|
|
$options = self::get_select_options(array('appname'=>$appname, 'plugin'=>$plugin));
|
2010-09-14 23:17:29 +02:00
|
|
|
$response = new xajaxResponse();
|
|
|
|
$response->addScript("clear_options('exec[definition]');");
|
2011-03-21 20:05:33 +01:00
|
|
|
$response->addScript("selectbox_add_option('exec[definition]','".lang('Select...')."', '',false);");
|
2010-02-26 00:14:24 +01:00
|
|
|
if(is_array($options['definition'])) {
|
|
|
|
foreach ($options['definition'] as $value => $title) {
|
2010-09-14 23:17:29 +02:00
|
|
|
$response->addScript("selectbox_add_option('exec[definition]','$title', '$value',false);");
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
2011-04-04 16:31:37 +02:00
|
|
|
if(count($options['definition']) == 1) {
|
|
|
|
$response->assign('exec[definition]','value',$value);
|
|
|
|
} else {
|
|
|
|
$response->addScript("document.getElementById('exec[definition]').value = ''");
|
|
|
|
}
|
2010-02-26 00:14:24 +01:00
|
|
|
return $response->getXML();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that the target is valid for the type (readable or writable)
|
2010-10-19 19:29:11 +02:00
|
|
|
* and that they're not trying to write directly to the filesystem
|
2011-04-11 17:45:29 +02:00
|
|
|
*
|
2010-02-26 00:14:24 +01:00
|
|
|
* $data should contain target & type
|
|
|
|
*/
|
|
|
|
public static function check_target(Array $data) {
|
2010-10-19 19:29:11 +02:00
|
|
|
$scheme = parse_url($data['target'], PHP_URL_SCHEME);
|
|
|
|
if($scheme == '' || $scheme == 'file') {
|
|
|
|
return 'Direct file access not allowed';
|
|
|
|
}
|
|
|
|
if($scheme == vfs_stream_wrapper::SCHEME && !in_array(vfs_stream_wrapper::SCHEME, stream_get_wrappers())) {
|
|
|
|
stream_wrapper_register(vfs_stream_wrapper::SCHEME, 'vfs_stream_wrapper', STREAM_IS_URL);
|
|
|
|
}
|
|
|
|
|
2011-12-13 18:42:32 +01:00
|
|
|
if ($data['type'] == 'import' && ($scheme == egw_vfs::SCHEME && !egw_vfs::is_readable($data['target'])))
|
2010-02-26 00:14:24 +01:00
|
|
|
{
|
2011-03-11 17:03:08 +01:00
|
|
|
return lang('%1 is not readable',$data['target']);
|
2011-04-11 17:45:29 +02:00
|
|
|
}
|
2011-12-13 18:42:32 +01:00
|
|
|
elseif ($data['type'] == 'import' && in_array($scheme, array('http','https')))
|
|
|
|
{
|
|
|
|
// Not supported by is_readable, try headers...
|
|
|
|
$options = array();
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
if(!$headers[$i]) break;
|
|
|
|
if(strpos($headers[$i],'200') !== false) return true;
|
|
|
|
}
|
|
|
|
return lang('%1 is not readable',$data['target']);
|
|
|
|
}
|
2010-10-19 19:29:11 +02:00
|
|
|
elseif ($data['type'] == 'export' && !self::is__writable($data['target'])) {
|
2011-03-11 17:03:08 +01:00
|
|
|
return lang('%1 is not writable',$data['target']);
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-19 19:29:11 +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
|
|
|
|
*/
|
|
|
|
private static function is__writable($path) {
|
|
|
|
if ($path{strlen($path)-1}=='/') // recursively return a temporary file path
|
|
|
|
return is__writable($path.uniqid(mt_rand()).'.tmp');
|
|
|
|
else if (is_dir($path))
|
|
|
|
return is__writable($path.'/'.uniqid(mt_rand()).'.tmp');
|
|
|
|
// check tmp file for read/write capabilities
|
|
|
|
$rm = file_exists($path);
|
2011-12-09 19:44:29 +01:00
|
|
|
$f = @fopen($path, 'a');
|
2010-10-19 19:29:11 +02:00
|
|
|
|
|
|
|
if ($f===false)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fclose($f);
|
|
|
|
if (!$rm)
|
2011-12-09 19:44:29 +01:00
|
|
|
@unlink($path);
|
2010-10-19 19:29:11 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-26 00:14:24 +01:00
|
|
|
/**
|
|
|
|
* Execute a scheduled import or export
|
|
|
|
*/
|
|
|
|
public static function exec($data) {
|
|
|
|
ob_start();
|
|
|
|
|
2011-12-07 21:05:02 +01:00
|
|
|
|
|
|
|
$data['record_count'] = 0;
|
|
|
|
unset($data['errors']);
|
2011-12-15 17:56:52 +01:00
|
|
|
unset($data['warnings']);
|
2011-12-07 21:05:02 +01:00
|
|
|
unset($data['result']);
|
2011-12-07 18:18:08 +01:00
|
|
|
$data['last_run'] = time();
|
|
|
|
|
2010-02-26 00:14:24 +01:00
|
|
|
// check file
|
|
|
|
$file_check = self::check_target($data);
|
|
|
|
if($file_check !== true) {
|
2011-12-07 21:05:02 +01:00
|
|
|
$data['errors'] = array($file_check=>'');
|
2011-12-07 18:18:08 +01:00
|
|
|
// Update job with results
|
|
|
|
self::update_job($data);
|
|
|
|
|
2010-02-26 00:14:24 +01:00
|
|
|
fwrite(STDERR,'importexport_schedule: ' . date('c') . ": $file_check \n");
|
2011-12-07 21:05:02 +01:00
|
|
|
return;
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 16:09:26 +01:00
|
|
|
$definition = new importexport_definition($data['definition']);
|
2010-02-26 00:14:24 +01:00
|
|
|
if( $definition->get_identifier() < 1 ) {
|
2011-12-07 21:05:02 +01:00
|
|
|
$data['errors'] = array('Definition not found!');
|
2011-12-07 18:18:08 +01:00
|
|
|
// Update job with results
|
|
|
|
self::update_job($data);
|
|
|
|
|
2010-02-26 00:14:24 +01:00
|
|
|
fwrite(STDERR,'importexport_schedule: ' . date('c') . ": Definition not found! \n");
|
2011-12-07 21:05:02 +01:00
|
|
|
return;
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
|
|
|
$GLOBALS['egw_info']['flags']['currentapp'] = $definition->application;
|
|
|
|
|
|
|
|
$po = new $definition->plugin;
|
|
|
|
|
|
|
|
$type = $data['type'];
|
|
|
|
|
2011-12-06 01:44:07 +01:00
|
|
|
if(is_dir($data['target']))
|
|
|
|
{
|
2011-12-07 21:05:02 +01:00
|
|
|
$dir = opendir($data['target']);
|
|
|
|
$contents = array();
|
|
|
|
while(false !== ($item = readdir($dir))) {
|
|
|
|
$contents[] = $item;
|
|
|
|
}
|
|
|
|
closedir($dir);
|
2011-12-06 01:44:07 +01:00
|
|
|
$targets = array_diff($contents, array('.','..'));
|
2011-12-12 18:23:24 +01:00
|
|
|
$files = array();
|
2011-12-06 01:44:07 +01:00
|
|
|
foreach($targets as $key => &$target)
|
|
|
|
{
|
2011-12-12 18:23:24 +01:00
|
|
|
$target = $data['target'].(substr($data['target'],-1) == '/' ? '' : '/').$target;
|
2011-12-06 01:44:07 +01:00
|
|
|
|
|
|
|
// Check modification time, make sure it's not currently being written
|
|
|
|
// Skip files modified in the last 10 seconds
|
2011-12-07 21:05:02 +01:00
|
|
|
$mod_time = filemtime($target);
|
|
|
|
if($mod_time >= time() - 10)
|
2011-12-06 01:44:07 +01:00
|
|
|
{
|
|
|
|
$data['result'][$target] = lang('Skipped');
|
2011-12-09 20:11:14 +01:00
|
|
|
unset($targets[$key]);
|
2011-12-07 21:05:02 +01:00
|
|
|
continue;
|
2011-12-06 01:44:07 +01:00
|
|
|
}
|
2011-12-07 21:05:02 +01:00
|
|
|
$files[$mod_time] = $target;
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2011-12-12 18:23:24 +01:00
|
|
|
if($files)
|
|
|
|
{
|
|
|
|
ksort($files);
|
|
|
|
}
|
2011-12-07 21:05:02 +01:00
|
|
|
$targets = $files;
|
2011-12-06 01:44:07 +01:00
|
|
|
unset($target); // Unset it, or it will be overwritten in loop below
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$targets = array($data['target']);
|
2010-03-02 23:33:47 +01:00
|
|
|
}
|
|
|
|
|
2011-12-06 01:44:07 +01:00
|
|
|
foreach($targets as $target)
|
|
|
|
{
|
2011-12-13 18:42:32 +01:00
|
|
|
if($resource = @fopen( $target, $data['type'] == 'import' ? 'rb' : 'wb' )) {
|
2011-12-06 01:44:07 +01:00
|
|
|
$result = $po->$type( $resource, $definition );
|
|
|
|
|
|
|
|
fclose($resource);
|
|
|
|
} else {
|
2011-12-12 18:23:24 +01:00
|
|
|
fwrite(STDERR,'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
|
|
|
}
|
2011-12-06 01:44:07 +01:00
|
|
|
|
|
|
|
|
2011-12-15 17:56:52 +01:00
|
|
|
if(method_exists($po, 'get_warnings') && $po->get_warnings()) {
|
|
|
|
fwrite(STDERR, 'importexport_schedule: ' . date('c') . ": Import warnings:\n#\tWarning\n");
|
|
|
|
foreach($po->get_warnings() as $record => $msg) {
|
|
|
|
$data['warnings'][$target][] = "#$record: $msg";
|
|
|
|
fwrite(STDERR, "$record\t$msg\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($data['warnings'][$target]);
|
|
|
|
}
|
2011-12-06 01:44:07 +01:00
|
|
|
if(method_exists($po, 'get_errors') && $po->get_errors()) {
|
|
|
|
fwrite(STDERR, 'importexport_schedule: ' . date('c') . ": Import errors:\n#\tError\n");
|
|
|
|
foreach($po->get_errors() as $record => $error) {
|
2011-12-07 21:05:02 +01:00
|
|
|
$data['errors'][$target][] = "#$record: $error";
|
2011-12-06 01:44:07 +01:00
|
|
|
fwrite(STDERR, "$record\t$error\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($data['errors'][$target]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($po instanceof importexport_iface_import_plugin) {
|
|
|
|
if(is_numeric($result)) {
|
|
|
|
$data['record_count'] += $result;
|
2011-12-07 21:05:02 +01:00
|
|
|
$data['result'][$target][] = lang('%1 records processed', $result);
|
2011-12-06 01:44:07 +01:00
|
|
|
}
|
2011-12-07 21:05:02 +01:00
|
|
|
$data['result'][$target] = array();
|
2011-12-06 01:44:07 +01:00
|
|
|
foreach($po->get_results() as $action => $count) {
|
2011-12-07 21:05:02 +01:00
|
|
|
$data['result'][$target][] = lang($action) . ": $count";
|
2011-12-06 01:44:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data['result'][$target] = $result;
|
2010-03-03 17:28:45 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-02 23:33:47 +01:00
|
|
|
|
2011-12-06 01:44:07 +01:00
|
|
|
// Delete file?
|
2011-12-07 18:18:08 +01:00
|
|
|
if($data['delete_files'] && $type == 'import' && !$data['errors'])
|
2011-12-06 01:44:07 +01:00
|
|
|
{
|
|
|
|
foreach($targets as $target)
|
|
|
|
{
|
|
|
|
if(unlink($target))
|
|
|
|
{
|
2011-12-12 18:23:24 +01:00
|
|
|
$data['result'][$target][] .= "\n..." . lang('deleted');
|
2011-12-06 01:44:07 +01:00
|
|
|
}
|
2011-12-07 18:18:08 +01:00
|
|
|
else
|
|
|
|
{
|
2011-12-12 18:23:24 +01:00
|
|
|
$data['errors'][$target][] .= "\n..." . lang('Unable to delete');
|
2011-12-07 18:18:08 +01:00
|
|
|
}
|
2011-12-06 01:44:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-13 21:00:40 +01:00
|
|
|
// Run time in minutes
|
|
|
|
$data['run_time'] = (time() - $data['last_run']) / 60;
|
|
|
|
|
2010-03-02 23:33:47 +01:00
|
|
|
// Update job with results
|
2011-12-07 18:18:08 +01:00
|
|
|
self::update_job($data);
|
|
|
|
|
|
|
|
$contents = ob_get_contents();
|
2011-12-09 19:44:29 +01:00
|
|
|
|
|
|
|
// Log to cron log
|
|
|
|
if($contents)
|
|
|
|
{
|
2011-12-07 18:18:08 +01:00
|
|
|
fwrite(STDOUT,'importexport_schedule: ' . date('c') . ": \n".$contents);
|
|
|
|
}
|
2011-12-09 19:44:29 +01:00
|
|
|
|
2011-12-07 18:18:08 +01:00
|
|
|
ob_end_clean();
|
|
|
|
}
|
|
|
|
|
2011-12-12 18:23:24 +01:00
|
|
|
/**
|
|
|
|
* Update the async job with current status, and send a notification
|
|
|
|
* to user if there were any errors.
|
|
|
|
*/
|
2011-12-07 18:18:08 +01:00
|
|
|
private static function update_job($data) {
|
2010-03-02 23:33:47 +01:00
|
|
|
$id = self::generate_id($data);
|
|
|
|
$async = ExecMethod('phpgwapi.asyncservice.read', $id);
|
|
|
|
$async = $async[$id];
|
|
|
|
if(is_array($async)) {
|
|
|
|
ExecMethod('phpgwapi.asyncservice.cancel_timer', $id);
|
2011-04-11 17:45:29 +02:00
|
|
|
$result = ExecMethod2('phpgwapi.asyncservice.set_timer',
|
2010-03-02 23:33:47 +01:00
|
|
|
$async['times'],
|
|
|
|
$id,
|
|
|
|
'importexport.importexport_schedule_ui.exec',
|
|
|
|
$data
|
|
|
|
);
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
2011-12-09 19:44:29 +01:00
|
|
|
|
|
|
|
// Send notification to user
|
|
|
|
if($data['errors'])
|
|
|
|
{
|
|
|
|
$notify = new notifications();
|
|
|
|
$notify->set_sender($data['account_id']);
|
|
|
|
$notify->add_receiver($data['account_id']);
|
2011-12-12 18:24:27 +01:00
|
|
|
$notify->set_subject(lang('Schedule import | export'). ' ' . lang('errors'));
|
2011-12-09 19:44:29 +01:00
|
|
|
|
|
|
|
$contents = lang($data['type']) . ' ' . lang('Errors') . ' ' . egw_time::to() . ':';
|
|
|
|
foreach($data['errors'] as $target => $errors)
|
|
|
|
{
|
|
|
|
$contents .= "\n". (is_numeric($target) ? '' : $target."\n");
|
2011-12-12 18:23:24 +01:00
|
|
|
$contents .= is_array($errors) ? implode("\n",$errors) : $errors;
|
2011-12-09 19:44:29 +01:00
|
|
|
}
|
|
|
|
$notify->set_message($contents);
|
|
|
|
$notify->send();
|
|
|
|
}
|
2011-12-07 18:18:08 +01:00
|
|
|
return $result;
|
2010-02-26 00:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|