2 /************************************************************************
3 * MXChange v0.2.1 Start: 10/07/2004 *
4 * ================ Last change: 10/07/2004 *
6 * -------------------------------------------------------------------- *
7 * File : what-transfer.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Point transfers *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Punktetransfers *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 *
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. *
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. *
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, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
42 } elseif (!isMember()) {
43 redirectToIndexMemberOnlyModule();
46 // Add description as navigation point
47 addMenuDescription('member', __FILE__);
49 if ((!isExtensionActive('transfer')) && (!isAdmin())) {
50 loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('transfer'));
55 $result = SQL_QUERY_ESC("SELECT `opt_in` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
56 array(getUserId()), __FILE__, __LINE__);
57 list($opt_in) = SQL_FETCHROW($result);
60 SQL_FREERESULT($result);
62 // Check for mode in GET
64 if (isGetRequestElementSet('mode')) $mode = getRequestElement('mode');
67 if (($opt_in != 'Y') && ($mode == 'new')) $mode = '';
70 case 'new': // Start new transfer
71 // Get total points and subtract the balance amount from it = maximum transferable points
72 $total = countSumTotalData(getUserId(), 'user_points', 'points') - countSumTotalData(getUserId(), 'user_data', 'used_points');
74 // Remember maximum value for template
75 $content['max_transferable'] = round($total - getConfig('transfer_balance') - 0.5);
79 if (getConfig('transfer_code') > 0) {
81 $code = generateRandomCode(getConfig('transfer_code'), postRequestElement('code_chk'), getUserId(), $content['max_transferable']);
82 $valid_code = ($code == postRequestElement('code'));
84 // Zero length (= disabled) is always valid!
89 $valid_pass = ($pass == generateHash(postRequestElement('password'), $pass));
91 // Test transfer amount
92 $valid_amount = ((isPostRequestElementSet('points')) && (postRequestElement('points') <= $content['max_transferable']));
94 // Test reason for transfer
95 $valid_reason = (isPostRequestElementSet('reason'));
97 // Test if a recipient is selected
98 $valid_recipient = (postRequestElement('to_userid') > 0);
100 // Check for nickname extension and set additional data
101 // @TODO Rewrite this to a filter
103 if (isExtensionActive('nickname')) {
104 $add = ', `nickname`';
107 // Re-check receivers and own personal data
108 $result = SQL_QUERY_ESC("SELECT `userid`, `gender`, `surname`, `family`, `email`".$add." FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid` IN ('%s','%s') AND `status`='CONFIRMED' LIMIT 2",
111 bigintval(postRequestElement('to_userid'))
112 ), __FILE__, __LINE__);
114 // Do we have two entries?
115 $valid_data = (SQL_NUMROWS($result) == 2);
117 // Final check if all is fine
118 if ($valid_code && $valid_data && $valid_pass && $valid_amount && $valid_reason && $valid_recipient) {
119 // Let's start the transfer and load user data
120 $content['sender'] = SQL_FETCHARRAY($result);
121 $content['recipient'] = SQL_FETCHARRAY($result);
123 // Is the nickname extension not installed?
124 if (!isExtensionActive('nickname')) {
125 // Fix empty nicknames
126 $content['sender']['nickname'] = '';
127 $content['recipient']['nickname'] = '';
130 // Translate some data
131 $content['sender']['gender'] = translateGender($content['sender']['gender']);
132 $content['recipient']['gender'] = translateGender($content['recipient']['gender']);
134 // Prepare variables for testing
135 $TEST_NICK_SENDER = $content['sender']['nickname'];
136 $TEST_NICK_REC = $content['recipient']['nickname'];
138 // Default is userids for subject line
139 $SENDER = getUserId();
140 $RECIPIENT = bigintval(postRequestElement('to_userid'));
142 // If nickname is installed we can set the nickname
143 // @TODO Rewrite this to a filter
144 if (isExtensionActive('nickname')) {
145 if (($TEST_NICK_SENDER != getUserId()) && (!empty($TEST_NICK_SENDER))) {
146 $SENDER = $content['sender']['nickname'];
149 if (($TEST_NICK_REC != postRequestElement('to_userid')) && (!empty($TEST_NICK_REC))) {
150 $RECIPIENT = $content['recipient']['nickname'];
154 // Remember transfer reason and fancy date/time in constants
155 $content['reason'] = secureString(postRequestElement('reason'));
156 $content['expires'] = createFancyTime(getConfig('transfer_age'));
158 // Generate tranafer id
159 $content['trans_id'] = bigintval(generateRandomCode('10', mt_rand(0, 99999), getUserId(), postRequestElement('reason')));
161 // Add entries to both tables
162 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_transfers_in` (`userid`, `from_userid`, `points`, `reason`, `time_trans`, `trans_id`) VALUES ('%s','%s','%s','%s', UNIX_TIMESTAMP(),'%s')",
163 array(bigintval(postRequestElement('to_userid')), getUserId(), bigintval(postRequestElement('points')), postRequestElement('reason'), $content['trans_id']), __FILE__, __LINE__);
164 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_transfers_out` (`userid`, `to_userid`, `points`, `reason`, `time_trans`, `trans_id`) VALUES ('%s','%s','%s','%s', UNIX_TIMESTAMP(),'%s')",
165 array(getUserId(), bigintval(postRequestElement('to_userid')), bigintval(postRequestElement('points')), postRequestElement('reason'), $content['trans_id']), __FILE__, __LINE__);
167 // Add points to account *directly* ...
168 addPointsDirectly('member_transfer', bigintval(postRequestElement('to_userid')), bigintval(postRequestElement('points')));
170 // ... and add it to current user's used points
171 subtractPoints('transfer', getUserId(), postRequestElement('points'));
173 // First send email to recipient
174 $message = loadEmailTemplate('member_transfer_recipient', $content, postRequestElement('to_userid'));
175 sendEmail($content['recipient']['email'], getMessage('TRANSFER_MEMBER_RECIPIENT_SUBJ') . ': ' . $SENDER, $message);
177 // Second send email to sender
178 $message = loadEmailTemplate('member_transfer_sender', $content, getUserId());
179 sendEmail($content['sender']['email'], getMessage('TRANSFER_MEMBER_SENDER_SUBJ') . ': ' . $RECIPIENT, $message);
181 // At last send admin mail(s)
182 $ADMIN_SUBJ = sprintf("%s (%s->%s)", getMessage('TRANSFER_ADMIN_SUBJECT'), $SENDER, $RECIPIENT);
183 sendAdminNotification($ADMIN_SUBJ, 'admin_transfer_points', $content);
185 // Transfer is completed
186 loadTemplate('admin_settings_saved', false, getMessage('TRANSFER_COMPLETED') . "<br /><a href=\"{?URL?}/modules.php?module=login&what=transfer\">{--TRANSFER_CONTINUE_OVERVIEW--}</a>");
187 } elseif ($valid_code === false) {
188 // Invalid Touring code!
189 loadTemplate('admin_settings_saved', false, "<div class=\"member_note\">{--TRANSFER_INVALID_CODE--}</div>");
190 unsetPostRequestElement('ok');
191 } elseif ($valid_pass === false) {
192 // Wrong password entered
193 loadTemplate('admin_settings_saved', false, "<div class=\"member_note\">{--TRANSFER_INVALID_PASSWORD--}</div>");
194 unsetPostRequestElement('ok');
195 } elseif ($valid_amount === false) {
196 // Too much points entered
197 loadTemplate('admin_settings_saved', false, "<div class=\"member_note\">{--TRANSFER_INVALID_POINTS--}</div>");
198 unsetPostRequestElement('ok');
199 } elseif ($valid_reason === false) {
200 // No transfer reason entered
201 loadTemplate('admin_settings_saved', false, "<div class=\"member_note\">{--TRANSFER_INVALID_REASON--}</div>");
202 unsetPostRequestElement('ok');
203 } elseif ($valid_recipient === false) {
204 // No recipient selected
205 loadTemplate('admin_settings_saved', false, "<div class=\"member_note\">{--TRANSFER_INVALID_RECIPIENT--}</div>");
206 unsetPostRequestElement('ok');
207 } elseif ($valid_data === false) {
208 // No recipient/sender selected
209 loadTemplate('admin_settings_saved', false, "<div class=\"member_note\">{--TRANSFER_INVALID_DATA--}</div>");
210 unsetPostRequestElement('ok');
214 SQL_FREERESULT($result);
219 if (isExtensionActive('nickname')) {
220 // Load userid and nickname
221 $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` ASC",
222 array(getUserId()), __FILE__, __LINE__);
225 $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` ASC",
226 array(getUserId()), __FILE__, __LINE__);
229 if (SQL_NUMROWS($result) > 0) {
231 $OUT = "<select name=\"to_userid\" size=\"1\" class=\"member_select\">
232 <option value=\"0\">{--SELECT_NONE--}</option>\n";
233 // @TODO Try to rewrite his to $content = SQL_FETCHARRAY(), see some lines above for two different queries
234 while (list($userid, $nick) = SQL_FETCHROW($result)) {
235 $OUT .= " <option value=\"".$userid."\"";
236 if ((isPostRequestElementSet(('to_userid'))) && (postRequestElement('to_userid') == $userid)) $OUT .= ' selected="selected"';
238 if (($nick != $userid) && (!empty($nick))) {
245 $OUT .= "</option>\n";
247 $OUT .= "</select>\n";
248 $content['to_disabled'] = '';
251 SQL_FREERESULT($result);
253 // No one else is opt-in
254 $OUT = loadTemplate('admin_settings_saved', true, getMessage('TRANSFER_NO_ONE_ELSE_OPT_IN'));
255 $content['to_disabled'] = ' disabled="disabled"';
258 // Transfer output to constant for the template
259 $content['userid_selection'] = $OUT;
262 if (getConfig('transfer_code') > 0) {
263 $rand = mt_rand(0, 99999);
264 $code = generateRandomCode(getConfig('transfer_code'), $rand, getUserId(), $content['max_transferable']);
265 $img = generateImageOrCode($code, false);
266 $content['captcha_code'] = "<input type=\"hidden\" name=\"code_chk\" value=\"".$rand."\" /><input type=\"text\" name=\"code\" class=\"member_normal\" size=\"5\" maxlength=\"7\"".$content['to_disabled']." /> " . $img;
269 $content['captcha_code'] = loadTemplate('admin_settings_saved', true, getMessage('TRANSFER_NO_CODE'));
272 // Init points/reason
273 $content['points'] = '';
274 $content['reason'] = '';
276 // Transfer maybe already entered valued'
277 if (isGetRequestElementSet('ok')) {
278 // Get values from form
279 $content['points'] = bigintval(postRequestElement('points'));
280 $content['reason'] = secureString(postRequestElement('reason'));
283 // Translate some array elements for template
284 $content['max_transferable'] = translateComma($content['max_transferable']);
287 loadTemplate('member_transfer_new', false, $content);
291 case 'list_in': // List only incoming transactions
292 case 'list_out': // List only outgoing transactions
293 // 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
294 $nothingMessage = '';
297 $sql = "SELECT `trans_id`, `from_userid` AS party_userid, `points`, `reason`, `time_trans` FROM `{?_MYSQL_PREFIX?}_user_transfers_in` WHERE `userid`=%s ORDER BY `time_trans` DESC LIMIT ".getConfig('transfer_max');
298 $nothingMessage = getMessage('TRANSFER_NO_INCOMING_TRANSFERS');
299 $content['balance'] = getMessage('TRANSFER_TOTAL_INCOMING');
300 $content['title'] = getMessage('TRANSFER_LIST_INCOMING');
304 $sql = "SELECT `trans_id`, `to_userid` AS party_userid, `points`, `reason`, `time_trans` FROM `{?_MYSQL_PREFIX?}_user_transfers_out` WHERE `userid`=%s ORDER BY `time_trans` DESC LIMIT ".getConfig('transfer_max');
305 $nothingMessage = getMessage('TRANSFER_NO_OUTGOING_TRANSFERS');
306 $content['balance'] = getMessage('TRANSFER_TOTAL_OUTGOING');
307 $content['title'] = getMessage('TRANSFER_LIST_OUTGOING');
311 // Run the SQL command and set total points to zero
313 $result = SQL_QUERY_ESC($sql, array(getUserId()), __FILE__, __LINE__);
315 // Do we have entries?
316 if (SQL_NUMROWS($result) > 0) {
318 // @TODO This should be somehow rewritten to $row = SQL_FETCHARRAY(), see switch() block above for SQL queries
319 while (list($tid, $userid, $points, $reason, $stamp) = SQL_FETCHROW($result)) {
321 if ($type == 'OUT') $points = $points . '-';
323 // Prepare content for template
324 // @TODO Rewrite: tid->trans_id,stamp->time_trans
328 'stamp' => generateDateTime($stamp, 3),
331 'points' => translateComma($points)
335 $OUT .= loadTemplate('member_transfer_row2', true, $row);
337 // Add points and switch color
338 $totalPoints += $points;
343 SQL_FREERESULT($result);
345 // Nothing for in or out
347 <td colspan=\"5\" align=\"center\" class=\"bottom2\" height=\"70\">
348 ".loadTemplate('admin_settings_saved', true, $nothingMessage)."
353 // ... and add them to a constant for the template
354 $content['rows'] = $OUT;
356 // Remeber total amount
357 $content['total'] = translateComma($totalPoints);
359 // Load final template
360 loadTemplate('member_transfer_list', false, $content);
363 case 'list_all': // List all transactions
364 // We fill a temporary table with data from both tables. This is much easier
365 // to code and unstand by you as sub-SELECT queries. I know this is not the
366 // fastest way but it shall be fine for now.
368 // First of all create the temporary table
369 $result = SQL_QUERY("CREATE TEMPORARY TABLE `{?_MYSQL_PREFIX?}_transfers_tmp` (
370 `trans_id` VARCHAR(12) NOT NULL DEFAULT '',
371 `party_userid` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
372 `points` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
373 `reason` VARCHAR(255) NOT NULL DEFAULT '',
374 `time_trans` VARCHAR(10) NOT NULL DEFAULT 0,
375 `trans_type` ENUM('IN','OUT') NOT NULL DEFAULT 'IN',
377 ) TYPE=HEAP", __FILE__, __LINE__);
379 // Let's begin with the incoming list
380 $result = SQL_QUERY_ESC("SELECT `trans_id`, `from_userid`, `points`, `reason`, `time_trans` FROM `{?_MYSQL_PREFIX?}_user_transfers_in` WHERE `userid`=%s ORDER BY `id` ASC LIMIT %s",
381 array(getUserId(), getConfig('transfer_max')), __FILE__, __LINE__);
382 while ($DATA = SQL_FETCHROW($result)) {
384 $DATA = implode("','", $DATA);
385 $res_temp = SQL_QUERY("INSERT INTO `{?_MYSQL_PREFIX?}_transfers_tmp` (`trans_id`, `party_userid`, `points`, `reason`, `time_trans`, `trans_type`) VALUES ('".$DATA."')", __FILE__, __LINE__);
389 SQL_FREERESULT($result);
391 // As the last table transfer data from outgoing table to temporary
392 $result = SQL_QUERY_ESC("SELECT `trans_id`, `to_userid`, `points`, `reason`, `time_trans` FROM `{?_MYSQL_PREFIX?}_user_transfers_out` WHERE `userid`=%s ORDER BY `id` LIMIT {?transfer_max?}",
393 array(getUserId()), __FILE__, __LINE__);
394 while ($DATA = SQL_FETCHROW($result)) {
396 $DATA = implode("','", $DATA);
397 $res_temp = SQL_QUERY("INSERT INTO `{?_MYSQL_PREFIX?}_transfers_tmp` (`trans_id`, `party_userid`, `points`, `reason`, `time_trans`, `trans_type`) VALUES ('".$DATA."')", __FILE__, __LINE__);
401 SQL_FREERESULT($result);
404 if (SQL_NUMROWS($result) > 0) {
405 // Search for entries
406 $result = SQL_QUERY("SELECT `party_userid`, `trans_id`, `points`, `reason`, `time_trans`, `trans_type` FROM `{?_MYSQL_PREFIX?}_transfers_tmp` ORDER BY `time_trans` DESC",
411 while ($content = SQL_FETCHARRAY($result)) {
413 if ($content['trans_type'] == 'OUT') $content['points'] = '-'.$content['points']."";
415 // Prepare content for template
416 $content['sw'] = $SW;
417 $content['time'] = generateDateTime($content['time_trans'], 3);
418 $content['points'] = translateComma($content['points']);
421 $OUT .= loadTemplate('member_transfer_row', true, $content);
423 // Add points and switch color
424 $total += $content['points'];
429 SQL_FREERESULT($result);
431 // Nothing for in and out
433 <td colspan=\"5\" align=\"center\" class=\"bottom2\" height=\"70\">
434 ".loadTemplate('admin_settings_saved', true, getMessage('TRANSFER_NO_INOUT_TRANSFERS'))."
439 // ... and add them to a constant for the template
440 $content['rows'] = $OUT;
442 // Remeber total amount
443 $content['total'] = translateComma($total);
446 $content['title'] = getMessage('TRANSFER_LIST_ALL');
448 // Set "balance" word
449 $content['balance'] = getMessage('TRANSFER_TOTAL_BALANCE');
451 // At the end we don't need a temporary table in memory
452 $result = SQL_QUERY("DROP TABLE IF EXISTS `{?_MYSQL_PREFIX?}_transfers_tmp`", __FILE__, __LINE__);
454 // Load final template
455 loadTemplate('member_transfer_list', false, $content);
457 // Free some memory...
458 SQL_FREERESULT($result);
461 default: // Overview page
462 // Check incoming transfers
463 $total = countSumTotalData(getUserId(), 'user_transfers_in', 'id', 'userid', true);
464 $content['in_link'] = $total;
466 $content['in_link'] = "<a href=\"{?URL?}/modules.php?module=login&what=transfer&mode=list_in\">".$total."</a>";
469 // Check outgoing transfers
470 $dmy = countSumTotalData(getUserId(), 'user_transfers_out', 'id', 'userid', true);
472 // Add to total amount
475 $content['out_link'] = $dmy;
477 $content['out_link'] = "<a href=\"{?URL?}/modules.php?module=login&what=transfer&mode=list_out\">".$dmy."</a>";
480 // Total transactions
481 $content['all_link'] = $total;
483 $content['all_link'] = "<a href=\"{?URL?}/modules.php?module=login&what=transfer&mode=list_all\">".$total."</a>";
488 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `opt_in`='%s' WHERE `userid`=%s LIMIT 1",
489 array(postRequestElement('opt_in'), getUserId()), __FILE__, __LINE__);
491 // Rember for next switch() command
492 $opt_in = substr(postRequestElement('opt_in'), 0, 1);
494 // "Settings saved..."
495 loadTemplate('admin_settings_saved', false, "<div class=\"member_done\">{--SETTINGS_SAVED--}</div>");
499 foreach (array('allow_y','allow_n') as $entry) {
500 $content[$entry] = '';
503 // Set current selection
504 $content['allow_' . strtolower($opt_in)] = ' checked="checked"';
506 // Set 'new transfer' link according to above option
509 $content['new_link'] = "<a href=\"{?URL?}/modules.php?module=login&what=transfer&mode=new\">{--TRANSFER_NOW_LINK--}</a>";
513 $content['new_link'] = getMessage('TRANSFER_PLEASE_ALLOW_OPT_IN');
517 // Check for latest out-transfers
518 $result = SQL_QUERY_ESC("SELECT
521 `{?_MYSQL_PREFIX?}_user_transfers_out`
523 `time_trans` > (UNIX_TIMESTAMP() - %s) AND `userid`=%s
528 getConfig('transfer_timeout'),
530 ), __FILE__, __LINE__);
532 // Do we have an entry?
533 if (SQL_NUMROWS($result) == 1) {
534 // Load newest transaction
535 list($newest) = SQL_FETCHROW($result);
536 $content['settings'] = sprintf(getMessage('TRANSFER_LATEST_IS'), generateDateTime($newest, 3));
539 $content['settings'] = loadTemplate('member_transfer_settings', true, $content);
543 SQL_FREERESULT($result);
546 loadTemplate('member_transfer_overview', false, $content);