summaryrefslogtreecommitdiffstats
path: root/include/db.php
blob: 208334f7772b93049dc1c7328e268758ca09eeec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
<?php
/* make sure that we are not called from outside the scripts, 
 * use a variable defined in config.php to check this
 */
if(!isset($HOST))
  exit;

/*
 * open database
 */

function DB_open()
{
  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');
    }
  else
    return -1;

  return 0;
}

function DB_close()
{
  global $DB;
  mysql_close($DB);
  return;
}

function DB_quote_smart($value)
{
    /* Stripslashes */
    if (get_magic_quotes_gpc()) {
        $value = stripslashes($value);
    }
    /* Quote if not a number or a numeric string */
    if (!is_numeric($value)) {
        $value = "'" . mysql_real_escape_string($value) . "'";
    }
    return $value;
}

function DB_test()
{
  $result = DB_query("SELECT * FROM User");
  while($r = DB_fetch_array($result))
    {
      foreach($r as $thing)
	echo "  $thing ";
      echo "<br />\n";
    }
  return;
}

/* use Mysql in the background */
function DB_query($query)
{
  return mysql_query($query);
}

function DB_fetch_array($result)
{
  return mysql_fetch_array($result,MYSQL_NUM);
}

function DB_insert_id()
{
  return mysql_insert_id();
}

function DB_num_rows($result)
{
  return mysql_num_rows($result);
}
/* end Mysql functions */

function DB_query_array($query)
{
  $result = DB_query($query);
  $return = DB_fetch_array($result);
  
  return $return;
}

function DB_get_passwd_by_name($name)
{
  $r = DB_query_array("SELECT password FROM User WHERE fullname=".DB_quote_smart($name)."");

  if($r)
    return $r[0];
  else
    return "";
}

function DB_get_passwd_by_userid($id)
{
  $r = DB_query_array("SELECT password FROM User WHERE id=".DB_quote_smart($id)."");

  if($r)
    return $r[0];
  else
    return "";
}

function DB_check_recovery_passwords($password,$email)
{
  $r = DB_query_array("SELECT User.id FROM User".
		      " LEFT JOIN Recovery ON User.id=Recovery.user_id".
		      " WHERE email=".DB_quote_smart($email).
		      " AND Recovery.password=".DB_quote_smart($password).
		      " AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= Recovery.create_date");
  if($r)
    return 1;
  else
    return 0;
}

function DB_get_handid($type,$var1='',$var2='')
{
  switch($type)
    {
    case 'hash':
      $r = DB_query_array("SELECT id FROM Hand WHERE hash=".DB_quote_smart($var1));
      break;
    case 'gameid-position':
      $r = DB_query_array("SELECT id FROM Hand WHERE game_id=".
			  DB_quote_smart($var1)." AND position=".
			  DB_quote_smart($var2));
      break;
    case 'gameid-userid':
      $r = DB_query_array("SELECT id FROM Hand WHERE game_id=".
			  DB_quote_smart($var1)." AND user_id=".
			  DB_quote_smart($var2));
      break;
    }

  if($r)
    return $r[0];
  else
    return 0;
}

function DB_get_pos_by_hash($hash)
{
  $r= DB_query_array("SELECT position FROM Hand WHERE hash=".DB_quote_smart($hash));

  if($r)
    return $r[0];
  else
    return 0;
}

function DB_get_status_by_hash($hash)
{
  $r= DB_query_array("SELECT status FROM Hand WHERE hash=".DB_quote_smart($hash));

  if($r)
    return $r[0];
  else
    return 0;
}

function DB_set_game_status_by_gameid($id,$status)
{
  DB_query("UPDATE Game SET status='".$status."' WHERE id=".DB_quote_smart($id));
  return;
}

