/** * process attachments by saving into media directory and optionally creating image tag in post * * @param string message content that is optionally manipulated by adding image tags (by reference) * @param array $mailAttachments array containing path to attachment files * @param string $mediadir path to media directory of blog as seen by file system * @param string $media_url url to media directory as seen by user * @param bool $add_img_tags should img tags be added to the post (instead of linking through the file manager) * @param string $type defines attachment type: 'attach' or 'related' */ function pbm_process_attachments(&$content, $mailAttachments, $mediadir, $media_url, $add_img_tags = true, $type = 'attach') { global $Settings, $pbm_item_files, $filename_max_length; pbm_msg('<h4>Processing attachments</h4>'); foreach ($mailAttachments as $attachment) { if (isset($attachment['FileName'])) { $filename = trim(evo_strtolower($attachment['FileName'])); } else { // Related attachments may not have file name, we'll generate one below $filename = ''; } if ($filename == '') { $filename = 'upload_' . uniqid() . '.' . $attachment['SubType']; pbm_msg(sprintf('Attachment without name. Using "%s".', htmlspecialchars($filename))); } // Check valid filename/extension: (includes check for locked filenames) if ($error_filename = process_filename($filename, true)) { pbm_msg('Invalid filename: ' . $error_filename); continue; } // If file exists count up a number $cnt = 0; $prename = substr($filename, 0, strrpos($filename, '.')) . '-'; $sufname = strrchr($filename, '.'); $error_in_filename = false; while (file_exists($mediadir . $filename)) { $filename = $prename . $cnt . $sufname; if (strlen($filename) > $filename_max_length) { // This is a special case, when the filename is longer then the maximum allowed // Cut as many characters as required before the counter on the file name $filename = fix_filename_length($filename, strlen($prename) - 1); if ($error_in_filename = process_filename($filename, true)) { // The file name is not valid, this is an unexpected situation, because the file name was already validated before pbm_msg('Invalid filename: ' . $error_filename); break; } } ++$cnt; } if ($error_in_filename) { // Don't create file with invalid file name continue; } pbm_msg(sprintf('New file name is <b>%s</b>', $filename)); $imginfo = NULL; if (!$Settings->get('eblog_test_mode')) { pbm_msg('Saving file to: ' . htmlspecialchars($mediadir . $filename)); if (!copy($attachment['DataFile'], $mediadir . $filename)) { pbm_msg('Unable to copy uploaded file to ' . htmlspecialchars($mediadir . $filename)); continue; } // chmod uploaded file: $chmod = $Settings->get('fm_default_chmod_file'); @chmod($mediadir . $filename, octdec($chmod)); $imginfo = @getimagesize($mediadir . $filename); pbm_msg('Is this an image?: ' . (is_array($imginfo) ? 'yes' : 'no')); } if ($type == 'attach') { $content .= "\n"; if (is_array($imginfo) && $add_img_tags) { $content .= '<img src="' . $media_url . $filename . '" ' . $imginfo[3] . ' />'; } else { pbm_msg(sprintf('The file <b>%s</b> will be attached to the post later, after we save the post in the database.', $filename)); $pbm_item_files[] = $filename; } $content .= "\n"; } elseif (!empty($attachment['ContentID'])) { // Replace relative "cid:xxxxx" URIs with absolute URLs to media files $content = str_replace('cid:' . $attachment['ContentID'], $media_url . $filename, $content); } } }
/** * Check if file exists in the target location with the given name. Used during file upload. * * @param FileRoot target file Root * @param string target path * @param string file name * @param array the new file image_info * @return array contains two elements * first elements is a new File object * second element is the existing file thumb, or empty string, if the file doesn't exists */ function check_file_exists($fm_FileRoot, $path, $newName, $image_info = NULL) { global $filename_max_length; // Get File object for requested target location: $FileCache =& get_FileCache(); $newFile =& $FileCache->get_by_root_and_path($fm_FileRoot->type, $fm_FileRoot->in_type_ID, trailing_slash($path) . $newName, true); $num_ext = 0; $oldName = $newName; $oldFile_thumb = ""; while ($newFile->exists()) { // The file already exists in the target location! $num_ext++; $ext_pos = strrpos($newName, '.'); if ($num_ext == 1) { if ($image_info == NULL) { $image_info = getimagesize($newFile->get_full_path()); } $newName = substr_replace($newName, '-' . $num_ext . '.', $ext_pos, 1); if ($image_info) { $oldFile_thumb = $newFile->get_preview_thumb('fulltype'); } else { $oldFile_thumb = $newFile->get_size_formatted(); } } else { $replace_length = strlen('-' . ($num_ext - 1)); $newName = substr_replace($newName, '-' . $num_ext, $ext_pos - $replace_length, $replace_length); } if (strlen($newName) > $filename_max_length) { $newName = fix_filename_length($newName, strrpos($newName, '-')); if ($error_filename = process_filename($newName, true)) { // The file name is still not valid debug_die('Invalid file name has found during file exists check: ' . $error_filename); } } $newFile =& $FileCache->get_by_root_and_path($fm_FileRoot->type, $fm_FileRoot->in_type_ID, trailing_slash($path) . $newName, true); } return array($newFile, $oldFile_thumb); }