NEW FEATURE: new user settings allows people to not be available for new games
[e-DoKo.git] / include / db.php
1 <?php
2 /* make sure that we are not called from outside the scripts,
3  * use a variable defined in config.php to check this
4  */
5 if(!isset($HOST))
6   exit;
7
8 /*
9  * open database
10  */
11
12 function DB_open()
13 {
14   global $DB,$DB_user,$DB_host,$DB_database,$DB_password;
15   $DB = @mysql_connect($DB_host,$DB_user, $DB_password);
16   if ( $DB )
17     {
18       mysql_select_db($DB_database) or die('Could not select database');
19     }
20   else
21     return -1;
22
23   return 0;
24 }
25
26 function DB_close()
27 {
28   global $DB;
29   mysql_close($DB);
30   return;
31 }
32
33 function DB_quote_smart($value)
34 {
35     /* Stripslashes */
36     if (get_magic_quotes_gpc()) {
37         $value = stripslashes($value);
38     }
39     /* Quote if not a number or a numeric string */
40     if (!is_numeric($value)) {
41         $value = "'" . mysql_real_escape_string($value) . "'";
42     }
43     return $value;
44 }
45
46 function DB_test()
47 {
48   $result = DB_query("SELECT * FROM User");
49   while($r = DB_fetch_array($result))
50     {
51       foreach($r as $thing)
52         echo "  $thing ";
53       echo "<br />\n";
54     }
55   return;
56 }
57
58 /* use Mysql in the background */
59 function DB_query($query)
60 {
61   return mysql_query($query);
62 }
63
64 function DB_fetch_array($result)
65 {
66   return mysql_fetch_array($result,MYSQL_NUM);
67 }
68
69 function DB_insert_id()
70 {
71   return mysql_insert_id();
72 }
73
74 function DB_num_rows($result)
75 {
76   return mysql_num_rows($result);
77 }
78 /* end Mysql functions */
79
80 function DB_query_array($query)
81 {
82   $result = DB_query($query);
83   $return = DB_fetch_array($result);
84
85   return $return;
86 }
87
88 function DB_query_array_all($query)
89 {
90   $result = array();
91
92   $queryresult  = DB_query($query);
93   while($row = DB_fetch_array($queryresult))
94     $result[] = $row;
95
96   return $result;
97 }
98
99 function DB_get_passwd_by_name($name)
100 {
101   $r = DB_query_array("SELECT password FROM User WHERE fullname=".DB_quote_smart($name)."");
102
103   if($r)
104     return $r[0];
105   else
106     return "";
107 }
108
109 function DB_get_passwd_by_userid($id)
110 {
111   $r = DB_query_array("SELECT password FROM User WHERE id=".DB_quote_smart($id)."");
112
113   if($r)
114     return $r[0];
115   else
116     return "";
117 }
118
119 function DB_check_recovery_passwords($password,$email)
120 {
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");
126   if($r)
127     return 1;
128   else
129     return 0;
130 }
131
132 function DB_get_handid($type,$var1='',$var2='')
133 {
134   switch($type)
135     {
136     case 'hash':
137       $r = DB_query_array("SELECT id FROM Hand WHERE hash=".DB_quote_smart($var1));
138       break;
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));
143       break;
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));
148       break;
149     }
150
151   if($r)
152     return $r[0];
153   else
154     return 0;
155 }
156
157 function DB_get_pos_by_hash($hash)
158 {
159   $r= DB_query_array("SELECT position FROM Hand WHERE hash=".DB_quote_smart($hash));
160
161   if($r)
162     return $r[0];
163   else
164     return 0;
165 }
166
167 function DB_get_status_by_hash($hash)
168 {
169   $r= DB_query_array("SELECT status FROM Hand WHERE hash=".DB_quote_smart($hash));
170
171   if($r)
172     return $r[0];
173   else
174     return 0;
175 }
176
177 function DB_set_game_status_by_gameid($id,$status)
178 {
179   DB_query("UPDATE Game SET status='".$status."' WHERE id=".DB_quote_smart($id));
180   return;
181 }
182
183 function DB_set_sickness_by_gameid($id,$status)
184 {
185   DB_query("UPDATE Game SET sickness='".$status."' WHERE id=".DB_quote_smart($id));
186   return;
187 }
188 function DB_get_sickness_by_gameid($id)
189 {
190   $r = DB_query_array("SELECT sickness FROM Game WHERE id=".DB_quote_smart($id));
191
192   if($r)
193     return $r[0];
194   else
195     return NULL;
196 }
197
198 function DB_get_game_status_by_gameid($id)
199 {
200   $r = DB_query_array("SELECT status FROM Game WHERE id=".DB_quote_smart($id));
201
202   if($r)
203     return $r[0];
204   else
205     return NULL;
206 }
207
208 function DB_set_hand_status_by_hash($hash,$status)
209 {
210   DB_query("UPDATE Hand SET status='".$status."' WHERE hash=".DB_quote_smart($hash));
211   return;
212 }
213
214 function DB_get_hand_status_by_userid_and_gameid($uid,$gid)
215 {
216   $r = DB_query_array("SELECT status FROM Hand WHERE user_id=".DB_quote_smart($uid).
217                       " AND game_id=".DB_quote_smart($gid));
218   if($r)
219     return $r[0];
220   else
221     return 0;
222 }
223
224 function DB_get_sickness_by_userid_and_gameid($uid,$gid)
225 {
226   $r = DB_query_array("SELECT sickness FROM Hand WHERE user_id=".DB_quote_smart($uid).
227                       " AND game_id=".DB_quote_smart($gid));
228   if($r)
229     return $r[0];
230   else
231     return 0;
232 }
233
234 function DB_get_sickness_by_pos_and_gameid($pos,$gid)
235 {
236   $r = DB_query_array("SELECT sickness FROM Hand WHERE position=".DB_quote_smart($pos).
237                       " AND game_id=".DB_quote_smart($gid));
238   if($r)
239     return $r[0];
240   else
241     return 0;
242 }
243
244 function DB_get_gameid_by_hash($hash)
245 {
246   $r = DB_query_array("SELECT game_id FROM Hand WHERE hash=".DB_quote_smart($hash));
247
248   if($r)
249     return $r[0];
250   else
251     return 0;
252 }
253
254 function DB_cancel_game($hash)
255 {
256   $gameid = DB_get_gameid_by_hash($hash);
257
258   if(!$gameid)
259     return;
260
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))
264     {
265       $id = $r[0];
266
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]));
269
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));
272     }
273
274   /* delete game */
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));
278
279   return;
280 }
281
282 function DB_get_hand($me)
283 {
284   $cards = array();
285
286   $handid = DB_get_handid('hash',$me);
287
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))
290     $cards[]=$r[0];
291
292   return $cards;
293 }
294
295 function DB_get_all_hand($me)
296 {
297   $cards = array();
298
299   $handid = DB_get_handid('hash',$me);
300
301   $result = DB_query("SELECT card_id FROM Hand_Card WHERE hand_id=".DB_quote_smart($handid));
302   while($r = DB_fetch_array($result))
303     $cards[]=$r[0];
304
305   return $cards;
306 }
307
308 function DB_get_cards_by_trick($id)
309 {
310   $cards = array();
311   $i     = 1;
312
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 ".
315                      "WHERE trick_id=".
316                      DB_quote_smart($id)." ORDER BY sequence ASC");
317   while($r = DB_fetch_array($result))
318     {
319       $cards[$i]=array("card"=>$r[0],"pos"=>$r[1]);
320       $i++;
321     }
322
323   return $cards;
324 }
325
326
327 function DB_set_solo_by_hash($hash,$solo)
328 {
329   DB_query("UPDATE Hand SET solo=".DB_quote_smart($solo)." WHERE hash=".DB_quote_smart($hash));
330   return;
331 }
332
333 function DB_set_solo_by_gameid($id,$solo)
334 {
335   DB_query("UPDATE Game SET solo=".DB_quote_smart($solo)." WHERE id=".DB_quote_smart($id));
336   return;
337 }
338
339 function DB_set_sickness_by_hash($hash,$sickness)
340 {
341   DB_query("UPDATE Hand SET sickness=".DB_quote_smart($sickness)." WHERE hash=".DB_quote_smart($hash));
342   return;
343 }
344
345 function DB_get_current_trickid($gameid)
346 {
347   $trickid  = NULL;
348   $sequence = NULL;
349   $number   = 0;
350
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) )
356     {
357       $trickid  = $r[0];
358       $sequence = $r[1];
359       $number++;
360     };
361
362   if(!$sequence || $sequence==4)
363     {
364       DB_query("INSERT INTO Trick VALUES (NULL,NULL,NULL, ".DB_quote_smart($gameid).",NULL)");
365       $trickid  = DB_insert_id();
366       $sequence = 1;
367       $number++;
368     }
369   else
370     {
371       $sequence++;
372     }
373
374   return array($trickid,$sequence,$number);
375 }
376
377 function DB_get_max_trickid($gameid)
378 {
379   $r = DB_query_array("SELECT MAX(id) FROM Trick WHERE game_id=".DB_quote_smart($gameid));
380
381   return ($r?$r[0]:NULL);
382 }
383
384 function DB_play_card($trickid,$handcardid,$sequence)
385 {
386   DB_query("INSERT INTO Play VALUES(NULL,NULL,NULL,".DB_quote_smart($trickid).
387            ",".DB_quote_smart($handcardid).",".DB_quote_smart($sequence).")");
388
389   $playid = DB_insert_id();
390   return $playid;
391 }
392
393 function DB_get_all_names_by_gameid($id)
394 {
395   $names = array();
396
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))
400     $names[] = $r[0];
401
402   return $names;
403 }
404
405 function DB_get_all_userid_by_gameid($id)
406 {
407   $names = array();
408
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))
412     $names[] = $r[0];
413
414   return $names;
415 }
416
417 function DB_get_hash_from_game_and_pos($id,$pos)
418 {
419   $r = DB_query_array("SELECT hash FROM Hand WHERE game_id=".DB_quote_smart($id)." and position=".DB_quote_smart($pos));
420
421   if($r)
422     return $r[0];
423   else
424     return "";
425 }
426
427 function DB_get_hash_from_gameid_and_userid($id,$user)
428 {
429   $r = DB_query_array("SELECT hash FROM Hand WHERE game_id=".DB_quote_smart($id)." and user_id=".DB_quote_smart($user));
430
431   if($r)
432     return $r[0];
433   else
434     return "";
435 }
436
437 function DB_get_all_names()
438 {
439   $names  = array();
440
441   $result = DB_query("SELECT fullname FROM User");
442
443   while($r = DB_fetch_array($result))
444     $names[] = $r[0];
445
446   return $names;
447 }
448
449 function DB_get_all_user_names_open_for_games()
450 {
451   $names  = array();
452
453   DB_query("DROP   TEMPORARY TABLE IF EXISTS Usertmp;");
454   DB_query("CREATE TEMPORARY TABLE Usertmp SELECT id,fullname FROM User;");
455   DB_query("DELETE FROM Usertmp WHERE id IN (SELECT user_id FROM User_Prefs WHERE pref_key='open for games' and value='no')");
456
457   $result = DB_query("SELECT fullname FROM Usertmp");
458   DB_query("DROP   TEMPORARY TABLE IF EXISTS Usertmp;");
459
460   while($r = DB_fetch_array($result))
461     $names[] = $r[0];
462
463   return $names;
464 }
465
466 function DB_get_names_of_last_logins($N)
467 {
468   $names  = array();
469
470   $result = DB_query("SELECT fullname FROM User ORDER BY last_login DESC LIMIT $N");
471   while($r = DB_fetch_array($result))
472     $names[] = $r[0];
473
474   return $names;
475 }
476
477 function DB_get_names_of_new_logins($N)
478 {
479   $names  = array();
480
481   $result = DB_query("SELECT fullname FROM User ORDER BY create_date DESC, id DESC LIMIT $N");
482   while($r = DB_fetch_array($result))
483     $names[] = $r[0];
484
485   return $names;
486 }
487
488 function DB_update_game_timestamp($gameid)
489 {
490   DB_query("UPDATE Game SET mod_date = CURRENT_TIMESTAMP WHERE id=".DB_quote_smart($gameid));
491   return;
492 }
493
494
495 function DB_update_user_timestamp($userid)
496 {
497   DB_query("UPDATE User SET last_login = CURRENT_TIMESTAMP WHERE id=".DB_quote_smart($userid));
498   return;
499 }
500
501 function DB_get_user_timestamp($userid)
502 {
503   $r = DB_query_array("SELECT last_login FROM User WHERE id=".DB_quote_smart($userid));
504
505   if($r)
506     return $r[0];
507   else
508     return NULL;
509 }
510 function DB_get_user_timezone($userid)
511 {
512   $r = DB_query_array("SELECT timezone FROM User WHERE id=".DB_quote_smart($userid));
513
514   if($r)
515     return $r[0];
516   else
517     return "Europe/London";
518 }
519
520 function DB_insert_comment($comment,$playid,$userid)
521 {
522   DB_query("INSERT INTO Comment VALUES (NULL,NULL,NULL,$userid,$playid, ".DB_quote_smart($comment).")");
523
524   return;
525 }
526
527 function DB_insert_note($comment,$gameid,$userid)
528 {
529   DB_query("INSERT INTO Notes VALUES (NULL,NULL,NULL,$userid,$gameid, ".DB_quote_smart($comment).")");
530
531   return;
532 }
533
534 function DB_get_notes_by_userid_and_gameid($userid,$gameid)
535 {
536   $notes = array();
537
538   $result = DB_query("SELECT comment FROM Notes WHERE user_id=".DB_quote_smart($userid) .
539                      " AND game_id=".DB_quote_smart($gameid));
540
541   while($r = DB_fetch_array($result))
542     $notes[] = $r[0];
543
544   return $notes;
545 }
546
547
548 function DB_get_gametype_by_gameid($id)
549 {
550   $r = DB_query_array("SELECT type FROM Game WHERE id=".DB_quote_smart($id));
551
552   if($r)
553     return $r[0]."";
554   else
555     return "";
556 }
557
558 function DB_set_gametype_by_gameid($id,$p)
559 {
560   DB_query("UPDATE Game SET type='".$p."' WHERE id=".DB_quote_smart($id));
561   return;
562 }
563
564 function DB_get_solo_by_gameid($id)
565 {
566   $r = DB_query_array("SELECT solo FROM Game WHERE id=".DB_quote_smart($id));
567
568   if($r)
569     return $r[0]."";
570   else
571     return "";
572 }
573
574
575 function DB_get_startplayer_by_gameid($id)
576 {
577   $r = DB_query_array("SELECT startplayer FROM Game WHERE id=".DB_quote_smart($id));
578
579   if($r)
580     return $r[0];
581   else
582     return 0;
583 }
584
585 function DB_set_startplayer_by_gameid($id,$p)
586 {
587   DB_query("UPDATE Game SET startplayer='".$p."' WHERE id=".DB_quote_smart($id));
588   return;
589 }
590
591 function DB_get_player_by_gameid($id)
592 {
593   $r = DB_query_array("SELECT player FROM Game WHERE id=".DB_quote_smart($id));
594
595   if($r)
596     return $r[0];
597   else
598     return 0;
599 }
600 function DB_set_player_by_gameid($id,$p)
601 {
602   DB_query("UPDATE Game SET player='".DB_quote_smart($p)."' WHERE id=".DB_quote_smart($id));
603   return;
604 }
605
606
607
608 function DB_get_ruleset_by_gameid($id)
609 {
610   $r = DB_query_array("SELECT ruleset FROM Game WHERE id=".DB_quote_smart($id));
611
612   if($r)
613     return $r[0];
614   else
615     return NULL;
616 }
617
618 function DB_get_session_by_gameid($id)
619 {
620   $r = DB_query_array("SELECT session FROM Game WHERE id=".DB_quote_smart($id));
621
622   if($r)
623     return $r[0];
624   else
625     return NULL;
626 }
627
628 function DB_get_max_session()
629 {
630   $r = DB_query_array("SELECT MAX(session) FROM Game");
631
632   if($r)
633     return $r[0];
634   else
635     return 0;
636 }
637
638 function DB_get_hashes_by_session($session,$user)
639 {
640   $r = array();
641
642   $result = DB_query("SELECT Hand.hash FROM Hand".
643                      " LEFT JOIN Game ON Game.id=Hand.game_id ".
644                      " WHERE Game.session=".DB_quote_smart($session).
645                      " AND Hand.user_id=".DB_quote_smart($user).
646                      " ORDER BY Game.create_date ASC");
647   while($t = DB_fetch_array($result))
648     $r[] = $t[0];
649
650   return $r;
651 }
652
653 function DB_get_ruleset($dullen,$schweinchen,$call)
654 {
655   $r = array();
656
657   $result = DB_query("SELECT id FROM Rulesets WHERE".
658                      " dullen=".DB_quote_smart($dullen)." AND ".
659                      " Rulesets.call=".DB_quote_smart($call)." AND ".
660                      " schweinchen=".DB_quote_smart($schweinchen));
661   if($result)
662     $r    = DB_fetch_array($result);
663
664   if($r)
665     return $r[0]; /* found ruleset */
666   else
667     {
668       /* create new one */
669       $result = DB_query("INSERT INTO Rulesets VALUES (NULL, NULL, ".
670                          DB_quote_smart($dullen).",".
671                          DB_quote_smart($schweinchen).",".
672                          DB_quote_smart($call).
673                          ", NULL)");
674       if($result)
675         return DB_insert_id();
676     };
677
678   return -1; /* something went wrong */
679 }
680
681 function DB_get_party_by_hash($hash)
682 {
683   $r = DB_query_array("SELECT party FROM Hand WHERE hash=".DB_quote_smart($hash));
684
685   if($r)
686     return $r[0];
687   else
688     return NULL;
689 }
690
691 function DB_get_party_by_gameid_and_userid($gameid,$userid)
692 {
693   $r = DB_query_array("SELECT party FROM Hand".
694                       " WHERE game_id=".DB_quote_smart($gameid).
695                       "  AND user_id=".DB_quote_smart($userid));
696   if($r)
697     return $r[0];
698   else
699     return NULL;
700 }
701
702 function DB_set_party_by_hash($hash,$party)
703 {
704   DB_query("UPDATE Hand SET party=".DB_quote_smart($party)." WHERE hash=".DB_quote_smart($hash));
705   return;
706 }
707
708 function DB_get_PREF($myid)
709 {
710   /* Cardset */
711   $r = DB_query_array("SELECT value from User_Prefs".
712                       " WHERE user_id='$myid' AND pref_key='cardset'" );
713   if($r)
714     {
715       /* licence only valid until then */
716       if($r[0]=="altenburg" && (time()-strtotime( "2009-12-31 23:59:59")<0) )
717         $PREF["cardset"]="altenburg";
718       else
719         $PREF["cardset"]="english";
720     }
721   else
722     $PREF["cardset"]="english";
723
724   /* Email */
725   $r = DB_query_array("SELECT value FROM User_Prefs".
726                       " WHERE user_id='$myid' AND pref_key='email'" );
727   if($r)
728     {
729       if($r[0]=="emailaddict")
730         $PREF["email"]="emailaddict";
731       else
732         $PREF["email"]="emailnonaddict";
733     }
734   else
735     $PREF["email"]="emailnonaddict";
736
737   /* Autosetup */
738   $r = DB_query_array("SELECT value FROM User_Prefs".
739                       " WHERE user_id='$myid' AND pref_key='autosetup'" );
740   if($r)
741     {
742       if($r[0]=='yes')
743         $PREF['autosetup']='yes';
744       else
745         $PREF['autosetup']='no';
746     }
747   else
748     $PREF['autosetup']='no';
749
750   /* Sorting */
751   $r = DB_query_array("SELECT value FROM User_Prefs".
752                       " WHERE user_id='$myid' AND pref_key='sorting'" );
753   if($r)
754     $PREF['sorting'] = $r[0];
755   else
756     $PREF['sorting']='high-low';
757
758   /* Open for new games */
759   $r = DB_query_array("SELECT value FROM User_Prefs".
760                       " WHERE user_id='$myid' AND pref_key='open for games'" );
761   if($r)
762     $PREF['open_for_games'] = $r[0];
763   else
764     $PREF['open_for_games']='yes';
765
766
767   return $PREF;
768 }
769
770 function DB_get_RULES($gameid)
771 {
772   $r = DB_query_array("SELECT * FROM Rulesets".
773                       " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
774                       " WHERE Game.id='$gameid'" );
775
776   $RULES["dullen"]      = $r[2];
777   $RULES["schweinchen"] = $r[3];
778   $RULES["call"]        = $r[4];
779
780   return $RULES;
781 }
782
783 function DB_get_email_pref_by_hash($hash)
784 {
785   $r = DB_query_array("SELECT value FROM Hand".
786                       " LEFT JOIN User_Prefs ON Hand.user_id=User_Prefs.user_id".
787                       " WHERE hash='$hash' AND pref_key='email'" );
788   if($r)
789     {
790       if($r[0]=="emailaddict")
791         return "emailaddict";
792       else
793         return "emailnonaddict";
794     }
795   else
796     return "emailnonaddict";
797 }
798
799 function DB_get_email_pref_by_uid($uid)
800 {
801   $r = DB_query_array("SELECT value FROM User_Prefs ".
802                       " WHERE user_id='$uid' AND pref_key='email'" );
803   if($r)
804     {
805       if($r[0]=="emailaddict")
806         return "emailaddict";
807       else
808         return "emailnonaddict";
809     }
810   else
811     return "emailnonaddict";
812 }
813
814 function DB_get_unused_randomnumbers($userstr)
815 {
816   /* optimized version of this query using temporary tables (perhaps we should use a procedure here?).
817      First we create a copy of the Game table using just the gameid and the cards.
818      Then in a second round we delete all the gameids of games where our players are in.
819      At the end we return only the first entry in the temporary table.
820   */
821   DB_query("DROP   TEMPORARY TABLE IF EXISTS gametmp;");
822   DB_query("CREATE TEMPORARY TABLE gametmp SELECT id,randomnumbers FROM Game;");
823   DB_query("DELETE FROM gametmp WHERE id IN (SELECT game_id FROM Hand WHERE user_id IN (".$userstr."));");
824
825   $r = DB_query_array("SELECT randomnumbers FROM gametmp LIMIT 1;");
826   DB_query("DROP   TEMPORARY TABLE IF EXISTS gametmp;");
827
828   if($r)
829     return $r[0];
830   else
831     return "";
832 }
833
834 function DB_get_number_of_passwords_recovery($user)
835 {
836   $r = DB_query_array("SELECT COUNT(*) FROM Recovery ".
837                       "  WHERE user_id=$user ".
838                       "  AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= create_date".
839                       "  GROUP BY user_id " );
840   if($r)
841     return $r[0];
842   else
843     return 0;
844 }
845
846 function DB_set_recovery_password($user,$newpw)
847 {
848   DB_query("INSERT INTO Recovery VALUES(NULL,".DB_quote_smart($user).
849            ",".DB_quote_smart($newpw).",NULL)");
850   return;
851 }
852
853 function DB_get_card_name($card)
854 {
855   $r = DB_query_array("SELECT strength,suite FROM Card WHERE id='$card'");
856
857   if($r)
858     return $r[0]." of ".$r[1];
859   else
860     return "Error during get_card_name ".$card;
861 }
862
863 function DB_get_current_playid($gameid)
864 {
865   $trick = DB_get_max_trickid($gameid);
866
867   if(!$trick) return NULL;
868
869   $r = DB_query_array("SELECT id FROM Play WHERE trick_id='$trick' ORDER BY create_date DESC LIMIT 1");
870
871   if($r)
872     return $r[0];
873
874   return "";
875 }
876
877 function DB_get_call_by_hash($hash)
878 {
879   $r = DB_query_array("SELECT point_call FROM Hand WHERE hash='$hash'");
880
881   if($r)
882     return $r[0];
883
884   return NULL;
885 }
886
887 function DB_get_partner_call_by_hash($hash)
888 {
889   $partner = DB_get_partner_hash_by_hash($hash);
890
891   if($partner)
892     {
893       $r = DB_query_array("SELECT point_call FROM Hand WHERE hash='$partner'");
894
895       if($r)
896         return $r[0];
897     }
898
899   return NULL;
900 }
901
902 function DB_get_partner_hash_by_hash($hash)
903 {
904   $gameid = DB_get_gameid_by_hash($hash);
905   $party  = DB_get_party_by_hash($hash);
906
907   $r = DB_query_array("SELECT hash FROM Hand WHERE game_id='$gameid' AND party='$party' AND hash<>'$hash'");
908
909   if($r)
910     return $r[0];
911
912   return NULL;
913 }
914
915 function DB_format_gameid($gameid)
916 {
917   $session = DB_get_session_by_gameid($gameid);
918
919   /* get number of game */
920   $r = DB_query_array("SELECT SUM(TIME_TO_SEC(TIMEDIFF(create_date, (SELECT create_date FROM Game WHERE id='$gameid')))<=0) ".
921                       " FROM Game".
922                       " WHERE session='$session' ");
923   return $session.".".$r[0];
924 }
925
926 function DB_get_reminder($user,$gameid)
927 {
928   $r = DB_query_array("SELECT COUNT(*) FROM Reminder ".
929                       "  WHERE user_id=$user ".
930                       "  AND game_id=$gameid ".
931                       "  AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= create_date".
932                       "  GROUP BY user_id " );
933   if($r)
934     return $r[0];
935   else
936     return 0;
937 }
938
939 function DB_set_reminder($user,$gameid)
940 {
941   DB_query("INSERT INTO Reminder ".
942            "  VALUES(NULL, ".DB_quote_smart($user).", ".DB_quote_smart($gameid).
943            ", NULL) ");
944   return 0;
945 }
946
947 function DB_is_session_active($session)
948 {
949   $r = DB_query_array("SELECT COUNT(*) FROM Game ".
950                       "  WHERE session=$session ".
951                       "  AND status<>'gameover' ");
952   if($r)
953     return $r[0];
954   else
955     return -1;
956 }
957
958 function DB_get_score_by_gameid($gameid)
959 {
960   /* returns the points of a game from the point of the re parth (<0 if they lost) */
961   $queryresult = DB_query("SELECT COUNT(*),party FROM Score ".
962                           "  WHERE game_id=$gameid ".
963                           "  GROUP BY party ");
964   $re     = 0;
965   $contra = 0;
966
967   while($r = DB_fetch_array($queryresult) )
968     {
969       if($r[1] == "re")
970         $re += $r[0];
971       else if ($r[1] == "contra")
972         $contra += $r[0];
973     };
974
975   return ($re - $contra);
976 }
977
978 function DB_get_gameids_of_finished_games_by_session($session)
979 {
980   $ids = array ();
981
982   if($session==0) /* return all games */
983     $queryresult = DB_query("SELECT id FROM Game ".
984                             " WHERE status='gameover' ".
985                             " ORDER BY create_date ASC");
986   else   /* return games in a session */
987     $queryresult = DB_query("SELECT id FROM Game ".
988                             "  WHERE session=$session ".
989                             "   AND status='gameover' ".
990                             " ORDER BY create_date ASC");
991
992   $i=0;
993   while($r = DB_fetch_array($queryresult) )
994     {
995       $ids[$i] = $r[0];
996       $i++;
997     }
998
999   return $ids;
1000 }
1001
1002 function DB_get_card_value_by_cardid($id)
1003 {
1004   $r = DB_query_array("SELECT points FROM Card ".
1005                       "  WHERE id=$id ");
1006
1007   if($r)
1008     return $r[0];
1009   else
1010     return NULL;
1011 }
1012
1013 function DB_get_userid($type,$var1="",$var2="")
1014 {
1015   /* get the userid of a user
1016    * this can be done several ways, which are all handled below
1017    * if a email/password combination is given and it doesn't work, we also
1018    * need to check the recovery table for additional passwords
1019    */
1020
1021   $r = NULL;
1022
1023   switch($type)
1024     {
1025     case 'name':
1026       $result = DB_query("SELECT id FROM User WHERE fullname=".DB_quote_smart($var1));
1027       break;
1028     case 'hash':
1029       $result = DB_query("SELECT user_id FROM Hand WHERE hash=".DB_quote_smart($var1));
1030       break;
1031     case 'password':
1032       $result = DB_query("SELECT id FROM User WHERE password=".DB_quote_smart($var1));
1033       break;
1034     case 'email':
1035       $result = DB_query("SELECT id FROM User WHERE email=".DB_quote_smart($var1));
1036       break;
1037     case 'email-password':
1038       $result = DB_query("SELECT id FROM User WHERE email=".DB_quote_smart($var1)." AND password=".DB_quote_smart($var2));
1039       $r = DB_fetch_array($result);
1040       /* test if a recovery password has been set */
1041       if(!$r)
1042         {
1043           echo "testing alternative password";
1044           $result = DB_query("SELECT User.id FROM User".
1045                              " LEFT JOIN Recovery ON User.id=Recovery.user_id".
1046                              " WHERE email=".DB_quote_smart($var1).
1047                              " AND Recovery.password=".DB_quote_smart($var2).
1048                              " AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= Recovery.create_date");
1049         }
1050       break;
1051     case 'gameid-position':
1052       $result = DB_query("SELECT user_id FROM Hand WHERE game_id=".
1053                          DB_quote_smart($var1)." AND position=".
1054                          DB_quote_smart($var2));
1055       break;
1056     }
1057
1058   if(!$r)
1059     $r = DB_fetch_array($result);
1060
1061   if($r)
1062     return $r[0];
1063   else
1064     return 0;
1065 }
1066
1067 function DB_get_email($type,$var1='',$var2='')
1068 {
1069   /* return the email of a user
1070    * this is used for sending out emails, but also for
1071    * testing the login for example
1072    */
1073   switch($type)
1074     {
1075     case 'name':
1076       $result = DB_query("SELECT email FROM User WHERE fullname=".DB_quote_smart($var1)."");
1077       break;
1078     case 'userid':
1079       $result = DB_query("SELECT email FROM User WHERE id=".DB_quote_smart($var1)."");
1080       break;
1081     case 'hash':
1082       $result = DB_query("SELECT User.email FROM User ".
1083                          "LEFT JOIN Hand ON Hand.user_id=User.id ".
1084                          "WHERE Hand.hash=".DB_quote_smart($var1)."");
1085       break;
1086     case 'position-gameid':
1087       $result = DB_query("SELECT email FROM User ".
1088                          "LEFT JOIN Hand ON User.id=Hand.user_id ".
1089                          "LEFT JOIN Game ON Game.id=Hand.game_id ".
1090                          "WHERE Game.id=".DB_quote_smart($var2)." ".
1091                          "AND Hand.position=".DB_quote_smart($var1)."");
1092       break;
1093     }
1094
1095   $r = DB_fetch_array($result);
1096
1097   if($r)
1098     return $r[0];
1099   else
1100     return "";
1101 }
1102
1103 function DB_get_name($type,$var1='')
1104 {
1105   /* get the full name of a user
1106    * a user can be uniquely identified several ways
1107    */
1108   switch($type)
1109     {
1110     case 'hash':
1111       $r = DB_query_array("SELECT fullname FROM Hand LEFT JOIN User ON Hand.user_id=User.id WHERE hash=".DB_quote_smart($var1));
1112       break;
1113     case 'email':
1114       $r = DB_query_array("SELECT fullname FROM User WHERE email=".DB_quote_smart($var1));
1115       break;
1116     case 'userid':
1117       $r = DB_query_array("SELECT fullname FROM User  WHERE id=".DB_quote_smart($var1));
1118     }
1119
1120   if($r)
1121     return $r[0];
1122   else
1123     return "";
1124 }
1125
1126 function DB_add_exchanged_card($card,$old_hand_id,$new_hand_id)
1127 {
1128   DB_query("INSERT INTO Card_Exchange VALUES (NULL,$new_hand_id,$old_hand_id,$card)");
1129   return;
1130 }
1131
1132 function DB_get_exchanged_cards($hash)
1133 {
1134   $cards = array();
1135
1136   $handid = DB_get_handid('hash',$hash);
1137
1138   $result = DB_query("SELECT card_id FROM Card_Exchange WHERE orig_hand_id=".DB_quote_smart($handid));
1139   while($r = DB_fetch_array($result))
1140     $cards[]=$r[0];
1141
1142   return $cards;
1143 }
1144
1145 function DB_played_by_others($gameid)
1146 {
1147   $gameids = array();
1148   $result = DB_query("SELECT id FROM Game WHERE randomnumbers=(SELECT randomnumbers FROM Game WHERE id=$gameid) AND status='gameover'");
1149   while($r = DB_fetch_array($result))
1150     if($r[0]!=$gameid)
1151       $gameids[]=$r[0];
1152   return $gameids;
1153 }
1154 ?>