Ref link fixed, nickname fixed, several rewrites, TODOs.txt updated:
[mailer.git] / inc / modules / member / what-primera.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 09/09/2008 *
4  * ================                             Last change: 09/09/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-primera.php                                 *
8  * -------------------------------------------------------------------- *
9  * Short description : Wernis requests                                  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Auszahlungsanfragen                              *
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')) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 } elseif (!IS_MEMBER()) {
44         // User is not logged in
45         redirectToUrl('modules.php?module=index');
46 } elseif ((!EXT_IS_ACTIVE('primera')) && (!IS_ADMIN())) {
47         // Extension 'primera' is not activated
48         addFatalMessage(__FILE__, __LINE__, generateExtensionInactiveNotInstalledMessage('primera'));
49         return;
50 }
51
52 // Add description as navigation point
53 ADD_DESCR('member', __FILE__);
54
55 // Check if the admin has entered the data
56 if ((getConfig('primera_api_name') == '') || (getConfig('primera_api_md5') == '')) {
57         // Something important is missing...
58         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_API_DATA_MISSING'));
59         return;
60 } // END - if
61
62 // Init the content array and points
63 $content = array();
64 $points = false;
65
66 // Is the mode set (payout only)
67 if (!REQUEST_ISSET_GET('mode')) {
68         // Get referal id
69         $content['refid'] = getConfig(('primera_refid'));
70
71         // Get Primus id
72         $result = SQL_QUERY_ESC("SELECT `primera_userid` FROM `{!_MYSQL_PREFIX!}_user_data` WHERE `userid`=%s LIMIT 1",
73                 array(getUserId()), __FILE__, __LINE__);
74
75         // Are there some entries?
76         if (SQL_NUMROWS($result) == 1) {
77                 // Fetch ID
78                 list($content['primera_nickname']) = SQL_FETCHROW($result);
79         } // END - if
80
81         // Free result
82         SQL_FREERESULT($result);
83
84         // Is there an ID?
85         if ((!empty($content['primera_nickname'])) && (!REQUEST_ISSET_GET('mode'))) {
86                 // Then use an other "mode"
87                 REQUEST_SET_GET('mode', 'list');
88
89                 // And load all rows!
90                 $result = SQL_QUERY_ESC("SELECT `id`,`primera_account`,`primera_amount`,`primera_timestamp`,`primera_type` FROM `{!_MYSQL_PREFIX!}_user_primera` WHERE `userid` = %s ORDER BY `primera_timestamp` DESC",
91                         array(getUserId()), __FILE__, __LINE__);
92
93                 // Load all rows
94                 $content['rows'] = ''; $SW = 2;
95                 while ($data = SQL_FETCHARRAY($result)) {
96                         // Prepare data for output
97                         $rowContent = array(
98                                 'stamp'         => generateDateTime($data['primera_timestamp'], '2'),
99                                 'points'                => translateComma($data['primera_amount']),
100                                 'acc'           => SQL_ESCAPE($data['primera_account']),
101                                 'status'                => PRIMERA_TRANSFER_STATUS($data['primera_type']),
102                                 'raw_type'      => strtolower($data['primera_type']),
103                                 'sw'                    => $SW,
104                         );
105
106                         // Load row template
107                         $content['rows'] .= LOAD_TEMPLATE('member_primera_mode_list_row', true, $rowContent);
108                         $SW = 3 - $SW;
109                 }
110
111                 // Free result
112                 SQL_FREERESULT($result);
113         } else {
114                 // Mode pay
115                 REQUEST_SET_GET('mode', 'pay');
116         }
117 } // END - if
118
119 if (REQUEST_GET('mode') == 'pay') {
120         // Get total points and check if the user can request a payout
121         $points = GET_TOTAL_DATA(getUserId(), 'user_points', 'points') - GET_TOTAL_DATA(getUserId(), 'user_data', 'used_points');
122
123         // No dots here...
124         $points = explode('.', $points);
125         $points = bigintval($points[0]);
126
127         // Remove the registration fee
128         $points = $points - getConfig('points_register');
129
130         // Is this enougth for a payout?
131         if ($points < getConfig('primera_min_payout')) {
132                 // No, then abort here
133                 LOAD_TEMPLATE('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_MIN_PAYOUT_PAYOUT'), translateComma(getConfig('primera_min_payout'))));
134                 return;
135         }
136
137         // Add points to content array
138         $content['points']     = $points;
139         $content['min_points'] = translateComma(getConfig('primera_min_payout'));
140
141         // Get Primera id
142         $content['primera_nickname'] = '';
143         $result = SQL_QUERY_ESC("SELECT `primera_userid` FROM `{!_MYSQL_PREFIX!}_user_data` WHERE `userid`=%s LIMIT 1",
144                 array(getUserId()), __FILE__, __LINE__);
145
146         // Are there some entries?
147         if (SQL_NUMROWS($result) == 1) {
148                 // Fetch ID
149                 list($content['primera_nickname']) = SQL_FETCHROW($result);
150         }
151
152         // Free result
153         SQL_FREERESULT($result);
154 } else {
155         // Invalid mode!
156         LOAD_TEMPLATE('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_MODE_INVALID'), REQUEST_GET('mode')));
157         return;
158 }
159
160 // Is the formular sent?
161 if ((isFormSent()) && (REQUEST_ISSET_GET('mode'))) {
162         // Check input data depending on the mode and execute the requested mode
163         switch (REQUEST_GET('mode')) {
164                 case 'pay': // Payout this exchange -> Primus
165                         // Is the user ID and password set?
166                         if (!REQUEST_ISSET_POST(('primera_nickname'))) {
167                                 // Nothing entered in Primus nickname
168                                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_EMPTY_USERNAME'));
169                         } elseif (!REQUEST_ISSET_POST(('primera_password'))) {
170                                 // Nothing entered in Primera password
171                                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_EMPTY_PASSWORD'));
172                         } elseif (!REQUEST_ISSET_POST(('amount'))) {
173                                 // Nothing entered in amount
174                                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_EMPTY_AMOUNT'));
175                         } elseif (REQUEST_POST('amount') != bigintval(REQUEST_POST('amount'))) {
176                                 // Only numbers in amount!
177                                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_INVALID_AMOUNT'));
178                         } elseif (REQUEST_POST('amount') < getConfig('primera_min_payout')) {
179                                 // Not enougth entered!
180                                 LOAD_TEMPLATE('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_AMOUNT_SMALLER_MIN'), getConfig(('primera_min_payout'))));
181                         } elseif (REQUEST_POST('amount') > $points) {
182                                 // Not enougth points left!
183                                 LOAD_TEMPLATE('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_PAYOUT_POINTS_DEPLETED'), bigintval(REQUEST_POST('amount')), bigintval($points)));
184                         } else {
185                                 // All is fine here so do the payout
186                                 $success = PRIMERA_EXECUTE_PAYOUT(REQUEST_POST('primera_nickname'), md5(REQUEST_POST('primera_password')), REQUEST_POST('amount'));
187                                 if ($success) {
188                                         // Default is locked!
189                                         $locked = true;
190
191                                         // Shall I 'pay' the referal points imidiately?
192                                         if (getConfig('ref_payout') == '0') {
193                                                 // Yes, 'pay' it now
194                                                 $locked = false;
195                                         } // END - if
196
197                                         // Remove points from account
198                                         SUB_POINTS('primera_payout', getUserId(), REQUEST_POST('amount'));
199
200                                         // Update primera nickname
201                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET `primera_userid`=%s WHERE `userid`=%s LIMIT 1",
202                                                 array(REQUEST_POST('primera_nickname'), getUserId()), __FILE__, __LINE__);
203
204                                         // All done!
205                                         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_PAYOUT_DONE'));
206                                         return;
207                                 } elseif ((GET_PRIMERA_ERROR_CODE() == 'user_failed') || (GET_PRIMERA_ERROR_CODE() == 'own_failed') || (GET_PRIMERA_ERROR_CODE() == 'amount_failed') || (GET_PRIMERA_ERROR_CODE() == 'api_amount_failed')) {
208                                         // Wrong login data
209                                         LOAD_TEMPLATE('admin_settings_saved', false, GET_PRIMERA_ERROR_MESSAGE());
210                                 } else {
211                                         // Something went wrong
212                                         LOAD_TEMPLATE('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_PAYOUT_FAILED'), GET_PRIMERA_ERROR_MESSAGE()));
213                                 }
214                         }
215                         break;
216
217                 default: // Invalid mode!
218                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Invalid mode %s detected.", REQUEST_GET('mode')));
219                         LOAD_TEMPLATE('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_MODE_INVALID'), REQUEST_GET('mode')));
220                         return;
221         }
222 } // END - if
223
224 // Prepare mode for template name
225 $mode = sprintf("member_primera_mode_%s", REQUEST_GET('mode'));
226
227 // Load the template
228 LOAD_TEMPLATE($mode, false, $content);
229
230 //
231 ?>