c4200c8fbc2ed4a1cf1d632eb72a708c7cf8aa0d
[mailer.git] / inc / fix_refsystem.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 07/13/2011 *
4  * ===================                          Last change: 07/13/2011 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : fix_refsystem.php                                *
8  * -------------------------------------------------------------------- *
9  * Short description : Generate refback table including all refs        *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Refback-Tabelle mit allen Refs generieren        *
12  * -------------------------------------------------------------------- *
13  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
14  * Copyright (c) 2009 - 2015 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 } // END - if
37
38 // Sanity-check: Is there ref level 0? If so, this must be fixed to NULL and `level` must be increased by one
39 $result = sqlQuery('SELECT `id` FROM `{?_MYSQL_PREFIX?}_refsystem` WHERE `level`=0', __FILE__, __LINE__);
40 if (!ifSqlHasZeroNumRows($result)) {
41         // Remove index temporaly
42         sqlQueryAlterTable('ALTER TABLE `{?_MYSQL_PREFIX?}_refsystem` DROP INDEX `userid_level`', __FILE__, __LINE__);
43
44         // Upgrade refsystem
45         sqlQuery('UPDATE `{?_MYSQL_PREFIX?}_refsystem` SET `level`=`level`+1', __FILE__, __LINE__);
46
47         // Add it again
48         sqlQueryAlterTable('ALTER TABLE `{?_MYSQL_PREFIX?}_refsystem` ADD UNIQUE INDEX `userid_level` (`userid`, `level`)', __FILE__, __LINE__);
49
50         // Rebuild cache
51         rebuildCache('refsystem', 'refsystem');
52 } // END - if
53
54 // Free result
55 sqlFreeResult($result);
56
57 // Reset the table
58 sqlQuery('TRUNCATE TABLE `{?_MYSQL_PREFIX?}_user_refs`', __FILE__, __LINE__);
59
60 // Begin with all users in level one
61 $result_direct = sqlQuery('SELECT
62         `userid`
63 FROM
64         `{?_MYSQL_PREFIX?}_user_data`
65 WHERE
66         `refid`=0 OR
67         `refid` IS NULL
68 ORDER BY
69         `userid` ASC', __FILE__, __LINE__);
70
71 // Are there entries?
72 if (!ifSqlHasZeroNumRows($result_direct)) {
73         // When 'walk' through all users
74         while ($content = sqlFetchArray($result_direct)) {
75                 // Update refback table
76                 /* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'userid=' . $content['userid'] . ' - BEFORE!');
77                 updateReferralTable($content['userid']);
78                 /* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'userid=' . $content['userid'] . ' - AFTER!');
79         } // END - while
80 } // END - if
81
82 // Free result
83 sqlFreeResult($result_direct);
84
85 // [EOF]
86 ?>