53bfce2af9f2f6f2795b85565f41f82a872125cb
[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 - 2009 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')) || (!isAdmin())) {
41         die();
42 } // END - if
43
44 // Add description as navigation point
45 addMenuDescription('admin', __FILE__);
46
47 if ((!isExtensionInstalled('user')) || (!isExtensionActive('user'))) {
48         // Missing extension!
49         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('user'));
50         return;
51 } // END - if
52
53 // Init variables
54 $MORE = '';
55 $colspan = 4;
56
57 // Add nickname if extension is found&active
58 // @TODO Rewrite these if-blocks in a filter
59 if (isExtensionActive('nickname')) {
60         // Add nickname
61         $MORE .= ", `nickname`";
62 } else {
63         $MORE .= ", `userid`";
64 }
65
66 // Add random confirmed if extension version matches
67 if (getExtensionVersion('user') >= '0.3.4') {
68         // Add it...
69         $MORE .= ", `rand_confirmed`";
70
71         // Add lock reason?
72         if (getExtensionVersion('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 (isExtensionActive('country')) {
80         // Add country code
81         $MORE .= ", `country_code`";
82 } else {
83         // Add direct value
84         $MORE .= ", `country`";
85 }
86
87 // Init unset data
88 if (!isGetRequestElementSet('letter')) { setRequestGetElement('letter', getMessage('_ALL2')); }
89 if (!isGetRequestElementSet('sortby')) { setRequestGetElement('sortby', 'userid');            }
90 if (!isGetRequestElementSet('page'))   { setRequestGetElement('page'  , 1);                 }
91
92 // Set base URL
93 $base = '[<a href="{?URL?}/modules.php?module=admin';
94
95 if (isGetRequestElementSet('userid')) {
96         // Secure the user id
97         $userid = bigintval(getRequestElement('userid'));
98
99         // Does the account exists?
100         $result_user = SQL_QUERY_ESC("SELECT
101         `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."
102 FROM
103         `{?_MYSQL_PREFIX?}_user_data`
104 WHERE
105         `userid`=%s
106 LIMIT 1",
107                 array($userid), __FILE__, __LINE__);
108
109         // User found?
110         if (SQL_NUMROWS($result_user) == 1) {
111                 // Account found!
112                 $content  = SQL_FETCHARRAY($result_user);
113
114                 // Get count/sum of refs, selected categories, unconfirmed mails
115                 $content['refs']  = countSumTotalData($userid, 'refsystem' , 'counter');
116                 $content['cats']  = countSumTotalData($userid, 'user_cats' , 'id', 'userid', true);
117                 $content['links'] = countSumTotalData($userid, 'user_links', 'id', 'userid', true);
118
119                 // Add links to the numbers
120                 if ($content['links'] > 0) $content['links'] = $base . '&amp;what=list_links&amp;userid=' . $userid . '">' . $content['links'] . '</a>]';
121                 if ($content['refid'] > 0) $content['refid'] = $base . '&amp;what=list_user&amp;userid=' . $content['refid'] . '">'.$content['refid'] . '</a>]';
122                 if ($content['refs']  > 0) $content['refs']  = $base . '&amp;what=list_refs&amp;userid=' . $userid . '">' . $content['refs'] . '</a>]';
123                 if ($content['cats']  > 0) $content['cats']  = $base . '&amp;what=list_cats&amp;userid=' . $userid . '">' . $content['cats'] . '</a>]';
124
125                 // Fix empty module
126                 if (empty($content['last_module'])) $content['last_module'] = '---';
127
128                 // Calculate timestamp for birthday
129                 $stamp = mktime(0, 0, 0, $content['birth_month'], $content['birth_day'], $content['birth_year']);
130
131                 // Is this above zero?
132                 if ($stamp > 0) {
133                         // Then use it
134                         $content['birthday'] = generateDateTime($stamp, 3);
135                 } else {
136                         // Zero or below so set zero!
137                         $content['birthday'] = generateDateTime(0, 3);
138                 }
139
140                 // Prepare data for template
141                 $content['admin_links']       = generateMemberAdminActionLinks($userid, $content['status']);
142                 $content['gender']            = translateGender($content['gender']);
143                 $content['email_link']        = generateEmailLink($content['email'], 'user_data');
144                 $content['status']            = translateUserStatus($content['status']);
145                 $content['last_online']       = generateDateTime($content['last_online'], 0);
146                 $content['used_points']       = translateComma($content['used_points']);
147                 if ($content['emails_sent'] > 0) $content['emails_sent'] = $base . '&amp;what=email_details&amp;userid=' . $userid . '">' . translateComma($content['emails_sent']) . '</a>]';
148                 $content['joined']            = generateDateTime($content['joined'], 0);
149                 $content['last_update']       = generateDateTime($content['last_update'], 0);
150                 $content['last_profile_sent'] = generateDateTime($content['last_profile_sent'], 0);
151                 $content['total']             = translateComma(countSumTotalData($userid, 'user_points', 'points'));
152                 $content['locked']            = translateComma(countSumTotalData($userid, 'user_points', 'locked_points'));
153                 $content['lock_timestamp']    = generateDateTime($content['lock_timestamp'], 2);
154
155                 // Is the lock reason not set?
156                 if (!isset($content['lock_reason'])) $content['lock_reason'] = '---';
157
158                 // Nickname inclusion?
159                 // @TODO Rewrite these to filters
160                 if (isExtensionActive('nickname')) {
161                         // Nickname not set or invalid? Then
162                         if ((empty($content['nickname'])) || ($content['nickname'] == $userid)) $content['nickname'] = '---';
163                 } else {
164                         // Extension not found
165                         $content['nickname'] = getMessage('EXT_NICKNAME_404');
166                 }
167
168                 // Is the user extension newer?
169                 if (getExtensionVersion('user') >= '0.3.4') {
170                         // Then "translate" the number
171                         $content['rand_confirmed'] = translateComma($content['rand_confirmed']);
172                 } // END - if
173
174                 // Clickrate
175                 $content['click_rate'] = 0;
176                 if ($content['emails_received'] > 0) {
177                         $content['click_rate'] = translateComma($content['mails_confirmed'] / $content['emails_received'] * 100);
178                 } // END - if
179
180                 // "Translate" more data
181                 $content['mails_confirmed'] = translateComma($content['mails_confirmed']);
182                 $content['emails_received'] = translateComma($content['emails_received']);
183
184                 // Is the extension 'country' installed?
185                 if (isExtensionActive('country')) {
186                         // Then overwrite country information
187                         $content['country'] = generateCountryInfo($content['country_code']);
188                 } elseif ($content['country'] == 0) {
189                         // Zero id???
190                         $content['country'] = '???';
191                 }
192
193                 // Add userid
194                 $content['userid'] = $userid;
195
196                 // Load user-details template
197                 loadTemplate('admin_user_details', false, $content);
198         } else {
199                 // Account does not exist!
200                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_MEMBER_404'), $userid));
201         }
202
203         // Free the result
204         SQL_FREERESULT($result_user);
205 } else {
206         $whereStatement = '';
207         if ((getRequestElement('letter') != getMessage('_ALL2')) && (getRequestElement('letter') != getMessage('_OTHERS')) && (isGetRequestElementSet('letter'))) {
208                 // List only persons w
209                 $whereStatement = " WHERE `family` LIKE '".getRequestElement('letter') . "%'";
210         } // END - if
211
212         // Parse the status or mode parameter
213         if (isGetRequestElementSet(('status'))) {
214                 // Is a WHERE statement already there?
215                 if (!empty($whereStatement)) {
216                         // Then append the status column
217                         $whereStatement .= sprintf(" AND `status`='%s'", SQL_ESCAPE(secureString(strtoupper(getRequestElement('status')))));
218                 } else {
219                         // Start a new one
220                         $whereStatement = sprintf(" WHERE `status`='%s'", SQL_ESCAPE(secureString(strtoupper(getRequestElement('status')))));
221                 }
222         } elseif (isGetRequestElementSet('mode')) {
223                 // Choose what we need to list
224                 switch (getRequestElement('mode')) {
225                         case 'norefs': // Users w/o refs
226                                 if (!empty($whereStatement)) {
227                                         // Add AND statement
228                                         $whereStatement .= " AND `refid`=0";
229                                 } else {
230                                         // Add WHERE statement
231                                         $whereStatement = " WHERE `refid`=0";
232                                 }
233                                 break;
234
235                         default: // Invalid list mode
236                                 logDebugMessage(__FILE__, __LINE__, sprintf("Invalid list mode %s detected.", getRequestElement('mode')));
237                                 break;
238                 } // END - switch
239         } // END - if
240
241         // Generate master query string
242         $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",
243                 getRequestElement('sortby')
244         );
245
246         // Prepare SQL and run it
247         $result_master = SQL_QUERY($sql, __FILE__, __LINE__);
248
249         // Calculate page count (0.5 fixes a bug with page count)
250         if (getConfig('user_limit') == 0) {
251                 setConfigEntry('user_limit', 100);
252                 loadTemplate('admin_settings_saved', false, getMessage('EXTENSION_WARNING_USER_LIMIT'));
253         } // END - if
254
255         // Activate the extension please!
256         $PAGES = round(SQL_NUMROWS($result_master) / getConfig('user_limit') + 0.5);
257
258         if (!isGetRequestElementSet('page'))   setRequestGetElement('page'  , 1);
259         if (!isGetRequestElementSet('offset')) setRequestGetElement('offset', getConfig('user_limit'));
260
261         // Add limitation to SQL string and run him again
262         $sql .= " LIMIT ".(getRequestElement('offset') * getRequestElement('page') - getRequestElement('offset')).", ".getRequestElement('offset');
263         $result = SQL_QUERY($sql, __FILE__, __LINE__);
264
265         // Count all confirmed users
266         $user_count = countSumTotalData('CONFIRMED','user_data','userid','status',true);
267
268         // Are there some entries?
269         if (SQL_NUMROWS($result_master) > 0) {
270                 // We have some (new?) registrations!
271                 define('__COLSPAN2'  , ($colspan + 2));
272                 define('__USER_CNT'  , $user_count);
273
274                 // Sorting links
275                 define('__ALPHA_SORT', alpha(getRequestElement('sortby'), $colspan, true));
276                 define('__SORT_LINKS', addSortLinks(getRequestElement('letter'), getRequestElement('sortby'), $colspan, true));
277
278                 if ($PAGES > 1) {
279                         define('__PAGE_NAV', addPageNavigation($PAGES, getConfig('user_limit'), true, $colspan, true));
280                 } else {
281                         // No page navigation is required
282                         define('__PAGE_NAV', '');
283                 }
284
285                 // Column with nickname when nickname extension is present
286                 // @TODO Rewrite this into a  filter
287                 if (isExtensionActive('nickname')) {
288                         // Nickname extension found
289                         define('__NICKNAME_TH', "  <td class=\"admin_title bottom2\" align=\"center\">{--NICKNAME--}</td>");
290                 } else {
291                         // Not found
292                         define('__NICKNAME_TH', '');
293                 }
294
295                 // Load all users
296                 $OUT = ''; $SW = 2;
297                 while ($content = SQL_FETCHARRAY($result)) {
298                         // Set refid link
299                         if ($content['refid'] > 0) $content['refid'] = generateUserProfileLink($content['refid']);
300
301                         // Get number of unconfirmed mails
302                         $content['links'] = countSumTotalData($content['userid'], 'user_links', 'id', 'userid', true);
303                         if ($content['links'] > 0) $content['links'] = $base . '&amp;what=list_links&amp;userid=' . $content['userid'] . '">' . translateComma($content['links']) . '</a>]';
304
305                         // Set link to sent mails if present
306                         if ($content['emails_sent'] > 0) $content['emails_sent'] = $base . '&amp;what=email_details&amp;userid=' . $content['userid'] . '">' . translateComma($content['emails_sent']) . '</a>]';
307
308                         // Add nickname
309                         if ((empty($content['nickname'])) || ($content['nickname'] == $content['userid'])) $content['nickname'] = '---';
310
311                         // Calculate total points
312                         $pointsTotal = countSumTotalData($content['userid'], 'user_points', 'points')  - countSumTotalData($content['userid'], 'user_data', 'used_points');
313
314                         // Clickrate
315                         $clickRate = 0;
316                         if ($content['emails_received'] > 0) {
317                                 $clickRate = $content['mails_confirmed'] / $content['emails_received'] * 100;
318                         } // END - if
319
320                         // Transfer data to array
321                         $content['sw']             = $SW;
322                         $content['userid_link']    = generateUserProfileLink($content['userid']);
323                         $content['gender']         = translateGender($content['gender']);
324                         $content['email']          = '[<a href="' . generateEmailLink($content['email'], 'user_data') . '">' . $content['email'] . '</a>]';
325                         $content['alinks']         = generateMemberAdminActionLinks($content['userid'], $content['status']);
326                         $content['points']         = translateComma($pointsTotal);
327                         $content['rate']           = translateComma($clickRate);
328                         $content['locked']         = translateComma(countSumTotalData($content['userid'], 'user_points', 'locked_points'));
329                         $content['lock_timestamp'] = generateDateTime($content['lock_timestamp'], 2);
330                         $content['status']         = translateUserStatus($content['status']);
331                         $content['refs']           = countSumTotalData($content['userid'], 'user_refs', 'id', 'userid', true);
332
333                         // If we have at least one referal, make it clickable to referal list
334                         if ($content['refs'] > 0) $content['refs'] = $base . '&amp;what=list_refs&amp;userid=' . $content['userid'] . '">' . $content['refs'] . '</a>]';
335
336                         // Is the lock reason not set?
337                         if (!isset($content['lock_reason'])) $content['lock_reason'] = '---';
338
339                         // Is the extension 'country' installed?
340                         // @TODO Rewrite this into a filter
341                         if (isExtensionActive('country')) {
342                                 // Then overwrite country information
343                                 $content['country'] = generateCountryInfo($content['country_code']);
344                         } elseif ($content['country'] == '') {
345                                 // Zero id???
346                                 $content['country'] = '???';
347                         }
348
349                         // Load row template and switch colors
350                         $OUT .= loadTemplate('admin_list_user_row', true, $content);
351                         $SW = 3 - $SW;
352                 } // END - while
353
354                 // Free memory
355                 SQL_FREERESULT($result);
356
357                 // Free some memory
358                 SQL_FREERESULT($result_master);
359
360                 // Remember all rows
361                 $content['rows'] = $OUT;
362
363                 // Init title with "all accounts"
364                 $content['title'] = getMessage('ADMIN_ALL_ACCOUNTS');
365                 if (isGetRequestElementSet(('status'))) {
366                         // Set title according to the 'status'
367                         $content['title'] = getMessage(sprintf("ADMIN_LIST_STATUS_%s_ACCOUNTS", strtoupper(getRequestElement(('status')))));
368                 } elseif (isGetRequestElementSet('mode')) {
369                         // Set title according to the "mode"
370                         $content['title'] = getMessage(sprintf("ADMIN_LIST_MODE_%s_ACCOUNTS", strtoupper(getRequestElement('mode'))));
371                 }
372
373                 // Load main template
374                 loadTemplate('admin_list_user', false, $content);
375         } else {
376                 // No one as registered so far! :-(
377                 loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NO_NONE_REGISTERED'));
378         }
379
380         // Free memory
381         SQL_FREERESULT($result_master);
382 }
383
384 // [EOF]
385 ?>