ajaxwebpage.class.inc.php 12 KB

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