remove Altenburger Spielkarten, their copyright notice, etc. since the licence expired
[e-DoKo.git] / include / db.php
index 882a1adb35448224451f230aff4f6278fa373a80..f683beba29402686e4e93a057139cd63787b0bd3 100644 (file)
@@ -1,4 +1,23 @@
 <?php
 <?php
+/* Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Arun Persaud <arun@nubati.net>
+ *
+ *   This file is part of e-DoKo.
+ *
+ *   e-DoKo is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   e-DoKo is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with e-DoKo.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
 /* make sure that we are not called from outside the scripts,
  * use a variable defined in config.php to check this
  */
 /* make sure that we are not called from outside the scripts,
  * use a variable defined in config.php to check this
  */
@@ -11,17 +30,24 @@ if(!isset($HOST))
 
 function DB_open()
 {
 
 function DB_open()
 {
+  $version_needed = 5;
+
   global $DB,$DB_user,$DB_host,$DB_database,$DB_password;
   $DB = @mysql_connect($DB_host,$DB_user, $DB_password);
   if ( $DB )
     {
   global $DB,$DB_user,$DB_host,$DB_database,$DB_password;
   $DB = @mysql_connect($DB_host,$DB_user, $DB_password);
   if ( $DB )
     {
-      mysql_select_db($DB_database) or die('Could not select database');
+      mysql_select_db($DB_database) or die('Error: Could not select database');
     }
   else
     {
       echo mysql_errno() . ": " . mysql_error(). "\n";
       return -1;
     }
   else
     {
       echo mysql_errno() . ": " . mysql_error(). "\n";
       return -1;
-    }
+    };
+
+  $version = DB_get_version();
+  if ($version != $version_needed)
+    return -2;
+
   return 0;
 }
 
   return 0;
 }
 
@@ -498,6 +524,15 @@ function DB_update_game_timestamp($gameid)
   return;
 }
 
   return;
 }
 
+function DB_get_game_timestamp($gameid)
+{
+  $r = DB_query_array("SELECT mod_date FROM Game WHERE id=".DB_quote_smart($gameid));
+
+  if($r)
+    return $r[0];
+  else
+    return NULL;
+}
 
 function DB_update_user_timestamp($userid)
 {
 
 function DB_update_user_timestamp($userid)
 {
@@ -514,6 +549,17 @@ function DB_get_user_timestamp($userid)
   else
     return NULL;
 }
   else
     return NULL;
 }
+
+function DB_get_user_creation_date($userid)
+{
+  $r = DB_query_array("SELECT create_date FROM User WHERE id=".DB_quote_smart($userid));
+
+  if($r)
+    return $r[0];
+  else
+    return NULL;
+}
+
 function DB_get_user_timezone($userid)
 {
   $r = DB_query_array("SELECT timezone FROM User WHERE id=".DB_quote_smart($userid));
 function DB_get_user_timezone($userid)
 {
   $r = DB_query_array("SELECT timezone FROM User WHERE id=".DB_quote_smart($userid));
@@ -524,13 +570,31 @@ function DB_get_user_timezone($userid)
     return "Europe/London";
 }
 
     return "Europe/London";
 }
 
-function DB_insert_comment($comment,$playid,$userid)
+function DB_insert_comment($comment,$playid,$gameid,$userid)
 {
 {
-  DB_query("INSERT INTO Comment VALUES (NULL,NULL,NULL,$userid,$playid, ".DB_quote_smart($comment).")");
+  DB_query("INSERT INTO Comment VALUES (NULL,NULL,NULL,$userid,$playid,$gameid, ".DB_quote_smart($comment).")");
 
   return;
 }
 
 
   return;
 }
 
+function DB_get_pre_comment($gameid)
+{
+  $r = DB_query_array_all("SELECT comment, User.fullname FROM Comment".
+                         " LEFT JOIN User ON User.id=user_id".
+                         " WHERE play_id=-1".
+                         " AND game_id=$gameid ");
+  return $r;
+}
+
+function DB_get_pre_comment_call($gameid)
+{
+  $r = DB_query_array_all("SELECT comment, User.fullname FROM Comment".
+                         " LEFT JOIN User ON User.id=user_id".
+                         " WHERE play_id=-2".
+                         " AND game_id=$gameid ");
+  return $r;
+}
+
 function DB_insert_note($comment,$gameid,$userid)
 {
   DB_query("INSERT INTO Notes VALUES (NULL,NULL,NULL,$userid,$gameid, ".DB_quote_smart($comment).")");
 function DB_insert_note($comment,$gameid,$userid)
 {
   DB_query("INSERT INTO Notes VALUES (NULL,NULL,NULL,$userid,$gameid, ".DB_quote_smart($comment).")");
@@ -657,14 +721,15 @@ function DB_get_hashes_by_session($session,$user)
   return $r;
 }
 
   return $r;
 }
 
