CLEANUP: removed ereg-functions (deprecated in newer php versions)
[e-DoKo.git] / include / functions.php
index 3e21e6d3e4ebcc82253879b3cf237b38eff33654..16d094c343f3f77707495616110c0f69b97affc5 100644 (file)
@@ -1,4 +1,23 @@
 <?php
+/* Copyright 2006, 2007, 2008, 2009, 2010 Arun Persaud <arun@nubati.net>
+ *
+ *   This file is part of e-DoKo.
+ *
+ *   e-DoKo is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   e-DoKo is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with e-DoKo.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
 /* make sure that we are not called from outside the scripts,
  * use a variable defined in config.php to check this
  */
@@ -58,51 +77,56 @@ function mymail($uid,$subject,$message)
 {
   global $EmailName;
 
-  /* check if user wants email right away or if we should save it in
-   * the database for later delivery
-   */
-  if(0)
-    {
-      /* send to database (not yet implemented)*/
-    }
-  else
-    {
-      /* send email right away */
+  /* do we send the email right away or save it in the database? */
+  $send_now = 1;
 
-      /* add standard header and footer */
-      $subject = "$EmailName".$subject;
+  /* add standard header and footer */
+  $subject = "$EmailName".$subject;
 
-      /* standard goodbye */
-      $footer  = "\nHave a nice day\n".
-       "   your E-Doko service department\n\n".
-       "-- \n".
-       "You can change your mail delivery mode in the preference menu.\n".
-       'web: http://doko.nubati.net   '.
-       'help: http://wiki.nubati.net/EmailDoko   '.
-       'bugs: http://wiki.nubati.net/EmailDokoIssues';
+  /* standard goodbye */
+  $footer  = "\nHave a nice day\n".
+    "   your E-Doko service department\n\n".
+    "-- \n".
+    "You can change your mail delivery mode in the preference menu.\n".
+    'web: http://doko.nubati.net   '.
+    'help: http://wiki.nubati.net/EmailDoko   '.
+    'bugs: http://wiki.nubati.net/EmailDokoIssues';
 
-      if(is_array($uid))
-       {
-         /* send email to more than one person */
+  if(is_array($uid))
+    {
+      /* send email to more than one person */
 
-         $header  = "Hello all\n\n";
+      $header  = "Hello all\n\n";
 
-         foreach($uid as $user)
-           {
-             $all[] = DB_get_email('userid',$user);
-           }
-         $To = implode(",",$all);
-       }
-      else
+      foreach($uid as $user)
        {
-         /* standard greeting */
-         $name    = DB_get_name('userid',$uid);
-         $header  = "Hello $name\n\n";
-
-         $To = DB_get_email('userid',$uid);
+         $all[] = DB_get_email('userid',$user);
        }
+      $To = implode(",",$all);
+    }
+  else
+    {
+      /* standard greeting */
+      $name    = DB_get_name('userid',$uid);
+      $header  = "Hello $name\n\n";
+
+      $To = DB_get_email('userid',$uid);
+
+      /* check if user wants email right away or if we should save it in
+       * the database for later delivery
+       */
 
-      sendmail($To,$subject,$header.$message.$footer);
+      $PREF = DB_get_PREF($uid);
+      if( $PREF['digest'] != 'digest-off' )
+       $send_now = 0;
+    }
+
+  if($send_now)
+    sendmail($To,$subject,$header.$message.$footer);
+  else
+    {
+      /* store email in database */
+      DB_digest_insert_email($To,$message);
     }
 }
 
@@ -122,8 +146,8 @@ function sendmail($To,$Subject,$message)
        * change txt -> html
        */
       $message = str_replace("\n","<br />\n",$message);
-      $message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
-                     "<a href=\"\\0\">\\0</a>", $message);
+      $message = preg_replace("#(\w+://[^<>\s]+[\w/]*)#",
+                             "<a href=\"$1\">$1</a>", $message);
 
       echo "<br />To: $To<br />";
       if($header != "")
