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