-function DB_get_ruleset($dullen,$schweinchen,$call)
+function DB_get_ruleset($dullen,$schweinchen,$call,$lowtrump)
 {
   $r = array();
 
   $result = DB_query("SELECT id FROM Rulesets WHERE".
                     " dullen=".DB_quote_smart($dullen)." AND ".
                     " Rulesets.call=".DB_quote_smart($call)." AND ".
 {
   $r = array();
 
   $result = DB_query("SELECT id FROM Rulesets WHERE".
                     " dullen=".DB_quote_smart($dullen)." AND ".
                     " Rulesets.call=".DB_quote_smart($call)." AND ".
-                    " schweinchen=".DB_quote_smart($schweinchen));
+                    " schweinchen=".DB_quote_smart($schweinchen)." AND ".
+                    " lowtrump=".DB_quote_smart($lowtrump));
   if($result)
     $r    = DB_fetch_array($result);
 
   if($result)
     $r    = DB_fetch_array($result);
 
@@ -676,6 +741,7 @@ function DB_get_ruleset($dullen,$schweinchen,$call)
       $result = DB_query("INSERT INTO Rulesets VALUES (NULL, NULL, ".
                         DB_quote_smart($dullen).",".
                         DB_quote_smart($schweinchen).",".
       $result = DB_query("INSERT INTO Rulesets VALUES (NULL, NULL, ".
                         DB_quote_smart($dullen).",".
                         DB_quote_smart($schweinchen).",".
+                        DB_quote_smart($lowtrump).",".
                         DB_quote_smart($call).
                         ", NULL)");
       if($result)
                         DB_quote_smart($call).
                         ", NULL)");
       if($result)
@@ -706,6 +772,18 @@ function DB_get_party_by_gameid_and_userid($gameid,$userid)
     return NULL;
 }
 
     return NULL;
 }
 
+function DB_get_party_by_session_and_userid($session,$userid)
+{
+  /* used in score table by index. gameids are sorted by date, so we need to sort here too! */
+  $r = DB_query_array_all("SELECT party FROM Hand".
+                         " LEFT JOIN Game ON Game.id = Hand.game_id".
+                         " WHERE Game.session=".DB_quote_smart($session).
+                         "  AND user_id=".DB_quote_smart($userid)."".
+                         "  AND Game.status='gameover' ".
+                         " ORDER BY Game.create_date ASC");
+  return $r;
+}
+
 function DB_set_party_by_hash($hash,$party)
 {
   DB_query("UPDATE Hand SET party=".DB_quote_smart($party)." WHERE hash=".DB_quote_smart($hash));
 function DB_set_party_by_hash($hash,$party)
 {
   DB_query("UPDATE Hand SET party=".DB_quote_smart($party)." WHERE hash=".DB_quote_smart($hash));
@@ -724,6 +802,7 @@ function DB_get_PREF($myid)
   $PREF['vacation_start']      =  NULL;
   $PREF['vacation_stop']       =  NULL;
   $PREF['vacation_comment']    =  '';
   $PREF['vacation_start']      =  NULL;
   $PREF['vacation_stop']       =  NULL;
   $PREF['vacation_comment']    =  '';
+  $PREF['language']            =  'en';
 
   /* get all preferences */
   $r = DB_query('SELECT pref_key, value FROM User_Prefs'.
 
   /* get all preferences */
   $r = DB_query('SELECT pref_key, value FROM User_Prefs'.
@@ -733,9 +812,6 @@ function DB_get_PREF($myid)
       switch($pref[0])
        {
        case 'cardset':
       switch($pref[0])
        {
        case 'cardset':
-         /* licence only valid until then */
-         if($pref[1]=='altenburg' && (time()-strtotime( '2009-12-31 23:59:59')<0) )
-           $PREF['cardset']='altenburg';
          break;
 
        case 'email':
          break;
 
        case 'email':
@@ -777,8 +853,14 @@ function DB_get_PREF($myid)
          if($pref[1])
            $PREF['vacation_comment'] = $pref[1];
          break;
          if($pref[1])
            $PREF['vacation_comment'] = $pref[1];
          break;
+
+       case 'language':
+         if($pref[1])
+           $PREF['language'] = $pref[1];
+         break;
        }
     }
        }
     }
+
   return $PREF;
 }
 
   return $PREF;
 }
 
@@ -788,9 +870,10 @@ function DB_get_RULES($gameid)
                      " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
                      " WHERE Game.id='$gameid'" );
 
                      " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
                      " WHERE Game.id='$gameid'" );
 
-  $RULES["dullen"]      = $r[2];
-  $RULES["schweinchen"] = $r[3];
-  $RULES["call"]        = $r[4];
+  $RULES['dullen']      = $r[2];
+  $RULES['schweinchen'] = $r[3];
+  $RULES['lowtrump']    = $r[4];
+  $RULES['call']        = $r[5];
 
   return $RULES;
 }
 
   return $RULES;
 }
@@ -865,6 +948,12 @@ function DB_set_recovery_password($user,$newpw)
   return;
 }
 
   return;
 }
 
