Better randomizer chossen (rand() does only create 32767 different numbers under...
[mailer.git] / inc / mails / birthday_mails.php
index 2c3f047dbb4b388d97ec062c3ba603cefc9f718c..c4dab04700104eea6215fbdf820e5e07286858fa 100644 (file)
  ************************************************************************/
 
 // Some security stuff...
-if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
-{
+if (!defined('__SECURITY')) {
        $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
        require($INC);
-}
- elseif ((!EXT_IS_ACTIVE("birthday")) && (!IS_ADMIN()))
-{
-       ADD_FATAL(EXTENSION_PROBLEM_EXT_INACTIVE, "birthday");
+} elseif (!EXT_IS_ACTIVE("birthday")) {
        return;
 }
 
 // Do not execute when script is in CSS mode
-if ($CSS == 1) return;
+global $CSS;
+if (($CSS == 1) || (!defined('__DAILY_RESET'))) return;
 
 // Get current day (01 to 31), month (01 to 12) and year (4-digits year)
 $DAY   = date("d", time());
 $MONTH = date("m", time());
-$YEAR  = date("Y", time());
+$YEAR  = date('Y', time());
 
 // Shall I include only active members?
 $ADD = "%s"; $VALUE = "";
-if (($CONFIG['birthday_active']) && (EXT_IS_ACTIVE("autopurge")) && ($CONFIG['ap_in_since'] > 0) && ($CONFIG['ap_inactive'] == "Y"))
-{
-       $ADD = " AND last_online >= %d";
-       $VALUE = bigintval(time() - $CONFIG['ap_in_since']);
+if (($_CONFIG['birthday_active']) && (EXT_IS_ACTIVE("autopurge")) && ($_CONFIG['autopurge_inactive'] == "Y") && ($_CONFIG['ap_inactive_since'] > 0)) {
+       $ADD = " AND last_online >= (UNIX_TIMESTAP() - %s)";
+       $VALUE = $_CONFIG['ap_inactive_since'];
 }
 
 // Only confirmed members shall receive birthday mails...
 $result_birthday = SQL_QUERY_ESC("SELECT userid, email, birth_year
 FROM "._MYSQL_PREFIX."_user_data
-WHERE status='CONFIRMED' AND birth_day=%d AND birth_month=%d AND birthday_sent < ".(time() - (ONE_DAY*364)).$ADD."
+WHERE status='CONFIRMED' AND birth_day=%s AND birth_month=%s AND birthday_sent < (UNIX_TIMESTAMP() - ".($_CONFIG['one_day'] * 364).")".$ADD."
 ORDER BY userid",
  array($DAY, $MONTH, $VALUE), __FILE__, __LINE__);
 
-if (SQL_NUMROWS($result_birthday) > 0)
-{
+if (SQL_NUMROWS($result_birthday) > 0) {
        // Start sending out birthday mails
-       while (list($uid, $email, $byear) = SQL_FETCHROW($result_birthday))
-       {
+       while (list($uid, $email, $byear) = SQL_FETCHROW($result_birthday)) {
                // Calculate own timestamp for birthday and today
-               $BD  = $byear + 12*$MONTH + 365*$DAY;
-               $NOW = $YEAR  + 12*$MONTH + 365*$DAY;
+               $BD  = $byear + 12 * $MONTH + 365 * $DAY;
+               $NOW = $YEAR  + 12 * $MONTH + 365 * $DAY;
 
                // Simply subtract both values and you got the age... :)
                $AGE = $NOW - $BD;
 
-               if ($CONFIG['birthday_points'] > 0)
-               {
+               if ($_CONFIG['birthday_points'] > 0) {
                        // Prepare array for loading template
                        $content = array(
                                'age'    => $AGE,
-                               'points' => $CONFIG['birthday_points'],
+                               'points' => $_CONFIG['birthday_points'],
                                'check'  => "",
                        );
-                       for ($idx = 0; $idx < 4; $idx++)
-                       {
-                               $content['check'] .= GEN_RANDOM_CODE("8", rand(0, "$MONTH$DAY"), $uid, ($AGE*($idx+1)));
+
+                       for ($idx = 0; $idx < 4; $idx++) {
+                               $content['check'] .= GEN_RANDOM_CODE("8", mt_rand(0, "$MONTH$DAY"), $uid, ($AGE*($idx+1)));
                        }
 
                        // Insert row into database
-                       $result_insert = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_birthday (userid, points, chk_value) VALUES ('%s', '%s', '%s' )",
-                        array(bigintval($uid), $CONFIG['birthday_points'], $content['check']), __FILE__, __LINE__);
+                       $result_insert = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_birthday (userid, points, chk_value) VALUES ('%s','%s','%s' )",
+                        array(bigintval($uid), $_CONFIG['birthday_points'], $content['check']), __FILE__, __LINE__);
 
                        // Load email template with confirmation link
                        $msg = LOAD_EMAIL_TEMPLATE("member_birthday_confirm", $content, bigintval($uid));
-               }
-                else
-               {
+               } else {
                        // Load default email template and fill in the age
                        $msg = LOAD_EMAIL_TEMPLATE("member_birthday", $AGE, $uid);
                }
 
                // Send email
-               SEND_EMAIL($email, HAPPY_BIRTHDAY, $msg);
+               SEND_EMAIL($uid, HAPPY_BIRTHDAY, $msg);
 
                // Remember him that he has received a birthday mail
-               $result_bd = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET birthday_sent=UNIX_TIMESTAMP() WHERE userid=%d LIMIT 1",
+               $result_bd = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET birthday_sent=UNIX_TIMESTAMP() WHERE userid=%s LIMIT 1",
                 array(bigintval($uid)), __FILE__, __LINE__);
        }