function DB_set_sickness_by_gameid($id,$status)
{
  DB_query("UPDATE Game SET sickness='".$status."' WHERE id=".DB_quote_smart($id));
  return;
}
function DB_get_sickness_by_gameid($id)
{
  $r = DB_query_array("SELECT sickness FROM Game WHERE id=".DB_quote_smart($id));

  if($r)
    return $r[0];
  else
    return NULL;
}

function DB_get_game_status_by_gameid($id)
{
  $r = DB_query_array("SELECT status FROM Game WHERE id=".DB_quote_smart($id));

  if($r)
    return $r[0];
  else
    return NULL;
}

function DB_set_hand_status_by_hash($hash,$status)
{
  DB_query("UPDATE Hand SET status='".$status."' WHERE hash=".DB_quote_smart($hash));
  return;
}

function DB_get_hand_status_by_userid_and_gameid($uid,$gid)
{
  $r = DB_query_array("SELECT status FROM Hand WHERE user_id=".DB_quote_smart($uid).
		      " AND game_id=".DB_quote_smart($gid));
  if($r)
    return $r[0];
  else
    return 0;
}

function DB_get_sickness_by_userid_and_gameid($uid,$gid)
{
  $r = DB_query_array("SELECT sickness FROM Hand WHERE user_id=".DB_quote_smart($uid).
		      " AND game_id=".DB_quote_smart($gid));
  if($r)
    return $r[0];
  else
    return 0;
}

function DB_get_sickness_by_pos_and_gameid($pos,$gid)
{
  $r = DB_query_array("SELECT sickness FROM Hand WHERE position=".DB_quote_smart($pos).
		      " AND game_id=".DB_quote_smart($gid));
  if($r)
    return $r[0];
  else
    return 0;
}

function DB_get_gameid_by_hash($hash)
{
  $r = DB_query_array("SELECT game_id FROM Hand WHERE hash=".DB_quote_smart($hash));

  if($r)
    return $r[0];
  else
    return 0;
}

function DB_cancel_game($hash)
{
  $gameid = DB_get_gameid_by_hash($hash);

  if(!$gameid)
    return;

  /* get the IDs of all players */
  $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
  while($r = DB_fetch_array($result))
    {
      $id = $r[0];

      $tmp = DB_query_array("SELECT id  FROM Hand_Card WHERE hand_id=".DB_quote_smart($id));
      DB_query("DELETE FROM Play WHERE hand_card_id=".DB_quote_smart($tmp[0]));

      DB_query("DELETE FROM Hand_Card WHERE hand_id=".DB_quote_smart($id));
      DB_query("DELETE FROM Hand WHERE id=".DB_quote_smart($id));
    }

  /* delete game */
  DB_query("DELETE FROM User_Game_Prefs WHERE game_id=".DB_quote_smart($gameid));
  DB_query("DELETE FROM Trick WHERE game_id=".DB_quote_smart($gameid));
  DB_query("DELETE FROM Game WHERE id=".DB_quote_smart($gameid));

  return;
}

function DB_get_hand($me)
{
  $cards = array();

  $handid = DB_get_handid('hash',$me);

  $result = DB_query("SELECT card_id FROM Hand_Card WHERE hand_id=".DB_quote_smart($handid)." and played='false' ");
  while($r = DB_fetch_array($result))
    $cards[]=$r[0];

  return $cards;
}

function DB_get_all_hand($me)
{
  $cards = array();

  $handid = DB_get_handid('hash',$me);

  $result = DB_query("SELECT card_id FROM Hand_Card WHERE hand_id=".DB_quote_smart($handid));
  while($r = DB_fetch_array($result))
    $cards[]=$r[0];

  return $cards;
}

function DB_get_cards_by_trick($id)
{
  $cards = array();
  $i     = 1;

  $result = DB_query("SELECT card_id,position FROM Play LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id ".
		     "LEFT JOIN Hand ON Hand.id=Hand_Card.hand_id ".
		     "WHERE trick_id=".
		     DB_quote_smart($id)." ORDER BY sequence ASC");
  while($r = DB_fetch_array($result))
    {
      $cards[$i]=array("card"=>$r[0],"pos"=>$r[1]);
      $i++;
    }

  return $cards;
}


