pull of unix socket to interact with apache2
[brisk.git] / web / usermgmt.php
index f3ea4e1..57a6ae1 100644 (file)
  *
  */
 
+foreach (array("HTTP_HOST", "DOCUMENT_ROOT") as $i) {
+    if (isset($_SERVER[$i])) {
+        $$i = $_SERVER[$i];
+        }
+    }
+
+foreach (array("pazz") as $i) {
+    if (isset($_POST[$i])) {
+        $$i = $_POST[$i];
+        }
+    }
+
+foreach (array("sess") as $i) {
+    if (isset($_COOKIE[$i])) {
+        $$i = $_COOKIE[$i];
+        }
+    }
+
+foreach (array("sess") as $i) {
+    if (isset($_COOKIE[$i])) {
+        $$i = $_COOKIE[$i];
+        }
+    }
+
 $G_base = "";
 
 $mlang_umgmt = array( 'nu_psubj' => array( 'it' => 'Brisk: credenziali di accesso.',
@@ -60,7 +84,12 @@ function check_auth()
 
     $socket = FALSE;
     $ret = FALSE;
-    $ip = $_SERVER["REMOTE_ADDR"];
+    if (array_key_exists("HTTP_X_REAL_IP", $_SERVER)) {
+        $ip = $_SERVER["HTTP_X_REAL_IP"];
+    }
+    else {
+        $ip = $_SERVER["REMOTE_ADDR"];
+    }
     $stp = 0;
     $private = md5($G_alarm_passwd.$ip.$sess);
     $cmd = array ("cmd" => "userauth", "sess" => $sess, "private" => $private, "the_end" => "true");
@@ -68,7 +97,7 @@ function check_auth()
     $cmd_len = mb_strlen($cmd_ser, "ASCII");
 
     do {
-        if (($socket = stream_socket_client("unix://".USOCK_PATH."2")) == FALSE)
+        if (($socket = stream_socket_client('unix://'.USOCK_PATH_PFX.'_admin.sock')) == FALSE)
             break;
         $stp = 1;
         if (($rwr = fwrite($socket, $cmd_ser, $cmd_len)) == FALSE
@@ -108,7 +137,7 @@ $s_style = "
 </style>";
 
 function main() {
-    GLOBAL $s_style, $G_dbpfx, $G_lang, $G_alarm_passwd, $G_domain, $G_webbase;
+    GLOBAL $s_style, $G_dbpfx, $G_lang, $G_alarm_passwd, $G_proto, $G_domain, $G_webbase;
     GLOBAL $mlang_umgmt, $mlang_indwr, $f_mailusers, $sess, $_POST, $_SERVER;
 
 
@@ -157,7 +186,7 @@ function main() {
 SELECT usr.*, guar.login AS guar_login
      FROM %susers AS usr
      JOIN %susers AS guar ON guar.code = usr.guar_code
-     WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
+     WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
          AND usr.disa_reas = %d AND usr.code = %d;",
                                $G_dbpfx, $G_dbpfx,
                                USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
@@ -192,14 +221,14 @@ SELECT usr.*, guar.login AS guar_login
                     }
                     $hash = md5($curtime . $G_alarm_passwd . $usr_obj->login . $usr_obj->email);
 
-                    $confirm_page = sprintf("http://%s/%s/mailmgr.php?f_act=checkmail&f_code=%d&f_hash=%s",
-                                            $G_domain, $G_webbase, $mail_code, $hash);
+                    $confirm_page = sprintf("%s://%s/%s/mailmgr.php?f_act=checkmail&f_code=%d&f_hash=%s",
+                                            $G_proto, $G_domain, $G_webbase, $mail_code, $hash);
                     $subj = $mlang_indwr['nu_msubj'][$G_lang];
                     if (($usr_obj->type & USER_FLAG_TY_APPR) == USER_FLAG_TY_APPR) {
                         $body_txt = sprintf($mlang_indwr['ap_mtext'][$G_lang],
-                                            $cli_name, $confirm_page);
+                                            $usr_obj->login, $confirm_page);
                         $body_htm = sprintf($mlang_indwr['ap_mhtml'][$G_lang],
-                                            $cli_name, $confirm_page);
+                                            $usr_obj->login, $confirm_page);
                     }
                     else {
                         $body_txt = sprintf($mlang_indwr['nu_mtext'][$G_lang],
@@ -233,7 +262,8 @@ SELECT usr.*, guar.login AS guar_login
                     break;
                 }
             }
-        }
+        } // else if ($action == "accept") {
+
 
         do {
             if (($bdb = BriskDB::create()) == FALSE) {
@@ -246,9 +276,8 @@ SELECT usr.*, guar.login AS guar_login
 SELECT usr.*, guar.login AS guar_login
      FROM %susers AS usr
      JOIN %susers AS guar ON guar.code = usr.guar_code
-     WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
-         AND usr.disa_reas = %d
-     ORDER BY usr.lintm;",
+     WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
+         AND usr.disa_reas = %d ORDER BY usr.lintm;",
                                $G_dbpfx, $G_dbpfx,
                                USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
                                USER_DIS_REA_NU_ADDED);
@@ -367,9 +396,8 @@ SELECT mail.*, usr.email AS email
 SELECT usr.*, guar.login AS guar_login
      FROM %susers AS usr
      JOIN %susers AS guar ON guar.code = usr.guar_code
-     WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
-         AND usr.disa_reas = %d
-     ORDER BY usr.lintm;",
+     WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
+         AND usr.disa_reas = %d ORDER BY usr.lintm;",
                                $G_dbpfx, $G_dbpfx,
                                USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
                                USER_DIS_REA_NU_MAILED);
@@ -444,10 +472,10 @@ SELECT usr.*, guar.login AS guar_login
 SELECT usr.*, guar.login AS guar_login
      FROM %susers AS usr
      JOIN %susers AS guar ON guar.code = usr.guar_code
-     WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
+     WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
          AND usr.disa_reas = %d AND usr.code = %d;",
                                    $G_dbpfx, $G_dbpfx,
-                                   USER_FLAG_TY_ALL & ~USER_FLAG_TY_APPR, USER_FLAG_TY_DISABLE,
+                                   USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
                                    USER_DIS_REA_NU_TOBECHK, $id);
                 if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
                     log_crit("stat-day: select from tournaments failed");
