/** * Create a new document using the given file and metadata. * This method makes sure that there are no inconsistencies between a real * file and its database entry. Only if the file were copied/moved to the * documents folder, the database entry is written. If this fails too, the * file will be unlinked again. * The first parameter can either be an uploaded file or the path to an * already existing one. This file will either be moved using * move_uploaded_file or it will be copied. * The destination is determined this way: If the second parameter $data * already contains a "dokument_id", this will be used as the file's * destination. This is usually the case when refreshing a file. * If there is no such parameter, a new "dokument_id" is generated as usual * and is used as the file's destination. * * Before a document (and its file) is created, the notification * "DocumentWillCreate" will be posted. * If the document was created successfuly, the notification * "DocumentDidCreate" will be posted. * It the document was updated rather than created (see above), the * notifications will be "DocumentWillUpdate" and "DocumentDidUpdate". * The subject of the notification will always be that document. * * @param $file string full path to a file (either uploaded or already existing) * @param $data array an array containing the metadata of the document; * just use the same way as StudipDocument::setData * @return StudipDocument|null if successful the created document, null otherwise */ static function createWithFile($file, $data) { $doc = new StudipDocument(@$data['dokument_id']); $doc->setData($data); // create new ID (and thus path) if (!$doc->getId()) { $doc->setId($doc->getNewId()); } $notifications = !isset($data['dokument_id']) ? array('DocumentWillCreate', 'DocumentDidCreate') : array('DocumentWillUpdate', 'DocumentDidUpdate'); // send DocumentWill(Create|Update) notification NotificationCenter::postNotification($notifications[0], $doc); if (!$doc->attachFile($file) || !$doc->safeStore()) { return null; } // send DocumentDid(Create|Update) notification NotificationCenter::postNotification($notifications[1], $doc); return $doc; }
/** * Saves the content as a file in the filesystem and returns a Stud.IP-document object. * @param string $filename name of the future file without the extension. * @param mixed $folder_id md5-id of a given folder in database or null for nothing * @return StudipDocument of the exported file or false if creation of StudipDocument failed. */ public function save($filename, $folder_id = null) { global $user; $db = DBManager::get(); $doc = new StudipDocument(); $doc['folder_id'] = $folder_id; if ($folder_id) { $query = "SELECT range_id FROM folder WHERE folder_id = ?"; $statement = DBManager::get()->prepare($query); $statement->execute(array($folder_id)); $doc['range_id'] = $statement->fetchColumn(); } $doc['user_id'] = $user->id; $doc['name'] = $filename; $doc['filename'] = $filename . ".pdf"; $doc['description'] = ""; $doc['author_host'] = getenv('REMOTE_ADDR'); $doc['author_name'] = get_fullname($user->id); if ($doc->store()) { $path = get_upload_file_path($doc->getId()); $this->Output($path, 'F'); $doc['filesize'] = filesize($path); $doc->store(); return $doc; } return false; }
public function upload_attachment_action() { if ($GLOBALS['user']->id === "nobody") { throw new AccessDeniedException(); } if (!$GLOBALS['ENABLE_EMAIL_ATTACHMENTS']) { throw new AccessDeniedException(_('Mailanhänge sind nicht erlaubt.')); } $file = studip_utf8decode($_FILES['file']); $output = array('name' => $file['name'], 'size' => $file['size']); $output['message_id'] = Request::option("message_id"); if (!validate_upload($file)) { list($type, $error) = explode("§", $GLOBALS['msg']); throw new Exception($error); } $document = new StudipDocument(); $document->setValue('range_id', 'provisional'); $document->setValue('seminar_id', $GLOBALS['user']->id); $document->setValue('name', $output['name']); $document->setValue('filename', $document->getValue('name')); $document->setValue('filesize', (int) $output['size']); $document->setValue('autor_host', $_SERVER['REMOTE_ADDR']); $document->setValue('user_id', $GLOBALS['user']->id); $document->setValue('description', Request::option('message_id')); $success = $document->store(); if (!$success) { throw new Exception("Unable to handle uploaded file."); } $file_moved = move_uploaded_file($file['tmp_name'], get_upload_file_path($document->getId())); if (!$file_moved) { throw new Exception("No permission to move file to destination."); } $output['document_id'] = $document->getId(); $output['icon'] = GetFileIcon(getFileExtension($output['name']))->asImg(['class' => "text-bottom"]); $this->render_json($output); }
/** * @param $dokument_id * @return StudipMail provides fluent interface */ function addStudipAttachment($dokument_id) { $doc = new StudipDocument($dokument_id); if (!$doc->isNew()) { $this->addFileAttachment(get_upload_file_path($doc->getId()), $doc->getValue('filename')); } return $this; }