2010-06-16 18:21:58 +00:00
|
|
|
<?php
|
|
|
|
/* Required libraries */
|
|
|
|
$TOPDIR = '..';
|
|
|
|
require_once "$TOPDIR/lib/functions.php";
|
|
|
|
require_once "$TOPDIR/lib/jsonwrapper.php";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the contents of an xCAT table
|
|
|
|
*
|
2012-05-04 14:45:17 +00:00
|
|
|
* @param $tab The xCAT table
|
|
|
|
* @param $cont The xCAT table contents
|
2010-06-16 18:21:58 +00:00
|
|
|
* @return The xCAT response. Replies are in the form of JSON
|
|
|
|
*/
|
|
|
|
if (isset($_POST["table"])) {
|
2012-10-15 01:50:28 +00:00
|
|
|
// HTTP POST requests
|
|
|
|
$tab = $_POST["table"];
|
|
|
|
$cont = $_POST["cont"];
|
2010-06-16 18:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create xCAT request
|
|
|
|
$request = simplexml_load_string('<xcatrequest></xcatrequest>');
|
|
|
|
|
|
|
|
// Command is tabrestore
|
|
|
|
$request->addChild('command', 'tabrestore');
|
|
|
|
|
|
|
|
// Setup authentication
|
2012-05-04 14:45:17 +00:00
|
|
|
$usernode = $request->addChild('becomeuser');
|
2010-06-16 18:21:58 +00:00
|
|
|
$usernode->addChild('username', $_SESSION["username"]);
|
|
|
|
$usernode->addChild('password', getpassword());
|
|
|
|
|
|
|
|
// Go through each table row
|
|
|
|
$first = 0;
|
|
|
|
foreach($cont as $line){
|
2012-10-15 01:50:28 +00:00
|
|
|
if($first == 0){
|
|
|
|
// The 1st line is the table header
|
|
|
|
// It does not need special processing
|
|
|
|
// Create string containing all array elements
|
|
|
|
$str = implode(",", $line);
|
|
|
|
$request->addChild('data', $str);
|
2010-06-16 18:21:58 +00:00
|
|
|
|
2012-10-15 01:50:28 +00:00
|
|
|
$first = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2010-06-16 18:21:58 +00:00
|
|
|
|
2012-10-15 01:50:28 +00:00
|
|
|
// Go through each column
|
|
|
|
foreach ($line as &$col){
|
|
|
|
// If the column does begins and end with a quote
|
|
|
|
// Change quotes to "
|
|
|
|
if(!empty($col) && !preg_match('/^".*"$/', $col)) {
|
|
|
|
$col = '"' . $col . '"';
|
|
|
|
}
|
|
|
|
}
|
2010-06-16 18:21:58 +00:00
|
|
|
|
2012-10-15 01:50:28 +00:00
|
|
|
// Sort line
|
|
|
|
ksort($line, SORT_NUMERIC);
|
|
|
|
$keys = array_keys($line);
|
|
|
|
$max = count($line) - 1;
|
|
|
|
if ($keys[$max] != $max){
|
|
|
|
for ($i = 0; $i <= $keys[$max]; $i++) {
|
|
|
|
if (!isset($line[$i])) {
|
|
|
|
$line[$i]='';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ksort($line, SORT_NUMERIC);
|
|
|
|
}
|
2010-06-16 18:21:58 +00:00
|
|
|
|
2012-10-15 01:50:28 +00:00
|
|
|
// Create string containing all array elements
|
|
|
|
$str = implode(",", $line);
|
|
|
|
// Replace " with "
|
|
|
|
$str = str_replace('"', '"', $str);
|
|
|
|
// Replace ' with '
|
|
|
|
$str = str_replace("'", ''', $str);
|
|
|
|
$request->addChild('data', $str);
|
2010-06-16 18:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run command
|
|
|
|
$request->addChild('table', $tab);
|
2011-09-09 19:34:59 +00:00
|
|
|
$xml = submit_request($request, 0, NULL);
|
2010-06-16 18:21:58 +00:00
|
|
|
|
|
|
|
// Reply in the form of JSON
|
|
|
|
$rtn = array("rsp" => $xml);
|
|
|
|
echo json_encode($rtn);
|
|
|
|
?>
|