First batch of removal of the headers needed for revision-functions.php
[mailer.git] / inc / daily / daily_user.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 04/20/2004 *
4  * ===================                          Last change: 06/20/2010 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : daily_daily.php                                  *
8  * -------------------------------------------------------------------- *
9  * Short description : Things to do on daily reset                      *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Dinge, die beim taeglichen Reset erledigt werden *
12  * -------------------------------------------------------------------- *
13  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
14  * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
15  * For more information visit: http://mxchange.org                      *
16  *                                                                      *
17  * This program is free software; you can redistribute it and/or modify *
18  * it under the terms of the GNU General Public License as published by *
19  * the Free Software Foundation; either version 2 of the License, or    *
20  * (at your option) any later version.                                  *
21  *                                                                      *
22  * This program is distributed in the hope that it will be useful,      *
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
25  * GNU General Public License for more details.                         *
26  *                                                                      *
27  * You should have received a copy of the GNU General Public License    *
28  * along with this program; if not, write to the Free Software          *
29  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
30  * MA  02110-1301  USA                                                  *
31  ************************************************************************/
32
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
35         die();
36 } elseif ((!isHtmlOutputMode()) || (!isDailyResetEnabled())) {
37         // Do not execute when script is in non-HTML mode or no daily reset
38         return;
39 } elseif ((!isExtensionActive('sql_patches')) || (!isExtensionActive('user'))) {
40         if (isDebugModeEnabled()) logDebugMessage(__FILE__, __LINE__, 'Not resetting, needed extension(s) ext-sql_patches/ext-user disabled.');
41         return;
42 }
43
44 // Debug line
45 //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'Daily reset started.');
46
47 // Update user profiles
48 if (isExtensionInstalledAndNewer('order', '0.1.1')) {
49         // Latest version
50         sqlQuery('UPDATE
51         `{?_MYSQL_PREFIX?}_user_data`
52 SET
53         `receive_mails`=`max_mails`,
54         `mail_orders`=0
55 WHERE
56         `receive_mails` != `max_mails`
57         ' . runFilterChain('user_exclusion_sql', ' ') . '', __FILE__, __LINE__);
58 } else {
59         // Obsolete version
60         sqlQuery('UPDATE
61         `{?_MYSQL_PREFIX?}_user_data`
62 SET
63         `receive_mails`=`max_mails`
64 WHERE
65         `receive_mails` != `max_mails`
66         ' . runFilterChain('user_exclusion_sql', ' ') . '', __FILE__, __LINE__);
67 }
68
69 // Transfer points from locked_points to points
70 $result_daily = sqlQuery("SELECT
71         `userid`
72 FROM
73         `{?_MYSQL_PREFIX?}_user_data`
74 WHERE
75         `ref_payout`=0 AND
76         `status`='CONFIRMED'
77         " . runFilterChain('user_exclusion_sql', ' ') . "
78 ORDER BY
79         `userid` ASC", __FILE__, __LINE__);
80
81 //* DEBUG: */ debugOutput(basename(__FILE__) . ':payout=0;daily|numRows=' . sqlNumRows($result_daily));
82 if (!ifSqlHasZeroNums($result_daily)) {
83         // Init SQLs
84         initSqls();
85
86         // Run through all 'locked_(foo)_points columsn
87         foreach (runFilterChain('locked_points_columns_array', array('points')) as $columnName) {
88                 // Start checking accounts which are on 0 confirmed-to-go mails
89                 while ($content = sqlFetchArray($result_daily)) {
90                         //* DEBUG: */ debugOutput(basename(__FILE__) . ':' . $content['userid']);
91                         $result_points = sqlQueryEscaped("SELECT
92         `ref_depth`,
93         `locked_%s`
94 FROM
95         `{?_MYSQL_PREFIX?}_user_points`
96 WHERE
97         `userid`=%s AND
98         `locked_%s` > 0.00000
99 ORDER BY
100         `ref_depth` ASC",
101                                 array(
102                                         $columnName,
103                                         bigintval($content['userid']),
104                                         $columnName
105                                 ), __FILE__, __LINE__);
106
107                         //* DEBUG: */ debugOutput(basename(__FILE__) . ':payout=0;points|numRows=' . sqlNumRows($result_points));
108                         if (!ifSqlHasZeroNums($result_points)) {
109                                 // Ok transfer points
110                                 while ($content2 = sqlFetchArray($result_points)) {
111                                         // Merge both arrays
112                                         $content = merge_array($content, $content2);
113
114                                         //* DEBUG: */ debugOutput(basename(__FILE__) . ':userid=' . $content['userid'].',depth='.$content['ref_depth'].',columnName=' . $columnName . ',locked='.$content['locked_' . $columnName]);
115                                         if ($content['ref_depth'] > 0) {
116                                                 // Level 1+
117                                                 addSql(sqlQueryEscaped("UPDATE
118         `{?_MYSQL_PREFIX?}_user_points`
119 SET
120         `%s`=`%s`+%s,
121         `locked_%s`=0.00000
122 WHERE
123         `userid`=%s AND
124         `ref_depth`=%s
125 LIMIT 1",
126                                                         array(
127                                                                 $columnName,
128                                                                 $columnName,
129                                                                 $content['locked_' . $columnName],
130                                                                 $columnName,
131                                                                 bigintval($content['userid']),
132                                                         $content['ref_depth']
133                                                         ), __FILE__, __LINE__, FALSE)
134                                                 );
135                                         } else {
136                                                 // Level zero
137                                                 addSql(sqlQueryEscaped("UPDATE
138         `{?_MYSQL_PREFIX?}_user_points`
139 SET
140         `%s`=`%s`+%s,
141         `locked_%s`=0.00000
142 WHERE
143         `userid`=%s AND
144         `ref_depth` IS NULL
145 LIMIT 1",
146                                                         array(
147                                                                 $columnName,
148                                                                 $columnName,
149                                                                 $content['locked_' . $columnName],
150                                                                 $columnName,
151                                                                 bigintval($content['userid'])
152                                                         ), __FILE__, __LINE__, FALSE)
153                                                 );
154                                         }
155
156                                         // Update mediadata as well
157                                         if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
158                                                 // Update database
159                                                 updateMediadataEntry(array('total_points'), 'add', $content['locked_' . $columnName]);
160                                         } // END - if
161                                 } // END - while
162                         } // END - if
163
164                         // Free memory
165                         sqlFreeResult($result_points);
166                 } // END - while
167         } // END - foreach
168
169         // Run all SQLs
170         runFilterChain('run_sqls');
171 } // END - if
172
173 // Free memory
174 sqlFreeResult($result_daily);
175
176 // Debug line
177 //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'Daily reset ended.');
178
179 // [EOF]
180 ?>