function DB_set_solo_by_hash($hash,$solo)
{
  DB_query("UPDATE Hand SET solo=".DB_quote_smart($solo)." WHERE hash=".DB_quote_smart($hash));
  return;
}

function DB_set_solo_by_gameid($id,$solo)
{
  DB_query("UPDATE Game SET solo=".DB_quote_smart($solo)." WHERE id=".DB_quote_smart($id));
  return;
}

function DB_set_sickness_by_hash($hash,$sickness)
{
  DB_query("UPDATE Hand SET sickness=".DB_quote_smart($sickness)." WHERE hash=".DB_quote_smart($hash));
  return;
}

function DB_get_current_trickid($gameid)
{
  $trickid  = NULL;
  $sequence = NULL;
  $number   = 0;

  $result = DB_query("SELECT Trick.id,MAX(Play.sequence) FROM Play ".
		     "LEFT JOIN Trick ON Play.trick_id=Trick.id ".
		     "WHERE Trick.game_id=".DB_quote_smart($gameid)." ".
		     "GROUP BY Trick.id");
  while( $r = DB_fetch_array($result) )
    {
      $trickid  = $r[0];
      $sequence = $r[1];
      $number++;
    };

  if(!$sequence || $sequence==4)
    {
      DB_query("INSERT INTO Trick VALUES (NULL,NULL,NULL, ".DB_quote_smart($gameid).",NULL)");
      $trickid  = DB_insert_id();
      $sequence = 1;
      $number++;
    }
  else
    {
      $sequence++;
    }

  return array($trickid,$sequence,$number);
}

function DB_get_max_trickid($gameid)
{
  $r = DB_query_array("SELECT MAX(id) FROM Trick WHERE game_id=".DB_quote_smart($gameid));

  return ($r?$r[0]:NULL);
}

function DB_play_card($trickid,$handcardid,$sequence)
{
  DB_query("INSERT INTO Play VALUES(NULL,NULL,NULL,".DB_quote_smart($trickid).
	   ",".DB_quote_smart($handcardid).",".DB_quote_smart($sequence).")");

  $playid = DB_insert_id();
  return $playid;
}

function DB_get_all_names_by_gameid($id)
{
  $names = array();

  $result = DB_query("SELECT fullname FROM Hand LEFT JOIN User ON Hand.user_id=User.id WHERE game_id=".
		     DB_quote_smart($id)." ORDER BY position ASC");
  while($r = DB_fetch_array($result))
    $names[] = $r[0];

  return $names;
}

function DB_get_all_userid_by_gameid($id)
{
  $names = array();

  $result = DB_query("SELECT user_id FROM Hand WHERE game_id=".
		     DB_quote_smart($id)." ORDER BY position ");
  while($r = DB_fetch_array($result))
    $names[] = $r[0];

  return $names;
}

function DB_get_hash_from_game_and_pos($id,$pos)
{
  $r = DB_query_array("SELECT hash FROM Hand WHERE game_id=".DB_quote_smart($id)." and position=".DB_quote_smart($pos));

  if($r)
    return $r[0];
  else
    return "";
}

function DB_get_hash_from_gameid_and_userid($id,$user)
{
  $r = DB_query_array("SELECT hash FROM Hand WHERE game_id=".DB_quote_smart($id)." and user_id=".DB_quote_smart($user));

  if($r)
    return $r[0];
  else
    return "";
}

function DB_get_all_names()
{
  $names  = array();

  $result = DB_query("SELECT fullname FROM User");
  while($r = DB_fetch_array($result))
    $names[] = $r[0];

  return $names;
}

function DB_get_names_of_last_logins($N)
{
  $names  = array();

  $result = DB_query("SELECT fullname FROM User ORDER BY last_login DESC LIMIT $N");
  while($r = DB_fetch_array($result))
    $names[] = $r[0];

  return $names;
}

