fix missing bold for your account item
[brisk.git] / web / xynt_test01_wss.php
1 <?php
2 foreach (array('isstream', 'f_test', 'f_trans', 'f_port', 'f_fback', 'sess', 'stat',
3                'subst', 'step', 'from', 'transp') as $i) {
4     if (isset($_REQUEST[$i])) {
5         $$i = $_REQUEST[$i];
6     }
7 }
8
9 $desc = array( "Semplice: da 1 a 9 ogni secondo, poi ricomincia (status sempre verde).",
10                "Continuo: da 1 a N ogni secondo, ricomincia ogni 9 (status sempre verde).",
11                "Restart: da 1 a 8 ogni secondo, pausa 16 secondi (status passa ad arancione e poi a rosso), poi ricomincia (e status torna a verde).",
12                "Pausa: da 1 a 5 ogni secondo, pausa 3 secondi, e poi 8 e 9 ogni secondo, e poi ricomincia (status sempre verde).",
13                "Keyword: da 1 a 5 ogni secondo, @BEGIN@, @END@, @BEGIN@ xxx yyy @END@, 9, (status sempre verde).",
14                "Reload limiter: da 1 a 8 ogni secondo e chiude, 9 setta e chiude subito,<br>il client aspetta 3 secondi, e poi da 10 a N ogni secondo, (status sempre verde).",
15                "Esegue un comando, manda loc_new a un valore irraggiungibile, stoppa il processore di comandi e poi dà un comando teoricamente irraggiungibile.");
16
17
18 // trim(mb_convert_case($split[0], MB_CASE_TITLE, 'UTF-8'))
19
20 function headers_render($header, $len)
21 {
22     $cookies = "";
23
24     if (isset($header['Cookies'])) {
25         $cookies = $header['Cookies']->render();
26         unset($header['Cookies']);
27     }
28     if (isset($header['Location'])) {
29         header(sprintf("HTTP/1.1 302 OK\r\n%sLocation: %s", $cookies, $header['Location']));
30     }
31     else if (isset($header['HTTP-Response'])) {
32         header(sprintf("HTTP/1.1 %s", $header['HTTP-Response']));
33         foreach($header as $key => $value) {
34             if (strtolower($key) == "http-response")
35                 continue;
36             header(sprintf("%s: %s", $key, $value));
37         }
38         if ($len >= 0) {
39             header(sprintf("Content-Length: %ld", $len));
40         }
41     }
42     else {
43         header("HTTP/1.1 200 OK\r\n");
44
45         if (!isset($header['Date']))
46             header(sprintf("Date: %s", date(DATE_RFC822)));
47         if (!isset($header['Connection']))
48             header("Connection: close");
49         if (!isset($header['Content-Type']))
50             header("Content-Type: text/html");
51         foreach($header as $key => $value) {
52             header(sprintf("%s: %s", $key, $value));
53         }
54         if ($len >= 0) {
55             header(sprintf("Content-Length: %d", $len));
56         }
57         else {
58             header("Cache-Control: no-cache, must-revalidate");
59             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
60             if (!isset($header['Content-Encoding'])) {
61                 header("Content-Encoding: chunked");
62             }
63             header("Transfer-Encoding: chunked");
64         }
65         header($cookies);
66     }
67
68     return (TRUE);
69 }
70
71 $transs = array( "iframe", "websocket", "websocketsec", "xhr", "htmlfile" );
72 $trans_ports = array( "iframe"=>80, "websocket"=>80, "websocketsec"=>443, "xhr"=>80, "htmlfile"=>80 );
73 if (!isset($f_trans))
74     $f_trans = $transs[0];
75
76 if (!isset($f_test))
77     $f_test = 1;
78
79 if (!isset($f_port))
80     $f_port = NULL;
81
82 if (!isset($f_fback))
83     $f_fback = 0;
84
85
86 function mop_flush()
87 {
88     for ($i = 0; $i < ob_get_level(); $i++)
89         ob_end_flush();
90     ob_implicit_flush(1);
91     flush();
92 }
93
94 $escpush_from = array("\\", "\"");
95 $escpush_to   = array("\\\\", "\\\"");
96 function escpush($s)
97 {
98     GLOBAL $escpush_from, $escpush_to;
99
100     return str_replace($escpush_from, $escpush_to, $s);
101 }
102
103 function xcape($s)
104 {
105   $from = array (   '\\',     '@',        '|' );
106   $to   = array ( '\\\\', '&#64;', '&brvbar;' );
107
108   return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
109 }
110
111 if (isset($isstream) && $isstream == "true") {
112     require_once("Obj/transports.phh");
113
114     if (isset($transp) && $transp == "websocket") {
115         $trobj = new Transport_websocket();
116     }
117     else if (isset($transp) && $transp == "websocketsec") {
118         $trobj = new Transport_websocket(TRUE);
119     }
120     else if (isset($transp) && $transp == "xhr") {
121         $trobj = new Transport_xhr();
122     }
123     else if (isset($transp) && $transp == "htmlfile") {
124         $trobj = new Transport_htmlfile();
125     }
126     else {
127         $trobj = new Transport_iframe();
128     }
129     $headers_out = array();
130
131     $init_string = "";
132     for ($i = 0 ; $i < 4096 ; $i++) {
133         if (($i % 128) == 0)
134             $init_string .= " ";
135         else
136             $init_string .= chr(mt_rand(65, 90));
137     }
138     $headers_in = getallheaders();
139     // error_log(print_r($headers_in, TRUE));
140     $headers = array();
141     foreach ($headers_in as $header_in => $value) {
142         $headers[mb_convert_case($header_in, MB_CASE_TITLE, 'UTF-8')] = $value;
143     }
144     $fp = fopen("/tmp/xynt.log", "a+");
145     fprintf($fp, "here we are\n");
146     fclose($fp);
147
148     $body = $trobj->init("plain", $headers, $headers_out, $init_string, "", "0");
149
150     if ($body === FALSE) {
151         $fp = fopen("/tmp/xynt.log", "a+");
152         fprintf($fp, "init failed\n");
153         fclose($fp);
154     }
155     else {
156         $fp = fopen("/tmp/xynt.log", "a+");
157         fprintf($fp, "after_init [%s] [%s]\n", $transp, print_r($headers_out, TRUE));
158         fprintf($fp, "body [%s][%d]\n", $body, mb_strlen($body, "ASCII"));
159         fclose($fp);
160     }
161
162     if (isset($transp) && ($transp == "websocket" || $transp == "websocketsec")) {
163         header_remove('Connection');
164         header_remove('Content-Encoding');
165         header_remove('Content-Type');
166         header_remove('Date');
167         header_remove('Keep-Alive');
168         header_remove('Server');
169         header_remove('Transfer-Encoding');
170         header_remove('Vary');
171         header_remove('X-Powered-By');
172
173         headers_render($headers_out, 100);
174     }
175     $lnz = 0;
176
177     print($body);
178     $lnz += mb_strlen($body, "ASCII");
179     mop_flush();
180
181     switch ($f_test) {
182     case 1:
183         // from 1 to 9 into the innerHTML and than close
184         for ($i = 1 ; $i < 10 ; $i++) {
185             $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
186             print($chunk);
187             mop_flush();
188             sleep(1);
189         }
190
191         break;
192     case 2:
193         // from 1 to 9 into the innerHTML and than close
194         for ($i = 1 ; $i < 10 ; $i++) {
195             $chunk = $trobj->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
196             print($chunk);
197             $lnz += mb_strlen($chunk, "ASCII");
198             mop_flush();
199             sleep(1);
200         }
201         break;
202     case 3:
203         // from 1 to 9 with 60 secs after 8, the client js api must restart stream after 12 secs
204         for ($i = 1 ; $i < 10 ; $i++) {
205             $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
206             print($chunk);
207             mop_flush();
208             sleep(1);
209             if ($i == 8)
210                 sleep(60);
211         }
212         break;
213     case 4:
214         // from 1 to 9 into the innerHTML and than close
215         for ($i = 1 ; $i < 10 ; $i++) {
216             if ($i != 5) {
217                 $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
218             }
219             else {
220                 $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';|sleep(gst,3000);", $i));
221             }
222             print($chunk);
223             mop_flush();
224             sleep(1);
225         }
226         break;
227     case 5:
228         // from 1 to 9 into the innerHTML and than close
229         $cont = array('@BEGIN@', '@END@', '@BEGIN@ sleep(1); @END@');
230         for ($i = 1 ; $i < 10 ; $i++) {
231             switch($i) {
232             case 6:
233             case 7:
234             case 8:
235                 $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%s';", xcape($cont[$i - 6])));
236                 break;
237             default:
238                 $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
239                 break;
240             }
241             print($chunk);
242             mop_flush();
243             if ($i < 9)
244                 sleep(1);
245         }
246         break;
247     case 6:
248         // from 1 to 9 into the innerHTML and than close
249         if ($step == 8) {
250             $chunk = $trobj->chunk(1, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
251             print($chunk);
252             // without this usleep the delay is doubled in iframe stream because 
253             // no transp.xynt_streaming back-set is performed
254             usleep(250000);
255             mop_flush();
256         }
257         else {
258             for ($i = 1 ; $i < 10 ; $i++) {
259                 $chunk = $trobj->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
260                 print($chunk);
261                 mop_flush();
262                 if ($i < 9)
263                     sleep(1);
264             }
265         }
266         break;
267     case 7:
268         $chunk = $trobj->chunk(1, sprintf("gst.st++; \$('container').innerHTML = 'prima';"));
269         print($chunk);
270
271         $chunk = $trobj->chunk(2, sprintf("gst.st++; gst.st_loc_new++; xstm.stop();"));
272         print($chunk);
273
274         $chunk = $trobj->chunk(3, sprintf("gst.st++; \$('container').innerHTML = 'dopo';"));
275         print($chunk);
276
277         break;
278     }
279
280     print($trobj->close());
281     mop_flush();
282
283     exit;
284 }
285 ?>
286 <html>
287 <head>
288 <title>XYNT TEST01</title>
289 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
290 <script type="text/javascript" src="xynt-streaming.js"></script>
291 <script type="text/javascript" src="commons.js"></script>
292 <script type="text/javascript" src="heartbit.js"></script>
293
294 <!-- <script type="text/javascript" src="myconsole.js"></script> -->
295
296 <script type="text/javascript"><!--
297      var sess = "for_test";
298      var stat = "";
299      var subst = "";
300      var gst = new globst();
301      window.onload = function() {
302
303          xstm = new xynt_streaming(window, "<?php echo "$f_trans";?>", <?php
304     echo ($f_port == NULL ? "${trans_ports[$f_trans]}" : "$f_port" );?>, <?php echo "$f_fback";?>, console, gst, 'xynt_test01_php', 'sess', sess, null, 'xynt_test01.php?isstream=true&f_test=<?php echo "$f_test";?>', function(com){eval(com);});
305      xstm.hbit_set(heartbit);
306      xstm.start();
307  }
308  //-->
309 </script>
310 </head>
311 <body>
312 <div>
313 <?php
314
315 printf("<table>");
316 for ($test = 1 ; $test <= count($desc) ; $test++) {
317     printf("<tr>");
318     foreach ($transs as $trans) {
319         printf("<td style=\"padding: 8px; border: 1px solid black;\"><a href=\"?f_trans=%s&f_test=%d%s&f_fback=%d\">Test %s %02d (port %d (fb %d))</a></td>", $trans, $test,
320                ($f_port == NULL ? "" : sprintf("&f_port=%d", $f_port)), $f_fback, $trans, $test,
321                ($f_port == NULL ? $trans_ports[$trans] : $f_port ), $f_fback);
322     }
323     printf("</tr>\n");
324 }
325 printf("<tr><td style=\"padding: 8px; border: 1px solid black; text-align: center;\" colspan='%d'><a href='#' onclick=\"xstm.abort(); \">STOP</a></td></tr>", count($transs));
326 printf("</table>");
327 printf("<br>[%s]<br>Test: %d<br>", $f_trans, $f_test);
328 ?>
329 </div>
330 <div>
331 <b>Descrizione</b>: <?php echo $desc[$f_test - 1]; ?>
332 </div>
333
334 <div>
335 <b>Status</b>: <img id="stm_stat" class="nobo" style="vertical-align: bottom;" src="img/line-status_b.png"></div>
336
337 </div>
338 <div>
339 <b>Counter</b>: <span id="container">
340 BEGIN
341 </span>
342 </div>
343 </body>
344 </html>