public static function getInstance() { if (!self::$_instance) { self::$_instance = new self(); } return self::$_instance; }
/** * Sets header to the mime type of the attachment as queried from the * database and tries to send the file contents to the client. * To identify the attachment, the action needs the parameters "key" * and "id" as found in the data model of the attachments. * */ public function downloadFileAction() { $id = $this->_request->getParam('id'); $key = $this->_request->getParam('key'); $userId = $this->_helper->registryAccess->getUserId(); $type = $this->_request->getParam('type'); $name = $this->_request->getParam('name'); $mimeType = false; $uId = $this->_request->getParam('uId'); $path = $this->_request->getParam('path'); $data = null; if ($path) { $path = urldecode($path); } $downloadCookieName = $this->_request->getParam('downloadCookieName'); if ($type == 'emailAttachment') { $data = $this->getAttachmentFromServer($key, $uId, $path); $mimeType = $data['mimeType']; } else { /** * @see Conjoon_Modules_Groupware_Files_File_Facade */ require_once 'Conjoon/Modules/Groupware/Files/File/Facade.php'; $facade = Conjoon_Modules_Groupware_Files_File_Facade::getInstance(); $data = $facade->getFileDownloadDataForUserId($id, $key, $userId); $mimeType = $data['mime_type']; } if (!$data) { /** * @see Conjoon_Exception */ require_once 'Conjoon/Exception.php'; // we'll throw an exception, that's okay for now throw new Conjoon_Exception("Sorry, but the requested file is not available."); return; } if ($mimeType === false) { /** * @see Conjoon_Exception */ require_once 'Conjoon/Exception.php'; throw new Conjoon_Exception("Sorry, but the \"mime type\" is missing. Make sure \"mimeType\" for attachments " . "is available, or \"mime_type\" for files."); return; } $this->_helper->viewRenderer->setNoRender(); $response = $this->getResponse(); $response->clearAllHeaders(); setcookie($downloadCookieName, 'downloading', 0, '/'); $response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)->setHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT', true)->setHeader('Pragma', 'no-cache', true)->setHeader('Content-Description', $name != "" ? $name : $data['name'], true)->setHeader('Content-Type', $mimeType, true)->setHeader('Content-Transfer-Encoding', 'binary', true)->setHeader('Content-Disposition', 'attachment; filename="' . addslashes($name != "" ? $name : $data['name']) . '"', true); $response->sendHeaders(); $response->setBody($data['resource']); }
/** * Will save attachments for a draft. * When requested to save/send a message by a client, all existing * attachments/files will be available in the postedAttachments property, * data according to the structure of com.conjoon.cudgets.data.FileRecord. * Important keys are 'orgId', 'metaType', 'key', 'name' * * Warning! since its not guaranteed that the ids in removeAttachments are * ids for attachments that indeed belong to the draft, it's needed * to check whether the current list of attachments holds this id. * * @param Conjoon_Modules_Groupware_Email_Draft $message * @param array $postedAttachments * @param array $removeAttachmentIds * @param array $attachmentMap * * @throws InvalidArgumentException */ public function saveAttachmentsForDraft(Conjoon_Modules_Groupware_Email_Draft $draft, $postedAttachments = array(), $removeAttachmentIds = array(), &$attachmentMap = array()) { if ($draft->getId() <= 0) { throw new InvalidArgumentException("Invalid draft supplied - id was " . $draft->getId()); } /** * @see Conjoon_Modules_Groupware_Email_Attachment_Model_Attachment */ require_once 'Conjoon/Modules/Groupware/Email/Attachment/Model/Attachment.php'; $attachmentModel = new Conjoon_Modules_Groupware_Email_Attachment_Model_Attachment(); // first off, get all the attachments from the draft $draftAttachments = $draft->getAttachments(); $postedEmailAttachmentIds = array(); $existingEmailAttachmentIds = array(); $postedFilesIds = array(); $finalPostedFiles = array(); $finalPostedAttachments = array(); $finalExistingAttachments = array(); //get ids for emailAttachments for ($i = 0, $len = count($postedAttachments); $i < $len; $i++) { if ($postedAttachments[$i]['metaType'] == 'emailAttachment') { $postedEmailAttachmentIds[] = $postedAttachments[$i]['orgId']; $finalPostedAttachments[$postedAttachments[$i]['orgId']] = $postedAttachments[$i]; } else { $postedFilesIds[] = $postedAttachments[$i]['orgId']; $finalPostedFiles[$postedAttachments[$i]['orgId']] = $postedAttachments[$i]; } } for ($i = 0, $len = count($draftAttachments); $i < $len; $i++) { // intersect will be created later $existingEmailAttachmentIds[] = $draftAttachments[$i]->getId(); if (in_array($draftAttachments[$i]->getId(), $removeAttachmentIds)) { continue; } $finalExistingAttachments[$draftAttachments[$i]->getId()] = $draftAttachments[$i]; } // finally create the intersection of all ids that are in the // lists of items to remove and in the list of existing items $removeAttachmentIds = array_values(array_intersect($removeAttachmentIds, $existingEmailAttachmentIds)); // get the ids from the attachments that need to get changed $changeNameIds = array_values(array_intersect($postedEmailAttachmentIds, $existingEmailAttachmentIds)); // get the ids from the attachments that need to get saved, i.e. // when a draft was created with email attachments which currently // beong to another email $copyAttachmentIds = array_values(array_diff($postedEmailAttachmentIds, $existingEmailAttachmentIds)); // take care of copying attachments for ($i = 0, $len = count($copyAttachmentIds); $i < $len; $i++) { $id = $copyAttachmentIds[$i]; $newAttachmentId = $attachmentModel->copyAttachmentForNewItemId($id, $draft->getId(), $finalPostedAttachments[$id]['name']); if ($newAttachmentId > 0) { $attachmentMap[$finalPostedAttachments[$id]['id']] = $newAttachmentId; } } // take care of deleting attachments for ($i = 0, $len = count($removeAttachmentIds); $i < $len; $i++) { $attachmentModel->deleteAttachmentForId($removeAttachmentIds[$i]); } // take care of renaming attachments for ($i = 0, $len = count($changeNameIds); $i < $len; $i++) { $id = $changeNameIds[$i]; if ($finalExistingAttachments[$id]->getFileName() != $finalPostedAttachments[$id]['name']) { $updated = $attachmentModel->updateNameForAttachment($id, $finalPostedAttachments[$id]['name']); if ($updated) { $finalExistingAttachments[$id]->setFileName($finalPostedAttachments[$id]['name']); $attachmentMap[$finalPostedAttachments[$id]['id']] = $id; } } } // copy files to attachments /** * @see Conjoon_Modules_Groupware_Files_File_Facade */ require_once 'Conjoon/Modules/Groupware/Files/File/Facade.php'; $filesFacade = Conjoon_Modules_Groupware_Files_File_Facade::getInstance(); foreach ($finalPostedFiles as $id => $file) { // possible that the file is stored in the file system, so get the content here // and pass as argument $fileData = $filesFacade->getLobContentWithData(array('id' => $file['orgId'], 'key' => $file['key'], 'includeResource' => true)); $newAttachmentId = $attachmentModel->copyFromFilesForItemId($file['key'], $file['orgId'], $draft->getId(), $file['name'], $fileData['resource']); if ($newAttachmentId > 0) { $attachmentMap[$finalPostedFiles[$id]['id']] = $newAttachmentId; } } }
/** * * @return Conjoon_Modules_Groupware_Files_File_Facade */ private function _getFileFacade() { if (!$this->_fileFacade) { /** * @see Conjoon_Modules_Groupware_Files_File_Facade */ require_once 'Conjoon/Modules/Groupware/Files/File/Facade.php'; $this->_fileFacade = Conjoon_Modules_Groupware_Files_File_Facade::getInstance(); } return $this->_fileFacade; }