function DB_get_names_of_new_logins($N)
{
  $names  = array();

  $result = DB_query("SELECT fullname FROM User ORDER BY create_date DESC, id DESC LIMIT $N");
  while($r = DB_fetch_array($result))
    $names[] = $r[0];

  return $names;
}

function DB_update_game_timestamp($gameid)
{
  DB_query("UPDATE Game SET mod_date = CURRENT_TIMESTAMP WHERE id=".DB_quote_smart($gameid));
  return;
}


function DB_update_user_timestamp($userid)
{
  DB_query("UPDATE User SET last_login = CURRENT_TIMESTAMP WHERE id=".DB_quote_smart($userid));
  return;
}

function DB_get_user_timestamp($userid)
{
  $r = DB_query_array("SELECT last_login 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));

  if($r)
    return $r[0];
  else
    return "Europe/London";
}

function DB_insert_comment($comment,$playid,$userid)
{
  DB_query("INSERT INTO Comment VALUES (NULL,NULL,NULL,$userid,$playid, ".DB_quote_smart($comment).")");

  return;
}

function DB_insert_note($comment,$gameid,$userid)
{
  DB_query("INSERT INTO Notes VALUES (NULL,NULL,NULL,$userid,$gameid, ".DB_quote_smart($comment).")");

  return;
}

function DB_get_notes_by_userid_and_gameid($userid,$gameid)
{
  $notes = array();

  $result = DB_query("SELECT comment FROM Notes WHERE user_id=".DB_quote_smart($userid) .
		     " AND game_id=".DB_quote_smart($gameid));

  while($r = DB_fetch_array($result))
    $notes[] = $r[0];

  return $notes;
}


function DB_get_gametype_by_gameid($id)
{
  $r = DB_query_array("SELECT type FROM Game WHERE id=".DB_quote_smart($id));

  if($r)
    return $r[0]."";
  else
    return "";
}

function DB_set_gametype_by_gameid($id,$p)
{
  DB_query("UPDATE Game SET type='".$p."' WHERE id=".DB_quote_smart($id));
  return;
}

function DB_get_solo_by_gameid($id)
{
  $r = DB_query_array("SELECT solo FROM Game WHERE id=".DB_quote_smart($id));

  if($r)
    return $r[0]."";
  else
    return "";
}


function DB_get_startplayer_by_gameid($id)
{
  $r = DB_query_array("SELECT startplayer FROM Game WHERE id=".DB_quote_smart($id));

  if($r)
    return $r[0];
  else
    return 0;
}

function DB_set_startplayer_by_gameid($id,$p)
{
  DB_query("UPDATE Game SET startplayer='".$p."' WHERE id=".DB_quote_smart($id));
  return;
}

function DB_get_player_by_gameid($id)
{
  $r = DB_query_array("SELECT player FROM Game WHERE id=".DB_quote_smart($id));

  if($r)
    return $r[0];
  else
    return 0;
}
function DB_set_player_by_gameid($id,$p)
{
  DB_query("UPDATE Game SET player='".DB_quote_smart($p)."' WHERE id=".DB_quote_smart($id));
  return;
}



function DB_get_ruleset_by_gameid($id)
{
  $r = DB_query_array("SELECT ruleset FROM Game WHERE id=".DB_quote_smart($id));

  if($r)
    return $r[0];
  else
    return NULL;
}

function DB_get_session_by_gameid($id)
{
  $r = DB_query_array("SELECT session FROM Game WHERE id=".DB_quote_smart($id));

  if($r)
    return $r[0];
  else
    return NULL;
}

function DB_get_max_session()
{
  $r = DB_query_array("SELECT MAX(session) FROM Game");

  if($r)
    return $r[0];
  else
    return 0;
}

function DB_get_hashes_by_session($session,$user)
{
  $r = array();

  $result = DB_query("SELECT Hand.hash FROM Hand".
		     " LEFT JOIN Game ON Game.id=Hand.game_id ".
		     " WHERE Game.session=".DB_quote_smart($session).
		     " AND Hand.user_id=".DB_quote_smart($user).
		     " ORDER BY Game.create_date ASC");
  while($t = DB_fetch_array($result))
    $r[] = $t[0];

  return $r;
}

function DB_get_ruleset($dullen,$schweinchen,$call)
{
  $r = array();

  $result = DB_query("SELECT id FROM Rulesets WHERE".
		     " dullen=".DB_quote_smart($dullen)." AND ".
		     " call=".DB_quote_smart($call)." AND ".
		     " schweinchen=".DB_quote_smart($schweinchen));
  if($result)
    $r    = DB_fetch_array($result);

  if($r)
    return $r[0]; /* found ruleset */
  else
    {
      /* create new one */
      $result = DB_query("INSERT INTO Rulesets VALUES (NULL, NULL, ".
			 DB_quote_smart($dullen).",".
			 DB_quote_smart($schweinchen).",".
			 DB_quote_smart($call).
			 ", NULL)");
      if($result)
	return DB_insert_id();
    };

  return -1; /* something went wrong */
}

function DB_get_party_by_hash($hash)
{
  $r = DB_query_array("SELECT party FROM Hand WHERE hash=".DB_quote_smart($hash));

  if($r)
    return $r[0];
  else
    return NULL;
}

function DB_get_party_by_gameid_and_userid($gameid,$userid)
{
  $r = DB_query_array("SELECT party FROM Hand".
		      " WHERE game_id=".DB_quote_smart($gameid).
		      "  AND user_id=".DB_quote_smart($userid));
  if($r)
    return $r[0];
  else
    return NULL;
}

function DB_set_party_by_hash($hash,$party)
{
  DB_query("UPDATE Hand SET party=".DB_quote_smart($party)." WHERE hash=".DB_quote_smart($hash));
  return;
}

function DB_get_PREF($myid)
{
  /* Cardset */
  $r = DB_query_array("SELECT value from User_Prefs".
		      " WHERE user_id='$myid' AND pref_key='cardset'" );
  if($r)
    {
      /* licence only valid until then */
      if($r[0]=="altenburg" && (time()-strtotime( "2009-12-31 23:59:59")<0) ) 
	$PREF["cardset"]="altenburg";
      else
	$PREF["cardset"]="english";
    }
  else
    $PREF["cardset"]="english";

  /* Email */
  $r = DB_query_array("SELECT value FROM User_Prefs".
		      " WHERE user_id='$myid' AND pref_key='email'" );
  if($r)
    {
      if($r[0]=="emailaddict")
	$PREF["email"]="emailaddict";
      else
	$PREF["email"]="emailnonaddict";
    }
  else
    $PREF["email"]="emailnonaddict";

  return $PREF;
}

function DB_get_RULES($gameid)
{
  $r = DB_query_array("SELECT * FROM Rulesets".
		      " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
		      " WHERE Game.id='$gameid'" );
  
  $RULES["dullen"]      = $r[2];
  $RULES["schweinchen"] = $r[3];
  $RULES["call"]        = $r[4];
  
  return $RULES;
}

function DB_get_email_pref_by_hash($hash)
{
  $r = DB_query_array("SELECT value FROM Hand".
		      " LEFT JOIN User_Prefs ON Hand.user_id=User_Prefs.user_id".
		      " WHERE hash='$hash' AND pref_key='email'" );
  if($r)
    {
      if($r[0]=="emailaddict")
	return "emailaddict";
      else
	return "emailnonaddict";
    }
  else
    return "emailnonaddict";
}

function DB_get_email_pref_by_uid($uid)
{
  $r = DB_query_array("SELECT value FROM User_Prefs ".
		      " WHERE user_id='$uid' AND pref_key='email'" );
  if($r)
    {
      if($r[0]=="emailaddict")
	return "emailaddict";
      else
	return "emailnonaddict";
    }
  else
    return "emailnonaddict";
}

function DB_get_unused_randomnumbers($userstr)
{
  $r = DB_query_array(" SELECT randomnumbers FROM Game".
		      "   WHERE randomnumbers NOT IN".
		      "           (SELECT randomnumbers FROM Game".
		      "                LEFT JOIN Hand ON Game.id=Hand.game_id".
		      "                WHERE user_id IN  (". $userstr .")".
		      "                GROUP BY randomnumbers".
		      "           )");
  if($r)
    return $r[0];
  else
    return "";
}

function DB_get_number_of_passwords_recovery($user)
{
  $r = DB_query_array("SELECT COUNT(*) FROM Recovery ".
		      "  WHERE user_id=$user ".
		      "  AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= create_date".
		      "  GROUP BY user_id " );
  if($r)
    return $r[0];
  else
    return 0;
}

function DB_set_recovery_password($user,$newpw)
{
  DB_query("INSERT INTO Recovery VALUES(NULL,".DB_quote_smart($user).
	   ",".DB_quote_smart($newpw).",NULL)");
  return;
}

function DB_get_card_name($card)
{
  $r = DB_query_array("SELECT strength,suite FROM Card WHERE id='$card'");

  if($r)
    return $r[0]." of ".$r[1];
  else
    return "Error during get_card_name ".$card;
}

function DB_get_current_playid($gameid)
{
  $trick = DB_get_max_trickid($gameid);

  if(!$trick) return NULL;

  $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 "";
}

function DB_get_call_by_hash($hash)
{
  $r = DB_query_array("SELECT point_call FROM Hand WHERE hash='$hash'");

  if($r)
    return $r[0];

  return NULL;
}

function DB_get_partner_call_by_hash($hash)
{
  $partner = DB_get_partner_hash_by_hash($hash);

  if($partner)
    {
      $r = DB_query_array("SELECT point_call FROM Hand WHERE hash='$partner'");

      if($r)
	return $r[0];
    }

  return NULL;
}

function DB_get_partner_hash_by_hash($hash)
{
  $gameid = DB_get_gameid_by_hash($hash);
  $party  = DB_get_party_by_hash($hash);

  $r = DB_query_array("SELECT hash FROM Hand WHERE game_id='$gameid' AND party='$party' AND hash<>'$hash'");

  if($r)
    return $r[0];

  return NULL;
}

function DB_format_gameid($gameid)
{
  $session = DB_get_session_by_gameid($gameid);

  /* get number of game */
  $r = DB_query_array("SELECT COUNT(*),create_date FROM Game".
		      " WHERE session='$session' ".
		      " AND TIMEDIFF(create_date, (SELECT create_date FROM Game WHERE id='$gameid'))<=0 ".
		      " GROUP by session");
  return $session.".".$r[0];
}

function DB_get_reminder($user,$gameid)
{
  $r = DB_query_array("SELECT COUNT(*) FROM Reminder ".
		      "  WHERE user_id=$user ".
		      "  AND game_id=$gameid ".
		      "  AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= create_date".
		      "  GROUP BY user_id " );
  if($r)
    return $r[0];
  else
    return 0;
}

function DB_set_reminder($user,$gameid)
{
  DB_query("INSERT INTO Reminder ".
	   "  VALUES(NULL, ".DB_quote_smart($user).", ".DB_quote_smart($gameid).
	   ", NULL) ");
  return 0;
}

function DB_is_session_active($session)
{
  $r = DB_query_array("SELECT COUNT(*) FROM Game ".
		      "  WHERE session=$session ".
		      "  AND status<>'gameover' ");
  if($r)
    return $r[0];
  else
    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 */
    $queryresult = DB_query("SELECT id FROM Game ".
			    " WHERE status='gameover' ".
			    " ORDER BY create_date ASC");
  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++;
    }

  return $ids;
}

