2 /* make sure that we are not called from outside the scripts,
3 * use a variable defined in config.php to check this
14 global $DB,$DB_user,$DB_host,$DB_database,$DB_password;
15 $DB = @mysql_connect($DB_host,$DB_user, $DB_password);
18 mysql_select_db($DB_database) or die('Could not select database');
33 function DB_quote_smart($value)
36 if (get_magic_quotes_gpc()) {
37 $value = stripslashes($value);
39 /* Quote if not a number or a numeric string */
40 if (!is_numeric($value)) {
41 $value = "'" . mysql_real_escape_string($value) . "'";
48 $result = DB_query("SELECT * FROM User");
49 while($r = DB_fetch_array($result))
58 /* use Mysql in the background */
59 function DB_query($query)
61 return mysql_query($query);
64 function DB_fetch_array($result)
66 return mysql_fetch_array($result,MYSQL_NUM);
69 function DB_insert_id()
71 return mysql_insert_id();
74 function DB_num_rows($result)
76 return mysql_num_rows($result);
78 /* end Mysql functions */
80 function DB_query_array($query)
82 $result = DB_query($query);
83 $return = DB_fetch_array($result);
88 function DB_query_array_all($query)
92 $queryresult = DB_query($query);
93 while($row = DB_fetch_array($queryresult))
99 function DB_get_passwd_by_name($name)
101 $r = DB_query_array("SELECT password FROM User WHERE fullname=".DB_quote_smart($name)."");
109 function DB_get_passwd_by_userid($id)
111 $r = DB_query_array("SELECT password FROM User WHERE id=".DB_quote_smart($id)."");
119 function DB_check_recovery_passwords($password,$email)
121 $r = DB_query_array("SELECT User.id FROM User".
122 " LEFT JOIN Recovery ON User.id=Recovery.user_id".
123 " WHERE email=".DB_quote_smart($email).
124 " AND Recovery.password=".DB_quote_smart($password).
125 " AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= Recovery.create_date");
132 function DB_get_handid($type,$var1='',$var2='')
137 $r = DB_query_array("SELECT id FROM Hand WHERE hash=".DB_quote_smart($var1));
139 case 'gameid-position':
140 $r = DB_query_array("SELECT id FROM Hand WHERE game_id=".
141 DB_quote_smart($var1)." AND position=".
142 DB_quote_smart($var2));
144 case 'gameid-userid':
145 $r = DB_query_array("SELECT id FROM Hand WHERE game_id=".
146 DB_quote_smart($var1)." AND user_id=".
147 DB_quote_smart($var2));
157 function DB_get_pos_by_hash($hash)
159 $r= DB_query_array("SELECT position FROM Hand WHERE hash=".DB_quote_smart($hash));
167 function DB_get_status_by_hash($hash)
169 $r= DB_query_array("SELECT status FROM Hand WHERE hash=".DB_quote_smart($hash));
177 function DB_set_game_status_by_gameid($id,$status)
179 DB_query("UPDATE Game SET status='".$status."' WHERE id=".DB_quote_smart($id));
183 function DB_set_sickness_by_gameid($id,$status)
185 DB_query("UPDATE Game SET sickness='".$status."' WHERE id=".DB_quote_smart($id));
188 function DB_get_sickness_by_gameid($id)
190 $r = DB_query_array("SELECT sickness FROM Game WHERE id=".DB_quote_smart($id));
198 function DB_get_game_status_by_gameid($id)
200 $r = DB_query_array("SELECT status FROM Game WHERE id=".DB_quote_smart($id));
208 function DB_set_hand_status_by_hash($hash,$status)
210 DB_query("UPDATE Hand SET status='".$status."' WHERE hash=".DB_quote_smart($hash));
214 function DB_get_hand_status_by_userid_and_gameid($uid,$gid)
216 $r = DB_query_array("SELECT status FROM Hand WHERE user_id=".DB_quote_smart($uid).
217 " AND game_id=".DB_quote_smart($gid));
224 function DB_get_sickness_by_userid_and_gameid($uid,$gid)
226 $r = DB_query_array("SELECT sickness FROM Hand WHERE user_id=".DB_quote_smart($uid).
227 " AND game_id=".DB_quote_smart($gid));
234 function DB_get_sickness_by_pos_and_gameid($pos,$gid)
236 $r = DB_query_array("SELECT sickness FROM Hand WHERE position=".DB_quote_smart($pos).
237 " AND game_id=".DB_quote_smart($gid));
244 function DB_get_gameid_by_hash($hash)
246 $r = DB_query_array("SELECT game_id FROM Hand WHERE hash=".DB_quote_smart($hash));
254 function DB_cancel_game($hash)
256 $gameid = DB_get_gameid_by_hash($hash);
261 /* get the IDs of all players */
262 $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
263 while($r = DB_fetch_array($result))
267 $tmp = DB_query_array("SELECT id FROM Hand_Card WHERE hand_id=".DB_quote_smart($id));
268 DB_query("DELETE FROM Play WHERE hand_card_id=".DB_quote_smart($tmp[0]));
270 DB_query("DELETE FROM Hand_Card WHERE hand_id=".DB_quote_smart($id));
271 DB_query("DELETE FROM Hand WHERE id=".DB_quote_smart($id));
275 DB_query("DELETE FROM User_Game_Prefs WHERE game_id=".DB_quote_smart($gameid));
276 DB_query("DELETE FROM Trick WHERE game_id=".DB_quote_smart($gameid));
277 DB_query("DELETE FROM Game WHERE id=".DB_quote_smart($gameid));
282 function DB_get_hand($me)
286 $handid = DB_get_handid('hash',$me);
288 $result = DB_query("SELECT card_id FROM Hand_Card WHERE hand_id=".DB_quote_smart($handid)." and played='false' ");
289 while($r = DB_fetch_array($result))
295 function DB_get_all_hand($me)
299 $handid = DB_get_handid('hash',$me);
301 $result = DB_query("SELECT card_id FROM Hand_Card WHERE hand_id=".DB_quote_smart($handid));
302 while($r = DB_fetch_array($result))
308 function DB_get_cards_by_trick($id)
313 $result = DB_query("SELECT card_id,position FROM Play LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id ".
314 "LEFT JOIN Hand ON Hand.id=Hand_Card.hand_id ".
316 DB_quote_smart($id)." ORDER BY sequence ASC");
317 while($r = DB_fetch_array($result))
319 $cards[$i]=array("card"=>$r[0],"pos"=>$r[1]);
327 function DB_set_solo_by_hash($hash,$solo)
329 DB_query("UPDATE Hand SET solo=".DB_quote_smart($solo)." WHERE hash=".DB_quote_smart($hash));
333 function DB_set_solo_by_gameid($id,$solo)
335 DB_query("UPDATE Game SET solo=".DB_quote_smart($solo)." WHERE id=".DB_quote_smart($id));
339 function DB_set_sickness_by_hash($hash,$sickness)
341 DB_query("UPDATE Hand SET sickness=".DB_quote_smart($sickness)." WHERE hash=".DB_quote_smart($hash));
345 function DB_get_current_trickid($gameid)
351 $result = DB_query("SELECT Trick.id,MAX(Play.sequence) FROM Play ".
352 "LEFT JOIN Trick ON Play.trick_id=Trick.id ".
353 "WHERE Trick.game_id=".DB_quote_smart($gameid)." ".
354 "GROUP BY Trick.id");
355 while( $r = DB_fetch_array($result) )
362 if(!$sequence || $sequence==4)
364 DB_query("INSERT INTO Trick VALUES (NULL,NULL,NULL, ".DB_quote_smart($gameid).",NULL)");
365 $trickid = DB_insert_id();
374 return array($trickid,$sequence,$number);
377 function DB_get_max_trickid($gameid)
379 $r = DB_query_array("SELECT MAX(id) FROM Trick WHERE game_id=".DB_quote_smart($gameid));
381 return ($r?$r[0]:NULL);
384 function DB_play_card($trickid,$handcardid,$sequence)
386 DB_query("INSERT INTO Play VALUES(NULL,NULL,NULL,".DB_quote_smart($trickid).
387 ",".DB_quote_smart($handcardid).",".DB_quote_smart($sequence).")");
389 $playid = DB_insert_id();
393 function DB_get_all_names_by_gameid($id)
397 $result = DB_query("SELECT fullname FROM Hand LEFT JOIN User ON Hand.user_id=User.id WHERE game_id=".
398 DB_quote_smart($id)." ORDER BY position ASC");
399 while($r = DB_fetch_array($result))
405 function DB_get_all_userid_by_gameid($id)
409 $result = DB_query("SELECT user_id FROM Hand WHERE game_id=".
410 DB_quote_smart($id)." ORDER BY position ");
411 while($r = DB_fetch_array($result))
417 function DB_get_hash_from_game_and_pos($id,$pos)
419 $r = DB_query_array("SELECT hash FROM Hand WHERE game_id=".DB_quote_smart($id)." and position=".DB_quote_smart($pos));
427 function DB_get_hash_from_gameid_and_userid($id,$user)
429 $r = DB_query_array("SELECT hash FROM Hand WHERE game_id=".DB_quote_smart($id)." and user_id=".DB_quote_smart($user));
437 function DB_get_all_names()
441 $result = DB_query("SELECT fullname FROM User");
442 while($r = DB_fetch_array($result))
448 function DB_get_names_of_last_logins($N)
452 $result = DB_query("SELECT fullname FROM User ORDER BY last_login DESC LIMIT $N");
453 while($r = DB_fetch_array($result))
459 function DB_get_names_of_new_logins($N)
463 $result = DB_query("SELECT fullname FROM User ORDER BY create_date DESC, id DESC LIMIT $N");
464 while($r = DB_fetch_array($result))
470 function DB_update_game_timestamp($gameid)
472 DB_query("UPDATE Game SET mod_date = CURRENT_TIMESTAMP WHERE id=".DB_quote_smart($gameid));
477 function DB_update_user_timestamp($userid)
479 DB_query("UPDATE User SET last_login = CURRENT_TIMESTAMP WHERE id=".DB_quote_smart($userid));
483 function DB_get_user_timestamp($userid)
485 $r = DB_query_array("SELECT last_login FROM User WHERE id=".DB_quote_smart($userid));
492 function DB_get_user_timezone($userid)
494 $r = DB_query_array("SELECT timezone FROM User WHERE id=".DB_quote_smart($userid));
499 return "Europe/London";
502 function DB_insert_comment($comment,$playid,$userid)
504 DB_query("INSERT INTO Comment VALUES (NULL,NULL,NULL,$userid,$playid, ".DB_quote_smart($comment).")");
509 function DB_insert_note($comment,$gameid,$userid)
511 DB_query("INSERT INTO Notes VALUES (NULL,NULL,NULL,$userid,$gameid, ".DB_quote_smart($comment).")");
516 function DB_get_notes_by_userid_and_gameid($userid,$gameid)
520 $result = DB_query("SELECT comment FROM Notes WHERE user_id=".DB_quote_smart($userid) .
521 " AND game_id=".DB_quote_smart($gameid));
523 while($r = DB_fetch_array($result))
530 function DB_get_gametype_by_gameid($id)
532 $r = DB_query_array("SELECT type FROM Game WHERE id=".DB_quote_smart($id));
540 function DB_set_gametype_by_gameid($id,$p)
542 DB_query("UPDATE Game SET type='".$p."' WHERE id=".DB_quote_smart($id));
546 function DB_get_solo_by_gameid($id)
548 $r = DB_query_array("SELECT solo FROM Game WHERE id=".DB_quote_smart($id));
557 function DB_get_startplayer_by_gameid($id)
559 $r = DB_query_array("SELECT startplayer FROM Game WHERE id=".DB_quote_smart($id));
567 function DB_set_startplayer_by_gameid($id,$p)
569 DB_query("UPDATE Game SET startplayer='".$p."' WHERE id=".DB_quote_smart($id));
573 function DB_get_player_by_gameid($id)
575 $r = DB_query_array("SELECT player FROM Game WHERE id=".DB_quote_smart($id));
582 function DB_set_player_by_gameid($id,$p)
584 DB_query("UPDATE Game SET player='".DB_quote_smart($p)."' WHERE id=".DB_quote_smart($id));
590 function DB_get_ruleset_by_gameid($id)
592 $r = DB_query_array("SELECT ruleset FROM Game WHERE id=".DB_quote_smart($id));
600 function DB_get_session_by_gameid($id)
602 $r = DB_query_array("SELECT session FROM Game WHERE id=".DB_quote_smart($id));
610 function DB_get_max_session()
612 $r = DB_query_array("SELECT MAX(session) FROM Game");
620 function DB_get_hashes_by_session($session,$user)
624 $result = DB_query("SELECT Hand.hash FROM Hand".
625 " LEFT JOIN Game ON Game.id=Hand.game_id ".
626 " WHERE Game.session=".DB_quote_smart($session).
627 " AND Hand.user_id=".DB_quote_smart($user).
628 " ORDER BY Game.create_date ASC");
629 while($t = DB_fetch_array($result))
635 function DB_get_ruleset($dullen,$schweinchen,$call)
639 $result = DB_query("SELECT id FROM Rulesets WHERE".
640 " dullen=".DB_quote_smart($dullen)." AND ".
641 " call=".DB_quote_smart($call)." AND ".
642 " schweinchen=".DB_quote_smart($schweinchen));
644 $r = DB_fetch_array($result);
647 return $r[0]; /* found ruleset */
651 $result = DB_query("INSERT INTO Rulesets VALUES (NULL, NULL, ".
652 DB_quote_smart($dullen).",".
653 DB_quote_smart($schweinchen).",".
654 DB_quote_smart($call).
657 return DB_insert_id();
660 return -1; /* something went wrong */
663 function DB_get_party_by_hash($hash)
665 $r = DB_query_array("SELECT party FROM Hand WHERE hash=".DB_quote_smart($hash));
673 function DB_get_party_by_gameid_and_userid($gameid,$userid)
675 $r = DB_query_array("SELECT party FROM Hand".
676 " WHERE game_id=".DB_quote_smart($gameid).
677 " AND user_id=".DB_quote_smart($userid));
684 function DB_set_party_by_hash($hash,$party)
686 DB_query("UPDATE Hand SET party=".DB_quote_smart($party)." WHERE hash=".DB_quote_smart($hash));
690 function DB_get_PREF($myid)
693 $r = DB_query_array("SELECT value from User_Prefs".
694 " WHERE user_id='$myid' AND pref_key='cardset'" );
697 /* licence only valid until then */
698 if($r[0]=="altenburg" && (time()-strtotime( "2009-12-31 23:59:59")<0) )
699 $PREF["cardset"]="altenburg";
701 $PREF["cardset"]="english";
704 $PREF["cardset"]="english";
707 $r = DB_query_array("SELECT value FROM User_Prefs".
708 " WHERE user_id='$myid' AND pref_key='email'" );
711 if($r[0]=="emailaddict")
712 $PREF["email"]="emailaddict";
714 $PREF["email"]="emailnonaddict";
717 $PREF["email"]="emailnonaddict";
720 $r = DB_query_array("SELECT value FROM User_Prefs".
721 " WHERE user_id='$myid' AND pref_key='autosetup'" );
725 $PREF['autosetup']='yes';
727 $PREF['autosetup']='no';
730 $PREF['autosetup']='no';
735 function DB_get_RULES($gameid)
737 $r = DB_query_array("SELECT * FROM Rulesets".
738 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
739 " WHERE Game.id='$gameid'" );
741 $RULES["dullen"] = $r[2];
742 $RULES["schweinchen"] = $r[3];
743 $RULES["call"] = $r[4];
748 function DB_get_email_pref_by_hash($hash)
750 $r = DB_query_array("SELECT value FROM Hand".
751 " LEFT JOIN User_Prefs ON Hand.user_id=User_Prefs.user_id".
752 " WHERE hash='$hash' AND pref_key='email'" );
755 if($r[0]=="emailaddict")
756 return "emailaddict";
758 return "emailnonaddict";
761 return "emailnonaddict";
764 function DB_get_email_pref_by_uid($uid)
766 $r = DB_query_array("SELECT value FROM User_Prefs ".
767 " WHERE user_id='$uid' AND pref_key='email'" );
770 if($r[0]=="emailaddict")
771 return "emailaddict";
773 return "emailnonaddict";
776 return "emailnonaddict";
779 function DB_get_unused_randomnumbers($userstr)
781 $r = DB_query_array(" SELECT randomnumbers FROM Game".
782 " WHERE randomnumbers NOT IN".
783 " (SELECT randomnumbers FROM Game".
784 " LEFT JOIN Hand ON Game.id=Hand.game_id".
785 " WHERE user_id IN (". $userstr .")".
786 " GROUP BY randomnumbers".
794 function DB_get_number_of_passwords_recovery($user)
796 $r = DB_query_array("SELECT COUNT(*) FROM Recovery ".
797 " WHERE user_id=$user ".
798 " AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= create_date".
799 " GROUP BY user_id " );
806 function DB_set_recovery_password($user,$newpw)
808 DB_query("INSERT INTO Recovery VALUES(NULL,".DB_quote_smart($user).
809 ",".DB_quote_smart($newpw).",NULL)");
813 function DB_get_card_name($card)
815 $r = DB_query_array("SELECT strength,suite FROM Card WHERE id='$card'");
818 return $r[0]." of ".$r[1];
820 return "Error during get_card_name ".$card;
823 function DB_get_current_playid($gameid)
825 $trick = DB_get_max_trickid($gameid);
827 if(!$trick) return NULL;
829 $r = DB_query_array("SELECT id FROM Play WHERE trick_id='$trick' ORDER BY create_date DESC LIMIT 1");
837 function DB_get_call_by_hash($hash)
839 $r = DB_query_array("SELECT point_call FROM Hand WHERE hash='$hash'");
847 function DB_get_partner_call_by_hash($hash)
849 $partner = DB_get_partner_hash_by_hash($hash);
853 $r = DB_query_array("SELECT point_call FROM Hand WHERE hash='$partner'");
862 function DB_get_partner_hash_by_hash($hash)
864 $gameid = DB_get_gameid_by_hash($hash);
865 $party = DB_get_party_by_hash($hash);
867 $r = DB_query_array("SELECT hash FROM Hand WHERE game_id='$gameid' AND party='$party' AND hash<>'$hash'");
875 function DB_format_gameid($gameid)
877 $session = DB_get_session_by_gameid($gameid);
879 /* get number of game */
880 $r = DB_query_array("SELECT SUM(TIME_TO_SEC(TIMEDIFF(create_date, (SELECT create_date FROM Game WHERE id='$gameid')))<=0) ".
882 " WHERE session='$session' ");
883 return $session.".".$r[0];
886 function DB_get_reminder($user,$gameid)
888 $r = DB_query_array("SELECT COUNT(*) FROM Reminder ".
889 " WHERE user_id=$user ".
890 " AND game_id=$gameid ".
891 " AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= create_date".
892 " GROUP BY user_id " );
899 function DB_set_reminder($user,$gameid)
901 DB_query("INSERT INTO Reminder ".
902 " VALUES(NULL, ".DB_quote_smart($user).", ".DB_quote_smart($gameid).
907 function DB_is_session_active($session)
909 $r = DB_query_array("SELECT COUNT(*) FROM Game ".
910 " WHERE session=$session ".
911 " AND status<>'gameover' ");
918 function DB_get_score_by_gameid($gameid)
920 /* returns the points of a game from the point of the re parth (<0 if they lost) */
921 $queryresult = DB_query("SELECT COUNT(*),party FROM Score ".
922 " WHERE game_id=$gameid ".
927 while($r = DB_fetch_array($queryresult) )
931 else if ($r[1] == "contra")
935 return ($re - $contra);
938 function DB_get_gameids_of_finished_games_by_session($session)
942 if($session==0) /* return all games */
943 $queryresult = DB_query("SELECT id FROM Game ".
944 " WHERE status='gameover' ".
945 " ORDER BY create_date ASC");
946 else /* return games in a session */
947 $queryresult = DB_query("SELECT id FROM Game ".
948 " WHERE session=$session ".
949 " AND status='gameover' ".
950 " ORDER BY create_date ASC");
953 while($r = DB_fetch_array($queryresult) )
962 function DB_get_card_value_by_cardid($id)
964 $r = DB_query_array("SELECT points FROM Card ".
973 function DB_get_userid($type,$var1="",$var2="")
975 /* get the userid of a user
976 * this can be done several ways, which are all handled below
977 * if a email/password combination is given and it doesn't work, we also
978 * need to check the recovery table for additional passwords
986 $result = DB_query("SELECT id FROM User WHERE fullname=".DB_quote_smart($var1));
989 $result = DB_query("SELECT user_id FROM Hand WHERE hash=".DB_quote_smart($var1));
992 $result = DB_query("SELECT id FROM User WHERE password=".DB_quote_smart($var1));
995 $result = DB_query("SELECT id FROM User WHERE email=".DB_quote_smart($var1));
997 case 'email-password':
998 $result = DB_query("SELECT id FROM User WHERE email=".DB_quote_smart($var1)." AND password=".DB_quote_smart($var2));
999 $r = DB_fetch_array($result);
1000 /* test if a recovery password has been set */
1003 echo "testing alternative password";
1004 $result = DB_query("SELECT User.id FROM User".
1005 " LEFT JOIN Recovery ON User.id=Recovery.user_id".
1006 " WHERE email=".DB_quote_smart($var1).
1007 " AND Recovery.password=".DB_quote_smart($var2).
1008 " AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= Recovery.create_date");
1011 case 'gameid-position':
1012 $result = DB_query("SELECT user_id FROM Hand WHERE game_id=".
1013 DB_quote_smart($var1)." AND position=".
1014 DB_quote_smart($var2));
1019 $r = DB_fetch_array($result);
1027 function DB_get_email($type,$var1='',$var2='')
1029 /* return the email of a user
1030 * this is used for sending out emails, but also for
1031 * testing the login for example
1036 $result = DB_query("SELECT email FROM User WHERE fullname=".DB_quote_smart($var1)."");
1039 $result = DB_query("SELECT email FROM User WHERE id=".DB_quote_smart($var1)."");
1042 $result = DB_query("SELECT User.email FROM User ".
1043 "LEFT JOIN Hand ON Hand.user_id=User.id ".
1044 "WHERE Hand.hash=".DB_quote_smart($var1)."");
1046 case 'position-gameid':
1047 $result = DB_query("SELECT email FROM User ".
1048 "LEFT JOIN Hand ON User.id=Hand.user_id ".
1049 "LEFT JOIN Game ON Game.id=Hand.game_id ".
1050 "WHERE Game.id=".DB_quote_smart($var2)." ".
1051 "AND Hand.position=".DB_quote_smart($var1)."");
1055 $r = DB_fetch_array($result);
1063 function DB_get_name($type,$var1='')
1065 /* get the full name of a user
1066 * a user can be uniquely identified several ways
1071 $r = DB_query_array("SELECT fullname FROM Hand LEFT JOIN User ON Hand.user_id=User.id WHERE hash=".DB_quote_smart($var1));
1074 $r = DB_query_array("SELECT fullname FROM User WHERE email=".DB_quote_smart($var1));
1077 $r = DB_query_array("SELECT fullname FROM User WHERE id=".DB_quote_smart($var1));
1086 function DB_add_exchanged_card($card,$old_hand_id,$new_hand_id)
1088 DB_query("INSERT INTO Card_Exchange VALUES (NULL,$new_hand_id,$old_hand_id,$card)");
1092 function DB_get_exchanged_cards($hash)
1096 $handid = DB_get_handid('hash',$hash);
1098 $result = DB_query("SELECT card_id FROM Card_Exchange WHERE orig_hand_id=".DB_quote_smart($handid));
1099 while($r = DB_fetch_array($result))