Exemple #1
0
        /// <summary>
        /// Notify the user of an event, optionally Bcc admins.
        /// </summary>
        /// <param name="szSubject">The subject to send</param>
        /// <param name="szMessage">The message to send  This will be rebranded per Branding.ReBrand().</param>
        /// <param name="maUser">The address of the recipient</param>
        /// <param name="fCcAdmins">True if you want Admins CC'd; false if not</param>
        /// <param name="fIsHTML">True if the content of the message is HTML</param>
        /// <param name="brand">The branding to use</param>
        /// <param name="RoleMask">The roles to whom this should go (if admin)</param>
        static private void NotifyUser(string szSubject, string szMessage, MailAddress maUser, bool fCcAdmins, bool fIsHTML, Brand brand, uint RoleMask)
        {
            try
            {
                if (brand == null)
                {
                    brand = Branding.CurrentBrand;
                }

                using (MailMessage msg = new MailMessage())
                {
                    msg.Subject    = szSubject;
                    msg.Body       = Branding.ReBrand(szMessage, brand);
                    msg.IsBodyHtml = fIsHTML || szMessage.Contains("<!DOCTYPE");

                    msg.From = new MailAddress(brand.EmailAddress, brand.AppName);

                    if (maUser != null)
                    {
                        msg.To.Add(maUser);
                    }

                    if (fCcAdmins)
                    {
                        AddAdminsToMessage(msg, (maUser == null), RoleMask);
                    }

                    SendMessage(msg);
                }
            }
            catch (ArgumentNullException ex)
            {
                MyFlightbookException mfbEx = new MyFlightbookException(String.Format(CultureInfo.CurrentCulture, "Null Argument in NotifyUser: {0}", ex.ParamName), ex);
                MyFlightbookException.NotifyAdminException(mfbEx);
            }
            catch (InvalidOperationException ex)
            {
                MyFlightbookException mfbEx = new MyFlightbookException("Invalid Operation in NotifyUser", ex);
                MyFlightbookException.NotifyAdminException(mfbEx);
            }
            catch (System.Net.Mail.SmtpException)
            {
                // Don't re-throw or do anything that would cause new mail to be sent because that could loop.  Just fail silently and eat this.
            }
        }
Exemple #2
0
        /// <summary>
        /// Saves a the user's logbook to GoogleDrive, if configured.
        /// </summary>
        /// <exception cref="MyFlightbookException"></exception>
        /// <exception cref="UnauthorizedAccessException"></exception>
        /// <param name="gd">The GoogleDrive object to use - Must be initialized and refreshed!</param>
        /// <param name="activeBrand">The brand to use.  Current brand is used if null.</param>
        /// <returns>True for success</returns>
        public async Task <IReadOnlyDictionary <string, string> > BackupToGoogleDrive(GoogleDrive gd, Brand activeBrand = null)
        {
            if (gd == null)
            {
                throw new ArgumentNullException(nameof(gd));
            }

            if (User.GoogleDriveAccessToken == null)
            {
                throw new MyFlightbookException(Resources.Profile.errNotConfiguredGoogleDrive);
            }

            if (activeBrand == null)
            {
                activeBrand = Branding.CurrentBrand;
            }

            return(await gd.PutFile(BackupFilename(activeBrand), LogbookDataForBackup(), "text/csv"));
        }
Exemple #3
0
        /// <summary>
        /// Saves a zip of the user's images to GoogleDrive, if configured.
        /// </summary>
        /// <exception cref="MyFlightbookException"></exception>
        /// <param name="activeBrand">The brand to use.  Current brand is used if null.</param>
        /// <param name="gd">The GoogleDrive object to use - Must be initialized and refreshed!</param>
        /// <returns>True for success</returns>
        public async Task <IReadOnlyDictionary <string, string> > BackupImagesToGoogleDrive(GoogleDrive gd, Brand activeBrand = null)
        {
            if (activeBrand == null)
            {
                activeBrand = Branding.CurrentBrand;
            }

            if (gd == null)
            {
                throw new ArgumentNullException(nameof(gd));
            }

            if (User.GoogleDriveAccessToken == null)
            {
                throw new MyFlightbookException(Resources.Profile.errNotConfiguredGoogleDrive);
            }

            using (MemoryStream ms = ZipOfImagesForUser(activeBrand))
            {
                return(await gd.PutFile(ms, BackupImagesFilename(activeBrand, true), "application/zip"));
            }
        }
