ajaxwebpage.class.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. // Copyright (C) 2010-2012 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-2012 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
  27. {
  28. /**
  29. * Jquery style ready script
  30. * @var Hash
  31. */
  32. protected $m_sReadyScript;
  33. protected $m_sCurrentTab;
  34. protected $m_sCurrentTabContainer;
  35. protected $m_aTabs;
  36. /**
  37. * constructor for the web page
  38. * @param string $s_title Not used
  39. */
  40. function __construct($s_title)
  41. {
  42. parent::__construct($s_title);
  43. $this->m_sReadyScript = "";
  44. //$this->add_header("Content-type: text/html; charset=utf-8");
  45. $this->add_header("Cache-control: no-cache");
  46. $this->m_sCurrentTabContainer = '';
  47. $this->m_sCurrentTab = '';
  48. $this->m_aTabs = array();
  49. $this->sContentType = 'text/html';
  50. $this->sContentDisposition = 'inline';
  51. }
  52. public function AddTabContainer($sTabContainer, $sPrefix = '')
  53. {
  54. $this->m_aTabs[$sTabContainer] = array('content' =>'', 'prefix' => $sPrefix);
  55. $this->add("\$Tabs:$sTabContainer\$");
  56. }
  57. public function AddToTab($sTabContainer, $sTabLabel, $sHtml)
  58. {
  59. if (!isset($this->m_aTabs[$sTabContainer]['content'][$sTabLabel]))
  60. {
  61. // Set the content of the tab
  62. $this->m_aTabs[$sTabContainer]['content'][$sTabLabel] = $sHtml;
  63. }
  64. else
  65. {
  66. // Append to the content of the tab
  67. $this->m_aTabs[$sTabContainer]['content'][$sTabLabel] .= $sHtml;
  68. }
  69. }
  70. public function SetCurrentTabContainer($sTabContainer = '')
  71. {
  72. $sPreviousTabContainer = $this->m_sCurrentTabContainer;
  73. $this->m_sCurrentTabContainer = $sTabContainer;
  74. return $sPreviousTabContainer;
  75. }
  76. public function SetCurrentTab($sTabLabel = '')
  77. {
  78. $sPreviousTab = $this->m_sCurrentTab;
  79. $this->m_sCurrentTab = $sTabLabel;
  80. return $sPreviousTab;
  81. }
  82. public function GetCurrentTab()
  83. {
  84. return $this->m_sCurrentTab;
  85. }
  86. /**
  87. * Echoes the content of the whole page
  88. * @return void
  89. */
  90. public function output()
  91. {
  92. if (!empty($this->sContentType))
  93. {
  94. $this->add_header('Content-type: '.$this->sContentType);
  95. }
  96. if (!empty($this->sContentDisposition))
  97. {
  98. $this->add_header('Content-Disposition: '.$this->sContentDisposition.'; filename="'.$this->sContentFileName.'"');
  99. }
  100. foreach($this->a_headers as $s_header)
  101. {
  102. header($s_header);
  103. }
  104. if (count($this->m_aTabs) > 0)
  105. {
  106. $this->add_ready_script(
  107. <<<EOF
  108. // The "tab widgets" to handle.
  109. var tabs = $('div[id^=tabbedContent]');
  110. if ($.bbq)
  111. {
  112. // This selector will be reused when selecting actual tab widget A elements.
  113. var tab_a_selector = 'ul.ui-tabs-nav a';
  114. // Enable tabs on all tab widgets. The `event` property must be overridden so
  115. // that the tabs aren't changed on click, and any custom event name can be
  116. // specified. Note that if you define a callback for the 'select' event, it
  117. // will be executed for the selected tab whenever the hash changes.
  118. tabs.tabs({ event: 'change' });
  119. // Define our own click handler for the tabs, overriding the default.
  120. tabs.find( tab_a_selector ).click(function()
  121. {
  122. var state = {};
  123. // Get the id of this tab widget.
  124. var id = $(this).closest( 'div[id^=tabbedContent]' ).attr( 'id' );
  125. // Get the index of this tab.
  126. var idx = $(this).parent().prevAll().length;
  127. // Set the state!
  128. state[ id ] = idx;
  129. $.bbq.pushState( state );
  130. });
  131. }
  132. else
  133. {
  134. tabs.tabs();
  135. }
  136. EOF
  137. );
  138. }
  139. // Render the tabs in the page (if any)
  140. foreach($this->m_aTabs as $sTabContainerName => $aTabContainer)
  141. {
  142. $sTabs = '';
  143. $m_aTabs = $aTabContainer['content'];
  144. $sPrefix = $aTabContainer['prefix'];
  145. $container_index = 0;
  146. if (count($m_aTabs) > 0)
  147. {
  148. $sTabs = "<!-- tabs -->\n<div id=\"tabbedContent_{$sPrefix}{$sTabContainerName}\" class=\"light\">\n";
  149. $sTabs .= "<ul>\n";
  150. // Display the unordered list that will be rendered as the tabs
  151. $i = 0;
  152. foreach($m_aTabs as $sTabName => $sTabContent)
  153. {
  154. $sTabs .= "<li><a href=\"#tab_{$sPrefix}$i\" class=\"tab\"><span>".htmlentities($sTabName, ENT_QUOTES, 'UTF-8')."</span></a></li>\n";
  155. $i++;
  156. }
  157. $sTabs .= "</ul>\n";
  158. // Now add the content of the tabs themselves
  159. $i = 0;
  160. foreach($m_aTabs as $sTabName => $sTabContent)
  161. {
  162. $sTabs .= "<div id=\"tab_{$sPrefix}$i\">".$sTabContent."</div>\n";
  163. $i++;
  164. }
  165. $sTabs .= "</div>\n<!-- end of tabs-->\n";
  166. }
  167. $this->s_content = str_replace("\$Tabs:$sTabContainerName\$", $sTabs, $this->s_content);
  168. $container_index++;
  169. }
  170. // Additional UI widgets to be activated inside the ajax fragment ??
  171. if (($this->sContentType == 'text/html') && (preg_match('/class="date-pick"/', $this->s_content) || preg_match('/class="datetime-pick"/', $this->s_content)) )
  172. {
  173. $this->add_ready_script(
  174. <<<EOF
  175. $(".date-pick").datepicker({
  176. showOn: 'button',
  177. buttonImage: '../images/calendar.png',
  178. buttonImageOnly: true,
  179. dateFormat: 'yy-mm-dd',
  180. constrainInput: false,
  181. changeMonth: true,
  182. changeYear: true
  183. });
  184. $(".datetime-pick").datepicker({
  185. showOn: 'button',
  186. buttonImage: '../images/calendar.png',
  187. buttonImageOnly: true,
  188. dateFormat: 'yy-mm-dd 00:00:00',
  189. constrainInput: false,
  190. changeMonth: true,
  191. changeYear: true
  192. });
  193. EOF
  194. );
  195. }
  196. $s_captured_output = ob_get_contents();
  197. ob_end_clean();
  198. if (($this->sContentType == 'text/html') && ($this->sContentDisposition == 'inline'))
  199. {
  200. // inline content != attachment && html => filter all scripts for malicious XSS scripts
  201. echo self::FilterXSS($this->s_content);
  202. }
  203. else
  204. {
  205. echo $this->s_content;
  206. }
  207. //echo $this->s_deferred_content;
  208. if (count($this->a_scripts) > 0)
  209. {
  210. echo "<script type=\"text/javascript\">\n";
  211. echo implode("\n", $this->a_scripts);
  212. echo "\n</script>\n";
  213. }
  214. if (!empty($this->s_deferred_content))
  215. {
  216. echo "<script type=\"text/javascript\">\n";
  217. echo "\$('body').append('".addslashes(str_replace("\n", '', $this->s_deferred_content))."');\n";
  218. echo "\n</script>\n";
  219. }
  220. if (!empty($this->m_sReadyScript))
  221. {
  222. echo "<script type=\"text/javascript\">\n";
  223. echo $this->m_sReadyScript; // Ready Scripts are output as simple scripts
  224. echo "\n</script>\n";
  225. }
  226. if (trim($s_captured_output) != "")
  227. {
  228. echo self::FilterXSS($s_captured_output);
  229. }
  230. }
  231. /**
  232. * Adds a paragraph with a smaller font into the page
  233. * NOT implemented (i.e does nothing)
  234. * @param string $sText Content of the (small) paragraph
  235. * @return void
  236. */
  237. public function small_p($sText)
  238. {
  239. }
  240. public function add($sHtml)
  241. {
  242. if (!empty($this->m_sCurrentTabContainer) && !empty($this->m_sCurrentTab))
  243. {
  244. $this->AddToTab($this->m_sCurrentTabContainer, $this->m_sCurrentTab, $sHtml);
  245. }
  246. else
  247. {
  248. parent::add($sHtml);
  249. }
  250. }
  251. /**
  252. * Records the current state of the 'html' part of the page output
  253. * @return mixed The current state of the 'html' output
  254. */
  255. public function start_capture()
  256. {
  257. if (!empty($this->m_sCurrentTabContainer) && !empty($this->m_sCurrentTab))
  258. {
  259. $iOffset = isset($this->m_aTabs[$this->m_sCurrentTabContainer]['content'][$this->m_sCurrentTab]) ? strlen($this->m_aTabs[$this->m_sCurrentTabContainer]['content'][$this->m_sCurrentTab]): 0;
  260. return array('tc' => $this->m_sCurrentTabContainer, 'tab' => $this->m_sCurrentTab, 'offset' => $iOffset);
  261. }
  262. else
  263. {
  264. return parent::start_capture();
  265. }
  266. }
  267. /**
  268. * Returns the part of the html output that occurred since the call to start_capture
  269. * and removes this part from the current html output
  270. * @param $offset mixed The value returned by start_capture
  271. * @return string The part of the html output that was added since the call to start_capture
  272. */
  273. public function end_capture($offset)
  274. {
  275. if (is_array($offset))
  276. {
  277. if (isset($this->m_aTabs[$offset['tc']]['content'][$offset['tab']]))
  278. {
  279. $sCaptured = substr($this->m_aTabs[$offset['tc']]['content'][$offset['tab']], $offset['offset']);
  280. $this->m_aTabs[$offset['tc']]['content'][$offset['tab']] = substr($this->m_aTabs[$offset['tc']]['content'][$offset['tab']], 0, $offset['offset']);
  281. }
  282. else
  283. {
  284. $sCaptured = '';
  285. }
  286. }
  287. else
  288. {
  289. $sCaptured = parent::end_capture($offset);
  290. }
  291. return $sCaptured;
  292. }
  293. /**
  294. * Add any text or HTML fragment (identified by an ID) at the end of the body of the page
  295. * This is useful to add hidden content, DIVs or FORMs that should not
  296. * be embedded into each other.
  297. */
  298. public function add_at_the_end($s_html, $sId = '')
  299. {
  300. if ($sId != '')
  301. {
  302. $this->add_script("$('#{$sId}').remove();"); // Remove any previous instance of the same Id
  303. }
  304. $this->s_deferred_content .= $s_html;
  305. }
  306. /**
  307. * Adds a script to be executed when the DOM is ready (typical JQuery use)
  308. * NOT implemented in this version of the class.
  309. * @return void
  310. */
  311. public function add_ready_script($sScript)
  312. {
  313. $this->m_sReadyScript .= $sScript."\n";
  314. }
  315. /**
  316. * Cannot be called in this context, since Ajax pages do not share
  317. * any context with the calling page !!
  318. */
  319. public function GetUniqueId()
  320. {
  321. assert(false);
  322. return 0;
  323. }
  324. public static function FilterXSS($sHTML)
  325. {
  326. return str_ireplace(array('<script', '</script>'), array('<!-- <removed-script', '</removed-script> -->'), $sHTML);
  327. }
  328. }
  329. ?>