8c9abb01dbab5c422b24962c80a11feb8a66fcfc
[mailer.git] / inc / modules / member / what-transfer.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/07/2004 *
4  * ================                             Last change: 10/07/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-transfer.php                                *
8  * -------------------------------------------------------------------- *
9  * Short description : Point transfers                                  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Punktetransfers                                  *
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')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 } elseif (!IS_MEMBER()) {
39         LOAD_URL("modules.php?module=index");
40 } elseif ((!EXT_IS_ACTIVE("transfer")) && (!IS_ADMIN())) {
41         addFatalMessage(EXTENSION_PROBLEM_EXT_INACTIVE, "transfer");
42         return;
43 }
44
45 // Add description as navigation point
46 ADD_DESCR("member", __FILE__);
47
48 // Load data
49 $result = SQL_QUERY_ESC("SELECT opt_in FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
50         array($GLOBALS['userid']), __FILE__, __LINE__);
51 list($opt_in) = SQL_FETCHROW($result);
52
53 // Free memory
54 SQL_FREERESULT($result);
55
56 $MODE = "";
57 if (!empty($_GET['mode'])) $MODE = $_GET['mode'];
58
59 // Check for "faker"
60 if (($opt_in == "N") && ($MODE == "new")) $MODE = "";
61
62 switch ($MODE)
63 {
64 case "new": // Start new transfer
65         // Get total points and subtract the balance amount from it = maximum transferable points
66         $total = GET_TOTAL_DATA($GLOBALS['userid'], "user_points", "points")  - GET_TOTAL_DATA($GLOBALS['userid'], "user_data", "used_points");
67
68         // Remember maximum value for template
69         define('__TRANSFER_MAX_VALUE', round($total - getConfig('transfer_balance') - 0.5));
70
71         if (isset($_POST['ok'])) {
72                 // Add new transfer
73                 if (getConfig('transfer_code') > 0) {
74                         // Check for code
75                         $code = GEN_RANDOM_CODE(getConfig('transfer_code'), $_POST['code_chk'], $GLOBALS['userid'], __TRANSFER_MAX_VALUE);
76                         $valid_code = ($code == $_POST['code']);
77                 } else {
78                         // Zero length (= disabled) is always valid!
79                         $valid_code = true;
80                 }
81
82                 // Test password
83                 $valid_pass = ($pass == generateHash($_POST['password'], $pass));
84
85                 // Test transfer amount
86                 $valid_amount = ((!empty($_POST['points'])) && ($_POST['points'] <= __TRANSFER_MAX_VALUE));
87
88                 // Test reason for transfer
89                 $valid_reason = (!empty($_POST['reason']));
90
91                 // Test if a recipient is selected
92                 $valid_recipient = ($_POST['to_uid'] > 0);
93
94                 // Check for nickname extension and set additional data
95                 $nick = false; $ADD = ", userid";
96                 if (EXT_IS_ACTIVE("nickname")) {
97                         $ADD = ", nickname";
98                         $nick = true;
99                 }
100
101                 // Re-check receivers and own personal data
102                 $result = SQL_QUERY_ESC("SELECT userid, gender, surname, family, email".$ADD." FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid IN ('%s','%s') AND status='CONFIRMED' ORDER BY userid LIMIT 2",
103                         array($GLOBALS['userid'], bigintval($_POST['to_uid'])), __FILE__, __LINE__);
104                 $valid_data = (SQL_NUMROWS($result) == 2);
105
106                 if ($valid_code && $valid_pass && $valid_amount && $valid_reason && $valid_recipient) {
107                         // Let's start the transfer and load user data
108                         list($uid1, $gender1, $sname1, $fname1, $email1, $nick1) = SQL_FETCHROW($result);
109                         list($uid2, $gender2, $sname2, $fname2, $email2, $nick2) = SQL_FETCHROW($result);
110                         SQL_FREERESULT($result);
111                         if ($uid1 == $GLOBALS['userid']) {
112                                 // Data row 1 is sender's data
113                                 define('__SENDER_GENDER'     , TRANSLATE_GENDER($gender1));
114                                 define('__SENDER_NICK'    , $nick1);
115                                 define('__SENDER_SNAME'   , $sname1);
116                                 define('__SENDER_FNAME'   , $fname1);
117                                 define('__SENDER_EMAIL'   , $email1);
118                                 // Data row 2 is recpient's data
119                                 define('__RECIPIENT_GENDER'  , TRANSLATE_GENDER($gender2));
120                                 define('__RECIPIENT_NICK' , $nick2);
121                                 define('__RECIPIENT_SNAME', $sname2);
122                                 define('__RECIPIENT_FNAME', $fname2);
123                                 define('__RECIPIENT_EMAIL', $email2);
124
125                                 // Prepare variables for testing
126                                 $TEST_NICK_SENDER = $nick1;
127                                 $TEST_NICK_REC = $nick2;
128                         } else {
129                                 // Data row 2 is sender's data
130                                 define('__SENDER_GENDER'     , TRANSLATE_GENDER($gender2));
131                                 define('__SENDER_NICK'    , $nick2);
132                                 define('__SENDER_SNAME'   , $sname2);
133                                 define('__SENDER_FNAME'   , $fname2);
134                                 define('__SENDER_EMAIL'   , $email2);
135                                 // Data row 1 is recpient's data
136                                 define('__RECIPIENT_GENDER'  , TRANSLATE_GENDER($gender1));
137                                 define('__RECIPIENT_NICK' , $nick1);
138                                 define('__RECIPIENT_SNAME', $sname1);
139                                 define('__RECIPIENT_FNAME', $fname1);
140                                 define('__RECIPIENT_EMAIL', $email1);
141
142                                 // Prepare variables for testing
143                                 $TEST_NICK_SENDER = $nick2;
144                                 $TEST_NICK_REC = $nick1;
145                         }
146
147                         // Sender's UID is always currently stored in cookie userid...
148                         define('__SENDER_UID'     , $GLOBALS['userid']);
149                         define('__RECIPIENT_UID'  , $_POST['to_uid']);
150
151                         $SENDER = __SENDER_UID;
152                         $RECIPIENT = __RECIPIENT_UID;
153                         if ($nick) {
154                                 if (($TEST_NICK_SENDER != __SENDER_UID) && (!empty($TEST_NICK_SENDER))) {
155                                         $SENDER = __SENDER_NICK;
156                                 }
157
158                                 if (($TEST_NICK_REC != __RECIPIENT_UID) && (!empty($TEST_NICK_REC))) {
159                                         $RECIPIENT = __RECIPIENT_NICK;
160                                 }
161                         }
162
163                         // Remember transfer reason and fancy date/time in constants
164                         define('__TRANSFER_REASON', $_POST['reason']);
165                         define('__TRANSFER_EXPIRES', CREATE_FANCY_TIME(getConfig('transfer_age')));
166
167                         // Generate tranafer id
168                         define('__TRANS_ID', bigintval(GEN_RANDOM_CODE("10", mt_rand(0, 99999), $GLOBALS['userid'], $_POST['reason'])));
169
170                         // Add entries to both tables
171                         SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_transfers_in` (userid, from_uid, points, reason, time_trans, trans_id) VALUES ('%s','%s','%s','%s', UNIX_TIMESTAMP(),'%s')",
172                                 array(bigintval($_POST['to_uid']), $GLOBALS['userid'], bigintval($_POST['points']), $_POST['reason'], __TRANS_ID),
173                                 __FILE__, __LINE__);
174                         SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_transfers_out` (userid, to_uid, points, reason, time_trans, trans_id) VALUES ('%s','%s','%s','%s', UNIX_TIMESTAMP(),'%s')",
175                                 array($GLOBALS['userid'], bigintval($_POST['to_uid']), bigintval($_POST['points']), $_POST['reason'], __TRANS_ID),
176                                 __FILE__, __LINE__);
177
178                         // Add points to account *directly* ...
179                         ADD_POINTS_REFSYSTEM("member_transfer", bigintval($_POST['to_uid']), bigintval($_POST['points']), false, "0", false, "direct");
180
181                         // ... and add it to current user's used points
182                         SUB_POINTS("transfer", $GLOBALS['userid'], $_POST['points']);
183
184                         // First send email to recipient
185                         $msg = LOAD_EMAIL_TEMPLATE("member_transfer_recipient", "", __RECIPIENT_UID);
186                         SEND_EMAIL(__RECIPIENT_EMAIL, TRANSFER_MEMBER_RECIPIENT_SUBJ.": ".$SENDER, $msg);
187
188                         // Second send email to sender
189                         $msg = LOAD_EMAIL_TEMPLATE("member_transfer_sender", "", __SENDER_UID);
190                         SEND_EMAIL(__SENDER_EMAIL, TRANSFER_MEMBER_SENDER_SUBJ.": ".$RECIPIENT, $msg);
191
192                         // At last send admin mail(s)
193                         $ADMIN_SUBJ = sprintf("%s (%s->%s)", TRANSFER_ADMIN_SUBJECT, $SENDER, $RECIPIENT);
194                         SEND_ADMIN_NOTIFICATION($ADMIN_SUBJ, "admin_transfer_points");
195
196                         // Transfer is completed
197                         LOAD_TEMPLATE("admin_settings_saved", false, TRANSFER_COMPLETED."<br /><a href=\"{!URL!}/modules.php?module=login&amp;what=transfer\">".TRANSFER_CONTINUE_OVERVIEW."</a>");
198                 } elseif (!$valid_code) {
199                         // Invalid Touring code!
200                         LOAD_TEMPLATE("admin_settings_saved", false, "<div class=\"member_note\">".TRANSFER_INVALID_CODE."</div>");
201                         unset($_POST['ok']);
202                 } elseif (!$valid_pass) {
203                         // Wrong password entered
204                         LOAD_TEMPLATE("admin_settings_saved", false, "<div class=\"member_note\">".TRANSFER_INVALID_PASSWORD."</div>");
205                         unset($_POST['ok']);
206                 } elseif (!$valid_amount) {
207                         // Too much points entered
208                         LOAD_TEMPLATE("admin_settings_saved", false, "<div class=\"member_note\">".TRANSFER_INVALID_POINTS."</div>");
209                         unset($_POST['ok']);
210                 } elseif (!$valid_reason) {
211                         // No transfer reason entered
212                         LOAD_TEMPLATE("admin_settings_saved", false, "<div class=\"member_note\">".TRANSFER_INVALID_REASON."</div>");
213                         unset($_POST['ok']);
214                 } elseif (!$valid_recipient) {
215                         // No recipient selected
216                         LOAD_TEMPLATE("admin_settings_saved", false, "<div class=\"member_note\">".TRANSFER_INVALID_RECIPIENT."</div>");
217                         unset($_POST['ok']);
218                 } elseif (!$valid_data) {
219                         // No recipient selected
220                         LOAD_TEMPLATE("admin_settings_saved", false, "<div class=\"member_note\">".TRANSFER_INVALID_DATA."</div>");
221                         unset($_POST['ok']);
222                 }
223         }
224
225         if (!isset($_POST['ok'])) {
226                 // Load member list
227                 if (EXT_IS_ACTIVE("nickname")) {
228                         // Load userid and nickname
229                         $result = SQL_QUERY_ESC("SELECT userid, nickname FROM `{!_MYSQL_PREFIX!}_user_data` WHERE status='CONFIRMED' AND opt_in='Y' AND userid != '%s' ORDER BY userid",
230                          array($GLOBALS['userid']), __FILE__, __LINE__);
231                 } else {
232                         // Load only userid
233                         $result = SQL_QUERY_ESC("SELECT userid, userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE status='CONFIRMED' AND opt_in='Y' AND userid != '%s' ORDER BY userid",
234                          array($GLOBALS['userid']), __FILE__, __LINE__);
235                 }
236
237                 if (SQL_NUMROWS($result) > 0) {
238                         // Load list
239                         $OUT  = "<select name=\"to_uid\" size=\"1\" class=\"member_select\">
240   <option value=\"0\">".SELECT_NONE."</option>\n";
241                         while (list($uid, $nick) = SQL_FETCHROW($result)) {
242                                 $OUT .= "<option value=\"".$uid."\"";
243                                 if ((isset($_POST['to_uid'])) && ($_POST['to_uid'] == $uid)) $OUT .= " selected=\"selected\"";
244                                 $OUT .= ">";
245                                 if (($nick != $uid) && (!empty($nick))) {
246                                         // Output nickname
247                                         $OUT .= $nick;
248                                 } else {
249                                         // Output userid
250                                         $OUT .= $uid;
251                                 }
252                                 $OUT .= "</option>\n";
253                         }
254                         $OUT .= "</select>\n";
255                         define('__TRANSFER_TO_DISABLED', "");
256
257                         // Free memory
258                         SQL_FREERESULT($result);
259                 } else {
260                         // No one else is opt-in
261                         $OUT = TRANSFER_NO_ONE_ELSE_OPT_IN;
262                         define('__TRANSFER_TO_DISABLED', " disabled");
263                 }
264
265                 // Transfer output to constant for the template
266                 define('__TRANSFER_USERID_SELECTION', $OUT);
267
268                 // Generate Code
269                 if (getConfig('transfer_code') > 0) {
270                         $rand = mt_rand(0, 99999);
271                         $code = GEN_RANDOM_CODE(getConfig('transfer_code'), $rand, $GLOBALS['userid'], __TRANSFER_MAX_VALUE);
272                         $img = GENERATE_IMAGE($code, false);
273                         define('__TRANSFER_IMAGE_INPUT', "<input type=\"hidden\" name=\"code_chk\" value=\"".$rand."\" /><input type=\"text\" name=\"code\" class=\"member_normal\" size=\"5\" maxlength=\"7\"{!__TRANSFER_TO_DISABLED!} />&nbsp;".$img);
274                 } else {
275                         $code = "00000";
276                         define('__TRANSFER_IMAGE_INPUT', TRANSFER_NO_CODE);
277                 }
278
279                 // Transfer maybe already entered valued'
280                 if (isset($_GET['ok'])) {
281                         // Get values from form
282                         define('__TRANSFER_POINTS_VALUE', bigintval($_POST['points']));
283                         define('__TRANSFER_REASON_VALUE', strip_tags($_POST['reason']));
284                 } else {
285                         // Set empty values
286                         define('__TRANSFER_POINTS_VALUE', "");
287                         define('__TRANSFER_REASON_VALUE', "");
288                 }
289
290                 // Output form
291                 LOAD_TEMPLATE("member_transfer_new");
292         }
293         break;
294
295 case "list_in": // List only incoming transactions
296 case "list_out": // List only outgoing transactions
297         // As you can see I put list_in and list_out together. I now do a switch() again on it for the right SQL command
298         switch ($MODE)
299         {
300         case "list_in":
301                 $SQL = "SELECT trans_id, from_uid, points, reason, time_trans FROM `{!_MYSQL_PREFIX!}_user_transfers_in` WHERE userid=%s ORDER BY time_trans DESC LIMIT ".getConfig('transfer_max');
302                 $NOTHING = TRANSFER_NO_INCOMING_TRANSFERS;
303                 define('__TRANSFER_SUM', TRANSFER_TOTAL_INCOMING);
304                 define('__TRANSFER_TITLE', TRANSFER_LIST_INCOMING);
305                 break;
306
307         case "list_out":
308                 $SQL = "SELECT trans_id, to_uid, points, reason, time_trans FROM `{!_MYSQL_PREFIX!}_user_transfers_out` WHERE userid=%s ORDER BY time_trans DESC LIMIT ".getConfig('transfer_max');
309                 $NOTHING = TRANSFER_NO_OUTGOING_TRANSFERS;
310                 define('__TRANSFER_SUM', TRANSFER_TOTAL_OUTGOING);
311                 define('__TRANSFER_TITLE', TRANSFER_LIST_OUTGOING);
312                 break;
313         }
314
315         // Run the SQL command
316         $total = 0;
317         $result = SQL_QUERY_ESC($SQL, array($GLOBALS['userid']), __FILE__, __LINE__);
318         if (SQL_NUMROWS($result) > 0) {
319                 $OUT = ""; $SW = 2;
320                 while (list($tid, $uid, $points, $reason, $stamp) = SQL_FETCHROW($result)) {
321                         if ($type == "OUT") $points = "$points-";
322                         $OUT .= "<tr>
323   <td class=\"transfer_row1 switch_sw".$SW." bottom2 right2\">
324     <div class=\"transfer_row1\">".$tid."</div>
325   </td>
326   <td class=\"transfer_row2 switch_sw".$SW." bottom2 right2\">
327     <div class=\"transfer_row2\">".MAKE_DATETIME($stamp, "3")."</div>
328   </td>
329   <td class=\"transfer_row3 switch_sw".$SW." bottom2 right2\">
330     <div class=\"transfer_row3\">".$uid."</div>
331   </td>
332   <td class=\"transfer_row4 switch_sw".$SW." bottom2 right2\">
333     <div class=\"transfer_row4\">".$reason."</div>
334   </td>
335   <td class=\"transfer_row5 switch_sw".$SW." bottom2\">
336     <div class=\"transfer_row5\">".$points."</div>
337   </td>
338 </tr>\n";
339                         $total += $points;
340                         $SW = 3 - $SW;
341                 }
342
343                 // Free memory
344                 SQL_FREERESULT($result);
345         } else {
346                 // Nothing for in or out
347                 $OUT = "<tr>
348   <td colspan=\"5\" align=\"center\" class=\"bottom2\" height=\"70\">
349     ".LOAD_TEMPLATE("admin_settings_saved", true, $NOTHING)."
350   </td>
351 </tr>";
352         }
353
354         // ... and add them to a constant for the template
355         define('__TRANSFER_ROWS', $OUT);
356
357         // Remeber total amount
358         define('__TRANSFER_TOTAL_VALUE', $total);
359
360         // Load final template
361         LOAD_TEMPLATE("member_transfer_list");
362         break;
363
364 case "list_all": // List all transactions
365         // We fill a temporary table with data from both tables. This is much easier
366         // to code and unstand by you as sub-SELECT queries. I know this is not the
367         // fastest way but it shall be fine for now.
368         //
369         // First of all create the temporary table
370         $result = SQL_QUERY("CREATE TEMPORARY TABLE `{!_MYSQL_PREFIX!}_transfers_tmp` (
371 trans_id VARCHAR(12) NOT NULL DEFAULT '',
372 party_uid BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
373 points BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
374 reason VARCHAR(255) NOT NULL DEFAULT '',
375 time_trans VARCHAR(10) NOT NULL DEFAULT '0',
376 trans_type ENUM('IN','OUT') NOT NULL DEFAULT 'IN',
377 KEY(party_uid)
378 ) TYPE=HEAP", __FILE__, __LINE__);
379
380         // Let's begin with the incoming list
381         $result = SQL_QUERY_ESC("SELECT trans_id, from_uid, points, reason, time_trans FROM `{!_MYSQL_PREFIX!}_user_transfers_in` WHERE userid=%s ORDER BY `id` LIMIT %s",
382 array($GLOBALS['userid'], getConfig('transfer_max')), __FILE__, __LINE__);
383         while ($DATA = SQL_FETCHROW($result)) {
384                 $DATA[] = "IN";
385                 $DATA = implode("','", $DATA);
386                 $res_temp = SQL_QUERY("INSERT INTO `{!_MYSQL_PREFIX!}_transfers_tmp` (trans_id, party_uid, points, reason, time_trans, trans_type) VALUES ('".$DATA."')", __FILE__, __LINE__);
387         }
388
389         // Free memory
390         SQL_FREERESULT($result);
391
392         // As the last table transfer data from outgoing table to temporary
393         $result = SQL_QUERY_ESC("SELECT trans_id, to_uid, points, reason, time_trans FROM `{!_MYSQL_PREFIX!}_user_transfers_out` WHERE userid=%s ORDER BY `id` LIMIT %s",
394 array($GLOBALS['userid'], getConfig('transfer_max')), __FILE__, __LINE__);
395         while ($DATA = SQL_FETCHROW($result)) {
396                 $DATA[] = "OUT";
397                 $DATA = implode("','", $DATA);
398                 $res_temp = SQL_QUERY("INSERT INTO `{!_MYSQL_PREFIX!}_transfers_tmp` (trans_id, party_uid, points, reason, time_trans, trans_type) VALUES ('".$DATA."')", __FILE__, __LINE__);
399         }
400
401         // Free memory
402         SQL_FREERESULT($result);
403
404         $total = 0;
405         if (SQL_NUMROWS($result) > 0) {
406                 // Output rows
407                 $OUT = ""; $SW = 2;
408                 $result = SQL_QUERY("SELECT party_uid, trans_id, points, reason, time_trans, trans_type FROM `{!_MYSQL_PREFIX!}_transfers_tmp` ORDER BY time_trans DESC", __FILE__, __LINE__);
409                 while (list($uid, $idx, $points, $reason, $stamp, $type) = SQL_FETCHROW($result)) {
410                         if ($type == "OUT") $points = "-$points";
411                         $OUT .= "<tr>
412   <td class=\"transfer_row1 switch_sw".$SW." bottom2 right2\">
413     <div class=\"transfer_row1\">".$idx."</div>
414   </td>
415   <td class=\"transfer_row2 switch_sw".$SW." bottom2 right2\">
416     <div class=\"transfer_row2\">".MAKE_DATETIME($stamp, "3")."</div>
417   </td>
418   <td class=\"transfer_row3 switch_sw".$SW." bottom2 right2\">
419     <div class=\"transfer_row3\">".$uid."</div>
420   </td>
421   <td class=\"transfer_row4 switch_sw".$SW." bottom2 right2\">
422     <div class=\"transfer_row4\">".$reason."</div>
423   </td>
424   <td class=\"transfer_row5 switch_sw".$SW." bottom2\">
425     <div class=\"transfer_row5\">".$points."</div>
426   </td>
427 </tr>\n";
428                         $total += $points;
429                         $SW = 3 - $SW;
430                 }
431
432                 // Free memory
433                 SQL_FREERESULT($result);
434         } else {
435                 // Nothing for in and out
436                 $OUT = "<tr>
437   <td colspan=\"5\" align=\"center\" class=\"bottom2\" height=\"70\">
438     ".LOAD_TEMPLATE("admin_settings_saved", true, TRANSFER_NO_INOUT_TRANSFERS)."
439   </td>
440 </tr>";
441         }
442
443         // ... and add them to a constant for the template
444         define('__TRANSFER_ROWS', $OUT);
445
446         // Remeber total amount
447         define('__TRANSFER_TOTAL_VALUE', $total);
448
449         // Set title
450         define('__TRANSFER_TITLE', TRANSFER_LIST_ALL);
451
452         // Set "balance" word
453         define('__TRANSFER_SUM', TRANSFER_TOTAL_BALANCE);
454
455         // Load final template
456         LOAD_TEMPLATE("member_transfer_list");
457
458         // At the end we don't need a temporary table in memory
459         $result = SQL_QUERY("DROP TABLE IF EXISTS `{!_MYSQL_PREFIX!}_transfers_tmp`", __FILE__, __LINE__);
460
461         // Free some memory...
462         SQL_FREERESULT($result);
463         break;
464
465 case "": // Overview page
466         // Check incoming transfers
467         $result = SQL_QUERY_ESC("SELECT COUNT(id) FROM `{!_MYSQL_PREFIX!}_user_transfers_in` WHERE userid=%s", array($GLOBALS['userid']), __FILE__, __LINE__);
468         list($dmy) = SQL_FETCHROW($result);
469         SQL_FREERESULT($result);
470
471         $total=$dmy;
472         if ($dmy > 0) {
473                 define('__TRANSFER_IN_LINK', "<a href=\"{!URL!}/modules.php?module=login&amp;what=transfer&amp;mode=list_in\">".$dmy."</a>");
474         } else {
475                 define('__TRANSFER_IN_LINK', $dmy);
476         }
477
478         // Check outgoing transfers
479         $result = SQL_QUERY_ESC("SELECT COUNT(id) FROM `{!_MYSQL_PREFIX!}_user_transfers_out` WHERE userid=%s", array($GLOBALS['userid']), __FILE__, __LINE__);
480         list($dmy) = SQL_FETCHROW($result);
481         SQL_FREERESULT($result);
482
483         $total+=$dmy;
484         if ($dmy > 0) {
485                 define('__TRANSFER_OUT_LINK', "<a href=\"{!URL!}/modules.php?module=login&amp;what=transfer&amp;mode=list_out\">".$dmy."</a>");
486         } else {
487                 define('__TRANSFER_OUT_LINK', $dmy);
488         }
489
490         // Total transactions
491         if ($total > 0) {
492                 define('__TRANSFER_ALL_LINK', "<a href=\"{!URL!}/modules.php?module=login&amp;what=transfer&amp;mode=list_all\">".$total."</a>");
493         } else {
494                 define('__TRANSFER_ALL_LINK', $total);
495         }
496
497         if (isset($_POST['ok'])) {
498                 // Save settings
499                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET opt_in='%s' WHERE userid=%s LIMIT 1",
500                         array($_POST['opt_in'], $GLOBALS['userid']), __FILE__, __LINE__);
501
502                 // Rember for next switch() command
503                 $opt_in = $_POST['opt_in'];
504
505                 // "Settings saved..."
506                 LOAD_TEMPLATE("admin_settings_saved", false, "<div class=\"member_done\">".SETTINGS_SAVED."</div>");
507         }
508
509         switch ($opt_in)
510         {
511         case "Y":
512                 define('__TRANSFER_ALLOW_Y', " checked=\"checked\"");
513                 define('__TRANSFER_ALLOW_N', "");
514                 define('__TRANSFER_NEW_LINK', "<a href=\"{!URL!}/modules.php?module=login&amp;what=transfer&amp;mode=new\">".TRANSFER_NOW_LINK."</a>");
515                 break;
516
517         case "N":
518                 define('__TRANSFER_ALLOW_Y', "");
519                 define('__TRANSFER_ALLOW_N', " checked=\"checked\"");
520                 define('__TRANSFER_NEW_LINK', TRANSFER_PLEASE_ALLOW_OPT_IN);
521                 break;
522         }
523
524         // Check for latest out-transfers
525         $result = SQL_QUERY_ESC("SELECT time_trans
526 FROM `{!_MYSQL_PREFIX!}_user_transfers_out`
527 WHERE time_trans > (UNIX_TIMESTAMP() - %s) AND userid=%s
528 ORDER BY time_trans DESC
529 LIMIT 1", array(getConfig('transfer_timeout'), $GLOBALS['userid']), __FILE__, __LINE__);
530         if (SQL_NUMROWS($result) == 0) {
531                 // Load template
532                 define('__TRANSFER_SETTINGS_CONTENT', LOAD_TEMPLATE("member_transfer_settings", true));
533         } else {
534                 // Load newest transaction
535                 list($newest) = SQL_FETCHROW($result);
536                 SQL_FREERESULT($result);
537                 define('__TRANSFER_SETTINGS_CONTENT', TRANSFER_LATEST_IS_1.MAKE_DATETIME($newest, "3").TRANSFER_LATEST_IS_2);
538         }
539
540         // Load template
541         LOAD_TEMPLATE("member_transfer_overview");
542         break;
543 }
544 //
545 ?>