view slbackup.php @ 217:58af72da7f60

Update copyrights.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Jan 2018 01:47:38 +0200
parents d2905ee5ff9c
children
line wrap: on
line source

<?php
//
// Syntilista - debt list/management database
// SQLite3 database backup backend PHP blurb
// Programmed and designed by Matti Hämäläinen <ccr@tnsp.org>
// (C) Copyright 2017-2018 Tecnic Software productions (TNSP)
//
// Distributed under 3-clause BSD style license, refer to
// included file "COPYING" for exact terms.
//

//
// Settings, etc.
//
$dataName = "backup";
$dataSuffix = ".sqlite3";
$dataMaxSize = 1024 * 1024;
$dataBackups = 24;
$configFile = "slbackup.cfg";


//
// Helper functions
//
function stError($msg)
{
  global $errorSet, $errorMsg;
  $errorSet = TRUE;
  $errorMsg = $msg;
}


function stGetBackupFilenameFilename($base, $suffix, $num)
{
  return sprintf("%s%02d%s", $base, $num, $suffix);
}


function stRotateBackups($base, $suffix, $num)
{
  for ($i = $num; $i > 0; $i--)
  {
    $srcFilename = stGetBackupFilenameFilename($base, $suffix, $i - 1);
    if (file_exists($srcFilename))
    {
      $dstFilename = stGetBackupFilenameFilename($base, $suffix, $i);
      if (file_exists($dstFilename))
        unlink($dstFilename);

      if (@rename($srcFilename, $dstFilename) === FALSE)
        return FALSE;
    }
  }  
  return TRUE;
}


function stLog($msg)
{
  error_log("SyntilistaBackup: ".$msg);
}


//
// Actual main code begins here
//
if (file_exists($configFile))
  require_once $configFile;
else
{
  stLog("Configuration file '".$configFile."' does not exist!");
  exit;
}


$errorMsg = "";
$errorSet = FALSE;
$index = "file";

if (!isset($dataSecret) || !isset($dataPath) ||
    $dataSecret == "" || $dataPath == "")
{
  stLog("Invalid configuration.");
  exit;
}

// Basic check for credentials ..
if (isset($_REQUEST["secret"]) && isset($_FILES[$index]) &&
  ($secret = $_REQUEST["secret"]) == $dataSecret)
{
  $fileName = $_FILES[$index]["tmp_name"];
  $fileSize = $_FILES[$index]["size"];
  $fileError = $_FILES[$index]["error"];

  switch ($fileError)
  {
    case UPLOAD_ERR_INI_SIZE:
      stError("File size exceeds PHP's max upload size.");
      break;

    case UPLOAD_ERR_PARTIAL:
      stError("File only partially uploaded.");
      break;

    case UPLOAD_ERR_NO_FILE:
      stError("No file data received!");
      break;

    case UPLOAD_ERR_NO_TMP_DIR:
      stError("Internal error: Temporary file directory not available!");
      break;

    case UPLOAD_ERR_CANT_WRITE:
      stError("Internal error: PHP could not write the file to disk.");
      break;

    case UPLOAD_ERR_OK:
      break;

    default:
      stError("Unknown PHP file error occured.");
      break;
  }
  
  if (!$errorSet && $fileSize > $dataMaxSize)
  {
    stError("File is too large (".$fileSize." > ".$dataMaxSize." bytes).");
  }
  
  if (!$errorSet)
  {
    $path = $dataPath."/".$dataName;
    stRotateBackups($path, $dataSuffix, $dataBackups);

    $dstFilename = stGetBackupFilenameFilename($path, $dataSuffix, 1);
    if (@move_uploaded_file($fileName, $dstFilename) === false)
    {
      stError("Could not move the uploaded file to proper directory.");
    }
  }
  
  if ($errorSet)
  {
    header("Status: 500 Internal error");
    header("Content-Type: text/plain");
    echo $errorMsg;
  }
}
else
{
  header("Status: 403 Forbidden");
  header("Content-Type: text/plain");
}
?>