public bool CreateNewIssueAttachment(int issueId, string creatorUserName, string fileName, string contentType, byte[] attachment, int size, string description)
        {
            if (issueId <= 0)
            {
                throw new ArgumentOutOfRangeException("issueId");
            }

            var projectId = IssueManager.GetById(issueId).ProjectId;

            //authentication checks against user access to project
            if (ProjectManager.GetById(projectId).AccessType == Common.ProjectAccessType.Private && !ProjectManager.IsUserProjectMember(UserName, projectId))
            {
                throw new UnauthorizedAccessException(string.Format(LoggingManager.GetErrorMessageResource("ProjectAccessDenied"), UserName));
            }

            var issueAttachment = new IssueAttachment
            {
                Id                 = Globals.NEW_ID,
                Attachment         = attachment,
                Description        = description,
                DateCreated        = DateTime.Now,
                ContentType        = contentType,
                CreatorDisplayName = string.Empty,
                CreatorUserName    = creatorUserName,
                FileName           = fileName,
                IssueId            = issueId,
                Size               = size
            };

            return(IssueAttachmentManager.SaveOrUpdate(issueAttachment));
        }
Example #2
0
        /// <summary>
        /// Saves the mailbox entry.
        /// </summary>
        /// <param name="entry">The entry.</param>
        void SaveMailboxEntry(MailboxEntry entry)
        {
            try
            {
                //load template
                var body = string.Format("<div >Sent by:{1} on: {2}<br/>{0}</div>", entry.Content.Trim(), entry.From, entry.Date);

                if (Config.BodyTemplate.Trim().Length > 0)
                {
                    var data = new Dictionary <string, object> {
                        { "MailboxEntry", entry }
                    };
                    body = NotificationManager.GenerateNotificationContent(Config.BodyTemplate, data);
                }

                var projectId = entry.ProjectMailbox.ProjectId;

                var mailIssue = IssueManager.GetDefaultIssueByProjectId(
                    projectId,
                    entry.Title.Trim(),
                    body.Trim(),
                    entry.ProjectMailbox.AssignToUserName,
                    Config.ReportingUserName);

                if (!IssueManager.SaveOrUpdate(mailIssue))
                {
                    return;
                }

                entry.IssueId      = mailIssue.Id;
                entry.WasProcessed = true;

                var project = ProjectManager.GetById(projectId);

                var projectFolderPath = Path.Combine(Config.UploadsFolderPath, project.UploadPath);

                var doc = new HtmlDocument();
                doc.LoadHtml(mailIssue.Description); // load the issue body to we can process it for inline images (if exist)

                //If there is an attached file present then add it to the database
                //and copy it to the directory specified in the web.config file
                foreach (MIME_Entity mimeEntity in entry.MailAttachments)
                {
                    string fileName;
                    var    isInline    = false;
                    var    contentType = mimeEntity.ContentType.Type.ToLower();

                    var attachment = new IssueAttachment
                    {
                        Id                 = 0,
                        Description        = "File attached by mailbox reader",
                        DateCreated        = DateTime.Now,
                        ContentType        = mimeEntity.ContentType.TypeWithSubtype,
                        CreatorDisplayName = Config.ReportingUserName,
                        CreatorUserName    = Config.ReportingUserName,
                        IssueId            = mailIssue.Id,
                        ProjectFolderPath  = projectFolderPath
                    };

                    switch (contentType)
                    {
                    case "application":
                        attachment.Attachment = ((MIME_b_SinglepartBase)mimeEntity.Body).Data;
                        break;

                    case "attachment":
                    case "image":
                    case "video":
                    case "audio":

                        attachment.Attachment = ((MIME_b_SinglepartBase)mimeEntity.Body).Data;
                        break;

                    case "message":

                        // we need to pull the actual email message out of the entity, and strip the "content type" out so that
                        // email programs will read the file properly
                        var messageBody = mimeEntity.ToString().Replace(mimeEntity.Header.ToString(), "");
                        if (messageBody.StartsWith("\r\n"))
                        {
                            messageBody = messageBody.Substring(2);
                        }

                        attachment.Attachment = Encoding.UTF8.GetBytes(messageBody);

                        break;

                    default:
                        LogWarning(string.Format("MailboxReader: Attachment type could not be processed {0}", mimeEntity.ContentType.Type));
                        break;
                    }

                    if (contentType.Equals("attachment")) // this is an attached email
                    {
                        fileName = mimeEntity.ContentDisposition.Param_FileName;
                    }
                    else if (contentType.Equals("message")) // message has no filename so we create one
                    {
                        fileName = string.Format("Attached_Message_{0}.eml", entry.AttachmentsSavedCount);
                    }
                    else
                    {
                        isInline = true;
                        fileName = string.IsNullOrWhiteSpace(mimeEntity.ContentType.Param_Name) ?
                                   string.Format("untitled.{0}", mimeEntity.ContentType.SubType) :
                                   mimeEntity.ContentType.Param_Name;
                    }

                    attachment.FileName = fileName;

                    var saveFile  = IsAllowedFileExtension(fileName);
                    var fileSaved = false;

                    // can we save the file?
                    if (saveFile)
                    {
                        fileSaved = IssueAttachmentManager.SaveOrUpdate(attachment);

                        if (fileSaved)
                        {
                            entry.AttachmentsSavedCount++;
                        }
                        else
                        {
                            LogWarning("MailboxReader: Attachment could not be saved, please see previous logs");
                        }
                    }

                    if (!entry.IsHtml || !isInline)
                    {
                        continue;
                    }

                    if (string.IsNullOrWhiteSpace(mimeEntity.ContentID))
                    {
                        continue;
                    }

                    var contentId = mimeEntity.ContentID.Replace("<", "").Replace(">", "").Replace("[", "").Replace("]", "");

                    // this is pretty greedy but since people might be sending screenshots I doubt they will send in dozens of images
                    // embedded in the email.  one would hope
                    foreach (var node in doc.DocumentNode.SelectNodes(XpathElementCaseInsensitive("img")).ToList())
                    {
                        var attr = node.Attributes.FirstOrDefault(p => p.Name.ToLowerInvariant() == "src");// get the src attribute

                        if (attr == null)
                        {
                            continue;               // image has no src attribute
                        }
                        if (!attr.Value.Contains(contentId))
                        {
                            continue;                                  // is the attribute value the content id?
                        }
                        // swap out the content of the parent node html will our link to the image
                        var anchor = string.Format("<span class='inline-mail-attachment'>Inline Attachment: <a href='DownloadAttachment.axd?id={0}' target='_blank'>{1}</a></span>", attachment.Id, fileName);

                        // for each image in the body if the file was saved swap out the inline link for a link to the saved attachment
                        // otherwise blank out the content link so we don't get a missing image link
                        node.ParentNode.InnerHtml = fileSaved ? anchor : "";
                    }

                    mailIssue.Description        = doc.DocumentNode.InnerHtml;
                    mailIssue.LastUpdateUserName = mailIssue.OwnerUserName;
                    mailIssue.LastUpdate         = DateTime.Now;

                    IssueManager.SaveOrUpdate(mailIssue);
                }
            }
            catch (Exception ex)
            {
                LogException(ex);
                throw;
            }
        }
