Example #1
0
        string PasteRestricted(string xmlString)
        {
            using (MemoryStream xmlStream1 = new MemoryStream())
                using (MemoryStream xmlStream2 = new MemoryStream())
                {
                    WebUtils.Write(xmlStream1, Report.SaveToString());
                    WebUtils.Write(xmlStream2, xmlString);
                    xmlStream1.Position = 0;
                    xmlStream2.Position = 0;
                    var xml1 = new XmlDocument();
                    var xml2 = new XmlDocument();
                    xml1.Load(xmlStream1);
                    xml2.Load(xmlStream2);

                    if (!DesignScriptCode)
                    {
                        xml2.Root.SetProp("CodeRestricted", "");
                        // paste old script
                        var scriptItem1 = xml1.Root.FindItem(nameof(Report.ScriptText));
                        if (scriptItem1 != null && String.IsNullOrEmpty(scriptItem1.Value))
                        {
                            var scriptItem2 = xml2.Root.FindItem(nameof(Report.ScriptText));
                            if (scriptItem2 != null)
                            {
                                scriptItem2.Value = scriptItem1.Value;
                                scriptItem2.Dispose();
                            }
                            else
                            {
                                xml2.Root.AddItem(scriptItem1);
                            }
                        }
                    }

                    // paste saved connection strings
                    var dictionary1 = xml1.Root.FindItem(nameof(Report.Dictionary));
                    var dictionary2 = xml2.Root.FindItem(nameof(Report.Dictionary));
                    if (dictionary1 != null && dictionary2 != null)
                    {
                        for (int i = 0; i < dictionary1.Items.Count; i++)
                        {
                            var    item1            = dictionary1.Items[i];
                            string connectionString = item1.GetProp("ConnectionString");
                            if (!String.IsNullOrEmpty(connectionString))
                            {
                                var item2 = dictionary2.FindItem(item1.Name);
                                if (item2 != null)
                                {
                                    item2.SetProp("ConnectionString", connectionString);
                                }
                            }
                        }
                    }

                    // save prepared xml
                    using (MemoryStream secondXmlStream = new MemoryStream())
                    {
                        xml2.Save(secondXmlStream);
                        secondXmlStream.Position = 0;
                        int    secondXmlLength = (int)secondXmlStream.Length;
                        bool   rent            = secondXmlLength > 1024;
                        byte[] buff            = rent ?
                                                 ArrayPool <byte> .Shared.Rent(secondXmlLength)
                        : new byte[secondXmlLength];

                        secondXmlStream.Read(buff, 0, secondXmlLength);
                        xmlString = Encoding.UTF8.GetString(buff, 0, secondXmlLength);
                        if (rent)
                        {
                            ArrayPool <byte> .Shared.Return(buff);
                        }
                    }
                    xml1.Dispose();
                    xml2.Dispose();
                }
            return(xmlString);
        }
