private function _process_form() { if (!isset($_POST['midgard_admin_asgard_save'])) { return false; } // Check if we have an uploaded file if (isset($_FILES['midgard_admin_asgard_file']) && is_uploaded_file($_FILES['midgard_admin_asgard_file']['tmp_name'])) { return $this->_process_file_upload($_FILES['midgard_admin_asgard_file']); } if (is_null($this->_file)) { if (empty($_POST['midgard_admin_asgard_filename'])) { return false; } // We're creating a new file $local_filename = midcom_db_attachment::safe_filename($_POST['midgard_admin_asgard_filename']); $local_file = $this->_get_file($local_filename); if (!$local_file) { // New file, create $local_file = new midcom_db_attachment(); $local_file->name = $local_filename; $local_file->parentguid = $this->_object->guid; if (!$local_file->create()) { throw new midcom_error('Failed to create attachment, reason: ' . midcom_connection::get_error_string()); } } } else { $local_file = $this->_file; } $success = true; if (!empty($_POST['midgard_admin_asgard_filename']) && $local_file->name != $_POST['midgard_admin_asgard_filename']) { $local_file->name = $_POST['midgard_admin_asgard_filename']; if (!$local_file->update()) { $success = false; } } if (!empty($_POST['midgard_admin_asgard_mimetype']) && $local_file->mimetype != $_POST['midgard_admin_asgard_mimetype']) { $local_file->mimetype = $_POST['midgard_admin_asgard_mimetype']; if (!$local_file->update()) { $success = false; } } // We should always store at least an empty string so it can be edited later $contents = ''; if (!empty($_POST['midgard_admin_asgard_contents'])) { $contents = $_POST['midgard_admin_asgard_contents']; } if (!$local_file->copy_from_memory($contents)) { $success = false; } if (!$success) { return false; } return $local_file->name; }
function set_video($filename, $tmpname, $title, $autodelete = true) { if (empty($filename)) { debug_add("filename must not be empty", MIDCOM_LOG_ERROR); return false; } // Ensure that the filename is URL safe and contains only one extension $filename = midcom_db_attachment::safe_filename($filename, true); return $this->_set_video($filename, $tmpname, $title, $autodelete); }
private function _upload_image(array $file, org_openpsa_slideshow_image_dba $image) { $attachment = new midcom_db_attachment(); $attachment->name = midcom_db_attachment::safe_filename($file['name']); $attachment->title = $_POST['title']; $attachment->mimetype = $file['type']; $attachment->parentguid = $image->guid; if (!$attachment->create() || !$attachment->copy_from_file($file['tmp_name'])) { throw new midcom_error('Failed to create attachment: ' . midcom_connection::get_error_string()); } $this->_response->filename = $attachment->name; $image->attachment = $attachment->id; $image->generate_image('thumbnail', $this->_config->get('thumbnail_filter')); $image->generate_image('image', $this->_config->get('image_filter')); $image->update(); }
/** * Adds the image to the type. Loads and processes the $tmpname file on disk. * * @param string $filename The name of the image attachment to be created. * @param string $tmpname The file to load. * @param string $title The title of the image. * @param boolean $autodelete If this is true (the default), the temporary file will * be deleted after postprocessing and attachment-creation. * @return boolean Indicating success. */ function set_image($filename, $tmpname, $title, $autodelete = true) { // Ensure that the filename is URL safe and contains only one extension $filename = midcom_db_attachment::safe_filename($filename, true); $this->_pending_attachments = $this->attachments; // Prepare Internal Members $this->title = $title; $this->_filename = $filename; $this->_original_tmpname = $tmpname; $this->_original_mimetype = midcom_helper_misc::get_mimetype($this->_original_tmpname); $this->_filter = new midcom_helper_imagefilter(); if (array_key_exists('archival', $this->_pending_attachments)) { // We never touch the archival version after it has been uploaded unset($this->_pending_attachments['archival']); } else { if (!$this->save_archival_image()) { debug_add("Failed to store the archival image for the uploaded file {$filename} in {$tmpname}, aborting type processing.", MIDCOM_LOG_ERROR); // Could not store even the archival image properly, clean up and abort $this->delete_all_attachments(); return false; } } if (!$this->_filter->set_file($this->_original_tmpname)) { debug_add("this->_filter->set_file('{$this->_original_tmpname}') returned failure, aborting type processing.", MIDCOM_LOG_ERROR); // NOTE: absense of delete_all_attachments is intentional return false; } $this->_preprocess_raw(); if (!$this->_auto_convert_to_web_type()) { debug_add("failed to convert to web type, aborting type processing.", MIDCOM_LOG_ERROR); // NOTE: absense of delete_all_attachments is intentional return false; } $this->_add_thumbnail_to_derived_images(); if (!$this->_save_main_image()) { debug_add("failed to save 'main' image, aborting type processing.", MIDCOM_LOG_ERROR); // NOTE: absense of delete_all_attachments is intentional return false; } if (!$this->_save_derived_images()) { debug_add("failed to save derived images, aborting type processing.", MIDCOM_LOG_ERROR); // NOTE: absense of delete_all_attachments is intentional return false; } // Clear up all attachments no longer in use $this->_clean_pending_attachments(); if ($autodelete) { unlink($this->_original_tmpname); } return true; }
/** * Update an existing attachment with a new file (this keeps GUIDs stable). * * @param string $identifier The identifier of the attachment to update. * @param string $tmpname The name of the source file. * @param string $filename The filename to use after processing. * @param string $title The new title of the attachment, set this to null to * keep the original title unchanged. * @param string $mimetype The new MIME Type of the file, set this to null to * keep the original title unchanged. If you are unsure of the mime type, * set this to '' not null, this will enforce a redetection. * @param boolean $autodelete Set this to true (the default) to automatically delete the * file after successful processing. * @return boolean Indicating success. */ function update_attachment($identifier, $filename, $title, $mimetype, $tmpname, $autodelete = true) { if (!file_exists($tmpname)) { debug_add("Cannot add attachment, the file {$tmpname} was not found.", MIDCOM_LOG_INFO); return false; } if (!$this->file_sanity_checks($tmpname, $filename)) { // the method will log errors and raise uimessages as needed return false; } $filename = midcom_db_attachment::safe_filename($filename, false); $handle = @fopen($tmpname, 'r'); if (!$handle) { debug_add("Cannot add attachment, could not open {$tmpname} for reading.", MIDCOM_LOG_INFO); if (isset($php_errormsg)) { debug_add("Last PHP error was: {$php_errormsg}", MIDCOM_LOG_INFO); } return false; } if (!$this->update_attachment_by_handle($identifier, $filename, $title, $mimetype, $handle, true, $tmpname)) { debug_add('Failed to create attachment, see above for details.'); return false; } if ($autodelete) { if (!@unlink($tmpname)) { debug_add('Failed to automatically delete the source file, ignoring silently.', MIDCOM_LOG_WARN); if (isset($php_errormsg)) { debug_add("Last PHP error was: {$php_errormsg}", MIDCOM_LOG_WARN); } } } return true; }
/** * Automatically convert the uploaded file to a web-compatible type. Uses * only the first image of multi-page uploads (like PDFs) and populates the * _target_mimetype member accordingly. The original_tmpname file is manipulated * directly. * * Uploaded GIF, PNG and JPEG files are left untouched. * * In case of any conversions being done, the new extension will be appended * to the uploaded file. * * @return boolean Indicating success */ function _auto_convert_to_web_type() { debug_add("\$this->_original_mimetype: {$this->_original_mimetype}"); switch (preg_replace('/;.+$/', '', $this->_original_mimetype)) { case 'image/png': case 'image/gif': case 'image/jpeg': debug_add('No conversion necessary we already have a web mime type'); return true; case 'application/postscript': case 'application/pdf': $this->_target_mimetype = 'image/png'; $conversion = 'png'; break; default: $this->_target_mimetype = 'image/jpeg'; $conversion = 'jpg'; break; } debug_add("\$conversion={$conversion}"); if (!$this->imagemagick_available()) { throw new midcom_error('DM2 type image requires ImageMagick for manipulation operations, see debug log for details'); } // Prevent double .jpg.jpg in case of trouble file the get_mimetype() if (!preg_match("/\\.{$conversion}\$/", $this->_filename)) { $this->_filename .= ".{$conversion}"; // Make sure there is only one extension on the file ?? $this->_filename = midcom_db_attachment::safe_filename($this->_filename, true); } if ($this->_filter) { return $this->_filter->convert($conversion); } }
/** * @dataProvider provider_safe_filename */ public function test_safe_filename($input, $extension, $output) { $converted = midcom_db_attachment::safe_filename($input, $extension); $this->assertEquals($converted, $output); }