Example #3
0
        /// <summary>
        /// Uploads the document.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void UploadDocument(object sender, EventArgs e)
        {
            // get the current file
            var uploadFile = AspUploadFile.PostedFile;

            // if there was a file uploaded
            if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                var inValidReason = string.Empty;
                var fileName = Path.GetFileName(uploadFile.FileName);

                var validFile = IssueAttachmentManager.IsValidFile(fileName, out inValidReason);

                if (validFile)
                {
                    byte[] fileBytes;
                    using (var input = uploadFile.InputStream)
                    {
                        fileBytes = new byte[uploadFile.ContentLength];
                        input.Read(fileBytes, 0, uploadFile.ContentLength);
                    }

                    var attachment = new IssueAttachment
                    {
                        Id = Globals.NEW_ID,
                        Attachment = fileBytes,
                        Description = AttachmentDescription.Text.Trim(),
                        DateCreated = DateTime.Now,
                        ContentType = uploadFile.ContentType,
                        CreatorDisplayName = string.Empty,
                        CreatorUserName = Security.GetUserName(),
                        FileName = fileName,
                        IssueId = IssueId,
                        Size = fileBytes.Length
                    };

                    if (!IssueAttachmentManager.SaveOrUpdate(attachment))
                    {
                        AttachmentsMessage.ShowErrorMessage(string.Format(GetGlobalResourceObject("Exceptions", "SaveAttachmentError").ToString(), uploadFile.FileName));
                        if (Log.IsWarnEnabled) Log.Warn(string.Format(GetGlobalResourceObject("Exceptions", "SaveAttachmentError").ToString(), uploadFile.FileName));
                        return;
                    }

                    //add history record and send notifications
                    var history = new IssueHistory
                    {
                        IssueId = IssueId,
                        CreatedUserName = Security.GetUserName(),
                        DateChanged = DateTime.Now,
                        FieldChanged = ResourceStrings.GetGlobalResource(GlobalResources.SharedResources, "Attachment", "Attachment"),
                        OldValue = fileName,
                        NewValue = ResourceStrings.GetGlobalResource(GlobalResources.SharedResources, "Added", "Added"),
                        TriggerLastUpdateChange = true
                    };

                    IssueHistoryManager.SaveOrUpdate(history);

                    var changes = new List<IssueHistory> { history };

                    IssueNotificationManager.SendIssueNotifications(IssueId, changes);

                    BindAttachments();
                }
                else
                    AttachmentsMessage.ShowErrorMessage(inValidReason); 
            }
        }
