<?php

/* This file contains some functions that are used throughout the site
   in different locations. It also deals with the initialisation of the
   page
*/


function humanify_size($size) {

    // this function takes a size in bytes and returns a size in
    // a more human readalbe form

    if($size > 1048576) {
	// This is more than 1 Mbyte
	$new_size = $size / 10485.76;
	$new_size = floor($new_size);
	$new_size = $new_size / 100;
	$new_size .= " Mb";
    }
    else if($size > 1024) {
	// This is more than 1 Kbyte
	$new_size = $size / 10.24;
	$new_size = floor($new_size);
	$new_size = $new_size / 100;
	$new_size .= " Kb";
    }
    else {
	$new_size = $size;
	$new_size .= " b";
    }

    return $new_size;
}


function delete_file($file_id) {

	global $db, $PHP_AUTH_USER, $is_administrator;

	// This function deletes a file from the system
	
	// get information about this file
	$sql = "select file, vfs_user from files where ";
	$sql .= "file_id='$file_id'";
	$result = $db->execute($sql);
	if($result) {
		$file = $result->getVal(0,0);
		$vfs_user = $result->getVal(0,1);
		
		// now we have the details check to see if we have permission
		if(($is_administrator) || ($vfs_user == $PHP_AUTH_USER)) {
			// ok to delete blob
			pg_lounlink($db->conn, $file);
			
			// delete the reference entry from the database
			$sql = "delete from files where file_id='$file_id'";
			$result = $db->execute($sql);

			// delete all access_logs for this file
			$sql = "delete from access_log where file_id='$file_id'";
			$result = $db->execute($sql);
		}
	}
}

?>