+function DB_delete_recovery_passwords($userid)
+{
+  DB_query("DELETE FROM Recovery WHERE user_id=".DB_quote_smart($userid));
+  return;
+}
+
 function DB_get_card_name($card)
 {
   if($card==0)
 function DB_get_card_name($card)
 {
   if($card==0)
@@ -880,16 +969,17 @@ function DB_get_card_name($card)
 
 function DB_get_current_playid($gameid)
 {
 
 function DB_get_current_playid($gameid)
 {
+  /* return playid or -1 for pre-game phase */
   $trick = DB_get_max_trickid($gameid);
 
   $trick = DB_get_max_trickid($gameid);
 
-  if(!$trick) return NULL;
+  if(!$trick) return -1;
 
   $r = DB_query_array("SELECT id FROM Play WHERE trick_id='$trick' ORDER BY create_date DESC LIMIT 1");
 
   if($r)
     return $r[0];
 
 
   $r = DB_query_array("SELECT id FROM Play WHERE trick_id='$trick' ORDER BY create_date DESC LIMIT 1");
 
   if($r)
     return $r[0];
 
-  return "";
+  return -1;
 }
 
 function DB_get_call_by_hash($hash)
 }
 
 function DB_get_call_by_hash($hash)
@@ -976,48 +1066,24 @@ function DB_is_session_active($session)
     return -1;
 }
 
     return -1;
 }
 
-function DB_get_score_by_gameid($gameid)
-{
-  /* returns the points of a game from the point of the re parth (<0 if they lost) */
-  $queryresult = DB_query("SELECT COUNT(*),party FROM Score ".
-                         "  WHERE game_id=$gameid ".
-                         "  GROUP BY party ");
-  $re     = 0;
-  $contra = 0;
-
-  while($r = DB_fetch_array($queryresult) )
-    {
-      if($r[1] == "re")
-       $re += $r[0];
-      else if ($r[1] == "contra")
-       $contra += $r[0];
-    };
-
-  return ($re - $contra);
-}
-
 function DB_get_gameids_of_finished_games_by_session($session)
 {
   $ids = array ();
 
   if($session==0) /* return all games */
 function DB_get_gameids_of_finished_games_by_session($session)
 {
   $ids = array ();
 
   if($session==0) /* return all games */
-    $queryresult = DB_query("SELECT id FROM Game ".
-                           " WHERE status='gameover' ".
-                           " ORDER BY create_date ASC");
+    $queryresult = DB_query_array_all("SELECT Game.id,SUM(IF(STRCMP(Score.party,'re'),-1,1)),Game.type FROM Game ".
+                                 " LEFT JOIN Score on game_id=Game.id".
+                                 " WHERE status='gameover' ".
+                                 " GROUP BY Game.id");
   else   /* return games in a session */
   else   /* return games in a session */
-    $queryresult = DB_query("SELECT id FROM Game ".
-                           "  WHERE session=$session ".
-                           "   AND status='gameover' ".
-                           " ORDER BY create_date ASC");
-
-  $i=0;
-  while($r = DB_fetch_array($queryresult) )
-    {
-      $ids[$i] = $r[0];
-      $i++;
-    }
+    $queryresult = DB_query_array_all("SELECT Game.id,SUM(IF(STRCMP(Score.party,'re'),-1,1)),Game.type FROM Game ".
+                                 " LEFT JOIN Score on game_id=Game.id".
+                                 "  WHERE session=$session ".
+                                 "   AND status='gameover' ".
+                                 " GROUP BY Game.id".
+                                 " ORDER BY Game.create_date ASC");
 
 
-  return $ids;
+  return $queryresult;
 }
 
 function DB_get_card_value_by_cardid($id)
 }
 
 function DB_get_card_value_by_cardid($id)
@@ -1179,9 +1245,12 @@ function DB_get_number_of_tricks($gameid,$position)
   return $r[0];
 }
 
   return $r[0];
 }
 
-function DB_digest_insert_email($To,$message)
+function DB_digest_insert_email($To,$message,$type,$gameid)
 {
 {
-  DB_query("INSERT INTO digest_email VALUES (NULL,".DB_quote_smart($To).",NULL,".DB_quote_smart($message).")");
+  if($type == GAME_YOUR_TURN)
+    DB_query("INSERT INTO digest_email VALUES (NULL,".DB_quote_smart($To).",NULL,'your_turn',$gameid,".DB_quote_smart($message).")");
+  else
+    DB_query("INSERT INTO digest_email VALUES (NULL,".DB_quote_smart($To).",NULL,'misc',NULL,".DB_quote_smart($message).")");
   return;
 }
 
   return;
 }
 
@@ -1200,7 +1269,7 @@ function DB_get_digest_message_by_email($email)
 {
   $messages = array();
 
 {
   $messages = array();
 
-  $result = DB_query("SELECT id,content FROM digest_email Where email='$email'");
+  $result = DB_query("SELECT id,content,type,game_id FROM digest_email Where email='$email'");
   while($r = DB_fetch_array($result))
     $messages[]=$r;
 
   while($r = DB_fetch_array($result))
     $messages[]=$r;