Exemple #4
0
        /// <summary>
        /// Saves a the user's logbook to OneDrive, if configured.
        /// </summary>
        /// <exception cref="MyFlightbookException"></exception>
        /// <exception cref="Microsoft.OneDrive.Sdk.OneDriveException"></exception>
        /// <exception cref="UnauthorizedAccessException"></exception>
        /// <param name="od">The OneDrive object to use (one will be initialized, if necessary)</param>
        /// <param name="activeBrand">The brand to use.  Current brand is used if null.</param>
        public async Task <Microsoft.OneDrive.Sdk.Item> BackupToOneDrive(OneDrive od = null, Brand activeBrand = null)
        {
            if (User.OneDriveAccessToken == null)
            {
                throw new MyFlightbookException(Resources.Profile.errNotConfiguredOneDrive);
            }

            if (activeBrand == null)
            {
                activeBrand = Branding.CurrentBrand;
            }

            if (od == null)
            {
                od = new OneDrive(User.OneDriveAccessToken);
            }

            return(await od.PutFile(BackupFilename(activeBrand), LogbookDataForBackup()));
        }
Exemple #5
0
        /// <summary>
        /// Saves a zip of the user's images to OneDrive, if configured.
        /// </summary>
        /// <exception cref="MyFlightbookException"></exception>
        /// <exception cref="Microsoft.OneDrive.Sdk.OneDriveException"></exception>
        /// <param name="activeBrand">The brand to use.  Current brand is used if null.</param>
        /// <param name="od">The OneDrive object to use (one will be initialized, if necessary)</param>
        public async Task <Microsoft.OneDrive.Sdk.Item> BackupImagesToOneDrive(OneDrive od = null, Brand activeBrand = null)
        {
            if (activeBrand == null)
            {
                activeBrand = Branding.CurrentBrand;
            }

            if (User.OneDriveAccessToken == null)
            {
                throw new MyFlightbookException(Resources.Profile.errNotConfiguredOneDrive);
            }

            if (od == null)
            {
                od = new OneDrive(User.OneDriveAccessToken);
            }

            using (MemoryStream ms = ZipOfImagesForUser(activeBrand))
            {
                return(await od.PutFile(ms, BackupImagesFilename(activeBrand)));
            }
        }