Example #4
0
        /// <summary>
        /// Saves the issue.
        /// </summary>
        /// <returns></returns>
        private bool SaveIssue()
        {
            decimal estimation;

            decimal.TryParse(txtEstimation.Text.Trim(), out estimation);
            var dueDate = DueDatePicker.SelectedValue ?? DateTime.MinValue;

            var issue = new Issue
            {
                AffectedMilestoneId       = DropAffectedMilestone.SelectedValue,
                AffectedMilestoneImageUrl = string.Empty,
                AffectedMilestoneName     = DropAffectedMilestone.SelectedText,
                AssignedDisplayName       = DropAssignedTo.SelectedText,
                AssignedUserId            = Guid.Empty,
                AssignedUserName          = DropAssignedTo.SelectedValue,
                CategoryId         = DropCategory.SelectedValue,
                CategoryName       = DropCategory.SelectedText,
                CreatorDisplayName = Security.GetDisplayName(),
                CreatorUserId      = Guid.Empty,
                CreatorUserName    = Security.GetUserName(),
                DateCreated        = DateTime.Now,
                Description        = DescriptionHtmlEditor.Text.Trim(),
                Disabled           = false,
                DueDate            = dueDate,
                Estimation         = estimation,
                Id                    = 0,
                IsClosed              = false,
                IssueTypeId           = DropIssueType.SelectedValue,
                IssueTypeName         = DropIssueType.SelectedText,
                IssueTypeImageUrl     = string.Empty,
                LastUpdate            = DateTime.Now,
                LastUpdateDisplayName = Security.GetDisplayName(),
                LastUpdateUserName    = Security.GetUserName(),
                MilestoneDueDate      = null,
                MilestoneId           = DropMilestone.SelectedValue,
                MilestoneImageUrl     = string.Empty,
                MilestoneName         = DropMilestone.SelectedText,
                OwnerDisplayName      = DropOwned.SelectedText,
                OwnerUserId           = Guid.Empty,
                OwnerUserName         = DropOwned.SelectedValue,
                PriorityId            = DropPriority.SelectedValue,
                PriorityImageUrl      = string.Empty,
                PriorityName          = DropPriority.SelectedText,
                Progress              = Convert.ToInt32(ProgressSlider.Text),
                ProjectCode           = string.Empty,
                ProjectId             = ProjectId,
                ProjectName           = string.Empty,
                ResolutionId          = DropResolution.SelectedValue,
                ResolutionImageUrl    = string.Empty,
                ResolutionName        = DropResolution.SelectedText,
                StatusId              = DropStatus.SelectedValue,
                StatusImageUrl        = string.Empty,
                StatusName            = DropStatus.SelectedText,
                Title                 = Server.HtmlEncode(TitleTextBox.Text),
                TimeLogged            = 0,
                Visibility            = chkPrivate.Checked ? 1 : 0,
                Votes                 = 0
            };

            if (!IssueManager.SaveOrUpdate(issue))
            {
                Message1.ShowErrorMessage(Resources.Exceptions.SaveIssueError);
                return(false);
            }

            if (!CustomFieldManager.SaveCustomFieldValues(issue.Id, ctlCustomFields.Values, true))
            {
                Message1.ShowErrorMessage(Resources.Exceptions.SaveCustomFieldValuesError);
                return(false);
            }

            IssueId = issue.Id;

            //add attachment if present.
            if (AspUploadFile.HasFile)
            {
                // get the current file
                var    uploadFile = AspUploadFile.PostedFile;
                string inValidReason;
                var    validFile = IssueAttachmentManager.IsValidFile(uploadFile.FileName, out inValidReason);

                if (validFile)
                {
                    if (uploadFile.ContentLength > 0)
                    {
                        byte[] fileBytes;
                        using (var input = uploadFile.InputStream)
                        {
                            fileBytes = new byte[uploadFile.ContentLength];
                            input.Read(fileBytes, 0, uploadFile.ContentLength);
                        }

                        var issueAttachment = new IssueAttachment
                        {
                            Id                 = Globals.NEW_ID,
                            Attachment         = fileBytes,
                            Description        = AttachmentDescription.Text.Trim(),
                            DateCreated        = DateTime.Now,
                            ContentType        = uploadFile.ContentType,
                            CreatorDisplayName = string.Empty,
                            CreatorUserName    = Security.GetUserName(),
                            FileName           = uploadFile.FileName,
                            IssueId            = issue.Id,
                            Size               = fileBytes.Length
                        };

                        if (!IssueAttachmentManager.SaveOrUpdate(issueAttachment))
                        {
                            Message1.ShowErrorMessage(string.Format(GetGlobalResourceObject("Exceptions", "SaveAttachmentError").ToString(), uploadFile.FileName));
                        }
                    }
                }
                else
                {
                    Message1.ShowErrorMessage(inValidReason);
                    return(false);
                }
            }

            //create a vote for the new issue
            var vote = new IssueVote {
                IssueId = issue.Id, VoteUsername = Security.GetUserName()
            };

            if (!IssueVoteManager.SaveOrUpdate(vote))
            {
                Message1.ShowErrorMessage(Resources.Exceptions.SaveIssueVoteError);
                return(false);
            }

            if (chkNotifyOwner.Checked && !string.IsNullOrEmpty(issue.OwnerUserName))
            {
                var oUser = UserManager.GetUser(issue.OwnerUserName);
                if (oUser != null)
                {
                    var notify = new IssueNotification {
                        IssueId = issue.Id, NotificationUsername = oUser.UserName
                    };
                    IssueNotificationManager.SaveOrUpdate(notify);
                }
            }
            if (chkNotifyAssignedTo.Checked && !string.IsNullOrEmpty(issue.AssignedUserName))
            {
                var oUser = UserManager.GetUser(issue.AssignedUserName);
                if (oUser != null)
                {
                    var notify = new IssueNotification {
                        IssueId = issue.Id, NotificationUsername = oUser.UserName
                    };
                    IssueNotificationManager.SaveOrUpdate(notify);
                }
            }

            //send issue notifications
            IssueNotificationManager.SendIssueAddNotifications(issue.Id);

            return(true);
        }