Example #2
0
        /// <summary>
        /// Save report from designer
        /// </summary>
        internal IActionResult DesignerSaveReport(HttpContext context)
        {
            var result = new ContentResult()
            {
                StatusCode  = (int)HttpStatusCode.OK,
                ContentType = "text/html",
            };

            string reportString = GetPOSTReport(context);

            try
            {
                // paste restricted back in report before save
                string restrictedReport = PasteRestricted(reportString);
                restrictedReport = FixLandscapeProperty(restrictedReport);
                Report.LoadFromString(restrictedReport);

                if (SaveDesignedReport != null)
                {
                    SaveDesignedReportEventArgs e = new SaveDesignedReportEventArgs();
                    e.Stream = new MemoryStream();
                    Report.Save(e.Stream);
                    e.Stream.Position = 0;
                    OnSaveDesignedReport(e);
                }

                if (!DesignerSaveCallBack.IsNullOrWhiteSpace())
                {
                    string report         = Report.SaveToString();
                    string reportFileName = ReportFileName;

                    UriBuilder uri = new UriBuilder
                    {
                        Scheme = context.Request.Scheme,
                        Host   = context.Request.Host.Host
                    };

                    //if (!FastReportGlobal.FastReportOptions.CloudEnvironmet)
                    if (context.Request.Host.Port != null)
                    {
                        uri.Port = (int)context.Request.Host.Port;
                    }
                    else if (uri.Scheme == "https")
                    {
                        uri.Port = 443;
                    }
                    else
                    {
                        uri.Port = 80;
                    }

                    // TODO:
                    //uri.Path = webReport.ResolveUrl(webReport.DesignerSaveCallBack);
                    uri.Path = DesignerSaveCallBack;
                    //var designerSaveCallBack = new Uri(DesignerSaveCallBack);
                    //if (!designerSaveCallBack.IsAbsoluteUri)
                    //{
                    //    designerSaveCallBack = new UriBuilder()
                    //    {
                    //        Scheme = context.Request.Scheme,
                    //        Host = context.Request.Host.Host,
                    //        Port = context.Request.Host.Port ?? 80,
                    //        Path = DesignerSaveCallBack,
                    //    }.Uri;
                    //}
                    //uri.Path = designerSaveCallBack.ToString();

                    // TODO: rename param names
                    string queryToAppend = $"reportID={ID}&reportUUID={reportFileName}";

                    if (uri.Query != null && uri.Query.Length > 1)
                    {
                        uri.Query = uri.Query.Substring(1) + "&" + queryToAppend;
                    }
                    else
                    {
                        uri.Query = queryToAppend;
                    }

                    string callBackURL = uri.ToString();

                    // return "true" to force the certificate to be accepted.
                    ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => true;

                    WebRequest request = WebRequest.Create(callBackURL);

                    if (request != null)
                    {
                        // set up the custom headers
                        if (RequestHeaders != null)
                        {
                            request.Headers = RequestHeaders;
                        }

                        WebUtils.CopyCookies(request, context);

                        // TODO: why here??
                        // if save report in reports folder
                        if (!String.IsNullOrEmpty(DesignerSavePath))
                        {
                            string savePath = WebUtils.MapPath(DesignerSavePath); // TODO: do we really need this?
                            if (Directory.Exists(savePath))
                            {
                                File.WriteAllText(Path.Combine(savePath, reportFileName), report, Encoding.UTF8);
                            }
                            else
                            {
                                result = new ContentResult()
                                {
                                    StatusCode  = (int)HttpStatusCode.NotFound,
                                    ContentType = "text/html",
                                    Content     = "DesignerSavePath does not exist",
                                };
                            }

                            request.Method = "GET";
                        }
                        else
                        // send report directly in POST
                        {
                            request.Method      = "POST";
                            request.ContentType = "text/xml";
                            byte[] postData = Encoding.UTF8.GetBytes(report);
                            request.ContentLength = postData.Length;
                            Stream reqStream = request.GetRequestStream();
                            reqStream.Write(postData, 0, postData.Length);
                            postData = null;
                            reqStream.Close();
                        }

                        // Request call-back
                        try
                        {
                            using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
                            {
                                //context.Response.StatusCode = (int)resp.StatusCode;
                                //context.Response.Write(resp.StatusDescription);

                                result = new ContentResult()
                                {
                                    StatusCode  = (int)resp.StatusCode,
                                    ContentType = "text/html",
                                    Content     = resp.StatusDescription,
                                };
                            }
                        }
                        catch (WebException err)
                        {
                            result = new ContentResult()
                            {
                                StatusCode  = (int)HttpStatusCode.InternalServerError,
                                ContentType = "text/html",
                            };

                            if (Debug)
                            {
                                using (Stream data = err.Response.GetResponseStream())
                                    using (StreamReader reader = new StreamReader(data))
                                    {
                                        string text = reader.ReadToEnd();
                                        if (!String.IsNullOrEmpty(text))
                                        {
                                            int startExceptionText = text.IndexOf("<!--");
                                            int endExceptionText   = text.LastIndexOf("-->");
                                            if (startExceptionText != -1)
                                            {
                                                text = text.Substring(startExceptionText + 6, endExceptionText - startExceptionText - 6);
                                            }

                                            result.Content    = text;
                                            result.StatusCode = (int)(err.Response as HttpWebResponse).StatusCode;
                                        }
                                    }
                            }
                            else
                            {
                                result.Content = err.Message;
                            }
                        }
                    }
                    request = null;
                }
            }
            catch (Exception e)
            {
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                if (Debug)
                {
                    result.Content = e.Message;
                }
            }

            return(result);
        }
Example #3
0
        // send report to the designer
        internal IActionResult DesignerGetReport()
        {
            string reportString = Report.SaveToString();
            string report       = CutRestricted(reportString);

            if (report.IndexOf("inherited") != -1)
            {
                List <string> reportInheritance = new List <string>();
                string        baseReport        = report;

                while (!String.IsNullOrEmpty(baseReport))
                {
                    reportInheritance.Add(baseReport);
                    using (MemoryStream xmlStream = new MemoryStream())
                    {
                        WebUtils.Write(xmlStream, baseReport);
                        xmlStream.Position = 0;
                        using (var xml = new XmlDocument())
                        {
                            xml.Load(xmlStream);
                            string baseReportFile = xml.Root.GetProp("BaseReport");
                            //string fileName = context.Request.MapPath(baseReportFile, webReport.Prop.ReportPath, true);
                            if (!Path.IsPathRooted(baseReportFile))
                            {
                                baseReportFile = Path.GetFullPath(Path.GetDirectoryName(Report.FileName) + Path.DirectorySeparatorChar + baseReportFile); //was ReportPath before(ToDo)
                            }
                            if (File.Exists(baseReportFile))
                            {
                                baseReport = File.ReadAllText(baseReportFile, Encoding.UTF8);
                            }
                            else
                            {
                                baseReport = String.Empty;
                            }
                        }
                    }
                }
                StringBuilder responseBuilder = new StringBuilder();
                responseBuilder.Append("{\"reports\":[");
                for (int i = reportInheritance.Count - 1; i >= 0; i--)
                {
                    string s = reportInheritance[i];
                    responseBuilder.Append('\"');
                    responseBuilder.Append(s.Replace("\r\n", "").Replace("\"", "\\\""));
                    if (i > 0)
                    {
                        responseBuilder.Append("\",");
                    }
                    else
                    {
                        responseBuilder.Append('\"');
                    }
                }
                responseBuilder.Append("]}");

                return(new ContentResult()
                {
                    StatusCode = (int)HttpStatusCode.OK,
                    ContentType = "text/html",
                    Content = responseBuilder.ToString(),
                });
            }

            return(new ContentResult()
            {
                StatusCode = (int)HttpStatusCode.OK,
                ContentType = "text/html",
                Content = report,
            });
        }
