/** * Builds the right content-type/encoding types based on file and * content-type. * * Will try to match a common description, based on the $type param. * If nothing is found, will return a base64 attached file disposition. * * Can be used to send to an email server directly. * * @param $type optional mime-type * @param $file optional the path of the attachment * @param $filename optional the name of the attached file * @param $charset optional the charset of the attached file * * @return array */ public function contentInfoArray($type = NULL, $file = NULL, $filename = NULL, $charset = NULL) { // Common descriptions $description = array('multipart/mixed' => array('Content-Type' => 'multipart/mixed; boundary="' . $this->getBoundary('multipart/mixed') . '"'), 'multipart/alternative' => array('Content-Type' => 'multipart/alternative; boundary="' . $this->getBoundary('multipart/alternative') . '"'), 'text/plain' => array('Content-Type' => 'text/plain; charset=UTF-8', 'Content-Transfer-Encoding' => $this->_text_encoding ? $this->_text_encoding : '8bit'), 'text/html' => array('Content-Type' => 'text/html; charset=UTF-8', 'Content-Transfer-Encoding' => $this->_text_encoding ? $this->_text_encoding : '8bit')); // Try common if (!empty($type) && !empty($description[$type])) { // return it if found return $description[$type]; } // assure we have a file name $filename = !is_null($filename) ? $filename : basename($file); // Format charset for insertion in content-type, if needed if (!empty($charset)) { $charset = sprintf('charset=%s;', $charset); } else { $charset = ''; } // Return binary description return array('Content-Type' => General::getMimeType($file) . ';' . $charset . ' name="' . $filename . '"', 'Content-Transfer-Encoding' => 'base64', 'Content-Disposition' => 'attachment; filename="' . $filename . '"'); }
public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = NULL) { $status = self::__OK__; //fixes bug where files are deleted, but their database entries are not. if ($data === NULL) { return array('file' => NULL, 'mimetype' => NULL, 'size' => NULL, 'meta' => NULL); } ## Its not an array, so just retain the current data and return (the case where we're not uploading a new file) if (!is_array($data)) { $result = array('file' => $data, 'mimetype' => NULL, 'size' => NULL, 'meta' => NULL); // Grab the existing entry data to preserve the MIME type and size information if (isset($entry_id) && !is_null($entry_id)) { $row = Symphony::Database()->fetchRow(0, sprintf("SELECT `file`, `mimetype`, `size`, `meta` FROM `tbl_entries_data_%d` WHERE `entry_id` = %d", $this->get('id'), $entry_id)); if (!empty($row)) { $result = $row; } } return $result; } if ($this->get('unique_filename') == true && isset($data['name'])) { $this->getUniqueFilename($data['name']); } // Editing an entry: Where we're uploading a new file and getting rid of the old one if (is_null($entry_id) === false) { $row = Symphony::Database()->fetchRow(0, sprintf("\r\n SELECT * FROM `tbl_entries_data_%d` WHERE `entry_id` = %d LIMIT 1\r\n ", $this->get('id'), $entry_id)); $existing_file = $row['file']; if (!is_null($existing_file) && strtolower($existing_file) != strtolower($data['file']) || $data['error'] == UPLOAD_ERR_NO_FILE && !is_null($existing_file)) { $this->s3->deleteObject($this->get('bucket'), basename($existing_file)); } } if ($data['error'] == UPLOAD_ERR_NO_FILE || $data['error'] != UPLOAD_ERR_OK) { return false; } // Sanitize the filename $data['name'] = Lang::createFilename($data['name']); ## Upload the new file $options = array('ACL' => 'public-read', 'ContentType' => $data['type']); if ($this->_driver->getCacheControl() != false) { $options['CacheControl'] = "max-age=" . $this->_driver->getCacheControl(); } try { $this->s3->putObject($this->get('bucket'), $data['name'], $data['tmp_name'], $options); } catch (Exception $e) { $status = self::__ERROR_CUSTOM__; $message = __(__('There was an error while trying to upload the file %s to the bucket %s.'), array('<code>' . $data['name'] . '</code>', '<code>' . $this->get('bucket') . '</code>')); return array('file' => NULL, 'mimetype' => NULL, 'size' => NULL, 'meta' => NULL); } $status = self::__OK__; // Get the mimetype, don't trust the browser. RE: #1609 $data['type'] = General::getMimeType($data['tmp_name']); // all we need is the path and name, the domain is abstracted depending on whether or not it has a cname return array('file' => $data['name'], 'size' => $data['size'], 'mimetype' => $data['type'], 'meta' => serialize(parent::getMetaInfo($data['tmp_name'], $data['type']))); }
public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null) { $status = self::__OK__; // No file given, save empty data: if ($data === null) { return array('file' => null, 'mimetype' => null, 'size' => null, 'meta' => null); } // Its not an array, so just retain the current data and return: if (is_array($data) === false) { $file = $this->getFilePath(basename($data)); $result = array('file' => $data, 'mimetype' => null, 'size' => null, 'meta' => null); // Grab the existing entry data to preserve the MIME type and size information if (isset($entry_id)) { $row = Symphony::Database()->fetchRow(0, sprintf("SELECT `file`, `mimetype`, `size`, `meta` FROM `tbl_entries_data_%d` WHERE `entry_id` = %d", $this->get('id'), $entry_id)); if (empty($row) === false) { $result = $row; } } // Found the file, add any missing meta information: if (file_exists($file) && is_readable($file)) { if (empty($result['mimetype'])) { $result['mimetype'] = General::getMimeType($file); } if (empty($result['size'])) { $result['size'] = filesize($file); } if (empty($result['meta'])) { $result['meta'] = serialize(self::getMetaInfo($file, $result['mimetype'])); } // The file was not found, or is unreadable: } else { $message = __('The file uploaded is no longer available. Please check that it exists, and is readable.'); $status = self::__INVALID_FIELDS__; } return $result; } if ($simulate && is_null($entry_id)) { return $data; } // Check to see if the entry already has a file associated with it: if (is_null($entry_id) === false) { $row = Symphony::Database()->fetchRow(0, sprintf("SELECT *\n FROM `tbl_entries_data_%s`\n WHERE `entry_id` = %d\n LIMIT 1", $this->get('id'), $entry_id)); $existing_file = isset($row['file']) ? $this->getFilePath($row['file']) : null; // File was removed: if ($data['error'] == UPLOAD_ERR_NO_FILE && !is_null($existing_file) && is_file($existing_file)) { General::deleteFile($existing_file); } } // Do not continue on upload error: if ($data['error'] == UPLOAD_ERR_NO_FILE || $data['error'] != UPLOAD_ERR_OK) { return false; } // Where to upload the new file? $abs_path = DOCROOT . '/' . trim($this->get('destination'), '/'); $rel_path = str_replace('/workspace', '', $this->get('destination')); // Sanitize the filename $data['name'] = Lang::createFilename($data['name']); // If a file already exists, then rename the file being uploaded by // adding `_1` to the filename. If `_1` already exists, the logic // will keep adding 1 until a filename is available (#672) if (file_exists($abs_path . '/' . $data['name'])) { $extension = General::getExtension($data['name']); $new_file = substr($abs_path . '/' . $data['name'], 0, -1 - strlen($extension)); $renamed_file = $new_file; $count = 1; do { $renamed_file = $new_file . '_' . $count . '.' . $extension; $count++; } while (file_exists($renamed_file)); // Extract the name filename from `$renamed_file`. $data['name'] = str_replace($abs_path . '/', '', $renamed_file); } $file = $this->getFilePath($data['name']); // Attempt to upload the file: $uploaded = General::uploadFile($abs_path, $data['name'], $data['tmp_name'], Symphony::Configuration()->get('write_mode', 'file')); if ($uploaded === false) { $message = __('There was an error while trying to upload the file %1$s to the target directory %2$s.', array('<code>' . $data['name'] . '</code>', '<code>workspace/' . ltrim($rel_path, '/') . '</code>')); $status = self::__ERROR_CUSTOM__; return false; } // File has been replaced: if (isset($existing_file) && $existing_file !== $file && is_file($existing_file)) { General::deleteFile($existing_file); } // Get the mimetype, don't trust the browser. RE: #1609 $data['type'] = General::getMimeType($file); return array('file' => basename($file), 'size' => $data['size'], 'mimetype' => $data['type'], 'meta' => serialize(self::getMetaInfo($file, $data['type']))); }
/** * Builds the right content-type/encoding types based on file and * content-type. * * Will return a string containing the section, or an empty array on * failure. Can be used to send to an email server directly. * @return string */ public function contentInfoArray($type = NULL, $file = NULL, $filename = NULL) { $description = array('multipart/mixed' => array("Content-Type" => 'multipart/mixed; boundary="' . $this->getBoundary('multipart/mixed') . '"'), 'multipart/alternative' => array('Content-Type' => 'multipart/alternative; boundary="' . $this->getBoundary('multipart/alternative') . '"'), 'text/plain' => array('Content-Type' => 'text/plain; charset=UTF-8', 'Content-Transfer-Encoding' => $this->_text_encoding ? $this->_text_encoding : '8bit'), 'text/html' => array('Content-Type' => 'text/html; charset=UTF-8', 'Content-Transfer-Encoding' => $this->_text_encoding ? $this->_text_encoding : '8bit')); $binary = array('Content-Type' => General::getMimeType($file) . '; name="' . (!is_null($filename) ? $filename : basename($file)) . '"', 'Content-Transfer-Encoding' => 'base64', 'Content-Disposition' => 'attachment; filename="' . (!is_null($filename) ? $filename : basename($file)) . '"'); return !empty($description[$type]) ? $description[$type] : (!is_null($filename) ? $filename : basename($file) ? $binary : array()); }
/** * Gets mime type of a file. * * For email attachments, the mime type is very important. * Uses the PHP 5.3 function `finfo_open` when available, otherwise falls * back to using a mapping of known of common mimetypes. If no matches * are found `application/octet-stream` will be returned. * * @deprecated This function is deprecated from the `EmailHelper` class, * and has been moved to the `General` class. It is recommended to use * `General::getMimeType` instead as it will be removed from here in the * next major release. * @author Michael Eichelsdoerfer * @author Huib Keemink * @param string $file * @return string MIMEtype */ public function getMimeType($file) { return General::getMimeType($file); }