1 <?php
3 class goto_import_file extends plugin
4 {
6 var $events = array();
7 var $csv_fields = array();
9 public function __construct($config,&$parent)
10 {
11 plugin::plugin($config,NULL);
12 $this->parent = $parent;
13 $this->daemon_events = DaemonEvent::get_event_types( SYSTEM_EVENT | HIDDEN_EVENT);
15 $this->csv_fields = array(
16 "0"=>"TIMESTAMP","1" => "MAC", "2" => "HEADER", "3" => "OGROUP",
17 "4" => "BASE", "5" => "FQDN", "6" => "IP", "7" => "DHCP");
18 }
21 private function parse_csv($str)
22 {
24 /* Some file checks
25 */
26 $lines = split("\n",$str);
27 if(empty($str) || !count($lines)){
28 msg_dialog::display(_("Import"), msgPool::incorrectUpload(_("file is empty")),ERROR_DIALOG);
29 return;
30 }
32 /* Reset current events
33 */
34 $this->events = array();
36 /* Parse each line of the given file
37 */
38 foreach($lines as $line){
40 // Skip empty lines.
41 if(empty($line)) continue;
43 /* Load values from file
44 */
45 $fields = split(";",$line);
46 $event = array();
47 foreach($this->csv_fields as $key => $val) {
48 $event[$val] = "";
49 if(isset($fields[$key])){
50 $event[$val] = $fields[$key];
51 }
52 }
53 $this->events[] = $event;
54 }
55 $this->check_fields();
56 }
59 public function execute()
60 {
61 /* Import file
62 */
63 if(isset($_POST['import']) && isset($_FILES['file'])) {
64 if(isset($_FILES['file']['tmp_name'])){
65 $str = file_get_contents($_FILES['file']['tmp_name']);
66 $this->parse_csv($str);
67 }
68 }
70 /* Import started
71 */
72 $confirmed = FALSE;
73 foreach($_POST as $name => $value){
74 if(preg_match("/^MSG_OK/",$name)){
75 $confirmed = TRUE;
76 }
77 }
78 if(isset($_POST['start_import']) || $confirmed){
79 $error = FALSE;
80 if(!$confirmed){
81 foreach($this->events as $event){
82 if(!empty($event['ERROR'])){
83 $error = TRUE;
84 break;
85 }
86 }
87 if($error){
88 msg_dialog::display(_("Import"),
89 _("Selected entries will be skipped because of errors. Do you want to proceed?"),CONFIRM_DIALOG);
90 }
91 }
92 if(!$error){
94 $success = 0;
95 $fail = 0;
97 foreach($this->events as $key => $event){
98 if(!empty($event['ERROR'])){
99 $fail ++;
100 continue;
101 }
103 /* Create event
104 */
105 $class= $this->daemon_events['QUEUED'][$event['HEADER']];
106 $o_data = $this->daemon_events['BY_CLASS'][$class];
107 $object = new $class($this->config);
108 $object->add_targets(array($event['MAC']));
109 if($o_data['s_Schedule_Action'] == $event['HEADER']){
110 $object->set_type(SCHEDULED_EVENT);
111 }else{
112 $object->set_type(TRIGGERED_EVENT);
113 }
115 /* Update values like fqdn a.s.o
116 */
117 foreach($this->csv_fields as $name){
118 if($name == "TIMESTAMP" && empty($event[$name])) continue;
119 $object->set_value($name,$event[$name]);
120 }
122 if(!$this->parent->o_queue->append($object)){
123 msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->parent->o_queue->get_error()),ERROR_DIALOG);
124 $fail ++;
125 }else{
126 unset($this->events[$key]);
127 $success ++;
128 }
129 }
130 msg_dialog::display(_("Import"),sprintf(_("Import complete: %s events successfully send, %s failed"),$success,$fail),INFO_DIALOG);
131 }
132 }
135 /* Prepare output
136 */
137 $evts = $this->events;
138 foreach($this->events as $id => $evt){
139 foreach($evt as $key => $val){
140 if(in_array($key,$this->csv_fields)){
141 $evts[$id][$key] = "<span style=\"white-space: nowrap;\">".strip_tags($val)."</span>";
142 }
143 }
144 }
146 $smarty = get_smarty();
147 $smarty->assign("info",$evts);
148 $smarty->assign("count",count($evts));
149 return($smarty->fetch(get_template_path('goto_import_file.tpl', TRUE)));
150 }
153 public function check()
154 {
155 $message = plugin::check();
156 $this->check_fields();
157 return($message);
158 }
161 private function check_fields()
162 {
163 foreach($this->events as $key => $event){
164 $this->events[$key]['ERROR'] = "";
165 if(empty($event['MAC']) || !tests::is_mac($event['MAC'])){
166 $this->events[$key]['ERROR'] .= msgPool::invalid(_("MAC")).", ";
167 }
168 if(empty($event['HEADER']) || !isset($this->daemon_events['QUEUED'][$event['HEADER']])){
169 $this->events[$key]['ERROR'] .= msgPool::invalid(_("Event")).", ";
170 }
171 $this->events[$key]['ERROR'] = trim($this->events[$key]['ERROR'],", ");
172 }
173 }
174 }
176 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
177 ?>