function DB_get_card_value_by_cardid($id)
{
  $r = DB_query_array("SELECT points FROM Card ".
		      "  WHERE id=$id ");

  if($r)
    return $r[0];
  else
    return NULL;
}

function DB_get_userid($type,$var1="",$var2="")
{
  /* get the userid of a user
   * this can be done several ways, which are all handled below
   * if a email/password combination is given and it doesn't work, we also
   * need to check the recovery table for additional passwords
   */

  $r = NULL;
  
  switch($type)
    {
    case 'name':
      $result = DB_query("SELECT id FROM User WHERE fullname=".DB_quote_smart($var1));
      break;
    case 'hash':
      $result = DB_query("SELECT user_id FROM Hand WHERE hash=".DB_quote_smart($var1));
      break;
    case 'password':
      $result = DB_query("SELECT id FROM User WHERE password=".DB_quote_smart($var1));
      break;
    case 'email':
      $result = DB_query("SELECT id FROM User WHERE email=".DB_quote_smart($var1));
      break;
    case 'email-password':
      $result = DB_query("SELECT id FROM User WHERE email=".DB_quote_smart($var1)." AND password=".DB_quote_smart($var2));
      $r = DB_fetch_array($result);
      /* test if a recovery password has been set */
      if(!$r)
	{
	  echo "testing alternative password";
	  $result = DB_query("SELECT User.id FROM User".
			     " LEFT JOIN Recovery ON User.id=Recovery.user_id".
			     " WHERE email=".DB_quote_smart($var1).
			     " AND Recovery.password=".DB_quote_smart($var2).
			     " AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= Recovery.create_date");
	}
      break;
    case 'gameid-position':
      $result = DB_query("SELECT user_id FROM Hand WHERE game_id=".
			 DB_quote_smart($var1)." AND position=".
			 DB_quote_smart($var2));
      break;
    }

  if(!$r)
    $r = DB_fetch_array($result);

  if($r)
    return $r[0];
  else
    return 0;
}

