Return to DevBlog

File: header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Evology Online Document Collaberation Tool</title>
<link href="/css/internal.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" src="/js/basicAJAX.js" type="text/javascript"></script>
</head>

<body onLoad="document.hiddenAction.action.value='read'; document.hiddenAction.path.value='<?php 
if($_POST['path'] != '')
    echo 
$_POST['path'];
else
    echo 
'./Files';
?>'; JavaScript:xmlhttpPost('action.php');">

<div id='wrapper'>
<div id="wrapper">
<div id="logo"><img src="/images/logo-small.jpg" alt="Evology" border="0"></div>
<div id="clearboth"></div>
<hr /><br />
</div>

<div id='body_wrapper'>
<div id='main_body'>
<h1>Evology: Online Document Collaberation Tool</h1>

File: index.php
<?php
if($_GET['user'] != ''){
setcookie('user'$_GET['user'], time()+12000000'/');
die(
header('Location: /')); }

require(
'header.php');
?>
<div>
<?php
if($_COOKIE['user'] == ''){
?>
<h3>Log in as...</h3><ul>
<li><a href="index.php?user=Sara">Sara</a></li>
<li><a href="index.php?user=Gillian">Gillian</a></li>
<?php
} else {
?>
<form name="hiddenAction" action="index.php">
<input type="hidden" name="action">
<input type="hidden" name="path">
<input type="hidden" name="file">
</form>
<div id="result" class="resultDiv"><p style="color:#990000;">Loading...</p></div>
<?php
}

require(
'footer.php');
?>
File: footer.php
</div>
</div>
</div>
</body>
</html>
File: action.php
<?php
require('functions.php');

