b8bcea80141a8595a5fc7e4ea578f8e6de4e02c7
[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   /* Vacation start */
767   $r = DB_query_array("SELECT value FROM User_Prefs".
768                       " WHERE user_id='$myid' AND pref_key='vacation start'" );
769   if($r)
770     $PREF['vacation_start'] = $r[0];
771   else
772     $PREF['vacation_start'] = NULL;
773
774   /* Vacation stop */
775   $r = DB_query_array("SELECT value FROM User_Prefs".
776                       " WHERE user_id='$myid' AND pref_key='vacation stop'" );
777   if($r)
778     $PREF['vacation_stop'] = $r[0];
779   else
780     $PREF['vacation_stop'] = NULL;
781
782   /* Vacation comment */
783   $r = DB_query_array("SELECT value FROM User_Prefs".
784                       " WHERE user_id='$myid' AND pref_key='vacation comment'" );
785   if($r)
786     $PREF['vacation_comment'] = $r[0];
787   else
788     $PREF['vacation_comment'] = "";
789
790   return $PREF;
791 }
792
793 function DB_get_RULES($gameid)
794 {
795   $r = DB_query_array("SELECT * FROM Rulesets".
796                       " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
797                       " WHERE Game.id='$gameid'" );
798
799   $RULES["dullen"]      = $r[2];
800   $RULES["schweinchen"] = $r[3];
801   $RULES["call"]        = $r[4];
802
803   return $RULES;
804 }
805
806 function DB_get_email_pref_by_hash($hash)
807 {
808   $r = DB_query_array("SELECT value FROM Hand".
809                       " LEFT JOIN User_Prefs ON Hand.user_id=User_Prefs.user_id".
810                       " WHERE hash='$hash' AND pref_key='email'" );
811   if($r)
812     {
813       if($r[0]=="emailaddict")
814         return "emailaddict";
815       else
816         return "emailnonaddict";
817     }
818   else
819     return "emailnonaddict";
820 }
821
822 function DB_get_email_pref_by_uid($uid)
823 {
824   $r = DB_query_array("SELECT value FROM User_Prefs ".
825                       " WHERE user_id='$uid' AND pref_key='email'" );
826   if($r)
827     {
828       if($r[0]=="emailaddict")
829         return "emailaddict";
830       else
831         return "emailnonaddict";
832     }
833   else
834     return "emailnonaddict";
835 }
836
837 function DB_get_unused_randomnumbers($userstr)
838 {
839   /* optimized version of this query using temporary tables (perhaps we should use a procedure here?).
840      First we create a copy of the Game table using just the gameid and the cards.
841      Then in a second round we delete all the gameids of games where our players are in.
842      At the end we return only the first entry in the temporary table.
843   */
844   DB_query("DROP   TEMPORARY TABLE IF EXISTS gametmp;");
845   DB_query("CREATE TEMPORARY TABLE gametmp SELECT id,randomnumbers FROM Game;");
846   DB_query("DELETE FROM gametmp WHERE randomnumbers IN (SELECT randomnumbers FROM Hand LEFT JOIN Game ON Game.id=game_id WHERE user_id IN (".$userstr."));");
847
848   $r = DB_query_array("SELECT randomnumbers FROM gametmp LIMIT 1;");
849   DB_query("DROP   TEMPORARY TABLE IF EXISTS gametmp;");
850
851   if($r)
852     return $r[0];
853   else
854     return "";
855 }
856
857 function DB_get_number_of_passwords_recovery($user)
858 {
859   $r = DB_query_array("SELECT COUNT(*) FROM Recovery ".
860                       "  WHERE user_id=$user ".
861                       "  AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= create_date".
862                       "  GROUP BY user_id " );
863   if($r)
864     return $r[0];
865   else
866     return 0;
867 }
868
869 function DB_set_recovery_password($user,$newpw)
870 {
871   DB_query("INSERT INTO Recovery VALUES(NULL,".DB_quote_smart($user).
872            ",".DB_quote_smart($newpw).",NULL)");
873   return;
874 }
875
876 function DB_get_card_name($card)
877 {
878   if($card==0)
879     return 'backside';
880
881   $r = DB_query_array("SELECT strength,suite FROM Card WHERE id='$card'");
882
883   if($r)
884     return $r[0]." of ".$r[1];
885   else
886     return "Error during get_card_name ".$card;
887 }
888
889 function DB_get_current_playid($gameid)
890 {
891   $trick = DB_get_max_trickid($gameid);
892
893   if(!$trick) return NULL;
894
895   $r = DB_query_array("SELECT id FROM Play WHERE trick_id='$trick' ORDER BY create_date DESC LIMIT 1");
896
897   if($r)
898     return $r[0];
899
900   return "";
901 }
902
903 function DB_get_call_by_hash($hash)
904 {
905   $r = DB_query_array("SELECT point_call FROM Hand WHERE hash='$hash'");
906
907   if($r)
908     return $r[0];
909
910   return NULL;
911 }
912
913 function DB_get_partner_call_by_hash($hash)
914 {
915   $partner = DB_get_partner_hash_by_hash($hash);
916
917   if($partner)
918     {
919       $r = DB_query_array("SELECT point_call FROM Hand WHERE hash='$partner'");
920
921       if($r)
922         return $r[0];
923     }
924
925   return NULL;
926 }
927
928 function DB_get_partner_hash_by_hash($hash)
929 {
930   $gameid = DB_get_gameid_by_hash($hash);
931   $party  = DB_get_party_by_hash($hash);
932
933   $r = DB_query_array("SELECT hash FROM Hand WHERE game_id='$gameid' AND party='$party' AND hash<>'$hash'");
934
935   if($r)
936     return $r[0];
937
938   return NULL;
939 }
940
941 function DB_format_gameid($gameid)
942 {
943   $session = DB_get_session_by_gameid($gameid);
944
945   /* get number of game */
946   $r = DB_query_array("SELECT SUM(TIME_TO_SEC(TIMEDIFF(create_date, (SELECT create_date FROM Game WHERE id='$gameid')))<=0) ".
947                       " FROM Game".
948                       " WHERE session='$session' ");
949   return $session.".".$r[0];
950 }
951
952 function DB_get_reminder($user,$gameid)
953 {
954   $r = DB_query_array("SELECT COUNT(*) FROM Reminder ".
955                       "  WHERE user_id=$user ".
956                       "  AND game_id=$gameid ".
957                       "  AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= create_date".
958                       "  GROUP BY user_id " );
959   if($r)
960     return $r[0];
961   else
962     return 0;
963 }
964
965 function DB_set_reminder($user,$gameid)
966 {
967   DB_query("INSERT INTO Reminder ".
968            "  VALUES(NULL, ".DB_quote_smart($user).", ".DB_quote_smart($gameid).
969            ", NULL) ");
970   return 0;
971 }
972
973 function DB_is_session_active($session)
974 {
975   $r = DB_query_array("SELECT COUNT(*) FROM Game ".
976                       "  WHERE session=$session ".
977                       "  AND status<>'gameover' ");
978   if($r)
979     return $r[0];
980   else
981     return -1;
982 }
983
984 function DB_get_score_by_gameid($gameid)
985 {
986   /* returns the points of a game from the point of the re parth (<0 if they lost) */
987   $queryresult = DB_query("SELECT COUNT(*),party FROM Score ".
988                           "  WHERE game_id=$gameid ".
989                           "  GROUP BY party ");
990   $re     = 0;
991   $contra = 0;
992
993   while($r = DB_fetch_array($queryresult) )
994     {
995       if($r[1] == "re")
996         $re += $r[0];
997       else if ($r[1] == "contra")
998         $contra += $r[0];
999     };
1000
1001   return ($re - $contra);
1002 }
1003
1004 function DB_get_gameids_of_finished_games_by_session($session)
1005 {
1006   $ids = array ();
1007
1008   if($session==0) /* return all games */
1009     $queryresult = DB_query("SELECT id FROM Game ".
1010                             " WHERE status='gameover' ".
1011                             " ORDER BY create_date ASC");
1012   else   /* return games in a session */
1013     $queryresult = DB_query("SELECT id FROM Game ".
1014                             "  WHERE session=$session ".
1015                             "   AND status='gameover' ".
1016                             " ORDER BY create_date ASC");
1017
1018   $i=0;
1019   while($r = DB_fetch_array($queryresult) )
1020     {
1021       $ids[$i] = $r[0];
1022       $i++;
1023     }
1024
1025   return $ids;
1026 }
1027
1028 function DB_get_card_value_by_cardid($id)
1029 {
1030   $r = DB_query_array("SELECT points FROM Card ".
1031                       "  WHERE id=$id ");
1032
1033   if($r)
1034     return $r[0];
1035   else
1036     return NULL;
1037 }
1038
1039 function DB_get_userid($type,$var1="",$var2="")
1040 {
1041   /* get the userid of a user
1042    * this can be done several ways, which are all handled below
1043    * if a email/password combination is given and it doesn't work, we also
1044    * need to check the recovery table for additional passwords
1045    */
1046
1047   $r = NULL;
1048
1049   switch($type)
1050     {
1051     case 'name':
1052       $result = DB_query("SELECT id FROM User WHERE fullname=".DB_quote_smart($var1));
1053       break;
1054     case 'hash':
1055       $result = DB_query("SELECT user_id FROM Hand WHERE hash=".DB_quote_smart($var1));
1056       break;
1057     case 'password':
1058       $result = DB_query("SELECT id FROM User WHERE password=".DB_quote_smart($var1));
1059       break;
1060     case 'email':
1061       $result = DB_query("SELECT id FROM User WHERE email=".DB_quote_smart($var1));
1062       break;
1063     case 'email-password':
1064       $result = DB_query("SELECT id FROM User WHERE email=".DB_quote_smart($var1)." AND password=".DB_quote_smart($var2));
1065       $r = DB_fetch_array($result);
1066       /* test if a recovery password has been set */
1067       if(!$r)
1068         {
1069           echo "testing alternative password";
1070           $result = DB_query("SELECT User.id FROM User".
1071                              " LEFT JOIN Recovery ON User.id=Recovery.user_id".
1072                              " WHERE email=".DB_quote_smart($var1).
1073                              " AND Recovery.password=".DB_quote_smart($var2).
1074                              " AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= Recovery.create_date");
1075         }
1076       break;
1077     case 'gameid-position':
1078       $result = DB_query("SELECT user_id FROM Hand WHERE game_id=".
1079                          DB_quote_smart($var1)." AND position=".
1080                          DB_quote_smart($var2));
1081       break;
1082     }
1083
1084   if(!$r)
1085     $r = DB_fetch_array($result);
1086
1087   if($r)
1088     return $r[0];
1089   else
1090     return 0;
1091 }
1092
1093 function DB_get_email($type,$var1='',$var2='')
1094 {
1095   /* return the email of a user
1096    * this is used for sending out emails, but also for
1097    * testing the login for example
1098    */
1099   switch($type)
1100     {
1101     case 'name':
1102       $result = DB_query("SELECT email FROM User WHERE fullname=".DB_quote_smart($var1)."");
1103       break;
1104     case 'userid':
1105       $result = DB_query("SELECT email FROM User WHERE id=".DB_quote_smart($var1)."");
1106       break;
1107     case 'hash':
1108       $result = DB_query("SELECT User.email FROM User ".
1109                          "LEFT JOIN Hand ON Hand.user_id=User.id ".
1110                          "WHERE Hand.hash=".DB_quote_smart($var1)."");
1111       break;
1112     case 'position-gameid':
1113       $result = DB_query("SELECT email FROM User ".
1114                          "LEFT JOIN Hand ON User.id=Hand.user_id ".
1115                          "LEFT JOIN Game ON Game.id=Hand.game_id ".
1116                          "WHERE Game.id=".DB_quote_smart($var2)." ".
1117                          "AND Hand.position=".DB_quote_smart($var1)."");
1118       break;
1119     }
1120
1121   $r = DB_fetch_array($result);
1122
1123   if($r)
1124     return $r[0];
1125   else
1126     return "";
1127 }
1128
1129 function DB_get_name($type,$var1='')
1130 {
1131   /* get the full name of a user
1132    * a user can be uniquely identified several ways
1133    */
1134   switch($type)
1135     {
1136     case 'hash':
1137       $r = DB_query_array("SELECT fullname FROM Hand LEFT JOIN User ON Hand.user_id=User.id WHERE hash=".DB_quote_smart($var1));
1138       break;
1139     case 'email':
1140       $r = DB_query_array("SELECT fullname FROM User WHERE email=".DB_quote_smart($var1));
1141       break;
1142     case 'userid':
1143       $r = DB_query_array("SELECT fullname FROM User  WHERE id=".DB_quote_smart($var1));
1144     }
1145
1146   if($r)
1147     return $r[0];
1148   else
1149     return "";
1150 }
1151
1152 function DB_add_exchanged_card($card,$old_hand_id,$new_hand_id)
1153 {
1154   DB_query("INSERT INTO Card_Exchange VALUES (NULL,$new_hand_id,$old_hand_id,$card)");
1155   return;
1156 }
1157
1158 function DB_get_exchanged_cards($hash)
1159 {
1160   $cards = array();
1161
1162   $handid = DB_get_handid('hash',$hash);
1163
1164   $result = DB_query("SELECT card_id FROM Card_Exchange WHERE orig_hand_id=".DB_quote_smart($handid));
1165   while($r = DB_fetch_array($result))
1166     $cards[]=$r[0];
1167
1168   return $cards;
1169 }
1170
1171 function DB_played_by_others($gameid)
1172 {
1173   $gameids = array();
1174   $result = DB_query("SELECT id FROM Game WHERE randomnumbers=(SELECT randomnumbers FROM Game WHERE id=$gameid) AND status='gameover'");
1175   while($r = DB_fetch_array($result))
1176     if($r[0]!=$gameid)
1177       $gameids[]=$r[0];
1178   return $gameids;
1179 }
1180 ?>