webpage.class.inc.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. $sDataAttCode = isset($aAttrib['attcode']) ? "data-attcode=\"{$aAttrib['attcode']}\"" : '';
  318. $sLayout = isset($aAttrib['layout']) ? $aAttrib['layout'] : 'small';
  319. $sHtml .= "<div class=\"field_container field_{$sLayout}\" $sDataAttCode>\n";
  320. $sHtml .= "<div class=\"field_label label\">{$aAttrib['label']}</div>\n";
  321. $sHtml .= "<div class=\"field_data\">\n";
  322. // By Rom, for csv import, proposed to show several values for column selection
  323. if (is_array($aAttrib['value']))
  324. {
  325. $sHtml .= "<div class=\"field_value\">".implode("</div><div>", $aAttrib['value'])."</div>\n";
  326. }
  327. else
  328. {
  329. $sHtml .= "<div class=\"field_value\">".$aAttrib['value']."</div>\n";
  330. }
  331. // Checking if we should add comments & infos
  332. $sComment = (isset($aAttrib['comments'])) ? $aAttrib['comments'] : '';
  333. $sInfo = (isset($aAttrib['infos'])) ? $aAttrib['infos'] : '';
  334. if($sComment !== '')
  335. {
  336. $sHtml .= "<div class=\"field_comments\">$sComment</div>\n";
  337. }
  338. if($sInfo !== '')
  339. {
  340. $sHtml .= "<div class=\"field_infos\">$sInfo</div>\n";
  341. }
  342. $sHtml .= "</div>\n";
  343. $sHtml .= "</div>\n";
  344. }
  345. $sHtml .= "</div>\n";
  346. return $sHtml;
  347. }
  348. /**
  349. * Build a set of radio buttons suitable for editing a field/attribute of an object (including its validation)
  350. * @param $aAllowedValues hash Array of value => display_value
  351. * @param $value mixed Current value for the field/attribute
  352. * @param $iId mixed Unique Id for the input control in the page
  353. * @param $sFieldName string The name of the field, attr_<$sFieldName> will hold the value for the field
  354. * @param $bMandatory bool Whether or not the field is mandatory
  355. * @param $bVertical bool Disposition of the radio buttons vertical or horizontal
  356. * @param $sValidationField string HTML fragment holding the validation field (exclamation icon...)
  357. * @return string The HTML fragment corresponding to the radio buttons
  358. */
  359. public function GetRadioButtons($aAllowedValues, $value, $iId, $sFieldName, $bMandatory, $bVertical, $sValidationField)
  360. {
  361. $idx = 0;
  362. $sHTMLValue = '';
  363. foreach($aAllowedValues as $key => $display_value)
  364. {
  365. if ((count($aAllowedValues) == 1) && ($bMandatory == 'true') )
  366. {
  367. // When there is only once choice, select it by default
  368. $sSelected = ' checked';
  369. }
  370. else
  371. {
  372. $sSelected = ($value == $key) ? ' checked' : '';
  373. }
  374. $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;";
  375. if ($bVertical)
  376. {
  377. if ($idx == 0)
  378. {
  379. // Validation icon at the end of the first line
  380. $sHTMLValue .= "&nbsp;{$sValidationField}\n";
  381. }
  382. $sHTMLValue .= "<br>\n";
  383. }
  384. $idx++;
  385. }
  386. $sHTMLValue .= "<input type=\"hidden\" id=\"$iId\" name=\"$sFieldName\" value=\"$value\"/>";
  387. if (!$bVertical)
  388. {
  389. // Validation icon at the end of the line
  390. $sHTMLValue .= "&nbsp;{$sValidationField}\n";
  391. }
  392. return $sHTMLValue;
  393. }
  394. /**
  395. * Discard unexpected output data (such as PHP warnings)
  396. * This is a MUST when the Page output is DATA (download of a document, download CSV export, download ...)
  397. */
  398. public function TrashUnexpectedOutput()
  399. {
  400. $this->bTrashUnexpectedOutput = true;
  401. }
  402. /**
  403. * Read the output buffer and deal with its contents:
  404. * - trash unexpected output if the flag has been set
  405. * - report unexpected behaviors such as the output buffering being stopped
  406. *
  407. * Possible improvement: I've noticed that several output buffers are stacked,
  408. * if they are not empty, the output will be corrupted. The solution would
  409. * consist in unstacking all of them (and concatenate the contents).
  410. */
  411. protected function ob_get_clean_safe()
  412. {
  413. $sOutput = ob_get_contents();
  414. if ($sOutput === false)
  415. {
  416. $sMsg = "Design/integration issue: No output buffer. Some piece of code has called ob_get_clean() or ob_end_clean() without calling ob_start()";
  417. if ($this->bTrashUnexpectedOutput)
  418. {
  419. IssueLog::Error($sMsg);
  420. $sOutput = '';
  421. }
  422. else
  423. {
  424. $sOutput = $sMsg;
  425. }
  426. }
  427. else
  428. {
  429. ob_end_clean(); // on some versions of PHP doing so when the output buffering is stopped can cause a notice
  430. if ($this->bTrashUnexpectedOutput)
  431. {
  432. if (trim($sOutput) != '')
  433. {
  434. if (Utils::GetConfig() && Utils::GetConfig()->Get('debug_report_spurious_chars'))
  435. {
  436. IssueLog::Error("Trashing unexpected output:'$sOutput'\n");
  437. }
  438. }
  439. $sOutput = '';
  440. }
  441. }
  442. return $sOutput;
  443. }
  444. /**
  445. * Outputs (via some echo) the complete HTML page by assembling all its elements
  446. */
  447. public function output()
  448. {
  449. foreach($this->a_headers as $s_header)
  450. {
  451. header($s_header);
  452. }
  453. $s_captured_output = $this->ob_get_clean_safe();
  454. echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
  455. echo "<html>\n";
  456. echo "<head>\n";
  457. echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
  458. echo "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, shrink-to-fit=no\" />";
  459. echo "<title>".htmlentities($this->s_title, ENT_QUOTES, 'UTF-8')."</title>\n";
  460. echo $this->get_base_tag();
  461. foreach($this->a_linked_scripts as $s_script)
  462. {
  463. // Make sure that the URL to the script contains the application's version number
  464. // so that the new script do NOT get reloaded from the cache when the application is upgraded
  465. if (strpos($s_script, '?') === false)
  466. {
  467. $s_script .= "?itopversion=".ITOP_VERSION;
  468. }
  469. else
  470. {
  471. $s_script .= "&itopversion=".ITOP_VERSION;
  472. }
  473. echo "<script type=\"text/javascript\" src=\"$s_script\"></script>\n";
  474. }
  475. if (count($this->a_scripts)>0)
  476. {
  477. echo "<script type=\"text/javascript\">\n";
  478. foreach($this->a_scripts as $s_script)
  479. {
  480. echo "$s_script\n";
  481. }
  482. echo "</script>\n";
  483. }
  484. $this->output_dict_entries();
  485. foreach($this->a_linked_stylesheets as $a_stylesheet)
  486. {
  487. if (strpos($a_stylesheet['link'], '?') === false)
  488. {
  489. $s_stylesheet = $a_stylesheet['link']."?itopversion=".ITOP_VERSION;
  490. }
  491. else
  492. {
  493. $s_stylesheet = $a_stylesheet['link']."&itopversion=".ITOP_VERSION;
  494. }
  495. if ($a_stylesheet['condition'] != "")
  496. {
  497. echo "<!--[if {$a_stylesheet['condition']}]>\n";
  498. }
  499. echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$s_stylesheet}\" />\n";
  500. if ($a_stylesheet['condition'] != "")
  501. {
  502. echo "<![endif]-->\n";
  503. }
  504. }
  505. if (count($this->a_styles)>0)
  506. {
  507. echo "<style>\n";
  508. foreach($this->a_styles as $s_style)
  509. {
  510. echo "$s_style\n";
  511. }
  512. echo "</style>\n";
  513. }
  514. if (class_exists('MetaModel') && MetaModel::GetConfig())
  515. {
  516. echo "<link rel=\"shortcut icon\" href=\"".utils::GetAbsoluteUrlAppRoot()."images/favicon.ico?itopversion=".ITOP_VERSION."\" />\n";
  517. }
  518. echo "</head>\n";
  519. echo "<body>\n";
  520. echo self::FilterXSS($this->s_content);
  521. if (trim($s_captured_output) != "")
  522. {
  523. echo "<div class=\"raw_output\">".self::FilterXSS($s_captured_output)."</div>\n";
  524. }
  525. echo '<div id="at_the_end">'.self::FilterXSS($this->s_deferred_content).'</div>';
  526. echo "</body>\n";
  527. echo "</html>\n";
  528. if (class_exists('DBSearch'))
  529. {
  530. DBSearch::RecordQueryTrace();
  531. }
  532. if (class_exists('ExecutionKPI'))
  533. {
  534. ExecutionKPI::ReportStats();
  535. }
  536. }
  537. /**
  538. * Build a series of hidden field[s] from an array
  539. */
  540. public function add_input_hidden($sLabel, $aData)
  541. {
  542. foreach($aData as $sKey => $sValue)
  543. {
  544. // Note: protection added to protect against the Notice 'array to string conversion' that appeared with PHP 5.4
  545. // (this function seems unused though!)
  546. if (is_scalar($sValue))
  547. {
  548. $this->add("<input type=\"hidden\" name=\"".$sLabel."[$sKey]\" value=\"$sValue\">");
  549. }
  550. }
  551. }
  552. protected function get_base_tag()
  553. {
  554. $sTag = '';
  555. if (($this->a_base['href'] != '') || ($this->a_base['target'] != ''))
  556. {
  557. $sTag = '<base ';
  558. if (($this->a_base['href'] != ''))
  559. {
  560. $sTag .= "href =\"{$this->a_base['href']}\" ";
  561. }
  562. if (($this->a_base['target'] != ''))
  563. {
  564. $sTag .= "target =\"{$this->a_base['target']}\" ";
  565. }
  566. $sTag .= " />\n";
  567. }
  568. return $sTag;
  569. }
  570. /**
  571. * Get an ID (for any kind of HTML tag) that is guaranteed unique in this page
  572. * @return int The unique ID (in this page)
  573. */
  574. public function GetUniqueId()
  575. {
  576. return $this->iNextId++;
  577. }
  578. /**
  579. * Set the content-type (mime type) for the page's content
  580. * @param $sContentType string
  581. * @return void
  582. */
  583. public function SetContentType($sContentType)
  584. {
  585. $this->sContentType = $sContentType;
  586. }
  587. /**
  588. * Set the content-disposition (mime type) for the page's content
  589. * @param $sDisposition string The disposition: 'inline' or 'attachment'
  590. * @param $sFileName string The original name of the file
  591. * @return void
  592. */
  593. public function SetContentDisposition($sDisposition, $sFileName)
  594. {
  595. $this->sContentDisposition = $sDisposition;
  596. $this->sContentFileName = $sFileName;
  597. }
  598. /**
  599. * Set the transactionId of the current form
  600. * @param $iTransactionId integer
  601. * @return void
  602. */
  603. public function SetTransactionId($iTransactionId)
  604. {
  605. $this->iTransactionId = $iTransactionId;
  606. }
  607. /**
  608. * Returns the transactionId of the current form
  609. * @return integer The current transactionID
  610. */
  611. public function GetTransactionId()
  612. {
  613. return $this->iTransactionId;
  614. }
  615. public static function FilterXSS($sHTML)
  616. {
  617. return str_ireplace('<script', '&lt;script', $sHTML);
  618. }
  619. /**
  620. * What is the currently selected output format
  621. * @return string The selected output format: html, pdf...
  622. */
  623. public function GetOutputFormat()
  624. {
  625. return $this->s_OutputFormat;
  626. }
  627. /**
  628. * Check whether the desired output format is possible or not
  629. * @param string $sOutputFormat The desired output format: html, pdf...
  630. * @return bool True if the format is Ok, false otherwise
  631. */
  632. function IsOutputFormatAvailable($sOutputFormat)
  633. {
  634. $bResult = false;
  635. switch($sOutputFormat)
  636. {
  637. case 'html':
  638. $bResult = true; // Always supported
  639. break;
  640. case 'pdf':
  641. $bResult = @is_readable(APPROOT.'lib/MPDF/mpdf.php');
  642. break;
  643. }
  644. return $bResult;
  645. }
  646. /**
  647. * Check whether the output must be printable (using print.css, for sure!)
  648. * @return bool ...
  649. */
  650. public function IsPrintableVersion()
  651. {
  652. return $this->bPrintable;
  653. }
  654. /**
  655. * Retrieves the value of a named output option for the given format
  656. * @param string $sFormat The format: html or pdf
  657. * @param string $sOptionName The name of the option
  658. * @return mixed false if the option was never set or the options's value
  659. */
  660. public function GetOutputOption($sFormat, $sOptionName)
  661. {
  662. if (isset($this->a_OutputOptions[$sFormat][$sOptionName]))
  663. {
  664. return $this->a_OutputOptions[$sFormat][$sOptionName];
  665. }
  666. return false;
  667. }
  668. /**
  669. * Sets a named output option for the given format
  670. * @param string $sFormat The format for which to set the option: html or pdf
  671. * @param string $sOptionName the name of the option
  672. * @param mixed $sValue The value of the option
  673. */
  674. public function SetOutputOption($sFormat, $sOptionName, $sValue)
  675. {
  676. if (!isset($this->a_OutputOptions[$sFormat]))
  677. {
  678. $this->a_OutputOptions[$sFormat] = array($sOptionName => $sValue);
  679. }
  680. else
  681. {
  682. $this->a_OutputOptions[$sFormat][$sOptionName] = $sValue;
  683. }
  684. }
  685. public function RenderPopupMenuItems($aActions, $aFavoriteActions = array())
  686. {
  687. $sPrevUrl = '';
  688. $sHtml = '';
  689. if (!$this->IsPrintableVersion())
  690. {
  691. foreach ($aActions as $aAction)
  692. {
  693. $sClass = isset($aAction['css_classes']) ? ' class="'.implode(' ', $aAction['css_classes']).'"' : '';
  694. $sOnClick = isset($aAction['onclick']) ? ' onclick="'.htmlspecialchars($aAction['onclick'], ENT_QUOTES, "UTF-8").'"' : '';
  695. $sTarget = isset($aAction['target']) ? " target=\"{$aAction['target']}\"" : "";
  696. if (empty($aAction['url']))
  697. {
  698. if ($sPrevUrl != '') // Don't output consecutively two separators...
  699. {
  700. $sHtml .= "<li>{$aAction['label']}</li>";
  701. }
  702. $sPrevUrl = '';
  703. }
  704. else
  705. {
  706. $sHtml .= "<li><a $sTarget href=\"{$aAction['url']}\"$sClass $sOnClick>{$aAction['label']}</a></li>";
  707. $sPrevUrl = $aAction['url'];
  708. }
  709. }
  710. $sHtml .= "</ul></li></ul></div>";
  711. foreach(array_reverse($aFavoriteActions) as $aAction)
  712. {
  713. $sTarget = isset($aAction['target']) ? " target=\"{$aAction['target']}\"" : "";
  714. $sHtml .= "<div class=\"actions_button\"><a $sTarget href='{$aAction['url']}'>{$aAction['label']}</a></div>";
  715. }
  716. }
  717. return $sHtml;
  718. }
  719. protected function output_dict_entries($bReturnOutput = false)
  720. {
  721. $sHtml = '';
  722. if (count($this->a_dict_entries)>0)
  723. {
  724. $sHtml .= "<script type=\"text/javascript\">\n";
  725. $sHtml .= "var Dict = {};\n";
  726. $sHtml .= "Dict._entries = {};\n";
  727. $sHtml .= "Dict.S = function(sEntry) {\n";
  728. $sHtml .= " if (sEntry in Dict._entries)\n";
  729. $sHtml .= " {\n";
  730. $sHtml .= " return Dict._entries[sEntry];\n";
  731. $sHtml .= " }\n";
  732. $sHtml .= " else\n";
  733. $sHtml .= " {\n";
  734. $sHtml .= " return sEntry;\n";
  735. $sHtml .= " }\n";
  736. $sHtml .= "};\n";
  737. foreach($this->a_dict_entries as $s_entry => $s_value)
  738. {
  739. $sHtml .= "Dict._entries['$s_entry'] = '".addslashes($s_value)."';\n";
  740. }
  741. $sHtml .= "</script>\n";
  742. }
  743. if ($bReturnOutput)
  744. {
  745. return $sHtml;
  746. }
  747. else
  748. {
  749. echo $sHtml;
  750. }
  751. }
  752. }
  753. interface iTabbedPage
  754. {
  755. public function AddTabContainer($sTabContainer, $sPrefix = '');
  756. public function AddToTab($sTabContainer, $sTabLabel, $sHtml);
  757. public function SetCurrentTabContainer($sTabContainer = '');
  758. public function SetCurrentTab($sTabLabel = '');
  759. /**
  760. * Add a tab which content will be loaded asynchronously via the supplied URL
  761. *
  762. * Limitations:
  763. * 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.
  764. * Static content cannot be added inside such tabs.
  765. *
  766. * @param string $sTabLabel The (localised) label of the tab
  767. * @param string $sUrl The URL to load (on the same server)
  768. * @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.
  769. * @since 2.0.3
  770. */
  771. public function AddAjaxTab($sTabLabel, $sUrl, $bCache = true);
  772. public function GetCurrentTab();
  773. public function RemoveTab($sTabLabel, $sTabContainer = null);
  774. /**
  775. * Finds the tab whose title matches a given pattern
  776. * @return mixed The name of the tab as a string or false if not found
  777. */
  778. public function FindTab($sPattern, $sTabContainer = null);
  779. }
  780. /**
  781. * Helper class to implement JQueryUI tabs inside a page
  782. */
  783. class TabManager
  784. {
  785. protected $m_aTabs;
  786. protected $m_sCurrentTabContainer;
  787. protected $m_sCurrentTab;
  788. public function __construct()
  789. {
  790. $this->m_aTabs = array();
  791. $this->m_sCurrentTabContainer = '';
  792. $this->m_sCurrentTab = '';
  793. }
  794. public function AddTabContainer($sTabContainer, $sPrefix = '')
  795. {
  796. $this->m_aTabs[$sTabContainer] = array('prefix' => $sPrefix, 'tabs' => array());
  797. return "\$Tabs:$sTabContainer\$";
  798. }
  799. public function AddToCurrentTab($sHtml)
  800. {
  801. $this->AddToTab($this->m_sCurrentTabContainer, $this->m_sCurrentTab, $sHtml);
  802. }
  803. public function GetCurrentTabLength($sHtml)
  804. {
  805. $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;
  806. return $iLength;
  807. }
  808. /**
  809. * Truncates the given tab to the specifed length and returns the truncated part
  810. * @param string $sTabContainer The tab container in which to truncate the tab
  811. * @param string $sTab The name/identifier of the tab to truncate
  812. * @param integer $iLength The length/offset at which to truncate the tab
  813. * @return string The truncated part
  814. */
  815. public function TruncateTab($sTabContainer, $sTab, $iLength)
  816. {
  817. $sResult = substr($this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html'], $iLength);
  818. $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);
  819. return $sResult;
  820. }
  821. public function TabExists($sTabContainer, $sTab)
  822. {
  823. return isset($this->m_aTabs[$sTabContainer]['tabs'][$sTab]);
  824. }
  825. public function TabsContainerCount()
  826. {
  827. return count($this->m_aTabs);
  828. }
  829. public function AddToTab($sTabContainer, $sTabLabel, $sHtml)
  830. {
  831. if (!isset($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]))
  832. {
  833. // Set the content of the tab
  834. $this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel] = array(
  835. 'type' => 'html',
  836. 'html' => $sHtml,
  837. );
  838. }
  839. else
  840. {
  841. if ($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]['type'] != 'html')
  842. {
  843. throw new Exception("Cannot add HTML content to the tab '$sTabLabel' of type '{$this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]['type']}'");
  844. }
  845. // Append to the content of the tab
  846. $this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]['html'] .= $sHtml;
  847. }
  848. return ''; // Nothing to add to the page for now
  849. }
  850. public function SetCurrentTabContainer($sTabContainer = '')
  851. {
  852. $sPreviousTabContainer = $this->m_sCurrentTabContainer;
  853. $this->m_sCurrentTabContainer = $sTabContainer;
  854. return $sPreviousTabContainer;
  855. }
  856. public function SetCurrentTab($sTabLabel = '')
  857. {
  858. $sPreviousTab = $this->m_sCurrentTab;
  859. $this->m_sCurrentTab = $sTabLabel;
  860. return $sPreviousTab;
  861. }
  862. /**
  863. * Add a tab which content will be loaded asynchronously via the supplied URL
  864. *
  865. * Limitations:
  866. * 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.
  867. * Static content cannot be added inside such tabs.
  868. *
  869. * @param string $sTabLabel The (localised) label of the tab
  870. * @param string $sUrl The URL to load (on the same server)
  871. * @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.
  872. * @since 2.0.3
  873. */
  874. public function AddAjaxTab($sTabLabel, $sUrl, $bCache = true)
  875. {
  876. // Set the content of the tab
  877. $this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$sTabLabel] = array(
  878. 'type' => 'ajax',
  879. 'url' => $sUrl,
  880. 'cache' => $bCache,
  881. );
  882. return ''; // Nothing to add to the page for now
  883. }
  884. public function GetCurrentTabContainer()
  885. {
  886. return $this->m_sCurrentTabContainer;
  887. }
  888. public function GetCurrentTab()
  889. {
  890. return $this->m_sCurrentTab;
  891. }
  892. public function RemoveTab($sTabLabel, $sTabContainer = null)
  893. {
  894. if ($sTabContainer == null)
  895. {
  896. $sTabContainer = $this->m_sCurrentTabContainer;
  897. }
  898. if (isset($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]))
  899. {
  900. // Delete the content of the tab
  901. unset($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]);
  902. // If we just removed the active tab, let's reset the active tab
  903. if (($this->m_sCurrentTabContainer == $sTabContainer) && ($this->m_sCurrentTab == $sTabLabel))
  904. {
  905. $this->m_sCurrentTab = '';
  906. }
  907. }
  908. }
  909. /**
  910. * Finds the tab whose title matches a given pattern
  911. * @return mixed The actual name of the tab (as a string) or false if not found
  912. */
  913. public function FindTab($sPattern, $sTabContainer = null)
  914. {
  915. $result = false;
  916. if ($sTabContainer == null)
  917. {
  918. $sTabContainer = $this->m_sCurrentTabContainer;
  919. }
  920. foreach($this->m_aTabs[$sTabContainer]['tabs'] as $sTabLabel => $void)
  921. {
  922. if (preg_match($sPattern, $sTabLabel))
  923. {
  924. $result = $sTabLabel;
  925. break;
  926. }
  927. }
  928. return $result;
  929. }
  930. /**
  931. * Make the given tab the active one, as if it were clicked
  932. * DOES NOT WORK: apparently in the *old* version of jquery
  933. * that we are using this is not supported... TO DO upgrade
  934. * the whole jquery bundle...
  935. */
  936. public function SelectTab($sTabContainer, $sTabLabel)
  937. {
  938. $container_index = 0;
  939. $tab_index = 0;
  940. foreach($this->m_aTabs as $sCurrentTabContainerName => $aTabs)
  941. {
  942. if ($sTabContainer == $sCurrentTabContainerName)
  943. {
  944. foreach($aTabs['tabs'] as $sCurrentTabLabel => $void)
  945. {
  946. if ($sCurrentTabLabel == $sTabLabel)
  947. {
  948. break;
  949. }
  950. $tab_index++;
  951. }
  952. break;
  953. }
  954. $container_index++;
  955. }
  956. $sSelector = '#tabbedContent_'.$container_index.' > ul';
  957. return "window.setTimeout(\"$('$sSelector').tabs('select', $tab_index);\", 100);"; // Let the time to the tabs widget to initialize
  958. }
  959. public function RenderIntoContent($sContent, WebPage $oPage)
  960. {
  961. // Render the tabs in the page (if any)
  962. foreach($this->m_aTabs as $sTabContainerName => $aTabs)
  963. {
  964. $sTabs = '';
  965. $sPrefix = $aTabs['prefix'];
  966. $container_index = 0;
  967. if (count($aTabs['tabs']) > 0)
  968. {
  969. if ($oPage->IsPrintableVersion())
  970. {
  971. $oPage->add_ready_script(
  972. <<< EOF
  973. oHiddeableChapters = {};
  974. EOF
  975. );
  976. $sTabs = "<!-- tabs -->\n<div id=\"tabbedContent_{$sPrefix}{$container_index}\" class=\"light\">\n";
  977. $i = 0;
  978. foreach($aTabs['tabs'] as $sTabName => $aTabData)
  979. {
  980. $sTabNameEsc = addslashes($sTabName);
  981. $sTabId = "tab_{$sPrefix}{$container_index}$i";
  982. switch($aTabData['type'])
  983. {
  984. case 'ajax':
  985. $sTabHtml = '';
  986. $sUrl = $aTabData['url'];
  987. $oPage->add_ready_script(
  988. <<< EOF
  989. $.post('$sUrl', {printable: '1'}, function(data){
  990. $('#$sTabId > .printable-tab-content').append(data);
  991. });
  992. EOF
  993. );
  994. break;
  995. case 'html':
  996. default:
  997. $sTabHtml = $aTabData['html'];
  998. }
  999. $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";
  1000. $oPage->add_ready_script(
  1001. <<< EOF
  1002. oHiddeableChapters['$sTabId'] = '$sTabNameEsc';
  1003. EOF
  1004. );
  1005. $i++;
  1006. }
  1007. $sTabs .= "</div>\n<!-- end of tabs-->\n";
  1008. }
  1009. else
  1010. {
  1011. $sTabs = "<!-- tabs -->\n<div id=\"tabbedContent_{$sPrefix}{$container_index}\" class=\"light\">\n";
  1012. $sTabs .= "<ul>\n";
  1013. // Display the unordered list that will be rendered as the tabs
  1014. $i = 0;
  1015. foreach($aTabs['tabs'] as $sTabName => $aTabData)
  1016. {
  1017. switch($aTabData['type'])
  1018. {
  1019. case 'ajax':
  1020. $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";
  1021. break;
  1022. case 'html':
  1023. default:
  1024. $sTabs .= "<li><a href=\"#tab_{$sPrefix}{$container_index}$i\" class=\"tab\"><span>".htmlentities($sTabName, ENT_QUOTES, 'UTF-8')."</span></a></li>\n";
  1025. }
  1026. $i++;
  1027. }
  1028. $sTabs .= "</ul>\n";
  1029. // Now add the content of the tabs themselves
  1030. $i = 0;
  1031. foreach($aTabs['tabs'] as $sTabName => $aTabData)
  1032. {
  1033. switch($aTabData['type'])
  1034. {
  1035. case 'ajax':
  1036. // Nothing to add
  1037. break;
  1038. case 'html':
  1039. default:
  1040. $sTabs .= "<div id=\"tab_{$sPrefix}{$container_index}$i\">".$aTabData['html']."</div>\n";
  1041. }
  1042. $i++;
  1043. }
  1044. $sTabs .= "</div>\n<!-- end of tabs-->\n";
  1045. }
  1046. }
  1047. $sContent = str_replace("\$Tabs:$sTabContainerName\$", $sTabs, $sContent);
  1048. $container_index++;
  1049. }
  1050. return $sContent;
  1051. }
  1052. }