oDb = &$oDb; $this->setDocId(0); $this->setUserWorkshopId($iUserId, $iWorkshopId); } public function setDocId($iDocId) { $this->iDocId = $iDocId; } public function setUserWorkshopId($iUserId, $iWorkshopId) { $this->iUserId = $iUserId; $this->iWorkshopId = $iWorkshopId; } public function exists($sFileName) { return file_exists(self::DOC_FOLDER.$sFileName); } public function add($sFileName, $sMimeType) { $sType = ''; switch(mime_content_type(self::getFilePath($sFileName))) { case 'application/msword': case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': $sType = 'word'; break; case 'application/pdf': $sType = 'pdf'; break; case 'audio/mpeg': $sType = 'audio'; break; case 'image/gif': $sType = 'image'; break; case 'image/jpeg': $sType = 'image'; break; case 'image/png': $sType = 'image'; break; default: switch(pathinfo($sFileName, PATHINFO_EXTENSION)) { case 'mp3': $sType = 'audio'; break; } } $asData = array_merge($this->getDocKeys(), array('type'=>$sType, 'filename'=>$sFileName, 'filehash'=>self::getChecksum($sFileName))); $bResult = $this->oDb->insertRow(self::DOC_TABLE, $asData); return $bResult?'':'error_db'; } public function delete() { if($this->iDocId > 0) { $asDoc = $this->getDoc(); $bResult = $this->oDb->deleteRow(self::DOC_TABLE, $this->iDocId); if($bResult) $bResult = unlink($asDoc['filepath']); } return $bResult; } public function getDoc() { $asDocList = $this->getList(array(Db::getId(self::DOC_TABLE) => $this->iDocId)); return array_shift($asDocList); } public function getList($oOption='self') { if($oOption=='self') $asKeys = $this->getDocKeys(); elseif($oOption=='full') $asKeys = array(); elseif(is_array($oOption)) $asKeys = $oOption; else return array(); $asDocs = $this->oDb->selectRows(array( 'select' => array(Db::getId(self::DOC_TABLE), 'type', 'filename', 'filehash'), 'from' => self::DOC_TABLE, 'constraint'=> $asKeys )); foreach($asDocs as &$asDoc) $asDoc['filepath'] = self::getFilePath($asDoc['filename']); return $asDocs; } public function getMissingFiles() { $asDocs = $this->getList('full'); $asMissingFiles = array(); foreach($asDocs as $asDoc) { $sHash = self::getChecksum($asDoc['filename']); if(!file_exists($asDoc['filepath']) || $sHash!=$asDoc['filehash']) { $asDoc['actual_hash'] = $sHash; $asMissingFiles[] = $asDoc; } } return $asMissingFiles; } private function getDocKeys() { return array(Db::getId(Auth::USER_TABLE) => $this->iUserId, Db::getId(Course::WS_TABLE) => $this->iWorkshopId); } private static function getChecksum($sFileName) { $sFilePath = self::getFilePath($sFileName); return file_exists($sFilePath)?sha1_file($sFilePath):''; } private static function getFilePath($sFileName) { return self::DOC_FOLDER.$sFileName; } }