echo 
'<div style="text-align:right;">';
output_link('read''./Files''''Back to the Main Page');
echo 
'</div>'.chr(10);

if((
$_POST['action'] == 'read')||($_POST['action'] == 'read_move'))
{
    echo 
'<div id="leftPane"><h3>Directory Listing For: ';
    
format_path();
    echo 
'</h3>';
    
create_folder_tree($_POST['action'], $_POST['path']);
    
read_folder($_POST['action'], $_POST['path'], $_POST['file']);
    
close_folder_tree();
    echo 
'</div><div id="rightPane"><h3>Actions:</h3><ul>'.chr(10);
    
output_link('new_folder'$_POST['path'], '''Create a new folder here');
    
output_link('new_file'$_POST['path'], '''Upload a new file here');
    echo 
'</ul>'.chr(10).'</div>';
}

if(
$_POST['action'] == 'move_into')
{
    
$filename explode('/'$_POST['file']);
    
copy($_POST['file'], $_POST['path'].'/'.$filename[count($filename)-1]);
    
unlink($_POST['file']);
    echo 
'File successfully moved<br /><br />';
    
output_link('read'$path'''Open folder');
}

if(
$_POST['action'] == 'new_folder')
{
    echo 
'<h3>Create New Folder</h3><form name="nameForm"><b>Folder Name:</b> <input type="text"';
    echo 
' name="nameInput"></form><a onClick="document.hiddenAction.action.value=\'add_folder\';';
    echo 
' document.hiddenAction.path.value=\''.$_POST['path'].'\'; document.hiddenAction.file.value=';
    echo 
'document.nameForm.nameInput.value; JavaScript:xmlhttpPost(\'action.php\');">Create</a>';
}

if(
$_POST['action'] == 'add_folder')
{
    
mkdir($_POST['path'].'/'.$_POST['file']);
    echo 
'<h3>Folder Created</h3>Open Folder ';
    
output_link('read'$_POST['path'].'/'.$_POST['file'], ''$_POST['file']);
}

if(
$_POST['action'] == 'file_actions')
{
    echo 
'<h3>File Actions</h3>File Name: <b>'.$_POST['file'].'</b><br />In Folder: <b>';
    
format_path();
    echo 
'</b><br /><ul>';
    if(
is_checked_in($_POST['path'].'/'.$_POST['file']) == 'i')
    {
        
output_link('read_move'$_POST['path'], $_POST['path'].'/'.$_POST['file'], 'Move to another folder');
        
output_link('delete'$_POST['path'], $_POST['path'].'/'.$_POST['file'], 'Delete this file');
        
output_link('download'$_POST['path'], $_POST['path'].'/'.$_POST['file'], 'Check-out (download)');
    } 
    else 
    {
        
output_link('upload'$_POST['path'], $_POST['path'].'/'.$_POST['file'], 'Check-in (upload)');
    }
}

if(
$_POST['action'] == 'delete')
{
    echo 
'<h3>Are you sure you want to permenantly delete this file?</h3><br /><br />';
    
output_link('delete_conf'$_POST['path'], $_POST['file'], 'Yes, Delete this file');
    
output_link('read'$_POST['path'], '''No, go back');
}

if(
$_POST['action'] == 'delete_conf')
{
    
unlink($_POST['file']);
    echo 
'<h3>File deleted</h3>';
    
output_link('read'$_POST['path'], '''Back to folder');
}

if(
$_POST['action'] == 'new_file')
{
    echo 
'<h3>Upload a new file</h3><form name="upload" method="post" enctype="multipart/form-data" action="file.php">';
    echo 
'Choose the file you wish to upload<br /><br /><input type="hidden" name="path" value="'.$_POST['path'].'">';
    echo 
'<input type="hidden" name="action" value="new_file"><input type="file" name="file"><br /><br /><a href="#" ';
    echo 
'onClick="document.upload.submit();">Upload File</a></form>';
}

if(
$_POST['action'] == 'upload')
{
    if(
checked_out_by($_POST['file']) != $_COOKIE['user'])
    {
        echo 
'You cannot check in this file. It is checked out by '.checked_out_by($_POST['file']);
    }
    else
    {
        
$file explode('/'$_POST['file']);
        
$file $file[count($file)-1];
        echo 
'<h3>Check-in a file</h3><form name="upload" method="post" enctype="multipart/form-data" action="file.php">';
        echo 
'Upload a file called: <b>'.$file.'</b><br /><br /><input type="hidden" name="path" value="'.$_POST['path'].'">';
        echo 
'<input type="hidden" name="action" value="upload"><input type="file" name="file"><br /><br /><a href="#" ';
        echo 
'onClick="if(/'.$file.'/.test(document.upload.file.value)){ document.upload.submit(); } else { alert(\'You must ';
        echo 
'upload a file called '.$file.'\'); }">Upload File</a></form>';
    }
}

if(
$_POST['action'] == 'download')
{
    
check_out_file($_POST['file']);
    echo 
'<h3>Your can now download your file</h3><a href="'.substr($_POST['file'], 1).'">Download file</a><br /><br />Make sure you ';
    echo 
'download the file. You will need it to check-in this file in the future otherwise it will be locked by the system<br /><br />';
    
output_link('read'$_POST['path'], '''Back to the previous folder');
}

?>
File: functions.php
<?php

function read_folder($action$path)
{
    if(
$handle opendir($path))
    {
        while(
false !== ($file readdir($handle)))
        {
            if(
$file != "." && $file != "..")
            {
                if(
$action == 'read_move')
                {
                    if(!
is_file($path.'/'.$file))
                        
output_move_link($path.'/'.$file$file);
                }
                else
                {
                    if(
is_file($path.'/'.$file))
                        
output_link('file_actions'$path$file$file);
                    else
                        
output_link($action$path.'/'.$file''$file);
                }
            }
        }
    
closedir($handle);
    }
}

function 
create_folder_tree($action$path)
{
    
$dirs '.';
    
$tree explode('/'urldecode($path));
    foreach(
$tree as $path)
    {
        if(
$path != '.')
        {
            
$dirs .= '/'.$path;
            if(
$action == 'read_move')
                
output_move_link($dirs$path);
            else
                
output_link($action$dirs''$path);
            echo 
'<div style="margin-left:30px;">';
        }
    }
}

function 
close_folder_tree()
{
    
$tree explode('/'urldecode($_POST['path']));
    foreach(
$tree as $path)
    {
        if(
$path != '.')
        {
            echo 
'</div>';
        }
    }
}

function 
format_path()
{
    echo 
ereg_replace('/'' > 'substr($_POST['path'], 2));
}

function 
output_link($action$path$file$anchor)
{
    
$ext explode('.'$file);
    
$ext $ext[count($ext) - 1];
    echo 
'<p style="vertical-align:top; margin-top:3px; margin-bottom:3px;">'.chr(10);
    if(!
is_file($path.'/'.$file))
        echo 
'<img src="images/icon_folder.gif">&nbsp;&nbsp;'.chr(10);
    else if(
file_exists('images/icon_'.$ext.'.gif'))
        echo 
'<img src="images/icon_'.$ext.'.gif">&nbsp;&nbsp;'.chr(10);
    else
        echo 
'<img src="images/icon_blank.gif">&nbsp;&nbsp;'.chr(10);
    echo 
'<a onClick="document.hiddenAction.action.value=\''.$action.'\'; document.hiddenAction.path.value=\''.$path.'\'; ';
    echo 
'document.hiddenAction.file.value=\''.$file.'\'; JavaScript:xmlhttpPost(\'action.php\');">'.$anchor.'</a></p>';
}

function 
output_move_link($path$file)
{
    echo 
'<p style="vertical-align:top; margin-top:3px; margin-bottom:3px;"><img src="images/icon_folder.gif">&nbsp;&nbsp;'.chr(10);
    echo 
$file.'&nbsp;&nbsp;'.chr(10).'<a onClick="document.hiddenAction.action.value=\'read_move\'; ';
    echo 
' document.hiddenAction.path.value=\''.$path.'\'; document.hiddenAction.file.value=\''.$_POST['file'].'\'; ';
    echo 
'JavaScript:xmlhttpPost(\'action.php\');">Open Folder</a>&nbsp;'.chr(10).'<a onClick="document.hiddenAction.action.value=';
    echo 
'\'move_into\'; document.hiddenAction.path.value=\''.$path.'\'; document.hiddenAction.file.value=\''.$_POST['file'].'\'; ';
    echo 
'JavaScript:xmlhttpPost(\'action.php\');">Move Here</a></p>';
}

function 
is_checked_in($file)
{
    
$conn mysql_connect('db138.oneandone.co.uk''dbo167292414''K7yRnxgS');
    
$db mysql_select_db('db167292414');
    while(
$result['inout'] == '')
    {
        
$result mysql_fetch_assoc(mysql_query("SELECT inout FROM checkout WHERE filename='$file'"));
        if(
$result['inout'] == '')
            
mysql_query("INSERT INTO checkout (filename) VALUES ('$file')");
    }
    
mysql_close($conn);
    return 
$result['inout'];
}

function 
checked_out_by($file)
{
    
$conn mysql_connect('db138.oneandone.co.uk''dbo167292414''K7yRnxgS');
    
$db mysql_select_db('db167292414');
    
$result mysql_fetch_assoc(mysql_query("SELECT user FROM checkout WHERE filename='$file'"));
    
mysql_close($conn);
    return 
$result['user'];
}

function 
check_out_file($file)
{
    
$conn mysql_connect('db138.oneandone.co.uk''dbo167292414''K7yRnxgS');
    
$db mysql_select_db('db167292414');
    
mysql_query("UPDATE checkout SET inout='o', user='".$_COOKIE['user']."' WHERE filename='$file'");
    
mysql_close($conn);
    return 
$result['user'];
}

function 
debug_post()
{
    foreach(
$_POST as $key => $var)
    {
        echo 
$key.': '.$var.'<br />';
    }
}

?>
File: file.php
<?php

require('header.php');

if(
$_POST['action'] == 'new_file')
{
    foreach(
$_FILES['file'] as $key => $var)
    {
        $
$key $var;
    }
    if(
$error == 0)
    {
        
copy($tmp_name$_POST['path'].'/'.$name);
        echo 
'<h3>File successfully uploaded</h3><form name="upload" action="/" method="post"><input type="hidden" name="path" ';
        echo 
'value="'.$_POST['path'].'"><a href="#" onClick="document.upload.submit();">Back to the previous folder</a></form>';
    }
}

if(
$_POST['action'] == 'upload')
{
    foreach(
$_FILES['file'] as $key => $var)
    {
        $
$key $var;
    }
    if(
$error == 0)
    {
        
$conn mysql_connect('db138.oneandone.co.uk''dbo167292414''K7yRnxgS');
        
$db mysql_select_db('db167292414');
        
mysql_query("UPDATE checkout SET inout='i' WHERE filename='".$_POST['path'].'/'.$name."'");
        
mysql_close($conn);
        
copy($tmp_name$_POST['path'].'/'.$name);
        echo 
'<h3>File successfully checked in</h3><form name="upload" action="/" method="post"><input type="hidden" name="path" ';
        echo 
'value="'.$_POST['path'].'"><a href="#" onClick="document.upload.submit();">Back to the previous folder</a></form>';
    }
}

require(
'footer.php');

?>
File: checkedin.php
<?php
require('header.php');
$conn mysql_connect('db138.oneandone.co.uk''dbo167292414''K7yRnxgS');
$db mysql_select_db('db167292414');

if(
$_FILES['file1']['size'] == 0){
echo 
'<h2>Upload Shared Files</h2><br />The file you upload must be named <b>'.urldecode($_GET['file']).'</b> otherwise it will 
      not be checked back in and will still be unavailable for download.<br /><form action="checkedin.php" method="post"
      enctype="multipart/form-data" name="form1"><input type="hidden" name="file" value="'
.urldecode($_GET['file']).'">
      <b>File: <input type="file" name="file1"><br /><br /><input type="submit" value="Upload New File"></form>'
;
} else {
copy($_FILES['file1']['tmp_name'], 'files/'.$_FILES['file1']['name']);
mysql_query("UPDATE checkout SET inout='i' WHERE filename='".$_POST['file']."'");
echo 
'<a href="/">Main Page</a>';
}

mysql_close($conn);
require(
'footer.php');
?>
File: checkedout.php
<?php
$conn 
mysql_connect('db138.oneandone.co.uk''dbo167292414''K7yRnxgS');
$db mysql_select_db('db167292414');

echo 
'<h2>Shared Files</h2><ul><li><a href="/">Main Page</a></li></ul><ul>';
$file urldecode($_GET['file']);
mysql_query("UPDATE checkout SET inout='o' WHERE filename='$file'");
echo 
'<li><a href="files/'.$file.'">'.$file.'</a><br /><b>Download this file now!</b><br />
      This file will not be available to check out again until someone has checked it back in</li></ul>'
;

mysql_close($conn);
?>