Mailer project continued:
[mailer.git] / inc / modules / admin / what-list_payouts.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 05/08/2004 *
4  * ===================                          Last change: 08/12/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-list_payouts.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : List member's payout requests                    *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Auflistung der Auszahlungsanfragen               *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if ((!defined('__SECURITY')) || (!isAdmin())) {
40         die();
41 } // END - if
42
43 // Add description as navigation point
44 addYouAreHereLink('admin', __FILE__);
45
46 if (isGetRequestElementSet('pid')) {
47         // First let's get the member's id
48         $result = SQL_QUERY_ESC("SELECT `userid`, `target_account`, `payout_total`, `payout_timestamp`, `password` FROM `{?_MYSQL_PREFIX?}_user_payouts` WHERE `id`=%s LIMIT 1",
49                 array(getRequestElement('pid')), __FILE__, __LINE__);
50         list($userid, $tuserid, $points, $tstamp, $tpass) = SQL_FETCHROW($result);
51         SQL_FREERESULT($result);
52
53         // Obtain some data
54         if (!isGetRequestElementSet('task') && (!empty($userid)) && (isValidUserId($userid))) {
55                 // Get task id from database
56                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `userid`=%s AND `task_type`='PAYOUT_REQUEST' AND `task_created`=%s LIMIT 1",
57                         array(bigintval($userid), bigintval($tstamp)), __FILE__, __LINE__);
58                 list($taskId) = SQL_FETCHROW($result);
59                 SQL_FREERESULT($result);
60                 if (empty($taskId)) $taskId = '0';
61         } elseif ((empty($userid)) || ($userid == '0')) {
62                 // Cannot obtain member id!
63                 displayMessage('{--ADMIN_PAYOUT_FAILED_OBTAIN_USERID--}');
64         } else {
65                 // Get task id from URL
66                 $taskId = getRequestElement('task');
67         }
68
69         if ((!empty($taskId)) && (!empty($userid)) && (isValidUserId($userid))) {
70                 // Load user's data
71                 if (!fetchUserData($userid)) {
72                         // Abort here because it is not valid!
73                         reportBug(__FILE__, __LINE__, 'No user account ' . $userid . ' found.');
74                 } // END - if
75
76                 if ((getRequestElement('do') == 'accept') && (getUserData('email') != '')) {
77                         // Ok, now we can output the form or execute accepting
78                         if (isFormSent()) {
79                                 // Obtain payout type and other data
80                                 $result = SQL_QUERY_ESC("SELECT `payout_id` FROM `{?_MYSQL_PREFIX?}_user_payouts` WHERE `id`=%s LIMIT 1",
81                                         array(bigintval(getRequestElement('pid'))), __FILE__, __LINE__);
82
83                                 // Load ptype (id)
84                                 list($ptype) = SQL_FETCHROW($result);
85
86                                 // Free result
87                                 SQL_FREERESULT($result);
88
89                                 if (!empty($ptype)) {
90                                         // Obtain data from payout type
91                                         $result = SQL_QUERY_ESC("SELECT
92         `from_account`,
93         `from_pass`,
94         `engine_url`,
95         `engine_ret_ok`,
96         `engine_ret_failed`,
97         `pass_enc`,
98         `allow_url`
99 FROM
100         `{?_MYSQL_PREFIX?}_payout_types`
101 WHERE
102         `id`=%s
103 LIMIT 1",
104                                                 array(bigintval($ptype)), __FILE__, __LINE__);
105
106                                         // Load data
107                                         $data = SQL_FETCHARRAY($result);
108
109                                         // Free result
110                                         SQL_FREERESULT($result);
111
112                                         if (!empty($data['engine_url'])) {
113                                                 // Ok, run URL...
114                                                 switch ($data['pass_enc']) {
115                                                         case 'md5':
116                                                                 $data['from_pass'] = md5($data['from_pass']);
117                                                                 $tpass = md5($tpass);
118                                                                 break;
119
120                                                         case 'base64':
121                                                                 $data['from_pass'] = base64_encode($data['from_pass']);
122                                                                 $tpass = base64_encode($tpass);
123                                                                 break;
124                                                 } // END - switch
125
126                                                 // Transfer variables...
127                                                 $reason = encodeString(getMessage('ADMIN_PAYOUT_REASON'), false);
128
129                                                 // Execute transfer
130                                                 $ret = sendGetRequest($data['engine_url']);
131                                         } else {
132                                                 // No URL to run
133                                                 $ret[0] = $data['engine_ret_ok'];
134                                         }
135
136                                         if ($ret[0] == $data['engine_ret_ok']) {
137                                                 // Clear task
138                                                 if ($taskId > 0) {
139                                                         runFilterChain('solve_task', $taskId);
140                                                 } // END - if
141
142                                                 // Clear payout request
143                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_payouts` SET `status`='ACCEPTED' WHERE `id`=%s LIMIT 1",
144                                                         array(bigintval(getRequestElement('pid'))), __FILE__, __LINE__);
145
146                                                 // Send out mail
147                                                 $message = loadEmailTemplate('member_payout_accepted', postRequestElement('text'), $userid);
148
149                                                 // Output message
150                                                 if ($data['allow_url'] == 'Y') {
151                                                         // Banner / Textlink request
152                                                         displayMessage('{--ADMIN_PAYOUT_BANNER_ACCEPTED_NOTIFIED--}');
153                                                 } else {
154                                                         // Normal request
155                                                         displayMessage('{--ADMIN_PAYOUT_ACCEPTED_NOTIFIED--}');
156                                                 }
157
158                                                 // Finally send mail
159                                                 sendEmail(getUserData('userid'), '{--MEMBER_PAYOUT_ACCEPTED_SUBJECT--}', $message);
160                                         } else {
161                                                 // Something goes wrong... :-(
162                                                 $content = implode('<br />', $ret);
163                                                 loadTemplate('admin_payout_failed_transfer', false, $content);
164                                         }
165                                 } else {
166                                         // Cannot load payout id
167                                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_PAYOUT_FAILED_OBTAIN_PAYOUT_ID--}');
168                                 }
169                         } else {
170                                 // Prepare content
171                                 $content = array(
172                                         'task' => $taskId,
173                                         'pid'  => bigintval(getRequestElement('pid')),
174                                         'user' => '<a href="' . generateEmailLink(getUserData('email'), 'user_data') . '">{%pipe,translateGender=' . getUserData('gender') . '%} ' . getUserData('surname') . ' ' . getUserData('family') . '</a>',
175                                 );
176
177                                 // Load template
178                                 loadTemplate('admin_payout_accept_form', false, $content);
179                         }
180                 } elseif ((getRequestElement('do') == 'reject') && (getUserData('email') != '')) {
181                         // Ok, now we can output the form or execute rejecting
182                         if (isFormSent()) {
183                                 if ($taskId > 0) {
184                                         // Clear task
185                                         runFilterChain('solve_task', $taskId);
186                                 } // END - if
187
188                                 // Clear payout request
189                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_payouts` SET `status`='REJECTED' WHERE `id`=%s LIMIT 1",
190                                         array(bigintval(getRequestElement('pid'))), __FILE__, __LINE__);
191
192                                 // Send out mail
193                                 $message = loadEmailTemplate('member_payout_rejected', postRequestElement('text'), $userid);
194
195                                 // Output message
196                                 displayMessage('{--ADMIN_PAYOUT_REJECTED_NOTIFIED--}');
197
198                                 // Finally send mail
199                                 sendEmail(getUserData('userid'), '{--MEMBER_PAYOUT_REJECTED_SUBJECT--}', $message);
200                         } else {
201                                 // Prepare content
202                                 $content = array(
203                                         'task' => $taskId,
204                                         'pid'  => bigintval(getRequestElement('pid')),
205                                         'user' => '<a href="' . generateEmailLink(getUserData('email'), 'user_data') . '">{%pipe,translateGender=' . getUserData('gender') . '%} ' . getUserData('surname') . ' ' . getUserData('family') . '</a>',
206                                 );
207
208                                 // Load template
209                                 loadTemplate('admin_payout_reject_form', false, $content);
210                         }
211                 } else {
212                         // Cannot load user data
213                         displayMessage('{--ADMIN_PAYOUT_FAILED_OBTAIN_USERDATA--}');
214                 }
215         } elseif ((empty($taskId)) || ($taskId == '0')) {
216                 // Failed loading task id
217                 displayMessage('{--ADMIN_PAYOUT_FAILED_OBTAIN_TASK_ID--}');
218         }
219 } else {
220         if (getRequestElement('do') == 'delete') {
221                 // Delete all requests
222                 $result = SQL_QUERY("TRUNCATE `{?_MYSQL_PREFIX?}_user_payouts`", __FILE__, __LINE__);
223         } // END - if
224
225         // Search for payouts
226         $result = SQL_QUERY("SELECT
227         p.id, p.userid, p.payout_total, p.target_account,
228         p.target_bank, t.type, p.payout_timestamp, p.status,
229         t.allow_url AS allow, p.target_url AS url, p.link_text AS alt,
230         p.banner_url AS banner
231 FROM
232         `{?_MYSQL_PREFIX?}_user_payouts` AS p
233 LEFT JOIN
234         `{?_MYSQL_PREFIX?}_payout_types` AS t
235 ON
236         p.payout_id=t.id
237 ORDER BY
238         p.payout_timestamp DESC", __FILE__, __LINE__);
239
240         if (!SQL_HASZERONUMS($result)) {
241                 // List found payouts
242                 $OUT = '';
243                 while ($content = SQL_FETCHARRAY($result)) {
244                         if ($content['status'] == 'NEW') {
245                                 // Generate links for direct accepting and rejecting
246                                 $content['status'] = '<a href="{%url=modules.php?module=admin&amp;what=list_payouts&amp;do=accept&amp;pid=' . $content['id'] . '%}">{--ADMIN_PAYOUT_ACCEPT_PAYOUT--}</a>|<a href="{%url=modules.php?module=admin&amp;what=list_payouts&amp;do=reject&amp;pid=' . $content['id'] . '%}">{--ADMIN_PAYOUT_REJECT_PAYOUT--}</a>';
247                         } else {
248                                 // Translate status
249                                 $content['status'] = translatePayoutStatus($content['status']);
250                         }
251
252                         // Nothing entered must be secured in member/what-payputs.php !
253                         if ($content['allow'] == 'Y') {
254                                 // Banner/Textlink views/clicks request
255                                 if (!empty($content['banner'])) {
256                                         // Load template for the banner
257                                         $content['target_account'] = loadTemplate('admin_list_payouts_banner', true, $content);
258                                 } else {
259                                         // Textlink
260                                         $content['target_account'] = loadTemplate('admin_list_payouts_txt', true, $content);
261                                 }
262
263                                 // Admins can addionally test the URL for framekillers
264                                 $content['target_bank'] = '<a href="' . generateFrametesterUrl($content['url']) . '" target="_blank">{--CLICK_HERE--}</a>';
265                         } // END - if
266
267                         // Add/Translate some data
268                         $content['payout_timestamp'] = generateDateTime($content['payout_timestamp'], 2);
269
270                         // Add row and switch color
271                         $OUT .= loadTemplate('admin_list_payouts_row', true, $content);
272                 } // END - while
273
274                 // Free memory
275                 SQL_FREERESULT($result);
276
277                 // Load final template
278                 loadTemplate('admin_list_payouts', false, $OUT);
279         } else {
280                 // No payout requests are sent so far
281                 displayMessage('{--ADMIN_PAYOUT_NO_REQUESTS_FOUND--}');
282         }
283 }
284
285 // [EOF]
286 ?>