public function test_copy_from_file() { $attachment = $this->create_object('midcom_db_attachment', array('parentguid' => self::$_topic->guid)); $stat = $attachment->copy_from_file(self::$_filepath . 'attach.png'); $this->assertTrue($stat, midcom_connection::get_error_string()); $blob = new midgard_blob($attachment->__object); $this->assertFileEquals(self::$_filepath . 'attach.png', $blob->get_path()); }
/** * @param mixed $handler_id The ID of the handler. * @param Array $args The argument list. * @param Array &$data The local request data. */ public function _handler_pdf($handler_id, array $args, array &$data) { $this->_invoice = new org_openpsa_invoices_invoice_dba($args[0]); $this->_request_data['invoice_url'] = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . "invoice/" . $this->_invoice->guid . "/"; //check for manually uploaded pdf-file & if user wants to replace it if (array_key_exists('cancel', $_POST)) { return new midcom_response_relocate($this->_request_data['invoice_url']); } else { if (array_key_exists('save', $_POST)) { $this->_update_attachment = true; } else { $data['confirmation_message'] = 'current pdf file was manually uploaded shall it be replaced ?'; // load schema & datamanager to get attachment $schemadb = midcom_helper_datamanager2_schema::load_database($this->_config->get('schemadb')); $this->_datamanager = new midcom_helper_datamanager2_datamanager($schemadb); if (!$this->_datamanager->autoset_storage($this->_invoice)) { throw new midcom_error("Failed to create a DM2 instance for object {$this->_invoice->guid}."); } if ($this->_invoice->sent) { $data['confirmation_message'] = 'invoice has already been sent. should it be replaced?'; } else { if (!empty($this->_datamanager->types['pdf_file']->attachments)) { foreach ($this->_datamanager->types['pdf_file']->attachments as $attachment) { $checksum = $attachment->get_parameter('org.openpsa.invoices', 'auto_generated'); // check if auto generated parameter is same as md5 in current-file // if not the file was manually uploaded if ($checksum) { $blob = new midgard_blob($attachment->__object); // check if md5 sum equals the one saved in auto_generated if ($checksum == md5_file($blob->get_path())) { $this->_update_attachment = true; } } } } else { $this->_update_attachment = true; } } } } if ($this->_update_attachment) { $this->_request_data['billing_data'] = $this->_invoice->get_billing_data(); if (self::render_and_attach_pdf($this->_invoice)) { midcom::get('uimessages')->add($this->_l10n->get($this->_component), $this->_l10n->get('pdf created')); } else { midcom::get('uimessages')->add($this->_l10n->get($this->_component), $this->_l10n->get('pdf creation failed') . ': ' . midcom_connection::get_error_string(), 'error'); } return new midcom_response_relocate($this->_request_data["invoice_url"]); } }
/** * Function serves the attachment by provided guid and exits. * @todo: Permission handling * @todo: Direct filesystem serving * @todo: Configuration options */ public function get_serve(array $args) { $att = new midgard_attachment($args['guid']); if (midgardmvc_core::get_instance()->configuration->enable_attachment_cache) { midgardmvc_core::get_instance()->dispatcher->header('Location: ' . midgardmvc_core_helpers_attachment::get_url($att)); midgardmvc_core::get_instance()->dispatcher->end_request(); } $blob = new midgard_blob($att); midgardmvc_core::get_instance()->dispatcher->header('Content-type: ' . $att->mimetype); /** * If X-Sendfile support is enabled just sending correct headers */ if (midgardmvc_core::get_instance()->configuration->enable_xsendfile) { midgardmvc_core::get_instance()->dispatcher->header('X-Sendfile: ' . $blob->get_path()); } else { echo $blob->read_content(); } midgardmvc_core::get_instance()->dispatcher->end_request(); }
/** * Links file to public web folder. * * @param midgard_attachment attachment An attachment object * @return true of false */ public static function add_to_cache(midgard_attachment &$attachment) { $blob = new midgard_blob($attachment); // FIXME: Attachment directory creating should be done more elegantly $attachment_dir = explode('/', $attachment->location); $attachment_dir = midgardmvc_core::get_instance()->configuration->attachment_cache_directory . "{$attachment_dir[0]}/{$attachment_dir[1]}/"; if (is_file(midgardmvc_core::get_instance()->configuration->attachment_cache_directory . $attachment->location)) { return false; } if (!is_dir($attachment_dir)) { mkdir($attachment_dir, 0700, true); } return symlink($blob->get_path(), midgardmvc_core::get_instance()->configuration->attachment_cache_directory . $attachment->location); }
/** * Simple wrapper for stat() on the blob object. * * @return mixed Either a stat array as for stat() or false on failure. */ function stat() { if (!$this->id) { debug_add('Cannot open a non-persistent attachment..', MIDCOM_LOG_WARN); debug_print_r('Object state:', $this); return false; } $blob = new midgard_blob($this->__object); $path = $blob->get_path(); if (!file_exists($path)) { debug_add("File {$path} that blob {$this->guid} points to cannot be found", MIDCOM_LOG_WARN); return false; } return stat($path); }
/** * Deliver a blob to the client. * * This is a replacement for mgd_serve_attachment that should work around most of * its bugs: It is missing all important HTTP Headers concerning file size, * modification date and expiration. It will not call _midcom_stop_request() when it is finished, * you still have to do that yourself. It will add the following HTTP Headers: * * - Cache-Control: public max-age=$expires * - Expires: GMT Date $now+$expires * - Last-Modified: GMT Date of the last modified timestamp of the Attachment * - Content-Length: The Length of the Attachment in Bytes * - Accept-Ranges: none * * This should enable caching of browsers for Navigation images and so on. You can * influence the expiration of the served attachment with the parameter $expires. * It is the time in seconds till the client should refetch the file. The default * for this is 24 hours. If you set it to "0" caching will be prohibited by * changing the sent headers like this: * * - Pragma: no-cache * - Cache-Control: no-cache * - Expires: Current GMT Date * * If expires is set to -1, no expires header gets sent. * * @param MidgardAttachment &$attachment A reference to the attachment to be delivered. * @param int $expires HTTP-Expires timeout in seconds, set this to 0 for uncacheable pages, or to -1 for no Expire header. */ public function serve_attachment(&$attachment, $expires = -1) { if ($GLOBALS['midcom_config']['attachment_cache_enabled']) { $path = '/' . substr($attachment->guid, 0, 1) . "/{$attachment->guid}_{$attachment->name}"; if (file_exists($GLOBALS['midcom_config']['attachment_cache_root'] . $path)) { $response = new midcom_response_relocate($GLOBALS['midcom_config']['attachment_cache_url'] . $path, 301); $response->send(); } } // Sanity check expires if (!is_int($expires) || $expires < -1) { throw new midcom_error("\$expires has to be a positive integer or zero or -1, is now {$expires}."); } // Doublecheck that this is registered $cache = midcom::get('cache'); $cache->content->register($attachment->guid); $stats = $attachment->stat(); $last_modified =& $stats[9]; $app = midcom::get(); $etag = md5("{$last_modified}{$attachment->name}{$attachment->mimetype}{$attachment->guid}"); // Check etag and return 304 if necessary if ($expires != 0 && $cache->content->_check_not_modified($last_modified, $etag)) { if (!_midcom_headers_sent()) { $cache->content->cache_control_headers(); // Doublemakesure these are present $app->header('HTTP/1.0 304 Not Modified', 304); $app->header("ETag: {$etag}"); } while (@ob_end_flush()) { } debug_add("End of MidCOM run: {$_SERVER['REQUEST_URI']}"); _midcom_stop_request(); } $f = $attachment->open('r'); if (!$f) { throw new midcom_error('Failed to open attachment for reading: ' . midcom_connection::get_error_string()); } $app->header("ETag: {$etag}"); $cache->content->content_type($attachment->mimetype); $cache->content->register_sent_header("Content-Type: {$attachment->mimetype}"); $app->header("Last-Modified: " . gmdate("D, d M Y H:i:s", $last_modified) . ' GMT'); $app->header("Content-Length: " . $stats[7]); $app->header("Content-Description: {$attachment->title}"); // PONDER: Support ranges ("continue download") somehow ? $app->header("Accept-Ranges: none"); if ($expires > 0) { // If custom expiry now+expires is set use that $cache->content->expires(time() + $expires); } else { if ($expires == 0) { // expires set to 0 means disable cache, so we shall $cache->content->no_cache(); } } // TODO: Check metadata service for the real expiry timestamp ? $cache->content->cache_control_headers(); $send_att_body = true; if ($GLOBALS['midcom_config']['attachment_xsendfile_enable']) { $blob = new midgard_blob($attachment->__object); $att_local_path = $blob->get_path(); debug_add("Checking is_readable({$att_local_path})"); if (is_readable($att_local_path)) { $app->header("X-Sendfile: {$att_local_path}"); $send_att_body = false; } } // Store metadata in cache so _check_hit() can help us if (!$cache->content->_uncached && !$cache->content->_no_cache) { $cache->content->write_meta_cache('A-' . $etag, $etag); } while (@ob_end_flush()) { } if (!$send_att_body) { debug_add('NOT sending file (X-Sendfile will take care of that, _midcom_stop_request()ing so nothing has a chance the mess things up anymore'); _midcom_stop_request(); } fpassthru($f); $attachment->close(); debug_add("End of MidCOM run: {$_SERVER['REQUEST_URI']}"); _midcom_stop_request(); }