PageRenderTime 21ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/ccds/base/ccdsXajaxResponsePlugin.inc.php

http://ccds.googlecode.com/
PHP | 304 lines | 220 code | 49 blank | 35 comment | 32 complexity | 94508c9850e684cc1b21c40586a7f5c0 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. class ccdsResponse
  3. extends xajaxResponse
  4. {
  5. var $bHeaderSent=false;
  6. var $fTimeOut=0.00001;
  7. var $comet=false;
  8. var $timer=0;
  9. var $openProgress = false;
  10. var $progressOpen=false;
  11. var $attack=1000;
  12. /*
  13. Function: xajaxCometResponse
  14. calls parent function xajaxResponse();
  15. */
  16. function ccdsResponse( $comet=false, $fTimeOut=false )
  17. {
  18. if ( false != $fTimeOut )
  19. $this->fTimeOut=$fTimeOut;
  20. if ( 1 == $_REQUEST['ccdscomet'] )
  21. $comet=true;
  22. $this->comet=$comet;
  23. $this->timer=microtime();
  24. parent::xajaxResponse();
  25. }
  26. function printOutput( )
  27. {
  28. if ( !$this->comet )
  29. return parent::printOutput();
  30. $this->flush( true );
  31. if ( "HTML5DRAFT" == $_GET['xjxstreaming'] )
  32. {
  33. $response="";
  34. $response.="Event: xjxendstream\n";
  35. $response.="data: done\n";
  36. $response.="\n";
  37. print $response;
  38. ob_flush();
  39. flush();
  40. }
  41. return;
  42. }
  43. /*
  44. Function: flush_XHR
  45. Flushes the command queue for comet browsers.
  46. */
  47. function flush_XHR( )
  48. {
  49. if ( !$this->bHeaderSent )
  50. {
  51. $this->_sendHeaders();
  52. $this->bHeaderSent=true;
  53. }
  54. ob_start();
  55. $this->_printResponse_XML();
  56. $c=ob_get_contents();
  57. ob_get_clean();
  58. $c=str_replace( chr( 1 ), " ", $c );
  59. $c=str_replace( chr( 2 ), " ", $c );
  60. $c=str_replace( chr( 31 ), " ", $c );
  61. $c=str_replace( "", " ", $c );
  62. if ( $c == "<xjx></xjx>" )
  63. return false;
  64. print $c;
  65. ob_flush();
  66. flush();
  67. }
  68. /*
  69. Function: flush_activeX
  70. Flushes the command queue for ActiveX browsers.
  71. */
  72. function flush_activeX( )
  73. {
  74. ob_start();
  75. $this->_printResponse_XML();
  76. $c=ob_get_contents();
  77. ob_end_clean();
  78. $c='<?xml version="1.0" ?>' . $c;
  79. $c=str_replace( '"', '\"', $c );
  80. $c=str_replace( "\n", '\n', $c );
  81. $c=str_replace( "\r", '\r', $c );
  82. $response="";
  83. $response.="<script>top.document.callback(\"";
  84. $response.=$c;
  85. $response.="\");</script>";
  86. print $response;
  87. ob_flush();
  88. flush();
  89. //$this->sleep( 0.0001 );
  90. }
  91. /*
  92. Function: flush_HTML5DRAFT
  93. Flushes the command queue for HTML5DRAFT browsers.
  94. */
  95. function debug( $msg ) {
  96. // file_put_contents("/var/www/web3/html/debug.txt",$msg,FILE_APPEND);
  97. }
  98. function flush_HTML5DRAFT( )
  99. {
  100. if ( !$this->bHeaderSent )
  101. {
  102. header( "Content-Type: application/x-dom-event-stream" );
  103. $this->bHeaderSent=1;
  104. }
  105. ob_start();
  106. $this->_printResponse_XML();
  107. $c=ob_get_contents();
  108. ob_end_clean();
  109. $c=str_replace( "\n", '', $c );
  110. $c=str_replace( "\r", '', $c );
  111. $response="";
  112. $response.="Event: xjxstream\n";
  113. $response.="data: $c\n";
  114. $response.="\n";
  115. print $response;
  116. // $this->debug($response);
  117. ob_flush();
  118. flush();
  119. }
  120. /*
  121. Function: flush
  122. Determines which browser is wating for a response and calls the according flush function.
  123. */
  124. function flush( $anyway=false )
  125. {
  126. if ( !$this->comet )
  127. return;
  128. if ( 0 == count( $this->aCommands ) )
  129. return false;
  130. if ( false == $anyway )
  131. {
  132. $diff=microtime() - $this->timer;
  133. if ( $diff > $this->attack )
  134. return;
  135. }
  136. if ( "xhr" == $_SERVER['HTTP_STREAMING'] )
  137. {
  138. $this->flush_XHR();
  139. }
  140. elseif ( "HTML5DRAFT" == $_GET['xjxstreaming'] )
  141. {
  142. $this->flush_HTML5DRAFT();
  143. }
  144. else
  145. {
  146. $this->flush_activeX();
  147. }
  148. $this->aCommands=array();
  149. }
  150. /*
  151. Function: sleep
  152. Very accurate sleep function.
  153. */
  154. function sleep( $seconds )
  155. {
  156. usleep( floor( $seconds * 1000000 ) );
  157. }
  158. function loadClassCommand( )
  159. {
  160. $iNumargs=func_num_args();
  161. $aArgList=func_get_args();
  162. $sClassName=array_shift( $aArgList );
  163. $sMethod=array_shift( $aArgList );
  164. $objModuleManager=ccdsModuleManager::getInstance();
  165. $foo=$objModuleManager->get( $sClassName );
  166. if ( method_exists( $foo, $sMethod ) )
  167. {
  168. $tmp=call_user_func_array( array
  169. (
  170. &$foo,
  171. $sMethod
  172. ), $aArgList );
  173. $this->loadcommands( $tmp );
  174. }
  175. }
  176. function loadcommands( $mCommands, $bBefore=false )
  177. {
  178. if ( is_a( $mCommands, 'xajaxResponse' ) || is_a( $mCommands, 'ccdsResponse' ) )
  179. {
  180. $this->returnValue=$mCommands->returnValue;
  181. if ( $bBefore )
  182. {
  183. $this->aCommands=array_merge( $mCommands->aCommands, $this->aCommands );
  184. }
  185. else
  186. {
  187. $this->aCommands=array_merge( $this->aCommands, $mCommands->aCommands );
  188. }
  189. }
  190. else if ( is_array( $mCommands ) )
  191. {
  192. if ( $bBefore )
  193. {
  194. $this->aCommands=array_merge( $mCommands, $this->aCommands );
  195. }
  196. else
  197. {
  198. $this->aCommands=array_merge( $this->aCommands, $mCommands );
  199. }
  200. }
  201. else
  202. {
  203. //SkipDebug
  204. if ( !empty( $mCommands ) )
  205. {
  206. $objLanguageManager=&xajaxLanguageManager::getInstance();
  207. trigger_error( $objLanguageManager->getText( 'XJXRSP:LCERR:01' ), E_USER_ERROR );
  208. }
  209. //EndSkipDebug
  210. }
  211. }
  212. function openProgress( )
  213. {
  214. if ( !$this->comet || $this->openProgress )
  215. return;
  216. $this->openProgress=false;
  217. $this->script(
  218. "
  219. try {
  220. $('<div id=\"CMSprogressWIN\"/>').xWindow(
  221. {
  222. title:'Laden...',
  223. width:350,
  224. height:80,
  225. draggable:false,
  226. resizable:false,
  227. maximizable:false,
  228. minimizable:false,
  229. content: '<div id=\"CMSprogressContainer\" style=\"margin:10px;height:22px;width:300px;background:#ccc;\"><div id=\"CMSprogressBAR\" style=\"padding:2px;height:20px;width:1px;background:#333;color:#fff;text-align:right;\">0%</div></div>'
  230. });
  231. } catch(ex) {
  232. alert(ex);
  233. }
  234. " );
  235. }
  236. function updateProgress( $percent )
  237. {
  238. if ( !$this->comet )
  239. return;
  240. $this->assign( "CMSprogressBAR", 'style.width', ( $percent * 3 ) . 'px' );
  241. $this->assign( "CMSprogressBAR", 'innerHTML', ($percent) . '%' );
  242. }
  243. function closeProgress( )
  244. {
  245. if ( !$this->comet )
  246. return;
  247. $this->script( "$('#CMSprogressWIN').xWindow('destroy');" );
  248. }
  249. }