BUGFIX: typo for emails at the end of game; able to set return address
[e-DoKo.git] / index.php
index fa828daabfb498aba84c089247b07cc680ab1f40..0b9cfb494fb5c2c3f0bd33fdb68534e0ef112f28 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1,9 +1,6 @@
 <?php
 error_reporting(E_ALL);
 
-global $REV;
-$REV  ="\$Rev$";
-
 include_once("config.php");      
 include_once("output.php");      /* html output only */
 include_once("db.php");          /* database only */
@@ -12,7 +9,10 @@ include_once("functions.php");   /* the rest */
 /* check if some variables are set in the config file, else set defaults */
 if(!isset($EmailName))
      $EmailName="[DoKo] ";
-
+if(isset($EMAIL_REPLY))
+  {
+    ini_set("sendmail_from",$EMAIL_REPLY);
+  }
 
 /* in case work has to be done on the database or other section we can
  * shut down the server and tell people to come back later 
@@ -94,7 +94,7 @@ if(myisset("new"))
        $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game, 
                                                         this way no manipulation is possible */
        if($session)
-         mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','pre',".
+         mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','1','pre',".
                      "'$ruleset','$session' ,NULL)");
        else
          {
@@ -102,12 +102,12 @@ if(myisset("new"))
            $max = DB_get_max_session();
            $max++;
            mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
-           mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','pre',".
+           mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','1','pre',".
                        "'$ruleset','$max' ,NULL)");
          }
       }
     else
-      mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','pre', ".
+      mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','1','pre', ".
                  "'$ruleset',NULL ,NULL)");
     $game_id = mysql_insert_id();
     
@@ -434,19 +434,19 @@ else if(myisset("me"))
          
          /* check if everyone has reached this stage, send out email */
          $userids = DB_get_all_userid_by_gameid($gameid);
