3 * brisk - Obj/sac-a-push.phh
5 * Copyright (C) 2012 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
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.
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.
25 define('SITE_PREFIX', '/brisk/');
26 define('SITE_PREFIX_LEN', 7);
30 function global_dump()
32 GLOBAL $G_alarm_passwd, $G_black_list, $G_btrace_pref_sub, $G_dbauth;
33 GLOBAL $G_dbpfx, $G_donors_all, $G_donors_cur, $G_is_local, $G_lang;
34 GLOBAL $G_poll_entries, $G_poll_name, $G_poll_title, $G_proxy_white_list;
35 GLOBAL $G_room_roadmap, $G_shutdown, $G_sidebanner, $G_sidebanner2;
36 GLOBAL $G_splash_content, $G_splash_contents, $G_splash_cont_idx;
37 GLOBAL $G_splash_h, $G_splash_idx, $G_splash_interval, $G_splash_timeout;
38 GLOBAL $G_splash_w, $G_topbanner, $G_with_donors, $G_with_poll;
39 GLOBAL $G_with_sidebanner, $G_with_sidebanner2, $G_with_splash;
40 GLOBAL $G_with_topbanner;
42 fprintf(STDERR, "G_alarm_passwd = [%s]\n", print_r($G_alarm_passwd, TRUE));
43 fprintf(STDERR, "G_black_list = [%s]\n", print_r($G_black_list, TRUE));
44 fprintf(STDERR, "G_btrace_pref_sub = [%s]\n", print_r($G_btrace_pref_sub, TRUE));
45 fprintf(STDERR, "G_dbauth = [%s]\n", print_r($G_dbauth, TRUE));
46 fprintf(STDERR, "G_dbpfx = [%s]\n", print_r($G_dbpfx, TRUE));
47 fprintf(STDERR, "G_donors_all = [%s]\n", print_r($G_donors_all, TRUE));
48 fprintf(STDERR, "G_donors_cur = [%s]\n", print_r($G_donors_cur, TRUE));
49 fprintf(STDERR, "G_is_local = [%s]\n", print_r($G_is_local, TRUE));
50 fprintf(STDERR, "G_lang = [%s]\n", print_r($G_lang, TRUE));
51 fprintf(STDERR, "G_poll_entries = [%s]\n", print_r($G_poll_entries, TRUE));
52 fprintf(STDERR, "G_poll_name = [%s]\n", print_r($G_poll_name, TRUE));
53 fprintf(STDERR, "G_poll_title = [%s]\n", print_r($G_poll_title, TRUE));
54 fprintf(STDERR, "G_proxy_white_list = [%s]\n", print_r($G_proxy_white_list, TRUE));
55 fprintf(STDERR, "G_room_roadmap = [%s]\n", print_r($G_room_roadmap, TRUE));
56 fprintf(STDERR, "G_shutdown = [%s]\n", print_r($G_shutdown, TRUE));
57 fprintf(STDERR, "G_sidebanner = [%s]\n", print_r($G_sidebanner, TRUE));
58 fprintf(STDERR, "G_sidebanner2 = [%s]\n", print_r($G_sidebanner2, TRUE));
59 fprintf(STDERR, "G_splash_content = [%s]\n", print_r($G_splash_content, TRUE));
60 fprintf(STDERR, "G_splash_contents = [%s]\n", print_r($G_splash_contents, TRUE));
61 fprintf(STDERR, "G_splash_cont_idx = [%s]\n", print_r($G_splash_cont_idx, TRUE));
62 fprintf(STDERR, "G_splash_h = [%s]\n", print_r($G_splash_h, TRUE));
63 fprintf(STDERR, "G_splash_idx = [%s]\n", print_r($G_splash_idx, TRUE));
64 fprintf(STDERR, "G_splash_interval = [%s]\n", print_r($G_splash_interval, TRUE));
65 fprintf(STDERR, "G_splash_timeout = [%s]\n", print_r($G_splash_timeout, TRUE));
66 fprintf(STDERR, "G_splash_w = [%s]\n", print_r($G_splash_w, TRUE));
67 fprintf(STDERR, "G_topbanner = [%s]\n", print_r($G_topbanner, TRUE));
68 fprintf(STDERR, "G_with_donors = [%s]\n", print_r($G_with_donors, TRUE));
69 fprintf(STDERR, "G_with_poll = [%s]\n", print_r($G_with_poll, TRUE));
70 fprintf(STDERR, "G_with_sidebanner = [%s]\n", print_r($G_with_sidebanner, TRUE));
71 fprintf(STDERR, "G_with_sidebanner2 = [%s]\n", print_r($G_with_sidebanner2, TRUE));
72 fprintf(STDERR, "G_with_splash = [%s]\n", print_r($G_with_splash, TRUE));
73 fprintf(STDERR, "G_with_topbanner = [%s]\n", print_r($G_with_topbanner, TRUE));
79 $fname = LEGAL_PATH."/brisk.pid";
81 if (file_exists($fname)) {
82 log_crit("WARN: brisk.pid already exists");
84 file_put_contents($fname, sprintf("%d\n", $pid));
89 $fname = LEGAL_PATH."/brisk.pid";
91 if (file_exists($fname)) {
96 function post_manage(&$post, $line)
98 $a = explode('&', $line);
99 for ($i = 0 ; $i < count($a) ; $i++) {
100 $b = explode('=', $a[$i]);
103 $post[$b[0]] = urldecode($b[1]);
112 function spu_process_info($stream_info, &$method, &$header, &$get, &$post, &$cookie, &$rest, &$cont)
119 foreach(preg_split("/(\r?\n)/", $stream_info) as $line) {
120 printf("LINE: [%s]\n", $line);
122 if (!isset($header['The-Request'])) {
125 $req = explode(" ", $header['The-Request']);
128 if (isset($header['Cookie'])) {
129 $cookies = explode(";", $header['Cookie']);
130 for ($i = 0 ; $i < count($cookies) ; $i++) {
131 $nameval = explode("=", trim($cookies[$i]));
132 if (count($nameval) != 2) {
133 printf("WARNING: malformat cookie element [%s]\n", $cookies[$i]);
136 $cookie[$nameval[0]] = urldecode($nameval[1]);
139 // GET params management
140 $get_vars = explode('?', $req[1], 2);
141 $path = $get_vars[0];
142 if (count($get_vars) > 1) {
143 $a = explode('&', $get_vars[1]);
144 printf("A COUNT: [%s] %d\n", $a[0], count($a));
145 for ($i = 0 ; $i < count($a) ; $i++) {
146 $b = explode('=', $a[$i]);
147 $get[$b[0]] = urldecode($b[1]);
150 // POST params management
151 if ($req[0] == 'POST') {
152 $conttype_all = explode(";", $header['Content-Type']);
153 $header['Content-Type'] = $conttype_all[0];
154 // $path_all[1-] other things like charset and so on
156 // if (content-type is wrong || content-length isn't set)
159 if ($header['Content-Type'] != 'application/x-www-form-urlencoded'
160 || !isset($header['Content-Length'])) {
163 $post_len = mb_strlen($line, "ASCII");
164 printf("INFO: postlen: %d\n", $post_len);
165 $rest = (int)($header['Content-Length']) - $post_len;
168 post_manage($post, $line);
180 $split = explode(":", $line, 2);
181 $hea_id = trim(mb_convert_case($split[0], MB_CASE_TITLE, 'UTF-8'));
182 $header[$hea_id] = $split[1];
187 function gpcs_var($name, $get, $post, $cookie)
189 if (isset($GLOBALS[$name]))
191 else if (isset($cookie[$name]))
192 return ($cookie[$name]);
193 else if (isset($post[$name]))
194 return ($post[$name]);
195 else if (isset($get[$name]))
196 return ($get[$name]);
201 function headers_render($header, $len)
205 if (isset($header['cookies'])) {
206 $cookies = $header['cookies']->render();
207 unset($header['cookies']);
209 if (isset($header['Location'])) {
210 $s = sprintf("HTTP/1.1 302 OK\r\n%sLocation: %s\r\n", $cookies, $header['Location']);
212 else if (isset($header['HTTP-Response'])) {
213 $s = sprintf("HTTP/1.1 %s\r\n", $header['HTTP-Response']);
216 $s = "HTTP/1.1 200 OK\r\n";
218 if (!isset($header['Date']))
219 $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
220 if (!isset($header['Connection']))
221 $s .= "Connection: close\r\n";
222 if (!isset($header['Content-Type']))
223 $s .= "Content-Type: text/html\r\n";
224 foreach($header as $key => $value) {
225 $s .= sprintf("%s: %s\r\n", $key, $value);
228 $s .= sprintf("Content-Length: %d\r\n", $len);
231 $s .= "Cache-Control: no-cache, must-revalidate\r\n";
232 $s .= "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n";
233 if (!isset($header['Content-Encoding'])) {
234 $s .= "Content-Encoding: chunked\r\n";
236 $s .= "Transfer-Encoding: chunked\r\n";
246 * Caching system using ob php system to cache old style pages
247 * to a var and than send it with more calm
252 log_rd2("SHUTTA [".connection_status()."] !");
255 register_shutdown_function('shutta');
261 function chunked_content($zls, $content)
264 $cont_comp = $zls->compress_chunk($content);
267 $cont_comp = $content;
269 $cont_comp_l = mb_strlen($cont_comp, "ASCII");
270 // printf("CHUNK: [%s]\n", $content);
272 return (sprintf("%X\r\n", $cont_comp_l).$cont_comp."\r\n");
275 function chunked_fini()
277 return sprintf("0\r\n");
280 function get_encoding($header)
283 if (isset($header['Accept-Encoding'])) {
284 $acc = explode(',', $header['Accept-Encoding']);
286 if (array_search('gzip', $acc) !== FALSE) {
289 else if (array_search('deflate', $acc) !== FALSE) {
299 // Set-Cookie: reg_fb_gate=deleted; Expires=Thu, 01-Jan-1970 00:00:01 GMT; Path=/; Domain=.foo.com; HttpOnly
300 // string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
303 $this->attr = array();
306 static function create($name)
308 $thiz = new Cookie();
310 $thiz->attr[$name] = "";
312 $argc = func_num_args();
313 for ($i = 1 ; $i < $argc ; $i++) {
314 $arg = func_get_arg($i);
317 $thiz->attr[$name] = urlencode($arg);
320 $thiz->attr['Expires'] = gmdate('D, d M Y H:i:s \G\M\T', $arg); // RFC 1211 format
323 $thiz->attr['Path'] = $arg;
326 $thiz->attr['Domain'] = $arg;
330 $thiz->attr['Secure'] = NULL;
335 $thiz->attr['HttpOnly'] = NULL;
351 foreach ($this->attr as $k => $v) {
353 $r .= sprintf("%s%s", ($isfirst ? "" : "; "), $k);
356 $r .= sprintf("%s%s=%s", ($isfirst ? "" : "; "), $k, $v);
371 $this->cookies = array();
376 if (($cookie = call_user_func_array("Cookie::create", func_get_args())) == FALSE)
379 array_push($this->cookies, $cookie);
387 foreach ($this->cookies as $cookie) {
388 $r .= $cookie->render();
397 // maybe fixed_fd is unuseful
398 static $fixed_fd = 3;
399 static $cnt_master = NULL;
400 static $cnt_slave = NULL;
405 var $s2u; // user associated with input socket
406 var $s2p; // pending page associated with input socket
422 function Sac_a_push()
426 function sig_handler($sig)
433 if (static::$cnt_master != NULL) {
434 fwrite(static::$cnt_master, "\nshutdown\n");
435 fflush(static::$cnt_master);
442 if (static::$cnt_master != NULL) {
443 fwrite(static::$cnt_master, "\nreload\n");
444 fflush(static::$cnt_master);
450 static function create(&$app, $sockname, $debug, $blocking_mode)
452 $thiz = new Sac_a_push();
455 $thiz->file_socket = $sockname;
456 $thiz->unix_socket = "unix://$sockname";
457 $thiz->debug = $debug;
458 $thiz->socks = array();
459 $thiz->s2u = array();
460 $thiz->s2p = array();
461 $thiz->pending_pages = array();
463 // create a couple of sockets for control management
464 if (($sockpair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM,
465 STREAM_IPPROTO_IP)) == FALSE) {
468 static::$cnt_master = $sockpair[0];
469 static::$cnt_slave = $sockpair[1];
471 pcntl_signal(SIGTERM, array("Sac_a_push", "sig_handler"));
472 pcntl_signal(SIGINT, array("Sac_a_push", "sig_handler"));
473 pcntl_signal(SIGHUP, array("Sac_a_push", "sig_handler"));
475 $thiz->blocking_mode = 0; // 0 for non-blocking
478 for ($i = 0 ; $i < 4096 ; $i++) {
480 $thiz->rndstr .= " ";
482 $thiz->rndstr .= chr(mt_rand(65, 90));
485 if (file_exists($thiz->file_socket)) {
486 unlink($thiz->file_socket);
489 $old_umask = umask(0);
490 if (($thiz->list = stream_socket_server($thiz->unix_socket, $err, $errs)) === FALSE) {
494 stream_set_blocking($thiz->list, $thiz->blocking_mode); # Set the stream to non-blocking
496 if (($thiz->in = fopen("php://stdin", "r")) === FALSE) {
500 $thiz->main_loop = FALSE;
505 function socks_set($sock, $user, $pendpage)
509 $this->socks[$id] = $sock;
511 $this->s2u[$id] = $user;
512 if ($pendpage != NULL)
513 $this->s2p[$id] = $pendpage;
516 function socks_unset($sock)
520 if (isset($this->s2u[$id]))
521 unset($this->s2u[$id]);
522 if (isset($this->s2p[$id]))
523 unset($this->s2p[$id]);
524 unset($this->socks[$id]);
527 function pendpage_try_addcont(&$new_socket, $tout, $method, $header, $get, $post, $cookie, $path, $addr, $rest, $cont)
529 $pendpage = PendingPage::pendingpage_continue(&$new_socket, $this->curtime, $tout, $method,
530 $header, $get, $post, $cookie,
531 $path, $addr, $rest, $cont);
533 $pendpage->try_flush($this->curtime);
534 // Add $pendpage to the pendpage array (in any case)
535 fprintf(STDERR, "IMPORTANT: Pendadd: %d\n", $pendpage->status);
536 $this->pendpage_add($pendpage);
539 function pendpage_try_addflush(&$new_socket, $tout, $enc, $header_out, $content)
541 $pendpage = PendingPage::pendingpage_flushing($new_socket, $this->curtime, $tout, $enc, $header_out, $content);
543 if ($pendpage->try_flush($this->curtime) == FALSE) {
544 // Add $pendpage to the pendpage array
545 $this->pendpage_add($pendpage);
549 function pendpage_add($pendpage)
551 array_push($this->pending_pages, $pendpage);
552 $this->socks_set($pendpage->socket_get(), NULL, $pendpage);
555 function pendpage_rem($pendpage)
557 $sock = $pendpage->socket_get();
558 if (($key = array_search($pendpage, $this->pending_pages)) !== FALSE) {
559 unset($this->pending_pages[$key]);
562 fprintf(STDERR, "WARNING: pendpage not found\n");
564 $this->socks_unset($sock);
565 fprintf(STDERR, "PP_REM: %d\n", intval($sock));
569 function pendpage_try_addwait(&$new_socket, $tout, $method, $header, $get, $post, $cookie, $path, $addr, $rest, $cont)
571 $pendpage = PendingPage::pendingpage_waiting($new_socket, $this->curtime, $tout, $method, $header, $get, $post, $cookie, $path, $addr, $rest, $cont);
573 if ($pendpage->try_flush($this->curtime) == FALSE) {
574 // Add $pendpage to the pendpage array
576 $this->pendpage_add($pendpage);
582 function garbage_manager($force)
584 $this->app->garbage_manager($force);
586 foreach ($this->socks as $k => $sock) {
588 if (isset($this->s2u[$id])) {
589 if ($this->s2u[$id]->sess == '') {
590 if ($this->s2u[$id]->rd_socket_get() != NULL) {
591 $this->s2u[$id]->rd_socket_set(NULL);
593 unset($this->socks[$id]);
594 unset($this->s2u[$id]);
596 printf("CLOSE ON GARBAGE MANAGER\n");
604 GLOBAL $DOCUMENT_ROOT, $HTTP_HOST;
606 GLOBAL $G_alarm_passwd, $G_black_list, $G_btrace_pref_sub, $G_dbauth;
607 GLOBAL $G_dbpfx, $G_donors_all, $G_donors_cur, $G_is_local, $G_lang;
608 GLOBAL $G_poll_entries, $G_poll_name, $G_poll_title, $G_proxy_white_list;
609 GLOBAL $G_room_roadmap, $G_shutdown, $G_sidebanner, $G_sidebanner2;
610 GLOBAL $G_splash_content, $G_splash_contents, $G_splash_cont_idx;
611 GLOBAL $G_splash_h, $G_splash_idx, $G_splash_interval, $G_splash_timeout;
612 GLOBAL $G_splash_w, $G_topbanner, $G_with_donors, $G_with_poll;
613 GLOBAL $G_with_sidebanner, $G_with_sidebanner2, $G_with_splash;
614 GLOBAL $G_with_topbanner;
616 if ($this->main_loop) {
620 $this->main_loop = TRUE;
622 while ($this->main_loop) {
623 $this->curtime = time();
624 fprintf(STDERR, "IN LOOP: Current opened: %d pending_pages: %d\n", count($this->socks), count($this->pending_pages));
626 /* Prepare the read array */
627 /* // when we manage it ... */
629 /* $read = array_merge(array("$in" => $in), $socks); */
631 $read = array_merge(array(intval($this->list) => $this->list, intval($this->in) => $this->in,
632 intval(static::$cnt_slave) => static::$cnt_slave),
635 if ($this->debug > 1) {
636 printf("PRE_SELECT\n");
641 $num_changed_sockets = @stream_select($read, $write, $except, 0, 500000);
643 if ($num_changed_sockets == 0) {
644 printf(" no data in 5 secs, splash [%d]\n", $G_with_splash);
646 else if ($num_changed_sockets > 0) {
647 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
648 if ($this->debug > 1) {
651 /* At least at one of the sockets something interesting happened */
652 foreach ($read as $i => $sock) {
654 $manage_page = FALSE;
655 /* is_resource check is required because there is the possibility that
656 during new request an old connection is closed */
657 if (!is_resource($sock)) {
660 if ($sock === $this->list) {
661 printf("NUOVA CONNEX\n");
662 if (($new_unix = stream_socket_accept($this->list)) == FALSE) {
663 printf("SOCKET_ACCEPT FAILED\n");
673 if (($new_socket = ancillary_getstream($new_unix, $stream_info)) !== FALSE) {
674 printf("NEW_SOCKET: %d\n", intval($new_socket));
675 stream_set_blocking($new_socket, $this->blocking_mode); // Set the stream to non-blocking
676 printf("RECEIVED HEADER:\n%s", $stream_info);
677 if (($path = spu_process_info($stream_info, $method, $header,
678 $get, $post, $cookie, $rest, $cont))
680 fprintf(STDERR, "TODO: fix wrong header management\n");
682 $addr = stream_socket_get_name($new_socket, TRUE);
683 printf("PATH: [%s]\n", $path);
684 if ($method == "POST" && $rest > 0) {
685 if (isset($header['Expect']) && $header['Expect'] == '100-continue') {
686 fprintf(STDERR, "\nPOSTA DE CHE\n\n");
687 $this->pendpage_try_addcont($new_socket, 20,
688 $method, $header, $get, $post, $cookie,
689 $path, $addr, $rest, $cont);
692 $this->pendpage_try_addwait($new_socket, 20,
693 $method, $header, $get, $post, $cookie,
694 $path, $addr, $rest, $cont);
701 printf("number of sockets after %d\n", count($this->socks));
704 printf("WARNING: ancillary_getstream failed\n");
708 $buf = fread($sock, 4096);
709 // if socket is closed
710 if ($buf == FALSE || mb_strlen($buf, "ASCII") == 0) {
713 printf("ERROR READING\n");
715 if ($sock === $this->list) {
716 printf("Arrivati %d bytes da list\n", mb_strlen($buf, "ASCII"));
719 else if ($sock === $this->in || $sock === static::$cnt_slave) {
720 printf("Arrivati %d bytes da stdin\n", mb_strlen($buf, "ASCII"));
724 unset($this->socks[$id]);
725 if (isset($this->s2u[$id])) {
726 // $user_a[$s2u[$id]]->disable();
727 if ($this->s2u[$id]->rd_socket_get() != NULL) {
728 $this->s2u[$id]->rd_socket_set(NULL);
730 unset($this->s2u[$id]);
734 printf("CLOSE ON READ\n");
736 if ($this->debug > 1) {
737 printf("post unset\n");
738 print_r($this->socks);
742 if ($this->debug > 1) {
745 if ($sock === $this->list) {
746 printf("Arrivati %d bytes da list\n", mb_strlen($buf, "ASCII"));
748 else if ($sock === $this->in || $sock === static::$cnt_slave) {
749 printf("Arrivati %d bytes da stdin\n", mb_strlen($buf, "ASCII"));
751 if ($line == "reload") {
752 require("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
756 else if ($line == "shutdown") {
757 if ($this->app->dump_data()) {
766 $key = array_search("$sock", $this->socks);
767 fprintf(STDERR, "Arrivati %d bytes dalla socket n. %d\n", mb_strlen($buf, "ASCII"), $key);
768 if (isset($this->s2p[$id])) {
769 $this->s2p[$id]->rest -= mb_strlen($buf, "ASCII");
770 $this->s2p[$id]->cont .= $buf;
771 if ($this->s2p[$id]->rest <= 0) {
772 $header = $new_socket = $path = $addr = $get = $cookie = 0;
775 $this->s2p[$id]->context_get($header, $new_socket, $path, $addr, $get, $post, $cookie);
776 $this->pendpage_rem($this->s2p[$id]);
777 fprintf(STDERR, "SOCKET RUN: %s\n", $new_socket);
786 if ($manage_page == TRUE) {
787 printf("M: %s\nHEADER:\n", $method);
796 $header_out = array();
797 // TODO: MOVE DOWN request_mgr to factorize new_sockets and POST closed
799 if (!strncmp($path, SITE_PREFIX, SITE_PREFIX_LEN)) {
800 $rret = $this->app->request_mgr($this, $header, $header_out, $new_socket, substr($path, SITE_PREFIX_LEN), $addr, $get, $post, $cookie);
802 fprintf(STDERR, "\n\n DI QUI PASSA [%s]\n\n", $rret);
803 if ($rret == FALSE) {
804 // FIXME: manage 404 !!!
805 printf("TODO: fix unknown page\n");
811 $this->garbage_manager(FALSE);
813 /* manage unfinished pages */
814 foreach ($this->pending_pages as $k => $pendpage) {
815 // TODO: try_flush if exists in the class
816 if ($pendpage->try_flush($this->curtime) == TRUE) {
817 unset($this->pending_pages[$k]);
822 $response: raw stream data not sent
823 $content: html consistent data (<script bla bla>)
824 $user->stream_keepalive($w_ping) ping srv->cli OR srv->cli + cli->srv if $w_ping == TRUE
827 /* manage open streaming */
828 foreach ($this->socks as $k => $sock) {
830 if (isset($this->s2u[$id])) {
831 $user = $this->s2u[$id];
832 $response = $user->rd_cache_get();
834 if (($this->curtime - $user->lacc) > (EXPIRE_TIME_RD / 3)) {
838 $user->ping_req = FALSE;
841 if ($response == "") {
843 $user->stream_main($content, $get, $post, $cookie);
844 printf("[%s] [%d] [%d]\n", $user->name, $user->lacc, $this->curtime);
845 if ($do_ping && $user->ping_req == FALSE) {
846 $content .= $user->stream_keepalive(TRUE);
847 $user->ping_req = TRUE;
849 else if ($content == "" && $user->rd_kalive_is_expired($this->curtime)) {
850 $content = $user->stream_keepalive(FALSE);
852 if ($content != "") {
853 $response = chunked_content($user->rd_zls_get(), $content);
857 if ($response != "") {
858 // echo "SPIA: [".substr($response, 0, 60)."...]\n";
859 // echo "SPIA: [".$response."]\n";
860 $response_l = mb_strlen($response, "ASCII");
861 $wret = @fwrite($sock, $response);
862 if ($wret < $response_l) {
863 printf("TROUBLE WITH FWRITE: %d\n", $wret);
864 $user->rd_cache_set(mb_substr($response, $wret, $response_l - $wret, "ASCII"));
867 $user->rd_cache_set("");
870 $user->rd_kalive_reset($this->curtime);
873 // close socket after a while to prevent client memory consumption
874 if ($user->rd_endtime_is_expired($this->curtime)) {
875 if ($this->s2u[$id]->rd_socket_get() != NULL) {
876 $this->s2u[$id]->rd_socket_set(NULL);
878 unset($this->socks[$id]);
879 unset($this->s2u[$id]);
881 printf("CLOSE ON LOOP\n");
884 } // foreach ($this->socks...
887 } // function run(...