dat/export.php -
index
<?php
/* DAT/EXPORT.PHP - Admin code for export and import posts */
// EXPERIMENTAL interface
$section = ($t = config('section')) ? $t: 'root';
$cmd['export'] = "Would you like to save ALL <b>$section</b> posts to the 'import' directory?";
$cmd['import'] = "Would you like to import ALL import files into <b>$section</b>?";
$cmd['delete'] = "Would you like to delete all files in the 'import' directory?";
$act['export'] = "export posts";
$act['import'] = "import files";
$act['delete'] = "delete import files";
$func['export'] = '_export';
$func['import'] = '_import';
$func['delete'] = '_delete';
$sum['export'] = "records written";
$sum['import'] = "files imported";
$sum['delete'] = "files deleted";
$query = <<<HTML
<form name="query" method="get" action="{\$_SERVER['PHP_SELF']}">
<input type="submit" value="\$expcmd">
<input type="hidden" name="op" value="export">
<input type="hidden" name="expcmd" value="\$expcmd">
<input type="hidden" name="arg" value="\$arg">
</form>
HTML;
if ($expcmd == '') {
$n = _export(1);
echo "There are $n records for export. ";
if ($n) {
$expcmd = "export";
// eval(shit_show($query));
// just a reminder that all that shit_show() does is this:
$html = addcslashes($query,'"');
eval("print \"$html\";");
}
$n = _import(1);
echo "<br>There are $n files available for import.";
if (!$n) echo "<br>";
if ($n) {
$expcmd = "import";
eval(shit_show($query));
$expcmd = "delete";
eval(shit_show($query));
}
$doneop = "read";
$HTML = "";
}
else {
if (!isset($_POST['yes'])) {
$preamble = $cmd[$expcmd];
$action = $act[$expcmd];
eval(shit_show(_message('confirm')));
}
else {
$n = $func[$expcmd]();
$HTML = "<br>$n ".$sum[$expcmd];
}
$doneop = $op;
}
if (isset($HTML))
echo <<<HTML
$HTML
<br>
<form title="$formtitle" name="done" method="get" action="{$_SERVER['PHP_SELF']}">
<input type="submit" value="done">
<input type="hidden" name="op" value="$doneop">
<input type="hidden" name="arg" value="$arg">
</form>
HTML;
function _import($cmd = 0) {
$files = glob('import/*.txt');
if ($cmd) return count($files);
echo 'Importing... ';
natsort($files);
foreach ($files as $file) {
echo _file($file);
$data = file_get_contents($file);
dbnewrecord($data);
}
return count($files);
}
function _export($cmd = 0) {
$recs = dblistrecords();
if (!$recs) return 0;
if ($cmd) return count($recs);
echo 'Saving... ';
$c = 1;
natsort($recs);
foreach ($recs as $id) {
$record = dbreadrecord($id);
echo "$id ";
$file = "import/$c.txt";
file_put_contents($file,recordtostring($record));
++$c;
}
return $c - 1;
}
function _delete() {
$files = glob('import/*.txt');
if (!$files) {
echo 'no files found';
return;
}
echo 'Deleting... ';
foreach ($files as $file) {
echo _file($file);
unlink($file);
}
return count($files);
}
function _file($file) {
return str_replace('.txt','',str_replace('import/','',$file)).' ';
}