Example #5
0
        private bool ProcessNewComment(List <string> recipients, POP3_ClientMessage message, Mail_Message mailHeader, MailboxReaderResult result)
        {
            string messageFrom = string.Empty;

            if (mailHeader.From.Count > 0)
            {
                messageFrom = string.Join("; ", mailHeader.From.ToList().Select(p => p.Address).ToArray()).Trim();
            }

            bool processed = false;

            foreach (var address in recipients)
            {
                Regex isReply      = new Regex(@"(.*)(\+iid-)(\d+)@(.*)");
                Match commentMatch = isReply.Match(address);
                if (commentMatch.Success && commentMatch.Groups.Count >= 4)
                {
                    // we are in a reply and group 4 must contain the id of the original issue
                    int issueId;
                    if (int.TryParse(commentMatch.Groups[3].Value, out issueId))
                    {
                        var _currentIssue = IssueManager.GetById(issueId);

                        if (_currentIssue != null)
                        {
                            var project = ProjectManager.GetById(_currentIssue.ProjectId);

                            var mailbody = Mail_Message.ParseFromByte(message.MessageToByte());

                            bool isHtml;
                            List <MIME_Entity> attachments = null;
                            string             content     = GetMessageContent(mailbody, project, out isHtml, ref attachments);

                            IssueComment comment = new IssueComment
                            {
                                IssueId     = issueId,
                                Comment     = content,
                                DateCreated = mailHeader.Date
                            };

                            // try to find if the creator is valid user in the project, otherwise take
                            // the user defined in the mailbox config
                            var users  = UserManager.GetUsersByProjectId(project.Id);
                            var emails = messageFrom.Split(';').Select(e => e.Trim().ToLower());
                            var user   = users.Find(x => emails.Contains(x.Email.ToLower()));
                            if (user != null)
                            {
                                comment.CreatorUserName = user.UserName;
                            }
                            else
                            {
                                // user not found
                                continue;
                            }

                            var saved = IssueCommentManager.SaveOrUpdate(comment);
                            if (saved)
                            {
                                //add history record
                                var history = new IssueHistory
                                {
                                    IssueId                 = issueId,
                                    CreatedUserName         = comment.CreatorUserName,
                                    DateChanged             = comment.DateCreated,
                                    FieldChanged            = ResourceStrings.GetGlobalResource(GlobalResources.SharedResources, "Comment", "Comment"),
                                    OldValue                = string.Empty,
                                    NewValue                = ResourceStrings.GetGlobalResource(GlobalResources.SharedResources, "Added", "Added"),
                                    TriggerLastUpdateChange = true
                                };
                                IssueHistoryManager.SaveOrUpdate(history);

                                var projectFolderPath = Path.Combine(Config.UploadsFolderPath, project.UploadPath);

                                // save attachments as new files
                                int attachmentsSavedCount = 1;
                                foreach (MIME_Entity mimeEntity in attachments)
                                {
                                    string fileName;
                                    var    contentType = mimeEntity.ContentType.Type.ToLower();

                                    var attachment = new IssueAttachment
                                    {
                                        Id                 = 0,
                                        Description        = "File attached by mailbox reader",
                                        DateCreated        = DateTime.Now,
                                        ContentType        = mimeEntity.ContentType.TypeWithSubtype,
                                        CreatorDisplayName = user.DisplayName,
                                        CreatorUserName    = user.UserName,
                                        IssueId            = issueId,
                                        ProjectFolderPath  = projectFolderPath
                                    };
                                    attachment.Attachment = ((MIME_b_SinglepartBase)mimeEntity.Body).Data;

                                    if (contentType.Equals("attachment")) // this is an attached email
                                    {
                                        fileName = mimeEntity.ContentDisposition.Param_FileName;
                                    }
                                    else if (contentType.Equals("message")) // message has no filename so we create one
                                    {
                                        fileName = string.Format("Attached_Message_{0}.eml", attachmentsSavedCount);
                                    }
                                    else
                                    {
                                        fileName = string.IsNullOrWhiteSpace(mimeEntity.ContentType.Param_Name) ?
                                                   string.Format("untitled.{0}", mimeEntity.ContentType.SubType) :
                                                   mimeEntity.ContentType.Param_Name;
                                    }

                                    attachment.FileName = fileName;

                                    var saveFile  = IsAllowedFileExtension(fileName);
                                    var fileSaved = false;

                                    // can we save the file?
                                    if (saveFile)
                                    {
                                        fileSaved = IssueAttachmentManager.SaveOrUpdate(attachment);

                                        if (fileSaved)
                                        {
                                            attachmentsSavedCount++;
                                        }
                                        else
                                        {
                                            LogWarning("MailboxReader: Attachment could not be saved, please see previous logs");
                                        }
                                    }
                                }

                                processed = true;

                                // add the entry if the save did not throw any exceptions
                                result.MailboxEntries.Add(new MailboxEntry());
                            }
                        }
                    }
                }
            }
            return(processed);
        }