fix hard cabled stream_fini response when some error occurs
[brisk.git] / web / Obj / transports.phh
1 <?php
2 /*
3  *  sac-a-push - Obj/transports.phh
4  *
5  *  Copyright (C) 2012 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 /*
26  *  test: SO x Browser
27  *  Values: Y: works, N: not works, @: continuous download,
28  *          D: continuous download after first reload
29  *
30  *  Stream IFRAME:
31  *
32  * Iframe| IW | FF | Ch | Op | Ko | IE
33  * ------+----+----+----+----+----+----
34  *   Lnx | D  |    | @  |    | @  | x
35  *   Win | x  | D  | @  | @  |    | D
36  *   Mac | x  |    |    |    |    |
37  *
38  *
39  *   XHR | IW | FF | Ch | Op | Ko | IE
40  * ------+----+----+----+----+----+----
41  *   Lnx | Y  |    | ^D |    | Y  | x
42  *   Win | x  | Y  | Y  |    |    | N
43  *   Mac | x  |    |    |    |    |
44  *
45  *
46  * HtmlFl| IW | FF | Ch | Op | Ko | IE
47  * ------+----+----+----+----+----+----
48  *   Lnx | N  |    |    |    | N  |
49  *   Win | x  | N  | N  |    |    | Y* (* seems delay between click and load of a new page)
50  *   Mac | x  |    |    |    |    |
51  *
52  *
53  */
54
55
56 class Transport_template {
57
58     function Transport_template() {
59     }
60
61     function init($enc, &$header_out, $init_string, $base, $step)
62     {
63     }
64
65     static function fini($init_string, $base, $blockerr)
66     {
67     }
68
69     function chunk($step, $cont)
70     {
71     }
72 }
73
74 class Transport_xhr {
75
76     function Transport_xhr() {
77     }
78
79     function init($enc, &$header_out, $init_string, $base, $step)
80     {
81         $ret = sprintf("@BEGIN@ /* %s */ @END@", $init_string);
82         if ($enc != 'plain')
83             $header_out['Content-Encoding'] = $enc;
84         $header_out['Cache-Control'] = 'no-cache, must-revalidate';     // HTTP/1.1
85         $header_out['Expires']       = 'Mon, 26 Jul 1997 05:00:00 GMT'; // Date in the past
86         $header_out['Content-type']  = 'application/xml; charset="utf-8"';
87
88         return ($ret);
89     }
90
91     static function fini($init_string, $base, $blockerr)
92     {
93         return (sprintf('@BEGIN@ %s window.onbeforeunload = null; window.onunload = null; document.location.assign("%sindex.php"); @END@',  ($blockerr ? 'xstm.stop(); ' : ''), $base));
94         return ("");
95     }
96
97     function chunk($step, $cont)
98     {
99         return ("@BEGIN@".$cont."@END@");
100     }
101 }
102
103 class Transport_iframe {
104
105     function Transport_iframe() {
106     }
107
108     function init($enc, &$header_out, $init_string, $base, $step)
109     {
110         $ret = "";
111
112         if ($enc != 'plain')
113             $header_out['Content-Encoding'] = $enc;
114         $header_out['Cache-Control'] = 'no-cache, must-revalidate';     // HTTP/1.1
115         $header_out['Expires']       = 'Mon, 26 Jul 1997 05:00:00 GMT'; // Date in the past
116         $header_out['Content-type']  = 'text/html; charset="utf-8"';
117         
118         $ret .= sprintf("<html>
119 <head>
120 <script type=\"text/javascript\" src=\"%scommons.js\"></script>
121 <script type=\"text/javascript\" src=\"%sxynt-streaming-ifra.js\"></script>
122 <script type=\"text/javascript\">
123 var xynt_streaming = \"ready\";", $base, $base);
124         if ($step > 0)
125             $ret .= sprintf("last_clean = %d;\n", ($step-1));
126         $ret .= sprintf("
127 window.onload = function () { try { if (xynt_streaming != \"ready\") { xynt_streaming.transp.stopped = true; } } catch(e) { /* console.log(\"catcha\"); */ } };
128 </script> 
129 </head>
130 <body>");
131         $ret .= sprintf("<!-- \n%s -->\n", $init_string);
132
133         return ($ret);
134     }
135
136     static function fini($init_string, $base, $blockerr)
137     {
138         $ret = "";
139         $ret .= sprintf("<html>
140 <head>
141 <script type=\"text/javascript\" src=\"%scommons.js\"></script>
142 <script type=\"text/javascript\" src=\"%sxynt-streaming-ifra.js\"></script>
143 <script type=\"text/javascript\">
144 var xynt_streaming = \"ready\";", $base, $base);
145         $ret .= sprintf("
146 window.onload = function () { try { if (xynt_streaming != \"ready\") { xynt_streaming.reload(); } } catch(e) { /* console.log(\"catcha\"); */ } };
147 </script>
148 </head>
149 <body>");
150         $ret .= sprintf("<!-- \n%s -->\n", $init_string);
151         $ret .= sprintf("<script id='hs%d' type='text/javascript'><!--
152 push(\"%s\");
153 // -->
154 </script>", 0, escpush($blockerr) );
155         return ($ret);
156     }
157
158     function chunk($step, $cont)
159     {
160         if ($cont == NULL) {
161             return sprintf("<script id='hs%d' type='text/javascript'><!--
162 push(null);\n// -->\n</script>", $step);
163         }
164         else {
165             return sprintf("<script id='hs%d' type='text/javascript'><!--
166 push(\"%s\");\n// -->\n</script>", $step, escpush($cont) );
167         }
168     }
169 }
170
171 class Transport_htmlfile extends Transport_iframe {
172 }
173
174 class Transport {
175     function Transport()
176     {
177     }
178
179     static function create($transp)
180     {
181         if ($transp == 'xhr') {
182             return new Transport_xhr();
183         }
184         else if ($transp == 'htmlfile') {
185             return new Transport_htmlfile();
186         }
187         else  {
188             return new Transport_iframe();
189         }
190     }
191     static function gettype($transp)
192     {
193         if ($transp == 'xhr' || $transp == 'htmlfile') {
194             return "Transport_".$transp;
195         }
196         else {
197             return 'Transport_iframe';
198         }
199     }
200 }
201 ?>