Exemple #6
0
        /// <summary>
        /// Creates/returns a memory stream containing a zip of a) an HTML file of images, and b) the thumbnails of the images, linked to Amazon.
        /// THE STREAM MUST BE CLOSED BY THE CALLER!
        /// </summary>
        /// <param name="activeBrand">The brand to use - null for current brand</param>
        /// <returns>A memory stream of flight images followed by any profile images</returns>
        public MemoryStream ZipOfImagesForUser(Brand activeBrand)
        {
            if (activeBrand == null)
            {
                activeBrand = Branding.CurrentBrand;
            }

            MemoryStream ms = new MemoryStream();

            using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Create, true))
            {
                StringWriter sw = new StringWriter();
                using (HtmlTextWriter tw = new HtmlTextWriter(sw))
                {
                    tw.RenderBeginTag(HtmlTextWriterTag.Html);
                    tw.RenderBeginTag(HtmlTextWriterTag.Head);
                    tw.AddAttribute("href", Branding.ReBrand("http://%APP_URL%%APP_ROOT%/public/stylesheet.css", activeBrand));
                    tw.AddAttribute("rel", "stylesheet");
                    tw.AddAttribute("type", "text/css");
                    tw.RenderBeginTag(HtmlTextWriterTag.Link);
                    tw.RenderEndTag();   // Link
                    tw.RenderBeginTag(HtmlTextWriterTag.Title);
                    tw.Write(HttpUtility.HtmlEncode(String.Format(CultureInfo.CurrentCulture, Resources.LocalizedText.ImagesBackupTitle, User.UserFullName)));
                    tw.RenderEndTag();   // Head
                    tw.RenderBeginTag(HtmlTextWriterTag.Body);

                    // Write out profile images
                    tw.RenderBeginTag(HtmlTextWriterTag.H1);
                    tw.Write(HttpUtility.HtmlEncode(String.Format(CultureInfo.CurrentCulture, Resources.LocalizedText.ImagesBackupEndorsementsHeader, User.UserFullName)));
                    tw.RenderEndTag();  // h1

                    ImageList il = new ImageList(MFBImageInfo.ImageClass.Endorsement, User.UserName);
                    il.Refresh(true);
                    foreach (MFBImageInfo mfbii in il.ImageArray)
                    {
                        mfbii.ToHtml(tw, szThumbFolderEndorsements);
                        AddThumbnailToZip(mfbii, zip, szThumbFolderEndorsements);
                        mfbii.UnCache();
                    }

                    // Write out any digital endorsements too
                    IEnumerable <Endorsement> rgEndorsements = Endorsement.EndorsementsForUser(User.UserName, null);
                    if (rgEndorsements.Count() > 0)
                    {
                        using (Page p = new FormlessPage())
                        {
                            p.Controls.Add(new HtmlForm());
                            IEndorsementListUpdate el = (IEndorsementListUpdate)p.LoadControl("~/Controls/mfbEndorsement.ascx");
                            foreach (Endorsement en in rgEndorsements)
                            {
                                el.SetEndorsement(en);
                                try { ((UserControl)el).RenderControl(tw); }
                                catch { }  // don't write bogus or incomplete HTML
                            }
                        }
                    }

                    // And any BasicMed stuff
                    IEnumerable <BasicMedEvent> lstBMed = BasicMedEvent.EventsForUser(User.UserName);
                    foreach (BasicMedEvent bme in lstBMed)
                    {
                        string    szZipFolder = String.Format(CultureInfo.InvariantCulture, "{0}-{1}", bme.ImageKey, szThumbFolderBasicMed);
                        ImageList ilBasicMed  = new ImageList(MFBImageInfo.ImageClass.BasicMed, bme.ImageKey);
                        ilBasicMed.Refresh(true);
                        foreach (MFBImageInfo mfbii in ilBasicMed.ImageArray)
                        {
                            mfbii.ToHtml(tw, szZipFolder);
                            AddThumbnailToZip(mfbii, zip, szZipFolder);
                            mfbii.UnCache();
                        }
                    }

                    // Write out flight images
                    tw.RenderBeginTag(HtmlTextWriterTag.H1);
                    tw.Write(HttpUtility.HtmlEncode(String.Format(CultureInfo.CurrentCulture, Resources.LocalizedText.ImagesBackupFlightsHeader, User.UserFullName)));
                    tw.RenderEndTag();  // H1

                    // We'll get images from the DB rather than slamming the disk
                    // this is a bit of a hack, but limits our queries
                    const string szQ       = @"SELECT f.idflight, img.*
            FROM Images img INNER JOIN flights f ON f.idflight=img.ImageKey
            WHERE f.username=?user AND img.VirtPathID=0
            ORDER BY f.Date desc, f.idFlight desc";
                    DBHelper     dbhImages = new DBHelper(szQ);
                    Dictionary <int, List <MFBImageInfo> > dImages = new Dictionary <int, List <MFBImageInfo> >();
                    dbhImages.ReadRows((comm) => { comm.Parameters.AddWithValue("user", User.UserName); },
                                       (dr) =>
                    {
                        int idFlight = Convert.ToInt32(dr["idflight"], CultureInfo.InvariantCulture);
                        List <MFBImageInfo> lstMFBii;
                        if (dImages.ContainsKey(idFlight))
                        {
                            lstMFBii = dImages[idFlight];
                        }
                        else
                        {
                            dImages[idFlight] = lstMFBii = new List <MFBImageInfo>();
                        }
                        lstMFBii.Add(MFBImageInfo.ImageFromDBRow(dr));
                    });

                    // Get all of the user's flights, including telemetry
                    const int PageSize     = 200; // get 200 flights at a time.
                    int       offset       = 0;
                    int       iRow         = 0;
                    bool      fCouldBeMore = true;

                    while (fCouldBeMore)
                    {
                        FlightQuery fq         = new FlightQuery(User.UserName);
                        DBHelper    dbhFlights = new DBHelper(LogbookEntry.QueryCommand(fq, offset, PageSize, true, LogbookEntry.LoadTelemetryOption.LoadAll));
                        dbhFlights.ReadRows((comm) => { },
                                            (dr) =>
                        {
                            LogbookEntry le = new LogbookEntry(dr, User.UserName, LogbookEntry.LoadTelemetryOption.LoadAll);
                            le.FlightImages = (dImages.ContainsKey(le.FlightID)) ? dImages[le.FlightID].ToArray() : Array.Empty <MFBImageInfo>();

                            // skip any flights here that don't have images, videos, or telemetry
                            if (le.FlightImages.Length > 0 || le.Videos.Count() > 0 || le.HasFlightData)
                            {
                                WriteFlightInfo(tw, zip, le);
                            }
                            iRow++;
                        });
                        if (fCouldBeMore = (iRow == offset + PageSize))
                        {
                            offset += PageSize;
                        }
                    }

                    tw.RenderEndTag();  // Body
                    tw.RenderEndTag();  // Html
                }

                ZipArchiveEntry zipArchiveEntry = zip.CreateEntry(Branding.ReBrand(String.Format(CultureInfo.InvariantCulture, "{0}.htm", Resources.LocalizedText.ImagesBackupFilename), activeBrand));
                using (StreamWriter swHtm = new StreamWriter(zipArchiveEntry.Open()))
                {
                    swHtm.Write(sw.ToString());
                }
            }

            return(ms);
        }