@@ -703,19 +727,36 @@ function sort_comp_low_high($a,$b)
 
 function can_call($what,$hash)
 {
+  /* figure out if a person can make a call:
+   $what in 0,30,60,90,120 = points of the call
+   $hash                   = the hash of the person who wants to make the call
+
+   return values:
+   0   can't make that call
+   1   can make the call
+   2   can make the call, but this is the last chance to do so...
+   */
+
   global $RULES;
 
+  /* get some information
+   */
   $gameid   = DB_get_gameid_by_hash($hash);
   $gametype = DB_get_gametype_by_gameid($gameid);
-  $oldcall  = DB_get_call_by_hash($hash);
-  $pcall    = DB_get_partner_call_by_hash($hash);
+  $oldcall  = DB_get_call_by_hash($hash); /* did the person already made a call? */
+  $pcall    = DB_get_partner_call_by_hash($hash); /* did the partner already made a call */
 
-  if( ($pcall!=NULL && $what >= $pcall) ||
-      ($oldcall!=NULL && $what >=$oldcall) )
+
+  /* you're call must be better than the one you or your partner already made
+   */
+  if( ($pcall!=NULL && ($what >= $pcall))
+      || ($oldcall!=NULL && ($what >=$oldcall)) )
     {
       return 0;
     }
 
+  /* for some rules we need to know how many cards people have
+   */
   $NRcards  = count(DB_get_hand($hash));
 
   $NRallcards = 0;
@@ -725,7 +766,8 @@ function can_call($what,$hash)
       $NRallcards  += count(DB_get_hand($user));
     };
 
-  /* in case of a wedding, everything will be delayed by an offset */
+  /* in case of a wedding, everything will be delayed by an offset
+   */
   $offset = 0;
   if($gametype=="wedding")
     {
@@ -734,18 +776,34 @@ function can_call($what,$hash)
        return 0;
     };
 