Example #4
0
        internal StringBuilder ReportInHtml()
        {
            PictureCache.Clear();

            var sb = new StringBuilder();

            using (HTMLExport html = new HTMLExport())
            {
                html.ExportMode = HTMLExport.ExportType.WebPreview;
                //html.CustomDraw += this.CustomDraw;
                html.StylePrefix = $"fr{ID}";         //html.StylePrefix = Prop.ControlID.Substring(0, 6);
                html.Init_WebMode();
                html.Pictures        = Pictures;      //html.Pictures = Prop.Pictures;
                html.EmbedPictures   = EmbedPictures; //html.EmbedPictures = EmbedPictures;
                html.OnClickTemplate = "fr{0}.click(this,'{1}','{2}')";
                html.ReportID        = ID;            //html.ReportID = Prop.ControlID;
                html.EnableMargins   = EnableMargins; //html.EnableMargins = Prop.EnableMargins;

                // calc zoom
                //CalcHtmlZoom(html);
                html.Zoom = Zoom;

                html.Layers      = Layers;                                              //html.Layers = Layers;
                html.PageNumbers = SinglePage ? "" : (CurrentPageIndex + 1).ToString(); //html.PageNumbers = SinglePage ? "" : (Prop.CurrentPage + 1).ToString();

                //if (Prop.AutoWidth)
                //    html.WidthUnits = HtmlSizeUnits.Percent;
                //if (Prop.AutoHeight)
                //    html.HeightUnits = HtmlSizeUnits.Percent;

                //html.WebImagePrefix = WebUtils.ToUrl(FastReportGlobal.FastReportOptions.RouteBasePath, controller.RouteBasePath, ID, "picture") + "/"; //html.WebImagePrefix = String.Concat(context.Response.ApplyAppPathModifier(WebUtils.HandlerFileName), "?", WebUtils.PicsPrefix);
                html.WebImagePrefix = WebUtils.ToUrl(FastReportGlobal.FastReportOptions.RoutePathBaseRoot, FastReportGlobal.FastReportOptions.RouteBasePath, $"preview.getPicture?reportId={ID}&pictureId=");
                html.SinglePage     = SinglePage;       //html.SinglePage = SinglePage;
                html.CurPage        = CurrentPageIndex; //html.CurPage = CurrentPage;
                html.Export(Report, (Stream)null);

                //sb.Append("<div class=\"frbody\" style =\"");
                //if (HtmlLayers)
                //    sb.Append("position:relative;z-index:0;");
                //sb.Append("\">");

                // container for html report body
                //int pageWidth = (int)Math.Ceiling(GetReportPageWidthInPixels() * html.Zoom);
                //int pageHeight = (int)Math.Ceiling(GetReportPageHeightInPixels() * html.Zoom);
                //int paddingLeft = (int)Math.Ceiling(Padding.Left * html.Zoom);
                //int paddingRight = (int)Math.Ceiling(Padding.Right * html.Zoom);
                //int paddingTop = (int)Math.Ceiling(Padding.Top * html.Zoom);
                //int paddingBottom = (int)Math.Ceiling(Padding.Bottom * html.Zoom);
                //sb.Append("<div class=\"frcontainer\" style=\"width:" + pageWidth +
                //    "px;height:" + (SinglePage ? pageHeight * html.Count : pageHeight) +
                //    "px;padding-left:" + paddingLeft +
                //    "px;padding-right:" + paddingRight +
                //    "px;padding-top:" + paddingTop +
                //    "px;padding-bottom:" + paddingBottom + "px\">");

                if (html.Count > 0)
                {
                    if (SinglePage)
                    {
                        DoAllHtmlPages(sb, html);
                        CurrentPageIndex = 0; //Prop.CurrentPage = 0;
                    }
                    else
                    {
                        DoHtmlPage(sb, html, 0);
                    }
                }

                //sb.Append("</div>");
                //sb.Append("</div>");

                // important container, it cuts off elements that are outside of the report page bounds
                int pageWidth  = (int)Math.Ceiling(GetReportPageWidthInPixels() * html.Zoom);
                int pageHeight = (int)Math.Ceiling(GetReportPageHeightInPixels() * html.Zoom);
                sb.Insert(0, $@"<div style=""width:{pageWidth}px;height:{pageHeight}px;overflow:hidden;display:inline-block;"">");
                sb.Append("</div>");
            }

            return(sb);
        }