webpage.class.inc.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. <?php
  2. // Copyright (C) 2010-2015 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Class WebPage
  20. *
  21. * @copyright Copyright (C) 2010-2015 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. /**
  25. * Generic interface common to CLI and Web pages
  26. */
  27. Interface Page
  28. {
  29. public function output();
  30. public function add($sText);
  31. public function p($sText);
  32. public function pre($sText);
  33. public function add_comment($sText);
  34. public function table($aConfig, $aData, $aParams = array());
  35. }
  36. /**
  37. * Simple helper class to ease the production of HTML pages
  38. *
  39. * This class provide methods to add content, scripts, includes... to a web page
  40. * and renders the full web page by putting the elements in the proper place & order
  41. * when the output() method is called.
  42. * Usage:
  43. * $oPage = new WebPage("Title of my page");
  44. * $oPage->p("Hello World !");
  45. * $oPage->output();
  46. */
  47. class WebPage implements Page
  48. {
  49. protected $s_title;
  50. protected $s_content;
  51. protected $s_deferred_content;
  52. protected $a_scripts;
  53. protected $a_dict_entries;
  54. protected $a_styles;
  55. protected $a_include_scripts;
  56. protected $a_include_stylesheets;
  57. protected $a_headers;
  58. protected $a_base;
  59. protected $iNextId;
  60. protected $iTransactionId;
  61. protected $sContentType;
  62. protected $sContentDisposition;
  63. protected $sContentFileName;
  64. protected $bTrashUnexpectedOutput;
  65. protected $s_sOutputFormat;
  66. protected $a_OutputOptions;
  67. protected $bPrintable;
  68. public function __construct($s_title, $bPrintable = false)
  69. {
  70. $this->s_title = $s_title;
  71. $this->s_content = "";
  72. $this->s_deferred_content = '';
  73. $this->a_scripts = array();
  74. $this->a_dict_entries = array();
  75. $this->a_styles = array();
  76. $this->a_linked_scripts = array();
  77. $this->a_linked_stylesheets = array();
  78. $this->a_headers = array();
  79. $this->a_base = array( 'href' => '', 'target' => '');
  80. $this->iNextId = 0;
  81. $this->iTransactionId = 0;
  82. $this->sContentType = '';
  83. $this->sContentDisposition = '';
  84. $this->sContentFileName = '';
  85. $this->bTrashUnexpectedOutput = false;
  86. $this->s_OutputFormat = utils::ReadParam('output_format', 'html');
  87. $this->a_OutputOptions = array();
  88. $this->bPrintable = $bPrintable;
  89. ob_start(); // Start capturing the output
  90. }
  91. /**
  92. * Change the title of the page after its creation
  93. */
  94. public function set_title($s_title)
  95. {
  96. $this->s_title = $s_title;
  97. }
  98. /**
  99. * Specify a default URL and a default target for all links on a page
  100. */
  101. public function set_base($s_href = '', $s_target = '')
  102. {
  103. $this->a_base['href'] = $s_href;
  104. $this->a_base['target'] = $s_target;
  105. }
  106. /**
  107. * Add any text or HTML fragment to the body of the page
  108. */
  109. public function add($s_html)
  110. {
  111. $this->s_content .= $s_html;
  112. }
  113. /**
  114. * Add any text or HTML fragment (identified by an ID) at the end of the body of the page
  115. * This is useful to add hidden content, DIVs or FORMs that should not
  116. * be embedded into each other.
  117. */
  118. public function add_at_the_end($s_html, $sId = '')
  119. {
  120. $this->s_deferred_content .= $s_html;
  121. }
  122. /**
  123. * Add a paragraph to the body of the page
  124. */
  125. public function p($s_html)
  126. {
  127. $this->add($this->GetP($s_html));
  128. }
  129. /**
  130. * Add a pre-formatted text to the body of the page
  131. */
  132. public function pre($s_html)
  133. {
  134. $this->add('<pre>'.$s_html.'</pre>');
  135. }
  136. /**
  137. * Add a comment
  138. */
  139. public function add_comment($sText)
  140. {
  141. $this->add('<!--'.$sText.'-->');
  142. }
  143. /**
  144. * Add a paragraph to the body of the page
  145. */
  146. public function GetP($s_html)
  147. {
  148. return "<p>$s_html</p>\n";
  149. }
  150. /**
  151. * Adds a tabular content to the web page
  152. * @param Hash $aConfig Configuration of the table: hash array of 'column_id' => 'Column Label'
  153. * @param Hash $aData Hash array. Data to display in the table: each row is made of 'column_id' => Data. A column 'pkey' is expected for each row
  154. * @param Hash $aParams Hash array. Extra parameters for the table.
  155. * @return void
  156. */
  157. public function table($aConfig, $aData, $aParams = array())
  158. {
  159. $this->add($this->GetTable($aConfig, $aData, $aParams));
  160. }
  161. public function GetTable($aConfig, $aData, $aParams = array())
  162. {
  163. $oAppContext = new ApplicationContext();
  164. static $iNbTables = 0;
  165. $iNbTables++;
  166. $sHtml = "";
  167. $sHtml .= "<table class=\"listResults\">\n";
  168. $sHtml .= "<thead>\n";
  169. $sHtml .= "<tr>\n";
  170. foreach($aConfig as $sName=>$aDef)
  171. {
  172. $sHtml .= "<th title=\"".$aDef['description']."\">".$aDef['label']."</th>\n";
  173. }
  174. $sHtml .= "</tr>\n";
  175. $sHtml .= "</thead>\n";
  176. $sHtml .= "<tbody>\n";
  177. foreach($aData as $aRow)
  178. {
  179. $sHtml .= $this->GetTableRow($aRow, $aConfig);
  180. }
  181. $sHtml .= "</tbody>\n";
  182. $sHtml .= "</table>\n";
  183. return $sHtml;
  184. }
  185. public function GetTableRow($aRow, $aConfig)
  186. {
  187. $sHtml = '';
  188. if (isset($aRow['@class'])) // Row specific class, for hilighting certain rows
  189. {
  190. $sHtml .= "<tr class=\"{$aRow['@class']}\">";
  191. }
  192. else
  193. {
  194. $sHtml .= "<tr>";
  195. }
  196. foreach($aConfig as $sName=>$aAttribs)
  197. {
  198. $sClass = isset($aAttribs['class']) ? 'class="'.$aAttribs['class'].'"' : '';
  199. $sValue = ($aRow[$sName] === '') ? '&nbsp;' : $aRow[$sName];
  200. $sHtml .= "<td $sClass>$sValue</td>";
  201. }
  202. $sHtml .= "</tr>";
  203. return $sHtml;
  204. }
  205. /**
  206. * Add some Javascript to the header of the page
  207. */
  208. public function add_script($s_script)
  209. {
  210. $this->a_scripts[] = $s_script;
  211. }
  212. /**
  213. * Add some Javascript to the header of the page
  214. */
  215. public function add_ready_script($s_script)
  216. {
  217. // Do nothing silently... this is not supported by this type of page...
  218. }
  219. /**
  220. * Add a dictionary entry for the Javascript side
  221. */
  222. public function add_dict_entry($s_entryId)
  223. {
  224. $this->a_dict_entries[$s_entryId] = Dict::S($s_entryId);
  225. }
  226. /**
  227. * Add some CSS definitions to the header of the page
  228. */
  229. public function add_style($s_style)
  230. {
  231. $this->a_styles[] = $s_style;
  232. }
  233. /**
  234. * Add a script (as an include, i.e. link) to the header of the page
  235. */
  236. public function add_linked_script($s_linked_script)
  237. {
  238. $this->a_linked_scripts[$s_linked_script] = $s_linked_script;
  239. }
  240. /**
  241. * Add a CSS stylesheet (as an include, i.e. link) to the header of the page
  242. */
  243. public function add_linked_stylesheet($s_linked_stylesheet, $s_condition = "")
  244. {
  245. $this->a_linked_stylesheets[] = array( 'link' => $s_linked_stylesheet, 'condition' => $s_condition);
  246. }
  247. public function add_saas($sSaasRelPath)
  248. {
  249. $sCssRelPath = utils::GetCSSFromSASS($sSaasRelPath);
  250. $sRootUrl = utils::GetAbsoluteUrlAppRoot();
  251. if ($sRootUrl === '')
  252. {
  253. // We're running the setup of the first install...
  254. $sRootUrl = '../';
  255. }
  256. $sCSSUrl = $sRootUrl.$sCssRelPath;
  257. $this->add_linked_stylesheet($sCSSUrl);
  258. }
  259. /**
  260. * Add some custom header to the page
  261. */
  262. public function add_header($s_header)
  263. {
  264. $this->a_headers[] = $s_header;
  265. }
  266. /**
  267. * Add needed headers to the page so that it will no be cached
  268. */
  269. public function no_cache()
  270. {
  271. $this->add_header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  272. $this->add_header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  273. }
  274. /**
  275. * Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
  276. */
  277. public function details($aFields)
  278. {
  279. $this->add($this->GetDetails($aFields));
  280. }
  281. /**
  282. * Whether or not the page is a PDF page
  283. * @return boolean
  284. */
  285. public function is_pdf()
  286. {
  287. return false;
  288. }
  289. /**
  290. * Records the current state of the 'html' part of the page output
  291. * @return mixed The current state of the 'html' output
  292. */
  293. public function start_capture()
  294. {
  295. return strlen($this->s_content);
  296. }
  297. /**
  298. * Returns the part of the html output that occurred since the call to start_capture
  299. * and removes this part from the current html output
  300. * @param $offset mixed The value returned by start_capture
  301. * @return string The part of the html output that was added since the call to start_capture
  302. */
  303. public function end_capture($offset)
  304. {
  305. $sCaptured = substr($this->s_content, $offset);
  306. $this->s_content = substr($this->s_content, 0, $offset);
  307. return $sCaptured;
  308. }
  309. /**
  310. * Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
  311. */
  312. public function GetDetails($aFields)
  313. {
  314. $sHtml = "<div class=\"details\">\n";
  315. foreach($aFields as $aAttrib)
  316. {
  317. $sHtml .= "<div class=\"field_container field_{$aAttrib['layout']}\" data-attcode=\"{$aAttrib['attcode']}\">\n";
  318. $sHtml .= "<div class=\"field_label label\">{$aAttrib['label']}</div>\n";
  319. $sHtml .= "<div class=\"field_data\">\n";
  320. // By Rom, for csv import, proposed to show several values for column selection
  321. if (is_array($aAttrib['value']))
  322. {
  323. $sHtml .= "<div class=\"field_value\">".implode("</div><div>", $aAttrib['value'])."</div>\n";
  324. }
  325. else
  326. {
  327. $sHtml .= "<div class=\"field_value\">".$aAttrib['value']."</div>\n";
  328. }
  329. // Checking if we should add comments & infos
  330. $sComment = (isset($aAttrib['comments'])) ? $aAttrib['comments'] : '';
  331. $sInfo = (isset($aAttrib['infos'])) ? $aAttrib['infos'] : '';
  332. if($sComment !== '')
  333. {
  334. $sHtml .= "<div class=\"field_comments\">$sComment</div>\n";
  335. }
  336. if($sInfo !== '')
  337. {
  338. $sHtml .= "<div class=\"field_infos\">$sInfo</div>\n";
  339. }
  340. $sHtml .= "</div>\n";
  341. $sHtml .= "</div>\n";
  342. }
  343. $sHtml .= "</div>\n";
  344. return $sHtml;
  345. }
  346. /**
  347. * Build a set of radio buttons suitable for editing a field/attribute of an object (including its validation)
  348. * @param $aAllowedValues hash Array of value => display_value
  349. * @param $value mixed Current value for the field/attribute
  350. * @param $iId mixed Unique Id for the input control in the page
  351. * @param $sFieldName string The name of the field, attr_<$sFieldName> will hold the value for the field
  352. * @param $bMandatory bool Whether or not the field is mandatory
  353. * @param $bVertical bool Disposition of the radio buttons vertical or horizontal
  354. * @param $sValidationField string HTML fragment holding the validation field (exclamation icon...)
  355. * @return string The HTML fragment corresponding to the radio buttons
  356. */
  357. public function GetRadioButtons($aAllowedValues, $value, $iId, $sFieldName, $bMandatory, $bVertical, $sValidationField)
  358. {
  359. $idx = 0;
  360. $sHTMLValue = '';
  361. foreach($aAllowedValues as $key => $display_value)
  362. {
  363. if ((count($aAllowedValues) == 1) && ($bMandatory == 'true') )
  364. {
  365. // When there is only once choice, select it by default
  366. $sSelected = ' checked';
  367. }
  368. else
  369. {
  370. $sSelected = ($value == $key) ? ' checked' : '';
  371. }
  372. $sHTMLValue .= "<input type=\"radio\" id=\"{$iId}_{$key}\" name=\"radio_$sFieldName\" onChange=\"$('#{$iId}').val(this.value).trigger('change');\" value=\"$key\"$sSelected><label class=\"radio\" for=\"{$iId}_{$key}\">&nbsp;$display_value</label>&nbsp;";
  373. if ($bVertical)
  374. {
  375. if ($idx == 0)
  376. {
  377. // Validation icon at the end of the first line
  378. $sHTMLValue .= "&nbsp;{$sValidationField}\n";
  379. }
  380. $sHTMLValue .= "<br>\n";
  381. }
  382. $idx++;
  383. }
  384. $sHTMLValue .= "<input type=\"hidden\" id=\"$iId\" name=\"$sFieldName\" value=\"$value\"/>";
  385. if (!$bVertical)
  386. {
  387. // Validation icon at the end of the line
  388. $sHTMLValue .= "&nbsp;{$sValidationField}\n";
  389. }
  390. return $sHTMLValue;
  391. }
  392. /**
  393. * Discard unexpected output data (such as PHP warnings)
  394. * This is a MUST when the Page output is DATA (download of a document, download CSV export, download ...)
  395. */
  396. public function TrashUnexpectedOutput()
  397. {
  398. $this->bTrashUnexpectedOutput = true;
  399. }
  400. /**
  401. * Read the output buffer and deal with its contents:
  402. * - trash unexpected output if the flag has been set
  403. * - report unexpected behaviors such as the output buffering being stopped
  404. *
  405. * Possible improvement: I've noticed that several output buffers are stacked,
  406. * if they are not empty, the output will be corrupted. The solution would
  407. * consist in unstacking all of them (and concatenate the contents).
  408. */
  409. protected function ob_get_clean_safe()
  410. {
  411. $sOutput = ob_get_contents();
  412. if ($sOutput === false)
  413. {
  414. $sMsg = "Design/integration issue: No output buffer. Some piece of code has called ob_get_clean() or ob_end_clean() without calling ob_start()";
  415. if ($this->bTrashUnexpectedOutput)
  416. {
  417. IssueLog::Error($sMsg);
  418. $sOutput = '';
  419. }
  420. else
  421. {
  422. $sOutput = $sMsg;
  423. }
  424. }
  425. else
  426. {
  427. ob_end_clean(); // on some versions of PHP doing so when the output buffering is stopped can cause a notice
  428. if ($this->bTrashUnexpectedOutput)
  429. {
  430. if (trim($sOutput) != '')
  431. {
  432. if (Utils::GetConfig() && Utils::GetConfig()->Get('debug_report_spurious_chars'))
  433. {
  434. IssueLog::Error("Trashing unexpected output:'$sOutput'\n");
  435. }
  436. }
  437. $sOutput = '';
  438. }
  439. }
  440. return $sOutput;
  441. }
  442. /**
  443. * Outputs (via some echo) the complete HTML page by assembling all its elements
  444. */
  445. public function output()
  446. {
  447. foreach($this->a_headers as $s_header)
  448. {
  449. header($s_header);
  450. }
  451. $s_captured_output = $this->ob_get_clean_safe();
  452. echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
  453. echo "<html>\n";
  454. echo "<head>\n";
  455. echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
  456. echo "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, shrink-to-fit=no\" />";
  457. echo "<title>".htmlentities($this->s_title, ENT_QUOTES, 'UTF-8')."</title>\n";
  458. echo $this->get_base_tag();
  459. foreach($this->a_linked_scripts as $s_script)
  460. {
  461. // Make sure that the URL to the script contains the application's version number
  462. // so that the new script do NOT get reloaded from the cache when the application is upgraded
  463. if (strpos($s_script, '?') === false)
  464. {
  465. $s_script .= "?itopversion=".ITOP_VERSION;
  466. }
  467. else
  468. {
  469. $s_script .= "&itopversion=".ITOP_VERSION;
  470. }
  471. echo "<script type=\"text/javascript\" src=\"$s_script\"></script>\n";
  472. }
  473. if (count($this->a_scripts)>0)
  474. {
  475. echo "<script type=\"text/javascript\">\n";
  476. foreach($this->a_scripts as $s_script)
  477. {
  478. echo "$s_script\n";
  479. }
  480. echo "</script>\n";
  481. }
  482. $this->output_dict_entries();
  483. foreach($this->a_linked_stylesheets as $a_stylesheet)
  484. {
  485. if (strpos($a_stylesheet['link'], '?') === false)
  486. {
  487. $s_stylesheet = $a_stylesheet['link']."?itopversion=".ITOP_VERSION;
  488. }
  489. else
  490. {
  491. $s_stylesheet = $a_stylesheet['link']."&itopversion=".ITOP_VERSION;
  492. }
  493. if ($a_stylesheet['condition'] != "")
  494. {
  495. echo "<!--[if {$a_stylesheet['condition']}]>\n";
  496. }
  497. echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$s_stylesheet}\" />\n";
  498. if ($a_stylesheet['condition'] != "")
  499. {
  500. echo "<![endif]-->\n";
  501. }
  502. }
  503. if (count($this->a_styles)>0)
  504. {
  505. echo "<style>\n";
  506. foreach($this->a_styles as $s_style)
  507. {
  508. echo "$s_style\n";
  509. }
  510. echo "</style>\n";
  511. }
  512. if (class_exists('MetaModel') && MetaModel::GetConfig())
  513. {
  514. echo "<link rel=\"shortcut icon\" href=\"".utils::GetAbsoluteUrlAppRoot()."images/favicon.ico?itopversion=".ITOP_VERSION."\" />\n";
  515. }
  516. echo "</head>\n";
  517. echo "<body>\n";
  518. echo self::FilterXSS($this->s_content);
  519. if (trim($s_captured_output) != "")
  520. {
  521. echo "<div class=\"raw_output\">".self::FilterXSS($s_captured_output)."</div>\n";
  522. }
  523. echo '<div id="at_the_end">'.self::FilterXSS($this->s_deferred_content).'</div>';
  524. echo "</body>\n";
  525. echo "</html>\n";
  526. if (class_exists('DBSearch'))
  527. {
  528. DBSearch::RecordQueryTrace();
  529. }
  530. if (class_exists('ExecutionKPI'))
  531. {
  532. ExecutionKPI::ReportStats();
  533. }
  534. }
  535. /**
  536. * Build a series of hidden field[s] from an array
  537. */
  538. public function add_input_hidden($sLabel, $aData)
  539. {
  540. foreach($aData as $sKey => $sValue)
  541. {
  542. // Note: protection added to protect against the Notice 'array to string conversion' that appeared with PHP 5.4
  543. // (this function seems unused though!)
  544. if (is_scalar($sValue))
  545. {
  546. $this->add("<input type=\"hidden\" name=\"".$sLabel."[$sKey]\" value=\"$sValue\">");
  547. }
  548. }
  549. }
  550. protected function get_base_tag()
  551. {
  552. $sTag = '';
  553. if (($this->a_base['href'] != '') || ($this->a_base['target'] != ''))
  554. {
  555. $sTag = '<base ';
  556. if (($this->a_base['href'] != ''))
  557. {
  558. $sTag .= "href =\"{$this->a_base['href']}\" ";
  559. }
  560. if (($this->a_base['target'] != ''))
  561. {
  562. $sTag .= "target =\"{$this->a_base['target']}\" ";
  563. }
  564. $sTag .= " />\n";
  565. }
  566. return $sTag;
  567. }
  568. /**
  569. * Get an ID (for any kind of HTML tag) that is guaranteed unique in this page
  570. * @return int The unique ID (in this page)
  571. */
  572. public function GetUniqueId()
  573. {
  574. return $this->iNextId++;
  575. }
  576. /**
  577. * Set the content-type (mime type) for the page's content
  578. * @param $sContentType string
  579. * @return void
  580. */
  581. public function SetContentType($sContentType)
  582. {
  583. $this->sContentType = $sContentType;
  584. }
  585. /**
  586. * Set the content-disposition (mime type) for the page's content
  587. * @param $sDisposition string The disposition: 'inline' or 'attachment'
  588. * @param $sFileName string The original name of the file
  589. * @return void
  590. */
  591. public function SetContentDisposition($sDisposition, $sFileName)
  592. {
  593. $this->sContentDisposition = $sDisposition;
  594. $this->sContentFileName = $sFileName;
  595. }
  596. /**
  597. * Set the transactionId of the current form
  598. * @param $iTransactionId integer
  599. * @return void
  600. */
  601. public function SetTransactionId($iTransactionId)
  602. {
  603. $this->iTransactionId = $iTransactionId;
  604. }
  605. /**
  606. * Returns the transactionId of the current form
  607. * @return integer The current transactionID
  608. */
  609. public function GetTransactionId()
  610. {
  611. return $this->iTransactionId;
  612. }
  613. public static function FilterXSS($sHTML)
  614. {
  615. return str_ireplace('<script', '&lt;script', $sHTML);
  616. }
  617. /**
  618. * What is the currently selected output format
  619. * @return string The selected output format: html, pdf...
  620. */
  621. public function GetOutputFormat()
  622. {
  623. return $this->s_OutputFormat;
  624. }
  625. /**
  626. * Check whether the desired output format is possible or not
  627. * @param string $sOutputFormat The desired output format: html, pdf...
  628. * @return bool True if the format is Ok, false otherwise
  629. */
  630. function IsOutputFormatAvailable($sOutputFormat)
  631. {
  632. $bResult = false;
  633. switch($sOutputFormat)
  634. {
  635. case 'html':
  636. $bResult = true; // Always supported
  637. break;
  638. case 'pdf':
  639. $bResult = @is_readable(APPROOT.'lib/MPDF/mpdf.php');
  640. break;
  641. }
  642. return $bResult;
  643. }
  644. /**
  645. * Check whether the output must be printable (using print.css, for sure!)
  646. * @return bool ...
  647. */
  648. public function IsPrintableVersion()
  649. {
  650. return $this->bPrintable;
  651. }
  652. /**
  653. * Retrieves the value of a named output option for the given format
  654. * @param string $sFormat The format: html or pdf
  655. * @param string $sOptionName The name of the option
  656. * @return mixed false if the option was never set or the options's value
  657. */
  658. public function GetOutputOption($sFormat, $sOptionName)
  659. {
  660. if (isset($this->a_OutputOptions[$sFormat][$sOptionName]))
  661. {
  662. return $this->a_OutputOptions[$sFormat][$sOptionName];
  663. }
  664. return false;
  665. }
  666. /**
  667. * Sets a named output option for the given format
  668. * @param string $sFormat The format for which to set the option: html or pdf
  669. * @param string $sOptionName the name of the option
  670. * @param mixed $sValue The value of the option
  671. */
  672. public function SetOutputOption($sFormat, $sOptionName, $sValue)
  673. {
  674. if (!isset($this->a_OutputOptions[$sFormat]))
  675. {
  676. $this->a_OutputOptions[$sFormat] = array($sOptionName => $sValue);
  677. }
  678. else
  679. {
  680. $this->a_OutputOptions[$sFormat][$sOptionName] = $sValue;
  681. }
  682. }
  683. public function RenderPopupMenuItems($aActions, $aFavoriteActions = array())
  684. {
  685. $sPrevUrl = '';
  686. $sHtml = '';
  687. if (!$this->IsPrintableVersion())
  688. {
  689. foreach ($aActions as $aAction)
  690. {
  691. $sClass = isset($aAction['css_classes']) ? ' class="'.implode(' ', $aAction['css_classes']).'"' : '';
  692. $sOnClick = isset($aAction['onclick']) ? ' onclick="'.htmlspecialchars($aAction['onclick'], ENT_QUOTES, "UTF-8").'"' : '';
  693. $sTarget = isset($aAction['target']) ? " target=\"{$aAction['target']}\"" : "";
  694. if (empty($aAction['url']))
  695. {
  696. if ($sPrevUrl != '') // Don't output consecutively two separators...
  697. {
  698. $sHtml .= "<li>{$aAction['label']}</li>";
  699. }
  700. $sPrevUrl = '';
  701. }
  702. else
  703. {
  704. $sHtml .= "<li><a $sTarget href=\"{$aAction['url']}\"$sClass $sOnClick>{$aAction['label']}</a></li>";
  705. $sPrevUrl = $aAction['url'];
  706. }
  707. }
  708. $sHtml .= "</ul></li></ul></div>";
  709. foreach(array_reverse($aFavoriteActions) as $aAction)
  710. {
  711. $sTarget = isset($aAction['target']) ? " target=\"{$aAction['target']}\"" : "";
  712. $sHtml .= "<div class=\"actions_button\"><a $sTarget href='{$aAction['url']}'>{$aAction['label']}</a></div>";
  713. }
  714. }
  715. return $sHtml;
  716. }
  717. protected function output_dict_entries($bReturnOutput = false)
  718. {
  719. $sHtml = '';
  720. if (count($this->a_dict_entries)>0)
  721. {
  722. $sHtml .= "<script type=\"text/javascript\">\n";
  723. $sHtml .= "var Dict = {};\n";
  724. $sHtml .= "Dict._entries = {};\n";
  725. $sHtml .= "Dict.S = function(sEntry) {\n";
  726. $sHtml .= " if (sEntry in Dict._entries)\n";
  727. $sHtml .= " {\n";
  728. $sHtml .= " return Dict._entries[sEntry];\n";
  729. $sHtml .= " }\n";
  730. $sHtml .= " else\n";
  731. $sHtml .= " {\n";
  732. $sHtml .= " return sEntry;\n";
  733. $sHtml .= " }\n";
  734. $sHtml .= "};\n";
  735. foreach($this->a_dict_entries as $s_entry => $s_value)
  736. {
  737. $sHtml .= "Dict._entries['$s_entry'] = '".addslashes($s_value)."';\n";
  738. }
  739. $sHtml .= "</script>\n";
  740. }
  741. if ($bReturnOutput)
  742. {
  743. return $sHtml;
  744. }
  745. else
  746. {
  747. echo $sHtml;
  748. }
  749. }
  750. }
  751. interface iTabbedPage
  752. {
  753. public function AddTabContainer($sTabContainer, $sPrefix = '');
  754. public function AddToTab($sTabContainer, $sTabLabel, $sHtml);
  755. public function SetCurrentTabContainer($sTabContainer = '');
  756. public function SetCurrentTab($sTabLabel = '');
  757. /**
  758. * Add a tab which content will be loaded asynchronously via the supplied URL
  759. *
  760. * Limitations:
  761. * Cross site scripting is not not allowed for security reasons. Use a normal tab with an IFRAME if you want to pull content from another server.
  762. * Static content cannot be added inside such tabs.
  763. *
  764. * @param string $sTabLabel The (localised) label of the tab
  765. * @param string $sUrl The URL to load (on the same server)
  766. * @param boolean $bCache Whether or not to cache the content of the tab once it has been loaded. flase will cause the tab to be reloaded upon each activation.
  767. * @since 2.0.3
  768. */
  769. public function AddAjaxTab($sTabLabel, $sUrl, $bCache = true);
  770. public function GetCurrentTab();
  771. public function RemoveTab($sTabLabel, $sTabContainer = null);
  772. /**
  773. * Finds the tab whose title matches a given pattern
  774. * @return mixed The name of the tab as a string or false if not found
  775. */
  776. public function FindTab($sPattern, $sTabContainer = null);
  777. }
  778. /**
  779. * Helper class to implement JQueryUI tabs inside a page
  780. */
  781. class TabManager
  782. {
  783. protected $m_aTabs;
  784. protected $m_sCurrentTabContainer;
  785. protected $m_sCurrentTab;
  786. public function __construct()
  787. {
  788. $this->m_aTabs = array();
  789. $this->m_sCurrentTabContainer = '';
  790. $this->m_sCurrentTab = '';
  791. }
  792. public function AddTabContainer($sTabContainer, $sPrefix = '')
  793. {
  794. $this->m_aTabs[$sTabContainer] = array('prefix' => $sPrefix, 'tabs' => array());
  795. return "\$Tabs:$sTabContainer\$";
  796. }
  797. public function AddToCurrentTab($sHtml)
  798. {
  799. $this->AddToTab($this->m_sCurrentTabContainer, $this->m_sCurrentTab, $sHtml);
  800. }
  801. public function GetCurrentTabLength($sHtml)
  802. {
  803. $iLength = isset($this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html']) ? strlen($this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html']): 0;
  804. return $iLength;
  805. }
  806. /**
  807. * Truncates the given tab to the specifed length and returns the truncated part
  808. * @param string $sTabContainer The tab container in which to truncate the tab
  809. * @param string $sTab The name/identifier of the tab to truncate
  810. * @param integer $iLength The length/offset at which to truncate the tab
  811. * @return string The truncated part
  812. */
  813. public function TruncateTab($sTabContainer, $sTab, $iLength)
  814. {
  815. $sResult = substr($this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html'], $iLength);
  816. $this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html'] = substr($this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html'], 0, $iLength);
  817. return $sResult;
  818. }
  819. public function TabExists($sTabContainer, $sTab)
  820. {
  821. return isset($this->m_aTabs[$sTabContainer]['tabs'][$sTab]);
  822. }
  823. public function TabsContainerCount()
  824. {
  825. return count($this->m_aTabs);
  826. }
  827. public function AddToTab($sTabContainer, $sTabLabel, $sHtml)
  828. {
  829. if (!isset($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]))
  830. {
  831. // Set the content of the tab
  832. $this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel] = array(
  833. 'type' => 'html',
  834. 'html' => $sHtml,
  835. );
  836. }
  837. else
  838. {
  839. if ($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]['type'] != 'html')
  840. {
  841. throw new Exception("Cannot add HTML content to the tab '$sTabLabel' of type '{$this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]['type']}'");
  842. }
  843. // Append to the content of the tab
  844. $this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]['html'] .= $sHtml;
  845. }
  846. return ''; // Nothing to add to the page for now
  847. }
  848. public function SetCurrentTabContainer($sTabContainer = '')
  849. {
  850. $sPreviousTabContainer = $this->m_sCurrentTabContainer;
  851. $this->m_sCurrentTabContainer = $sTabContainer;
  852. return $sPreviousTabContainer;
  853. }
  854. public function SetCurrentTab($sTabLabel = '')
  855. {
  856. $sPreviousTab = $this->m_sCurrentTab;
  857. $this->m_sCurrentTab = $sTabLabel;
  858. return $sPreviousTab;
  859. }
  860. /**
  861. * Add a tab which content will be loaded asynchronously via the supplied URL
  862. *
  863. * Limitations:
  864. * Cross site scripting is not not allowed for security reasons. Use a normal tab with an IFRAME if you want to pull content from another server.
  865. * Static content cannot be added inside such tabs.
  866. *
  867. * @param string $sTabLabel The (localised) label of the tab
  868. * @param string $sUrl The URL to load (on the same server)
  869. * @param boolean $bCache Whether or not to cache the content of the tab once it has been loaded. flase will cause the tab to be reloaded upon each activation.
  870. * @since 2.0.3
  871. */
  872. public function AddAjaxTab($sTabLabel, $sUrl, $bCache = true)
  873. {
  874. // Set the content of the tab
  875. $this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$sTabLabel] = array(
  876. 'type' => 'ajax',
  877. 'url' => $sUrl,
  878. 'cache' => $bCache,
  879. );
  880. return ''; // Nothing to add to the page for now
  881. }
  882. public function GetCurrentTabContainer()
  883. {
  884. return $this->m_sCurrentTabContainer;
  885. }
  886. public function GetCurrentTab()
  887. {
  888. return $this->m_sCurrentTab;
  889. }
  890. public function RemoveTab($sTabLabel, $sTabContainer = null)
  891. {
  892. if ($sTabContainer == null)
  893. {
  894. $sTabContainer = $this->m_sCurrentTabContainer;
  895. }
  896. if (isset($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]))
  897. {
  898. // Delete the content of the tab
  899. unset($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]);
  900. // If we just removed the active tab, let's reset the active tab
  901. if (($this->m_sCurrentTabContainer == $sTabContainer) && ($this->m_sCurrentTab == $sTabLabel))
  902. {
  903. $this->m_sCurrentTab = '';
  904. }
  905. }
  906. }
  907. /**
  908. * Finds the tab whose title matches a given pattern
  909. * @return mixed The actual name of the tab (as a string) or false if not found
  910. */
  911. public function FindTab($sPattern, $sTabContainer = null)
  912. {
  913. $result = false;
  914. if ($sTabContainer == null)
  915. {
  916. $sTabContainer = $this->m_sCurrentTabContainer;
  917. }
  918. foreach($this->m_aTabs[$sTabContainer]['tabs'] as $sTabLabel => $void)
  919. {
  920. if (preg_match($sPattern, $sTabLabel))
  921. {
  922. $result = $sTabLabel;
  923. break;
  924. }
  925. }
  926. return $result;
  927. }
  928. /**
  929. * Make the given tab the active one, as if it were clicked
  930. * DOES NOT WORK: apparently in the *old* version of jquery
  931. * that we are using this is not supported... TO DO upgrade
  932. * the whole jquery bundle...
  933. */
  934. public function SelectTab($sTabContainer, $sTabLabel)
  935. {
  936. $container_index = 0;
  937. $tab_index = 0;
  938. foreach($this->m_aTabs as $sCurrentTabContainerName => $aTabs)
  939. {
  940. if ($sTabContainer == $sCurrentTabContainerName)
  941. {
  942. foreach($aTabs['tabs'] as $sCurrentTabLabel => $void)
  943. {
  944. if ($sCurrentTabLabel == $sTabLabel)
  945. {
  946. break;
  947. }
  948. $tab_index++;
  949. }
  950. break;
  951. }
  952. $container_index++;
  953. }
  954. $sSelector = '#tabbedContent_'.$container_index.' > ul';
  955. return "window.setTimeout(\"$('$sSelector').tabs('select', $tab_index);\", 100);"; // Let the time to the tabs widget to initialize
  956. }
  957. public function RenderIntoContent($sContent, WebPage $oPage)
  958. {
  959. // Render the tabs in the page (if any)
  960. foreach($this->m_aTabs as $sTabContainerName => $aTabs)
  961. {
  962. $sTabs = '';
  963. $sPrefix = $aTabs['prefix'];
  964. $container_index = 0;
  965. if (count($aTabs['tabs']) > 0)
  966. {
  967. if ($oPage->IsPrintableVersion())
  968. {
  969. $oPage->add_ready_script(
  970. <<< EOF
  971. oHiddeableChapters = {};
  972. EOF
  973. );
  974. $sTabs = "<!-- tabs -->\n<div id=\"tabbedContent_{$sPrefix}{$container_index}\" class=\"light\">\n";
  975. $i = 0;
  976. foreach($aTabs['tabs'] as $sTabName => $aTabData)
  977. {
  978. $sTabNameEsc = addslashes($sTabName);
  979. $sTabId = "tab_{$sPrefix}{$container_index}$i";
  980. switch($aTabData['type'])
  981. {
  982. case 'ajax':
  983. $sTabHtml = '';
  984. $sUrl = $aTabData['url'];
  985. $oPage->add_ready_script(
  986. <<< EOF
  987. $.post('$sUrl', {printable: '1'}, function(data){
  988. $('#$sTabId > .printable-tab-content').append(data);
  989. });
  990. EOF
  991. );
  992. break;
  993. case 'html':
  994. default:
  995. $sTabHtml = $aTabData['html'];
  996. }
  997. $sTabs .= "<div class=\"printable-tab\" id=\"$sTabId\"><h2 class=\"printable-tab-title\">".htmlentities($sTabName, ENT_QUOTES, 'UTF-8')."</h2><div class=\"printable-tab-content\">".$sTabHtml."</div></div>\n";
  998. $oPage->add_ready_script(
  999. <<< EOF
  1000. oHiddeableChapters['$sTabId'] = '$sTabNameEsc';
  1001. EOF
  1002. );
  1003. $i++;
  1004. }
  1005. $sTabs .= "</div>\n<!-- end of tabs-->\n";
  1006. }
  1007. else
  1008. {
  1009. $sTabs = "<!-- tabs -->\n<div id=\"tabbedContent_{$sPrefix}{$container_index}\" class=\"light\">\n";
  1010. $sTabs .= "<ul>\n";
  1011. // Display the unordered list that will be rendered as the tabs
  1012. $i = 0;
  1013. foreach($aTabs['tabs'] as $sTabName => $aTabData)
  1014. {
  1015. switch($aTabData['type'])
  1016. {
  1017. case 'ajax':
  1018. $sTabs .= "<li data-cache=\"".($aTabData['cache'] ? 'true' : 'false')."\"><a href=\"{$aTabData['url']}\" class=\"tab\"><span>".htmlentities($sTabName, ENT_QUOTES, 'UTF-8')."</span></a></li>\n";
  1019. break;
  1020. case 'html':
  1021. default:
  1022. $sTabs .= "<li><a href=\"#tab_{$sPrefix}{$container_index}$i\" class=\"tab\"><span>".htmlentities($sTabName, ENT_QUOTES, 'UTF-8')."</span></a></li>\n";
  1023. }
  1024. $i++;
  1025. }
  1026. $sTabs .= "</ul>\n";
  1027. // Now add the content of the tabs themselves
  1028. $i = 0;
  1029. foreach($aTabs['tabs'] as $sTabName => $aTabData)
  1030. {
  1031. switch($aTabData['type'])
  1032. {
  1033. case 'ajax':
  1034. // Nothing to add
  1035. break;
  1036. case 'html':
  1037. default:
  1038. $sTabs .= "<div id=\"tab_{$sPrefix}{$container_index}$i\">".$aTabData['html']."</div>\n";
  1039. }
  1040. $i++;
  1041. }
  1042. $sTabs .= "</div>\n<!-- end of tabs-->\n";
  1043. }
  1044. }
  1045. $sContent = str_replace("\$Tabs:$sTabContainerName\$", $sTabs, $sContent);
  1046. $container_index++;
  1047. }
  1048. return $sContent;
  1049. }
  1050. }