+  /* now check if the call is allowed depending on the rule set
+   */
   switch ($RULES["call"])
     {
     case "1st-own-card":
-      if( 4-($what/30) >= 12 - ($NRcards + $offset))
+      /* calls can be made before/while you play your card...
+       * first card = 120, second card = 90, etc.
+       */
+      if( 4-($what/30) == 12 - ($NRcards + $offset))
+       return 2;
+      if( 4-($what/30) > 12 - ($NRcards + $offset))
        return 1;
       break;
     case "5th-card":
-      if( 27+4*($what/30) <= $NRallcards + $offset*4)
+      /* you can make the first call anytime during the first trick
+       */
+      if( 27+4*($what/30) == $NRallcards + $offset*4)
+       return 2;
+      if( 27+4*($what/30) < $NRallcards + $offset*4)
        return 1;
       break;
     case "9-cards":
+      /* you can call 120 with 12 cards, 90 with 9 or more cards, 60 with 6 or more, etc.
+       * you can't skip a call though
+       */
 
+      /* figure out last call
+       */
       if($oldcall!=NULL && $pcall!=NULL)
        $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
       else if($oldcall!=NULL)
@@ -755,26 +813,51 @@ function can_call($what,$hash)
       else
        $mincall = -1;
 
-      if( 12 <= ($NRcards + $offset))
+
+      if( 12 == ($NRcards + $offset))
+       {
+         return 2;
+       }
+      else if( 12 < ($NRcards + $offset))
        {
          return 1;
        }
-      else if ( 9 <= ($NRcards + $offset))
+      else if ( 9 == ($NRcards + $offset))
+       {
+         if( ($mincall>=0 && $mincall==120) )
+           return 2;
+       }
+      else if ( 9 < ($NRcards + $offset))
        {
          if( ($mincall>=0 && $mincall==120) )
            return 1;
        }
-      else if ( 6 <= ($NRcards + $offset))
+      else if ( 6 == ($NRcards + $offset))
+       {
+         if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
+           return 2;
+       }
+      else if ( 6 < ($NRcards + $offset))
        {
          if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
            return 1;
        }
-      else if ( 3 <= ($NRcards + $offset))
+      else if ( 3 == ($NRcards + $offset))
+       {
+         if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
+           return 2;
+       }
+      else if ( 3 < ($NRcards + $offset))
        {
          if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
            return 1;
        }
-      else if ( 0 <= ($NRcards + $offset))
+      else if ( 0 == ($NRcards + $offset))
+       {
+         if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
+           return 2;
+       }
+      else if ( 0 < ($NRcards + $offset))
        {
          if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
            return 1;
@@ -798,7 +881,8 @@ function display_table ()
                     "        Hand.point_call, ".
                     "        User.last_login, ".
                     "        Hand.hash,       ".
-                    "        User.timezone    ".
+                    "        User.timezone,    ".
+                    "        User.email       ".
                     "FROM Hand ".
                     "LEFT JOIN User ON User.id=Hand.user_id ".
                     "WHERE Hand.game_id='".$gameid."' ".
@@ -816,11 +900,14 @@ function display_table ()
       $call      = $r[5];
       $hash      = $r[7];
       $timezone  = $r[8];
+      $email     = $r[9];
       $wins      = DB_get_number_of_tricks($gameid,$pos);
       date_default_timezone_set($defaulttimezone);
       $lastlogin = strtotime($r[6]);
       date_default_timezone_set($timezone);
       $timenow   = strtotime(date("Y-m-d H:i:s"));
+      $gravatar = "$name<br /><img class=\"gravatar\" title=\"$name\" src=\"http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=identicon\" />";
+
 
       echo "  <div class=\"table".($pos-1)."\">\n";
 
@@ -833,10 +920,10 @@ function display_table ()
          $comment = $vacation[2];
 
              $title = "begin: $start  end: $stop $comment";
-             echo "   <span class=\"vacation\" title=\"$title\">$name (on vacation until $stop)</span> \n";
+             echo "   <span class=\"vacation\" title=\"$title\">$gravatar (on vacation until $stop)</span> \n";
        }
       else
-       echo "   $name \n";
+       echo "   $gravatar \n";
       if($debug)
        echo"</a>\n";
 
@@ -893,21 +980,21 @@ function display_table ()
        else
          echo "   <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" title=\"wedding partner\" />";
 
-      if(ereg("solo",$GT) && $party=="re")
+      if( (strpos($GT,"solo")!==false) && $party=="re")
        {
-         if(ereg("queen",$GT))
+         if(strpos($GT,"queen")!==false)
            echo "   <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Queen solo\" />";
-         else if(ereg("jack",$GT))
+         else if(strpos($GT,"jack")!==false)
            echo "   <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Jack solo\" />";
-         else if(ereg("club",$GT))
+         else if(strpos($GT,"club")!==false)
            echo "   <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Club solo\" />";
-         else if(ereg("spade",$GT))
+         else if(strpos($GT,"spade")!==false)
            echo "   <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Spade solo\" />";
-         else if(ereg("heart",$GT))
+         else if(strpos($GT,"heart")!==false)
            echo "   <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Heart solo\" />";
-         else if(ereg("trumpless",$GT))
+         else if(strpos($GT,"trumpless")!==false)
            echo "   <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trumpless solo\" />";
-         else if(ereg("trump",$GT))
+         else if(strpos($GT,"trump")!==false)
            echo "   <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trump solo\" />";
        }
 
@@ -1316,4 +1403,18 @@ function cancel_game($why,$gameid)
   return;
 }
 
+function get_user_token($userid)
+{
+
+  $token = NULL;
+
+  $date = DB_get_user_creation_date($userid);
+  $name = DB_get_name('userid',$userid);
+
+  if($date && $name)
+    $token = md5("token".$name.$date);
+
+  return $token;
+}
+
 ?>