-         $ok=1;
+         $ok = 1;
          foreach($userids as $user)
            {
              $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
              if($userstat!='poverty' && $userstat!='play')
-               $ok=0;
+               $ok = 0;
            };
          if($ok)
            foreach($userids as $user)
              {
-               $To = DB_get_email_by_userid($user);
-               $userhash =DB_get_hash_from_gameid_and_userid($gameid,$user);
-               if($userhash!=$me)
+               $To       = DB_get_email_by_userid($user);
+               $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
+               if($userhash != $me)
                  {
                    $message = "Everyone finish the questionary in game $gameid, ".
                      "please visit this link now to continue: \n".
@@ -468,12 +468,12 @@ else if(myisset("me"))
       
       /* check if everyone has reached this stage */
       $userids = DB_get_all_userid_by_gameid($gameid);
-      $ok=1;
+      $ok = 1;
       foreach($userids as $user)
        {
          $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
          if($userstat!='poverty' && $userstat!='play')
-           $ok=0;
+           $ok = 0;
        };
 
       if(!$ok)
@@ -499,7 +499,7 @@ else if(myisset("me"))
          $solo    = 0;
          foreach($userids as $user)
            {
-             $name = DB_get_name_by_userid($user);
+             $name     = DB_get_name_by_userid($user);
              $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
              if($usersick == 'nines')
                {
@@ -560,7 +560,7 @@ else if(myisset("me"))
            {
              DB_set_gametype_by_gameid($gameid,"poverty");
              $gametype = "poverty";
-             $who=DB_get_sickness_by_gameid($gameid);
+             $who      = DB_get_sickness_by_gameid($gameid);
              if(!$who)
                {
                  $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
@@ -574,7 +574,7 @@ else if(myisset("me"))
            {
              DB_set_gametype_by_gameid($gameid,"dpoverty");
              $gametype = "dpoverty";
-             $who=DB_get_sickness_by_gameid($gameid);
+             $who      = DB_get_sickness_by_gameid($gameid);
              if(!$who)
                {
                  $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
@@ -813,13 +813,13 @@ else if(myisset("me"))
                      DB_set_hand_status_by_hash($hash,'play');
 
                      /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
-                     $re_set=0;
+                     $re_set = 0;
                      foreach($userids as $user)
                        {
-                         $userhash =DB_get_hash_from_gameid_and_userid($gameid,$user);
-                         $party=DB_get_party_by_hash($userhash);
+                         $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
+                         $party    = DB_get_party_by_hash($userhash);
                          if($party=="re")
-                           $re_set=1;
+                           $re_set = 1;
                        }
                      if($re_set)
                        {
@@ -830,7 +830,7 @@ else if(myisset("me"))
                        {
                          foreach($userids as $user)
                            {
-                             $userhash =DB_get_hash_from_gameid_and_userid($gameid,$user);
+                             $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
                              if($userhash==$hash||$userhash==$me)
                                DB_set_party_by_hash($userhash,"re");
                              else
@@ -864,22 +864,21 @@ else if(myisset("me"))
                {
                  foreach($userids as $user)
                    {
-                     $name = DB_get_name_by_userid($user);
+                     $name     = DB_get_name_by_userid($user);
                      $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
                      
                      if($usersick=="poverty")
                        {
-                         $hash =DB_get_hash_from_gameid_and_userid($gameid,$user);
-                         $cards=DB_get_hand($hash);
-                         $nrtrump=count_trump($cards);
+                         $hash    = DB_get_hash_from_gameid_and_userid($gameid,$user);
+                         $cards   = DB_get_hand($hash);
+                         $nrtrump = count_trump($cards);
                          /* count trump */
                          if($nrtrump<4)
                            echo "Player $name has $nrtrump trump. Do you want to take them?".
                              "<a href=\"index.php?me=$me&amp;trump=$user\">yes</a> <br />";
                        }
                    }
-                 echo "I don't want to take any trump: ".
-                   "<a href=\"index.php?me=$me&amp;trump=no\">yes</a> <br />";
+                 echo "<a href=\"index.php?me=$me&amp;trump=no\">No,way I take those trump...</a> <br />";
 
                  echo "Your cards are: <br />\n";
                  $mycards = DB_get_hand($me);
@@ -897,13 +896,7 @@ else if(myisset("me"))
                  else
                    echo "it's not your turn yet to decide if you want to take the trump or not.";
                }
-             /*
-              *    yes, display number of trump and user's hand, ask if he wants to take it 
-              *      no, set whom-to-ask to next player, email next player, cancle game if no next player
-              *      yes -> link to new page:display all cards, ask for N return cards
-              *          set re/contra 
-              *        
-              */
+
            };
        }
       /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
@@ -930,10 +923,10 @@ else if(myisset("me"))
        }
 
       /* check if all players are ready to play */
-      $ok=1;
+      $ok = 1;
       foreach($userids as $user)
        if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
-         $ok=0;
+         $ok = 0;
 
       if($ok)
        {
@@ -974,14 +967,14 @@ else if(myisset("me"))
        */
       
       $gametype = DB_get_gametype_by_gameid($gameid);
-      $GT = $gametype;
+      $GT       = $gametype;
       if($gametype=="solo")
        {
          $gametype = DB_get_solo_by_gameid($gameid);
-         $GT = $gametype." ".$GT;
+         $GT       = $gametype." ".$GT;
        }
       else
-       $gametype="normal";
+       $gametype = "normal";
       
       set_gametype($gametype); /* this sets the $CARDS variable */
       
@@ -1050,56 +1043,91 @@ else if(myisset("me"))
            if($sickness=="poverty")
              {
                $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
-               $cards = DB_get_all_hand($userhash);
-               $trumpNR = count_trump($cards);
+               $cards    = DB_get_all_hand($userhash);
+               $trumpNR  = count_trump($cards);
                if($trumpNR)
-                 echo "(poverty < trump back)";
+                 echo "<img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
                else
-                 echo "(poverty <)";
+                 echo "<img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
              }
            else
-             echo "(poverty >)";
+             echo "<img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
 
          if($GT=="dpoverty")
            if($party=="re")
              if($sickness=="poverty")
                {
                $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
-               $cards = DB_get_all_hand($userhash);
-               $trumpNR = count_trump($cards);
+               $cards    = DB_get_all_hand($userhash);
+               $trumpNR  = count_trump($cards);
                if($trumpNR)
-                 echo "(poverty A < trump back)";
+                 echo "<img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
                else
-                 echo "(poverty A <)";
+                 echo "<img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
                }
              else
-               echo "(poverty A >)";
+               echo "<img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
            else
              if($sickness=="poverty")
                {
                $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
-               $cards = DB_get_all_hand($userhash);
-               $trumpNR = count_trump($cards);
+               $cards    = DB_get_all_hand($userhash);
+               $trumpNR  = count_trump($cards);
                if($trumpNR)
-                 echo "(poverty B < trump back)";
+                 echo "<img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" />";
                else
-                 echo "(poverty B <)";
+                 echo "<img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" />";
                }
              else
-               echo "(poverty B >)";
+               echo "<img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" />";
              
          if($GT=="wedding" && $party=="re")
              if($sickness=="wedding")
-               echo "(wedding  +)";
+               echo "<img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" />";
              else
-               echo "(wedding)";
+               echo "<img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" />";
          
          if(ereg("solo",$GT) && $party=="re")
-            echo "($GT)";
+           {
+             if(ereg("queen",$GT))
+               echo "<img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" />";
+             else if(ereg("jack",$GT))
+               echo "<img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" />";
+             else if(ereg("club",$GT))
+               echo "<img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" />";
+             else if(ereg("spade",$GT))
+               echo "<img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" />";
+             else if(ereg("heart",$GT))
+               echo "<img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" />";
+             else if(ereg("trumpless",$GT))
+               echo "<img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
+             else if(ereg("trump",$GT))
+               echo "<img src=\"pics/button/diamandsolo_button.png\" class=\"button\" alt=\"$GT\" />";
+           }
 
          /* add point calls */
          if($call!=NULL)
-           echo " $party $call ";
+           {
+             if($party=="re")
+               echo "<img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" />";
+             else
+               echo "<img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" />";
+             switch($call)
+               {
+               case "0":
+                 echo "<img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" />";
+                 break;
+               case "30":
+                 echo "<img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" />";
+                 break;
+               case "60":
+                 echo "<img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" />";
+                 break;
+               case "90":
+                 echo "<img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" />";
+                 break;
+               }
+           }
 
          echo "<br />\n";
          echo " local time: ".date("Y-m-d H:i:s")."<br />\n";
@@ -1124,8 +1152,7 @@ else if(myisset("me"))
                            "LEFT JOIN Comment ON Play.id=Comment.play_id ".
                            "WHERE Trick.game_id='".$gameid."' ".
                            "ORDER BY Trick.id,sequence ASC");
-      $trickNR = 1;
-      
+      $trickNR   = 1;
       $lasttrick = DB_get_max_trickid($gameid);
       
       $play = array(); /* needed to calculate winner later  */
@@ -1206,14 +1233,14 @@ else if(myisset("me"))
       /* whos turn is it? */
       if($seq==4)
        {
-         $winner = get_winner($play,$gametype); /* returns the position */
-         $next = $winner;
+         $winner    = get_winner($play,$gametype); /* returns the position */
+         $next      = $winner;
          $firstcard = ""; /* new trick, no first card */
        }
       else
        {
          $next = $pos+1;
-         if($next==5) $next=1;
+         if($next==5) $next = 1;
        }
       
       /* my turn?, display cards as links, ask for comments*/
@@ -1370,7 +1397,7 @@ else if(myisset("me"))
                                        " LEFT JOIN User ON Hand.user_id=User.id".
                                        " WHERE Hand.game_id=$gameid".
                                        " GROUP BY fullname" );
-                 $message = "The game is over. Thanks for playing :)\n";
+                 $message  = "The game is over. Thanks for playing :)\n";
                  $message .= "Final score:\n";
                  while( $r = mysql_fetch_array($result,MYSQL_NUM))
                    $message .= "   ".$r[0]."(".$r[2].") ".$r[1]."\n";
@@ -1384,26 +1411,23 @@ else if(myisset("me"))
                  while( $r = mysql_fetch_array($result,MYSQL_NUM))
                    $message .= "    ".$r[0]." ".$r[1]."\n";
                  
-                 /* check who wants to be CC'ed on the email */
-                 $h = array();
-                 $header = "";
+                 /* send out final email */
+                 $all = array();
+
                  foreach($userids as $user)
-                   {
-                     $result = mysql_query("SELECT value from User_Prefs".
-                                           " WHERE user_id='$user' AND pref_key='ccemail'" );
-                     $r = mysql_fetch_array($result,MYSQL_NUM);
-                     if($r && $r[0]=="yes")
-                       $h[]   = DB_get_email_by_userid($user);
-                   }
-                 if(sizeof($h))
-                   $header = "CC: ".join(",",$h)."\r\n";
-                 
+                   $all[] = DB_get_email_by_userid($user);
+                 $To = implode(",",$all);
+
+                 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
+                 mymail($To,$EmailName."game over (game $gameid) part 1(2)",$message.$help);
+
                  foreach($userids as $user)
                    {
                      $To   = DB_get_email_by_userid($user);
                      $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
-                     $mymessage = $message."Use this link to have a look at the game: ".$host."?me=".$hash."\n\n" ;
-                     mymail($To,$EmailName."game over (game $gameid)",$mymessage,$header);
+                     
+                     $link = "Use this link to have a look at game $gameid: ".$host."?me=".$hash."\n\n" ;
+                     mymail($To,$EmailName."game over (game $gameid) part 2(2)",$link);
                    }
                }
              
@@ -1521,6 +1545,24 @@ else if(myisset("me"))
          echo "Your cards were: <br />\n";
          foreach($oldcards as $card) 
            display_card($card,$PREF["cardset"]);
+         
+         $userids = DB_get_all_userid_by_gameid($gameid);
+          foreach($userids as $user)
+            {
+              $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
+             
+              if($userhash!=$me)
+                {
+                  echo "<br />";
+                 
+                  $name = DB_get_name_by_userid($user);
+                  $oldcards = DB_get_all_hand($userhash);
+                  $oldcards = mysort($oldcards,$gametype);
+                  echo "$name's cards were: <br />\n";
+                  foreach($oldcards as $card)
+                    display_card($card,$PREF["cardset"]);
+                }
+            }
        }
       echo "</div>\n";
       
@@ -1564,7 +1606,7 @@ else if(myisset("me"))
                                 " WHERE session=$session".
                                 " ORDER BY create_date DESC".
                                 " LIMIT 1");
-         $r=-1;
+         $r = -1;
          if($result)
            $r = mysql_fetch_array($result,MYSQL_NUM);
          
@@ -1593,11 +1635,11 @@ else if(myisset("me"))
 
      if(myisset("forgot"))
        {
-        $ok=1;
+        $ok = 1;
 
         $uid = DB_get_userid_by_email($email);
         if(!$uid)
-          $ok=0;
+          $ok = 0;
         
         if($ok)
           {
@@ -1619,10 +1661,10 @@ else if(myisset("me"))
        if(strlen($password)!=32)
         $password = md5($password);
        
-       $ok=1;
+       $ok  = 1;
        $uid = DB_get_userid_by_email_and_password($email,$password);
        if(!$uid)
-        $ok=0;
+        $ok = 0;
        
        if($ok)
         {
@@ -1644,21 +1686,6 @@ else if(myisset("me"))
                     $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$uid','cardset',".DB_quote_smart($setpref).")");
                   echo "Ok, changed you preferences for the cards.\n";
                   break;
-                case "ccemail":
-                  $result = mysql_query("SELECT * from User_Prefs".
-                                        " WHERE user_id='$uid' AND pref_key='ccemail'" );
-                  if( mysql_fetch_array($result,MYSQL_NUM))
-                    if($PREF["ccemail"]=="yes")
-                      $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart("no").
-                                            " WHERE user_id='$uid' AND pref_key='ccemail'" );
-                    else
-                      $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart("yes").
-                                            " WHERE user_id='$uid' AND pref_key='ccemail'" );
-                  else
-                    $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$uid','ccemail',".DB_quote_smart("yes").")");
-                  echo "Ok, changed you preferences for being CC'ed on emails.\n";
-                  break;
-
                 }
             }
           else /* output default user page */
@@ -1677,7 +1704,7 @@ else if(myisset("me"))
               
               DB_update_user_timestamp($uid);
               
-              echo "<p>these are your games that haven't started yet:<br />\n";
+              echo "<p>These are your games that haven't started yet:<br />\n";
               $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
                                     " LEFT JOIN Game On Hand.game_id=Game.id".
                                     " WHERE Hand.user_id='$uid' AND Game.status='pre'" );
@@ -1702,7 +1729,7 @@ else if(myisset("me"))
                 }
               echo "</p>\n";
 
-              echo "<p>these are the games you are playing in:<br />\n";
+              echo "<p>These are the games you are playing in:<br />\n";
               $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
                                     " LEFT JOIN Game On Hand.game_id=Game.id".
                                     " WHERE Hand.user_id='$uid' AND Game.status='play'" );
@@ -1728,15 +1755,15 @@ else if(myisset("me"))
               echo "</p>\n";
               
               
-              echo "<p>and these are your games that are already done:<br />Game: \n";
-              $output=array();
+              echo "<p>And these are your games that are already done:<br />Game: \n";
+              $output = array();
               $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
               while( $r = mysql_fetch_array($result,MYSQL_NUM))
-                $output[]= "<a href=\"".$host."?me=".$r[0]."\">#".$r[1]." </a>";
+                $output[] = "<a href=\"".$host."?me=".$r[0]."\">#".$r[1]." </a>";
               echo implode(", ",$output)."</p>\n";
               
               $names = DB_get_all_names();
-              echo "<p>registered players:<br />\n";
+              echo "<p>Registered players:<br />\n";
               echo implode(", ",$names)."\n";
               echo "</p>\n";
               
@@ -1745,7 +1772,7 @@ else if(myisset("me"))
         }
        else
         {
-          echo "sorry email and password don't match <br />";
+          echo "Sorry email and password don't match <br />";
         }
      };
      output_footer();