Skip to content
This repository has been archived by the owner on May 27, 2023. It is now read-only.

Commit

Permalink
WIP: import
Browse files Browse the repository at this point in the history
  • Loading branch information
Spiros Ioannou committed Jul 10, 2012
1 parent f37caab commit 8f13fad
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ function getmicrotime() {
$title="Settings";
break;

case "import":
$title="Import";
break;
case "edituser":
$stitle="User";
$title="Edit User";
Expand Down Expand Up @@ -318,6 +321,8 @@ function getmicrotime() {
<tr><td colspan=2><hr class='light1'></td></tr>

<tr><td colspan=2><a style="<?php echo $style_settings; ?>" class='ahdr' href="<?php echo $scriptname?>?action=settings" ><?php te("Settings");?></a></td></tr>

<tr><td colspan=2><a style="<?php echo $style_import; ?>" class='ahdr' href="<?php echo $scriptname?>?action=import" ><?php te("Import");?></a></td></tr>
<tr><td colspan=2><a style="<?php echo $style_translations; ?>" class='ahdr' href="<?php echo $scriptname?>?action=translations" ><?php te("Translations");?></a></td></tr>
<tr><td colspan=2><a style="<?php echo $style_showhist; ?>" class='ahdr' href="<?php echo $scriptname?>?action=showhist" >DB Log</a></td></tr>
</table>
Expand Down
102 changes: 102 additions & 0 deletions php/import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
if (!isset($initok)) {echo "do not run this script directly";exit;}


if (!isset($_POST['nextstep']))
$nextstep=0;
else
$nextstep=$_POST['nextstep'];

//0: show import form
//1: show imported file
//2: do import


if (strlen($_FILES['file']['name'])>2) { //insert file
$filefn=strtolower("import-".$_COOKIE["itdbuser"]."-".validfn($_FILES['file']['name']));
$uploadedfile = "/tmp/".$filefn;
$result = '';

//Move the file from the stored location to the new location
if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadedfile)) {
$result = "Cannot upload the file '".$_FILES['file']['name']."'";
if(!file_exists($uploaddir)) {
$result .= " : Folder doesn't exist.";
} elseif(!is_writable($uploaddir)) {
$result .= " : Folder not writable.";
} elseif(!is_writable($uploadedfile)) {
$result .= " : File not writable.";
}
$filefn = '';

echo "<br><b>ERROR: $result</b><br>";
}
else { //file ok
$nextstep=1;
print "<br>Uploaded $uploadedfile<br>";
}
}//insert file
?>


<?php if ($nextstep==0) { ?>
<table>
<form method=post name='importfrm' action='<?=$scriptname?>?action=<?=$action?>' enctype='multipart/form-data'>
<tr>
<tr><td>File:</td><td> <input name="file" id="file" size="25" type="file"></td></tr>
<tr><td>Delimeter:</td><td> <input size=1 type=text name='delim' value=';' maxlength=1></td></tr>
<tr><td>Skip 1st row:</td><td><select name=skip1st><option value=1>Yes</option><option value=0>No</option></select></td></tr>
<tr><td colspan=2><input type=submit value='Upload and inspect file'></td></tr>
</form>
<?php }?>

<?php if ($nextstep==1) {
$delim=$_POST['delim'];
$imlines=file($uploadedfile);
?>

<br><b> Please check imported file for consistency before submiting</b>:
<div style='height:400px;overflow:auto'>
<table class='brdr sortable'>
<thead>
</thead>
<th>ID</th><th>Room</th><th>Owner</th><th>Status</th><th>DNS Hostname</th><th>IPv4</th><th>OS</th><th>Manufacturer</th><th>Model</th><th>SN</th><th>SN2</th><th>Comments</th></tr>
<tbody>

<?
$nfields=12;
foreach ($imlines as $line_num => $line) {
if ($line_num==0 && $_POST['skip1st'])
continue;

$cols=explode($delim,$line);
if (count($cols) != $nfields) {
echo "Error: field count in line $line_num is ".count($cols).", $nfields expected";
exit;
}
echo "<tr>";
foreach ($cols as $col) {
$col=trim($col);
echo "<td>$col</td>";
}
echo "</tr>\n";
//echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
echo "</tbody></table>\n";
echo "</div>";
$nextstep=2;
?>
<form method=post name='importfrm' action='<?=$scriptname?>?action=<?=$action?>' enctype='multipart/form-data'>
<input type=hidden name='nextstep' value='2'>
<td colspan=2><input type=submit value='Import' ></td></tr>
</form>

<?
}

if ($nextstep==2) {
echo "<b>importing</b>";
}
?>


0 comments on commit 8f13fad

Please sign in to comment.