More SQL rewrites, TODO: Put all table and column names in backticks (`)
[mailer.git] / inc / modules / admin / what-list_user.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 09/28/2003 *
4  * ===============                              Last change: 08/18/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-list_user.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : List all or partial members                      *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Alle oder nur bestimmte Mitglieder auflisten     *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if ((!defined('__SECURITY')) || (!IS_ADMIN())) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 } elseif ((!EXT_IS_ACTIVE("user")) || (GET_EXT_VERSION("user") == "")) {
39         // Missing extension!
40         ADD_FATAL(sprintf(EXTENSION_PROBLEM_NOT_INSTALLED, "user"));
41         return;
42 }
43
44 // Add description as navigation point
45 ADD_DESCR("admin", __FILE__);
46
47 // Init title with "all accounts"
48 $listHeader = ADMIN_ALL_ACCOUNTS;
49 if (!empty($_GET['status'])) {
50         // Set title according to the "status"
51         $listHeader = constant(sprintf("ADMIN_LIST_STATUS_%s_ACCOUNTS", strtoupper(SQL_ESCAPE($_GET['status']))));
52 } elseif (!empty($_GET['mode'])) {
53         // Set title according to the "mode"
54         $listHeader = constant(sprintf("ADMIN_LIST_MODE_%s_ACCOUNTS", strtoupper(SQL_ESCAPE($_GET['mode']))));
55 }
56
57 // Remember it
58 define('__TITLE', $listHeader);
59
60 // Init variables
61 $MORE = ", userid"; $colspan = "4";
62
63 // Add nickname if extension is found&active
64 if (EXT_IS_ACTIVE("nickname")) { $MORE = ", nickname"; }
65
66 // Add random confirmed if extension version matches
67 if (GET_EXT_VERSION("user") >= "0.3.4") {
68         // Add it...
69         $MORE .= ", rand_confirmed";
70
71         // Add lock reason?
72         if (GET_EXT_VERSION("user") >= "0.3.5") {
73                 // Add them...
74                 $MORE .= ", lock_reason, UNIX_TIMESTAMP(`lock_timestamp`) AS lock_timestamp";
75         } // END - if
76 } // END - if
77
78 // Is the extension "country" installed?
79 if (EXT_IS_ACTIVE("country")) {
80         // Add country code
81         $MORE .= ", country_code";
82 } // END - if
83
84 // Init unset data (bad that we change $_GET here!)
85 if (empty($_GET['letter'])) { $_GET['letter'] = _ALL2;    }
86 if (empty($_GET['sortby'])) { $_GET['sortby'] = "userid"; }
87 if (empty($_GET['page']))   { $_GET['page']   = "1";      }
88
89 // Set base URL
90 $BASE = "[<A href=\"".URL."/modules.php?module=admin";
91
92 if (!empty($_GET['u_id'])) {
93         // Secure the user ID
94         $uid = bigintval($_GET['u_id']);
95
96         // Does the account exists?
97         $result_user = SQL_QUERY_ESC("SELECT gender, surname, family, street_nr, zip, city, country, email, birth_day, birth_month, birth_year, max_mails, receive_mails, refid, status, REMOTE_ADDR, last_online, last_module, ref_clicks, total_logins, used_points, emails_sent, joined, last_update, last_profile_sent, notified, ref_payout, emails_received, mails_confirmed".$MORE."
98 FROM "._MYSQL_PREFIX."_user_data
99 WHERE userid=%s
100 LIMIT 1",
101                 array($uid), __FILE__, __LINE__);
102         if (SQL_NUMROWS($result_user) == 1) {
103                 // Account found!
104                 $DATA  = SQL_FETCHARRAY($result_user);
105
106                 // Get count/sum of refs, selected categories, unconfirmed mails
107                 $REFS  = GET_TOTAL_DATA($uid, "refsystem", "counter");
108                 $CATS  = GET_TOTAL_DATA($uid, "user_cats", "id", "userid", true);
109                 $LINKS = GET_TOTAL_DATA($uid, "user_links", "id", "userid", true);
110
111                 // Add links to the numbers
112                 if ($LINKS > 0) $LINKS = $BASE."&amp;what=list_links&amp;u_id=".$uid."\">".$LINKS."</A>]";
113                 if ($DATA['refid'] > 0) $DATA['refid'] = $BASE."&amp;what=list_user&amp;u_id=".$DATA['refid']."\">".$DATA['refid']."</A>]";
114                 if (empty($DATA['last_module'])) $DATA['last_module'] = "---";
115                 if ($REFS > 0) $REFS = $BASE."&amp;what=list_refs&amp;u_id=".$uid."\">".$REFS."</A>]";
116                 if ($CATS > 0) $CATS = $BASE."&amp;what=list_cats&amp;u_id=".$uid."\">".$CATS."</A>]";
117
118                 // Prepare data for template
119                 define('_BIRTHDAY', MAKE_DATETIME(mktime(0, 0, 0, $DATA['birth_month'], $DATA['birth_day'], $DATA['birth_year']), "3"));
120                 define('_REFS' , $REFS);
121                 define('_CATS' , $CATS);
122                 define('_LINKS', $LINKS);
123                 define('_ADMIN_LINKS', MEMBER_ACTION_LINKS($uid, $DATA['status']));
124                 $DATA['gender']            = TRANSLATE_GENDER($DATA['gender']);
125                 $DATA['email_link']        = CREATE_EMAIL_LINK($DATA['email'], "user_data");
126                 $DATA['status']            = TRANSLATE_STATUS($DATA['status']);
127                 $DATA['last_online']       = MAKE_DATETIME($DATA['last_online'], "0");
128                 $DATA['used_points']       = TRANSLATE_COMMA($DATA['used_points']);
129                 if ($DATA['emails_sent'] > 0) $DATA['emails_sent'] = $BASE."&amp;what=email_details&amp;u_id=".$uid."\">".TRANSLATE_COMMA($DATA['emails_sent'])."</A>]";
130                 $DATA['joined']            = MAKE_DATETIME($DATA['joined'], "0");
131                 $DATA['last_update']       = MAKE_DATETIME($DATA['last_update'], "0");
132                 $DATA['last_profile_sent'] = MAKE_DATETIME($DATA['last_profile_sent'], "0");
133                 $DATA['total']             = TRANSLATE_COMMA(GET_TOTAL_DATA($uid, "user_points", "points"));
134                 $DATA['locked']            = TRANSLATE_COMMA(GET_TOTAL_DATA($uid, "user_points", "locked_points"));
135                 $DATA['lock_timestamp']    = MAKE_DATETIME($DATA['lock_timestamp'], "2");
136
137                 // Is the lock reason not set?
138                 if (!isset($DATA['lock_reason'])) $DATA['lock_reason'] = "---";
139
140                 // Nickname inclusion?
141                 if (EXT_IS_ACTIVE("nickname")) {
142                         // Nickname not set or invalid? Then 
143                         if ((empty($DATA['nickname'])) || ($DATA['nickname'] == $uid)) $DATA['nickname'] = "---";
144                 } else {
145                         // Extension not found
146                         $DATA['nickname'] = EXT_NICKNAME_404;
147                 }
148
149                 // Is the user extension newer?
150                 if (GET_EXT_VERSION("user") >= "0.3.4") {
151                         // Then "translate" the number
152                         $DATA['rand_confirmed'] = TRANSLATE_COMMA($DATA['rand_confirmed']);
153                 } // END - if
154
155                 // Clickrate
156                 $DATA['click_rate'] = 0;
157                 if ($DATA['emails_received'] > 0) {
158                         $DATA['click_rate'] = TRANSLATE_COMMA($DATA['mails_confirmed'] / $DATA['emails_received'] * 100);
159                 } // END - if
160
161                 // "Translate" more data
162                 $DATA['mails_confirmed'] = TRANSLATE_COMMA($DATA['mails_confirmed']);
163                 $DATA['emails_received'] = TRANSLATE_COMMA($DATA['emails_received']);
164
165                 // Is the extension "country" installed?
166                 if (EXT_IS_ACTIVE("country")) {
167                         // Then overwrite country information
168                         $DATA['country'] = COUNTRY_GENERATE_INFO($DATA['country_code']);
169                 } elseif ($DATA['country'] == 0) {
170                         // Zero ID???
171                         $DATA['country'] = "???";
172                 }
173
174                 // Load user-details template
175                 LOAD_TEMPLATE("admin_user_details", false, $uid);
176         } else {
177                 // Account does not exist!
178                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_MEMBER_404_1.$uid.ADMIN_MEMBER_404_2);
179         }
180
181         // Free the result
182         SQL_FREERESULT($result_user);
183 } else {
184         $whereStatement = "";
185         if (($_GET['letter'] != _ALL2) && ($_GET['letter'] != _OTHERS) && (!empty($_GET['letter']))) {
186                 // List only persons w
187                 $whereStatement = " WHERE family LIKE '".$_GET['letter']."%'";
188         } // END - if
189         if ($_GET['sortby'] == "family_name") $_GET['sortby'] = "family";
190
191         // Parse the status or mode parameter
192         if (isset($_GET['status'])) {
193                 // Is a WHERE statement already there?
194                 if (!empty($whereStatement)) {
195                         // Then append the status column
196                         $whereStatement .= sprintf(" AND status='%s'", SQL_ESCAPE(strip_tags(strtoupper($_GET['status']))));
197                 } else {
198                         // Start a new one
199                         $whereStatement = sprintf(" WHERE status='%s'", SQL_ESCAPE(strip_tags(strtoupper($_GET['status']))));
200                 }
201         } elseif (isset($_GET['mode'])) {
202                 // Choose what we need to list
203                 switch ($_GET['mode']) {
204                         case "norefs": // Users w/o refs
205                                 if (!empty($whereStatement)) {
206                                         // Add AND statement
207                                         $whereStatement .= " AND refid=0";
208                                 } else {
209                                         // Add WHERE statement
210                                         $whereStatement = " WHERE refid=0";
211                                 }
212                                 break;
213
214                         default: // Invalid list mode
215                                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Invalid list mode %s detected.", SQL_ESCAPE($_GET['mode'])));
216                                 break;
217                 }
218         } // END = if
219
220         // Prepare SQL and run it
221         $SQL = "SELECT userid, gender, surname, family, email, REMOTE_ADDR, refid, status, emails_sent, mails_confirmed, emails_received".$MORE." FROM "._MYSQL_PREFIX."_user_data".$whereStatement." ORDER BY ".SQL_ESCAPE($_GET['sortby']);
222         $result_master = SQL_QUERY($SQL, __FILE__, __LINE__);
223
224         // Calculate page count (0.5 fixes a bug with page count)
225         if ($_CONFIG['user_limit'] == 0) {
226                 $_CONFIG['user_limit'] = 100;
227                 LOAD_TEMPLATE("admin_settings_saved", false, EXTENSION_WARNING_USER_LIMIT);
228         } // END - if
229
230         // Activate the extension please!
231         $PAGES = round(SQL_NUMROWS($result_master) / $_CONFIG['user_limit'] + 0.5);
232
233         if (empty($_GET['page']))   $_GET['page']   = "1";
234         if (empty($_GET['offset'])) $_GET['offset'] = $_CONFIG['user_limit'];
235
236         // Add limitation to SQL string and run him again
237         $SQL .= " LIMIT ".($_GET['offset'] * $_GET['page'] - $_GET['offset']).", ".$_GET['offset'];
238         $result = SQL_QUERY($SQL, __FILE__, __LINE__);
239
240         $result_user = SQL_QUERY("SELECT emails_sent FROM `"._MYSQL_PREFIX."_user_data` WHERE status='CONFIRMED'", __FILE__, __LINE__);
241         $user_count = SQL_NUMROWS($result_user);
242         SQL_FREERESULT($result_user);
243
244         if (SQL_NUMROWS($result_master) > 0) {
245                 // Free memory
246                 SQL_FREERESULT($result_master);
247
248                 // We have some (new?) registrations!
249                 define('__COLSPAN1'  , $colspan);
250                 define('__COLSPAN2'  , ($colspan + 2));
251                 define('__USER_CNT'  , $user_count);
252
253                 if ((function_exists('alpha')) && (function_exists('SortLinks'))) {
254                         define('__ALPHA_SORT', alpha($_GET['sortby'], $colspan, true));
255                         define('__SORT_LINKS', SortLinks($_GET['letter'], $_GET['sortby'], $colspan, true));
256                 } else {
257                         define('__ALPHA_SORT', "");
258                         define('__SORT_LINKS', "");
259                 }
260
261                 if ($PAGES > 1) {
262                         define('__PAGE_NAV', ADD_PAGENAV($PAGES, $_CONFIG['user_limit'], true, $colspan, true));
263                 } else {
264                         // No page navigation is required
265                         define('__PAGE_NAV', "");
266                 }
267
268                 // Column with nickname when nickname extension is present
269                 if (EXT_IS_ACTIVE("nickname"))
270                 {
271                         // Nickname extension found
272                         define('__NICKNAME_TH', "  <TD class=\"admin_title bottom2\" align=\"center\">".NICKNAME."</TD>");
273                 }
274                  else
275                 {
276                         // Not found
277                         define('__NICKNAME_TH', "");
278                 }
279
280                 // Load all users
281                 $SW = 2; $OUT = "";
282                 while ($content = SQL_FETCHARRAY($result)) {
283                         // Set refid link
284                         if ($content['refid'] > 0) $content['refid'] = ADMIN_USER_PROFILE_LINK($content['refid']);
285
286                         // Get number of unconfirmed mails
287                         $LINKS = GET_TOTAL_DATA($content['userid'], "user_links", "id", "userid", true);
288                         if ($LINKS > 0) $LINKS = $BASE."&amp;what=list_links&amp;u_id=".$content['userid']."\">".TRANSLATE_COMMA($LINKS)."</A>]";
289
290                         // Set link to sent mails if present
291                         if ($content['emails_sent'] > 0) $content['emails_sent'] = $BASE."&amp;what=email_details&amp;u_id=".$content['userid']."\">".TRANSLATE_COMMA($content['emails_sent'])."</A>]";
292
293                         // Add nickname
294                         if (empty($content['nickname']) || $content['nickname'] == $content['userid']) $content['nickname'] = "---";
295
296                         // Calculate total points
297                         $pointsTotal = GET_TOTAL_DATA($content['userid'], "user_points", "points")  - GET_TOTAL_DATA($content['userid'], "user_data", "used_points");
298
299                         // Clickrate
300                         $clickRate = 0;
301                         if ($content['emails_received'] > 0) {
302                                 $clickRate = $content['mails_confirmed'] / $content['emails_received'] * 100;
303                         } // END - if
304
305                         // Transfer data to array
306                         $content['sw']     = $SW;
307                         $content['uid']    = ADMIN_USER_PROFILE_LINK($content['userid']);
308                         $content['gender'] = TRANSLATE_GENDER($content['gender']);
309                         $content['email']  = "[<A href=\"".CREATE_EMAIL_LINK($content['email'], "user_data")."\">".$content['email']."</A>]";
310                         $content['addr']   = $content['REMOTE_ADDR'];
311                         $content['links']  = $LINKS;
312                         $content['alinks'] = MEMBER_ACTION_LINKS($content['userid'], $content['status']);
313                         $content['points'] = TRANSLATE_COMMA($pointsTotal);
314                         $content['rate']   = TRANSLATE_COMMA($clickRate);
315                         $content['locked'] = TRANSLATE_COMMA(GET_TOTAL_DATA($content['userid'], "user_points", "locked_points"));
316                         $content['lock_timestamp'] = MAKE_DATETIME($content['lock_timestamp'], "2");
317                         $content['status'] = TRANSLATE_STATUS($content['status']);
318
319                         // Is the lock reason not set?
320                         if (!isset($content['lock_reason'])) $content['lock_reason'] = "---";
321
322                         // Is the extension "country" installed?
323                         if (EXT_IS_ACTIVE("country")) {
324                                 // Then overwrite country information
325                                 $content['country'] = COUNTRY_GENERATE_INFO($content['country_code']);
326                         } elseif ($content['country'] == 0) {
327                                 // Zero ID???
328                                 $content['country'] = "???";
329                         }
330
331                         // Load row template and switch colors
332                         $OUT .= LOAD_TEMPLATE("admin_list_user_row", true, $content);
333                         $SW = 3 - $SW;
334                 } // END - while
335
336                 // Free memory
337                 SQL_FREERESULT($result);
338
339                 define('__USER_ROWS', $OUT);
340
341                 // Load main template
342                 LOAD_TEMPLATE("admin_list_user");
343
344                 // Free some memory
345                 SQL_FREERESULT($result_master);
346         } else {
347                 // No one as registered so far! :-(
348                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_NO_NONE_REGISTERED);
349         }
350 }
351
352 //
353 ?>