WebReportDesigner.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. using FastReport.Data;
  2. using FastReport.Utils;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.IO;
  7. using System.Net;
  8. using System.Text;
  9. using System.Web;
  10. using System.Web.UI;
  11. namespace FastReport.Web.Handlers
  12. {
  13. /// <summary>
  14. /// Web handler class
  15. /// </summary>
  16. public partial class WebExport : IHttpHandler
  17. {
  18. private string CutRestricted(WebReport webReport, string xmlString)
  19. {
  20. using (MemoryStream xmlStream = new MemoryStream())
  21. {
  22. WebUtils.Write(xmlStream, xmlString);
  23. xmlStream.Position = 0;
  24. using (FastReport.Utils.XmlDocument xml = new FastReport.Utils.XmlDocument())
  25. {
  26. xml.Load(xmlStream);
  27. if (!webReport.DesignScriptCode)
  28. {
  29. xml.Root.SetProp("CodeRestricted", "true");
  30. // cut script
  31. FastReport.Utils.XmlItem scriptItem = xml.Root.FindItem("ScriptText");
  32. if (scriptItem != null && !String.IsNullOrEmpty(scriptItem.Value))
  33. scriptItem.Value = String.Empty;
  34. }
  35. // cut connection strings
  36. FastReport.Utils.XmlItem dictionary = xml.Root.FindItem("Dictionary");
  37. if (dictionary != null)
  38. {
  39. for (int i = 0; i < dictionary.Items.Count; i++)
  40. {
  41. FastReport.Utils.XmlItem item = dictionary.Items[i];
  42. if (!String.IsNullOrEmpty(item.GetProp("ConnectionString")))
  43. item.SetProp("ConnectionString", String.Empty);
  44. }
  45. }
  46. // save prepared xml
  47. using (MemoryStream secondXmlStream = new MemoryStream())
  48. {
  49. xml.Save(secondXmlStream);
  50. secondXmlStream.Position = 0;
  51. byte[] buff = new byte[secondXmlStream.Length];
  52. secondXmlStream.Read(buff, 0, buff.Length);
  53. xmlString = Encoding.UTF8.GetString(buff);
  54. }
  55. }
  56. }
  57. return xmlString;
  58. }
  59. private string PasteRestricted(WebReport webReport, string xmlString)
  60. {
  61. using (MemoryStream xmlStream1 = new MemoryStream())
  62. using (MemoryStream xmlStream2 = new MemoryStream())
  63. {
  64. WebUtils.Write(xmlStream1, webReport.Report.SaveToString());
  65. WebUtils.Write(xmlStream2, xmlString);
  66. xmlStream1.Position = 0;
  67. xmlStream2.Position = 0;
  68. FastReport.Utils.XmlDocument xml1 = new FastReport.Utils.XmlDocument();
  69. FastReport.Utils.XmlDocument xml2 = new FastReport.Utils.XmlDocument();
  70. xml1.Load(xmlStream1);
  71. xml2.Load(xmlStream2);
  72. if (!webReport.DesignScriptCode)
  73. {
  74. xml2.Root.SetProp("CodeRestricted", "");
  75. // clean received script
  76. FastReport.Utils.XmlItem scriptItem2 = xml2.Root.FindItem("ScriptText");
  77. if (scriptItem2 != null)
  78. scriptItem2.Value = "";
  79. // paste old script
  80. FastReport.Utils.XmlItem scriptItem1 = xml1.Root.FindItem("ScriptText");
  81. if (scriptItem1 != null)
  82. {
  83. if (String.IsNullOrEmpty(scriptItem1.Value))
  84. {
  85. scriptItem2.Dispose();
  86. scriptItem2 = null;
  87. }
  88. else
  89. if (scriptItem2 != null)
  90. scriptItem2.Value = scriptItem1.Value;
  91. else
  92. xml2.Root.AddItem(scriptItem1);
  93. }
  94. }
  95. // paste saved connection strings
  96. FastReport.Utils.XmlItem dictionary1 = xml1.Root.FindItem("Dictionary");
  97. FastReport.Utils.XmlItem dictionary2 = xml2.Root.FindItem("Dictionary");
  98. if (dictionary1 != null && dictionary2 != null)
  99. {
  100. for (int i = 0; i < dictionary1.Items.Count; i++)
  101. {
  102. FastReport.Utils.XmlItem item1 = dictionary1.Items[i];
  103. string connectionString = item1.GetProp("ConnectionString");
  104. if (!String.IsNullOrEmpty(connectionString))
  105. {
  106. FastReport.Utils.XmlItem item2 = dictionary2.FindItem(item1.Name);
  107. if (item2 != null)
  108. item2.SetProp("ConnectionString", connectionString);
  109. }
  110. }
  111. }
  112. // save prepared xml
  113. using (MemoryStream secondXmlStream = new MemoryStream())
  114. {
  115. xml2.Save(secondXmlStream);
  116. secondXmlStream.Position = 0;
  117. byte[] buff = new byte[secondXmlStream.Length];
  118. secondXmlStream.Read(buff, 0, buff.Length);
  119. xmlString = Encoding.UTF8.GetString(buff);
  120. }
  121. }
  122. return xmlString;
  123. }
  124. // save report
  125. private void SetReportTemplate(HttpContext context)
  126. {
  127. ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
  128. string guid = context.Request.Params["putReport"];
  129. SetUpWebReport(guid, context);
  130. if (WebUtils.SetupResponse(webReport, context))
  131. {
  132. if ((webReport != null))
  133. {
  134. string reportString = GetPOSTReport(context);
  135. string restrictedReport = PasteRestricted(webReport, reportString);
  136. restrictedReport = FixLandscapeProperty(restrictedReport);
  137. try
  138. {
  139. // paste restricted back in report before save
  140. webReport.Report.LoadFromString(restrictedReport);
  141. SaveDesignedReportEventArgs e = new SaveDesignedReportEventArgs();
  142. e.Stream = new MemoryStream();
  143. webReport.Report.Save(e.Stream);
  144. e.Stream.Position = 0;
  145. webReport.OnSaveDesignedReport(e);
  146. if (!String.IsNullOrEmpty(webReport.DesignerSaveCallBack))
  147. {
  148. string report = webReport.Report.SaveToString();
  149. string reportFileName = String.Concat(webReport.ReportGuid, ".frx");
  150. UriBuilder uri = new UriBuilder();
  151. uri.Scheme = context.Request.Url.Scheme;
  152. uri.Host = context.Request.Url.Host;
  153. if (!webReport.CloudEnvironmet)
  154. uri.Port = context.Request.Url.Port;
  155. uri.Path = webReport.ResolveUrl(webReport.DesignerSaveCallBack);
  156. string queryToAppend = String.Format(
  157. "reportID={0}&reportUUID={1}", webReport.ID != null ? webReport.ID : "", reportFileName);
  158. if (uri.Query != null && uri.Query.Length > 1)
  159. uri.Query = uri.Query.Substring(1) + "&" + queryToAppend;
  160. else
  161. uri.Query = queryToAppend;
  162. string callBackURL = uri.ToString();
  163. ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
  164. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(callBackURL);
  165. if (request != null)
  166. {
  167. request.KeepAlive = false;
  168. request.Proxy = null;
  169. // set up the custom headers
  170. if (webReport.RequestHeaders != null)
  171. request.Headers = webReport.RequestHeaders;
  172. WebUtils.CopyCookies(request, context);
  173. // if save report in reports folder
  174. if (!String.IsNullOrEmpty(webReport.DesignerSavePath))
  175. {
  176. string savePath = context.Request.MapPath(webReport.DesignerSavePath);
  177. if (Directory.Exists(savePath))
  178. {
  179. File.WriteAllText(Path.Combine(savePath, reportFileName), report, Encoding.UTF8);
  180. }
  181. else
  182. {
  183. context.Response.Write("DesignerSavePath does not exists");
  184. context.Response.StatusCode = 404;
  185. }
  186. request.Method = "GET";
  187. }
  188. else
  189. // send report directly in POST
  190. {
  191. request.Method = "POST";
  192. request.ContentType = "text/xml";
  193. byte[] postData = Encoding.UTF8.GetBytes(report);
  194. request.ContentLength = postData.Length;
  195. Stream reqStream = request.GetRequestStream();
  196. reqStream.Write(postData, 0, postData.Length);
  197. postData = null;
  198. reqStream.Close();
  199. }
  200. // Request call-back
  201. try
  202. {
  203. request.BeginGetResponse(new AsyncCallback(FinishWebRequest), request);
  204. context.Response.StatusCode = 202;
  205. }
  206. catch (WebException err)
  207. {
  208. context.Response.StatusCode = 500;
  209. if (webReport.Debug)
  210. using (Stream data = err.Response.GetResponseStream())
  211. using (StreamReader reader = new StreamReader(data))
  212. {
  213. string text = reader.ReadToEnd();
  214. if (!String.IsNullOrEmpty(text))
  215. {
  216. int startExceptionText = text.IndexOf("<!--");
  217. int endExceptionText = text.LastIndexOf("-->");
  218. if (startExceptionText != -1)
  219. text = text.Substring(startExceptionText + 6, endExceptionText - startExceptionText - 6);
  220. context.Response.Write(text);
  221. context.Response.StatusCode = (int)(err.Response as HttpWebResponse).StatusCode;
  222. }
  223. }
  224. else
  225. context.Response.Write(err.Message);
  226. }
  227. }
  228. request = null;
  229. }
  230. }
  231. catch (Exception e)
  232. {
  233. if (webReport.Debug)
  234. context.Response.Write(e.Message);
  235. context.Response.StatusCode = 500;
  236. }
  237. }
  238. else
  239. context.Response.StatusCode = 404;
  240. }
  241. Finalize(context);
  242. }
  243. private void FinishWebRequest(IAsyncResult result)
  244. {
  245. HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
  246. }
  247. // send report to the designer
  248. private void SendReportTemplate(HttpContext context)
  249. {
  250. string guid = context.Request.Params["getReport"];
  251. SetUpWebReport(guid, context);
  252. if (WebUtils.SetupResponse(webReport, context))
  253. {
  254. if (webReport != null)
  255. {
  256. string reportString = webReport.Report.SaveToString();
  257. string report = CutRestricted(webReport, reportString);
  258. if (report.IndexOf("inherited") != -1)
  259. {
  260. List<string> reportInheritance = new List<string>();
  261. string baseReport = report;
  262. while (!String.IsNullOrEmpty(baseReport))
  263. {
  264. reportInheritance.Add(baseReport);
  265. using (MemoryStream xmlStream = new MemoryStream())
  266. {
  267. WebUtils.Write(xmlStream, baseReport);
  268. xmlStream.Position = 0;
  269. using (FastReport.Utils.XmlDocument xml = new Utils.XmlDocument())
  270. {
  271. xml.Load(xmlStream);
  272. string baseReportFile = xml.Root.GetProp("BaseReport");
  273. //context.Request.MapPath(baseReportFile, webReport.Report.FileName, true);
  274. string fileName = Path.GetFullPath(Path.GetDirectoryName(Report.FileName) + Path.DirectorySeparatorChar + baseReportFile);
  275. if (File.Exists(fileName))
  276. {
  277. baseReport = File.ReadAllText(fileName, Encoding.UTF8);
  278. }
  279. else
  280. baseReport = String.Empty;
  281. }
  282. }
  283. }
  284. StringBuilder responseBuilder = new StringBuilder();
  285. responseBuilder.Append("{\"reports\":[");
  286. for (int i = reportInheritance.Count - 1; i >= 0; i--)
  287. {
  288. string s = reportInheritance[i];
  289. responseBuilder.Append("\"");
  290. responseBuilder.Append(s.Replace("\r\n", "").Replace("\"", "\\\""));
  291. if (i > 0)
  292. responseBuilder.Append("\",");
  293. else
  294. responseBuilder.Append("\"");
  295. }
  296. responseBuilder.Append("]}");
  297. context.Response.Write(responseBuilder.ToString());
  298. }
  299. else
  300. context.Response.Write(report);
  301. }
  302. else
  303. context.Response.StatusCode = 404;
  304. }
  305. Finalize(context);
  306. }
  307. // preview for Designer
  308. private void MakeReportPreview(HttpContext context)
  309. {
  310. string guid = context.Request.Params["makePreview"];
  311. SetUpWebReport(guid, context);
  312. if (WebUtils.SetupResponse(webReport, context))
  313. {
  314. if (webReport != null)
  315. {
  316. string receivedReportString = GetPOSTReport(context);
  317. try
  318. {
  319. WebReport previewReport = new WebReport();
  320. previewReport.ID = webReport.ID + "-preview";
  321. previewReport.Report = webReport.Report;
  322. previewReport.Prop.Assign(webReport.Prop);
  323. //previewReport.LocalizationFile = webReport.LocalizationFile;
  324. previewReport.Width = System.Web.UI.WebControls.Unit.Pixel(880);
  325. previewReport.Height = System.Web.UI.WebControls.Unit.Pixel(770);
  326. previewReport.Toolbar.EnableFit = true;
  327. previewReport.Layers = true;
  328. string reportString = PasteRestricted(webReport, receivedReportString);
  329. reportString = FixLandscapeProperty(reportString);
  330. previewReport.Report.ReportResourceString = reportString;
  331. previewReport.ReportFile = String.Empty;
  332. previewReport.ReportResourceString = reportString;
  333. previewReport.Prepare();
  334. StringBuilder sb = new StringBuilder();
  335. sb.Append("<script src=\"").Append(GetResourceTemplateUrl(context, "fr_util.js")).AppendLine("\" type=\"text/javascript\"></script>");
  336. HtmlTextWriter writer = new HtmlTextWriter(new StringWriter(sb, System.Globalization.CultureInfo.InvariantCulture));
  337. previewReport.ShowZoomButton = false;
  338. previewReport.PreviewMode = true;
  339. previewReport.DesignReport = false;
  340. previewReport.Page = null;
  341. previewReport.Style.Add("overflow", "auto");
  342. previewReport.Style.Add("max-width", "100%");
  343. previewReport.RenderControl(writer);
  344. string responseText = WebReportGlobals.ScriptsAsString() + previewReport.Toolbar.GetCss() + sb.ToString();
  345. context.Response.Write(responseText);
  346. }
  347. catch (Exception e)
  348. {
  349. if (webReport.Debug)
  350. context.Response.Write(e.Message);
  351. context.Response.StatusCode = 500;
  352. }
  353. }
  354. else
  355. {
  356. context.Response.StatusCode = 404;
  357. }
  358. }
  359. Finalize(context);
  360. }
  361. // In an Online-Designer, the page property 'Landscape' may come last in the list, however, it must come first
  362. private static string FixLandscapeProperty(string reportString)
  363. {
  364. int indexOfLandscape = reportString.IndexOf(nameof(ReportPage.Landscape));
  365. if (indexOfLandscape != -1)
  366. {
  367. // Landscape="~"
  368. int lastIndexOfLandscapeValue =
  369. reportString.IndexOf('"', indexOfLandscape + nameof(ReportPage.Landscape).Length + 2, 10);
  370. var indexOfPage = reportString.IndexOf(nameof(ReportPage), 0, indexOfLandscape);
  371. int startposition = indexOfPage + nameof(ReportPage).Length + 1;
  372. if (indexOfLandscape == startposition)
  373. return reportString;
  374. StringBuilder sb = new StringBuilder(reportString);
  375. var property = reportString.Substring(indexOfLandscape, lastIndexOfLandscapeValue - indexOfLandscape + 2);
  376. sb.Remove(indexOfLandscape, property.Length);
  377. sb.Insert(startposition, property);
  378. reportString = sb.ToString();
  379. }
  380. return reportString;
  381. }
  382. private string GetPOSTReport(HttpContext context)
  383. {
  384. string requestString = "";
  385. using (TextReader textReader = new StreamReader(context.Request.InputStream))
  386. requestString = textReader.ReadToEnd();
  387. const string xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  388. StringBuilder result = new StringBuilder(xmlHeader.Length + requestString.Length + 100);
  389. result.Append(xmlHeader);
  390. result.Append(requestString.
  391. Replace("&gt;", ">").
  392. Replace("&lt;", "<").
  393. Replace("&quot;", "\"").
  394. Replace("&amp;#10;", "&#10;").
  395. Replace("&amp;#13;", "&#13;").
  396. Replace("&amp;#xA;", "&#10;").
  397. Replace("&amp;#xD;", "&#13;").
  398. Replace("&amp;quot;", "&quot;").
  399. Replace("&amp;amp;", "&").
  400. Replace("&amp;lt;", "&lt;").
  401. Replace("&amp;gt;", "&gt;"));
  402. return result.ToString();
  403. }
  404. private void SendPreviewObjectResponse(HttpContext context)
  405. {
  406. string uuid = context.Request.Params["previewobject"];
  407. SetUpWebReport(uuid, context);
  408. if (WebUtils.SetupResponse(webReport, context))
  409. {
  410. if (!NeedExport(context) && !NeedPrint(context))
  411. SendReport(context);
  412. cache.PutObject(uuid, webReport);
  413. }
  414. Finalize(context);
  415. }
  416. // On-line Designer
  417. private void SendDesigner(HttpContext context, string uuid)
  418. {
  419. if (WebUtils.SetupResponse(webReport, context))
  420. {
  421. StringBuilder sb = new StringBuilder();
  422. context.Response.AddHeader("Content-Type", "html/text");
  423. try
  424. {
  425. string designerPath = WebUtils.GetAppRoot(context, webReport.DesignerPath);
  426. string designerLocale = String.IsNullOrEmpty(webReport.DesignerLocale) ? "" : "&lang=" + webReport.DesignerLocale;
  427. sb.Append(String.Format("<iframe src=\"{0}?uuid={1}{2}{3}\" style=\"border:none;\" width=\"{4}\" height=\"{5}\" allowfullscreen=\"true\" webkitallowfullscreen=\"true\" mozallowfullscreen=\"true\" oallowfullscreen=\"true\" msallowfullscreen=\"true\">",
  428. designerPath, //0
  429. uuid, //1
  430. WebUtils.GetARRAffinity(), //2
  431. designerLocale, //3
  432. webReport.Width.ToString(), //4
  433. webReport.Height.ToString() //5
  434. ));
  435. sb.Append("<p style=\"color:red\">ERROR: Browser does not support IFRAME!</p>");
  436. sb.AppendLine("</iframe>");
  437. // add resize here
  438. if (webReport.Height == System.Web.UI.WebControls.Unit.Percentage(100))
  439. sb.Append(GetFitScript(uuid));
  440. }
  441. catch (Exception e)
  442. {
  443. log.AddError(e);
  444. }
  445. if (log.Text.Length > 0)
  446. {
  447. context.Response.Write(log.Text);
  448. log.Clear();
  449. }
  450. SetContainer(context, Properties.ControlID);
  451. context.Response.Write(sb.ToString());
  452. }
  453. }
  454. private string GetFitScript(string ID)
  455. {
  456. StringBuilder sb = new StringBuilder();
  457. sb.AppendLine("<script>");
  458. sb.AppendLine("(function() {");
  459. sb.AppendLine(String.Format("var div = document.querySelector('#{0}'),", ID));
  460. sb.AppendLine("iframe,");
  461. sb.AppendLine("rect,");
  462. sb.AppendLine("e = document.documentElement,");
  463. sb.AppendLine("g = document.getElementsByTagName('body')[0],");
  464. //sb.AppendLine("x = window.innerWidth || e.clientWidth || g.clientWidth,");
  465. sb.AppendLine("y = window.innerHeight|| e.clientHeight|| g.clientHeight;");
  466. sb.AppendLine("if (div) {");
  467. sb.AppendLine("iframe = div.querySelector('iframe');");
  468. sb.AppendLine("if (iframe) {");
  469. sb.AppendLine("rect = iframe.getBoundingClientRect();");
  470. //sb.AppendLine("iframe.setAttribute('width', x - rect.left);");
  471. sb.AppendLine("iframe.setAttribute('height', y - rect.top - 11);");
  472. sb.AppendLine("}}}());");
  473. sb.AppendLine("</script>");
  474. return sb.ToString();
  475. }
  476. private void MakeDesignerConfig(HttpContext context)
  477. {
  478. context.Response.AddHeader("Content-Type", "application/json");
  479. string uuid = context.Request.Params["getDesignerConfig"];
  480. SetUpWebReport(uuid, context);
  481. if (WebUtils.SetupResponse(webReport, context))
  482. {
  483. if (webReport != null)
  484. {
  485. context.Response.Write(webReport.DesignerConfig);
  486. }
  487. }
  488. Finalize(context);
  489. }
  490. private void MakeConnectionTypes(HttpContext context)
  491. {
  492. context.Response.AddHeader("Content-Type", "application/json");
  493. string uuid = context.Request.Params["getConnectionTypes"];
  494. SetUpWebReport(uuid, context);
  495. if (WebUtils.SetupResponse(webReport, context))
  496. {
  497. List<string> names = new List<string>();
  498. var dataConnections = new List<DataConnectionInfo>();
  499. RegisteredObjects.DataConnections.EnumItems(dataConnections);
  500. foreach (var info in dataConnections)
  501. {
  502. if (info.Object != null && info.Text != null)
  503. names.Add("\"" + info.Object.FullName + "\":\"" + Res.TryGetBuiltin(info.Text) + "\"");
  504. }
  505. context.Response.Write("{" + String.Join(",", names.ToArray()) + "}");
  506. }
  507. Finalize(context);
  508. }
  509. private void MakeConnectionTables(HttpContext context)
  510. {
  511. string uuid = context.Request.Params["getConnectionTables"];
  512. SetUpWebReport(uuid, context);
  513. if (WebUtils.SetupResponse(webReport, context))
  514. {
  515. string connectionType = context.Request.Params["connectionType"];
  516. string connectionString = context.Request.Params["connectionString"];
  517. var dataConnections = new List<DataConnectionInfo>();
  518. RegisteredObjects.DataConnections.EnumItems(dataConnections);
  519. Type connType = null;
  520. foreach (var dataConnection in dataConnections)
  521. if (dataConnection.Object != null &&
  522. dataConnection.Object.FullName == connectionType)
  523. {
  524. connType = dataConnection.Object;
  525. break;
  526. }
  527. if (connType != null)
  528. {
  529. try
  530. {
  531. using (DataConnectionBase conn = (DataConnectionBase)Activator.CreateInstance(connType))
  532. using (FRWriter writer = new FRWriter())
  533. {
  534. conn.ConnectionString = connectionString;
  535. conn.CreateAllTables(true);
  536. foreach (TableDataSource c in conn.Tables)
  537. c.Enabled = true;
  538. writer.SaveChildren = true;
  539. writer.WriteHeader = false;
  540. writer.Write(conn);
  541. writer.Save(context.Response.OutputStream);
  542. context.Response.ContentType = "application/xml";
  543. }
  544. }
  545. catch (Exception ex)
  546. {
  547. context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
  548. context.Response.TrySkipIisCustomErrors = true;
  549. context.Response.ContentType = "text/plain";
  550. context.Response.Write(ex.ToString());
  551. }
  552. }
  553. else
  554. {
  555. context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
  556. context.Response.TrySkipIisCustomErrors = true;
  557. context.Response.ContentType = "text/plain";
  558. context.Response.Write("Connection type not found");
  559. }
  560. }
  561. Finalize(context);
  562. }
  563. private void GetConnectionStringProperties(HttpContext context)
  564. {
  565. string uuid = context.Request.Params["getConnectionStringProperties"];
  566. SetUpWebReport(uuid, context);
  567. if (WebUtils.SetupResponse(webReport, context))
  568. {
  569. string connectionType = context.Request.Params["connectionType"];
  570. string connectionString = context.Request.Params["connectionString"];
  571. var dataConnections = new List<DataConnectionInfo>();
  572. RegisteredObjects.DataConnections.EnumItems(dataConnections);
  573. Type connType = null;
  574. foreach (var dataConnection in dataConnections)
  575. if (dataConnection.Object != null &&
  576. dataConnection.Object.FullName == connectionType)
  577. {
  578. connType = dataConnection.Object;
  579. break;
  580. }
  581. if (connType == null)
  582. {
  583. context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
  584. context.Response.TrySkipIisCustomErrors = true;
  585. context.Response.ContentType = "text/plain";
  586. context.Response.Write("Connection type not found");
  587. Finalize(context);
  588. return;
  589. }
  590. StringBuilder data = new StringBuilder();
  591. // this piece of code mimics functionality of PropertyGrid: finds available properties
  592. try
  593. {
  594. using (DataConnectionBase conn = (DataConnectionBase)Activator.CreateInstance(connType))
  595. {
  596. conn.ConnectionString = connectionString;
  597. PropertyDescriptorCollection props = TypeDescriptor.GetProperties(conn);
  598. foreach (PropertyDescriptor pd in props)
  599. {
  600. if (!pd.IsBrowsable || pd.IsReadOnly)
  601. continue;
  602. if (pd.Name == "Name" ||
  603. pd.Name == "ConnectionString" ||
  604. pd.Name == "ConnectionStringExpression" ||
  605. pd.Name == "LoginPrompt" ||
  606. pd.Name == "CommandTimeout" ||
  607. pd.Name == "Alias" ||
  608. pd.Name == "Description" ||
  609. pd.Name == "Restrictions")
  610. continue;
  611. object value = null;
  612. try
  613. {
  614. object owner = conn;
  615. if (conn is ICustomTypeDescriptor)
  616. owner = ((ICustomTypeDescriptor)conn).GetPropertyOwner(pd);
  617. value = pd.GetValue(owner);
  618. }
  619. catch { }
  620. data.Append("{");
  621. data.Append("\"name\":\"" + WebUtils.JavaScriptStringEncode(pd.Name) + "\",");
  622. data.Append("\"displayName\":\"" + WebUtils.JavaScriptStringEncode(pd.DisplayName) + "\",");
  623. data.Append("\"description\":\"" + WebUtils.JavaScriptStringEncode(pd.Description) + "\",");
  624. data.Append("\"value\":\"" + WebUtils.JavaScriptStringEncode(value == null ? "" : value.ToString()) + "\",");
  625. data.Append("\"propertyType\":\"" + WebUtils.JavaScriptStringEncode(pd.PropertyType.FullName) + "\"");
  626. data.Append("},");
  627. }
  628. }
  629. }
  630. catch (Exception ex)
  631. {
  632. context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
  633. context.Response.TrySkipIisCustomErrors = true;
  634. context.Response.ContentType = "text/plain";
  635. context.Response.Write(ex.ToString());
  636. Finalize(context);
  637. return;
  638. }
  639. context.Response.ContentType = "application/json";
  640. context.Response.Write("{\"properties\":[" + data.ToString().TrimEnd(',') + "]}");
  641. }
  642. Finalize(context);
  643. }
  644. private void MakeConnectionString(HttpContext context)
  645. {
  646. string uuid = context.Request.Params["makeConnectionString"];
  647. SetUpWebReport(uuid, context);
  648. if (WebUtils.SetupResponse(webReport, context))
  649. {
  650. string connectionType = context.Request.Params["connectionType"];
  651. var dataConnections = new List<DataConnectionInfo>();
  652. RegisteredObjects.DataConnections.EnumItems(dataConnections);
  653. Type connType = null;
  654. foreach (var dataConnection in dataConnections)
  655. if (dataConnection.Object != null &&
  656. dataConnection.Object.FullName == connectionType)
  657. {
  658. connType = dataConnection.Object;
  659. break;
  660. }
  661. if (connType == null)
  662. {
  663. context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
  664. context.Response.TrySkipIisCustomErrors = true;
  665. context.Response.ContentType = "text/plain";
  666. context.Response.Write("Connection type not found");
  667. Finalize(context);
  668. return;
  669. }
  670. try
  671. {
  672. using (DataConnectionBase conn = (DataConnectionBase)Activator.CreateInstance(connType))
  673. {
  674. PropertyDescriptorCollection props = TypeDescriptor.GetProperties(conn);
  675. foreach (PropertyDescriptor pd in props)
  676. {
  677. if (!pd.IsBrowsable || pd.IsReadOnly)
  678. continue;
  679. if (pd.Name == "Name" ||
  680. pd.Name == "ConnectionString" ||
  681. pd.Name == "ConnectionStringExpression" ||
  682. pd.Name == "LoginPrompt" ||
  683. pd.Name == "CommandTimeout" ||
  684. pd.Name == "Alias" ||
  685. pd.Name == "Description" ||
  686. pd.Name == "Restrictions")
  687. continue;
  688. try
  689. {
  690. string propertyValue = context.Request.Form[pd.Name];
  691. TypeConverter typeConverter = TypeDescriptor.GetConverter(pd.PropertyType);
  692. object value = typeConverter.ConvertFromString(propertyValue);
  693. object owner = conn;
  694. if (conn is ICustomTypeDescriptor)
  695. owner = ((ICustomTypeDescriptor)conn).GetPropertyOwner(pd);
  696. pd.SetValue(owner, value);
  697. }
  698. catch (Exception ex)
  699. {
  700. context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
  701. context.Response.TrySkipIisCustomErrors = true;
  702. context.Response.ContentType = "text/plain";
  703. context.Response.Write(ex.ToString());
  704. Finalize(context);
  705. return;
  706. }
  707. }
  708. context.Response.ContentType = "application/json";
  709. context.Response.Write("{\"connectionString\":\"" + WebUtils.JavaScriptStringEncode(conn.ConnectionString) + "\"}");
  710. Finalize(context);
  711. return;
  712. }
  713. }
  714. catch (Exception ex)
  715. {
  716. context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
  717. context.Response.TrySkipIisCustomErrors = true;
  718. context.Response.ContentType = "text/plain";
  719. context.Response.Write(ex.ToString());
  720. Finalize(context);
  721. return;
  722. }
  723. }
  724. Finalize(context);
  725. }
  726. private void MakeFunctionsList(HttpContext context)
  727. {
  728. context.Response.AddHeader("Content-Type", "application/xml");
  729. string uuid = context.Request.Params["getFunctions"];
  730. SetUpWebReport(uuid, context);
  731. if (WebUtils.SetupResponse(webReport, context))
  732. {
  733. FastReport.Utils.XmlDocument xml = new FastReport.Utils.XmlDocument();
  734. xml.AutoIndent = true;
  735. List<FunctionInfo> list = new List<FunctionInfo>();
  736. RegisteredObjects.Functions.EnumItems(list);
  737. FunctionInfo rootFunctions = null;
  738. foreach (FunctionInfo item in list)
  739. {
  740. if (item.Name == "Functions")
  741. {
  742. rootFunctions = item;
  743. break;
  744. }
  745. }
  746. xml.Root.Name = "ReportFunctions";
  747. #if !FRCORE
  748. if (rootFunctions != null)
  749. RegisteredObjects.CreateFunctionsTree(Report, rootFunctions, xml.Root);
  750. #endif
  751. using (MemoryStream stream = new MemoryStream())
  752. {
  753. xml.Save(stream);
  754. stream.Position = 0;
  755. byte[] buff = new byte[stream.Length];
  756. stream.Read(buff, 0, buff.Length);
  757. string answer = Encoding.UTF8.GetString(buff);
  758. context.Response.Write(answer);
  759. }
  760. }
  761. Finalize(context);
  762. }
  763. private void SendMsChartTemplate(HttpContext context)
  764. {
  765. var resourceName = context.Request.Params["designerMSChartTemplateName"];
  766. string result;
  767. var stream = ResourceLoader.GetStream("MSChart." + resourceName + ".xml");
  768. try
  769. {
  770. result = new StreamReader(stream).ReadToEnd();
  771. }
  772. catch (Exception ex)
  773. {
  774. context.Response.StatusCode = 404;
  775. return;
  776. }
  777. context.Response.StatusCode = 200;
  778. context.Response.ContentType = "application/xml";
  779. context.Response.Write(result);
  780. }
  781. }
  782. }