diff --git a/application/controllers/lehre/anrechnung/ApproveAnrechnungDetail.php b/application/controllers/lehre/anrechnung/ApproveAnrechnungDetail.php index 28edf4824..1f5c853db 100644 --- a/application/controllers/lehre/anrechnung/ApproveAnrechnungDetail.php +++ b/application/controllers/lehre/anrechnung/ApproveAnrechnungDetail.php @@ -395,8 +395,12 @@ class approveAnrechnungDetail extends Auth_Controller // Set filename to be used on downlaod $filename = $this->anrechnunglib->setFilenameOnDownload($dms_id); + // Get file to be downloaded from DMS + $download = $this->dmslib->download($dms_id, $filename); + if (isError($download)) return $download; + // Download file - $this->dmslib->download($dms_id, $filename); + $this->outputFile(getData($download)); } /** diff --git a/application/controllers/lehre/anrechnung/ApproveAnrechnungUebersicht.php b/application/controllers/lehre/anrechnung/ApproveAnrechnungUebersicht.php index 053988848..20368398a 100644 --- a/application/controllers/lehre/anrechnung/ApproveAnrechnungUebersicht.php +++ b/application/controllers/lehre/anrechnung/ApproveAnrechnungUebersicht.php @@ -8,13 +8,13 @@ class approveAnrechnungUebersicht extends Auth_Controller const BERECHTIGUNG_ANRECHNUNG_ANLEGEN = 'lehre/anrechnung_anlegen'; const REVIEW_ANRECHNUNG_URI = '/lehre/anrechnung/ReviewAnrechnungUebersicht'; - + const ANRECHNUNGSTATUS_PROGRESSED_BY_STGL = 'inProgressDP'; const ANRECHNUNGSTATUS_PROGRESSED_BY_KF = 'inProgressKF'; const ANRECHNUNGSTATUS_PROGRESSED_BY_LEKTOR = 'inProgressLektor'; const ANRECHNUNGSTATUS_APPROVED = 'approved'; const ANRECHNUNGSTATUS_REJECTED = 'rejected'; - + public function __construct() { // Set required permissions @@ -27,23 +27,23 @@ class approveAnrechnungUebersicht extends Auth_Controller 'requestRecommendation' => 'lehre/anrechnung_genehmigen:rw' ) ); - + // Load models $this->load->model('education/Anrechnung_model', 'AnrechnungModel'); $this->load->model('education/Anrechnungstatus_model', 'AnrechnungstatusModel'); $this->load->model('content/DmsVersion_model', 'DmsVersionModel'); - + // Load libraries $this->load->library('WidgetLib'); $this->load->library('PermissionLib'); $this->load->library('AnrechnungLib'); $this->load->library('DmsLib'); - + // Load helpers $this->load->helper('form'); $this->load->helper('url'); $this->load->helper('hlp_sancho_helper'); - + // Load language phrases $this->loadPhrases( array( @@ -55,23 +55,23 @@ class approveAnrechnungUebersicht extends Auth_Controller 'table' ) ); - + $this->_setAuthUID(); - + $this->setControllerId(); } - + public function index() { // Get study semester $studiensemester_kurzbz = $this->input->get('studiensemester'); - + if (isEmptyString($studiensemester_kurzbz)) { $result = $this->StudiensemesterModel->getNearest(); $studiensemester_kurzbz = getData($result)[0]->studiensemester_kurzbz; } - + // Get studiengaenge the user is entitled for if (!$studiengang_kz_arr = $this->permissionlib->getSTG_isEntitledFor(self::BERECHTIGUNG_ANRECHNUNG_GENEHMIGEN)) { @@ -91,10 +91,10 @@ class approveAnrechnungUebersicht extends Auth_Controller 'hasReadOnlyAccess' => $hasReadOnlyAccess, 'hasCreateAnrechnungAccess' => $hasCreateAnrechnungAccess ); - + $this->load->view('lehre/anrechnung/approveAnrechnungUebersicht.php', $viewData); } - + /** * Approve Anrechnungen. */ @@ -107,7 +107,7 @@ class approveAnrechnungUebersicht extends Auth_Controller { return $this->outputJsonError('Fehler beim Übertragen der Daten.'); } - + // Approve Anrechnung foreach ($data as $item) { @@ -120,7 +120,7 @@ class approveAnrechnungUebersicht extends Auth_Controller ); } } - + // Output json to ajax if (isset($json) && !isEmptyArray($json)) { @@ -131,20 +131,20 @@ class approveAnrechnungUebersicht extends Auth_Controller return $this->outputJsonError('Es wurden keine Anrechnungen genehmigt.'); } } - + /** * Reject Anrechnungen. */ public function reject() { $data = $this->input->post('data'); - + // Validate data if (isEmptyArray($data)) { return $this->outputJsonError('Fehler beim Übertragen der Daten.'); } - + // Reject Anrechnung foreach ($data as $item) { @@ -157,7 +157,7 @@ class approveAnrechnungUebersicht extends Auth_Controller ); } } - + // Output json to ajax if (isset($json) && !isEmptyArray($json)) { @@ -168,22 +168,22 @@ class approveAnrechnungUebersicht extends Auth_Controller return $this->outputJsonError('Es wurden keine Anrechnungen genehmigt.'); } } - + /** * Request recommendation for Anrechnungen. */ public function requestRecommendation() { $data = $this->input->post('data'); - + if(isEmptyArray($data)) { return $this->outputJsonError('Fehler beim Übertragen der Daten.'); } - + $retval = array(); $counter = 0; - + foreach ($data as $item) { // Check if Anrechnungs-LV has lector @@ -191,7 +191,7 @@ class approveAnrechnungUebersicht extends Auth_Controller { // Count up LV with no lector $counter++; - + // Continue loop, if LV has no lector continue; } @@ -205,7 +205,7 @@ class approveAnrechnungUebersicht extends Auth_Controller $empfehlungsanfrage_an = !isEmptyArray($lector_arr) ? implode(', ', array_column($lector_arr, 'fullname')) : ''; - + $retval[]= array( 'anrechnung_id' => $item['anrechnung_id'], 'status_kurzbz' => self::ANRECHNUNGSTATUS_PROGRESSED_BY_LEKTOR, @@ -216,7 +216,7 @@ class approveAnrechnungUebersicht extends Auth_Controller ); } } - + /** * Send mails to lectors * NOTE: mails are sent at the end to ensure sending only ONE mail to each LV-Leitung or lector @@ -226,16 +226,16 @@ class approveAnrechnungUebersicht extends Auth_Controller { self::_sendSanchoMailToLectors($retval); } - + // Output json to ajax if (isEmptyArray($retval) && $counter == 0) { return $this->outputJsonError('Es wurden keine Empfehlungen angefordert'); } - + return $this->outputJsonSuccess($retval); } - + /** * Download and open uploaded document (Nachweisdokument). */ @@ -250,25 +250,28 @@ class approveAnrechnungUebersicht extends Auth_Controller // Check if user is entitled to read dms doc $this->_checkIfEntitledToReadDMSDoc($dms_id); - + // Set filename to be used on downlaod $filename = $this->anrechnunglib->setFilenameOnDownload($dms_id); - + + // Get file to be downloaded from DMS + $download = $this->dmslib->download($dms_id, $filename); + if (isError($download)) return $download; + // Download file - $this->dmslib->download($dms_id, $filename); + $this->outputFile(getData($download)); } - - + /** * Retrieve the UID of the logged user and checks if it is valid */ private function _setAuthUID() { $this->_uid = getAuthUID(); - + if (!$this->_uid) show_error('User authentification failed'); } - + /** * Check if user is entitled to read dms doc * @param $dms_id @@ -276,31 +279,30 @@ class approveAnrechnungUebersicht extends Auth_Controller private function _checkIfEntitledToReadDMSDoc($dms_id) { $result = $this->AnrechnungModel->loadWhere(array('dms_id' => $dms_id)); - + if(!$result = getData($result)[0]) { show_error('Failed retrieving Anrechnung'); } - + $result = $this->LehrveranstaltungModel->loadWhere(array( 'lehrveranstaltung_id' => $result->lehrveranstaltung_id )); - - + if(!$result = getData($result)[0]) { show_error('Failed loading Lehrveranstaltung'); } $studiengang_kz = $result->studiengang_kz; - + // Check if user is entitled if (!$this->permissionlib->isBerechtigt(self::BERECHTIGUNG_ANRECHNUNG_GENEHMIGEN, 's', $studiengang_kz)) { show_error('You are not entitled to read this document'); } } - + /** * Send mail to lectors asking for recommendation. (first to LV-Leitung, if not present to all lectors of lv) * @param $mail_params @@ -310,7 +312,7 @@ class approveAnrechnungUebersicht extends Auth_Controller { // Get Lehrveranstaltungen $anrechnung_arr = array(); - + foreach ($mail_params as $item) { $this->AnrechnungModel->addSelect('lehrveranstaltung_id, studiensemester_kurzbz'); @@ -319,10 +321,10 @@ class approveAnrechnungUebersicht extends Auth_Controller 'studiensemester_kurzbz' => $this->AnrechnungModel->load($item['anrechnung_id'])->retval[0]->studiensemester_kurzbz ); } - + $anrechnung_arr = array_unique($anrechnung_arr, SORT_REGULAR); - + /** * Get lectors (prio for LV-Leitung, if not present to all lectors of LV. * Anyway this function will receive a unique array to avoid sending more mails to one and the same lector. @@ -334,27 +336,27 @@ class approveAnrechnungUebersicht extends Auth_Controller { $to = $lector->uid; $vorname = $lector->vorname; - + // Get full name of stgl $this->load->model('person/Person_model', 'PersonModel'); if (!$stgl_name = getData($this->PersonModel->getFullName($this->_uid))) { show_error ('Failed retrieving person'); } - + // Link to Antrag genehmigen $url = CIS_ROOT. 'cis/index.php?menu='. CIS_ROOT. 'cis/menu.php?content_id=&content='. CIS_ROOT. index_page(). self::REVIEW_ANRECHNUNG_URI; - + // Prepare mail content $body_fields = array( 'vorname' => $vorname, 'stgl_name' => $stgl_name, 'link' => anchor($url, 'Anrechnungsanträge Übersicht') ); - + sendSanchoMail( 'AnrechnungEmpfehlungAnfordern', $body_fields, @@ -364,7 +366,7 @@ class approveAnrechnungUebersicht extends Auth_Controller } return true; } - + /** * Get lectors (prio for LV-Leitung, if not present to all lectors of LV. * Anyway this function will receive a unique array to avoid sending more mails to one and the same lector. @@ -374,18 +376,18 @@ class approveAnrechnungUebersicht extends Auth_Controller private function _getLectors($anrechnung_arr) { $lector_arr = array(); - + // Get lectors foreach($anrechnung_arr as $anrechnung) { $this->load->model('education/Lehrveranstaltung_model', 'LehrveranstaltungModel'); $result = $this->LehrveranstaltungModel->getLecturersByLv($anrechnung['studiensemester_kurzbz'], $anrechnung['lehrveranstaltung_id']); - + if (!hasData($result)) { show_error('Failed retrieving lectors of Lehrveranstaltung'); } - + $lecturersByLv = getData($result); // Check if lv has LV-Leitung @@ -401,7 +403,7 @@ class approveAnrechnungUebersicht extends Auth_Controller $lector_arr = array_merge($lector_arr, $lecturersByLv); } } - + /** * NOTE: This step is only done to make the array unique by uid, vorname and nachname in the following step * (e.g. if same lector is ones LV-Leitung and another time not, then array_unique would leave both. @@ -411,10 +413,10 @@ class approveAnrechnungUebersicht extends Auth_Controller { unset($lector->lvleiter); } - + // Now make the lector array aka mail receivers unique $lector_arr = array_unique($lector_arr, SORT_REGULAR); - + return $lector_arr; } -} \ No newline at end of file +} diff --git a/application/controllers/lehre/anrechnung/RequestAnrechnung.php b/application/controllers/lehre/anrechnung/RequestAnrechnung.php index bf98e1c59..bc8ab562e 100644 --- a/application/controllers/lehre/anrechnung/RequestAnrechnung.php +++ b/application/controllers/lehre/anrechnung/RequestAnrechnung.php @@ -205,9 +205,14 @@ class requestAnrechnung extends Auth_Controller } // Check if user is entitled to read dms doc - self::_checkIfEntitledToReadDMSDoc($dms_id); + $this->_checkIfEntitledToReadDMSDoc($dms_id); - $this->dmslib->download($dms_id); + // Get file to be downloaded from DMS + $download = $this->dmslib->download($dms_id, $filename); + if (isError($download)) return $download; + + // Download file + $this->outputFile(getData($download)); } /** @@ -366,4 +371,4 @@ class requestAnrechnung extends Auth_Controller // Upload document return $this->dmslib->upload($dms, 'uploadfile', array('pdf')); } -} \ No newline at end of file +} diff --git a/application/controllers/lehre/anrechnung/ReviewAnrechnungDetail.php b/application/controllers/lehre/anrechnung/ReviewAnrechnungDetail.php index 7981f0ac8..f574e7dfc 100644 --- a/application/controllers/lehre/anrechnung/ReviewAnrechnungDetail.php +++ b/application/controllers/lehre/anrechnung/ReviewAnrechnungDetail.php @@ -217,15 +217,18 @@ class reviewAnrechnungDetail extends Auth_Controller } // Check if user is entitled to read dms doc - self::_checkIfEntitledToReadDMSDoc($dms_id); + $this->_checkIfEntitledToReadDMSDoc($dms_id); // Set filename to be used on downlaod $filename = $this->anrechnunglib->setFilenameOnDownload($dms_id); - // Download file - $this->dmslib->download($dms_id, $filename); - } + // Get file to be downloaded from DMS + $download = $this->dmslib->download($dms_id, $filename); + if (isError($download)) return $download; + // Download file + $this->outputFile(getData($download)); + } /** * Retrieve the UID of the logged user and checks if it is valid diff --git a/application/controllers/lehre/anrechnung/ReviewAnrechnungUebersicht.php b/application/controllers/lehre/anrechnung/ReviewAnrechnungUebersicht.php index e5475d0d0..974b32393 100644 --- a/application/controllers/lehre/anrechnung/ReviewAnrechnungUebersicht.php +++ b/application/controllers/lehre/anrechnung/ReviewAnrechnungUebersicht.php @@ -180,13 +180,17 @@ class reviewAnrechnungUebersicht extends Auth_Controller } // Check if user is entitled to read dms doc - self::_checkIfEntitledToReadDMSDoc($dms_id); + $this->_checkIfEntitledToReadDMSDoc($dms_id); // Set filename to be used on downlaod $filename = $this->anrechnunglib->setFilenameOnDownload($dms_id); + // Get file to be downloaded from DMS + $download = $this->dmslib->download($dms_id, $filename); + if (isError($download)) return $download; + // Download file - $this->dmslib->download($dms_id, $filename); + $this->outputFile(getData($download)); } diff --git a/application/core/FHC_Controller.php b/application/core/FHC_Controller.php index ce8748c5a..c71d5f6d0 100644 --- a/application/core/FHC_Controller.php +++ b/application/core/FHC_Controller.php @@ -121,7 +121,7 @@ abstract class FHC_Controller extends CI_Controller protected function terminateWithJsonError($message) { header('Content-Type: application/json'); - echo json_encode(error($message)); + echo json_encode(error($message)); // KEEP IT!!! exit; } @@ -132,31 +132,41 @@ abstract class FHC_Controller extends CI_Controller { $this->output->set_content_type('application/json')->set_output(json_encode($mixed)); } - + + /** + * To download the given file represented by the fileObj parameter. + * fileObj has the following structure: + * $fileObj->filename + * $fileObj->file + * $fileObj->name + * $fileObj->mimetype + * $fileObj->disposition + */ protected function outputFile($fileObj) { - if (file_exists($fileObj->file)) + // If the file exists + if (isset($fileObj->file) && !isEmptyString($fileObj->file) && file_exists($fileObj->file)) { - $finfo = new finfo(FILEINFO_MIME); - header('Content-Description: File Transfer'); - header('Content-Type: '. $finfo->file($fileObj->file)); + header('Content-Type: '. $fileObj->mimetype); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($fileObj->file)); - - if (isset($fileObj->disposition) && ($fileObj->disposition == 'inline' || $fileObj->disposition == 'attachment')) + + if (isset($fileObj->disposition) + && ($fileObj->disposition == 'inline' || $fileObj->disposition == 'attachment')) { header('Content-Disposition: '. $fileObj->disposition. '; filename="'. $fileObj->name. '"'); } - - readfile($fileObj->file); - - exit; + + readfile($fileObj->file); // reads the file content to the output buffer + } + else + { + // Otherwise print an error + show_error('The provided file does not exist: '.(isset($fileObj->file) ? $fileObj->file : 'file not given')); } - - return false; } //------------------------------------------------------------------------------------------------------------------ diff --git a/application/core/FS_Model.php b/application/core/FS_Model.php index 03a0ee095..0d696d378 100644 --- a/application/core/FS_Model.php +++ b/application/core/FS_Model.php @@ -3,113 +3,238 @@ if (!defined('BASEPATH')) exit('No direct script access allowed'); /** - * + * Model to work with the filesystem, it represents a directory + * It could be extended or could be used directly to work on the given path */ -abstract class FS_Model extends CI_Model +class FS_Model extends CI_Model { - protected $filepath; // Path of the file + const READ_MODE = 'r'; + const READ_WRITE_MODE = 'w+'; + const READ_APPEND_MODE = 'a+'; + const BLOCK_SIZE = 8192; + const META_URI = 'uri'; + + private $_path; // Directory where this model can operate /** - * Loads FilesystemLib and set properties + * Set properties */ - public function __construct($filepath = null) + public function __construct($path) { parent::__construct(); - // Load the filesystem library - $this->load->library('FilesystemLib'); + $this->_path = $path; + } - $this->filepath = $filepath; + // ------------------------------------------------------------------------------------------------------------------ + // Public methods + + /** + * Opens a file in read mode and returns its file handle + */ + public function openRead($filename) + { + return $this->_open($filename, self::READ_MODE); } /** - * Read data from file system - * - * @return array + * Opens a file in read and write mode and returns its file handle + * If the file does not exist then it is created */ - public function read($filename) + public function openReadWrite($filename) { - // Check Class-Attributes - if (is_null($this->filepath)) return error('The given filepath in not valid', EXIT_ERROR); - - // Check method parameters - if (is_null($filename)) return error('The given filename is not valid', EXIT_ERROR); - - if (!is_null($data = $this->filesystemlib->read($this->filepath, $filename))) - { - return success(base64_encode($data)); - } - else - { - return error('An error occurred while reading a file', EXIT_ERROR); - } + return $this->_open($filename, self::READ_WRITE_MODE); } /** - * Writing data to file system - * - * @param string $fileContent File content - * @return object + * Opens a file in read and append mode and returns its file handle + * If the file does not exist then it is created */ - public function write($filename, $content) + public function openReadAppend($filename) { - // Check Class-Attributes - if (is_null($this->filepath)) return error('The given filepath in not valid', EXIT_ERROR); + return $this->_open($filename, self::READ_APPEND_MODE); + } - // Check method parameters - if (is_null($content)) return error('The given file content is not valid', EXIT_ERROR); - if (is_null($filename)) return error('The given filename is not valid', EXIT_ERROR); + /** + * Closes a file handle + */ + public function close($fileHandle) + { + return fclose($fileHandle) === true ? success() : error('Error while closing the file handler'); + } - if ($this->filesystemlib->write($this->filepath, $filename, base64_decode($content)) === true) + /** + * Reads a block of bytes from the given file + * Returns a success that contains the block + * On failure returns an error + */ + public function readBlock($fileHandle) + { + // Reads a block of bytes from the file + $block = fread($fileHandle, self::BLOCK_SIZE); + + // If an error occurred + if ($block === false) + { + // Prepare the error message + $errorMsg = 'An error occurred while reading a file'; + + // Tries to get the file name and to concatenate it to the error message + $fileMetaData = stream_get_meta_data($fileHandle); + if (isset($fileMetaData[self::META_URI])) $errorMsg .= ': '.$fileMetaData[self::META_URI]; + + return error($errorMsg); // returns the error + } + + return success($block); // return success if everything was fine + } + + /** + * Writes/appends (depending on how the file was opened) a content into a file + * Returns a success that contains the written number of bytes + * On failure returns an error + */ + public function write($fileHandle, $content) + { + // Writes the provided content to the file + $writeResult = fwrite($fileHandle, $content); + + // If an error occurred + if ($writeResult === false) + { + $errorMsg = 'An error occurred while writing a file'; + + // Tries to get the file name and to concatenate it to the error message + $fileMetaData = stream_get_meta_data($fileHandle); + if (isset($fileMetaData[self::META_URI])) $errorMsg .= ': '.$fileMetaData[self::META_URI]; + + return error($errorMsg); // returns the error + } + + return success($writeResult); + } + + /** + * Removes a given file + */ + public function remove($filename) + { + // Check if the property _path represents a valid directory + $checkResult = $this->_checkPath(); + + if (isError($checkResult)) return $checkResult; // If not then return the error + + // Check filename + if (isEmptyString($filename)) return error('The given filename is not valid'); + + // remove file + if (unlink($this->_path.DIRECTORY_SEPARATOR.$filename) === true) { return success(); } else { - return error('An error occurred while writing a file', EXIT_ERROR); + return error('An error occurred while removing a file'); } } + // ------------------------------------------------------------------------------------------------------------------ + // Old public methods that work with the base64 encoding, not to be used! + /** - * Append data to a file - * - * @param array $data File content - * @return array + * Read data from the given file and encode its content to base64 */ - public function append($filename, $content) + public function readBase64($filename) { - // Check Class-Attributes - if (is_null($this->filepath)) return error('The given filepath in not valid', EXIT_ERROR); + // Open the file in read mode + $openReadResult = $this->openRead($filename); + if (isError($openReadResult)) return $openReadResult; // if an error occurred then return it - // Check method parameters - if (is_null($content)) return error('The given file content is not valid', EXIT_ERROR); - if (is_null($filename)) return error('The given filename is not valid', EXIT_ERROR); + $fileContent = ''; // to store the file content + $fileHandle = getData($openReadResult); // get the file handle - if ($this->filesystemlib->append($this->filepath, $filename, base64_decode($content)) === true) + // While the end of the file is not reached and the read does not fail + while (!feof($fileHandle) && isSuccess($readBlockResult = $this->readBlock($fileHandle))) { - return success(); - } - else - { - return error('An error occurred while appending to a file', EXIT_ERROR); + // Concatenate the content of the file + $fileContent .= getData($readBlockResult); } + + // If an error occurred while reading then return it + if (isError($readBlockResult)) return $readBlockResult; + + // Close the file handler + $closeResult = $this->close($fileHandle); + if (isError($closeResult)) return $closeResult; // if it fails then return the error + + // If everything was fine encode the file content into base64 and return it as a success + return success(base64_encode($fileContent)); + } + + /** + * Writes the given content into the given file. The content is base64 encoded + */ + public function writeBase64($filename, $content) + { + // Open the file in read and write mode + $openWriteResult = $this->openReadWrite($filename); + if (isError($openWriteResult)) return $openWriteResult; // if an error occurred then return it + + $fileHandle = getData($openWriteResult); // get the file handle + + // Writes the given base64 encoded content into to given file + $writeResult = $this->write($fileHandle, base64_decode($content)); + // If an error occurred while writing then return it + if (isError($writeResult)) return $writeResult; + + // Close the file handler + $closeResult = $this->close($fileHandle); + if (isError($closeResult)) return $closeResult; // if it fails then return the error + + // If everything was fine + return success(); + } + + /** + * Appends the given content into the given file. The content is base64 encoded + */ + public function appendBase64($filename, $content) + { + // Open the file in read and append mode + $openWriteResult = $this->openReadAppend($filename); + if (isError($openWriteResult)) return $openWriteResult; // if an error occurred then return it + + $fileHandle = getData($openWriteResult); // get the file handle + + // Writes the given base64 encoded content into to given file + $writeResult = $this->write($fileHandle, base64_decode($content)); + // If an error occurred while writing then return it + if (isError($writeResult)) return $writeResult; + + // Close the file handler + $closeResult = $this->close($fileHandle); + if (isError($closeResult)) return $closeResult; // if it fails then return the error + + // If everything was fine + return success(); } /** * Delete data from file system + * NOTE: it does not work with the base64 encoding but it has been kept for retro compatibility * * @param string $id Primary Key for DELETE * @return array */ - public function remove($filename) + public function removeBase64($filename) { // Check Class-Attributes - if (is_null($this->filepath)) return error('The given filepath in not valid', EXIT_ERROR); + if (is_null($this->_path)) return error('The given _path in not valid', EXIT_ERROR); // Check method parameters if (is_null($filename)) return error('The given filename is not valid', EXIT_ERROR); - if ($this->filesystemlib->remove($this->filepath, $filename) === true) + if (unlink($this->_path.DIRECTORY_SEPARATOR.$filename) === true) { return success(); } @@ -121,20 +246,21 @@ abstract class FS_Model extends CI_Model /** * Rename a file + * NOTE: it does not work with the base64 encoding but it has been kept for retro compatibility * * @param string $id Primary Key for DELETE * @return array */ - public function rename($filename, $newFilename) + public function renameBase64($filename, $newFilename) { // Check Class-Attributes - if (is_null($this->filepath)) return error('The given filepath in not valid', EXIT_ERROR); + if (is_null($this->_path)) return error('The given _path in not valid', EXIT_ERROR); // Check method parameters if (is_null($filename)) return error('The given filename is not valid', EXIT_ERROR); if (is_null($newFilename)) return error('The given new filename is not valid', EXIT_ERROR); - if ($this->filesystemlib->rename($this->filepath, $filename, $this->filepath, $newFilename) === true) + if (rename($this->_path.DIRECTORY_SEPARATOR.$filename, $this->_path.DIRECTORY_SEPARATOR.$newFilename) === true) { return success(); } @@ -143,4 +269,67 @@ abstract class FS_Model extends CI_Model return error('An error occurred while renaming a file', EXIT_ERROR); } } + + // ------------------------------------------------------------------------------------------------------------------ + // Private methods + + /** + * Checks if the given $this->_path is a valid directory + */ + private function _checkPath() + { + // If _path... + if (!isEmptyString($this->_path) // ...is a not empty string... + && file_exists($this->_path) && is_dir($this->_path)) // ...exists on the file system and it is a directory... + { + return success(); // return a success + } + + // If not a valid path return an error + return error('The given path is not valid: '.$this->_path); + } + + /** + * Open a file using the provided mode + * It returns a file handle + * Or write and append if the file does not exist then creates it + */ + private function _open($filename, $mode) + { + // Check if the property _path represents a valid directory + $checkResult = $this->_checkPath(); + if (isError($checkResult)) return $checkResult; // If not then return the error + + // Full file path: path + filename + $fileFullPath = $this->_path.DIRECTORY_SEPARATOR.$filename; + + // If needed then check if the file exists and really it is a file + if ($mode == self::READ_MODE && (!file_exists($fileFullPath) || !is_file($fileFullPath))) + { + return error('Trying to read a not existing file'); + } + + // If needed then check if it is possible to read from the path and the file + if ($mode == self::READ_MODE && (!is_readable($this->_path) || !is_readable($fileFullPath))) + { + return error('The given path or filename are not readable: '.$fileFullPath); + } + + // If needed then check if the path and the filename are writable + if (($mode == self::READ_WRITE_MODE || $mode == self::READ_APPEND_MODE) + && (!is_writable($this->_path) || (file_exists($fileFullPath) && !is_writable($fileFullPath)))) + { + return error('The given path or filename are not writable: '.$fileFullPath); + } + + // Open the file in read mode + $fileHandle = fopen($fileFullPath, $mode); + + // If it was a failure the return the error + if ($fileHandle === false) return error('An error occurred while opening a file in '.$mode.' mode'); + + // Otherwise return the file handle + return success($fileHandle); + } } + diff --git a/application/libraries/AkteLib.php b/application/libraries/AkteLib.php new file mode 100644 index 000000000..c4aee0095 --- /dev/null +++ b/application/libraries/AkteLib.php @@ -0,0 +1,268 @@ +_ci =& get_instance(); + + // Set the the _who property + $this->_who = 'Akte system'; // default + // It is possible to set it using the who parameter + if (!isEmptyArray($params) && isset($params['who']) && !isEmptyString($params['who'])) $this->_who = $params['who']; + + $this->_ci->load->model('crm/Akte_model', 'AkteModel'); + $this->_ci->load->model('content/DmsFS_model', 'DmsFSModel'); + + $this->_ci->load->library('DmsLib'); + } + + /** + * Writes a new file, adds a new dms entry with given akte data, + * adds a new dms version 0 for the written file, and adds Akte if dms add was successfull + * Returns success with inserted akte id or error + */ + public function add( + $person_id, $dokument_kurzbz, $titel, $mimetype, $fileHandle, // Required parameters + $bezeichnung = null, $archiv = false, $signiert = false, $stud_selfservice = false + ) + { + // add new dms entry and new dms version for the Akte, using Akte data (title, mimetype, file content as handle) + $dmsAddResult = $this->_ci->dmslib->add($titel, $mimetype, $fileHandle, self::AKTE_KATEGORIE_KURZBZ, $dokument_kurzbz, $bezeichnung); + + if (isError($dmsAddResult)) return $dmsAddResult; + + if (!hasData($dmsAddResult)) return error("Dms document could not be added"); + + $dmsAddData = getData($dmsAddResult); + + // insert the Akte + return $this->_ci->AkteModel->insert( + array( + 'person_id' => $person_id, + 'dokument_kurzbz' => $dokument_kurzbz, + 'titel' => $titel, + 'mimetype' => $mimetype, + 'bezeichnung' => $bezeichnung, + 'erstelltam' => date('Y-m-d'), + 'dms_id' => $dmsAddData->dms_id, + 'archiv' => $archiv, + 'signiert' => $signiert, + 'stud_selfservice' => $stud_selfservice, + 'insertamum' => date('Y-m-d H:i:s'), + 'insertvon' => $this->_who + ) + ); + } + + /** + * Writes a new file, adds a new dms version 0 for the written file, and updates Akte if dms version add was successfull + * Returns success with updated akte id or error + */ + public function update($akte_id, $titel, $mimetype, $fileHandle, $bezeichnung = null, $archiv = false, $signiert = false, $stud_selfservice = false) + { + // check if Akte with dms exists + $this->_ci->AkteModel->addSelect('dms_id'); + $akteResult = $this->_ci->AkteModel->load($akte_id); + + if (isError($akteResult)) return $akteResult; + + if (!hasData($akteResult)) return error("Akte not found"); + + $dms_id = getData($akteResult)[0]->dms_id; + + if (isEmptyString($dms_id)) return error("Akte has no dms document"); + + // if Akte with dms found, update the last dms version + $dmsUpdateResult = $this->_ci->dmslib->updateLastVersion($dms_id, $fileHandle, $titel, $mimetype, $bezeichnung); + + if (isError($dmsUpdateResult)) return $dmsUpdateResult; + + if (!hasData($dmsUpdateResult)) return error("Dms document could not be updated"); + + // update the Akte + return $this->_ci->AkteModel->update( + $akte_id, + array( + 'titel' => $titel, + 'mimetype' => $mimetype, + 'bezeichnung' => $bezeichnung, + 'erstelltam' => date('Y-m-d'), + 'dms_id' => $dms_id, + 'archiv' => $archiv, + 'signiert' => $signiert, + 'stud_selfservice' => $stud_selfservice, + 'updateamum' => date('Y-m-d H:i:s'), + 'updatevon' => $this->_who + ) + ); + } + + /** + * Gets akte data and associated dms data by akte Id + * Returns success with akte and dms data or error + */ + public function get($akte_id) + { + // get Akte data + $this->_ci->AkteModel->addSelect('person_id, dokument_kurzbz, mimetype, erstelltam, titel, bezeichnung, + gedruckt, uid, dms_id, nachgereicht, nachgereicht_am, anmerkung, + ausstellungsnation, formal_geprueft_amum, archiv, signiert, + stud_selfservice, akzeptiertamum, insertvon, insertamum, updatevon, updateamum'); + $this->_ci->AkteModel->load($akte_id); + $akteResult = $this->_ci->AkteModel->load($akte_id); + + if (isError($akteResult)) return $akteResult; + + if (!hasData($akteResult)) return error("Akte not found"); + + $resultObject = getData($akteResult)[0]; + + // set properties with same name in Akte and Dms table + $resultObject->akte_mimetype = $resultObject->mimetype; + + // get dms data + $dmsResult = $this->_ci->dmslib->getLastVersion($resultObject->dms_id); + + if (isError($dmsResult)) return $dmsResult; + + // properties to retrieve from dms + $dmsProperties = array('version', 'filename', 'mimetype', 'name', 'beschreibung', 'cis_suche', 'schlagworte', DmsLib::FILE_CONTENT_PROPERTY); + + // set dms properties + if (hasData($dmsResult)) + { + $dmsData = getData($dmsResult); + + foreach ($dmsProperties as $dmsProperty) + { + $resultObject->{$dmsProperty} = $dmsData->{$dmsProperty}; + } + } + else + { + // set null if no dms result found + foreach ($dmsProperties as $dmsProperty) + { + $resultObject->{$dmsProperty} = null; + } + } + + // return the object containing akte and dms data + return success($resultObject); + } + + /** + * Gets Akte data and associated dms data by person Id and dokument_kurzbz + * Returns success with result array with akte and dms data or error + */ + public function getByPersonIdAndDocumentType($person_id, $dokument_kurzbz) + { + // load all Akte entries for given person and dokument_kurzbz + $this->_ci->AkteModel->addSelect('akte_id'); + $akteResult = $this->_ci->AkteModel->loadWhere( + array( + 'person_id' => $person_id, + 'dokument_kurzbz' => $dokument_kurzbz + ) + ); + + if (!hasData($akteResult)) return error("Akte not found"); + + $akteData = getData($akteResult); + + $resultArr = array(); + + // for each found akte entry + foreach ($akteData as $akte) + { + // get dms and akte data from akte Id + $getAkteDmsResult = $this->get($akte->akte_id); + + if (isError($getAkteDmsResult)) return $getAkteDmsResult; + + $resultArr[] = getData($getAkteDmsResult); + } + + // return all found entries + return success($resultArr); + } + + /** + * Removes Akte by akte Id, removes all associated dms entries and versions, and deletes all associated files + * Returns success with removed version numbers or error + */ + public function remove($akte_id) + { + // get dms_id for akte + $this->_ci->AkteModel->addSelect('dms_id'); + $akteResult = $this->_ci->AkteModel->load($akte_id); + + if (isError($akteResult)) return $akteResult; + + if (!hasData($akteResult)) return error("Akte not found"); + + $dms_id = getData($akteResult)[0]->dms_id; + $error = null; + + // Start DB transaction to avoid deleting only part of the data + $this->_ci->db->trans_begin(); + + // delete Akte + $deleteResult = $this->_ci->AkteModel->delete($akte_id); + + if (isError($deleteResult)) + { + $error = $deleteResult; + } + else + { + // remove all dms entry for dms of the akte + $removeAllResult = $this->_ci->dmslib->removeAll($dms_id); + + if (isError($removeAllResult)) + $error = $removeAllResult; + } + + // Transaction complete! + $this->_ci->db->trans_complete(); + + // Check if everything went ok during the transaction + if ($this->_ci->db->trans_status() === false || isset($error)) + { + $this->_ci->db->trans_rollback(); + + // return occured error + if (isset($error)) + return $error; + else + return error("Error occured when deleting, rolled back"); + } + else + { + $this->_ci->db->trans_commit(); + + // return removed dms entry data + return $removeAllResult; + } + } +} diff --git a/application/libraries/DmsLib.php b/application/libraries/DmsLib.php index eae1a9ac4..b2c53a7d9 100644 --- a/application/libraries/DmsLib.php +++ b/application/libraries/DmsLib.php @@ -1,25 +1,532 @@ ci =& get_instance(); + $this->_ci =& get_instance(); - $this->ci->load->model('crm/Akte_model', 'AkteModel'); - $this->ci->load->model('content/Dms_model', 'DmsModel'); - $this->ci->load->model('content/DmsVersion_model', 'DmsVersionModel'); - $this->ci->load->model('content/DmsFS_model', 'DmsFSModel'); + // Set the the _who property + $this->_who = 'DMS system'; // default + // It is possible to set it using the who parameter + if (!isEmptyArray($params) && isset($params['who']) && !isEmptyString($params['who'])) $this->_who = $params['who']; + + $this->_ci->load->model('crm/Akte_model', 'AkteModel'); // deprecated, should not be used here! + $this->_ci->load->model('content/Dms_model', 'DmsModel'); + $this->_ci->load->model('content/DmsVersion_model', 'DmsVersionModel'); + $this->_ci->load->model('content/DmsFS_model', 'DmsFSModel'); } + + // ----------------------------------------------------------------------------------------------------------- + // Public methods + + /** + * Writes a new file, adds a new dms entry and a new dms version 0 for the written file + * Returns success info of added dms entry (dms_id, version, filename) or error + */ + public function add( + $name, $mimetype, $fileHandle, // Required parameters + $kategorie_kurzbz = null, $dokument_kurzbz = null, $beschreibung = null, $cis_suche = false, $schlagworte = null + ) + { + // write file with content of fileHandle + $writeFileResult = $this->_writeNewFile($name, $fileHandle); + + if (isError($writeFileResult)) return $writeFileResult; + + if (hasData($writeFileResult)) + { + $writeFileData = getData($writeFileResult); + $filename = $writeFileData->filename; + + // if file written successful, insert dms + $dmsResult = $this->_ci->DmsModel->insert( + array( + 'kategorie_kurzbz' => $kategorie_kurzbz, + 'dokument_kurzbz' => $dokument_kurzbz + ) + ); + + if (isError($dmsResult)) return $dmsResult; + + if (hasData($dmsResult)) + { + $dms_id = getData($dmsResult); + $version = 0; + + // insert dms version + $dmsVersion = array( + 'dms_id' => $dms_id, + 'version' => $version, + 'filename' => $filename, + 'mimetype' => $mimetype, + 'name' => $name, + 'beschreibung' => $beschreibung, + 'cis_suche' => $cis_suche, + 'schlagworte' => $schlagworte, + 'insertvon' => $this->_who, + 'insertamum' => date('Y-m-d H:i:s') + ); + + $dmsVersionResult = $this->_ci->DmsVersionModel->insert($dmsVersion); + + if (isError($dmsVersionResult)) return $dmsVersionResult; + + // return dms info + $resObj = new stdClass(); + $resObj->dms_id = $dms_id; + $resObj->version = $version; + $resObj->filename = $filename; + + return success($resObj); + } + else + return error("error when inserting DMS"); + } + else + return error("file could not be written"); + } + + /** + * Writes a new file with content of fileHandle, adds a new dms version (max version number + 1) for the written file + * Returns success with info of added dms version (version, filename) or error + */ + public function addNewVersion($dms_id, $fileHandle, $name = null, $mimetype = null, $beschreibung = null, $cis_suche = false, $schlagworte = null) + { + // get the latest version + $lastVersionResult = $this->getLastVersion($dms_id); + + if (isError($lastVersionResult)) return $lastVersionResult; + + if (hasData($lastVersionResult)) + { + $lastVersion = getData($lastVersionResult); + + $originalName = isset($name) ? $name : $lastVersion->name; + + // write new file with content of fileHandle + $writeFileResult = $this->_writeNewFile($originalName, $fileHandle); + + if (isError($writeFileResult)) return $writeFileResult; + + if (hasData($writeFileResult)) + { + $writeFileData = getData($writeFileResult); + $filename = $writeFileData->filename; + + // insert new version + $newVersionNumber = $lastVersion->version + 1; + + // if new parameters given, use them, otherwise use parameters from last version + $newVersion = array( + 'dms_id' => $dms_id, + 'name' => $originalName, + 'filename' => $filename, + 'version' => $newVersionNumber, + 'mimetype' => isset($mimetype) ? $mimetype : $lastVersion->mimetype, + 'beschreibung' => isset($beschreibung) ? $beschreibung : $lastVersion->beschreibung, + 'cis_suche' => isset($cis_suche) ? $cis_suche : $lastVersion->cis_suche, + 'schlagworte' => isset($schlagworte) ? $schlagworte : $lastVersion->schlagworte, + 'insertvon' => $this->_who, + 'insertamum' => date('Y-m-d H:i:s') + ); + + $addVersionResult = $this->_ci->DmsVersionModel->insert($newVersion); + + if (isError($addVersionResult)) return $addVersionResult; + + // return dms info + $resObj = new stdClass(); + $resObj->version = $newVersionNumber; + $resObj->filename = $filename; + + return success($resObj); + } + else + return error("file could not be written"); + } + else + return error("last version not found"); + } + + /** + * Updates the last version (max version number) of a dms entry + * Overwrites the file associated with this version with content read from fileHandle + * Returns success with info of added dms version (version, filename) or error + */ + public function updateLastVersion($dms_id, $fileHandle, $name = null, $mimetype = null, $beschreibung = null, $cis_suche = false, $schlagworte = null) + { + // get the latest version + $lastVersionResult = $this->getLastVersion($dms_id); + + if (isError($lastVersionResult)) return $lastVersionResult; + + if (hasData($lastVersionResult)) + { + $lastVersion = getData($lastVersionResult); + + // update file in filesystem + $writeFileResult = $this->_writeFile($lastVersion->filename, $fileHandle); + + if (isError($writeFileResult)) return $writeFileResult; + + if (hasData($writeFileResult)) + { + $writeFileData = getData($writeFileResult); + $filename = $writeFileData->filename; + + // if new parameters given, use them, otherwise use parameters from last version + $newVersion = array( + 'name' => isset($name) ? $name : $lastVersion->name, + 'filename' => $filename, + 'mimetype' => isset($mimetype) ? $mimetype : $lastVersion->mimetype, + 'beschreibung' => isset($beschreibung) ? $beschreibung : $lastVersion->beschreibung, + 'cis_suche' => isset($cis_suche) ? $cis_suche : $lastVersion->cis_suche, + 'schlagworte' => isset($schlagworte) ? $schlagworte : $lastVersion->schlagworte, + ); + + // update last dms version + $addVersionResult = $this->_ci->DmsVersionModel->update( + array($dms_id, $lastVersion->version), + $newVersion + ); + + if (isError($addVersionResult)) return $addVersionResult; + + // return dms info + $resObj = new stdClass(); + $resObj->version = $lastVersion->version; + $resObj->filename = $filename; + + return success($resObj); + } + else + return error("file could not be written"); + } + else + return error("last version not found"); + } + + /** + * Gets dms version with highest number + * Returns success with dms data and fileHandle with file content or error + */ + public function getLastVersion($dms_id) + { + // get the latest version number + $this->_ci->DmsVersionModel->addSelect('version'); + $this->_ci->DmsVersionModel->addOrder('version', 'DESC'); + $this->_ci->DmsVersionModel->addOrder('insertamum', 'DESC'); + $this->_ci->DmsVersionModel->addLimit(1); + $lastDmsVersionResult = $this->_ci->DmsVersionModel->loadWhere( + array('dms_id' => $dms_id) + ); + + if (isError($lastDmsVersionResult)) return $lastDmsVersionResult; + + if (hasData($lastDmsVersionResult)) + { + $lastDmsVersionData = getData($lastDmsVersionResult)[0]; + $lastDmsVersion = $lastDmsVersionData->version; + + // call get Version with last version number + return $this->getVersion($dms_id, $lastDmsVersion); + } + else + return error("Dms last version not found"); + } + + /** + * Gets specified dms version + * Returns success with dms data and fileHandle with file content or error + */ + public function getVersion($dms_id, $version) + { + $this->_ci->DmsVersionModel->addSelect('dms_id, version, filename, mimetype, name, beschreibung, cis_suche, schlagworte'); + $dmsVersionResult = $this->_ci->DmsVersionModel->loadWhere( + array( + 'dms_id' => $dms_id, + 'version' => $version + ) + ); + + if (isError($dmsVersionResult)) return $dmsVersionResult; + + if (hasData($dmsVersionResult)) + { + $dmsVersion = getData($dmsVersionResult)[0]; + + // get file content as file pointer + $fileHandleResult = $this->_ci->DmsFSModel->openRead($dmsVersion->filename); + + if (isError($fileHandleResult)) return $fileHandleResult; + + if (hasData($fileHandleResult)) + { + $fileHandle = getData($fileHandleResult); + $dmsVersion->{self::FILE_CONTENT_PROPERTY} = $fileHandle; + + // close file pointer + $closeResult = $this->_ci->DmsFSModel->close($fileHandle); + + if (isError($closeResult)) return $closeResult; + + return success($dmsVersion); + } + else + return error("File could not be opened"); + } + else + return error("Dms version not found"); + } + + /** + * Removes dms entry and all its versions, deletes all associated files + * Returns success with removed version numbers or error + */ + public function removeAll($dms_id) + { + $versionsRemoved = array(); + + $this->_ci->DmsVersionModel->addSelect('version, filename'); + $allVersionsResult = $this->_ci->DmsVersionModel->loadWhere(array('dms_id' => $dms_id)); + + if (hasData($allVersionsResult)) + { + $allVersionsData = getData($allVersionsResult); + + $error = null; + + // Start DB transaction to avoid deleting only part of the data + $this->_ci->db->trans_begin(); + + // remove all versions of the dms Id + foreach ($allVersionsData as $version) + { + $removeVersionResult = $this->removeVersion($dms_id, $version->version); + + if (isError($removeVersionResult)) + { + $error = $removeVersionResult; + break; + } + else + $versionsRemoved[] = $version; // return removed versions + } + + // Transaction complete! + $this->_ci->db->trans_complete(); + + // Check if everything went ok during the transaction + if ($this->_ci->db->trans_status() === false || isset($error)) + { + $this->_ci->db->trans_rollback(); + + if (isset($error)) + return $error; + else + return error("Error occured when deleting, rolled back"); + } + else + { + $this->_ci->db->trans_commit(); + } + } + + return success($versionsRemoved); + } + + /** + * Removes latest version and its associated file + * Returns success with removed dms version data (dms_id, version, filename) or error + */ + public function removeLastVersion($dms_id) + { + $lastVersionNumber = 0; + // get the latest version + $lastVersionResult = $this->getLastVersion($dms_id); + + if (isError($lastVersionResult)) return $lastVersionResult; + + if (hasData($lastVersionResult)) + { + $lastVersion = getData($lastVersionResult); + $lastVersionNumber = $lastVersion->version; + } + + // call remove method for latest version + return $this->removeVersion($dms_id, $lastVersionNumber); + } + + /** + * Removes latest version and its associated file + * Returns success with removed dms version data (dms_id, version, filename) or error + */ + public function removeVersion($dms_id, $version) + { + $removeVersionResultObj = new stdClass(); + $removeVersionResultObj->dms_id = null; + $removeVersionResultObj->version = null; + $removeVersionResultObj->filename = null; + + // load dms version and check how many versions there are + $db = new DB_Model(); + + $checkDeleteResult = $db->execReadOnlyQuery( + "SELECT filename, + (SELECT count(version) + FROM campus.tbl_dms_version dv_anzahl + WHERE dv_anzahl.dms_id = dv.dms_id) AS anzahl_versionen + FROM campus.tbl_dms_version dv + WHERE dms_id=? + AND version=?", + array($dms_id, $version) + ); + + if (isError($checkDeleteResult)) return $checkDeleteResult; + + if (hasData($checkDeleteResult)) + { + $checkDeleteData = getData($checkDeleteResult)[0]; + + // delete version + $deleteVersionResult = $this->_ci->DmsVersionModel->delete(array($dms_id, $version)); + + if (isError($deleteVersionResult)) return $deleteVersionResult; + + $removeVersionResultObj->version = $version; + $removeVersionResultObj->filename = $checkDeleteData->filename; + + // delete dms too if no versions left + if ($checkDeleteData->anzahl_versionen <= 1) + { + $deleteDmsResult = $this->_ci->DmsModel->delete($dms_id); + + if (isError($deleteDmsResult)) return $deleteDmsResult; + + $removeVersionResultObj->dms_id = $dms_id; + } + + // delete file from file system + $removeResult = $this->_ci->DmsFSModel->remove($checkDeleteData->filename); + + if (isError($removeResult)) return $removeResult; + } + + return success($removeVersionResultObj); + } + + // ----------------------------------------------------------------------------------------------------------- + // Private methods + + /** + * Writes file with content of fileHandle using original document name for file extension + */ + private function _writeNewFile($originalName, $fileHandle) + { + // create unique filename, using original document name to detect file extension + $filename = $this->_getUniqueFilename($originalName); + + // write the file + return $this->_writeFile($filename, $fileHandle); + } + + /** + * Writes file with content of fileHandle + * Returns number of bytes written and filename + */ + private function _writeFile($filename, $fileHandle) + { + // file content provided by fileHandle + $fileContent = ''; + + $readBlockResult = success(); + + // While the end of the file is not reached and the read does not fail + while (!feof($fileHandle) && isSuccess($readBlockResult = $this->_ci->DmsFSModel->readBlock($fileHandle))) + { + // Concatenate the content of the file + $fileContent .= getData($readBlockResult); + } + + // If an error occurred while reading then return it + if (isError($readBlockResult)) return $readBlockResult; + + // open file for writing + $openFileResult = $this->_ci->DmsFSModel->openReadWrite($filename); + + if (isError($openFileResult)) return $openFileResult; + + if (!hasData($openFileResult)) return error("File could not be opened"); + + $newFileHandle = getData($openFileResult); + + // write file + $writeFileResult = $this->_ci->DmsFSModel->write($newFileHandle, $fileContent); + + if (isError($writeFileResult)) return $writeFileResult; + + // return number of bytes written and filename + $resObj = new stdClass(); + $resObj->bytesWritten = 0; + $resObj->filename = ''; + + if (hasData($writeFileResult)) + { + $resObj->bytesWritten = getData($writeFileResult); + $resObj->filename = $filename; + } + + // close handle + $closeResult = $this->_ci->DmsFSModel->close($newFileHandle, $fileContent); + + if (isError($closeResult)) return $closeResult; + + return success($resObj); + } + + /** + * Generates unique filename, appends file extension from document name + * Returns the filename string + */ + private function _getUniqueFilename($dokname) + { + // create unique id + $uniqueFilename = uniqid(); + + // getting extension of file from document name + $fileExtension = pathinfo($dokname, PATHINFO_EXTENSION); + + // if file extension found, append it + if (!isEmptyString($fileExtension)) + $uniqueFilename .= '.'.$fileExtension; + + return $uniqueFilename; + } + + // ----------------------------------------------------------------------------------------------------------- + // Deprecated methods, not to be used /** * Load a DMS Document. @@ -33,19 +540,20 @@ class DmsLib extends FHC_Controller { if (is_numeric($dms_id)) { - $this->ci->DmsModel->addJoin('campus.tbl_dms_version', 'dms_id'); - $this->ci->DmsModel->addOrder('version', 'DESC'); - $this->ci->DmsModel->addLimit(1); + $this->_ci->DmsModel->addJoin('campus.tbl_dms_version', 'dms_id'); + $this->_ci->DmsModel->addOrder('version', 'DESC'); + $this->_ci->DmsModel->addLimit(1); if (!is_numeric($version)) { - return $this->ci->DmsModel->load($dms_id); + return $this->_ci->DmsModel->load($dms_id); } else { - return $this->ci->DmsModel->loadWhere(array('dms_id' => $dms_id, 'version' => $version)); + return $this->_ci->DmsModel->loadWhere(array('dms_id' => $dms_id, 'version' => $version)); } } + return error('The parameter DMS ID must be a number'); } @@ -57,34 +565,30 @@ class DmsLib extends FHC_Controller */ public function read($dms_id, $version = null) { - $result = null; + $result = error('Wrong dms_id parameter'); if (isset($dms_id)) { - $this->ci->DmsModel->addJoin('campus.tbl_dms_version', 'dms_id'); - $this->ci->DmsModel->addOrder('version', 'DESC'); - $this->ci->DmsModel->addLimit(1); + $this->_ci->DmsModel->addJoin('campus.tbl_dms_version', 'dms_id'); + $this->_ci->DmsModel->addOrder('version', 'DESC'); + $this->_ci->DmsModel->addLimit(1); if (!isset($version)) { - $result = $this->ci->DmsModel->load($dms_id); + $result = $this->_ci->DmsModel->load($dms_id); } else { - $result = $this->ci->DmsModel->loadWhere(array('dms_id' => $dms_id, 'version' => $version)); + $result = $this->_ci->DmsModel->loadWhere(array('dms_id' => $dms_id, 'version' => $version)); } - } - if (hasData($result)) - { - $resultFS = $this->ci->DmsFSModel->read($result->retval[0]->filename); - if (isSuccess($resultFS)) + // If a dms has been found + if (hasData($result)) { - $result->retval[0]->{DmsLib::FILE_CONTENT_PROPERTY} = $resultFS->retval; - } - else - { - $result = $resultFS; + $resultFS = $this->_ci->DmsFSModel->readBase64(getData($result)[0]->filename); + if (isError($resultFS)) return $resultFS; // if an error occurred return it + + $result->retval[0]->{self::FILE_CONTENT_PROPERTY} = getData($resultFS); } } @@ -101,22 +605,16 @@ class DmsLib extends FHC_Controller */ public function getAktenAcceptedDms($person_id, $dokument_kurzbz = null, $no_file = null) { - $result = $this->ci->AkteModel->getAktenAcceptedDms($person_id, $dokument_kurzbz); + $result = $this->_ci->AkteModel->getAktenAcceptedDms($person_id, $dokument_kurzbz); if (hasData($result) && $no_file == null) { - $cnt = count($result->retval); - for ($i = 0; $i < $cnt; $i++) + for ($i = 0; $i < count(getData($result)); $i++) { - $resultFS = $this->ci->DmsFSModel->read($result->retval[$i]->filename); - if (isSuccess($resultFS)) - { - $result->retval[$i]->{DmsLib::FILE_CONTENT_PROPERTY} = $resultFS->retval; - } - else - { - $result = $resultFS; - } + $resultFS = $this->_ci->DmsFSModel->readBase64(getData($result)[$i]->filename); + if (isError($resultFS)) return $resultFS; // if an error occurred return it + + $result->retval[$i]->{self::FILE_CONTENT_PROPERTY} = getData($resultFS); } } @@ -135,30 +633,31 @@ class DmsLib extends FHC_Controller // Init upload configs $this->_loadUploadLibrary($allowed_types); - if (!$this->ci->upload->do_upload($field_name)) + if (!$this->_ci->upload->do_upload($field_name)) { - return error($this->ci->upload->display_errors()); + return error($this->_ci->upload->display_errors()); } - $upload_data = $this->ci->upload->data(); // data about the uploaded file - $filename = $upload_data['file_name']; + $upload_data = $this->_ci->upload->data(); // data about the uploaded file // Insert to DMS table - if (!$result = $this->ci->DmsModel->insert($this->ci->DmsModel->filterFields($dms))) - { - return error('Failed inserting to DMS'); - } - $upload_data['dms_id'] = $result->retval; + $insDmsResult = $this->_ci->DmsModel->insert($this->_ci->DmsModel->filterFields($dms)); + if (isError($insDmsResult)) return $insDmsResult; + + $upload_data['dms_id'] = getData($insDmsResult); // Insert DMS version - if (!$result = $this->ci->DmsVersionModel->insert( - $this->ci->DmsVersionModel->filterFields($dms, $result->retval, $filename))) - { - return error('Failed inserting DMS version'); - } + $insVersionResult = $this->_ci->DmsVersionModel->insert( + $this->_ci->DmsVersionModel->filterFields( + $dms, + $upload_data['dms_id'], + $upload_data['file_name'] + ) + ); + if (isError($insVersionResult)) return $insVersionResult; - // return result of uploaded data - return success($upload_data); // data about the uploaded file + // Return result of uploaded data + return success($upload_data); } /** @@ -171,36 +670,29 @@ class DmsLib extends FHC_Controller */ public function download($dms_id, $filename = null, $disposition = 'inline') { - $result = $this->getFileInfo($dms_id); - - if (isError($result)) + // Retrieves info about the given dms + $fileInfoResult = $this->getFileInfo($dms_id); + if (isError($fileInfoResult)) return error(getError($fileInfoResult)); + + // If data have been found + if (hasData($fileInfoResult)) { - return error(getError($result)); + $fileObj = getData($fileInfoResult); + + // Change filename, if filename is provided + if (!isEmptyString($filename)) $fileObj->name = $filename; + + // Add file disposition if disposition has a valid value + if ($disposition == 'attachment' || $disposition == 'inline') + { + $fileObj->disposition = $disposition; + } + + return success($fileObj); } - $fileObj = getData($result); - - // Change filename, if filename is provided - if (is_string($filename)) - { - $fileObj->name = $filename; - } - - // Add file disposition - if ($disposition == 'attachment') - { - $fileObj->disposition = 'attachment'; - } - else - { - $fileObj->disposition = 'inline'; - } - - // Output file - if(!$this->outputFile($fileObj)) - { - return error('Error on file output'); - } + // If no data have been found then return an empty success + return success(); } /** @@ -212,28 +704,28 @@ class DmsLib extends FHC_Controller */ public function getFileInfo($dms_id, $version = null) { - if (!is_numeric($dms_id)) - { - return error('Wrong parameter'); - } + // Checks the dms_id parameter + if (!is_numeric($dms_id)) return error('Wrong parameter'); - // Load file + // Load DMS from database $result = $this->load($dms_id, $version); + if (isError($result)) return error(getError($result)); - if (isError($result)) + // If data have been found + if (hasData($result)) { - return error(getError($result)); + // Store file information in fileObj + $fileObj = new stdClass(); + $fileObj->filename = getData($result)[0]->filename; + $fileObj->file = DMS_PATH.getData($result)[0]->filename; + $fileObj->name = DMS_PATH.getData($result)[0]->name; // original user filename + $fileObj->mimetype = getData($result)[0]->mimetype; + + return success($fileObj); } - // Store file information in fileObj - $fileObj = new StdClass(); - $fileObj->filename = getData($result)[0]->filename; - $fileObj->file = DMS_PATH. getData($result)[0]->filename; - $fileObj->name = DMS_PATH. getData($result)[0]->name; // original users filename - $fileObj->mimetype = DMS_PATH. getData($result)[0]->mimetype; - - return success($fileObj); - + // If no data have been found return an empty success + return success(); } /** @@ -253,20 +745,20 @@ class DmsLib extends FHC_Controller $result = $this->_saveFileOnInsert($dms); if (isSuccess($result)) { - $filename = $result->retval; + $filename = getData($result); if (isset($dms['dms_id']) && $dms['dms_id'] != '') { - $result = $this->ci->DmsVersionModel->insert( - $this->ci->DmsVersionModel->filterFields($dms, $dms['dms_id'], $filename) + $result = $this->_ci->DmsVersionModel->insert( + $this->_ci->DmsVersionModel->filterFields($dms, $dms['dms_id'], $filename) ); } else { - $result = $this->ci->DmsModel->insert($this->ci->DmsModel->filterFields($dms)); + $result = $this->_ci->DmsModel->insert($this->_ci->DmsModel->filterFields($dms)); if (isSuccess($result)) { - $result = $this->ci->DmsVersionModel->insert( - $this->ci->DmsVersionModel->filterFields($dms, $result->retval, $filename) + $result = $this->_ci->DmsVersionModel->insert( + $this->_ci->DmsVersionModel->filterFields($dms, getData($result), $filename) ); } } @@ -277,15 +769,15 @@ class DmsLib extends FHC_Controller $result = $this->_saveFileOnUpdate($dms); if (isSuccess($result)) { - $result = $this->ci->DmsModel->update($dms['dms_id'], $this->ci->DmsModel->filterFields($dms)); + $result = $this->_ci->DmsModel->update($dms['dms_id'], $this->_ci->DmsModel->filterFields($dms)); if (isSuccess($result)) { - $result = $this->ci->DmsVersionModel->update( + $result = $this->_ci->DmsVersionModel->update( array( $dms['dms_id'], $dms['version'] ), - $this->ci->DmsVersionModel->filterFields($dms) + $this->_ci->DmsVersionModel->filterFields($dms) ); } } @@ -308,56 +800,54 @@ class DmsLib extends FHC_Controller if (is_numeric($person_id) && is_numeric($dms_id)) { // Start DB transaction - $this->ci->db->trans_start(false); + $this->_ci->db->trans_start(false); // Get akte_id from table tbl_akte - $result = $this->ci->AkteModel->loadWhere(array('person_id' => $person_id, 'dms_id' => $dms_id)); + $result = $this->_ci->AkteModel->loadWhere(array('person_id' => $person_id, 'dms_id' => $dms_id)); if (isSuccess($result)) { // Delete all entries in tbl_akte - $cnt = count($result->retval); - for ($i = 0; $i < $cnt; $i++) + for ($i = 0; $i < count(getData($result)); $i++) { - $this->ci->AkteModel->delete($result->retval[$i]->akte_id); + $this->_ci->AkteModel->delete(getData($result)[$i]->akte_id); } // Get all filenames related to this dms - $resultFileNames = $this->ci->DmsVersionModel->loadWhere(array('dms_id' => $dms_id)); + $resultFileNames = $this->_ci->DmsVersionModel->loadWhere(array('dms_id' => $dms_id)); if (isSuccess($resultFileNames)) { // Delete from tbl_dms_version - $result = $this->ci->DmsVersionModel->delete(array('dms_id' => $dms_id)); + $result = $this->_ci->DmsVersionModel->delete(array('dms_id' => $dms_id)); if (isSuccess($result)) { // Delete from tbl_dms - $result = $this->ci->DmsModel->delete($dms_id); + $result = $this->_ci->DmsModel->delete($dms_id); } } } // Transaction complete! - $this->ci->db->trans_complete(); + $this->_ci->db->trans_complete(); // Check if everything went ok during the transaction - if ($this->ci->db->trans_status() === false || isError($result)) + if ($this->_ci->db->trans_status() === false || isError($result)) { - $this->ci->db->trans_rollback(); + $this->_ci->db->trans_rollback(); $result = error('An error occurred while performing a delete operation', EXIT_ERROR); } else { - $this->ci->db->trans_commit(); + $this->_ci->db->trans_commit(); $result = success('Dms successfully removed from DB'); } // If everything is ok if (isSuccess($result)) { - $cnt = count($resultFileNames->retval); // Remove all files related to this person and dms - for ($i = 0; $i < $cnt; $i++) + for ($i = 0; $i < count(getData($resultFileNames)); $i++) { - $this->ci->DmsFSModel->remove($resultFileNames->retval[$i]->filename); + $this->_ci->DmsFSModel->removeBase64(getData($resultFileNames)[$i]->filename); } } } @@ -376,19 +866,19 @@ class DmsLib extends FHC_Controller */ public function getAkteContent($akte_id) { - $akte = $this->ci->AkteModel->load($akte_id); + $akte = $this->_ci->AkteModel->load($akte_id); if (hasData($akte)) { - if ($akte->retval[0]->inhalt != '') + if (getData($akte)[0]->inhalt != '') { - return success(base64_decode($akte->retval[0]->inhalt)); + return success(base64_decode(getData($akte)[0]->inhalt)); } - elseif ($akte->retval[0]->dms_id != '') + elseif (getData($akte)[0]->dms_id != '') { - $dmscontent = $this->read($akte->retval[0]->dms_id); + $dmscontent = $this->read(getData($akte)[0]->dms_id); if (isSuccess($dmscontent)) { - return success(base64_decode($dmscontent->retval[0]->file_content)); + return success(base64_decode(getData($dmscontent)[0]->file_content)); } else { @@ -415,10 +905,10 @@ class DmsLib extends FHC_Controller { $filename = uniqid().'.'.pathinfo($dms['name'], PATHINFO_EXTENSION); - $result = $this->ci->DmsFSModel->write($filename, $dms['file_content']); + $result = $this->_ci->DmsFSModel->writeBase64($filename, $dms['file_content']); if (isSuccess($result)) { - $result->retval = $filename; + $result = success($filename); } return $result; @@ -439,7 +929,7 @@ class DmsLib extends FHC_Controller if (hasData($result)) { - $result = $this->ci->DmsFSModel->write($result->retval[0]->filename, $dms['file_content']); + $result = $this->_ci->DmsFSModel->writeBase64(getData($result)[0]->filename, $dms['file_content']); } } @@ -451,12 +941,14 @@ class DmsLib extends FHC_Controller */ private function _loadUploadLibrary($allowed_types) { - $config['upload_path'] = $this->UPLOAD_PATH; - $config['allowed_types'] = implode('|', $allowed_types); - $config['overwrite'] = true; - $config['file_name'] = uniqid().'.pdf'; + $config = array(); + $config['upload_path'] = DMS_PATH; + $config['allowed_types'] = implode('|', $allowed_types); + $config['overwrite'] = true; + $config['file_name'] = uniqid().'.pdf'; - $this->ci->load->library('upload', $config); - $this->ci->upload->initialize($config); + $this->_ci->load->library('upload', $config); + $this->_ci->upload->initialize($config); } } + diff --git a/application/libraries/FilesystemLib.php b/application/libraries/FilesystemLib.php deleted file mode 100644 index c940acede..000000000 --- a/application/libraries/FilesystemLib.php +++ /dev/null @@ -1,142 +0,0 @@ -checkParameters($filepath, $filename)) - { - $resource = $filepath.DIRECTORY_SEPARATOR.$filename; - if (file_exists($resource) && $fileHandle = fopen($resource, 'r')) - { - $result = ''; - while (!feof($fileHandle)) - { - $result .= fread($fileHandle, 8192); - } - fclose($fileHandle); - } - } - - return $result; - } - - /** - * write - */ - public function write($filepath, $filename, $content) - { - $result = null; - - if ($this->checkParameters($filepath, $filename) && isset($content)) - { - $resource = $filepath.DIRECTORY_SEPARATOR.$filename; - if (is_writable($filepath) && $fileHandle = fopen($resource, 'w')) - { - if (fwrite($fileHandle, $content) !== false) - { - $result = true; - } - fclose($fileHandle); - } - } - - return $result; - } - - /** - * append - */ - public function append($filepath, $filename, $content) - { - $result = null; - - if ($this->checkParameters($filepath, $filename) && isset($content)) - { - $resource = $filepath.DIRECTORY_SEPARATOR.$filename; - if (is_writable($resource) && $fileHandle = fopen($resource, 'a')) - { - if (fwrite($fileHandle, $content) !== false) - { - $result = true; - } - fclose($fileHandle); - } - } - - return $result; - } - - /** - * remove - */ - public function remove($filepath, $filename) - { - $result = null; - - if ($this->checkParameters($filepath, $filename)) - { - if (is_writable($filepath)) - { - $resource = $filepath.DIRECTORY_SEPARATOR.$filename; - $result = unlink($resource); - } - } - - return $result; - } - - /** - * rename - */ - public function rename($filepath, $filename, $newFilepath, $newFilename) - { - $result = null; - - if ($this->checkParameters($filepath, $filename) && $this->checkParameters($newFilepath, $newFilename)) - { - $resource = $filepath.DIRECTORY_SEPARATOR.$filename; - if (is_writable($filepath) && is_writable($newFilepath) && file_exists($resource)) - { - $destination = $newFilepath.DIRECTORY_SEPARATOR.$newFilename; - $result = rename($resource, $destination); - } - } - - return $result; - } -} diff --git a/application/models/content/DmsFS_model.php b/application/models/content/DmsFS_model.php index 38a72d853..81443805f 100644 --- a/application/models/content/DmsFS_model.php +++ b/application/models/content/DmsFS_model.php @@ -7,7 +7,7 @@ class DmsFS_model extends FS_Model */ public function __construct() { - parent::__construct(); - $this->filepath = DMS_PATH; + parent::__construct(DMS_PATH); } -} \ No newline at end of file +} + diff --git a/application/models/content/TempFS_model.php b/application/models/content/TempFS_model.php new file mode 100644 index 000000000..ac55a24d7 --- /dev/null +++ b/application/models/content/TempFS_model.php @@ -0,0 +1,16 @@ +>' . $LANGUAGE_INDEX . ' AS "status_bezeichnung", CASE @@ -191,4 +194,4 @@ $filterWidgetArray = array( echo $this->widgetlib->widget('TableWidget', $filterWidgetArray); -?> \ No newline at end of file +?>