Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1285 → Rev 1286

/FileXch/branches/php-impl/upload.php
0,0 → 1,24
<?php
$isEntryPoint = true;
include("lib/common.php");
 
try {
// upload
if($_POST["upload"] !== null) {
$id = $p->integer($_POST["id"], true, "ID");
$s = $sm->getSession($id);
 
foreach($_FILES["file"]["error"] as $key => $error) {
if($error == UPLOAD_ERR_OK) {
$s->saveFile($_FILES["file"]["tmp_name"][$key], $_FILES["file"]["name"][$key]);
}
}
 
dataCollected();
http_redirect("manage.php?id=$id");
}
}
catch(Exception $ex) {
generalError();
}
?>
/FileXch/branches/php-impl/guest.php
0,0 → 1,72
<?php
$isEntryPoint = true;
include("lib/common.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 
<head>
<title>FilExch</title>
</head>
 
<body>
 
<table border=1>
<tr>
<th>Expires</th>
<td>01.01.2008 12:00:00</td>
</tr>
<tr>
<th>Current Size</th>
<td>123 MB</td>
</tr>
<tr>
<th>Size Limit</th>
<td>1 GB</td>
</tr>
<tr>
<th>Files</th>
<td>3</td>
</tr>
<tr>
<th>Files Limit</th>
<td>-</td>
</tr>
</table>
 
<table border=1>
<tr><td><a href="#">file1.txt</a></td></tr>
<tr><td><a href="#">file2.mp3</a></td></tr>
<tr><td><a href="#">file3.avi</a></td></tr>
</table>
 
<form>
 
<table border=1>
<tr>
<td>File 1</td>
<td><input type=file></td>
</tr>
<tr>
<td>File 2</td>
<td><input type=file></td>
</tr>
<tr>
<td>File 3</td>
<td><input type=file></td>
</tr>
<tr>
<td>File 4</td>
<td><input type=file></td>
</tr>
<tr>
<td colspan=2 align=right><input type=submit value="Upload"></td>
</tr>
</table>
 
</form>
 
</body>
 
</html>
 
/FileXch/branches/php-impl/admin.php
0,0 → 1,29
<?php
$isEntryPoint = true;
include("lib/common.php");
 
try {
// remove a session
if($id = $_GET["remove"]) {
$id = $p->integer($id, false, "ID");
$sm->remove($id);
 
dataCollected();
http_redirect("admin.php");
}
 
// show the list
else {
$aliveList = $sm->listAlive();
$aliveSummary = $sm->summaryAlive();
$removedList = $sm->listRemoved();
$removedSummary = $sm->summaryRemoved();
 
dataCollected();
include("pages/admin_list.php");
}
}
catch(Exception $ex) {
generalError();
}
?>
/FileXch/branches/php-impl/manage.php
0,0 → 1,92
<?php
$isEntryPoint = true;
include("lib/common.php");
 
try {
// save session
if($_POST["save"] !== null) {
$id = $p->integer($_POST["id"], true, "ID");
 
// dummy session to store user input
$s = $sm->newSession();
$s->setExpire ($_POST["expire"]);
$s->setSizeLimit ($_POST["size_limit"]);
$s->setCountLimit ($_POST["count_limit"]);
$s->setDownloadable ($_POST["downloadable"]);
$s->setUploadable ($_POST["uploadable"]);
$s->setDeletable ($_POST["deletable"]);
$s->setPublicComment ($_POST["public_comment"]);
$s->setPrivateComment($_POST["private_comment"]);
$s->setBlocked ($_POST["blocked"]);
 
// load
if($id) {
$s2 = $sm->getSession($id);
}
else {
$s2 = $sm->newSession();
}
 
// parse the input
try {
$s2->setExpire ($p->date ($_POST["expire"], true, "Expire"));
$s2->setSizeLimit ($p->size ($_POST["size_limit"], true, "Size Limit"));
$s2->setCountLimit ($p->integer($_POST["count_limit"], true, "Files Limit"));
$s2->setDownloadable ($p->boolean($_POST["downloadable"]));
$s2->setUploadable ($p->boolean($_POST["uploadable"]));
$s2->setDeletable ($p->boolean($_POST["deletable"]));
$s2->setPublicComment ($p->string ($_POST["public_comment"]));
$s2->setPrivateComment($p->string ($_POST["private_comment"]));
$s2->setBlocked ($p->boolean($_POST["blocked"]));
}
catch(Exception $ex) {
dataCollected();
include("pages/manage_display.php");
exit;
}
 
// save
if($id) {
$sm->update($s2); // existing
}
else {
$sm->insert($s2); // new
}
 
// end
dataCollected();
http_redirect("admin.php");
}
 
// delete one file from a session
else if($_GET["delete"]) {
$id = $p->integer($_GET["id"], false, "ID");
$name = $p->string($_GET["delete"], false, "Name");
$s = $sm->getSession($id);
$s->removeFile($name);
 
dataCollected();
http_redirect("manage.php?id=$id");
}
 
// display existing session
else if($id = $_GET["id"]) {
$id = $p->integer($id, true, "ID");
$s = $sm->getSession($id);
 
dataCollected();
include("pages/manage_display.php");
}
 
// display new session
else {
$s = $sm->newSession();
 
dataCollected();
include("pages/manage_display.php");
}
}
catch(Exception $ex) {
generalError();
}
?>
/FileXch/branches/php-impl/pages/admin_list.php
0,0 → 1,87
<?php checkDataCollected(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 
<head>
<title>FilExch Admin</title>
</head>
 
<body>
 
<h1>FilExch Admin</h1>
 
<?php if($ex) { ?>
<p><?= $ex->getMessage() ?></p>
<?php } ?>
 
<p><a href="manage.php">Create a new session.</a></p>
 
<?php if(!$aliveList) { ?>
<p>No session alive.</p>
<?php } else { ?>
<table border=1>
<tr>
<th>Session</th>
<th>Owner</th>
<th>Created</th>
<th>Expire</th>
<th>Size</th>
<th>Count</th>
<th colspan=4>Flags</th>
<th colspan=2>Comments</th>
<th>Active</th>
<th>&nbsp;</th>
</tr>
<?php foreach($aliveList as $s) { ?>
<tr>
<td><a href="guest.php?s=<?= $f->htmlUrlPart($s->getName()) ?>"><?= $f->string($s->getName()) ?></a></td>
<td><?= $s->getOwner() ?></td>
<td><?= $f->date($s->getCreated(), "&nbsp;") ?></td>
<td><?= $f->date($s->getExpire(), "&nbsp;") ?></td>
<td><?= $f->size($s->getSizeCurrent(), "0 B") ?> / <?= $f->size($s->getSizeLimit(), "-") ?></td>
<td><?= $f->string($s->getCountCurrent(), "0") ?> / <?= $f->string($s->getCountLimit(), "-") ?></td>
<td title="<?= $f->boolean($s->getDownloadable(), "Guest may download files",
"Guest may NOT download files") ?>"><?= $f->boolean($s->getDownloadable(), "D", "&nbsp;") ?></td>
<td title="<?= $f->boolean($s->getUploadable(), "Guest may upload files",
"Guest may NOT upload files") ?>"><?= $f->boolean($s->getUploadable(), "U", "&nbsp;") ?></td>
<td title="<?= $f->boolean($s->getDeletable(), "Guest may delete files",
"Guest may NOT delete files") ?>"><?= $f->boolean($s->getDeletable(), "x", "&nbsp;") ?></td>
<td title="<?= $f->boolean($s->getBlocked(), "Session is blocked",
"Session is not blocked") ?>"><?= $f->boolean($s->getBlocked(), "B", "&nbsp;") ?></td>
<td title="<?= $f->string($s->getPublicComment()) ?>"><?=
$f->stringBegin($s->getPublicComment(), 10) ?></td>
<td title="<?= $f->string($s->getPrivateComment()) ?>"><?=
$f->stringBegin($s->getPrivateComment(), 10) ?></td>
<td><?= $f->boolean($s->isActive(), "x", "&nbsp;") ?></td>
<td nowrap>
<a href="manage.php?id=<?= $f->htmlUrlPart($s->getId()) ?>">Manage</a>
<a href="admin.php?remove=<?= $f->htmlUrlPart($s->getId()) ?>">Remove</a>
</td>
</tr>
<?php } ?>
<tr>
<th>Total</th>
<td colspan="3">&nbsp;</td>
<td><?= $f->size($aliveSummary['sizeCurrent'], "0 B") ?>
/ <?= $f->size($aliveSummary['sizeLimit'], "-") ?></td>
<td><?= $f->string($aliveSummary['countCurrent'], "0") ?>
/ <?= $f->string($aliveSummary['countLimit'], "-") ?></td>
<td colspan="7">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<?php } ?>
 
