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