110 lines
3.5 KiB
PHP
110 lines
3.5 KiB
PHP
<?php
|
|
|
|
|
|
/// Perform a move by copy, and if successful then delete the prior version
|
|
///
|
|
function MoveFiles($src, $dst) {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "move($src, $dst)", TR_ENTRY);
|
|
|
|
// Source - https://stackoverflow.com/q/9835492
|
|
// Posted by Sara, modified by community. See post 'Timeline' for change history
|
|
// Retrieved 2026-10-18, License - CC BY-SA 3.0
|
|
|
|
// Get array of all source files
|
|
$files = scandir($src);
|
|
// Identify directories
|
|
// Cycle through all source files
|
|
$delete = array();
|
|
foreach ($files as $file) {
|
|
if (in_array($file, array(".",".."))) continue;
|
|
// If we copied this successfully, mark it for deletion
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "copy($src.$file, $dst.$file)");
|
|
if (copy($src.$file, $dst.$file)) {
|
|
$delete[] = $src.$file;
|
|
}
|
|
}
|
|
// Delete all successfully-copied files
|
|
foreach ($delete as $file) {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "unlink($file)");
|
|
unlink($file);
|
|
}
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
|
|
}
|
|
|
|
|
|
function MakeValidFolderName($input, $replacement = '_') {
|
|
// Ensure input is not empty
|
|
if (trim($input) === '') {
|
|
return 'untitled';
|
|
}
|
|
$input = mb_convert_encoding($input, 'UTF-8', 'UTF-8');
|
|
$input = preg_replace('/\s+/', $replacement, $input);
|
|
$input = preg_replace('~[\\/:*?"<>|\x00-\x1F]~', '', $input);
|
|
$input = preg_replace('/[^A-Za-z0-9._-]/', $replacement, $input);
|
|
$input = preg_replace('/' . preg_quote($replacement, '/') . '+/', $replacement, $input);
|
|
$input = trim($input, $replacement);
|
|
if ($input === '') {
|
|
return 'untitled';
|
|
}
|
|
return $input;
|
|
}
|
|
|
|
|
|
function ReadTableData($path, $formFields, &$userData) {
|
|
$success = false;
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_ENTRY);
|
|
$path = @rtrim($path, "/");
|
|
$fqfnData = $path . '/' . 'data.txt';
|
|
if (file_exists($fqfnData)) {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $fqfnData);
|
|
$fh = fopen($fqfnData, "r");
|
|
while (!feof($fh)) {
|
|
$line = trim(fgets($fh));
|
|
if ($line != "") {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "$line");
|
|
$array = explode("=", $line, 2);
|
|
$userData[$array[0]] = $array[1];
|
|
$success = true;
|
|
}
|
|
}
|
|
fclose($fh);
|
|
foreach ($formFields as $key => $value) {
|
|
if ($value['FieldType'] == "pix") {
|
|
if (isset($userData[$key])) {
|
|
$fqfnPix = sprintf("%s/%s", $path, $userData[$key]);
|
|
if ($userData[$key] == '') { // but maybe the file exists
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "fqfnPix: $fqfnPix, key: $key");
|
|
$candidates = glob($fqfnPix . $key . ".*");
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($candidates, true));
|
|
foreach ($candidates as $candidate) {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $candidate);
|
|
$userData[$key] = basename($candidate);
|
|
}
|
|
}
|
|
$userData[$key] = (file_exists($fqfnPix)) ? $userData[$key] : '';
|
|
$success = true;
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "file '$fqfnData' does not exist.");
|
|
}
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
|
|
return $success;
|
|
}
|
|
|
|
function SaveTableData($path, $formFields, $formData) {
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "\nSaveTableData: " . print_r($formData, true), TR_ENTRY);
|
|
$dataFile = $path . "/data.txt";
|
|
$fh = fopen($dataFile, "wt");
|
|
if ($fh) {
|
|
foreach ($formFields as $key => $value) {
|
|
fprintf($fh, "%s=%s\n", $key, isset($formData[$key]) ? $formData[$key] : $value['Default']);
|
|
}
|
|
fclose($fh);
|
|
}
|
|
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
|
|
}
|
|
|
|
|
|
?>
|