reload global vars refactored and add a script to speed up modifications
[brisk.git] / web / Obj / sac-a-push.phh
1 <?php
2 /*
3  *  brisk - spush/sac-a-push.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 define('SITE_PREFIX', '/brisk/');
26 define('SITE_PREFIX_LEN', 7);
27
28
29 function global_dump()
30 {
31     GLOBAL $G_alarm_passwd, $G_black_list, $G_btrace_pref_sub, $G_dbauth;
32     GLOBAL $G_dbpfx, $G_donors_all, $G_donors_cur, $G_is_local, $G_lang;
33     GLOBAL $G_poll_entries, $G_poll_name, $G_poll_title, $G_proxy_white_list;
34     GLOBAL $G_room_roadmap, $G_shutdown, $G_sidebanner, $G_sidebanner2;
35     GLOBAL $G_splash_content, $G_splash_contents, $G_splash_cont_idx;
36     GLOBAL $G_splash_h, $G_splash_idx, $G_splash_interval, $G_splash_timeout;
37     GLOBAL $G_splash_w, $G_topbanner, $G_with_donors, $G_with_poll;
38     GLOBAL $G_with_sidebanner, $G_with_sidebanner2, $G_with_splash;
39     GLOBAL $G_with_topbanner;
40
41     fprintf(STDERR, "G_alarm_passwd = [%s]\n", print_r($G_alarm_passwd, TRUE));
42     fprintf(STDERR, "G_black_list = [%s]\n", print_r($G_black_list, TRUE));
43     fprintf(STDERR, "G_btrace_pref_sub = [%s]\n", print_r($G_btrace_pref_sub, TRUE));
44     fprintf(STDERR, "G_dbauth = [%s]\n", print_r($G_dbauth, TRUE));
45     fprintf(STDERR, "G_dbpfx = [%s]\n", print_r($G_dbpfx, TRUE));
46     fprintf(STDERR, "G_donors_all = [%s]\n", print_r($G_donors_all, TRUE));
47     fprintf(STDERR, "G_donors_cur = [%s]\n", print_r($G_donors_cur, TRUE));
48     fprintf(STDERR, "G_is_local = [%s]\n", print_r($G_is_local, TRUE));
49     fprintf(STDERR, "G_lang = [%s]\n", print_r($G_lang, TRUE));
50     fprintf(STDERR, "G_poll_entries = [%s]\n", print_r($G_poll_entries, TRUE));
51     fprintf(STDERR, "G_poll_name = [%s]\n", print_r($G_poll_name, TRUE));
52     fprintf(STDERR, "G_poll_title = [%s]\n", print_r($G_poll_title, TRUE));
53     fprintf(STDERR, "G_proxy_white_list = [%s]\n", print_r($G_proxy_white_list, TRUE));
54     fprintf(STDERR, "G_room_roadmap = [%s]\n", print_r($G_room_roadmap, TRUE));
55     fprintf(STDERR, "G_shutdown = [%s]\n", print_r($G_shutdown, TRUE));
56     fprintf(STDERR, "G_sidebanner = [%s]\n", print_r($G_sidebanner, TRUE));
57     fprintf(STDERR, "G_sidebanner2 = [%s]\n", print_r($G_sidebanner2, TRUE));
58     fprintf(STDERR, "G_splash_content = [%s]\n", print_r($G_splash_content, TRUE));
59     fprintf(STDERR, "G_splash_contents = [%s]\n", print_r($G_splash_contents, TRUE));
60     fprintf(STDERR, "G_splash_cont_idx = [%s]\n", print_r($G_splash_cont_idx, TRUE));
61     fprintf(STDERR, "G_splash_h = [%s]\n", print_r($G_splash_h, TRUE));
62     fprintf(STDERR, "G_splash_idx = [%s]\n", print_r($G_splash_idx, TRUE));
63     fprintf(STDERR, "G_splash_interval = [%s]\n", print_r($G_splash_interval, TRUE));
64     fprintf(STDERR, "G_splash_timeout = [%s]\n", print_r($G_splash_timeout, TRUE));
65     fprintf(STDERR, "G_splash_w = [%s]\n", print_r($G_splash_w, TRUE));
66     fprintf(STDERR, "G_topbanner = [%s]\n", print_r($G_topbanner, TRUE));
67     fprintf(STDERR, "G_with_donors = [%s]\n", print_r($G_with_donors, TRUE));
68     fprintf(STDERR, "G_with_poll = [%s]\n", print_r($G_with_poll, TRUE));
69     fprintf(STDERR, "G_with_sidebanner = [%s]\n", print_r($G_with_sidebanner, TRUE));
70     fprintf(STDERR, "G_with_sidebanner2 = [%s]\n", print_r($G_with_sidebanner2, TRUE));
71     fprintf(STDERR, "G_with_splash = [%s]\n", print_r($G_with_splash, TRUE));
72     fprintf(STDERR, "G_with_topbanner = [%s]\n", print_r($G_with_topbanner, TRUE));
73 }
74
75 function spu_process_info($stream_info, $method, &$header, &$get, &$post, &$cookie)
76 {
77     $check_post = FALSE;
78     $header = array();
79     $get = array();
80     $post = array();
81     foreach(preg_split("/(\r?\n)/", $stream_info) as $line) {
82         // printf("LINE: [%s]\n", $line);
83         if ($check_post) {
84             if (!isset($header['The-Request'])) {
85                 return FALSE;
86             }
87             $req = explode(" ", $header['The-Request']);
88             $method = $req[0];
89
90             if (isset($header['Cookie'])) {
91                 $cookies = explode(";", $header['Cookie']);
92                 for ($i = 0 ; $i < count($cookies) ; $i++) {
93                     $nameval = explode("=", trim($cookies[$i]));
94                     if (count($nameval) != 2) {
95                         printf("WARNING: malformat cookie element [%s]\n", $cookies[$i]);
96                         continue;
97                     }
98                     $cookie[$nameval[0]] = urldecode($nameval[1]);
99                 }
100             }
101             // GET params management
102             $get_vars = explode('?', $req[1], 2);
103             $path =   $get_vars[0];
104             if (count($get_vars) > 1) {
105                 $a = explode('&', $get_vars[1]);
106                 printf("A COUNT: [%s] %d\n", $a[0], count($a));
107                 for ($i = 0 ; $i < count($a) ; $i++) {
108                     $b = explode('=', $a[$i]);
109                     $get[$b[0]] = urldecode($b[1]);
110                 }
111             }
112             // POST params management
113             if ($req[0] == 'POST') {
114                 if ($header['Content-Type'] != 'application/x-www-form-urlencoded' 
115                     || !isset($header['Content-Length'])) {
116                     return FALSE;
117                 }
118                 $post_len = mb_strlen($line, "latin1");
119                 $a = explode('&', $line);
120                 for ($i = 0 ; $i < count($a) ; $i++) {
121                     $b = explode('=', $a[$i]);
122                     $post[$b[0]] = urldecode($b[1]);
123                 }
124                 printf("INFO: postlen: %d\n", $post_len);
125             }
126             break;
127         }
128         if ($line == "") {
129             $check_post = TRUE;
130             continue;
131         }
132         $split = explode(":", $line, 2);
133         $header[$split[0]] = $split[1];        
134     }
135     return $path;
136 }
137
138 function gpcs_var($name, $get, $post, $cookie)
139 {
140     if (isset($GLOBALS[$name])) 
141         return FALSE;
142     else if (isset($cookie[$name])) 
143         return ($cookie[$name]);
144     else if (isset($post[$name])) 
145         return ($post[$name]);
146     else if (isset($get[$name])) 
147         return ($get[$name]);
148
149     return FALSE;
150 }
151
152 function headers_render($header, $len)
153 {
154     $cookies = "";
155
156     if (isset($header['cookies'])) {
157         $cookies = $header['cookies']->render();
158         unset($header['cookies']);
159     }
160     if (isset($header['Location'])) {
161         $s = sprintf("HTTP/1.1 302 OK\r\n%sLocation: %s\r\n", $cookies, $header['Location']);
162     }
163     else {
164         $s = "HTTP/1.1 200 OK\r\n";
165
166         if (!isset($header['Date']))
167             $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
168         if (!isset($header['Connection']))
169             $s .= "Connection: close\r\n";
170         if (!isset($header['Content-Type']))
171             $s .= "Content-Type: text/html\r\n";
172         foreach($header as $key => $value) {
173             $s .= sprintf("%s: %s\r\n", $key, $value);
174         }
175         if ($len >= 0) {
176             $s .= sprintf("Content-Length: %d\r\n", $len);
177         }
178         else {
179             $s .= "Cache-Control: no-cache, must-revalidate\r\n";
180             $s .= "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n";
181             if (!isset($header['Content-Encoding'])) {
182                 $s .= "Content-Encoding: chunked\r\n";
183             }
184             $s .= "Transfer-Encoding: chunked\r\n";
185         }
186         $s .= $cookies;
187     }
188     $s .= "\r\n";
189
190     return ($s);
191 }
192
193 /*
194  *  Caching system using ob php system to cache old style pages
195  *  to a var and than send it with more calm
196  */
197
198 function shutta()
199 {
200   log_rd2("SHUTTA [".connection_status()."] !");
201 }
202
203 register_shutdown_function('shutta');
204
205 /*
206  *  MAIN
207  */
208
209 function chunked_content($zls, $content)
210 {
211     if ($zls) {
212         $cont_comp = $zls->compress_chunk($content);
213     }
214     else {
215         $cont_comp = $content;
216     }
217     $cont_comp_l = mb_strlen($cont_comp, "ASCII");
218     printf("CHUNK: [%s]\n", $content);
219
220     return (sprintf("%X\r\n", $cont_comp_l).$cont_comp."\r\n");
221 }
222
223 function chunked_fini()
224 {
225     return sprintf("0\r\n");
226 }
227
228 function get_encoding($header)
229 {
230     $enc = "plain";
231     if (isset($header['Accept-Encoding'])) {
232         $acc = explode(',', $header['Accept-Encoding']);
233
234         if (array_search('gzip', $acc) !== FALSE) {
235             $enc = 'gzip';
236         }
237         else if (array_search('deflate', $acc) !== FALSE) {
238             $enc = 'deflate';
239         }
240     }
241
242     return ($enc);
243 }
244
245 class Cookie {
246     var $attr;
247     // Set-Cookie: reg_fb_gate=deleted; Expires=Thu, 01-Jan-1970 00:00:01 GMT; Path=/; Domain=.foo.com; HttpOnly
248     // string $name  [, string $value  [, int $expire = 0  [, string $path  [, string $domain  [, bool $secure = false  [, bool $httponly = false  ]]]]]] )
249     function Cookie()
250     {
251         $this->attr = array();
252     }
253
254     static function create($name)
255     {
256         $thiz = new Cookie();
257
258         $thiz->attr[$name] = "";
259
260         $argc = func_num_args();
261         for ($i = 1 ; $i < $argc ; $i++) {
262             $arg = func_get_arg($i);
263             switch ($i) {
264             case 1:
265                 $thiz->attr[$name] = urlencode($arg);
266                 break;
267             case 2:
268                 $thiz->attr['Expires'] = gmdate('D, d M Y H:i:s \G\M\T', $arg); // RFC 1211 format
269                 break;
270             case 3:
271                 $thiz->attr['Path'] = $arg;
272                 break;
273             case 4:
274                 $thiz->attr['Domain'] = $arg;
275                 break;
276             case 5:
277                 if ($arg == TRUE) {
278                     $thiz->attr['Secure'] = NULL;
279                 }
280                 break;
281             case 6:
282                 if ($arg == TRUE) {
283                     $thiz->attr['HttpOnly'] = NULL;
284                 }
285                 break;
286             default:
287                 return FALSE;
288             }
289         }
290
291         return $thiz;
292     }
293
294     function render()
295     {
296         $r = "Set-Cookie: ";
297         $isfirst = TRUE;
298
299         foreach ($this->attr as $k => $v) {
300             if ($v == NULL) {
301                 $r .= sprintf("%s%s", ($isfirst ? "" : "; "), $k);
302             }
303             else {
304                 $r .= sprintf("%s%s=%s", ($isfirst ? "" : "; "), $k, $v);
305             }
306             $isfirst = FALSE;
307         }
308         $r .= "\r\n";
309
310         return $r;
311     }
312 }
313
314 class Cookies {
315     var $cookies;
316
317     function Cookies()
318     {
319         $this->cookies = array();
320     }
321
322     function add($name)
323     {
324         if (($cookie = call_user_func_array("Cookie::create", func_get_args())) == FALSE)
325             return (FALSE);
326
327         array_push($this->cookies, $cookie);
328
329         return (TRUE);
330     }
331
332     function render()
333     {
334         $r = "";
335         foreach ($this->cookies as $cookie) {
336             $r .= $cookie->render();
337         }
338
339         return ($r);
340     }
341 }
342
343
344 class Sac_a_push {
345     static $fixed_fd = 2;
346     
347     var $file_socket;
348     var $unix_socket;
349     var $socks;
350     var $s2u;
351     var $pages_flush;
352
353     var $list;
354     var $in;
355
356     var $debug;
357     var $blocking_mode;
358
359     var $app;
360
361     var $curtime;
362
363     var $rndstr;
364     var $main_loop;
365
366     function Sac_a_push()
367     {
368     }
369
370     // Sac_a_push::create("/tmp/brisk.sock", 0, 0
371
372     static function create(&$app, $sockname, $debug, $blocking_mode)
373     {        
374         $thiz = new Sac_a_push();
375         
376         $thiz->app = $app;
377         $thiz->file_socket = $sockname;
378         $thiz->unix_socket = "unix://$sockname";
379         $thiz->debug = $debug;
380         $thiz->socks = array();
381         $thiz->s2u  = array();
382         $thiz->pages_flush = array();
383
384         $thiz->blocking_mode = 0; // 0 for non-blocking
385
386         $thiz->rndstr = "";
387         for ($i = 0 ; $i < 4096 ; $i++) {
388             if (($i % 128) == 0)
389                 $thiz->rndstr .= " ";
390             else
391                 $thiz->rndstr .= chr(mt_rand(65, 90));
392         }
393         
394         if (file_exists($thiz->file_socket)) {
395             unlink($thiz->file_socket);
396         }
397     
398         $old_umask = umask(0);
399         if (($thiz->list = stream_socket_server($thiz->unix_socket, $err, $errs)) === FALSE) {
400             return (FALSE);
401         }
402         umask($old_umask);
403         stream_set_blocking($thiz->list, $thiz->blocking_mode); # Set the stream to non-blocking
404
405         if (($thiz->in = fopen("php://stdin", "r")) === FALSE) {
406             return(FALSE);
407         }
408
409         $thiz->main_loop = FALSE;
410
411         return ($thiz);
412     }
413
414     function socks_set($sock, $user)
415     {
416         $id = intval($sock);
417
418         $this->s2u[$id]   = $user;
419         $this->socks[$id] = $sock;
420     }
421
422     function socks_unset($sock)
423     {
424         $id = intval($sock);
425
426         unset($this->s2u[$id]);
427         unset($this->socks[$id]);
428     }
429
430     function pgflush_try_add($enc, &$new_socket, $tout, $header_out, $content)
431     {
432         $pgflush = new PageFlush($new_socket, $enc, $this->curtime, $tout, $header_out, $content);
433
434         if ($pgflush->try_flush($this->curtime) == FALSE) {
435             // Add $pgflush to the pgflush array
436             $this->pgflush_add($pgflush);
437         }
438     }
439
440     function pgflush_add($pgflush)
441     {
442         array_push($this->pages_flush, $pgflush);
443     }
444
445     function garbage_manager($force)
446     {
447         $this->app->garbage_manager($force);
448
449         foreach ($this->socks as $k => $sock) {
450             if ($this->s2u[intval($sock)]->sess == '') {
451                 if ($this->s2u[intval($sock)]->rd_socket_get() != NULL) {
452                     $this->s2u[intval($sock)]->rd_socket_set(NULL);
453                 }
454                 unset($this->socks[intval($sock)]);
455                 unset($this->s2u[intval($sock)]);
456                 fclose($sock);
457                 printf("CLOSE ON GARBAGE MANAGER\n");
458             }
459         }
460     }
461
462     function run()
463     {
464         GLOBAL $DOCUMENT_ROOT, $HTTP_HOST;
465
466         GLOBAL $G_alarm_passwd, $G_black_list, $G_btrace_pref_sub, $G_dbauth;
467         GLOBAL $G_dbpfx, $G_donors_all, $G_donors_cur, $G_is_local, $G_lang;
468         GLOBAL $G_poll_entries, $G_poll_name, $G_poll_title, $G_proxy_white_list;
469         GLOBAL $G_room_roadmap, $G_shutdown, $G_sidebanner, $G_sidebanner2;
470         GLOBAL $G_splash_content, $G_splash_contents, $G_splash_cont_idx;
471         GLOBAL $G_splash_h, $G_splash_idx, $G_splash_interval, $G_splash_timeout;
472         GLOBAL $G_splash_w, $G_topbanner, $G_with_donors, $G_with_poll;
473         GLOBAL $G_with_sidebanner, $G_with_sidebanner2, $G_with_splash;
474         GLOBAL $G_with_topbanner;
475
476         if ($this->main_loop) {
477             return (FALSE);
478         }
479         
480         $this->main_loop = TRUE;
481         
482         while ($this->main_loop) {
483             $this->curtime = time();
484             printf("IN LOOP: Current opened: %d  pages_flush: %d - ", count($this->socks), count($this->pages_flush));
485             
486             /* Prepare the read array */
487             /* // when we manage it ... */
488             /* if ($shutdown)  */
489             /*     $read   = array_merge(array("$in" => $in), $socks); */
490             /* else */
491             $read   = array_merge(array(intval($this->list) => $this->list, intval($this->in) => $this->in),
492                                   $this->socks);
493             
494             if ($this->debug > 1) {
495                 printf("PRE_SELECT\n");
496                 print_r($read);
497             }
498             $write  = NULL;
499             $except = NULL;
500             $num_changed_sockets = stream_select($read, $write, $except, 0, 500000);
501         
502             if ($num_changed_sockets == 0) {
503                 printf(" no data in 5 secs, splash [%d]\n", $G_with_splash);
504             } 
505             else if ($num_changed_sockets > 0) {
506                 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
507                 if ($this->debug > 1) {
508                     print_r($read);
509                 }
510                 /* At least at one of the sockets something interesting happened */
511                 foreach ($read as $i => $sock) {
512                     /* is_resource check is required because there is the possibility that
513                        during new request an old connection is closed */
514                     if (!is_resource($sock)) {
515                         continue;
516                     }
517                     if ($sock === $this->list) {
518                         printf("NUOVA CONNEX\n");
519                         if (($new_unix = stream_socket_accept($this->list)) == FALSE) {
520                             printf("SOCKET_ACCEPT FAILED\n");
521                             continue;
522                         }
523                         $stream_info = "";
524                         $method      = "";
525                         $get         = array();
526                         $post        = array();
527                         $cookie      = array();
528                         if (($new_socket = ancillary_getstream($new_unix, $stream_info)) !== FALSE) {
529                             printf("NEW_SOCKET: %d\n", intval($new_socket));
530                             stream_set_blocking($new_socket, $this->blocking_mode); // Set the stream to non-blocking
531                             printf("RECEIVED HEADER:\n%s", $stream_info);
532                             $path = spu_process_info($stream_info, $method, $header, $get, $post, $cookie);
533                             printf("PATH: [%s]\n", $path);
534                             printf("M: %s\nHEADER:\n", $method);
535                             print_r($header);
536                             printf("GET:\n");
537                             print_r($get);
538                             printf("POST:\n");
539                             print_r($post);
540                             printf("COOKIE:\n");
541                             print_r($cookie);
542                             $addr = stream_socket_get_name($new_socket, TRUE);
543                             $header_out = array();
544
545                             $subs = SITE_PREFIX."briskin5/";
546                             $subs_l = strlen($subs);
547                             $rret = FALSE;
548                             if (!strncmp($path, SITE_PREFIX, SITE_PREFIX_LEN)) {
549                                 $rret = $this->app->request_mgr($this, $header, $header_out, $new_socket, substr($path, SITE_PREFIX_LEN), $addr, $get, $post, $cookie);
550                             }
551                             if ($rret == FALSE) { 
552                                 // FIXME: manage 404 !!!
553                                 printf("TODO: fix unknown page\n");
554                             }
555                             printf("number of sockets after %d\n", count($this->socks));
556                         }
557                         else {
558                             printf("WARNING: ancillary_getstream failed\n");
559                         }
560                     }
561                     else {
562                         $buf = fread($sock, 512);
563                         // if socket is closed
564                         if ($buf == FALSE || strlen($buf) == 0) {
565                             if ($buf == FALSE) {
566                                 printf("ERROR READING\n");
567                             }
568                             if ($sock === $this->list) {
569                                 printf("Arrivati %d bytes da list\n", strlen($buf));
570                                 exit(21);
571                             }
572                             else if ($sock === $this->in) {
573                                 printf("Arrivati %d bytes da stdin\n", strlen($buf));
574                                 exit(22);
575                             }
576                             else {
577                                 // $user_a[$s2u[intval($sock)]]->disable();
578                                 if ($this->s2u[intval($sock)]->rd_socket_get() != NULL) {
579                                     $this->s2u[intval($sock)]->rd_socket_set(NULL);
580                                 }
581                                 unset($this->socks[intval($sock)]);
582                                 unset($this->s2u[intval($sock)]);
583                             }
584                             fclose($sock);
585                             printf("CLOSE ON READ\n");
586
587                             if ($this->debug > 1) {
588                                 printf("post unset\n");
589                                 print_r($this->socks);
590                             }
591                         }
592                         else {
593                             if ($this->debug > 1) {
594                                 print_r($read);
595                             }
596                             if ($sock === $this->list) {
597                                 printf("Arrivati %d bytes da list\n", strlen($buf));
598                             }
599                             else if ($sock === $this->in) {
600                                 printf("Arrivati %d bytes da stdin\n", strlen($buf));
601                                 $line = trim($buf);
602                                 if ($line == "reload") {
603                                     require("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
604
605                                     global_dump();
606                                 }
607                                 else if ($line == "shutdown") {
608                                     if ($this->app->dump_data()) {
609                                         exit(0);
610                                     }
611                                     else {
612                                         exit(1);
613                                     }
614                                 }
615                             }
616                             else {
617                                 $key = array_search("$sock", $this->socks);
618                                 printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
619                             }
620                         }
621                     }
622                 }
623             }
624
625             $this->garbage_manager(FALSE);
626
627             /* manage unfinished pages */
628             foreach ($this->pages_flush as $k => $pgflush) {
629                 if ($pgflush->try_flush($this->curtime) == TRUE) {
630                     unset($this->pages_flush[$k]);
631                 }
632             }
633             
634             /*
635                $response:                        raw stream data not sent
636                $content:                         html consistent data (<script bla bla>)
637                $user->stream_keepalive($w_ping)  ping srv->cli OR srv->cli + cli->srv if $w_ping == TRUE
638             */
639
640             /* manage open streaming */
641             foreach ($this->socks as $k => $sock) {
642                 if (isset($this->s2u[intval($sock)])) {
643                     $user = $this->s2u[intval($sock)];
644                     $response = $user->rd_cache_get();
645                     $do_ping = FALSE;
646                     if (($this->curtime - $user->lacc) > (EXPIRE_TIME_RD / 3)) {
647                         $do_ping = TRUE;
648                     }
649                     else {
650                         $user->ping_req = FALSE;
651                     }
652
653                     if ($response == "") {
654                         $content = "";
655                         $user->stream_main($content, $get, $post, $cookie);
656                         printf("[%s] [%d] [%d]\n", $user->name, $user->lacc, $this->curtime);
657                         if ($do_ping && $user->ping_req == FALSE) {
658                             $content .= $user->stream_keepalive(TRUE);
659                             $user->ping_req = TRUE;
660                         }
661                         else if ($content == "" && $user->rd_kalive_is_expired($this->curtime)) {
662                             $content = $user->stream_keepalive(FALSE);
663                         }
664                         if ($content != "") {
665                             $response = chunked_content($user->rd_zls_get(), $content);
666                         }
667                     }
668                     
669                     if ($response != "") {
670                         // echo "SPIA: [".substr($response, 0, 60)."...]\n";
671                         echo "SPIA: [".$response."]\n";
672                         $response_l = mb_strlen($response, "ASCII");
673                         $wret = @fwrite($sock, $response);
674                         if ($wret < $response_l) {
675                             printf("TROUBLE WITH FWRITE: %d\n", $wret);
676                             $user->rd_cache_set(mb_substr($response, $wret, $response_l - $wret, "ASCII"));
677                         }
678                         else {
679                             $user->rd_cache_set("");
680                         }
681                         fflush($sock);
682                         $user->rd_kalive_reset($this->curtime);
683                     }
684                     
685                     // close socket after a while to prevent client memory consumption
686                     if ($user->rd_endtime_is_expired($this->curtime)) {
687                         if ($this->s2u[intval($sock)]->rd_socket_get() != NULL) {
688                             $this->s2u[intval($sock)]->rd_socket_set(NULL);
689                         }
690                         unset($this->socks[intval($sock)]);
691                         unset($this->s2u[intval($sock)]);
692                         fclose($sock);
693                         printf("CLOSE ON LOOP\n");
694                     }
695                 }
696             }  // foreach ($this->socks...
697             printf("\n");
698         }  // while (...
699     }  // function run(...
700 }
701
702 ?>