<?PHP
class BSGUpload
{
var $bsgar;
var $webappcfg;
function BSGUpload()
{
global $webappcfg;
$this->webappcfg = $webappcfg;
$this->bsgar = new BSGAuthRight();
}
function uploadFile($_dirname, $_ownerid, $_keypass, $_FILES, $_vartype=FILENAME)
{
$createfile = $this->bsgar->checkDirCreateFile($_dirname, NSession::getuid(),
$_ownerid, $_keypass, $_vartype);
$filedirdb = $this->bsgar->getfiledirdb();
//echo("<PRE>");
//print_r($filedirdb);
$daolsfile = new DAOlsFile();
$filelist = $daolsfile->lsFile($filedirdb);
if ($filedirdb!=null)
{
// Create File Content;
foreach($_FILES as $key=>$value)
{
$updateneed = 0;
for ($i=0; $i<count($filelist); $i++)
{
//$value['name'] = iconv("Big5", "UTF-8", $value['name']);
$tmp = $filelist[$i];
$twfile = $tmp->getobj();
if ($value['name'] == $twfile->getfilename())
{
if (!$this->bsgar->checkDirUpdateFile($_dirname, NSession::getuid(),
$_ownerid, $_keypass, $_vartype))
{
// UPDATE FILE DENIED
return false;
}
else
{
$targeted = $twfile->getfilename();
$updateneed = 1;
}
}
}
if (($updateneed==1) && !($createfile==true))
{
// CREATE FILE DENIED
return false;
}
if ($updateneed == 0)
{
// Get the Auth List
// save in "$tauthlist"
$bsgetdirfilereadaccess = new BSGetDirFileReadAccess();
list($tauthlist) = $bsgetdirfilereadaccess->getAuthList($filedirdb->getid(), NSession::getuid(), DIRECTORY);
$tauthlist = $tauthlist[DIRECTORY];
//print_r($tauthlist);
}
else
{
//echo("FILE OVERWRITE: NOT YET FINISHED");
if ($this->bsgar->checkFileWrite($_dirname . "/" . $targeted, NSession::getuid(),
$_ownerid, $_keypass, $_vartype))
{
$tfiledirdb = $this->bsgar->getfiledirdb();
$tobj = $tfiledirdb->getobj();
$tobj->setfilesize(filesize($value['tmp_name']));
$tfiledirdb->setobj($tobj);
$daoufile = new DAOUpdateFile();
if ($daoufile->updatefile($tfiledirdb))
{
copy($value['tmp_name'], $this->webappcfg['filestorage']."/".$_ownerid."/".$tfiledirdb->getid());
return true;
}
else
return false;
}
else
echo("ACCESSDENIED");
return false;
}
if ($value['error']==1)
{
echo("TMPFILEERROR");
continue;
}
$tufdobj = new Ufd(-1, -1, SFILE, $filedirdb->getid(), 1, $_ownerid, USER);
$dirobj = $filedirdb->getobj();
$tufdobj->setobj(new WFile(-1, $dirobj->getfullpath(), $value['name'], filesize($value['tmp_name']), $value['type'], "", 1, $_keypass));
// Update the Database and retrieve the new id
$daof = new DAONewFile();
$nufdobj = $daof->newFile($tufdobj);
if ($nufdobj!=null)
{
// copy the file to the targetted location
if (!is_dir($this->webappcfg['filestorage']."/".$_ownerid))
{
mkdir($this->webappcfg['filestorage']."/".$_ownerid);
}
if (copy($value['tmp_name'], $this->webappcfg['filestorage']."/".$_ownerid."/".$nufdobj->getid()))
{
// Update the path location
$twfile = $nufdobj->getobj();
$twfile->setlocation($_ownerid."/".$nufdobj->getid());
// Apply Rules
if ($daof->updatePath($twfile))
{
// Add Index
$filearrindex = explode("/", $dirobj->getfullpath()."/".$value['name']);
$daoadi = new DAOAddDirIndex();
if ($daoadi->addindex($filearrindex, $nufdobj->getid()))
{
if ($updateneed==0)
{
$daoaauth = new DAOAddAuth();
if ($daoaauth->addAuth($nufdobj->getid(), $tauthlist, SFILE))
{
$daof->commit();
return true;
}
else
$daof->rollback();
}
else
{
$daof->commit();
return true;
}
}
else
$daof->rollback();
}
else
$daof->rollback();
}
else
{
// Rollback
$daof->rollback();
return false;
}
}
else
{
// include error page
echo("INSERTFAIL");
return;
}
}
}
else
return null;
}
}
?>