1<?php 2 3/** 4 * IP Addresses import form + upload 5 */ 6 7# include required scripts 8require_once( dirname(__FILE__) . '/../../../functions/functions.php' ); 9 10# initialize user object 11$Database = new Database_PDO; 12$User = new User ($Database); 13$Tools = new Tools ($Database); 14$Admin = new Admin ($Database); 15 16# verify that user is logged in 17$User->check_user_session(); 18 19$tpl_field_names = ""; 20$tpl_field_types = ""; 21 22# predefine field list 23$expfields = array ("section","ip_addr","hostname","description","vrf","subnet","mac","owner","device","note","tag","is_gateway", "port", "location"); 24//$disfields = array ("Section","IP Address","Hostname","Description","VRF","Subnet","MAC","owner","device","note","TAG"); 25$mtable = "ipaddresses"; # main table where to check the fields 26 27# extra fields 28$extfields["section"]["table"] = "sections"; 29$extfields["section"]["field"] = "name"; 30$extfields["section"]["pname"] = "section"; 31$extfields["vrf"]["table"] = "vrf"; 32$extfields["vrf"]["field"] = "name"; 33$extfields["vrf"]["pname"] = "vrf"; 34$extfields["subnet"]["table"] = "subnets"; 35$extfields["subnet"]["field"] = "subnet"; 36$extfields["subnet"]["pname"] = "subnet"; 37//$extfields["mask"]["table"] = "subnets"; 38//$extfields["mask"]["field"] = "mask"; 39//$extfields["mask"]["pname"] = "subnet"; 40$extfields["device"]["table"] = "devices"; 41$extfields["device"]["field"] = "hostname"; 42$extfields["device"]["pname"] = "device"; 43$extfields["tag"]["table"] = "ipTags"; 44$extfields["tag"]["field"] = "type"; 45$extfields["tag"]["pname"] = "tag"; 46 47## using the extra fields as a trick to display some nicer names for these regular fields 48$extfields["ip_addr"]["table"] = "ipaddresses"; 49$extfields["ip_addr"]["field"] = "ip_addr"; 50$extfields["ip_addr"]["pname"] = "ip address"; 51$extfields["hostname"]["table"] = "ipaddresses"; 52$extfields["hostname"]["field"] = "hostname"; 53$extfields["hostname"]["pname"] = "hostname"; 54$extfields["gateway"]["table"] = "ipaddresses"; 55$extfields["gateway"]["field"] = "is_gateway"; 56$extfields["gateway"]["pname"] = "gateway"; 57 58# required fields without which we will not continue 59$reqfields = array("section","ip_addr","subnet"); 60 61# manually adjust the standard fields 62foreach($expfields as $std_field) { 63 # extra table and field 64 if (isset($extfields[$std_field])) { 65 $cfield = $extfields[$std_field]["field"]; 66 $ctable = $extfields[$std_field]["table"]; 67 $pname = $extfields[$std_field]["pname"]." "; 68 } else { 69 # default table and field 70 $cfield = $std_field; 71 $ctable = $mtable; 72 $pname = ""; 73 } 74 75 # read field attributes 76 $field = $Tools->fetch_full_field_definition($ctable,$cfield); 77 $field = (array) $field; 78 79 # mark required fields with * 80 $msgr = in_array($std_field,$reqfields) ? "*" : ""; 81 82 #prebuild template table rows to avoid useless foreach loops 83 $tpl_field_names.= "<th>".($pname ? $pname : $field['Field']).$msgr."</th>"; 84 $tpl_field_types.= "<td><small>". wordwrap($field['Type'],18,"<br>\n",true) ."</small></td>"; 85} 86 87# append the custom fields, if any 88$custom_fields = $Tools->fetch_custom_fields($mtable); 89if(sizeof($custom_fields) > 0) { 90 foreach($custom_fields as $myField) { 91 # add field to required fields if needed 92 if ($myField['Null'] == "NO") { $reqfields[] = $myField['name']; } 93 # mark required fields with * 94 $msgr = in_array($myField['name'],$reqfields) ? "*" : ""; 95 96 $tpl_field_names.= "<th>".$myField['name'].$msgr."</th>"; 97 $tpl_field_types.= "<td><small>". wordwrap($myField['type'],18,"<br>\n",true) ."</small></td>"; 98 $expfields[] = $myField['name']; 99// $disfields[] = $myField['name']; 100 } 101} 102 103?> 104 105<!-- header --> 106<div class="pHeader"><?php print _("Select IP Addresses file and fields to import"); ?></div> 107 108<!-- content --> 109<div class="pContent"> 110 111<?php 112if (!is_writeable( dirname(__FILE__) . '/upload' )) $Tools->Result->show("danger", _("'app/admin/import-export/upload' folder is not writeable."), false, false); 113 114# print template form 115print "<form id='selectImportFields'><div id='topmsg'>"; 116print '<h4>'._("Template").'</h4><hr>'; 117print _("The import XLS/CSV should have the following fields and a <b>header row</b> for a succesful import:"); 118print "</div>"; 119print "<input name='expfields' type='hidden' value='".implode('|',$expfields)."' style='display:none;'>"; 120print "<input name='reqfields' type='hidden' value='".implode('|',$reqfields)."' style='display:none;'>"; 121print "<input name='filetype' id='filetype' type='hidden' value='' style='display:none;'>"; 122print "<table class='table table-striped table-condensed' id='fieldstable'><tbody>"; 123print "<tr>" . $tpl_field_names . "</tr>"; 124print "<tr>" . $tpl_field_types . "</tr>"; 125print "</tbody></table>"; 126print "<div id='bottommsg'>"._("The fields marked with * are mandatory.")." 127 <br>"._("Providing a subnet is optional, the system will add the IP to the longest match if no subnet is provided.")." 128 <br>"._("The mask can be provided either as a separate field or with the subnet, sparated by \"/\"")." 129 </div>"; 130print "<div class='checkbox'><label><input name='searchallvrfs' id='searchallvrfs' type='checkbox' unchecked>"._("Search for matching subnet in all VRFs.")."</label></div>"; 131#TODO# add option to hide php fields 132#print "<div class='checkbox'><label><input name='showspecific' id='showspecific' type='checkbox' unchecked>"._("Show PHPIPAM specific columns.")."</label></div>"; 133print "</form>"; 134 135$templatetype = 'ipaddr'; 136# print upload section 137print "<div id='uplmsg'>"; 138print '<h4>'._("Upload file").'</h4><hr>'; 139include 'import-button.php'; 140print "</div>"; 141?> 142</div> 143 144<!-- footer --> 145<div class="pFooter"> 146 <div class="btn-group"> 147 <button class="btn btn-sm btn-default hidePopups"><?php print _('Cancel'); ?></button> 148 <button class="btn btn-sm btn-default" id="dataImportPreview" data-type="ipaddr" disabled><i class="fa fa-eye"></i> <?php print _('Preview'); ?></button> 149 </div> 150</div> 151