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