/usr/share/fusiondirectory/plugins/addons/argonaut/class_argonautImportFile.inc is in fusiondirectory-plugin-argonaut 1.0.19-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | <?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2011-2016 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class argonautImportFile extends simplePlugin
{
protected $csv_fields = array(
'0' => 'TIMESTAMP',
'1' => 'MAC',
'2' => 'HEADER',
'3' => 'OGROUP'
);
static function plInfo()
{
return array(
'plShortName' => _('Argonaut task import'),
'plDescription' => _('Imports argonaut tasks from CSV file'),
'plObjectType' => array('argonautQueueImport' => array(
'name' => _('Argonaut task import'),
'aclCategory' => 'argonautQueue',
'icon' => 'geticon.php?context=applications&icon=argonaut&size=16',
)),
'plProvidedAcls' => parent::generatePlProvidedAcls(static::getAttributesInfo())
);
}
static function getAttributesInfo ()
{
return array(
'main' => array(
'name' => _('Import actions from CSV file'),
'class' => array('fullwidth'),
'attrs' => array(
new CompositeAttribute (
_('Import a list of task into argonaut'),
'import',
array(
new FileAttribute (
'', '',
'import_file', FALSE
),
new ButtonAttribute (
'', '',
'import_submit',
_('Upload')
)
),
'', '%s%s', '',
_('Import file')
)
)
),
'events' => array(
'name' => _('Imported tasks'),
'attrs' => array(new FakeAttribute('events')),
'template' => get_template_path('import_events.tpl', TRUE, dirname(__FILE__)),
),
);
}
public function __construct($parent)
{
parent::__construct($config->current['BASE'], NULL, $parent);
$this->attributesAccess['import']->setInLdap(FALSE);
$this->attributesAccess['import']->setLinearRendering(TRUE);
$this->events = array();
$this->daemon_events = argonautEventTypes::get_event_types();
}
function execute()
{
$smarty = get_smarty();
$smarty->assign('jobtypes', join(',',array_keys($this->daemon_events)));
return parent::execute().
'<p class="plugbottom">'.
' <input type="submit" name="start_import" value="'._('Import').'" >'.
' <input type="submit" name="import_abort" value="'.msgPool::backButton().'">'.
'</p>';
}
function handle_import_submit()
{
$this->parse_csv($this->import);
}
function save_object()
{
/* Import started */
if (isset($_POST['start_import'])) {
$events = $this->events;
$error = FALSE;
foreach ($events as $event) {
if (!empty($event['ERROR'])) {
$error = TRUE;
break;
}
}
if ($error) {
msg_dialog::display(
_('Import'),
_('Fix the errors in your CSV file first'),
ERROR_DIALOG
);
} else {
$success = 0;
$fail = 0;
foreach ($events as $key => $event) {
/* Create event */
if (empty($event['TIMESTAMP'])) {
$data = array();
} else {
$data = array('timestamp' => $event['TIMESTAMP']);
}
$this->parent->o_queue->append_call($event['HEADER'], array($event['MAC']), $data);
if ($this->parent->o_queue->is_error()) {
msg_dialog::display(_('Infrastructure service'), msgPool::siError($this->parent->o_queue->get_error()), ERROR_DIALOG);
$fail++;
} else {
unset($events[$key]);
$success++;
}
}
msg_dialog::display(_('Import'), sprintf(_('Import complete: %s events successfully send, %s failed'), $success, $fail), INFO_DIALOG);
$this->events = $events;
}
}
parent::save_object();
}
private function parse_csv($str)
{
/* Some file checks */
$lines = preg_split('/\n/', $str);
if (empty($str) || !count($lines)) {
msg_dialog::display(_('Import'), msgPool::incorrectUpload(_('file is empty')), ERROR_DIALOG);
return;
}
/* Reset current events */
$events = array();
/* Parse each line of the given file */
foreach ($lines as $line) {
/* Skip empty lines */
if (empty($line)) {
continue;
}
/* Load values from file */
$fields = explode(';', $line);
$event = array();
foreach ($this->csv_fields as $key => $val) {
$event[$val] = '';
if (isset($fields[$key])) {
$event[$val] = $fields[$key];
}
}
$event['ERROR'] = '';
if (empty($event['MAC']) || !tests::is_mac($event['MAC'])) {
$event['ERROR'] .= msgPool::invalid(_('MAC')).', ';
}
if (empty($event['HEADER']) || !isset($this->daemon_events[$event['HEADER']])) {
$event['ERROR'] .= msgPool::invalid(_('Event')).', ';
}
$event['ERROR'] = trim($event['ERROR'], ', ');
$events[] = $event;
}
$this->attributesAccess['events']->setPostValue($events);
}
}
?>
|