2f2a72e90c
git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/branches/2.7@14006 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd
83 lines
2.2 KiB
PHP
83 lines
2.2 KiB
PHP
<?php
|
|
/* Required libraries */
|
|
$TOPDIR = '..';
|
|
require_once "$TOPDIR/lib/functions.php";
|
|
require_once "$TOPDIR/lib/jsonwrapper.php";
|
|
|
|
/**
|
|
* Replace the contents of an xCAT table
|
|
*
|
|
* @param $tab The xCAT table
|
|
* @param $cont The xCAT table contents
|
|
* @return The xCAT response. Replies are in the form of JSON
|
|
*/
|
|
if (isset($_POST["table"])) {
|
|
// HTTP POST requests
|
|
$tab = $_POST["table"];
|
|
$cont = $_POST["cont"];
|
|
}
|
|
|
|
// Create xCAT request
|
|
$request = simplexml_load_string('<xcatrequest></xcatrequest>');
|
|
|
|
// Command is tabrestore
|
|
$request->addChild('command', 'tabrestore');
|
|
|
|
// Setup authentication
|
|
$usernode = $request->addChild('becomeuser');
|
|
$usernode->addChild('username', $_SESSION["username"]);
|
|
$usernode->addChild('password', getpassword());
|
|
|
|
// Go through each table row
|
|
$first = 0;
|
|
foreach($cont as $line){
|
|
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);
|
|
|
|
$first = 1;
|
|
continue;
|
|
}
|
|
|
|
// 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 . '"';
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
// Run command
|
|
$request->addChild('table', $tab);
|
|
$xml = submit_request($request, 0, NULL);
|
|
|
|
// Reply in the form of JSON
|
|
$rtn = array("rsp" => $xml);
|
|
echo json_encode($rtn);
|
|
?>
|