ajaxwebpage.class.inc.php 12 KB

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