Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

Webserver1/Ovningar/Slutprojekt/

.env
DEBUG/
Media/
account.js
account.php
callback_log.txt
change_account_details.php
composer.json
composer.lock
forgot_pass.php
forgot_pass_new_pass.php
header.php
index.php
login.php
mediaplayer.php
node_modules/
package-lock.json
package.json
signup.php
style.css
upload.js
upload_callback.php
upload_callback_simulated.php
upload_chunk.php
upload_errors.log
upload_form.php
upload_handler.php
upload_success.log
vendor/
verify_file.php
verifypage.php

upload_chunk.php

127 lines UTF-8 Windows (CRLF)
<?php
session_start
();

$local = isset($_SERVER['HTTP_HOST']) && strpos($_SERVER['HTTP_HOST'], 'labb.vgy.se') === false;

define("LOCAL_UPLOAD_DIR""../../../filhantering/");
define("SERVER_UPLOAD_DIR""/home/antasp23/public_html/filhantering/");
define("UPLOAD_DIR"$local LOCAL_UPLOAD_DIR SERVER_UPLOAD_DIR);
define("CHUNK_UPLOAD_DIR"UPLOAD_DIR); // Server tillåter inte write till chunks/

function get_chunk_upload_dir(): string {
  if (!
is_dir(CHUNK_UPLOAD_DIR)) {
    
$created = @mkdir(CHUNK_UPLOAD_DIR0755true);
    
$debug_msg date('Y-m-d H:i:s') . " - Created chunk dir: " . ($created 'yes' 'no') . ", path: " CHUNK_UPLOAD_DIR ", exists: " . (is_dir(CHUNK_UPLOAD_DIR) ? 'yes' 'no') . "\n";
    @
file_put_contents(CHUNK_UPLOAD_DIR 'chunk_debug.log'$debug_msgFILE_APPEND);
  }
  return 
CHUNK_UPLOAD_DIR;
}

/**
 * Cleans up abandoned temp/part files older than 2 hours.
 */
function gc_temp_files(string $dir): void {

    
$expire_time time() - (3600); // 2 hours ago
    
$files glob($dir "*.{part,tmp,meta.json}"GLOB_BRACE);

    if (
$files) {
        foreach (
$files as $file) {
            if (
file_exists($file) && filemtime($file) < $expire_time) {
                @
unlink($file);
            }
        }
    }
}

header('Content-Type: application/json; charset=utf-8');

// Debug logging
$debug_log CHUNK_UPLOAD_DIR 'chunk_debug.log';
$debug_msg date('Y-m-d H:i:s') . " - Chunk request: " json_encode([
    
'method' => $_SERVER['REQUEST_METHOD'],
    
'post' => $_POST,
    
'files' => array_keys($_FILES),
    
'upload_id' => $_POST['uploadId'] ?? 'none'
]) . "\n";
@
file_put_contents($debug_log$debug_msgFILE_APPEND);

function 
json_error(string $messageint $status 400, array $extra = []): void {
    
http_response_code($status);
    echo 
json_encode(array_merge(['status' => 'error''message' => $message], $extra));
    exit;
}

$uploadId $_POST['uploadId'] ?? '';
$chunkIndex = isset($_POST['chunkIndex']) ? intval($_POST['chunkIndex']) : null;
$totalChunks = isset($_POST['totalChunks']) ? intval($_POST['totalChunks']) : null;
$fileName basename($_POST['fileName'] ?? '');
$fileType $_POST['fileType'] ?? 'video/mp4';
$fileSize = isset($_POST['fileSize']) ? intval($_POST['fileSize']) : null;
$chunk $_FILES['chunk'] ?? null;

if (!
$uploadId || $chunkIndex === null || $totalChunks === null || !$chunk) {
    
json_error('Missing upload fields or chunk data');
}

if (
$chunk['error'] !== UPLOAD_ERR_OK) {
    
json_error('Chunk upload failed with PHP error code ' $chunk['error']);
}

$safeUploadId preg_replace('/[^A-Za-z0-9_-]/'''$uploadId);
if (
$safeUploadId !== $uploadId) {
    
json_error('Invalid upload ID');
}

if (
$chunkIndex || $chunkIndex >= $totalChunks) {
    
json_error('Invalid chunk index');
}

$targetDir get_chunk_upload_dir();
$chunkedPath $targetDir $safeUploadId '.part';
$finalPath $targetDir $safeUploadId '.tmp';
$metaPath $targetDir $safeUploadId '.meta.json';

if (
$chunkIndex === && file_exists($chunkedPath)) {
    @
unlink($chunkedPath);
}

if (
$chunkIndex === 0) {
    
$metadata = [
        
'uploadId' => $safeUploadId,
        
'fileName' => $fileName,
        
'fileType' => $fileType,
        
'fileSize' => $fileSize,
        
'totalChunks' => $totalChunks,
        
'createdAt' => time(),
    ];
    
file_put_contents($metaPathjson_encode($metadataJSON_UNESCAPED_UNICODE));
}

$chunkData = @file_get_contents($chunk['tmp_name']);
if (
$chunkData === false) {
    
json_error('Could not read uploaded chunk');
}

$fp = @fopen($chunkedPath'ab');
if (!
$fp) {
    
json_error('Could not open temporary chunk file for appending');
}

fwrite($fp$chunkData);
fclose($fp);
@
unlink($chunk['tmp_name']);

if (
$chunkIndex === $totalChunks 1) {
    if (!@
rename($chunkedPath$finalPath)) {
        
json_error('Could not finalize chunked upload file');
    }
}

echo 
json_encode([
    
'status' => 'ok',
    
'uploadId' => $safeUploadId,
    
'chunkIndex' => $chunkIndex,
    
'totalChunks' => $totalChunks,
]);