Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/notanexus.git
<?php
$filename = isset( $_GET['filename'] ) ? $_GET['filename'] : '';

// Handle annotation POST requests
if( $_SERVER['REQUEST_METHOD'] === 'POST' &&
    isset( $_POST['annotations'] ) &&
    isset( $_POST['filename'] ) ) {
  // Get the directory of the current script
  $script_dir = dirname( __FILE__ );

  // Ensure target directory exists ( relative to this script )
  $target_dir = $script_dir . "/nn";

  if( !is_dir( $target_dir ) ) {
    mkdir( $target_dir, 0755, true );
  }

  // Sanitize the filename to prevent directory traversal
  $sanitized_filename = basename( $_POST['filename'] );

  // Remove .pdf extension if present
  $sanitized_filename = preg_replace( '/\.pdf$/', '', $sanitized_filename );

  // Full path to the target JSON file
  $target_file = "$target_dir/{$sanitized_filename}.json";

  // Write the annotations data to the file
  $annotations_data = $_POST['annotations'];
  file_put_contents( $target_file, $annotations_data );

  // Return a success response
  header( 'Content-Type: application/json' );
  echo json_encode( ['success' => true] );
  exit;
}

$script_dir = dirname( __FILE__ );

// Define paths to check
$alpha_file = realpath( "$script_dir/alpha/$filename" );
$beta_file = realpath( "$script_dir/beta/$filename" );

// Set default path
$pdf_path = "/alpha/$filename";

// Check if file exists in alpha directory, otherwise try beta
if( !$alpha_file && $beta_file ) {
  $pdf_path = "/beta/$filename";
}

// Dynamically generate the base URL
$base_url = rtrim( dirname( $_SERVER['SCRIPT_NAME'] ), '/' ) . '/';
?>
<!DOCTYPE html>
<html dir="ltr" lang="en" mozdisallowselectionprint>
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <base href="<?= $base_url ?>" />
  <title>NotaNexus: <?= $filename ?></title>
  <link rel='stylesheet' type='text/css' href='styles/main.css'>
</head>
<body tabindex="1">
  <div id='pdf' data-filename='<?= $pdf_path ?>'></div>
  <script type="module" src='js/pdf.min.mjs'></script>
  <script type='module' src='js/viewer.js'></script>
</body>
</html>