1<?php 2 3/* 4 * Data import load 5 *************************************************/ 6 7# include required scripts 8require_once( dirname(__FILE__) . '/../../../functions/functions.php' ); 9 10# initialize user object, if not already set 11if (!isset($Database)) { $Database = new Database_PDO; } 12if (!isset($User)) { $User = new User ($Database); } 13if (!isset($Admin)) { $Admin = new Admin ($Database); } 14if (!isset($Tools)) { $Tools = new Tools ($Database); } 15 16# verify that user is logged in, to guard against direct access of page and possible exploits 17$User->check_user_session(); 18 19# fetch all l2 domains 20$vlan_domains = $Admin->fetch_all_objects("vlanDomains", "id"); 21 22# read again the custom fields, if any 23if (!isset($custom_fields)) { $custom_fields = $Tools->fetch_custom_fields("vlans"); } 24 25# Load existing data 26$edata = array(); $vdom = array(); $vdomid = array(); 27# process for easier later check 28foreach ($vlan_domains as $vlan_domain) { 29 //cast 30 $vlan_domain = (array) $vlan_domain; 31 $vdom[] = $vlan_domain['name']; 32 $vdomid[$vlan_domain['name']] = $vlan_domain['id']; 33 // read vlans 34 $all_vlans = $Admin->fetch_multiple_objects("vlans", "domainId", $vlan_domain['id'], "number"); 35 $all_vlans = (array) $all_vlans; 36 // skip empty domains 37 if (sizeof($all_vlans)==0) { continue; } 38 //write all VLAN entries 39 foreach ($all_vlans as $vlan) { 40 //cast 41 $vlan = (array) $vlan; 42 $edata[$vlan_domain['name']][$vlan['number']] = $vlan; 43 } 44} 45 46$rows = ""; 47$counters = array(); 48$unique = array(); 49 50# check the fields 51foreach ($data as &$cdata) { 52 $msg = ""; $action = ""; $cfieldtds = ""; 53 54 # set a default domain if none specified 55 if ($cdata['domain'] != "") { $cdom = $cdata['domain']; } else { $cdom = "default"; } 56 57 # check if domain exists and link ID, otherwise issue error 58 if (!in_array($cdom,$vdom)) { 59 $msg.= "Missing VLAN domain. Please add/import VLAN domain first."; $action = "error"; 60 } else { 61 $cdata['domainId'] = $vdomid[$cdom]; 62 } 63 64 # check if required fields are present and not empty 65 foreach($reqfields as $creq) { 66 if ((!isset($cdata[$creq])) or ($cdata[$creq] == "")) { $msg.= "Required field ".$creq." missing or empty."; $action = "error"; } 67 } 68 69 # check data format 70 if ($action != "error") { 71 if (!preg_match("/^[a-zA-Z0-9-_]+$/", $cdata['name'])) { $msg.="Invalid name format."; $action = "error"; } 72 if (!preg_match("/^[0-9]+$/", $cdata['number'])) { $msg.="Invalid number format."; $action = "error"; } 73 if (preg_match("/[;'\"]/", $cdata['description'])) { $msg.="Invalid characters in description."; $action = "error"; } 74 if (!preg_match("/^[a-zA-Z0-9-_. ]+$/", $cdom)) { $msg.="Invalid domain format."; $action = "error"; } 75 if ($action != "error") { if ($cdata['number']>$User->settings->vlanMax) { $msg.= _('Highest possible VLAN number is ').$User->settings->vlanMax.'!'; $action = "error"; } } 76 } 77 78 # Generate the custom fields columns 79 if(sizeof($custom_fields) > 0) { foreach($custom_fields as $myField) { $cfieldtds.= "<td>".$cdata[$myField['name']]."</td>"; } } 80 81 # check if duplicate VLAN 82 if (isset($unique[$cdom][$cdata['number']])) { $msg.= "Duplicate VLAN domain and number not supported. Please check import file."; $action = "error"; } 83 84 # check if existing 85 if ($action != "error") { 86 if (isset($edata[$cdom][$cdata['number']])) { 87 $cdata['vlanId'] = $edata[$cdom][$cdata['number']]['vlanId']; 88 $action = "skip"; # skip duplicate fields if identical, update if different 89 if ($cdata['name'] != $edata[$cdom][$cdata['number']]['name']) { $msg.= "VLAN name will be updated."; $action = "edit"; } 90 if ($cdata['description'] != $edata[$cdom][$cdata['number']]['description']) { $msg.= "VLAN description will be updated."; $action = "edit"; } 91 # Check if the values of the custom fields have changed 92 if(sizeof($custom_fields) > 0) { 93 foreach($custom_fields as $myField) { 94 if ($cdata[$myField['name']] != $edata[$cdom][$cdata['number']][$myField['name']]) { 95 $msg.= "VLAN ".$myField['name']." will be updated."; $action = "edit"; 96 } 97 } 98 } 99 100 if ($action == "skip") { 101 $msg.= "Duplicate, will skip."; 102 } 103 } else { 104 $msg.="New entry, will be added."; $action = "add"; 105 } 106 } 107 108 $cdata['msg'].= $msg; 109 $cdata['action'] = $action; 110 $counters[$action]++; 111 if (!isset($unique[$cdom][$cdata['number']])) { $unique[$cdom][$cdata['number']] = $cdata['name']; } 112 113 $rows.="<tr class='".$colors[$action]."'><td><i class='fa ".$icons[$action]."' rel='tooltip' data-placement='bottom' title='"._($msg)."'></i></td> 114 <td>".$cdata['name']."</td> 115 <td>".$cdata['number']."</td> 116 <td>".$cdata['description']."</td> 117 <td>".$cdata['domain']."</td> 118 ".$cfieldtds." 119 <td>"._($cdata['msg'])."</td></tr>"; 120} 121 122?> 123 124