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; |
} |
} |
?> |