<?php if(!$removedList) { ?>
<p>No removed sessions.</p>
<?php } else { ?>
<p><a href="removed.php">Removed sessions</a>
(<?= count($removedList) ?> sessions,
<?= $f->string($removedSummary['countCurrent'], "0") ?> files,
<?= $f->size($removedSummary['sizeCurrent'], "0 B") ?>)</p>
<?php } ?>
 
</body>
 
</html>
 
/FileXch/branches/php-impl/pages/general_error.php
0,0 → 1,22
<?php checkDataCollected(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 
<head>
<title>FilExch - Error</title>
</head>
 
<body>
 
<h1>FilExch - Error</h1>
 
<?php if($ex) { ?>
<p><?= $ex->getMessage() ?></p>
<?php } else { ?>
<p>Internal error</p>
<?php } ?>
 
</body>
 
</html>
 
/FileXch/branches/php-impl/pages/removed_list.php
0,0 → 1,79
<?php checkDataCollected(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 
<head>
<title>FilExch Admin - Removed Sessions</title>
</head>
 
<body>
 
<h1>FilExch Admin - Removed Sessions</h1>
 
<?php if($ex) { ?>
<p><?= $ex->getMessage() ?></p>
<?php } ?>
 
<p><a href="admin.php">Back to admin</a></p>
 
<?php if(!$removedList) { ?>
<p>No removed sessions.</p>
<?php } else { ?>
<table border=1>
<tr>
<th>Session</th>
<th>Owner</th>
<th>Created</th>
<th>Expire</th>
<th>Size</th>
<th>Count</th>
<th colspan=4>Flags</th>
<th colspan=2>Comments</th>
<th>Active</th>
<th>&nbsp;</th>
</tr>
<?php foreach($removedList as $s) { ?>
<tr>
<td><a href="admin_show.php?s=<?= $f->htmlUrlPart($s->getName()) ?>"><?= $f->string($s->getName()) ?></a></td>
<td><?= $s->getOwner() ?></td>
<td><?= $f->date($s->getCreated(), "&nbsp;") ?></td>
<td><?= $f->date($s->getExpire(), "&nbsp;") ?></td>
<td><?= $f->size($s->getSizeCurrent(), "0 B") ?> / <?= $f->size($s->getSizeLimit(), "-") ?></td>
<td><?= $f->string($s->getCountCurrent(), "0") ?> / <?= $f->string($s->getCountLimit(), "-") ?></td>
<td title="<?= $f->boolean($s->getDownloadable(), "Guest may download files",
"Guest may NOT download files") ?>"><?= $f->boolean($s->getDownloadable(), "D", "&nbsp;") ?></td>
<td title="<?= $f->boolean($s->getUploadable(), "Guest may upload files",
"Guest may NOT upload files") ?>"><?= $f->boolean($s->getUploadable(), "U", "&nbsp;") ?></td>
<td title="<?= $f->boolean($s->getDeletable(), "Guest may delete files",
"Guest may NOT delete files") ?>"><?= $f->boolean($s->getDeletable(), "x", "&nbsp;") ?></td>
<td title="<?= $f->boolean($s->getBlocked(), "Session is blocked",
"Session is not blocked") ?>"><?= $f->boolean($s->getBlocked(), "B", "&nbsp;") ?></td>
<td title="<?= $f->string($s->getPublicComment()) ?>"><?=
$f->stringBegin($s->getPublicComment(), 10) ?></td>
<td title="<?= $f->string($s->getPrivateComment()) ?>"><?=
$f->stringBegin($s->getPrivateComment(), 10) ?></td>
<td><?= $f->boolean($s->isActive(), "x", "&nbsp;") ?></td>
<td nowrap>
<a href="removed.php?restore=<?= $f->htmlUrlPart($s->getId()) ?>">Restore</a>
</td>
</tr>
<?php } ?>
<tr>
<th>Total</th>
<td colspan="3">&nbsp;</td>
<td><?= $f->size($removedSummary['sizeCurrent'], "0 B") ?>
/ <?= $f->size($removedSummary['sizeLimit'], "-") ?></td>
<td><?= $f->string($removedSummary['countCurrent'], "0") ?>
/ <?= $f->string($removedSummary['countLimit'], "-") ?></td>
<td colspan="7">&nbsp;</td>
<td>
<a href="removed.php?purge">Purge All</a>
</td>
</tr>
</table>
<?php } ?>
 
