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);
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 }
102 $class= $this->daemon_events['QUEUED'][$event['HEADER']];
103 $object = new $class($this->config);
104 $object->set_type(TRIGGERED_EVENT);
105 $object->add_targets(array($event['MAC']));
107 /* Update values like fqdn a.s.o
108 */
109 foreach($this->csv_fields as $name){
110 if($name == "TIMESTAMP" && empty($event[$name])) continue;
111 $object->set_value($name,$event[$name]);
112 }
114 if(!$this->parent->o_queue->append($object)){
115 msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->parent->o_queue->get_error()),ERROR_DIALOG);
116 $fail ++;
117 }else{
118 unset($this->events[$key]);
119 $success ++;
120 }
121 }
122 msg_dialog::display(_("Import"),sprintf(_("Import complete: %s events successfully send, %s failed"),$success,$fail),INFO_DIALOG);
123 }
124 }
127 /* Prepare output
128 */
129 $evts = $this->events;
130 foreach($this->events as $id => $evt){
131 foreach($evt as $key => $val){
132 if(in_array($key,$this->csv_fields)){
133 $evts[$id][$key] = "<span style=\"white-space: nowrap;\">".strip_tags($val)."</span>";
134 }
135 }
136 }
138 $smarty = get_smarty();
139 $smarty->assign("info",$evts);
140 $smarty->assign("count",count($evts));
141 return($smarty->fetch(get_template_path('goto_import_file.tpl', TRUE)));
142 }
145 public function check()
146 {
147 $message = plugin::check();
148 $this->check_fields();
149 return($message);
150 }
153 private function check_fields()
154 {
155 foreach($this->events as $key => $event){
156 $this->events[$key]['ERROR'] = "";
157 if(empty($event['MAC']) || !tests::is_mac($event['MAC'])){
158 $this->events[$key]['ERROR'] .= msgPool::invalid(_("MAC")).", ";
159 }
160 if(empty($event['HEADER']) || !isset($this->daemon_events['QUEUED'][$event['HEADER']])){
161 $this->events[$key]['ERROR'] .= msgPool::invalid(_("Event")).", ";
162 }
163 $this->events[$key]['ERROR'] = trim($this->events[$key]['ERROR'],", ");
164 }
165 }
166 }
168 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
169 ?>