function DB_get_email($type,$var1='',$var2='')
{
  /* return the email of a user
   * this is used for sending out emails, but also for
   * testing the login for example
   */
  switch($type)
    {
    case 'name':
      $result = DB_query("SELECT email FROM User WHERE fullname=".DB_quote_smart($var1)."");
      break;
    case 'userid':
      $result = DB_query("SELECT email FROM User WHERE id=".DB_quote_smart($var1)."");
      break;
    case 'hash':
      $result = DB_query("SELECT User.email FROM User ".
			 "LEFT JOIN Hand ON Hand.user_id=User.id ".
			 "WHERE Hand.hash=".DB_quote_smart($var1)."");
      break;
    case 'position-gameid':
      $result = DB_query("SELECT email FROM User ".
			 "LEFT JOIN Hand ON User.id=Hand.user_id ".
			 "LEFT JOIN Game ON Game.id=Hand.game_id ".
			 "WHERE Game.id=".DB_quote_smart($var2)." ".
			 "AND Hand.position=".DB_quote_smart($var1)."");
      break;
    }
  
  $r = DB_fetch_array($result);

  if($r)
    return $r[0];
  else
    return "";
}

function DB_get_name($type,$var1='')
{
  /* get the full name of a user
   * a user can be uniquely identified several ways
   */
  switch($type)
    {
    case 'hash':
      $r = DB_query_array("SELECT fullname FROM Hand LEFT JOIN User ON Hand.user_id=User.id WHERE hash=".DB_quote_smart($var1));
      break;
    case 'email':
      $r = DB_query_array("SELECT fullname FROM User WHERE email=".DB_quote_smart($var1));
      break;
    case 'userid':
      $r = DB_query_array("SELECT fullname FROM User  WHERE id=".DB_quote_smart($var1));
    }

  if($r)
    return $r[0];
  else
    return "";
}

?>