Fixes for SQL error + missing config
[mailer.git] / inc / purge / purge-inact.php
index 25b1d830b01fcce283bc7c459db6efd765e5ebf2..a137f2ebb6315e07a2e528ee79773e3886aff946 100644 (file)
@@ -16,8 +16,8 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
- * For more information visit: http://www.mxchange.org                  *
+ * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
+ * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  * it under the terms of the GNU General Public License as published by *
@@ -43,7 +43,7 @@ if (!defined('__SECURITY')) {
 // Abort if autopurge is not active or disabled by admin
 if ((!isExtensionActive('autopurge')) || (!isAutoPurgingActive())) {
        // Abort here
-       return false;
+       return FALSE;
 } // END - if
 
 // Shall I look for inactive accounts and autopurge inactive accounts?
@@ -51,25 +51,25 @@ if (isAutopurgeInactiveEnabled()) {
        // Init SQLs
        initSqls();
 
-       // Init exclusion list
-       // @TODO Rewrite these if() blocks to a filter
-       $EXCLUDE_LIST = '';
-       if (isValidUserId(getDefRefid())) $EXCLUDE_LIST .= ' AND `userid` != {?def_refid?}';
-
-       // Check for more extensions
-       if (isExtensionActive('beg'))     $EXCLUDE_LIST .= ' AND `userid` != {?beg_userid?}';
-       if (isExtensionActive('bonus'))   $EXCLUDE_LIST .= ' AND `userid` != {?bonus_userid?}';
-       if (isExtensionActive('doubler')) $EXCLUDE_LIST .= ' AND `userid` != {?doubler_userid?}';
+       // Init userid exclusion
+       $excludeSql = '';
+       $excludedUserids = runFilterChain('config_userid_exclusion_sql', array());
+       if (isFilledArray($excludedUserids)) {
+               // Exclude all
+               $excludeSql = ' AND `userid` NOT IN (' . implode(', ', $excludedUserids) . ')';
+       } // END - if
 
        // Check for new holiday extension
        if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
                // Include only users with no active holiday
-               $EXCLUDE_LIST .= " AND `holiday_active`='N'";
+               $excludeSql .= " AND `holiday_active`='N'";
        } // END - if
 
        // Check for all accounts
-       $result_inactive = SQL_QUERY("SELECT
-       `userid`,`email`,`last_online`
+       $result_inactive = sqlQuery("SELECT
+       `userid`,
+       `email`,
+       `last_online`
 FROM
        `{?_MYSQL_PREFIX?}_user_data`
 WHERE
@@ -77,23 +77,23 @@ WHERE
        (UNIX_TIMESTAMP() - `joined`) >= {?ap_inactive_since?} AND
        (UNIX_TIMESTAMP() - `last_online`) >= {?ap_inactive_since?} AND
        (UNIX_TIMESTAMP() - `ap_notified`) >= {?ap_inactive_since?}
-       " . $EXCLUDE_LIST . "
+       " . runFilterChain('user_exclusion_sql', $excludeSql) . "
 ORDER BY
        `userid` ASC", __FILE__, __LINE__);
 
-       if (!SQL_HASZERONUMS($result_inactive)) {
+       if (!ifSqlHasZeroNums($result_inactive)) {
                // Prepare variables and constants...
-               $useridsContent = '';
+               $userids = array();
                $content['since'] = (getApInactiveSince() / 60 / 60);
                $content['time']  = (getApInactiveTime()  / 60 / 60);
 
                // Mark found accounts as inactive and send an email
-               while ($row = SQL_FETCHARRAY($result_inactive)) {
+               while ($row = sqlFetchArray($result_inactive)) {
                        // Merge both arrays
                        $content = merge_array($content, $row);
 
                        // Remember userids for the admin
-                       $useridsContent .= $content['userid'] . ', ';
+                       array_push($userids, $content['userid']);
 
                        // Get date/time from timestamp
                        $content['last_online'] = generateDateTime($content['last_online'], 0);
@@ -103,43 +103,49 @@ ORDER BY
                        sendEmail($content['userid'], '{--MEMBER_AUTOPURGE_INACTIVE_SUBJECT--}', $message);
 
                        // Update this account
-                       addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `ap_notified`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
-                               array(bigintval($content['userid'])), __FILE__, __LINE__, false));
+                       addSql(sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `ap_notified`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
+                               array(bigintval($content['userid'])), __FILE__, __LINE__, FALSE));
                } // END - while
 
                // Remove last comma
-               $useridsContent = str_replace(', ', "\n", substr($useridsContent, 0, -2));
+               $content['userids'] = implode(PHP_EOL, $userids);
 
                // Send mail notification to admin
-               sendAdminNotification('{--ADMIN_AUTOPURGE_INACTIVE_SUBJECT--}', 'admin_autopurge_inactive', $useridsContent);
+               sendAdminNotification('{--ADMIN_AUTOPURGE_INACTIVE_SUBJECT--}', 'admin_purge_inactive', $content);
        } // END - if
 
        // Free memory
-       SQL_FREERESULT($result_inactive);
-
-       // Now let's have a look for inactive accounts we want to delete we newly use the same exclude list
-       // here for e.g. excluding holiday users
-       $result_inactive = SQL_QUERY("SELECT
-       `userid`,`email`,`last_online`
+       sqlFreeResult($result_inactive);
+
+       /*
+        * Now let's have a look for inactive accounts for deletion. Use the newly
+        * added exclude list here for e.g. excluding holiday users, test users et
+        * cetera.
+        */
+       $result_inactive = sqlQuery("SELECT
+       `userid`,
+       `email`,
+       `last_online`
 FROM
        `{?_MYSQL_PREFIX?}_user_data`
 WHERE
-       `status`='CONFIRMED' AND
+       `status`='CONFIRMED'
+       " . runFilterChain('user_exclusion_sql', $excludeSql) . " AND
        (UNIX_TIMESTAMP() - `joined`) >= {?ap_inactive_since?} AND
        (UNIX_TIMESTAMP() - `last_online`) >= {?ap_inactive_since?} AND
-       (UNIX_TIMESTAMP() - `ap_notified`) >= {?ap_inactive_time?}
-" . $EXCLUDE_LIST . "
+       (UNIX_TIMESTAMP() - `ap_notified`) >= {?ap_inactive_time?} AND
+       `ap_notified` > 0
 ORDER BY
        `userid` ASC", __FILE__, __LINE__);
 
-       if (!SQL_HASZERONUMS($result_inactive)) {
+       if (!ifSqlHasZeroNums($result_inactive)) {
                // Prepare userid array for admin...
                $userids = array();
 
                // Delete inactive accounts
-               while ($content = SQL_FETCHARRAY($result_inactive)) {
+               while ($content = sqlFetchArray($result_inactive)) {
                        // Remember userids for the admin
-                       $useridsContent[] = $content['userid'];
+                       array_push($userids, $content['userid']);
 
                        // Get date/time from timestamp
                        $content['last_online'] = generateDateTime($content['last_online'], 0);
@@ -149,16 +155,16 @@ ORDER BY
                } // END - while
 
                // Display all userids
-               $useridsContent = implode("\n", $userids);
+               $useridsContent = implode(PHP_EOL, $userids);
 
                // Send mail notification to admin
                if (getConfig('ap_in_notify') == 'Y') {
-                       sendAdminNotification('{--ADMIN_AUTOPURGE_DELETE_SUBJECT--}', 'admin_autopurge_delete', $useridsContent);
+                       sendAdminNotification('{--ADMIN_AUTOPURGE_DELETE_SUBJECT--}', 'admin_purge_delete', $useridsContent);
                } // END - if
        } // END - if
 
        // Free memory
-       SQL_FREERESULT($result_inactive);
+       sqlFreeResult($result_inactive);
 
        // Run all SQLs
        runFilterChain('run_sqls');