store pid of process and return instead of exit into Sac_a_push::run method
[brisk.git] / web / Obj / sac-a-push.phh
index c5faa55..441d2eb 100644 (file)
@@ -1,7 +1,6 @@
 <?php
-
 /*
- *  brisk - spush/sac-a-push.phh
+ *  brisk - Obj/sac-a-push.phh
  *
  *  Copyright (C) 2012 Matteo Nastasi
  *                          mailto: nastasi@alternativeoutput.it 
 define('SITE_PREFIX', '/brisk/');
 define('SITE_PREFIX_LEN', 7);
 
+
+function global_dump()
+{
+    GLOBAL $G_alarm_passwd, $G_black_list, $G_btrace_pref_sub, $G_dbauth;
+    GLOBAL $G_dbpfx, $G_donors_all, $G_donors_cur, $G_is_local, $G_lang;
+    GLOBAL $G_poll_entries, $G_poll_name, $G_poll_title, $G_proxy_white_list;
+    GLOBAL $G_room_roadmap, $G_shutdown, $G_sidebanner, $G_sidebanner2;
+    GLOBAL $G_splash_content, $G_splash_contents, $G_splash_cont_idx;
+    GLOBAL $G_splash_h, $G_splash_idx, $G_splash_interval, $G_splash_timeout;
+    GLOBAL $G_splash_w, $G_topbanner, $G_with_donors, $G_with_poll;
+    GLOBAL $G_with_sidebanner, $G_with_sidebanner2, $G_with_splash;
+    GLOBAL $G_with_topbanner;
+
+    fprintf(STDERR, "G_alarm_passwd = [%s]\n", print_r($G_alarm_passwd, TRUE));
+    fprintf(STDERR, "G_black_list = [%s]\n", print_r($G_black_list, TRUE));
+    fprintf(STDERR, "G_btrace_pref_sub = [%s]\n", print_r($G_btrace_pref_sub, TRUE));
+    fprintf(STDERR, "G_dbauth = [%s]\n", print_r($G_dbauth, TRUE));
+    fprintf(STDERR, "G_dbpfx = [%s]\n", print_r($G_dbpfx, TRUE));
+    fprintf(STDERR, "G_donors_all = [%s]\n", print_r($G_donors_all, TRUE));
+    fprintf(STDERR, "G_donors_cur = [%s]\n", print_r($G_donors_cur, TRUE));
+    fprintf(STDERR, "G_is_local = [%s]\n", print_r($G_is_local, TRUE));
+    fprintf(STDERR, "G_lang = [%s]\n", print_r($G_lang, TRUE));
+    fprintf(STDERR, "G_poll_entries = [%s]\n", print_r($G_poll_entries, TRUE));
+    fprintf(STDERR, "G_poll_name = [%s]\n", print_r($G_poll_name, TRUE));
+    fprintf(STDERR, "G_poll_title = [%s]\n", print_r($G_poll_title, TRUE));
+    fprintf(STDERR, "G_proxy_white_list = [%s]\n", print_r($G_proxy_white_list, TRUE));
+    fprintf(STDERR, "G_room_roadmap = [%s]\n", print_r($G_room_roadmap, TRUE));
+    fprintf(STDERR, "G_shutdown = [%s]\n", print_r($G_shutdown, TRUE));
+    fprintf(STDERR, "G_sidebanner = [%s]\n", print_r($G_sidebanner, TRUE));
+    fprintf(STDERR, "G_sidebanner2 = [%s]\n", print_r($G_sidebanner2, TRUE));
+    fprintf(STDERR, "G_splash_content = [%s]\n", print_r($G_splash_content, TRUE));
+    fprintf(STDERR, "G_splash_contents = [%s]\n", print_r($G_splash_contents, TRUE));
+    fprintf(STDERR, "G_splash_cont_idx = [%s]\n", print_r($G_splash_cont_idx, TRUE));
+    fprintf(STDERR, "G_splash_h = [%s]\n", print_r($G_splash_h, TRUE));
+    fprintf(STDERR, "G_splash_idx = [%s]\n", print_r($G_splash_idx, TRUE));
+    fprintf(STDERR, "G_splash_interval = [%s]\n", print_r($G_splash_interval, TRUE));
+    fprintf(STDERR, "G_splash_timeout = [%s]\n", print_r($G_splash_timeout, TRUE));
+    fprintf(STDERR, "G_splash_w = [%s]\n", print_r($G_splash_w, TRUE));
+    fprintf(STDERR, "G_topbanner = [%s]\n", print_r($G_topbanner, TRUE));
+    fprintf(STDERR, "G_with_donors = [%s]\n", print_r($G_with_donors, TRUE));
+    fprintf(STDERR, "G_with_poll = [%s]\n", print_r($G_with_poll, TRUE));
+    fprintf(STDERR, "G_with_sidebanner = [%s]\n", print_r($G_with_sidebanner, TRUE));
+    fprintf(STDERR, "G_with_sidebanner2 = [%s]\n", print_r($G_with_sidebanner2, TRUE));
+    fprintf(STDERR, "G_with_splash = [%s]\n", print_r($G_with_splash, TRUE));
+    fprintf(STDERR, "G_with_topbanner = [%s]\n", print_r($G_with_topbanner, TRUE));
+}
+
+function pid_save()
+{
+    $pid = getmypid();
+    $fname = LEGAL_PATH."/brisk.pid";
+
+    if (file_exists($fname)) {
+        log_crit("WARN: brisk.pid already exists");
+    }
+    file_put_contents($fname, sprintf("%d\n", $pid));
+}
+
+function pid_remove()
+{
+    $fname = LEGAL_PATH."/brisk.pid";
+
+    if (file_exists($fname)) {
+        unlink($fname);
+    }
+}
+
 function spu_process_info($stream_info, $method, &$header, &$get, &$post, &$cookie)
 {
     $check_post = FALSE;
@@ -105,32 +171,39 @@ function gpcs_var($name, $get, $post, $cookie)
 
 function headers_render($header, $len)
 {
-    $s = "";
-    if (isset($header['Location'])) {
-        return sprintf("HTTP/1.1 302 OK\r\nLocation: %s\r\n\r\n", $header['Location']);
-    }
-    else {
-        $s .= "HTTP/1.1 200 OK\r\n";
-    }
-    if (!isset($header['Date']))
-        $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
-    if (!isset($header['Connection']))
-        $s .= "Connection: close\r\n";
-    if (!isset($header['Content-Type']))
-        $s .= "Content-Type: text/html\r\n";
-    foreach($header as $key => $value) {
-        $s .= sprintf("%s: %s\r\n", $key, $value);
+    $cookies = "";
+
+    if (isset($header['cookies'])) {
+        $cookies = $header['cookies']->render();
+        unset($header['cookies']);
     }
-    if ($len >= 0) {
-        $s .= sprintf("Content-Length: %d\r\n", $len);
+    if (isset($header['Location'])) {
+        $s = sprintf("HTTP/1.1 302 OK\r\n%sLocation: %s\r\n", $cookies, $header['Location']);
     }
     else {
-        $s .= "Cache-Control: no-cache, must-revalidate\r\n";
-        $s .= "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n";
-        if (!isset($header['Content-Encoding'])) {
-            $s .= "Content-Encoding: chunked\r\n";
+        $s = "HTTP/1.1 200 OK\r\n";
+
+        if (!isset($header['Date']))
+            $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
+        if (!isset($header['Connection']))
+            $s .= "Connection: close\r\n";
+        if (!isset($header['Content-Type']))
+            $s .= "Content-Type: text/html\r\n";
+        foreach($header as $key => $value) {
+            $s .= sprintf("%s: %s\r\n", $key, $value);
+        }
+        if ($len >= 0) {
+            $s .= sprintf("Content-Length: %d\r\n", $len);
         }
-        $s .= "Transfer-Encoding: chunked\r\n";
+        else {
+            $s .= "Cache-Control: no-cache, must-revalidate\r\n";
+            $s .= "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n";
+            if (!isset($header['Content-Encoding'])) {
+                $s .= "Content-Encoding: chunked\r\n";
+            }
+            $s .= "Transfer-Encoding: chunked\r\n";
+        }
+        $s .= $cookies;
     }
     $s .= "\r\n";
 
@@ -189,6 +262,104 @@ function get_encoding($header)
     return ($enc);
 }
 
+class Cookie {
+    var $attr;
+    // Set-Cookie: reg_fb_gate=deleted; Expires=Thu, 01-Jan-1970 00:00:01 GMT; Path=/; Domain=.foo.com; HttpOnly
+    // string $name  [, string $value  [, int $expire = 0  [, string $path  [, string $domain  [, bool $secure = false  [, bool $httponly = false  ]]]]]] )
+    function Cookie()
+    {
+        $this->attr = array();
+    }
+
+    static function create($name)
+    {
+        $thiz = new Cookie();
+
+        $thiz->attr[$name] = "";
+
+        $argc = func_num_args();
+        for ($i = 1 ; $i < $argc ; $i++) {
+            $arg = func_get_arg($i);
+            switch ($i) {
+            case 1:
+                $thiz->attr[$name] = urlencode($arg);
+                break;
+            case 2:
+                $thiz->attr['Expires'] = gmdate('D, d M Y H:i:s \G\M\T', $arg); // RFC 1211 format
+                break;
+            case 3:
+                $thiz->attr['Path'] = $arg;
+                break;
+            case 4:
+                $thiz->attr['Domain'] = $arg;
+                break;
+            case 5:
+                if ($arg == TRUE) {
+                    $thiz->attr['Secure'] = NULL;
+                }
+                break;
+            case 6:
+                if ($arg == TRUE) {
+                    $thiz->attr['HttpOnly'] = NULL;
+                }
+                break;
+            default:
+                return FALSE;
+            }
+        }
+
+        return $thiz;
+    }
+
+    function render()
+    {
+        $r = "Set-Cookie: ";
+        $isfirst = TRUE;
+
+        foreach ($this->attr as $k => $v) {
+            if ($v == NULL) {
+                $r .= sprintf("%s%s", ($isfirst ? "" : "; "), $k);
+            }
+            else {
+                $r .= sprintf("%s%s=%s", ($isfirst ? "" : "; "), $k, $v);
+            }
+            $isfirst = FALSE;
+        }
+        $r .= "\r\n";
+
+        return $r;
+    }
+}
+
+class Cookies {
+    var $cookies;
+
+    function Cookies()
+    {
+        $this->cookies = array();
+    }
+
+    function add($name)
+    {
+        if (($cookie = call_user_func_array("Cookie::create", func_get_args())) == FALSE)
+            return (FALSE);
+
+        array_push($this->cookies, $cookie);
+
+        return (TRUE);
+    }
+
+    function render()
+    {
+        $r = "";
+        foreach ($this->cookies as $cookie) {
+            $r .= $cookie->render();
+        }
+
+        return ($r);
+    }
+}
+
 
 class Sac_a_push {
     static $fixed_fd = 2;
@@ -206,7 +377,6 @@ class Sac_a_push {
     var $blocking_mode;
 
     var $app;
-    var $bin5;
 
     var $curtime;
 
@@ -236,7 +406,7 @@ class Sac_a_push {
         $thiz->rndstr = "";
         for ($i = 0 ; $i < 4096 ; $i++) {
             if (($i % 128) == 0)
-                $thiz->rndstr .= "\n";
+                $thiz->rndstr .= " ";
             else
                 $thiz->rndstr .= chr(mt_rand(65, 90));
         }
@@ -311,6 +481,18 @@ class Sac_a_push {
 
     function run()
     {
+        GLOBAL $DOCUMENT_ROOT, $HTTP_HOST;
+
+        GLOBAL $G_alarm_passwd, $G_black_list, $G_btrace_pref_sub, $G_dbauth;
+        GLOBAL $G_dbpfx, $G_donors_all, $G_donors_cur, $G_is_local, $G_lang;
+        GLOBAL $G_poll_entries, $G_poll_name, $G_poll_title, $G_proxy_white_list;
+        GLOBAL $G_room_roadmap, $G_shutdown, $G_sidebanner, $G_sidebanner2;
+        GLOBAL $G_splash_content, $G_splash_contents, $G_splash_cont_idx;
+        GLOBAL $G_splash_h, $G_splash_idx, $G_splash_interval, $G_splash_timeout;
+        GLOBAL $G_splash_w, $G_topbanner, $G_with_donors, $G_with_poll;
+        GLOBAL $G_with_sidebanner, $G_with_sidebanner2, $G_with_splash;
+        GLOBAL $G_with_topbanner;
+
         if ($this->main_loop) {
             return (FALSE);
         }
@@ -338,7 +520,7 @@ class Sac_a_push {
             $num_changed_sockets = stream_select($read, $write, $except, 0, 500000);
         
             if ($num_changed_sockets == 0) {
-                printf(" no data in 5 secs ");
+                printf(" no data in 5 secs, splash [%d]\n", $G_with_splash);
             } 
             else if ($num_changed_sockets > 0) {
                 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
@@ -377,17 +559,14 @@ class Sac_a_push {
                             print_r($post);
                             printf("COOKIE:\n");
                             print_r($cookie);
-
                             $addr = stream_socket_get_name($new_socket, TRUE);
                             $header_out = array();
 
-                            $enc = get_encoding($header);
-
                             $subs = SITE_PREFIX."briskin5/";
                             $subs_l = strlen($subs);
                             $rret = FALSE;
                             if (!strncmp($path, SITE_PREFIX, SITE_PREFIX_LEN)) {
-                                $rret = $this->app->request_mgr($this, $enc, $header_out, $new_socket, substr($path, SITE_PREFIX_LEN), $addr, $get, $post, $cookie);
+                                $rret = $this->app->request_mgr($this, $header, $header_out, $new_socket, substr($path, SITE_PREFIX_LEN), $addr, $get, $post, $cookie);
                             }
                             if ($rret == FALSE) { 
                                 // FIXME: manage 404 !!!
@@ -401,17 +580,18 @@ class Sac_a_push {
                     }
                     else {
                         $buf = fread($sock, 512);
+                        // if socket is closed
                         if ($buf == FALSE || strlen($buf) == 0) {
                             if ($buf == FALSE) {
                                 printf("ERROR READING\n");
                             }
                             if ($sock === $this->list) {
                                 printf("Arrivati %d bytes da list\n", strlen($buf));
-                                exit(21);
+                                return(21);
                             }
                             else if ($sock === $this->in) {
                                 printf("Arrivati %d bytes da stdin\n", strlen($buf));
-                                exit(22);
+                                return(22);
                             }
                             else {
                                 // $user_a[$s2u[intval($sock)]]->disable();
@@ -438,6 +618,20 @@ class Sac_a_push {
                             }
                             else if ($sock === $this->in) {
                                 printf("Arrivati %d bytes da stdin\n", strlen($buf));
+                                $line = trim($buf);
+                                if ($line == "reload") {
+                                    require("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
+
+                                    global_dump();
+                                }
+                                else if ($line == "shutdown") {
+                                    if ($this->app->dump_data()) {
+                                        return(0);
+                                    }
+                                    else {
+                                        return(1);
+                                    }
+                                }
                             }
                             else {
                                 $key = array_search("$sock", $this->socks);