</body>
 
</html>
 
/FileXch/branches/php-impl/pages/manage_display.php
0,0 → 1,148
<?php checkDataCollected(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 
<head>
<title>FilExch - Manage Session</title>
</head>
 
<body>
 
<h1>FilExch - Manage Session</h1>
 
<?php if($s->isActive()) { ?>
<p>Session is active</p>
<?php } else { ?>
<p>Session is NOT active</p>
<?php } ?>
 
<?php if($ex) { ?>
<p><?= $ex->getMessage() ?></p>
<?php } ?>
 
<form method=POST action="manage.php">
<input type=hidden name=id value="<?= $f->integer($s->getId()) ?>">
 
<h2>Settings</h2>
 
<table border=1>
<tr>
<th>Name</th>
<td><?= $f->string($s->getName(), "&nbsp;") ?></td>
</tr>
<tr>
<th>Owner (created by)</th>
<td><?= $f->string($s->getOwner(), "&nbsp;") ?></td>
</tr>
<tr>
<th>Created</th>
<td><?= $f->date($s->getCreated(), "&nbsp;") ?></td>
</tr>
<tr>
<th>Expires</th>
<td><input type=text name=expire value="<?= $f->date($s->getExpire()) ?>"></td>
</tr>
<tr>
<th>Current Size</th>
<td><?= $f->size($s->getSizeCurrent(), "0 B") ?></td>
</tr>
<tr>
<th>Size Limit</th>
<td><input type=text name=size_limit value="<?= $f->size($s->getSizeLimit()) ?>"></td>
</tr>
<tr>
<th>Files Count</th>
<td><?= $f->string($s->getCountCurrent(), "0") ?></td>
</tr>
<tr>
<th>Files Limit</th>
<td><input type=text name=count_limit value="<?= $f->string($s->getCountLimit()) ?>"></td>
</tr>
<tr>
<th>Guest may download</th>
<td><input type=checkbox name=downloadable <?= $f->boolean($s->getDownloadable(), "checked", "") ?>></td>
</tr>
<tr>
<th>Guest may upload</th>
<td><input type=checkbox name=uploadable <?= $f->boolean($s->getUploadable(), "checked", "") ?>></td>
</tr>
<tr>
<th>Guest may delete files</th>
<td><input type=checkbox name=deletable <?= $f->boolean($s->getDeletable(), "checked", "") ?>></td>
</tr>
<tr>
<th>Public comment</th>
<td><textarea name="public_comment"><?= $f->string($s->getPublicComment()) ?></textarea></td>
</tr>
<tr>
<th>Private comment</th>
<td><textarea name="private_comment"><?= $f->string($s->getPrivateComment()) ?></textarea></td>
</tr>
<tr>
<th>Blocked</th>
<td><input type=checkbox name=blocked <?= $f->boolean($s->getBlocked(), "checked", "") ?>></td>
</tr>
</table>
 
<p><input type=submit name="save" value="Save"> <a href="admin.php">Back</a></p>
 
</form>
 
<h2>Current Files</h2>
 
<?php if(!$s->getFileList()) { ?>
<p>No files</p>
<?php } else { ?>
<table border=1>
<tr>
<th>Name</th>
<th>Size</th>
<th>&nbsp;</th>
</tr>
<?php foreach($s->getFileList() as $file) { ?>
<tr>
<td><?= $f->string($file->name) ?></td>
<td><?= $f->size($file->size) ?></td>
<td><a href="download.php?name=<?= $f->htmlUrlPart($s->getName()) ?>/<?=
$f->htmlUrlPart($file->name) ?>">Download</a>
<a href="manage.php?id=<?= $f->integer($s->getId()) ?>&amp;rename=<?=
$f->htmlUrlPart($file->name) ?>">Rename</a>
<a href="manage.php?id=<?= $f->integer($s->getId()) ?>&amp;delete=<?=
$f->htmlUrlPart($file->name) ?>">Delete</a></td>
</tr>
<?php } ?>
</table>
<?php } ?>
 
<h2>Upload New Files</h2>
 
<form method=POST action="upload.php" enctype="multipart/form-data">
<input type=hidden name=id value="<?= $f->integer($s->getId()) ?>">
 
<table border=1>
<tr>
<td>File 1</td>
<td><input name="file[]" type=file></td>
</tr>
<tr>
<td>File 2</td>
<td><input name="file[]" type=file></td>
</tr>
<tr>
<td>File 3</td>
<td><input name="file[]" type=file></td>
</tr>
<tr>
<td>File 4</td>
<td><input name="file[]" type=file></td>
</tr>
</table>
 
<p><input type=submit name="upload" value="Upload"></p>
 
</form>
 
</body>
 
</html>
 
/FileXch/branches/php-impl/sql/tables.sql
0,0 → 1,17
create table sessions (
id serial primary key,
name varchar(16) not null unique key,
owner varchar(16) not null,
created timestamp not null default current_timestamp,
expire timestamp null default null,
size_limit int null default null,
count_limit int null default null,
downloadable bool not null default true,
uploadable bool not null default true,
deletable bool not null default false,
public_comment text null default null,
private_comment text null default null,
blocked bool not null default false,
removed bool not null default false
);
 
/FileXch/branches/php-impl/config/config.php.sample
0,0 → 1,12
<?php
class Config {
const dbType = "mysql";
const dbHost = "localhost";
const dbUser = "filexch";
const dbPass = "password";
const dbDatabase = "filexch";
 
const root = "store";
const umask = 0;
}
?>
/FileXch/branches/php-impl/lib/parser.php
0,0 → 1,104
<?php
class Parser
{
function checkEmpty($s, $nullable, $field)
{
if($nullable) {
return null;
}
else {
throw new Exception("'$field' must have a value");
}
}
 
function string($s)
{
return $s;
}
 
function integer($s, $nullable, $field)
{
if($s !== null) {
$s = trim($s);
}
 
if($s === null || $s === '') {
return $this->checkEmpty($s, $nullable, $field);
}
 
if(!is_numeric($s)) {
throw new Exception("'$field' is not an integer");
}
 
return intval($s);
}
 
function date($s, $nullable, $field)
{
if($s !== null) {
$s = trim($s);
}
 
if($s === null || $s === '') {
return $this->checkEmpty($s, $nullable, $field);
}
 
$d = date_create($s);
 
if(!$d) {
throw new Exception("'$field' is not a time");
}
else {
return $d;
}
}
 
function boolean($s)
{
return ($s ? true : false);
}
 
function size($s, $nullable, $field)
{
if($s !== null) {
$s = trim($s);
}
 
if($s === null || $s === '') {
return $this->checkEmpty($s, $nullable, $field);
}
 
$s = strtoupper($s);
 
// may end with 'B' - remove
if(substr($s, -1) == 'B') {
$s = substr($s, 0, -1);
if($s === '') {
throw new Exception("'$field' is not a size");
}
}
// find and remove unit
$unit = substr($s, -1);
$mul = 1;
switch($unit) {
case 'G': $mul *= 1024;
case 'M': $mul *= 1024;
case 'K': $mul *= 1024;
}
if($mul > 1) {
$s = substr($s, 0, -1);
$s = trim($s);
if($s === '') {
throw new Exception("'$field' is not a size");
}
}
 
if(!is_numeric($s)) {
throw new Exception("'$field' is not a size");
}
return intval($s) * $mul;
}
}
?>
/FileXch/branches/php-impl/lib/file.php
0,0 → 1,178
<?php
class File
{
public $name;
public $size;
}
 
class FileManager
{
private $endOfLife;
private $root;
private $umask;
 
function __construct($root, $umask)
{
$this->endOfLife = false;
$this->root = $root;
$this->umask = $umask;
 
umask($this->umask);
}
 
function endTheLife()
{
$this->checkLiveCycle();
 
$this->endOfLife = true;
}
 
function checkLiveCycle()
{
if($this->endOfLife) {
throw new Exception("I'm die");
}
}
 
function checkFileName($name)
{
if(!$name) {
throw new Exception("Empty name");
}
 
if(strpos($name, '/') !== false) {
throw new Exception("Name contains slash");
}
 
if(substr($s, 0, 1) == '.') {
throw new Exception("Name starts with dot");
}
}
 
function createDir($name)
{
$this->checkLiveCycle();
$this->checkFileName($name);
 
$fullName = "$this->root/$name";
if(!@mkdir($fullName, 0777, true)) {
if(!is_dir($fullName)) { // already exists?
throw new Exception("Cannot create dir");
}
}
}
 
function removeDir($name)
{
$this->checkLiveCycle();
$this->checkFileName($name);
 
$fullName = "$this->root/$name";
if(!@rmdir($fullName)) {
if(file_exists($fullName)) { // already removed?
throw new Exception("Cannot remove dir");
}
}
}
 
static function cmpFiles($a, $b)
{
if($a->name == $b->name) {
return 0;
}
else {
return ($a->name < $b->name ? -1 : 1);
}
}
 
function listDir($name)
{
$this->checkLiveCycle();
$this->checkFileName($name);
 
$res = array();
$fullName = "$this->root/$name";
$d = dir($fullName);
 
while(false !== ($e = $d->read())) {
if($e == '.' || $e == '..') continue;
$s = stat("$fullName/$e");
 
$f = new File();
$f->name = $e;
$f->size = $s[7];
 
array_push($res, $f);
}
 
$d->close();
 
usort($res, "FileManager::cmpFiles");
 
return $res;
}
 
function removeFile($dir, $name)
{
$this->checkLiveCycle();
$this->checkFileName($dir);
$this->checkFileName($name);
 
if(!unlink("$this->root/$dir/$name")) {
throw new Exception("Cannot remove file");
}
}
 
function removeDirWithFiles($dir)
{
$this->checkLiveCycle();
$this->checkFileName($dir);
 
$fullName = "$this->root/$dir";
$d = dir($fullName);
 
while(false !== ($e = $d->read())) {
if($e == '.' || $e == '..') continue;
 
if(!unlink("$fullName/$e")) {
throw new Exception("Cannot remove file");
}
}
 
if(!@rmdir($fullName)) {
throw new Exception("Cannot remove dir");
}
}
 
function moveFile($tmpName, $dir, $name)
{
$this->checkLiveCycle();
$this->checkFileName($dir);
$this->checkFileName($name);
 
$uniqueName = $name;
$counter = 0;
for(;;) {
$fullName = "$this->root/$dir/$uniqueName";
if(!file_exists($fullName)) {
break;
}
 
$uniqueName = $this->generateUniqueName($name, ++$counter);
}
 
move_uploaded_file($tmpName, $fullName);
}
 
function generateUniqueName($name, $counter)
{
$pos = strrpos($name, '.');
if($pos > 0) {
return substr($name, 0, $pos) . "($counter)" . substr($name, $pos);
}
else {
return "$name($counter)";
}
}
}
?>
/FileXch/branches/php-impl/lib/formatter.php
0,0 → 1,96
<?php
class Formatter
{
function integer($s, $def = '0')
{
if($s === null) {
return $def;
}
else if(!is_int($s)) {
throw new Exception("Not an integer");
}
else {
return '' . $s;
}
}
 
function string($s, $def = '')
{
if(!$s) return $def;
 
return htmlspecialchars($s);
}
 
function stringBegin($s, $len, $cont = '...', $def = '')
{
if(!$s) return $def;
 
if(strlen($s) > $len) {
return $this->string(substr($s, 0, $len) . $cont, $def);
}
else {
return $this->string($s, $def);
}
}
 
function urlPart($s, $def = '')
{
if(!$s) return $def;
 
return urlencode($s);
}
 
function htmlUrlPart($s, $def = '')
{
return $this->string($this->urlPart($s, $def));
}
 
function date($d, $def = '')
{
if(!$d) {
return $def;
}
else if($d instanceof DateTime) {
return date_format($d, 'd.m.Y H:i:s');
}
else if(is_string($d)) {
return $this->string($d, $def);
}
else {
return $def;
}
}
 
function size($s, $def = '')
{
if(!$s) {
return $def;
}
else if($s < 1024) {
return $s . ' B';
}
else if($s < 1024 * 1024) {
return round($s / 1024 * 100) / 100 . ' KB';
}
else if($s < 1024 * 1024 * 1024) {
return round($s / 1024 / 1024 * 100) / 100 . ' MB';
}
else {
return round($s / 1024 / 1024 / 1024 * 100) / 100 . ' GB';
}
}
 
function boolean($b, $true, $false, $def = '')
{
if($b === null) {
return $def;
}
else if($b) {
return $true;
}
else {
return $false;
}
}
}
?>
/FileXch/branches/php-impl/lib/session.php
0,0 → 1,535
<?php
class Session
{
private $fm;
private $id;
private $name;
private $owner;
private $created;
private $expire;
private $sizeLimit;
private $sizeCurrent;
private $countLimit;
private $countCurrent;
private $downloadable;
private $uploadable;
private $deletable;
private $publicComment;
private $privateComment;
private $blocked;
private $removed;
private $fileList;
 
function __construct($db, $fm, $dbRow = null)
{
$this->fm = $fm;
 
if($dbRow) {
$this->id = $db->parseInteger($dbRow['id']);
$this->name = $dbRow['name'];
$this->owner = $dbRow['owner'];
$this->created = $db->parseDate($dbRow['created']);
$this->expire = $db->parseDate($dbRow['expire']);
$this->sizeLimit = $db->parseInteger($dbRow['size_limit']);
$this->countLimit = $db->parseInteger($dbRow['count_limit']);
$this->downloadable = $db->parseBoolean($dbRow['downloadable']);
$this->uploadable = $db->parseBoolean($dbRow['uploadable']);
$this->deletable = $db->parseBoolean($dbRow['deletable']);
$this->publicComment = $dbRow['public_comment'];
$this->privateComment = $dbRow['private_comment'];
$this->blocked = $db->parseBoolean($dbRow['blocked']);
$this->removed = $db->parseBoolean($dbRow['removed']);
}
 
if($this->name) {
$this->fm->createDir($this->name);
$this->fileList = $this->fm->listDir($this->name);
}
else {
$this->fileList = array();
}
$this->statFileList();
}
 
function statFileList()
{
$this->countCurrent = count($this->fileList);
$this->sizeCurrent = 0;
foreach($this->fileList as $f) {
$this->sizeCurrent += $f->size;
}
}
 
function getId()
{
return $this->id;
}
 
function getName()
{
return $this->name;
}
 
function setName($name)
{
if($this->name) {
$this->fm->removeDir($this->name);
}
 
$this->name = $name;
 
if($this->name) {
$this->fm->createDir($this->name);
}
}
 
function getOwner()
{
return $this->owner;
}
 
function setOwner($owner)
{
$this->owner = $owner;
}
 
function getCreated()
{
return $this->created;
}
 
function getExpire()
{
return $this->expire;
}
 
function setExpire($expire)
{
$this->expire = $expire;
}
 
function getSizeLimit()
{
return $this->sizeLimit;
}
 
function setSizeLimit($sizeLimit)
{
$this->sizeLimit = $sizeLimit;
}
 
function getSizeCurrent()
{
return $this->sizeCurrent;
}
 
function getCountLimit()
{
return $this->countLimit;
}
 
function setCountLimit($countLimit)
{
$this->countLimit = $countLimit;
}
 
function getCountCurrent()
{
return $this->countCurrent;
}
 
function getDownloadable()
{
return $this->downloadable;
}
 
function setDownloadable($downloadable)
{
$this->downloadable = $downloadable;
}
 
function getUploadable()
{
return $this->uploadable;
}
 
function setUploadable($uploadable)
{
$this->uploadable = $uploadable;
}
 
function getDeletable()
{
return $this->deletable;
}
 
function setDeletable($deletable)
{
$this->deletable = $deletable;
}
 
function getPublicComment()
{
return $this->publicComment;
}
 
function setPublicComment($publicComment)
{
$this->publicComment = $publicComment;
}
 
function getPrivateComment()
{
return $this->privateComment;
}
 
function setPrivateComment($privateComment)
{
$this->privateComment = $privateComment;
}
 
function isExpired()
{
if(!$this->expire) return false;
 
return ($this->expire <= date_create());
}
 
function isBlocked()
{
if(!$this->blocked) return false;
 
return $this->blocked;
}
 
function isActive()
{
return (!$this->isExpired() && !$this->isBlocked());
}
 
function getBlocked()
{
return $this->blocked;
}
 
function setBlocked($blocked)
{
$this->blocked = $blocked;
}
 
function getRemoved()
{
return $this->removed;
}
 
function getFileList()
{
return $this->fileList;
}
 
function removeFile($name)
{
$this->fm->removeFile($this->name, $name);
}
 
function removeAll()
{
$this->fm->removeDirWithFiles($this->name);
}
 
function saveFile($tmpName, $name)
{
$this->fm->moveFile($tmpName, $this->name, $this->prepareSaveFileName($name));
}
 
function prepareSaveFileName($name)
{
// get tail after last (back)slash
$pos1 = strrpos($name, '/');
$pos2 = strrpos($name, '\\');
if($pos !== false && $pos1 < $pos2) {
$pos = $pos2;
}
else {
$pos = $pos1;
}
if($pos !== false) {
$name = substr($name, $pos+1);
}
 
// strip leading dot
if($name != '') {
if(substr($name, 0, 1) == '.') {
$name = substr($name, 1);
}
}
 
return $name;
}
}
 
class SessionManager
{
private $db;
private $fm;
private $endOfLife;
private $alive;
private $removed;
 
function __construct($db, $fm)
{
$this->db = $db;
$this->fm = $fm;
$this->endOfLife = false;
}
 
function endTheLife()
{
$this->checkLiveCycle();
 
$this->endOfLife = true;
}
 
function checkLiveCycle()
{
if($this->endOfLife) {
throw new Exception("I'm die");
}
}
 
function createList($removed)
{
$this->checkLiveCycle();
 
$res = array();
 
$this->db->query("select * from sessions where removed = " . $this->db->formatBoolean($removed)
. " order by created");
while($row = $this->db->fetchRow()) {
array_push($res, new Session($this->db, $this->fm, $row));
}
$this->db->freeResult();
 
return $res;
}
 
function summaryField($current, &$summary)
{
if($summary === null) {
}
else if($current === null) {
$summary = null;
}
else {
$summary += $current;
}
}
 
function summary($list)
{
$res = array("sizeLimit" => 0, "sizeCurrent" => 0, "countLimit" => 0, "countCurrent" => 0);
 
foreach($list as $e) {
$this->summaryField($e->getSizeLimit(), $res["sizeLimit"]);
$this->summaryField($e->getSizeCurrent(), $res["sizeCurrent"]);
$this->summaryField($e->getCountLimit(), $res["countLimit"]);
$this->summaryField($e->getCountCurrent(), $res["countCurrent"]);
}
 
return $res;
}
 
function newSession()
{
$this->checkLiveCycle();
 
$s = new Session($this->db, $this->fm);
 
$s->setOwner($_SERVER["REMOTE_USER"]);
$s->setDownloadable(true);
$s->setUploadable(true);
 
return $s;
}
 
function getSession($id)
{
$this->checkLiveCycle();
 
$s = null;
 
$this->db->query("select * from sessions where id = " . $this->db->formatInteger($id));
if($row = $this->db->fetchRow()) {
$s = new Session($this->db, $this->fm, $row);
}
$this->db->freeResult();
 
if($s === null) {
throw new Exception("Session not found");
}
 
return $s;
}
 
function setNewSessionName($s)
{
$name = null;
$found = true;
while($found) {
$name = $this->generateRandomName();
 
$this->db->query("select id from sessions where name = " . $this->db->formatString($name));
if(!$this->db->fetchRow()) {
$found = false;
}
$this->db->freeResult();
}
$s->setName($name);
}
 
function generateRandomName()
{
$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$max = strlen($chars) - 1;
$name = '';
 
for($i = 0; $i < 16; ++$i) {
$name .= $chars[mt_rand(0, $max)];
}
 
return $name;
}
 
function listAlive()
{
$this->checkLiveCycle();
 
if($this->alive === null) {
$this->alive = $this->createList(false);
}
return $this->alive;
}
 
function summaryAlive()
{
$this->checkLiveCycle();
 
if($this->alive === null) {
$this->alive = $this->createList(false);
}
 
return $this->summary($this->alive);
}
 
function listRemoved()
{
$this->checkLiveCycle();
 
if($this->removed === null) {
$this->removed = $this->createList(true);
}
return $this->removed;
}
 
function summaryRemoved()
{
$this->checkLiveCycle();
 
if($this->removed === null) {
$this->removed = $this->createList(true);
}
 
return $this->summary($this->removed);
}
 
function purge()
{
$this->checkLiveCycle();
 
if($this->removed === null) {
$this->removed = $this->createList(true);
}
$this->db->execute("delete from sessions where removed = true");
 
foreach($this->removed as $s) {
$s->removeAll();
}
 
$this->invalidateLists();
}
 
function remove($id)
{
$this->checkLiveCycle();
 
$this->db->execute("update sessions set removed = true where id = "
. $this->db->formatInteger($id));
 
$this->invalidateLists();
}
 
function restore($id)
{
$this->checkLiveCycle();
 
$this->db->execute("update sessions set removed = false where id = "
. $this->db->formatInteger($id));
 
$this->invalidateLists();
}
 
function insert($s)
{
$this->checkLiveCycle();
 
if(!$s->getName()) {
$this->setNewSessionName($s);
}
 
$this->db->execute("insert into sessions "
. " (name, owner, expire, size_limit, count_limit, downloadable, uploadable,"
. " deletable, public_comment, private_comment, blocked) values ("
. $this->db->formatString($s->getName()) . ", "
. $this->db->formatString($s->getOwner()) . ", "
. $this->db->formatDate($s->getExpire()) . ", "
. $this->db->formatInteger($s->getSizeLimit()) . ", "
. $this->db->formatInteger($s->getCountLimit()) . ", "
. $this->db->formatBoolean($s->getDownloadable()) . ", "
. $this->db->formatBoolean($s->getUploadable()) . ", "
. $this->db->formatBoolean($s->getDeletable()) . ", "
. $this->db->formatString($s->getPublicComment()) . ", "
. $this->db->formatString($s->getPrivateComment()) . ", "
. $this->db->formatBoolean($s->getBlocked()) . ")");
 
$this->invalidateLists();
}
 
function update($s)
{
$this->checkLiveCycle();
 
$this->db->execute("update sessions set"
. " expire = " . $this->db->formatDate($s->getExpire()) . ","
. " size_limit = " . $this->db->formatInteger($s->getSizeLimit()) . ","
. " count_limit = " . $this->db->formatInteger($s->getCountLimit()) . ","
. " downloadable = " . $this->db->formatBoolean($s->getDownloadable()) . ","
. " uploadable = " . $this->db->formatBoolean($s->getUploadable()) . ","
. " deletable = " . $this->db->formatBoolean($s->getDeletable()) . ","
. " public_comment = " . $this->db->formatString($s->getPublicComment()) . ","
. " private_comment = " . $this->db->formatString($s->getPrivateComment()) . ","
. " blocked = " . $this->db->formatBoolean($s->getBlocked())
. " where id = " . $this->db->formatInteger($s->getId()));
 
$this->invalidateLists();
}
 
function invalidateLists()
{
$this->alive = null;
$this->removed = null;
}
}
?>
/FileXch/branches/php-impl/lib/db.php
0,0 → 1,272
<?php
function createDbInterface($type, $host, $user, $password, $database)
{
if($type == "mysql") {
return new DbMySql($host, $user, $password, $database);
}
else {
throw new Exception("Unknown DB type '$type'");
}
}
 
class DbMySql
{
private $endOfLife;
private $host;
private $user;
private $password;
private $database;
private $conn;
private $result;
private $rollbacked;
 
function __construct($host, $user, $password, $database)
{
debugMsg("DbMySql::__construct");
 
$this->endOfLife = false;
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->database = $database;
}
 
function __destruct()
{
debugMsg("DbMySql::__destruct");
 
$this->disconnect();
}
 
function endTheLife()
{
$this->checkLiveCycle();
$this->disconnect();
 
$this->endOfLife = true;
}
 
function checkLiveCycle()
{
if($this->endOfLife) {
throw new Exception("I'm die");
}
}
 
function connect()
{
$this->checkLiveCycle();
 
if(!$this->conn) {
debugMsg("DbMySql::connect");
 
$this->conn = mysql_connect($this->host, $this->user, $this->password);
if(!$this->conn) {
throw new Exception("Cannot connect to MySql database: " . mysql_error());
}
 
mysql_select_db($this->database, $this->conn);
}
}
 
function disconnect()
{
if($this->conn) {
$this->checkLiveCycle();
 
debugMsg("DbMySql::disconnect");
 
if(!$this->rollbacked) {
$this->commit();
}
 
mysql_close($this->conn);
$this->conn = null;
}
}
 
function beginTransaction()
{
$this->execute("begin");
$this->rollbacked = false;
}
 
function commit()
{
$this->execute("commit");
}
 
function rollback()
{
$this->execute("rollback");
$this->rollbacked = true;
}
 
function query($sql)
{
$this->checkLiveCycle();
 
debugMsg("DbMySql::query [$sql]");
$this->connect();
$this->result = mysql_query($sql);
 
if(!$this->result) {
throw new Exception('Cannot execute query: ' . mysql_error() . "\n" . $sql);
}
 
return $this->result;
}
 
function freeResult()
{
$this->checkLiveCycle();
 
debugMsg("DbMySql::freeResult");
 
mysql_free_result($this->result);
}
 
function fetchRow()
{
$this->checkLiveCycle();
 
debugMsg("DbMySql::fetchRow");
 
if($this->result) {
return mysql_fetch_assoc($this->result);
}
else {
return false;
}
}
 
function execute($sql)
{
$this->checkLiveCycle();
 
debugMsg("DbMySql::execute [$sql]");
 
$this->connect();
 
$success = mysql_query($sql);
 
if(!$success) {
throw new Exception('Cannot execute query: ' . mysql_error() . "\n" . $sql);
}
}
 
function escape($s)
{
$this->checkLiveCycle();
 
$this->connect();
 
return mysql_real_escape_string($s);
}
 
function formatString($s, $nullable = true)
{
$this->checkLiveCycle();
 
if($s === null && $nullable) {
return ($nullable ? 'null' : "''");
}
else if(is_string($s)) {
return "'" . $this->escape($s) . "'";
}
else {
throw new Exception("Not a string");
}
}
 
function formatBoolean($b, $nullable = true)
{
$this->checkLiveCycle();
 
if($b === null) {
return ($nullable ? 'null' : 'false');
}
else {
return ($b ? 'true' : 'false');
}
}
 
function formatInteger($i, $def = null)
{
$this->checkLiveCycle();
 
if($i === null) {
if($def === null) {
return 'null';
}
else {
return '' . $def;
}
}
else if(is_int($i)) {
return '' . $i;
}
else {
throw new Exception("Not an integer");
}
}
 
function formatDate($d, $def = null)
{
$this->checkLiveCycle();
 
if($d === null) {
if($def === null) {
return 'null';
}
else {
return "'" . date_format($def, 'Y-m-d H:i:s') . "'";
}
}
else if($d instanceof DateTime) {
return "'" . date_format($d, 'Y-m-d H:i:s') . "'";
}
else {
throw new Exception("Not a date");
}
}
 
function parseInteger($s, $def = null)
{
$this->checkLiveCycle();
 
if($s === null) {
return $def;
}
else {
return intval($s);
}
}
 
function parseBoolean($s, $def = null)
{
$this->checkLiveCycle();
 
if($s === null) {
return $def;
}
else {
return ($s ? true : false);
}
}
 
function parseDate($s, $def = null)
{
$this->checkLiveCycle();
 
if($s === null) {
return $def;
}
else {
return date_create($s);
}
}
 
}
?>
/FileXch/branches/php-impl/lib/common.php
0,0 → 1,56
<?php
if(!$isEntryPoint) {
die("Not an entry point");
}
 
include("config/config.php");
include("lib/db.php");
include("lib/file.php");
include("lib/session.php");
include("lib/formatter.php");
include("lib/parser.php");
 
$dataAllowed = true; // interaction with DB or file system is allowed
 
$db = createDbInterface(Config::dbType, Config::dbHost, Config::dbUser, Config::dbPass, Config::dbDatabase);
$fm = new FileManager(Config::root, Config::umask);
$sm = new SessionManager($db, $fm);
$f = new Formatter();
$p = new Parser();
 
function debugMsg($msg)
{
//echo "$msg<br>\n";
}
 
function dataCollected()
{
global $dataAllowed, $db, $fm, $sm;
 
if($dataAllowed) {
// interaction with DB or file system is not more allowed
$dataAllowed = false;
$db->endTheLife();
$fm->endTheLife();
$sm->endTheLife();
}
}
 
function checkDataCollected()
{
global $dataAllowed;
 
if($dataAllowed) {
throw new Exception("Data collection is still allowed");
}
}
 
function generalError()
{
global $db;
 
$db->rollback();
dataCollected();
include("pages/general_error.php");
}
?>
/FileXch/branches/php-impl/removed.php
0,0 → 1,37
<?php
$isEntryPoint = true;
include("lib/common.php");
 
$redirect = false;
 
try {
// purge all
if($_GET["purge"] !== null) {
$sm->purge();
 
dataCollected();
http_redirect("removed.php");
}
 
// restore a session
else if($id = $_GET["restore"]) {
$id = $p->integer($id, false, "ID");
$sm->restore($id);
 
dataCollected();
http_redirect("removed.php");
}
 
// show the removed lsit
else {
$removedList = $sm->listRemoved();
$removedSummary = $sm->summaryRemoved();
 
dataCollected();
include("pages/removed_list.php");
}
}
catch(Exception $ex) {
generalError();
}
?>