@@ -491,6 +519,108 @@ SELECT usr.*, guar.login AS guar_login
             }
             exit;
         }
+
+
+        else if ($action == "delete") {
+            foreach($_POST as $key => $value) {
+                if (substr($key, 0, 9) != "f_newuser")
+                    continue;
+
+                $id = (int)substr($key, 9);
+                if ($id <= 0)
+                    continue;
+
+                // check existence of username or email
+                $is_trans = FALSE;
+                $res = FALSE;
+                do {
+                    if (($bdb = BriskDB::create()) == FALSE)
+                        break;
+
+                    // retrieve list added users
+                    $usr_sql = sprintf("
+SELECT usr.*, guar.login AS guar_login
+     FROM %susers AS usr
+     JOIN %susers AS guar ON guar.code = usr.guar_code
+     WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
+         AND usr.disa_reas = %d AND usr.code = %d;",
+                               $G_dbpfx, $G_dbpfx,
+                                       USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
+                                       USER_DIS_REA_NU_TOBECHK, $id);
+
+
+                    if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
+                        log_crit("stat-day: select from tournaments failed");
+                        break;
+                    }
+                    $usr_n = pg_numrows($usr_pg);
+                    if ($usr_n != 1) {
+                        $status .= sprintf("Inconsistency for code %d, returned %d records, skipped.<br>",
+                                          $id, $usr_n);
+                        break;
+                    }
+
+                    $usr_obj = pg_fetch_object($usr_pg, 0);
+
+                    $bdb->transaction('BEGIN');
+                    $is_trans = TRUE;
+
+                    $del_sql = sprintf("DELETE FROM %susers WHERE code = %d;",
+                                       $G_dbpfx, $usr_obj->code);
+
+                    if (($del_pg = pg_query($bdb->dbconn->db(), $del_sql)) == FALSE) {
+                        log_crit("stat-day: select from tournaments failed");
+                        break;
+                    }
+
+                    // FIXME: add to index_wr.php strings
+                    $subj = "Brisk: nickname rifiutato";
+                    // the same for both cases:
+                    //  if (($usr_obj->type & USER_FLAG_TY_APPR) == USER_FLAG_TY_APPR) {
+                        $body_txt = sprintf('Ciao, sono l\' amministratore del sito di Brisk.
+
+Ti volevo segnalare che il nickname \'%s\' con cui ti volevi registrare
+non ha superato la fase di verifica manuale; il motivo può essere
+la sua illeggibilità per gli altri utenti o il contenuto poco ortodosso
+o troppo aggressivo o ci sono troppe cifre consecutive o qualcosa del genere.
+
+La procedura di registrazione va ripetuta.
+
+Saluti e buone partite, mop.', $usr_obj->login);
+
+                        $body_htm = sprintf('Ciao, sono l\' amministratore del sito di Brisk.<br><br>
+Ti volevo segnalare che il nickname \'%s\' con cui ti volevi registrare
+non ha superato la fase di verifica manuale; il motivo può essere
+la sua illeggibilità per gli altri utenti o il contenuto poco ortodosso
+o troppo aggressivo o ci sono troppe cifre consecutive o qualcosa del genere.<br><br>
+La procedura di registrazione va ripetuta.<br><br>
+Saluti e buone partite, mop.', $usr_obj->login);
+                    /* } */
+                    /* else { */
+                    /*     $body_txt = sprintf($mlang_indwr['nu_mtext'][$G_lang], */
+                    /*                         $usr_obj->guar_login, $usr_obj->login, $confirm_page); */
+                    /*     $body_htm = sprintf($mlang_indwr['nu_mhtml'][$G_lang], */
+                    /*                         $usr_obj->guar_login, $usr_obj->login, $confirm_page); */
+                    /* } */
+
+                    if (brisk_mail($usr_obj->email, $subj, $body_txt, $body_htm) == FALSE) {
+                        // mail error
+                        fprintf(STDERR, "ERROR: mail send FAILED\n");
+                        break;
+                    }
+                    $status .= sprintf("user delete for %s: SUCCESS<br>", $usr_obj->login);
+                    $bdb->transaction('COMMIT');
+                    $res = TRUE;
+                } while(FALSE);
+                if ($res == FALSE) {
+                    $status .= sprintf("Error occurred during accept action<br>");
+                    if ($is_trans)
+                        $bdb->transaction('ROLLBACK');
+                    break;
+                }
+                printf("Registration %s for login %s deleted<br>\n", $usr_obj->code, $usr_obj->login);
+            }
+        }
         else {
             do {
             if (($bdb = BriskDB::create()) == FALSE) {
@@ -503,11 +633,10 @@ SELECT usr.*, guar.login AS guar_login
 SELECT usr.*, guar.login AS guar_login
      FROM %susers AS usr
      JOIN %susers AS guar ON guar.code = usr.guar_code
-     WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
-         AND usr.disa_reas = %d
-     ORDER BY usr.lintm;",
+     WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
+         AND usr.disa_reas = %d ORDER BY usr.lintm;",
                                $G_dbpfx, $G_dbpfx,
-                               USER_FLAG_TY_ALL & ~USER_FLAG_TY_APPR, USER_FLAG_TY_DISABLE,
+                               USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
                                USER_DIS_REA_NU_TOBECHK);
             if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
                 log_crit("stat-day: select from tournaments failed");
@@ -515,13 +644,13 @@ SELECT usr.*, guar.login AS guar_login
             }
 
             $usr_n = pg_numrows($usr_pg);
-            $tab_lines = "<tr><th></th><th>User</th><th>Guar</th><th>Apprendice</th><th>Date</th></tr>";
+            $tab_lines = "<tr><th></th><th>User</th><th>EMail</th><th>Guar</th><th>Apprendice</th><th>Date</th></tr>";
             for ($i = 0 ; $i < $usr_n ; $i++) {
                 $usr_obj = pg_fetch_object($usr_pg, $i);
 
-                $tab_lines .= sprintf("<tr><td><input name=\"f_newuser%d\" type=\"checkbox\" %s></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
+                $tab_lines .= sprintf("<tr><td><input name=\"f_newuser%d\" type=\"checkbox\" %s></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
                                       $usr_obj->code, ($nocheck ? "" : "CHECKED"),
-                                      eschtml($usr_obj->login), eschtml($usr_obj->guar_login),
+                                      eschtml($usr_obj->login), eschtml($usr_obj->email), eschtml($usr_obj->guar_login),
                                       ($usr_obj->type & USER_FLAG_TY_APPR ? "Yes" : "No"),
                                       $usr_obj->lintm);
             }