Merge branch 'master' of /home/arun/nubati.net/git/e-DoKo
[e-DoKo.git] / include / functions.php
1 <?php
2 /* Copyright 2006, 2007, 2008, 2009, 2010 Arun Persaud <arun@nubati.net>
3  *
4  *   This file is part of e-DoKo.
5  *
6  *   e-DoKo is free software: you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, either version 3 of the License, or
9  *   (at your option) any later version.
10  *
11  *   e-DoKo is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with e-DoKo.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 /* make sure that we are not called from outside the scripts,
22  * use a variable defined in config.php to check this
23  */
24 if(!isset($HOST))
25   exit;
26
27 function config_check()
28 {
29   global $EmailName,$EMAIL_REPLY,$ADMIN_NAME,$ADMIN_EMAIL,$DB_work;
30
31   /* check if some variables are set in the config file, else set defaults */
32   if(!isset($EmailName))
33     $EmailName="[DoKo] ";
34   if(isset($EMAIL_REPLY))
35     {
36       ini_set("sendmail_from",$EMAIL_REPLY);
37     }
38   if(!isset($ADMIN_NAME))
39     {
40       output_header();
41       echo "<h1>Setup not completed</h1>";
42       echo "You need to set \$ADMIN_NAME in config.php.";
43       output_footer();
44       exit();
45     }
46   if(!isset($ADMIN_EMAIL))
47     {
48       output_header();
49       echo "<h1>Setup not completed</h1>";
50       echo "You need to set \$ADMIN_EMAIL in config.php. ".
51         "If something goes wrong an email will be send to this address.";
52       output_footer();
53       exit();
54     }
55   if(!isset($DB_work))
56     {
57       output_header();
58       echo "<h1>Setup not completed</h1>";
59       echo "You need to set \$DB_work in config.php. ".
60         "If this is set to 1, the game will be suspended and one can work safely on the database.".
61         "The default should be 0 for the game to work.";
62       output_footer();
63       exit();
64     }
65   if($DB_work)
66     {
67       output_header();
68       echo "Working on the database...please check back later.";
69       output_footer();
70       exit();
71     }
72
73   return;
74 }
75
76 function mymail($uid,$subject,$message)
77 {
78   global $EmailName;
79
80   /* do we send the email right away or save it in the database? */
81   $send_now = 1;
82
83   /* add standard header and footer */
84   $subject = "$EmailName".$subject;
85
86   /* standard goodbye */
87   $footer  = "\nHave a nice day\n".
88     "   your E-Doko service department\n\n".
89     "-- \n".
90     "You can change your mail delivery mode in the preference menu.\n".
91     'web: http://doko.nubati.net   '.
92     'help: http://wiki.nubati.net/EmailDoko   '.
93     'bugs: http://wiki.nubati.net/EmailDokoIssues';
94
95   if(is_array($uid))
96     {
97       /* send email to more than one person */
98
99       $header  = "Hello all\n\n";
100
101       foreach($uid as $user)
102         {
103           $all[] = DB_get_email('userid',$user);
104         }
105       $To = implode(",",$all);
106     }
107   else
108     {
109       /* standard greeting */
110       $name    = DB_get_name('userid',$uid);
111       $header  = "Hello $name\n\n";
112
113       $To = DB_get_email('userid',$uid);
114
115       /* check if user wants email right away or if we should save it in
116        * the database for later delivery
117        */
118
119       $PREF = DB_get_PREF($uid);
120       if( $PREF['digest'] != 'digest-off' )
121         $send_now = 0;
122     }
123
124   if($send_now)
125     sendmail($To,$subject,$header.$message.$footer);
126   else
127     {
128       /* store email in database */
129       DB_digest_insert_email($To,$message);
130     }
131 }
132
133 function sendmail($To,$Subject,$message)
134 {
135   /* this function sends the mail or outputs to the screen in case of debugging */
136   global $debug,$EMAIL_REPLY;
137
138   $header = "";
139
140   if(isset($EMAIL_REPLY))
141     $header .= "From: e-DoKo daemon <$EMAIL_REPLY>\r\n";
142
143   if($debug)
144     {
145       /* display email on screen,
146        * change txt -> html
147        */
148       $message = str_replace("\n","<br />\n",$message);
149       $message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
150                      "<a href=\"\\0\">\\0</a>", $message);
151
152       echo "<br />To: $To<br />";
153       if($header != "")
154         echo $header."<br />";
155       echo "Subject: $Subject <br />$message<br />\n";
156     }
157   else
158     if($header != "")
159       mail($To,$Subject,$message,$header);
160     else
161       mail($To,$Subject,$message);
162   return;
163 }
164
165 function myisset()
166 {
167   /* returns 1 if all names passed as args are defined by a GET or POST statement,
168    * else return 0
169    */
170
171   $ok   = 1;
172   $args = func_get_args();
173
174   foreach($args as $arg)
175     {
176       $ok = $ok * isset($_REQUEST[$arg]);
177       /*echo "$arg: ok = $ok <br />";
178        */
179     }
180   return $ok;
181 }
182
183 function myerror($message)
184 {
185   echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
186   sendmail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
187   return;
188 }
189
190 function pos_array($c,$arr)
191 {
192   $ret = 0;
193
194   $i   = 0;
195   foreach($arr as $a)
196     {
197       $i++;
198       if($a == $c)
199         {
200           $ret = $i;
201           break;
202         }
203     }
204   return $ret;
205 }
206
207 function is_trump($c)
208 {
209   global $CARDS;
210
211   if(in_array($c,$CARDS["trump"]))
212     return 1;
213   else
214     return 0;
215 }
216
217 function is_same_suite($c1,$c2)
218 {
219   global $CARDS;
220
221   if(in_array($c1,$CARDS["trump"]   ) && in_array($c2,$CARDS["trump"]   ) ) return 1;
222   if(in_array($c1,$CARDS["clubs"]   ) && in_array($c2,$CARDS["clubs"]   ) ) return 1;
223   if(in_array($c1,$CARDS["hearts"]  ) && in_array($c2,$CARDS["hearts"]  ) ) return 1;
224   if(in_array($c1,$CARDS["spades"]  ) && in_array($c2,$CARDS["spades"]  ) ) return 1;
225   if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
226
227   return 0;
228 }
229
230 function compare_cards($a,$b,$game)
231 {
232   /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
233
234   global $CARDS;
235   global $RULES;
236   global $GAME;
237
238   /* first map all cards to the odd number,
239    * this insure that the first card wins the trick
240    * if they are the same card
241    */
242   if( $a/2 - (int)($a/2) != 0.5)
243     $a--;
244   if( $b/2 - (int)($b/2) != 0.5)
245     $b--;
246
247   /* check for schweinchen and ten of hearts*/
248   switch($game)
249     {
250     case "normal":
251     case "silent":
252     case "trump":
253       if($RULES['schweinchen']=='both' && $GAME['schweinchen-who'])
254         {
255           if($a == 19 || $a == 20 )
256             return 1;
257           if($b == 19 || $b == 20 )
258             return 0;
259         }
260       else if($RULES['schweinchen']=='second' && $GAME['schweinchen-second'])
261         {
262           if($a == 19 || $a == 20 )
263             return 1;
264           if($b == 19 || $b == 20 )
265             return 0;
266         }
267       else if($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-who'] && $GAME['schweinchen-second'] )
268         {
269           /* check if a call was made either by the player or his partner. If so activate Schweinchen rule. */
270           if(DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
271             {
272               if($a == 19 || $a == 20 )
273                 return 1;
274               if($b == 19 || $b == 20 )
275                 return 0;
276             }
277           /* if not, do nothing and the foxes are just handeled as normal trump */
278         }
279         ;
280     case "heart":
281     case "spade":
282     case "club":
283       /* check for ten of hearts rule */
284       if($RULES["dullen"]=="secondwins")
285         if($a==1 && $b==1) /* both 10 of hearts */
286           return 0;        /* second one wins.*/
287     case "trumpless":
288     case "jack":
289     case "queen":
290       /* no special cases here */
291     }
292
293   /* normal case */
294   if(is_trump($a) && is_trump($b) && $a<=$b)
295     return 1;
296   else if(is_trump($a) && is_trump($b) )
297     return 0;
298   else
299     { /*$a is not a trump */
300       if(is_trump($b))
301         return 0;
302       else
303         { /* both no trump */
304
305           /* both clubs? */
306           $posA = pos_array($a,$CARDS["clubs"]);
307           $posB = pos_array($b,$CARDS["clubs"]);
308           if($posA && $posB)
309             if($posA <= $posB)
310               return 1;
311             else
312               return 0;
313
314           /* both spades? */
315           $posA = pos_array($a,$CARDS["spades"]);
316           $posB = pos_array($b,$CARDS["spades"]);
317           if($posA && $posB)
318             if($posA <= $posB)
319               return 1;
320             else
321               return 0;
322
323           /* both hearts? */
324           $posA = pos_array($a,$CARDS["hearts"]);
325           $posB = pos_array($b,$CARDS["hearts"]);
326           if($posA && $posB)
327             if($posA <= $posB)
328               return 1;
329             else
330               return 0;
331
332           /* both diamonds? */
333           $posA = pos_array($a,$CARDS["diamonds"]);
334           $posB = pos_array($b,$CARDS["diamonds"]);
335           if($posA && $posB)
336             if($posA <= $posB)
337               return 1;
338             else
339               return 0;
340
341           /* not the same suit and no trump: a wins */
342           return 1;
343         }
344     }
345 }
346
347 function get_winner($p,$mode)
348 {
349   /* get all 4 cards played in a trick, in the order they are played */
350   $tmp = $p[1];
351   $c1    = $tmp["card"];
352   $c1pos = $tmp["pos"];
353
354   $tmp = $p[2];
355   $c2    = $tmp["card"];
356   $c2pos = $tmp["pos"];
357
358   $tmp = $p[3];
359   $c3    = $tmp["card"];
360   $c3pos = $tmp["pos"];
361
362   $tmp = $p[4];
363   $c4    = $tmp["card"];
364   $c4pos = $tmp["pos"];
365
366   /* first card is better than all the rest */
367   if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
368     return $c1pos;
369
370   /* second card is better than first and better than the rest */
371   if( !compare_cards($c1,$c2,$mode) &&  compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
372     return $c2pos;
373
374   /* third card is better than first card and better than last */
375   if( !compare_cards($c1,$c3,$mode) &&  compare_cards($c3,$c4,$mode) )
376     /* if second card is better than first, third card needs to be even better */
377     if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
378       return $c3pos;
379     /* second is worse than first, e.g. not following suite */
380     else if (compare_cards($c1,$c2,$mode) )
381       return $c3pos;
382
383   /* non of the above */
384   return $c4pos;
385 }
386
387 function count_nines($cards)
388 {
389   $nines = 0;
390
391   foreach($cards as $c)
392     {
393       if($c == "25" || $c == "26") $nines++;
394       else if($c == "33" || $c == "34") $nines++;
395       else if($c == "41" || $c == "42") $nines++;
396       else if($c == "47" || $c == "48") $nines++;
397     }
398
399   return $nines;
400 }
401
402 function check_wedding($cards)
403 {
404
405   if( in_array("3",$cards) && in_array("4",$cards) )
406     return 1;
407
408   return 0;
409 }
410
411 function count_trump($cards,$status='pregame')
412 {
413   global $RULES;
414
415   $trump = 0;
416
417   /* count each trump, including the foxes, since this is used to determine poverty status */
418   foreach($cards as $c)
419     if( (int)($c) <27)
420       $trump++;
421
422   /* In case we really want to know the amount of trump, we can use the status variable.
423    * This is needed for example to figure out what icon to display on the table in case of
424    * trump given back in poverty */
425   if($status=='all') return $trump;
426
427   /* normally foxes don't count as trump, so we substract them here
428    * in case someone has schweinchen, one or two of them should count as trump
429    * though, so we need to add one trump for those cases */
430
431   /* subtract foxes */
432   if( in_array("19",$cards))
433     $trump--;
434   if( in_array("20",$cards) )
435     $trump--;
436
437   /* handle case where player has schweinchen */
438   if( in_array("19",$cards) && in_array("20",$cards) )
439     switch($RULES["schweinchen"])
440       {
441       case "both":
442         /* add two, in case the player has both foxes (schweinchen) */
443         $trump++;
444         $trump++;
445         break;
446       case "second":
447       case "secondaftercall":
448         /* add one, in case the player has both foxes (schweinchen) */
449         $trump++;
450         break;
451       case "none":
452         break;
453       }
454
455   return $trump;
456 }
457
458 function  create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
459 {
460   global $debug;
461
462   $r = array();
463
464   if($debug)
465     {
466       $r[ 0]=1;     $r[12]=47;   $r[24]=13;       $r[36]=37;
467       $r[ 1]=2;     $r[13]=23;   $r[25]=14;       $r[37]=38;
468       $r[ 2]=3;     $r[14]=27;   $r[26]=15;       $r[38]=39;
469       $r[ 3]=4;     $r[15]=16;   $r[27]=28;       $r[39]=40;
470       $r[ 4]=5;     $r[16]=17;   $r[28]=29;       $r[40]=41;
471       $r[ 5]=18;    $r[17]=6;    $r[29]=30;       $r[41]=42;
472       $r[ 6]=21;    $r[18]=7;    $r[30]=31;       $r[42]=43;
473       $r[ 7]=22;    $r[19]=8;    $r[31]=32;       $r[43]=44;
474       $r[ 8]=45;    $r[20]=9;    $r[32]=19;       $r[44]=33;
475       $r[ 9]=46;    $r[21]=10;   $r[33]=20;       $r[45]=24;
476       $r[10]=35;    $r[22]=11;   $r[34]=48;       $r[46]=25;
477       $r[11]=36;    $r[23]=12;   $r[35]=34;       $r[47]=26;
478     }
479   else
480     {
481       /* check if we can find a game were non of the player was involved and return
482        * cards insted
483        */
484       $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
485       $randomnumbers = DB_get_unused_randomnumbers($userstr);
486       $randomnumbers = explode(":",$randomnumbers);
487
488       if(sizeof($randomnumbers)==48)
489         return $randomnumbers;
490
491       /* need to create new numbers */
492       for($i=0;$i<48;$i++)
493         $r[$i]=$i+1;
494
495       /* shuffle using a better random generator than the standard one */
496       for ($i = 0; $i <48; $i++)
497         {
498           $j = @mt_rand(0, $i);
499           $tmp = $r[$i];
500           $r[$i] = $r[$j];
501           $r[$j] = $tmp;
502         }
503     };
504
505   return $r;
506 }
507
508 function display_cards($me,$myturn)
509 {
510   return;
511 }
512
513 function have_suit($cards,$c)
514 {
515   global $CARDS;
516   $suite = array();
517
518   if(in_array($c,$CARDS["trump"]))
519     $suite = $CARDS["trump"];
520   else if(in_array($c,$CARDS["clubs"]))
521     $suite = $CARDS["clubs"];
522   else if(in_array($c,$CARDS["spades"]))
523     $suite = $CARDS["spades"];
524   else if(in_array($c,$CARDS["hearts"]))
525     $suite = $CARDS["hearts"];
526   else if(in_array($c,$CARDS["diamonds"]))
527     $suite = $CARDS["diamonds"];
528
529   foreach($cards as $card)
530     {
531       if(in_array($card,$suite))
532         return 1;
533     }
534
535   return 0;
536 }
537
538 function same_type($card,$c)
539 {
540   global $CARDS;
541   $suite = "";
542
543   /* figure out what kind of card c is */
544   if(in_array($c,$CARDS["trump"]))
545     $suite = $CARDS["trump"];
546   else if(in_array($c,$CARDS["clubs"]))
547     $suite = $CARDS["clubs"];
548   else if(in_array($c,$CARDS["spades"]))
549     $suite = $CARDS["spades"];
550   else if(in_array($c,$CARDS["hearts"]))
551     $suite = $CARDS["hearts"];
552   else if(in_array($c,$CARDS["diamonds"]))
553     $suite = $CARDS["diamonds"];
554
555   /* card is the same suid return 1 */
556   if(in_array($card,$suite))
557     return 1;
558
559   return 0;
560 }
561
562 function set_gametype($gametype)
563 {
564   global $CARDS;
565   global $RULES;
566   global $GAME;
567
568   switch($gametype)
569     {
570     case "normal":
571     case "wedding":
572     case "poverty":
573     case "dpoverty":
574     case "trump":
575     case "silent":
576       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
577                                  '17','18','19','20','21','22','23','24','25','26');
578       $CARDS["diamonds"] = array();
579       $CARDS["clubs"]    = array('27','28','29','30','31','32','33','34');
580       $CARDS["spades"]   = array('35','36','37','38','39','40','41','42');
581       $CARDS["hearts"]   = array('43','44','45','46','47','48');
582       $CARDS["foxes"]    = array('19','20');
583       if($RULES["dullen"]=='none')
584         {
585           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
586                                      '17','18','19','20','21','22','23','24','25','26');
587           $CARDS["hearts"]   = array('43','44','1','2','45','46','47','48');
588         }
589       /* do we need to reorder for Schweinchen? need to search for it because of special case for dullen above*/
590       if($RULES['schweinchen']=='both'&& $GAME['schweinchen-who'])
591         {
592           /* find the fox and put them at the top of the stack */
593           foreach(array('19','20') as $fox)
594             {
595               /* search for fox */
596               $trump = $CARDS['trump'];
597               $key = array_keys($trump, $fox);
598
599               /* reorder */
600               $foxa = array();
601               $foxa[]=$trump[$key[0]];
602               unset($trump[$key[0]]);
603               $trump = array_merge($foxa,$trump);
604               $CARDS['trump'] = $trump;
605             }
606         }
607       else if( ($RULES['schweinchen']=='second' || $RULES['schweinchen']=='secondaftercall')
608                && $GAME['schweinchen-who'])
609         {
610           /* find the fox and put them at the top of the stack */
611           $trump = $CARDS['trump'];
612           $key = array_keys($trump, '19');
613
614           /* reorder */
615           $foxa = array();
616           $foxa[]=$trump[$key[0]];
617           unset($trump[$key[0]]);
618           $trump = array_merge($foxa,$trump);
619           $CARDS['trump'] = $trump;
620         }
621       break;
622     case "queen":
623       $CARDS["trump"]    = array('3','4','5','6','7','8','9','10');
624       $CARDS["clubs"]    = array('27','28','29','30','31','32','11','12','33','34');
625       $CARDS["spades"]   = array('35','36','37','38','39','40','13','14','41','42');
626       $CARDS["hearts"]   = array('43','44', '1', '2','45','46','15','16','47','48');
627       $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
628       $CARDS["foxes"]    = array();
629       break;
630     case "jack":
631       $CARDS["trump"]    = array('11','12','13','14','15','16','17','18');
632       $CARDS["clubs"]    = array('27','28','29','30','31','32','3', '4','33','34');
633       $CARDS["spades"]   = array('35','36','37','38','39','40','5', '6','41','42');
634       $CARDS["hearts"]   = array('43','44', '1', '2','45','46','7', '8','47','48');
635       $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
636       $CARDS["foxes"]    = array();
637       break;
638     case "trumpless":
639       $CARDS["trump"]    = array();
640       $CARDS["clubs"]    = array('27','28','29','30','31','32','3', '4','11','12','33','34');
641       $CARDS["spades"]   = array('35','36','37','38','39','40','5', '6','13','14','41','42');
642       $CARDS["hearts"]   = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
643       $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
644       $CARDS["foxes"]    = array();
645       break;
646     case "club":
647       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
648                                  '17','18','27','28','29','30','31','32','33','34');
649       $CARDS["clubs"]    = array();
650       $CARDS["spades"]   = array('35','36','37','38','39','40','41','42');
651       $CARDS["hearts"]   = array('43','44','45','46','47','48');
652       $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
653       $CARDS["foxes"]    = array();
654       if($RULES["dullen"]=='none')
655         {
656           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
657                                      '17','18','27','28','29','30','31','32','33','34');
658           $CARDS["hearts"]   = array('43','44','1','2','45','46','47','48');
659         }
660       break;
661     case "spade":
662       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
663                                  '17','18','35','36','37','38','39','40','41','42');
664       $CARDS["clubs"]    = array('27','28','29','30','31','32','33','34');
665       $CARDS["spades"]   = array();
666       $CARDS["hearts"]   = array('43','44','45','46','47','48');
667       $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
668       $CARDS["foxes"]    = array();
669       if($RULES["dullen"]=='none')
670         {
671           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
672                                      '17','18','35','36','37','38','39','40','41','42');
673           $CARDS["hearts"]   = array('43','44','1','2','45','46','47','48');
674         }
675       break;
676     case "heart":
677       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
678                                  '17','18','43','44','45','46','47','48');
679       $CARDS["clubs"]    = array('27','28','29','30','31','32','33','34');
680       $CARDS["spades"]   = array('35','36','37','38','39','40','41','42');
681       $CARDS["hearts"]   = array();
682       $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
683       $CARDS["foxes"]    = array();
684       if($RULES["dullen"]=='none')
685         {
686           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
687                             '17','18','43','44','1','2','45','46','47','48');
688         }
689       break;
690     }
691 }
692
693 function mysort($cards,$gametype)
694 {
695   global $PREF;
696   if(isset($PREF['sorting']))
697     if($PREF['sorting']=='high-low')
698       usort ( $cards, 'sort_comp_high_low' );
699     else
700       usort ( $cards, 'sort_comp_low_high' );
701   else
702     usort ( $cards, 'sort_comp_high_low' );
703   return $cards;
704 }
705
706 function sort_comp_high_low($a,$b)
707 {
708   global $CARDS;
709
710   $ALL = array();
711   $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
712                      $CARDS['hearts'],$CARDS['spades']);
713
714   return pos_array($a,$ALL)-pos_array($b,$ALL);
715 }
716
717 function sort_comp_low_high($a,$b)
718 {
719   global $CARDS;
720
721   $ALL = array();
722   $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
723                      $CARDS['hearts'],$CARDS['spades']);
724
725   return -pos_array($a,$ALL)+pos_array($b,$ALL);
726 }
727
728 function can_call($what,$hash)
729 {
730   /* figure out if a person can make a call:
731    $what in 0,30,60,90,120 = points of the call
732    $hash                   = the hash of the person who wants to make the call
733
734    return values:
735    0   can't make that call
736    1   can make the call
737    2   can make the call, but this is the last chance to do so...
738    */
739
740   global $RULES;
741
742   /* get some information
743    */
744   $gameid   = DB_get_gameid_by_hash($hash);
745   $gametype = DB_get_gametype_by_gameid($gameid);
746   $oldcall  = DB_get_call_by_hash($hash); /* did the person already made a call? */
747   $pcall    = DB_get_partner_call_by_hash($hash); /* did the partner already made a call */
748
749
750   /* you're call must be better than the one you or your partner already made
751    */
752   if( ($pcall!=NULL && ($what >= $pcall))
753       || ($oldcall!=NULL && ($what >=$oldcall)) )
754     {
755       return 0;
756     }
757
758   /* for some rules we need to know how many cards people have
759    */
760   $NRcards  = count(DB_get_hand($hash));
761
762   $NRallcards = 0;
763   for ($i=1;$i<5;$i++)
764     {
765       $user         = DB_get_hash_from_game_and_pos($gameid,$i);
766       $NRallcards  += count(DB_get_hand($user));
767     };
768
769   /* in case of a wedding, everything will be delayed by an offset
770    */
771   $offset = 0;
772   if($gametype=="wedding")
773     {
774       $offset = DB_get_sickness_by_gameid($gameid);
775       if ($offset <0) /* not resolved */
776         return 0;
777     };
778
779   /* now check if the call is allowed depending on the rule set
780    */
781   switch ($RULES["call"])
782     {
783     case "1st-own-card":
784       /* calls can be made before/while you play your card...
785        * first card = 120, second card = 90, etc.
786        */
787       if( 4-($what/30) == 12 - ($NRcards + $offset))
788         return 2;
789       if( 4-($what/30) > 12 - ($NRcards + $offset))
790         return 1;
791       break;
792     case "5th-card":
793       /* you can make the first call anytime during the first trick
794        */
795       if( 27+4*($what/30) == $NRallcards + $offset*4)
796         return 2;
797       if( 27+4*($what/30) < $NRallcards + $offset*4)
798         return 1;
799       break;
800     case "9-cards":
801       /* you can call 120 with 12 cards, 90 with 9 or more cards, 60 with 6 or more, etc.
802        * you can't skip a call though
803        */
804
805       /* figure out last call
806        */
807       if($oldcall!=NULL && $pcall!=NULL)
808         $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
809       else if($oldcall!=NULL)
810         $mincall = $oldcall;
811       else if ($pcall!=NULL)
812         $mincall = $pcall;
813       else
814         $mincall = -1;
815
816
817       if( 12 == ($NRcards + $offset))
818         {
819           return 2;
820         }
821       else if( 12 < ($NRcards + $offset))
822         {
823           return 1;
824         }
825       else if ( 9 == ($NRcards + $offset))
826         {
827           if( ($mincall>=0 && $mincall==120) )
828             return 2;
829         }
830       else if ( 9 < ($NRcards + $offset))
831         {
832           if( ($mincall>=0 && $mincall==120) )
833             return 1;
834         }
835       else if ( 6 == ($NRcards + $offset))
836         {
837           if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
838             return 2;
839         }
840       else if ( 6 < ($NRcards + $offset))
841         {
842           if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
843             return 1;
844         }
845       else if ( 3 == ($NRcards + $offset))
846         {
847           if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
848             return 2;
849         }
850       else if ( 3 < ($NRcards + $offset))
851         {
852           if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
853             return 1;
854         }
855       else if ( 0 == ($NRcards + $offset))
856         {
857           if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
858             return 2;
859         }
860       else if ( 0 < ($NRcards + $offset))
861         {
862           if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
863             return 1;
864         };
865       break;
866     }
867
868   return 0;
869 }
870
871 function display_table ()
872 {
873   global $gameid, $GT, $debug,$INDEX,$defaulttimezone,$session;
874   global $RULES,$GAME,$gametype;
875
876   $result = DB_query("SELECT  User.fullname as name,".
877                      "        Hand.position as position, ".
878                      "        User.id, ".
879                      "        Hand.party as party, ".
880                      "        Hand.sickness as sickness, ".
881                      "        Hand.point_call, ".
882                      "        User.last_login, ".
883                      "        Hand.hash,       ".
884                      "        User.timezone,    ".
885                      "        User.email       ".
886                      "FROM Hand ".
887                      "LEFT JOIN User ON User.id=Hand.user_id ".
888                      "WHERE Hand.game_id='".$gameid."' ".
889                      "ORDER BY position ASC");
890
891   echo "<div class=\"table\">\n".
892     "  <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
893   while($r = DB_fetch_array($result))
894     {
895       $name  = $r[0];
896       $pos   = $r[1];
897       $user  = $r[2];
898       $party = $r[3];
899       $sickness  = $r[4];
900       $call      = $r[5];
901       $hash      = $r[7];
902       $timezone  = $r[8];
903       $email     = $r[9];
904       $wins      = DB_get_number_of_tricks($gameid,$pos);
905       date_default_timezone_set($defaulttimezone);
906       $lastlogin = strtotime($r[6]);
907       date_default_timezone_set($timezone);
908       $timenow   = strtotime(date("Y-m-d H:i:s"));
909       $gravatar = "$name<br /><img class=\"gravatar\" title=\"$name\" src=\"http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=identicon\" />";
910
911
912       echo "  <div class=\"table".($pos-1)."\">\n";
913
914       if($debug)
915         echo "   <a href=\"".$INDEX."?action=game&amp;me=".$hash."\">";
916       if($vacation = check_vacation($user))
917         {
918           $start   = $vacation[0];
919           $stop    = substr($vacation[1],0,10);
920           $comment = $vacation[2];
921
922               $title = "begin: $start  end: $stop $comment";
923               echo "   <span class=\"vacation\" title=\"$title\">$gravatar (on vacation until $stop)</span> \n";
924         }
925       else
926         echo "   $gravatar \n";
927       if($debug)
928         echo"</a>\n";
929
930       /* add hints for poverty, wedding, solo, etc */
931       if( $gametype != "solo")
932         if( $RULES["schweinchen"]=="both" && $GAME["schweinchen-who"]==$hash )
933           echo " Schweinchen. <br />";
934
935       if($GT=="poverty" && $party=="re")
936         if($sickness=="poverty")
937           {
938             $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
939             $cards    = DB_get_all_hand($userhash);
940             $trumpNR  = count_trump($cards,'all');
941             if($trumpNR)
942               echo "   <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" title=\"poverty - trump back\" />";
943             else
944               echo "   <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" title=\"poverty - no trump back\" />";
945           }
946         else
947           echo "   <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
948
949       if($GT=="dpoverty")
950         if($party=="re")
951           if($sickness=="poverty")
952             {
953               $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
954               $cards    = DB_get_all_hand($userhash);
955               $trumpNR  = count_trump($cards,'all');
956               if($trumpNR)
957                 echo "   <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" title=\"poverty - trump back\" />";
958               else
959                 echo "   <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" title=\"poverty - no trump back\" />";
960             }
961           else
962             echo "   <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
963         else
964           if($sickness=="poverty")
965             {
966               $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
967               $cards    = DB_get_all_hand($userhash);
968               $trumpNR  = count_trump($cards,'all');
969               if($trumpNR)
970                 echo "   <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" title=\"poverty2 - trump back\"/>";
971               else
972                 echo "   <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" title=\"poverty2 - no trump back\" />";
973             }
974           else
975             echo "   <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" title=\"poverty2 partner\" />";
976
977       if($GT=="wedding" && $party=="re")
978         if($sickness=="wedding")
979           echo "   <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" title=\"wedding\" />";
980         else
981           echo "   <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" title=\"wedding partner\" />";
982
983       if(ereg("solo",$GT) && $party=="re")
984         {
985           if(ereg("queen",$GT))
986             echo "   <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Queen solo\" />";
987           else if(ereg("jack",$GT))
988             echo "   <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Jack solo\" />";
989           else if(ereg("club",$GT))
990             echo "   <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Club solo\" />";
991           else if(ereg("spade",$GT))
992             echo "   <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Spade solo\" />";
993           else if(ereg("heart",$GT))
994             echo "   <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Heart solo\" />";
995           else if(ereg("trumpless",$GT))
996             echo "   <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trumpless solo\" />";
997           else if(ereg("trump",$GT))
998             echo "   <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trump solo\" />";
999         }
1000
1001       /* add point calls */
1002       if($call!=NULL)
1003         {
1004           if($party=="re")
1005             echo "  <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" title=\"Re\" />";
1006           else
1007             echo "  <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" title=\"Contra\" />";
1008           switch($call)
1009             {
1010             case "0":
1011               echo "   <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" title=\"Call 0\" />";
1012               break;
1013             case "30":
1014               echo "   <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" title=\"Call 30\" />";
1015               break;
1016             case "60":
1017               echo "   <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" title=\"Call 60\" />";
1018               break;
1019             case "90":
1020               echo "   <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" title=\"Call 90\" />";
1021               break;
1022             }
1023         }
1024
1025       echo "    <br />\n";
1026       echo "    <span title=\"local time: ".date("Y-m-d H:i:s",$timenow).  " ".
1027                              "last login: ".date("Y-m-d H:i:s",$lastlogin)."\">".
1028                              "<img src=\"pics/button/time-info.png\" class=\"tinybutton\" alt=\"time info\" />".
1029                              "</span>\n";
1030
1031       /* show how many tricks the person made */
1032       switch($wins)
1033         {
1034         case 0:
1035           echo "#tricks 0"; break;
1036         case 1:
1037           echo "#tricks 1"; break;
1038         case 2:
1039         case 3:
1040         case 4:
1041           echo "#tricks few"; break;
1042         default:
1043           echo "#tricks many"; break;
1044         }
1045
1046       echo "   </div>\n";
1047
1048     }
1049   echo  "</div>\n"; /* end output table */
1050
1051
1052   return;
1053 }
1054
1055
1056 function display_user_menu($id)
1057 {
1058   global $WIKI,$INDEX;
1059
1060   $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
1061                      " LEFT JOIN Game On Hand.game_id=Game.id".
1062                      " WHERE Hand.user_id='$id'".
1063                      " AND ( Game.player='$id' OR ISNULL(Game.player) )".
1064                      " AND ( Game.status='pre' OR Game.status='play' )".
1065                      " ORDER BY Game.session" );
1066
1067   $i=0;
1068   while( $r = DB_fetch_array($result))
1069     {
1070       if($i==0)
1071         {
1072           echo "<div class=\"usermenu\">\n";
1073           echo "It's your turn in these games:<br />\n";
1074         }
1075
1076       $i++;
1077       echo "<a href=\"".$INDEX."?action=game&amp;me=".$r[0].
1078         "\">game ".DB_format_gameid($r[1])." </a><br />\n";
1079       if($i>4)
1080         {
1081           echo "...<br />\n";
1082           break;
1083         }
1084     }
1085
1086   if($i)
1087     echo  "</div>\n";
1088   return;
1089 }
1090
1091 function generate_score_table($session)
1092 {
1093   /* returns an array with N entries
1094    * $score[$i]["gameid"]   = gameid
1095    * $score[$i]["players"] = array (id=>total points)
1096    * $score[$i]["points"]   = points for this game
1097    * $score[$i]["solo"]     = 1 or 0
1098    */
1099   $score = array();
1100   $i=0;
1101
1102   /* get all ids */
1103   $gameids = DB_get_gameids_of_finished_games_by_session($session);
1104
1105   if($gameids == NULL)
1106     return $score;
1107
1108   /* get player id, names... from the first game */
1109   $player = array();
1110   $result = DB_query("SELECT User.id, User.fullname from Hand".
1111                      " LEFT JOIN User On Hand.user_id=User.id".
1112                      " WHERE Hand.game_id=".$gameids[0]);
1113   while( $r = DB_fetch_array($result))
1114     $player[$r[0]] = 0;
1115
1116   /* get points and generate table */
1117   foreach($gameids as $gameid)
1118     {
1119       $re_score = DB_get_score_by_gameid($gameid);
1120       $gametype = DB_get_gametype_by_gameid($gameid);
1121       foreach($player as $id=>$points)
1122         {
1123           $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1124           if($party == "re")
1125             if($gametype=="solo")
1126               $player[$id] += 3*$re_score;
1127             else
1128               $player[$id] += $re_score;
1129           else if ($party == "contra")
1130             $player[$id] -= $re_score;
1131         }
1132       $score[$i]['gameid']  = $gameid ;
1133       $score[$i]['players'] = $player;
1134       $score[$i]['points']  = abs($re_score);
1135       $score[$i]['solo']    = ($gametype=="solo");
1136
1137       $i++;
1138     }
1139
1140   return $score;
1141 }
1142
1143 function generate_global_score_table()
1144 {
1145   $return = array();
1146
1147   /* get all ids */
1148   $gameids = DB_get_gameids_of_finished_games_by_session(0);
1149
1150   if($gameids == NULL)
1151     return '';
1152
1153   /* get player id, names... from the User table */
1154   $player = array();
1155   $result = DB_query('SELECT User.id, User.fullname FROM User');
1156
1157   /* save information in an array */
1158   while( $r = DB_fetch_array($result))
1159     $player[$r[0]] = array('name'=> $r[1], 'points' => 0 ,'nr' => 0);
1160
1161   /* get points and generate table */
1162   foreach($gameids as $gameid)
1163     {
1164       $re_score = DB_get_score_by_gameid($gameid);
1165       $gametype = DB_get_gametype_by_gameid($gameid);
1166
1167       /* get players involved in this game */
1168       $result = DB_query('SELECT user_id FROM Hand WHERE game_id='.DB_quote_smart($gameid));
1169       while($r = DB_fetch_array($result))
1170         {
1171           $id = $r[0];
1172           $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1173           if($party == 're')
1174             if($gametype=='solo')
1175               $player[$id]['points'] += 3*$re_score;
1176             else
1177               $player[$id]['points'] += $re_score;
1178           else if ($party == 'contra')
1179             $player[$id]['points'] -= $re_score;
1180           if($party)
1181             $player[$id]['nr']+=1;
1182         }
1183     }
1184
1185   function cmp($a,$b)
1186   {
1187     if($a['nr']==0 ) return 1;
1188     if($b['nr']==0) return 1;
1189
1190     $a=$a['points']/$a['nr'];
1191     $b=$b['points']/$b['nr'];
1192
1193     if ($a == $b)
1194       return 0;
1195     return ($a > $b) ? -1 : 1;
1196   }
1197   usort($player,'cmp');
1198
1199   foreach($player as $pl)
1200     {
1201       /* limit to players with at least 10 games */
1202       if($pl['nr']>10)
1203         $return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3), $pl['points'],$pl['nr']);
1204     }
1205
1206   return $return;
1207 }
1208
1209 function format_score_table_ascii($score)
1210 {
1211   $output="";
1212   if(sizeof($score)==0)
1213     return "";
1214
1215   /* truncate table if we have too many games */
1216   $max = sizeof($score);
1217   if($max>6) $output.=" (table truncated to last 6 games)\n";
1218
1219   /* output header */
1220   foreach($score[0]['players'] as $id=>$points)
1221     {
1222       $name = DB_get_name('userid',$id); /*TODO*/
1223       $output.= "  ".substr($name,0,2)."  |";
1224     }
1225   $output.="  P   |\n";
1226   $output.= "------+------+------+------+------+\n";
1227
1228   /* output score for each game */
1229   $i=0;
1230   foreach($score as $game)
1231     {
1232       $i++;
1233       if($i-1<$max-6) continue;
1234
1235       foreach($game['players'] as $id=>$points)
1236         $output.=str_pad($points,6," ",STR_PAD_LEFT)."|";
1237       $output.=str_pad($game['points'],4," ",STR_PAD_LEFT);
1238
1239       /* check for solo */
1240       if($game['solo'])
1241         $output.= " S|";
1242       else
1243         $output.= "  |";
1244
1245       $output.="\n";
1246     }
1247   return $output;
1248 }
1249
1250 function format_score_table_html($score,$userid)
1251 {
1252   global $INDEX;
1253
1254   if(sizeof($score)==0)
1255     return "";
1256
1257   $output = "<div class=\"scoretable\">\n<table class=\"score\">\n";
1258
1259   /* output header */
1260   $header = "";
1261   $header.= " <thead>\n  <tr>\n";
1262   $header.= "   <th> No </th>";
1263   foreach($score[0]['players'] as $id=>$points)
1264     {
1265       $name = DB_get_name('userid',$id); /*TODO*/
1266       $header.= "<th> ".substr($name,0,2)." </th>";
1267     }
1268   $header.="<th>P</th>\n  </tr>\n </thead>\n";
1269
1270   /* use the same as footer */
1271   $footer = "";
1272   $footer.= " <tfoot>\n  <tr>\n";
1273   $footer.= "   <td> No </td>";
1274   foreach($score[0]['players'] as $id=>$points)
1275     {
1276       $name = DB_get_name('userid',$id); /*TODO*/
1277       $footer.= "<td> ".substr($name,0,2)." </td>";
1278     }
1279   $footer.="<td>P</td>\n  </tr>\n </tfoot>\n";
1280
1281   /* body */
1282   $body = "";
1283   $body.= " <tbody>\n";
1284   $i=0;
1285   foreach($score as $game)
1286     {
1287       $i++;
1288       $body.="  <tr>";
1289       $userhash = DB_get_hash_from_gameid_and_userid($game['gameid'],$userid);
1290       /* create link to old games only if you are logged in and its your game*/
1291       if(isset($_SESSION['id']) && $_SESSION['id']==$userid)
1292         $body.="  <td> <a href=\"".$INDEX."?action=game&amp;me=".$userhash."\">$i</a></td>";
1293       else
1294         $body.="  <td>$i</td>";
1295
1296       foreach($game['players'] as $id=>$points)
1297         $body.="<td>".$points."</td>";
1298       $body.="<td>".$game['points'];
1299
1300       /* check for solo */
1301       if($game['solo'])
1302         $body.= " S";
1303       $body.="</td></tr>\n";
1304     }
1305
1306   $output.=$header;
1307   if($i>12)
1308     $output.=$footer;
1309   $output.=$body;
1310
1311   $output.=" </tbody>\n</table>\n</div>\n";
1312
1313   return $output;
1314 }
1315
1316 function createCache($content, $cacheFile)
1317 {
1318   $fp = fopen($cacheFile,"w");
1319   if($fp)
1320     {
1321       fwrite($fp,$content);
1322       fclose($fp);
1323     }
1324   else
1325     echo "WARNING: couldn't create cache file";
1326
1327   return;
1328 }
1329
1330 function getCache($cacheFile, $expireTime)
1331 {
1332   if( file_exists($cacheFile) &&
1333       filemtime($cacheFile )>( time() - $expireTime ) )
1334     {
1335       return file_get_contents($cacheFile);
1336     }
1337
1338   return false;
1339 }
1340
1341 function check_vacation($userid)
1342 {
1343   /* get start date */
1344   $result = DB_query_array("SELECT value FROM User_Prefs".
1345                      " WHERE user_id='$userid' AND pref_key='vacation start'" );
1346   if($result)
1347     $start = $result[0];
1348   else
1349     return NULL;
1350
1351   /* get end date */
1352   $result = DB_query_array("SELECT value FROM User_Prefs".
1353                      " WHERE user_id='$userid' AND pref_key='vacation stop'" );
1354   if($result)
1355     $stop = $result[0];
1356   else
1357     return NULL;
1358
1359   /* get comment */
1360   $result = DB_query_array("SELECT value FROM User_Prefs".
1361                      " WHERE user_id='$userid' AND pref_key='vacation comment'" );
1362   if($result)
1363     $comment = $result[0];
1364   else
1365     $comment = '';
1366
1367   /* check if user is on vacation. TODO: use user's timezone */
1368   if( (time() - strtotime($start) >0) &&
1369       (strtotime($stop) - time()  >0))
1370     return array ($start,$stop,$comment);
1371   else
1372     return NULL;
1373 }
1374
1375 function cancel_game($why,$gameid)
1376 {
1377   $gameid = DB_quote_smart($gameid);
1378
1379   /* update the game table */
1380   switch($why)
1381     {
1382     case 'timedout':
1383       DB_query("UPDATE Game SET status='cancel-timedout' WHERE id=$gameid");
1384       break;
1385     case 'nines':
1386       DB_query("UPDATE Game SET status='cancel-nines' WHERE id=$gameid");
1387       break;
1388     case 'trump':
1389       DB_query("UPDATE Game SET status='cancel-trump' WHERE id=$gameid");
1390       break;
1391     case 'noplay':
1392       DB_query("UPDATE Game SET status='cancel-noplay' WHERE id=$gameid");
1393       break;
1394     }
1395   /* set each player to gameover */
1396   $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
1397   while($r = DB_fetch_array($result))
1398     {
1399       $id = $r[0];
1400       DB_query("UPDATE Hand SET status='gameover' WHERE id=".DB_quote_smart($id));
1401     }
1402
1403   return;
1404 }
1405
1406 function get_user_token($userid)
1407 {
1408
1409   $token = NULL;
1410
1411   $date = DB_get_user_creation_date($userid);
1412   $name = DB_get_name('userid',$userid);
1413
1414   if($date && $name)
1415     $token = md5("token".$name.$date);
1416
1417   return $token;
1418 }
1419
1420 ?>