forked from smeighan/nutcracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.php
More file actions
122 lines (116 loc) · 3.61 KB
/
import.php
File metadata and controls
122 lines (116 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
function importXML($inFile,$my_username)
{
//$newLineStr = "\n";
$newLineStr = "<br />";
$inFile = BASE_PATH."\\xml\\".$inFile;
echo "opening file " . $inFile . $newLineStr;
$xml = simplexml_load_file($inFile);
$filename = $xml->getName();
// echo $xml->getName() . $newLineStr;
foreach($xml->children() as $tablename)
{
$cntRow = 0;
$tableStr = $tablename->getName();
echo "importing table " . $tableStr . $newLineStr;
foreach($tablename->children() as $child)
{
// echo $child->getName() . ": " . $child . $newLineStr;
if (substr($child->getName(),0,3) == "row")
{
$cntRow += 1;
$intoStr = "";
$valStr = "";
$sepstr = "";
foreach($child->children() as $grandchild)
{
if ($grandchild->getName() == "username")
{
$intoStr .= $sepstr."username";
$valStr .=$sepstr.'"'.$my_username.'"';
} elseif ($grandchild->getName() == "date_created")
{
$intoStr .= $sepstr."date_created";
$valStr .= $sepstr."NOW()";
} else {
$intoStr .= $sepstr.$grandchild->getName();
$valStr .= $sepstr.'"'.$grandchild.'"';
}
$sepstr = ',';
// echo $grandchild->getName() . ": " . $grandchild . $newLineStr;
}
$sqlStr = "REPLACE INTO " . $tableStr . " (".$intoStr.") VALUES (".$valStr.");";
//echo $sqlStr .$newLineStr; // for webpage debug
if (strlen($sqlStr) > 0)
{
doSQL($sqlStr);
}
// Do the SQL statement here
}
}
echo "Processed ". $cntRow ." rows of data".$newLineStr;
}
?>
<br />
<a href="/nutcracker/login/member-index.php">Return to the main page</a>
<?php
}
function doSQL($sqlStr)
{
require_once('./conf/config.php');
$DB_link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Could not connect to host.");
mysql_select_db(DB_DATABASE, $DB_link) or die ("Could not find or access the database.");
mysql_query($sqlStr, $DB_link) or die ("Your SQL query didn't work... ");
// echo $sqlStr."<br />"; // for testing
}
//array of files without directories... optionally filtered by extension
function file_list($d,$x){
foreach(array_diff(scandir($d),array('.','..')) as $f)
if (is_file($d.'/'.$f)&&(substr($f,(-1*strlen($x))) == $x))
$l[]=$f;
return $l;
}
function displayFormHeader()
{
echo '<form method="post" action="import.php" name="import">';
echo '<div class="formRow">';
echo ' <div class="label">';
echo ' <label for="import">Select a file to import into your local database</label>';
echo ' </div>';
echo ' <div class="dropDown">';
}
function displayFormFooter()
{
echo '</div>';
echo '</div>';
echo '</form>';
}
function fileSelector($d, $username) {
displayFormHeader();
echo '<input type="hidden" name="username" value="'.$username.'">';
echo '<select name="importFile">';
foreach ($d as $filename) {
echo '<option value="'.$filename.'">'.$filename.'</option>';
}
echo '</select>';
echo '<input type="submit" name="submit" value="Submit">';
echo '</div>';
echo '</div>';
echo '</form>';
echo '<form method="post" action="login/member-index.php">';
echo '<input type="submit" name="cancel" value="Cancel">';
displayFormFooter();
}
// MAIN FUNCTION HERE
define('BASE_PATH',realpath('.'));
$username = $_POST['username'];
if (isset($_POST['submit'])) {
$filename = $_POST['importFile'];
importXML($filename, $username);
} else {
$dirname = 'xml/';
// $username = 'kgustafson'; // For testing
$dirlist = file_list($dirname,'.xml');
fileSelector($dirlist, $username);
}
?>