ajaxwebpage.class.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. // Copyright (C) 2010-2017 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. * Simple web page with no includes, header or fancy formatting, useful to
  20. * generate HTML fragments when called by an AJAX method
  21. *
  22. * @copyright Copyright (C) 2010-2017 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. require_once(APPROOT."/application/webpage.class.inc.php");
  26. class ajax_page extends WebPage implements iTabbedPage
  27. {
  28. /**
  29. * Jquery style ready script
  30. * @var Hash
  31. */
  32. protected $m_sReadyScript;
  33. protected $m_oTabs;
  34. private $m_sMenu; // If set, then the menu will be updated
  35. /**
  36. * constructor for the web page
  37. * @param string $s_title Not used
  38. */
  39. function __construct($s_title)
  40. {
  41. $sPrintable = utils::ReadParam('printable', '0');
  42. $bPrintable = ($sPrintable == '1');
  43. parent::__construct($s_title, $bPrintable);
  44. $this->m_sReadyScript = "";
  45. //$this->add_header("Content-type: text/html; charset=utf-8");
  46. $this->add_header("Cache-control: no-cache");
  47. $this->m_oTabs = new TabManager();
  48. $this->sContentType = 'text/html';
  49. $this->sContentDisposition = 'inline';
  50. $this->m_sMenu = "";
  51. $bArchiveMode = utils::IsArchiveMode();
  52. DBSearch::SetArchiveModeDefault($bArchiveMode);
  53. if ($bArchiveMode) MetaModel::DBSetReadOnly();
  54. }
  55. public function AddTabContainer($sTabContainer, $sPrefix = '')
  56. {
  57. $this->add($this->m_oTabs->AddTabContainer($sTabContainer, $sPrefix));
  58. }
  59. public function AddToTab($sTabContainer, $sTabLabel, $sHtml)
  60. {
  61. $this->add($this->m_oTabs->AddToTab($sTabContainer, $sTabLabel, $sHtml));
  62. }
  63. public function SetCurrentTabContainer($sTabContainer = '')
  64. {
  65. return $this->m_oTabs->SetCurrentTabContainer($sTabContainer);
  66. }
  67. public function SetCurrentTab($sTabLabel = '')
  68. {
  69. return $this->m_oTabs->SetCurrentTab($sTabLabel);
  70. }
  71. /**
  72. * Add a tab which content will be loaded asynchronously via the supplied URL
  73. *
  74. * Limitations:
  75. * 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.
  76. * Static content cannot be added inside such tabs.
  77. *
  78. * @param string $sTabLabel The (localised) label of the tab
  79. * @param string $sUrl The URL to load (on the same server)
  80. * @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.
  81. * @since 2.0.3
  82. */
  83. public function AddAjaxTab($sTabLabel, $sUrl, $bCache = true)
  84. {
  85. $this->add($this->m_oTabs->AddAjaxTab($sTabLabel, $sUrl, $bCache));
  86. }
  87. public function GetCurrentTab()
  88. {
  89. return $this->m_oTabs->GetCurrentTab();
  90. }
  91. public function RemoveTab($sTabLabel, $sTabContainer = null)
  92. {
  93. $this->m_oTabs->RemoveTab($sTabLabel, $sTabContainer);
  94. }
  95. /**
  96. * Finds the tab whose title matches a given pattern
  97. * @return mixed The name of the tab as a string or false if not found
  98. */
  99. public function FindTab($sPattern, $sTabContainer = null)
  100. {
  101. return $this->m_oTabs->FindTab($sPattern, $sTabContainer);
  102. }
  103. /**
  104. * Make the given tab the active one, as if it were clicked
  105. * DOES NOT WORK: apparently in the *old* version of jquery
  106. * that we are using this is not supported... TO DO upgrade
  107. * the whole jquery bundle...
  108. */
  109. public function SelectTab($sTabContainer, $sTabLabel)
  110. {
  111. $this->add_ready_script($this->m_oTabs->SelectTab($sTabContainer, $sTabLabel));
  112. }
  113. public function AddToMenu($sHtml)
  114. {
  115. $this->m_sMenu .= $sHtml;
  116. }
  117. /**
  118. * Echoes the content of the whole page
  119. * @return void
  120. */
  121. public function output()
  122. {
  123. if (!empty($this->sContentType))
  124. {
  125. $this->add_header('Content-type: '.$this->sContentType);
  126. }
  127. if (!empty($this->sContentDisposition))
  128. {
  129. $this->add_header('Content-Disposition: '.$this->sContentDisposition.'; filename="'.$this->sContentFileName.'"');
  130. }
  131. foreach($this->a_headers as $s_header)
  132. {
  133. header($s_header);
  134. }
  135. if ($this->m_oTabs->TabsContainerCount() > 0)
  136. {
  137. $this->add_ready_script(
  138. <<<EOF
  139. // The "tab widgets" to handle.
  140. var tabs = $('div[id^=tabbedContent]');
  141. // Ugly patch for a change in the behavior of jQuery UI:
  142. // Before jQuery UI 1.9, tabs were always considered as "local" (opposed to Ajax)
  143. // when their href was beginning by #. Starting with 1.9, a <base> tag in the page
  144. // is taken into account and causes "local" tabs to be considered as Ajax
  145. // unless their URL is equal to the URL of the page...
  146. if ($('base').length > 0)
  147. {
  148. $('div[id^=tabbedContent] > ul > li > a').each(function() {
  149. var sHash = location.hash;
  150. var sCleanLocation = location.href.toString().replace(sHash, '').replace(/#$/, '');
  151. $(this).attr("href", sCleanLocation+$(this).attr("href"));
  152. });
  153. }
  154. if ($.bbq)
  155. {
  156. // This selector will be reused when selecting actual tab widget A elements.
  157. var tab_a_selector = 'ul.ui-tabs-nav a';
  158. // Enable tabs on all tab widgets. The `event` property must be overridden so
  159. // that the tabs aren't changed on click, and any custom event name can be
  160. // specified. Note that if you define a callback for the 'select' event, it
  161. // will be executed for the selected tab whenever the hash changes.
  162. tabs.tabs({ event: 'change' });
  163. // Define our own click handler for the tabs, overriding the default.
  164. tabs.find( tab_a_selector ).click(function()
  165. {
  166. var state = {};
  167. // Get the id of this tab widget.
  168. var id = $(this).closest( 'div[id^=tabbedContent]' ).attr( 'id' );
  169. // Get the index of this tab.
  170. var idx = $(this).parent().prevAll().length;
  171. // Set the state!
  172. state[ id ] = idx;
  173. $.bbq.pushState( state );
  174. });
  175. }
  176. else
  177. {
  178. tabs.tabs();
  179. }
  180. EOF
  181. );
  182. }
  183. // Render the tabs in the page (if any)
  184. $this->s_content = $this->m_oTabs->RenderIntoContent($this->s_content, $this);
  185. // Additional UI widgets to be activated inside the ajax fragment
  186. // Important: Testing the content type is not enough because some ajax handlers have not correctly positionned the flag (e.g json response corrupted by the script)
  187. if (($this->sContentType == 'text/html') && (preg_match('/class="date-pick"/', $this->s_content) || preg_match('/class="datetime-pick"/', $this->s_content)) )
  188. {
  189. $this->add_ready_script(
  190. <<<EOF
  191. PrepareWidgets();
  192. EOF
  193. );
  194. }
  195. $s_captured_output = $this->ob_get_clean_safe();
  196. if (($this->sContentType == 'text/html') && ($this->sContentDisposition == 'inline'))
  197. {
  198. // inline content != attachment && html => filter all scripts for malicious XSS scripts
  199. echo self::FilterXSS($this->s_content);
  200. }
  201. else
  202. {
  203. echo $this->s_content;
  204. }
  205. if (!empty($this->m_sMenu))
  206. {
  207. $uid = time();
  208. echo "<div id=\"accordion_temp_$uid\">\n";
  209. echo "<div id=\"accordion\">\n";
  210. echo "<!-- Beginning of the accordion menu -->\n";
  211. echo self::FilterXSS($this->m_sMenu);
  212. echo "<!-- End of the accordion menu-->\n";
  213. echo "</div>\n";
  214. echo "</div>\n";
  215. echo "<script type=\"text/javascript\">\n";
  216. echo "$('#inner_menu').html($('#accordion_temp_$uid').html());\n";
  217. echo "$('#accordion_temp_$uid').remove();\n";
  218. echo "\n</script>\n";
  219. }
  220. //echo $this->s_deferred_content;
  221. if (count($this->a_scripts) > 0)
  222. {
  223. echo "<script type=\"text/javascript\">\n";
  224. echo implode("\n", $this->a_scripts);
  225. echo "\n</script>\n";
  226. }
  227. if (!empty($this->s_deferred_content))
  228. {
  229. echo "<script type=\"text/javascript\">\n";
  230. echo "\$('body').append('".addslashes(str_replace("\n", '', $this->s_deferred_content))."');\n";
  231. echo "\n</script>\n";
  232. }
  233. if (!empty($this->m_sReadyScript))
  234. {
  235. echo "<script type=\"text/javascript\">\n";
  236. echo $this->m_sReadyScript; // Ready Scripts are output as simple scripts
  237. echo "\n</script>\n";
  238. }
  239. if (trim($s_captured_output) != "")
  240. {
  241. echo self::FilterXSS($s_captured_output);
  242. }
  243. if (class_exists('DBSearch'))
  244. {
  245. DBSearch::RecordQueryTrace();
  246. }
  247. }
  248. /**
  249. * Adds a paragraph with a smaller font into the page
  250. * NOT implemented (i.e does nothing)
  251. * @param string $sText Content of the (small) paragraph
  252. * @return void
  253. */
  254. public function small_p($sText)
  255. {
  256. }
  257. public function add($sHtml)
  258. {
  259. if (($this->m_oTabs->GetCurrentTabContainer() != '') && ($this->m_oTabs->GetCurrentTab() != ''))
  260. {
  261. $this->m_oTabs->AddToTab($this->m_oTabs->GetCurrentTabContainer(), $this->m_oTabs->GetCurrentTab(), $sHtml);
  262. }
  263. else
  264. {
  265. parent::add($sHtml);
  266. }
  267. }
  268. /**
  269. * Records the current state of the 'html' part of the page output
  270. * @return mixed The current state of the 'html' output
  271. */
  272. public function start_capture()
  273. {
  274. $sCurrentTabContainer = $this->m_oTabs->GetCurrentTabContainer();
  275. $sCurrentTab = $this->m_oTabs->GetCurrentTab();
  276. if (!empty($sCurrentTabContainer) && !empty($sCurrentTab))
  277. {
  278. $iOffset = $this->m_oTabs->GetCurrentTabLength();
  279. return array('tc' => $sCurrentTabContainer, 'tab' => $sCurrentTab, 'offset' => $iOffset);
  280. }
  281. else
  282. {
  283. return parent::start_capture();
  284. }
  285. }
  286. /**
  287. * Returns the part of the html output that occurred since the call to start_capture
  288. * and removes this part from the current html output
  289. * @param $offset mixed The value returned by start_capture
  290. * @return string The part of the html output that was added since the call to start_capture
  291. */
  292. public function end_capture($offset)
  293. {
  294. if (is_array($offset))
  295. {
  296. if ($this->m_oTabs->TabExists($offset['tc'], $offset['tab']))
  297. {
  298. $sCaptured = $this->m_oTabs->TruncateTab($offset['tc'], $offset['tab'], $offset['offset']);
  299. }
  300. else
  301. {
  302. $sCaptured = '';
  303. }
  304. }
  305. else
  306. {
  307. $sCaptured = parent::end_capture($offset);
  308. }
  309. return $sCaptured;
  310. }
  311. /**
  312. * Add any text or HTML fragment (identified by an ID) at the end of the body of the page
  313. * This is useful to add hidden content, DIVs or FORMs that should not
  314. * be embedded into each other.
  315. */
  316. public function add_at_the_end($s_html, $sId = '')
  317. {
  318. if ($sId != '')
  319. {
  320. $this->add_script("$('#{$sId}').remove();"); // Remove any previous instance of the same Id
  321. }
  322. $this->s_deferred_content .= $s_html;
  323. }
  324. /**
  325. * Adds a script to be executed when the DOM is ready (typical JQuery use)
  326. * NOT implemented in this version of the class.
  327. * @return void
  328. */
  329. public function add_ready_script($sScript)
  330. {
  331. $this->m_sReadyScript .= $sScript."\n";
  332. }
  333. /**
  334. * Cannot be called in this context, since Ajax pages do not share
  335. * any context with the calling page !!
  336. */
  337. public function GetUniqueId()
  338. {
  339. assert(false);
  340. return 0;
  341. }
  342. public static function FilterXSS($sHTML)
  343. {
  344. return str_ireplace(array('<script', '</script>'), array('<!-- <removed-script', '</removed-script> -->'), $sHTML);
  345. }
  346. }