WebHandlers.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. using FastReport.Preview;
  2. using FastReport.Table;
  3. using System;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Security.Cryptography.X509Certificates;
  7. using System.Text;
  8. using System.Web;
  9. using System.Web.SessionState;
  10. namespace FastReport.Web.Handlers
  11. {
  12. /// <summary>
  13. /// Web handler class
  14. /// </summary>
  15. public partial class WebExport : IHttpHandler, IRequiresSessionState
  16. {
  17. /// <summary>
  18. /// You will need to configure this handler in the web.config file of your
  19. /// web and register it with IIS before being able to use it. For more information
  20. /// see the following link: http://go.microsoft.com/?linkid=8101007
  21. /// </summary>
  22. #region IHttpHandler Members
  23. private WebReportCache cache;
  24. private WebReport webReport;
  25. private WebLog log;
  26. private Report Report
  27. {
  28. get { return webReport.Report; }
  29. }
  30. private WebReportProperties Properties
  31. {
  32. get { return webReport.Prop; }
  33. }
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. public bool IsReusable
  38. {
  39. get { return true; }
  40. }
  41. /// <summary>
  42. /// Process Request
  43. /// </summary>
  44. /// <param name="context"></param>
  45. public void ProcessRequest(HttpContext context)
  46. {
  47. // init of cache
  48. cache = new WebReportCache(context);
  49. // pictures
  50. if (context.Request.Params[WebUtils.PicsPrefix] != null)
  51. SendPicture(context);
  52. // export
  53. else if (context.Request.QueryString[WebUtils.ConstID] != null)
  54. SendExport(context);
  55. // print
  56. else if (context.Request.Params[WebUtils.PrintPrefix] != null &&
  57. context.Request.Params[WebUtils.ReportPrefix] != null)
  58. SendPrint(context);
  59. // report
  60. else if (context.Request.Params["object"] != null)
  61. SendObjectResponse(context);
  62. else if (context.Request.Params["previewobject"] != null)
  63. SendPreviewObjectResponse(context);
  64. else if (context.Request.Params["form"] != null)
  65. SendForm(context);
  66. else if (context.Request.Params["respic"] != null)
  67. SendResourcePic(context);
  68. else if (context.Request.Params["getReport"] != null)
  69. SendReportTemplate(context);
  70. else if (context.Request.Params["putReport"] != null)
  71. SetReportTemplate(context);
  72. else if (context.Request.Params["getPreview"] != null)
  73. SendReportPreview(context);
  74. else if (context.Request.Params["designerMSChartTemplateName"] != null)
  75. SendMsChartTemplate(context);
  76. else if (context.Request.Params["makePreview"] != null)
  77. MakeReportPreview(context);
  78. else if (context.Request.Params["getFunctions"] != null)
  79. MakeFunctionsList(context);
  80. else if (context.Request.Params["getDesignerConfig"] != null)
  81. MakeDesignerConfig(context);
  82. else if (context.Request.Params["getConnectionTypes"] != null)
  83. MakeConnectionTypes(context);
  84. else if (context.Request.Params["getConnectionTables"] != null)
  85. MakeConnectionTables(context);
  86. else if (context.Request.Params["getConnectionStringProperties"] != null)
  87. GetConnectionStringProperties(context);
  88. else if (context.Request.Params["makeConnectionString"] != null)
  89. MakeConnectionString(context);
  90. else
  91. SendDebug(context);
  92. }
  93. private void Finalize(HttpContext context)
  94. {
  95. // clean for objects in file cache
  96. if (cache.WebFarmMode)
  97. {
  98. if (webReport != null)
  99. {
  100. if (webReport.Report != null)
  101. {
  102. if (webReport.Report.PreparedPages != null)
  103. {
  104. webReport.Report.PreparedPages.Clear();
  105. }
  106. webReport.Report.Dispose();
  107. webReport.Report = null;
  108. }
  109. webReport.Dispose();
  110. webReport = null;
  111. }
  112. }
  113. // see https://stackoverflow.com/questions/20988445/how-to-avoid-response-end-thread-was-being-aborted-exception-during-the-exce
  114. context.Response.Flush();
  115. context.Response.SuppressContent = true;
  116. context.ApplicationInstance.CompleteRequest();
  117. //context.Response.End();
  118. }
  119. private string GetResourceTemplateUrl(HttpContext context, string resName)
  120. {
  121. return new System.Web.UI.Page().ClientScript.
  122. GetWebResourceUrl(this.GetType(), string.Format("FastReport.Web.Resources.Templates.{0}", resName));
  123. }
  124. private void SendReportPreview(HttpContext context)
  125. {
  126. string guid = context.Request.Params["getPreview"];
  127. WebUtils.SetupResponse(webReport, context);
  128. context.Response.StatusCode = 501;
  129. Finalize(context);
  130. }
  131. private bool CertificateValidationCallBack(object sender,
  132. X509Certificate certificate,
  133. X509Chain chain,
  134. System.Net.Security.SslPolicyErrors sslPolicyErrors)
  135. {
  136. //Return True to force the certificate to be accepted.
  137. return true;
  138. }
  139. private void SendResourcePic(HttpContext context)
  140. {
  141. if (WebUtils.SetupResponse(null, context))
  142. {
  143. string file = context.Request.Params["respic"];
  144. context.Response.AddHeader("Content-Type", string.Concat("image/", Path.GetExtension(file)));
  145. string resname = file.Replace('/', '.');
  146. using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(string.Concat("FastReport.Web.Resources.jquery.", resname)))
  147. using (BinaryReader reader = new BinaryReader(stream))
  148. {
  149. byte[] res = reader.ReadBytes((int)stream.Length);
  150. context.Response.OutputStream.Write(res, 0, res.Length);
  151. res = null;
  152. }
  153. }
  154. Finalize(context);
  155. }
  156. private void SendObjectResponse(HttpContext context)
  157. {
  158. string uuid = context.Request.Params["object"];
  159. SetUpWebReport(uuid, context);
  160. if (WebUtils.SetupResponse(webReport, context))
  161. {
  162. if (!NeedExport(context) && !NeedPrint(context))
  163. {
  164. if (webReport.DesignReport)
  165. {
  166. webReport.ReportLoad(context);
  167. webReport.ReportRegisterDataAndRunEvent();
  168. SendDesigner(context, uuid);
  169. }
  170. else
  171. {
  172. SendReport(context);
  173. }
  174. }
  175. cache.PutObject(uuid, webReport);
  176. }
  177. Finalize(context);
  178. }
  179. private void SendReport(HttpContext context)
  180. {
  181. StringBuilder sb = new StringBuilder();
  182. if (context.Request.Params["settab"] != null)
  183. {
  184. int i = 0;
  185. if (int.TryParse(context.Request.Params["settab"], out i))
  186. if (i >= 0 && i < webReport.Tabs.Count)
  187. webReport.CurrentTabIndex = i;
  188. }
  189. else if (context.Request.Params["closetab"] != null)
  190. {
  191. int i = 0;
  192. if (int.TryParse(context.Request.Params["closetab"], out i))
  193. if (i >= 0 && i < webReport.Tabs.Count)
  194. {
  195. webReport.Tabs[i].Report.Dispose();
  196. webReport.Tabs.RemoveAt(i);
  197. if (i < webReport.Tabs.Count)
  198. webReport.CurrentTabIndex = i;
  199. else
  200. webReport.CurrentTabIndex = i - 1;
  201. }
  202. }
  203. Properties.ControlID = context.Request.Params["object"];
  204. if (String.IsNullOrEmpty(Properties.ControlID))
  205. Properties.ControlID = context.Request.Params["previewobject"];
  206. context.Response.AddHeader("Content-Type", "html/text");
  207. try
  208. {
  209. webReport.Localize();
  210. if (context.Request.Params["refresh"] != null)
  211. {
  212. webReport.Refresh();
  213. sb.Append(
  214. webReport.Toolbar.GetHtmlProgress(
  215. context.Response.ApplyAppPathModifier(WebUtils.HandlerFileName), Properties.ControlID, false, webReport.Width, webReport.Height));
  216. }
  217. else
  218. {
  219. if (context.Request.Params["reload"] != null)
  220. webReport.Refresh();
  221. if (context.Request.Params["dialog"] != null)
  222. webReport.SetUpDialogs(context);
  223. SetReportPage(context);
  224. if (Properties.RefreshTimeout > 0)
  225. sb.Append(
  226. webReport.Toolbar.GetAutoRefresh(
  227. context.Response.ApplyAppPathModifier(
  228. WebUtils.HandlerFileName), Properties.ControlID, Properties.RefreshTimeout * 1000));
  229. webReport.BeginReport(sb, context);
  230. StringBuilder sb_rep = new StringBuilder();
  231. webReport.GetReportHTML(sb_rep, context);
  232. webReport.GetReportTopToolbar(sb);
  233. sb.Append(sb_rep);
  234. webReport.GetReportBottomToolbar(sb);
  235. webReport.EndReport(sb);
  236. }
  237. }
  238. catch (Exception e)
  239. {
  240. log.AddError(e);
  241. }
  242. if (log.Text.Length > 0)
  243. {
  244. context.Response.Write(log.Text);
  245. log.Flush();
  246. log.Clear();
  247. }
  248. SetContainer(context, Properties.ControlID);
  249. context.Response.Write(sb.ToString());
  250. }
  251. private void SetContainer(HttpContext context, string p)
  252. {
  253. context.Response.AddHeader("FastReport-container", p);
  254. }
  255. private bool NeedPrint(HttpContext context)
  256. {
  257. if (context.Request.Params["print_browser"] != null)
  258. webReport.ExportHtml(context, true, true);
  259. else if (context.Request.Params["print_pdf"] != null)
  260. webReport.ExportPdf(context, true, true, true);
  261. else
  262. return false;
  263. return true;
  264. }
  265. private bool NeedExport(HttpContext context)
  266. {
  267. bool result = true;
  268. if (context.Request.Params["export_pdf"] != null)
  269. webReport.ExportPdf(context);
  270. else if (context.Request.Params["export_excel2007"] != null)
  271. webReport.ExportExcel2007(context);
  272. else if (context.Request.Params["export_word2007"] != null)
  273. webReport.ExportWord2007(context);
  274. else if (context.Request.Params["export_pp2007"] != null)
  275. webReport.ExportPowerPoint2007(context);
  276. else if (context.Request.Params["export_text"] != null)
  277. webReport.ExportText(context);
  278. else if (context.Request.Params["export_rtf"] != null)
  279. webReport.ExportRtf(context);
  280. else if (context.Request.Params["export_xps"] != null)
  281. webReport.ExportXps(context);
  282. else if (context.Request.Params["export_ods"] != null)
  283. webReport.ExportOds(context);
  284. else if (context.Request.Params["export_odt"] != null)
  285. webReport.ExportOdt(context);
  286. else if (context.Request.Params["export_mht"] != null)
  287. webReport.ExportMht(context);
  288. else if (context.Request.Params["export_xml"] != null)
  289. webReport.ExportXmlExcel(context);
  290. else if (context.Request.Params["export_dbf"] != null)
  291. webReport.ExportDbf(context);
  292. else if (context.Request.Params["export_csv"] != null)
  293. webReport.ExportCsv(context);
  294. else if (context.Request.Params["export_fpx"] != null)
  295. webReport.ExportPrepared(context);
  296. else
  297. result = false;
  298. return result;
  299. }
  300. private void SetReportPage(HttpContext context)
  301. {
  302. if (context.Request.Params["next"] != null)
  303. webReport.NextPage();
  304. else if (context.Request.Params["prev"] != null)
  305. webReport.PrevPage();
  306. else if (context.Request.Params["first"] != null)
  307. webReport.FirstPage();
  308. else if (context.Request.Params["last"] != null)
  309. webReport.LastPage();
  310. else if (context.Request.Params["goto"] != null)
  311. {
  312. int i = 0;
  313. if (int.TryParse(context.Request.Params["goto"], out i))
  314. webReport.SetPage(i - 1);
  315. }
  316. else if (context.Request.Params["bookmark"] != null)
  317. {
  318. int i = PageNByBookmark(context.Server.UrlDecode(context.Request.Params["bookmark"]));
  319. if (i != -1)
  320. webReport.SetPage(i);
  321. }
  322. else if (context.Request.Params["advmatrix_click"] != null)
  323. {
  324. string[] clickParams = context.Request.Params["advmatrix_click"].Split(',');
  325. if (clickParams.Length == 3)
  326. {
  327. int index = 0;
  328. int pageN = 0;
  329. if (int.TryParse(clickParams[1], out pageN) &&
  330. int.TryParse(clickParams[2], out index))
  331. {
  332. DoClickAdvancedMatrixObjectByParamID(clickParams[0], pageN, index);
  333. }
  334. }
  335. }
  336. else if (context.Request.Params["click"] != null)
  337. {
  338. string[] clickParams = context.Request.Params["click"].Split(',');
  339. if (clickParams.Length == 4)
  340. {
  341. float left = 0;
  342. float top = 0;
  343. int pageN = 0;
  344. if (int.TryParse(clickParams[1], out pageN) &&
  345. float.TryParse(clickParams[2], out left) &&
  346. float.TryParse(clickParams[3], out top))
  347. {
  348. DoClickObjectByParamID(clickParams[0], pageN, left, top);
  349. }
  350. }
  351. }
  352. else if (context.Request.Params["detailed_report"] != null)
  353. {
  354. string[] detailParams = context.Server.UrlDecode(context.Request.Params["detailed_report"]).Split(',');
  355. if (detailParams.Length == 3)
  356. {
  357. if (!String.IsNullOrEmpty(detailParams[0]) &&
  358. !String.IsNullOrEmpty(detailParams[1]) &&
  359. !String.IsNullOrEmpty(detailParams[2])
  360. )
  361. {
  362. DoDetailedReport(detailParams[0], detailParams[1], detailParams[2]);
  363. }
  364. }
  365. }
  366. else if (context.Request.Params["detailed_page"] != null)
  367. {
  368. string[] detailParams = context.Server.UrlDecode(context.Request.Params["detailed_page"]).Split(',');
  369. if (detailParams.Length == 3)
  370. {
  371. if (!String.IsNullOrEmpty(detailParams[0]) &&
  372. !String.IsNullOrEmpty(detailParams[1]) &&
  373. !String.IsNullOrEmpty(detailParams[2])
  374. )
  375. {
  376. DoDetailedPage(detailParams[0], detailParams[1], detailParams[2]);
  377. }
  378. }
  379. }
  380. else if (context.Request.Params["zoom_width"] != null)
  381. webReport.Prop.ZoomMode = ZoomMode.Width;
  382. else if (context.Request.Params["zoom_page"] != null)
  383. webReport.Prop.ZoomMode = ZoomMode.Page;
  384. else if (context.Request.Params["zoom_300"] != null)
  385. webReport.Prop.Zoom = 3;
  386. else if (context.Request.Params["zoom_200"] != null)
  387. webReport.Prop.Zoom = 2;
  388. else if (context.Request.Params["zoom_150"] != null)
  389. webReport.Prop.Zoom = 1.5f;
  390. else if (context.Request.Params["zoom_100"] != null)
  391. webReport.Prop.Zoom = 1;
  392. else if (context.Request.Params["zoom_90"] != null)
  393. webReport.Prop.Zoom = 0.9f;
  394. else if (context.Request.Params["zoom_75"] != null)
  395. webReport.Prop.Zoom = 0.75f;
  396. else if (context.Request.Params["zoom_50"] != null)
  397. webReport.Prop.Zoom = 0.5f;
  398. else if (context.Request.Params["zoom_25"] != null)
  399. webReport.Prop.Zoom = 0.25f;
  400. else if (context.Request.Params["checkbox_click"] != null)
  401. {
  402. string[] clickParams = context.Request.Params["checkbox_click"].Split(',');
  403. if (clickParams.Length == 4)
  404. {
  405. float left = 0;
  406. float top = 0;
  407. int pageN = 0;
  408. if (int.TryParse(clickParams[1], out pageN) &&
  409. float.TryParse(clickParams[2], out left) &&
  410. float.TryParse(clickParams[3], out top))
  411. {
  412. DoCheckboxClick(clickParams[0], pageN, left, top);
  413. }
  414. }
  415. }
  416. else if (context.Request.Params["text_edit"] != null)
  417. {
  418. string[] clickParams = context.Request.Params["text_edit"].Split(',');
  419. string text = context.Request.Form["text"];
  420. if (clickParams.Length == 4 && text != null)
  421. {
  422. float left = 0;
  423. float top = 0;
  424. int pageN = 0;
  425. if (int.TryParse(clickParams[1], out pageN) &&
  426. float.TryParse(clickParams[2], out left) &&
  427. float.TryParse(clickParams[3], out top))
  428. {
  429. string decodedText = HttpUtility.HtmlDecode(text);
  430. // we need to normalize line endings because they are different in .net and browsers
  431. string normalizedText = System.Text.RegularExpressions.Regex.Replace(decodedText, @"\r\n|\n\r|\n|\r", "\r\n");
  432. DoTextEdit(clickParams[0], pageN, left, top, normalizedText);
  433. }
  434. }
  435. }
  436. }
  437. private void DoCheckboxClick(string objectName, int pageN, float left, float top)
  438. {
  439. if (Report.PreparedPages == null)
  440. return;
  441. bool found = false;
  442. while (pageN < Report.PreparedPages.Count && !found)
  443. {
  444. ReportPage page = Report.PreparedPages.GetPage(pageN);
  445. if (page != null)
  446. {
  447. ObjectCollection allObjects = page.AllObjects;
  448. System.Drawing.PointF point = new System.Drawing.PointF(left + 1, top + 1);
  449. foreach (Base obj in allObjects)
  450. {
  451. CheckBoxObject c = obj as CheckBoxObject;
  452. if (c != null &&
  453. c.Name == objectName &&
  454. c.AbsBounds.Contains(point))
  455. {
  456. c.Checked = !c.Checked;
  457. if (webReport.Report.NeedRefresh)
  458. webReport.Report.InteractiveRefresh();
  459. else
  460. webReport.Report.PreparedPages.ModifyPage(pageN, page);
  461. found = true;
  462. break;
  463. }
  464. }
  465. page.Dispose();
  466. pageN++;
  467. }
  468. }
  469. }
  470. private void DoTextEdit(string objectName, int pageN, float left, float top, string text)
  471. {
  472. if (Report.PreparedPages == null)
  473. return;
  474. Action<TextObject, ReportPage> AssignText = delegate (TextObject t, ReportPage page)
  475. {
  476. t.Text = text;
  477. if (webReport.Report.NeedRefresh)
  478. webReport.Report.InteractiveRefresh();
  479. else
  480. webReport.Report.PreparedPages.ModifyPage(pageN, page);
  481. };
  482. ProcessClick(objectName, pageN, left, top, null, AssignText, null);
  483. }
  484. private void DoDetailedReport(string objectName, string paramName, string paramValue)
  485. {
  486. if (!String.IsNullOrEmpty(objectName))
  487. {
  488. ReportComponentBase obj = Report.FindObject(objectName) as ReportComponentBase;
  489. if (obj != null && obj.Hyperlink.Kind == HyperlinkKind.DetailReport)
  490. {
  491. Report tabReport = new Report();
  492. string fileName = obj.Hyperlink.DetailReportName;
  493. if (File.Exists(fileName))
  494. {
  495. tabReport.Load(fileName);
  496. Data.Parameter param = tabReport.Parameters.FindByName(paramName);
  497. if (param != null && param.ChildObjects.Count > 0)
  498. {
  499. string[] paramValues = paramValue.Split(obj.Hyperlink.ValuesSeparator[0]);
  500. if (paramValues.Length > 0)
  501. {
  502. int i = 0;
  503. foreach (Data.Parameter childParam in param.ChildObjects)
  504. {
  505. childParam.Value = paramValues[i++];
  506. if (i >= paramValues.Length)
  507. break;
  508. }
  509. }
  510. }
  511. else
  512. tabReport.SetParameterValue(paramName, paramValue);
  513. Report.Dictionary.ReRegisterData(tabReport.Dictionary);
  514. webReport.AddTab(tabReport, paramValue, false);
  515. int prevTab = webReport.CurrentTabIndex;
  516. webReport.CurrentTabIndex = webReport.Tabs.Count - 1;
  517. webReport.Prop.PreviousTab = prevTab;
  518. }
  519. }
  520. }
  521. }
  522. private void DoDetailedPage(string objectName, string paramName, string paramValue)
  523. {
  524. if (!String.IsNullOrEmpty(objectName))
  525. {
  526. Report currentReport = webReport.CurrentTab.NeedParent ? webReport.ReportTabs[0].Report : Report;
  527. ReportComponentBase obj = currentReport.FindObject(objectName) as ReportComponentBase;
  528. if (obj != null && obj.Hyperlink.Kind == HyperlinkKind.DetailPage)
  529. {
  530. ReportPage reportPage = currentReport.FindObject(obj.Hyperlink.DetailPageName) as ReportPage;
  531. if (reportPage != null)
  532. {
  533. Data.Parameter param = currentReport.Parameters.FindByName(paramName);
  534. if (param != null && param.ChildObjects.Count > 0)
  535. {
  536. string[] paramValues = paramValue.Split(obj.Hyperlink.ValuesSeparator[0]);
  537. if (paramValues.Length > 0)
  538. {
  539. int i = 0;
  540. foreach (Data.Parameter childParam in param.ChildObjects)
  541. {
  542. childParam.Value = paramValues[i++];
  543. if (i >= paramValues.Length)
  544. break;
  545. }
  546. }
  547. }
  548. else
  549. {
  550. currentReport.SetParameterValue(paramName, paramValue);
  551. }
  552. PreparedPages oldPreparedPages = currentReport.PreparedPages;
  553. PreparedPages pages = new PreparedPages(currentReport);
  554. Report tabReport = new Report();
  555. currentReport.SetPreparedPages(pages);
  556. currentReport.PreparePage(reportPage);
  557. tabReport.SetPreparedPages(currentReport.PreparedPages);
  558. webReport.AddTab(tabReport, paramValue, true, true);
  559. int prevTab = webReport.CurrentTabIndex;
  560. currentReport.SetPreparedPages(oldPreparedPages);
  561. webReport.CurrentTabIndex = webReport.Tabs.Count - 1;
  562. webReport.Prop.PreviousTab = prevTab;
  563. }
  564. }
  565. }
  566. }
  567. private void DoClickAdvancedMatrixObjectByParamID(string objectName, int pageN, int index)
  568. {
  569. if (webReport.Report.PreparedPages != null)
  570. {
  571. bool found = false;
  572. while (pageN < webReport.Report.PreparedPages.Count && !found)
  573. {
  574. ReportPage page = webReport.Report.PreparedPages.GetPage(pageN);
  575. if (page != null)
  576. {
  577. ObjectCollection allObjects = page.AllObjects;
  578. foreach (Base obj in allObjects)
  579. {
  580. if (obj is ReportComponentBase)
  581. {
  582. if (obj is AdvMatrix.MatrixCollapseButton)
  583. {
  584. AdvMatrix.MatrixCollapseButton collapseButton = obj as AdvMatrix.MatrixCollapseButton;
  585. if (collapseButton.Name == objectName
  586. && collapseButton.Index == index)
  587. {
  588. collapseButton.MatrixCollapseButtonClick();
  589. Click(collapseButton, pageN, page);
  590. found = true;
  591. break;
  592. }
  593. }
  594. else if (obj is AdvMatrix.MatrixSortButton)
  595. {
  596. AdvMatrix.MatrixSortButton sortButton = obj as AdvMatrix.MatrixSortButton;
  597. if (sortButton.Name == objectName
  598. && sortButton.Index == index)
  599. {
  600. sortButton.MatrixSortButtonClick();
  601. Click(sortButton, pageN, page);
  602. found = true;
  603. break;
  604. }
  605. }
  606. }
  607. }
  608. page.Dispose();
  609. pageN++;
  610. }
  611. }
  612. }
  613. }
  614. private void DoClickObjectByParamID(string objectName, int pageN, float left, float top)
  615. {
  616. if (webReport.Report.PreparedPages != null)
  617. {
  618. bool found = false;
  619. while (pageN < webReport.Report.PreparedPages.Count && !found)
  620. {
  621. ReportPage page = webReport.Report.PreparedPages.GetPage(pageN);
  622. if (page != null)
  623. {
  624. ObjectCollection allObjects = page.AllObjects;
  625. System.Drawing.PointF point = new System.Drawing.PointF(left + 1, top + 1);
  626. foreach (Base obj in allObjects)
  627. {
  628. if (obj is ReportComponentBase)
  629. {
  630. ReportComponentBase c = obj as ReportComponentBase;
  631. if (c is TableBase)
  632. {
  633. TableBase table = c as TableBase;
  634. for (int i = 0; i < table.RowCount; i++)
  635. {
  636. for (int j = 0; j < table.ColumnCount; j++)
  637. {
  638. TableCell textcell = table[j, i];
  639. if (textcell.Name == objectName)
  640. {
  641. System.Drawing.RectangleF rect =
  642. new System.Drawing.RectangleF(table.Columns[j].AbsLeft,
  643. table.Rows[i].AbsTop,
  644. textcell.Width,
  645. textcell.Height);
  646. if (rect.Contains(point))
  647. {
  648. Click(textcell, pageN, page);
  649. found = true;
  650. break;
  651. }
  652. }
  653. }
  654. if (found)
  655. break;
  656. }
  657. }
  658. else
  659. if (c.Name == objectName &&
  660. #if FRCORE
  661. c.AbsBounds.Contains(point))
  662. #else
  663. c.PointInObject(point))
  664. #endif
  665. {
  666. Click(c, pageN, page);
  667. found = true;
  668. break;
  669. }
  670. }
  671. }
  672. page.Dispose();
  673. pageN++;
  674. }
  675. }
  676. }
  677. }
  678. private void Click(ReportComponentBase c, int pageN, ReportPage page)
  679. {
  680. c.OnClick(null);
  681. if (webReport.Report.NeedRefresh)
  682. webReport.Report.InteractiveRefresh();
  683. else
  684. webReport.Report.PreparedPages.ModifyPage(pageN, page);
  685. }
  686. private int PageNByBookmark(string bookmark)
  687. {
  688. int pageN = -1;
  689. if (webReport.Report.PreparedPages != null)
  690. {
  691. for (int i = 0; i < webReport.Report.PreparedPages.Count; i++)
  692. {
  693. ReportPage page = webReport.Report.PreparedPages.GetPage(i);
  694. if (page != null)
  695. {
  696. ObjectCollection allObjects = page.AllObjects;
  697. for (int j = 0; j < allObjects.Count; j++)
  698. {
  699. ReportComponentBase c = allObjects[j] as ReportComponentBase;
  700. if (c.Bookmark == bookmark)
  701. {
  702. pageN = i;
  703. break;
  704. }
  705. }
  706. page.Dispose();
  707. if (pageN != -1)
  708. break;
  709. }
  710. }
  711. }
  712. return pageN;
  713. }
  714. private void SendDebug(HttpContext context)
  715. {
  716. if (WebUtils.SetupResponse(null, context))
  717. {
  718. int count = cache.CleanFileStorage();
  719. context.Response.ContentType = "text/html";
  720. context.Response.Write("FastReport.Web.WebExport handler: FastReport.NET");
  721. context.Response.Write(String.Concat("<b>v", FastReport.Utils.Config.Version, "</b><br/>",
  722. "Current server time: <b>", DateTime.Now.ToString(), "</b><br/>",
  723. "Cluster mode: <b>", (cache.WebFarmMode ? "ON" : "OFF"), "</b><br/>",
  724. "Files in storage: <b>", count.ToString(), "</b>"));
  725. }
  726. Finalize(context);
  727. }
  728. private void SendPrint(HttpContext context)
  729. {
  730. try
  731. {
  732. string guid = context.Request.Params[WebUtils.ReportPrefix];
  733. SetUpWebReport(guid, context);
  734. if (WebUtils.SetupResponse(webReport, context))
  735. {
  736. if (context.Request.Params[WebUtils.PrintPrefix] == "pdf")
  737. webReport.PrintPdf(context);
  738. else
  739. webReport.PrintHtml(context);
  740. }
  741. }
  742. catch (Exception e)
  743. {
  744. log.AddError(e);
  745. }
  746. if (log.Text.Length > 0)
  747. {
  748. context.Response.Write(log.Text);
  749. log.Flush();
  750. log.Clear();
  751. }
  752. Finalize(context);
  753. }
  754. private void SendExport(HttpContext context)
  755. {
  756. log = new WebLog(false);
  757. string cacheKeyName = context.Request.QueryString[WebUtils.ConstID];
  758. WebExportItem exportItem = cache.GetObject(cacheKeyName) as WebExportItem;
  759. SetUpWebReport(exportItem.ReportID, context);
  760. if (WebUtils.SetupResponse(webReport, context))
  761. {
  762. if (exportItem != null)
  763. {
  764. try
  765. {
  766. bool isIE8 = WebUtils.IsIE8(context);
  767. //cache.Remove(cacheKeyName);
  768. context.Response.ClearContent();
  769. context.Response.ClearHeaders();
  770. if (string.IsNullOrEmpty(exportItem.ContentType))
  771. context.Response.ContentType = "application/unknown";
  772. else
  773. context.Response.ContentType = exportItem.ContentType;
  774. context.Response.AddHeader("Content-Type", context.Response.ContentType);
  775. if (!isIE8)
  776. WebUtils.AddNoCacheHeaders(context);
  777. string disposition = "attachment";
  778. if (context.Request.QueryString["displayinline"].Equals("True", StringComparison.OrdinalIgnoreCase))
  779. disposition = "inline";
  780. string fileName = context.Server.UrlPathEncode(exportItem.FileName).Replace(",", "");
  781. string s = String.Concat(isIE8 ? "=" : "*=UTF-8''", fileName);
  782. string contentDisposition = string.Format("{0}; filename{1}", disposition, s);
  783. context.Response.AddHeader("Content-Disposition", contentDisposition);
  784. context.Response.AddHeader("Cache-Control", "private");
  785. context.Response.Flush();
  786. WebUtils.ResponseChunked(context.Response, exportItem.File);
  787. //exportItem.File = null;
  788. //exportItem = null;
  789. }
  790. catch (Exception e)
  791. {
  792. log.AddError(e);
  793. }
  794. if (log.Text.Length > 0)
  795. {
  796. context.Response.Write(log.Text);
  797. log.Flush();
  798. log.Clear();
  799. }
  800. }
  801. }
  802. Finalize(context);
  803. }
  804. private void SendPicture(HttpContext context)
  805. {
  806. if (WebUtils.SetupResponse(null, context))
  807. {
  808. try
  809. {
  810. string imageKeyName = Convert.ToString(context.Request.Params[WebUtils.PicsPrefix]);
  811. byte[] image = cache.GetObject(imageKeyName) as byte[];
  812. context.Response.ContentType = WebUtils.IsPng(image) ? "image/png" : "image/svg+xml";
  813. context.Response.Flush();
  814. try
  815. {
  816. if (image != null)
  817. WebUtils.ResponseChunked(context.Response, image);
  818. }
  819. finally
  820. {
  821. image = null;
  822. }
  823. }
  824. catch
  825. {
  826. // nothing
  827. }
  828. }
  829. Finalize(context);
  830. }
  831. private void SendForm(HttpContext context)
  832. {
  833. context.Response.AddHeader("Content-Type", "text/html");
  834. string uuid = context.Request.Params["form"];
  835. SetUpWebReport(uuid, context);
  836. if (WebUtils.SetupResponse(webReport, context))
  837. {
  838. string formName = context.Request.Params["formName"];
  839. if (webReport != null && formName == "text_edit")
  840. {
  841. string click = context.Request.Params["formClick"].ToString();
  842. if (!String.IsNullOrEmpty(click))
  843. {
  844. string[] clickParams = click.Split(',');
  845. if (clickParams.Length == 4)
  846. {
  847. int pageN;
  848. float left;
  849. float top;
  850. if (int.TryParse(clickParams[1], out pageN) &&
  851. float.TryParse(clickParams[2], out left) &&
  852. float.TryParse(clickParams[3], out top))
  853. {
  854. WriteTextEditForm(context.Response, clickParams[0], pageN, left, top);
  855. }
  856. }
  857. }
  858. }
  859. }
  860. Finalize(context);
  861. }
  862. private void WriteTextEditForm(HttpResponse response, string objectName, int pageN, float left, float top)
  863. {
  864. if (Report.PreparedPages == null)
  865. return;
  866. ProcessClick(objectName, pageN, left, top, EditClick, null, response);
  867. }
  868. private void EditClick(TextObject c, HttpResponse response)
  869. {
  870. string encodedText = HttpUtility.HtmlEncode((c as TextObject).Text);
  871. WebTemplate template = new WebTemplate(GetResourceTemplate("textedit_form.html"), WebTemplateMode.HTML, true);
  872. template.SetVariable("text", encodedText);
  873. template.SetVariable("cancel-button", GetCancelButton());
  874. template.SetVariable("ok-button", GetOkButton());
  875. response.Write(template.Prepare());
  876. }
  877. private void ProcessClick(string objectName, int pageN, float left, float top, Action<TextObject, HttpResponse> HandleClickFront, Action<TextObject,
  878. ReportPage> HandleClickBack, HttpResponse response)
  879. {
  880. bool found = false;
  881. while (pageN < Report.PreparedPages.Count && !found)
  882. {
  883. ReportPage page = Report.PreparedPages.GetPage(pageN);
  884. if (page != null)
  885. {
  886. ObjectCollection allObjects = page.AllObjects;
  887. System.Drawing.PointF point = new System.Drawing.PointF(left + 1, top + 1);
  888. foreach (Base obj in allObjects)
  889. {
  890. if (obj is ReportComponentBase)
  891. {
  892. ReportComponentBase c = obj as ReportComponentBase;
  893. if (c is TableBase)
  894. {
  895. TableBase table = c as TableBase;
  896. for (int i = 0; i < table.RowCount; i++)
  897. {
  898. for (int j = 0; j < table.ColumnCount; j++)
  899. {
  900. TableCell textcell = table[j, i];
  901. if (textcell.Name == objectName)
  902. {
  903. RectangleF rect = new RectangleF(table.Columns[j].AbsLeft,
  904. table.Rows[i].AbsTop,
  905. textcell.Width,
  906. textcell.Height);
  907. if (rect.Contains(point))
  908. {
  909. if (HandleClickFront != null)
  910. HandleClickFront(textcell as TextObject, response);
  911. if (HandleClickBack != null)
  912. HandleClickBack(textcell as TextObject, page);
  913. found = true;
  914. break;
  915. }
  916. }
  917. }
  918. if (found)
  919. break;
  920. }
  921. }
  922. else if (c is TextObject)
  923. {
  924. if (c != null && c.Name == objectName && c.AbsBounds.Contains(point))
  925. {
  926. if (HandleClickFront != null)
  927. HandleClickFront(c as TextObject, response);
  928. if (HandleClickBack != null)
  929. HandleClickBack(c as TextObject, page);
  930. found = true;
  931. break;
  932. }
  933. }
  934. if (found)
  935. break;
  936. }
  937. }
  938. page.Dispose();
  939. pageN++;
  940. }
  941. }
  942. }
  943. private string GetCancelButton()
  944. {
  945. StringBuilder sb = new StringBuilder();
  946. WebRes res = webReport.ReportRes;
  947. res.Root("Buttons");
  948. sb.Append(string.Format("<button onclick=\"window.close();\">{0}</button>", res.Get("Cancel")));
  949. return sb.ToString();
  950. }
  951. private string GetOkButton()
  952. {
  953. StringBuilder sb = new StringBuilder();
  954. WebRes res = webReport.ReportRes;
  955. res.Root("Buttons");
  956. sb.Append(string.Format("<button onclick=\"window.postMessage('submit', '*'); \">{0}</button>", res.Get("Ok")));
  957. return sb.ToString();
  958. }
  959. private void SetUpWebReport(string ID, HttpContext context)
  960. {
  961. if (webReport == null)
  962. {
  963. webReport = new WebReport();
  964. webReport.ReportGuid = ID;
  965. }
  966. webReport = cache.GetObject(ID, webReport) as WebReport;
  967. webReport.Prop.HandlerURL = WebUtils.GetBasePath(context) + WebUtils.HandlerFileName;
  968. cache.Priority = webReport.CachePriority;
  969. cache.Delay = webReport.CacheDelay;
  970. log = new WebLog(webReport.Debug);
  971. if (!String.IsNullOrEmpty(webReport.LogFile))
  972. {
  973. log.LogFile = context.Server.MapPath(webReport.LogFile);
  974. }
  975. }
  976. private string GetResourceTemplate(string name)
  977. {
  978. string result;
  979. using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(string.Format("FastReport.Web.Resources.Templates.{0}", name)))
  980. using (TextReader reader = new StreamReader(stream))
  981. result = reader.ReadToEnd();
  982. return result;
  983. }
  984. #endregion
  985. }
  986. }