moved chunked_content into user class and set chunked as optional
[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     function is_chunked()
74     {
75     }
76 }
77
78 class Transport_xhr {
79
80     function Transport_xhr() {
81     }
82
83     function init($enc, &$header_out, $init_string, $base, $step)
84     {
85         $ret = sprintf("@BEGIN@ /* %s */ @END@", $init_string);
86         if ($enc != 'plain')
87             $header_out['Content-Encoding'] = $enc;
88         $header_out['Cache-Control'] = 'no-cache, must-revalidate';     // HTTP/1.1
89         $header_out['Expires']       = 'Mon, 26 Jul 1997 05:00:00 GMT'; // Date in the past
90         $header_out['Content-type']  = 'application/xml; charset="utf-8"';
91
92         return ($ret);
93     }
94
95     static function fini($init_string, $base, $blockerr)
96     {
97         return (sprintf('@BEGIN@ %s window.onbeforeunload = null; window.onunload = null; document.location.assign("%sindex.php"); @END@',  ($blockerr ? 'xstm.stop(); ' : ''), $base));
98         return ("");
99     }
100
101     function chunk($step, $cont)
102     {
103         return ("@BEGIN@".$cont."@END@");
104     }
105
106     function is_chunked()
107     {
108         return TRUE;
109     }
110 }
111
112 class Transport_iframe {
113
114     function Transport_iframe() {
115     }
116
117     function init($enc, &$header_out, $init_string, $base, $step)
118     {
119         $ret = "";
120
121         if ($enc != 'plain')
122             $header_out['Content-Encoding'] = $enc;
123         $header_out['Cache-Control'] = 'no-cache, must-revalidate';     // HTTP/1.1
124         $header_out['Expires']       = 'Mon, 26 Jul 1997 05:00:00 GMT'; // Date in the past
125         $header_out['Content-type']  = 'text/html; charset="utf-8"';
126         
127         $ret .= sprintf("<html>
128 <head>
129 <script type=\"text/javascript\" src=\"%scommons.js\"></script>
130 <script type=\"text/javascript\" src=\"%sxynt-streaming-ifra.js\"></script>
131 <script type=\"text/javascript\">
132 var xynt_streaming = \"ready\";", $base, $base);
133         if ($step > 0)
134             $ret .= sprintf("last_clean = %d;\n", ($step-1));
135         $ret .= sprintf("
136 window.onload = function () { try { if (xynt_streaming != \"ready\") { xynt_streaming.transp.stopped = true; } } catch(e) { /* console.log(\"catcha\"); */ } };
137 </script> 
138 </head>
139 <body>");
140         $ret .= sprintf("<!-- \n%s -->\n", $init_string);
141
142         return ($ret);
143     }
144
145     static function fini($init_string, $base, $blockerr)
146     {
147         $ret = "";
148         $ret .= sprintf("<html>
149 <head>
150 <script type=\"text/javascript\" src=\"%scommons.js\"></script>
151 <script type=\"text/javascript\" src=\"%sxynt-streaming-ifra.js\"></script>
152 <script type=\"text/javascript\">
153 var xynt_streaming = \"ready\";", $base, $base);
154         $ret .= sprintf("
155 window.onload = function () { try { if (xynt_streaming != \"ready\") { xynt_streaming.reload(); } } catch(e) { /* console.log(\"catcha\"); */ } };
156 </script>
157 </head>
158 <body>");
159         $ret .= sprintf("<!-- \n%s -->\n", $init_string);
160         $ret .= sprintf("<script id='hs%d' type='text/javascript'><!--
161 push(\"%s\");
162 // -->
163 </script>", 0, escpush($blockerr) );
164         return ($ret);
165     }
166
167     function chunk($step, $cont)
168     {
169         if ($cont == NULL) {
170             return sprintf("<script id='hs%d' type='text/javascript'><!--
171 push(null);\n// -->\n</script>", $step);
172         }
173         else {
174             return sprintf("<script id='hs%d' type='text/javascript'><!--
175 push(\"%s\");\n// -->\n</script>", $step, escpush($cont) );
176         }
177     }
178
179     function is_chunked()
180     {
181         return TRUE;
182     }
183 }
184
185 class Transport_htmlfile extends Transport_iframe {
186 }
187
188 class Transport {
189     function Transport()
190     {
191     }
192
193     static function create($transp)
194     {
195         if ($transp == 'xhr') {
196             return new Transport_xhr();
197         }
198         else if ($transp == 'htmlfile') {
199             return new Transport_htmlfile();
200         }
201         else  {
202             return new Transport_iframe();
203         }
204     }
205     static function gettype($transp)
206     {
207         if ($transp == 'xhr' || $transp == 'htmlfile') {
208             return "Transport_".$transp;
209         }
210         else {
211             return 'Transport_iframe';
212         }
213     }
214 }
215 ?>