2 /************************************************************************
3 * MXChange v0.2.1 Start: 09/09/2008 *
4 * ================ Last change: 09/09/2008 *
6 * -------------------------------------------------------------------- *
7 * File : what-primera.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Wernis requests *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Auszahlungsanfragen *
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 // User is not logged in
44 redirectToIndexMemberOnlyModule();
47 // Add description as navigation point
48 addMenuDescription('member', __FILE__);
50 if ((!isExtensionActive('primera')) && (!isAdmin())) {
51 // Extension 'primera' is not activated
52 loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('primera'));
56 // Check if the admin has entered the data
57 if ((getConfig('primera_api_name') == '') || (getConfig('primera_api_md5') == '')) {
58 // Something important is missing...
59 loadTemplate('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_API_DATA_MISSING'));
63 // Init the content array and points
67 // Is the mode set (payout only)
68 if (!isGetRequestElementSet('mode')) {
70 $content['refid'] = getConfig(('primera_refid'));
73 $result = SQL_QUERY_ESC("SELECT `primera_userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
74 array(getUserId()), __FILE__, __LINE__);
76 // Are there some entries?
77 if (SQL_NUMROWS($result) == 1) {
79 list($content['primera_nickname']) = SQL_FETCHROW($result);
83 SQL_FREERESULT($result);
86 if ((!empty($content['primera_nickname'])) && (!isGetRequestElementSet('mode'))) {
87 // Then use an other "mode"
88 setRequestGetElement('mode', 'list');
91 $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",
92 array(getUserId()), __FILE__, __LINE__);
95 $content['rows'] = ''; $SW = 2;
96 while ($data = SQL_FETCHARRAY($result)) {
97 // Prepare data for output
99 'stamp' => generateDateTime($data['primera_timestamp'], 2),
100 'points' => translateComma($data['primera_amount']),
101 'acc' => SQL_ESCAPE($data['primera_account']),
102 'status' => PRIMERA_TRANSFER_STATUS($data['primera_type']),
103 'raw_type' => strtolower($data['primera_type']),
108 $content['rows'] .= loadTemplate('member_primera_mode_list_row', true, $rowContent);
113 SQL_FREERESULT($result);
116 setRequestGetElement('mode', 'pay');
120 if (getRequestElement('mode') == 'pay') {
121 // Get total points and check if the user can request a payout
122 $points = countSumTotalData(getUserId(), 'user_points', 'points') - countSumTotalData(getUserId(), 'user_data', 'used_points');
125 $points = explode('.', $points);
126 $points = bigintval($points[0]);
128 // Remove the registration fee
129 $points = $points - getConfig('points_register');
131 // Is this enougth for a payout?
132 if ($points < getConfig('primera_min_payout')) {
133 // No, then abort here
134 loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_MIN_PAYOUT_PAYOUT'), translateComma(getConfig('primera_min_payout'))));
138 // Add points to content array
139 $content['points'] = $points;
140 $content['min_points'] = translateComma(getConfig('primera_min_payout'));
143 $content['primera_nickname'] = '';
144 $result = SQL_QUERY_ESC("SELECT `primera_userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
145 array(getUserId()), __FILE__, __LINE__);
147 // Are there some entries?
148 if (SQL_NUMROWS($result) == 1) {
150 list($content['primera_nickname']) = SQL_FETCHROW($result);
154 SQL_FREERESULT($result);
157 loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_MODE_INVALID'), getRequestElement('mode')));
161 // Is the formular sent?
162 if ((isFormSent()) && (isGetRequestElementSet('mode'))) {
163 // Check input data depending on the mode and execute the requested mode
164 switch (getRequestElement('mode')) {
165 case 'pay': // Payout this exchange -> Primus
166 // Is the user ID and password set?
167 if (!isPostRequestElementSet(('primera_nickname'))) {
168 // Nothing entered in Primus nickname
169 loadTemplate('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_EMPTY_USERNAME'));
170 } elseif (!isPostRequestElementSet(('primera_password'))) {
171 // Nothing entered in Primera password
172 loadTemplate('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_EMPTY_PASSWORD'));
173 } elseif (!isPostRequestElementSet(('amount'))) {
174 // Nothing entered in amount
175 loadTemplate('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_EMPTY_AMOUNT'));
176 } elseif (postRequestElement('amount') != bigintval(postRequestElement('amount'))) {
177 // Only numbers in amount!
178 loadTemplate('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_INVALID_AMOUNT'));
179 } elseif (postRequestElement('amount') < getConfig('primera_min_payout')) {
180 // Not enougth entered!
181 loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_AMOUNT_SMALLER_MIN'), getConfig(('primera_min_payout'))));
182 } elseif (postRequestElement('amount') > $points) {
183 // Not enougth points left!
184 loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_PAYOUT_POINTS_DEPLETED'), bigintval(postRequestElement('amount')), bigintval($points)));
186 // All is fine here so do the payout
187 $success = executePrimeraPayout(postRequestElement('primera_nickname'), md5(postRequestElement('primera_password')), postRequestElement('amount'));
189 // Default is locked!
192 // Shall I 'pay' the referal points imidiately?
193 if (getConfig('ref_payout') == 0) {
198 // Remove points from account
199 subtractPoints('primera_payout', getUserId(), postRequestElement('amount'));
201 // Update primera nickname
202 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `primera_userid`=%s WHERE `userid`=%s LIMIT 1",
203 array(postRequestElement('primera_nickname'), getUserId()), __FILE__, __LINE__);
206 loadTemplate('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_PAYOUT_DONE'));
208 } 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')) {
210 loadTemplate('admin_settings_saved', false, GET_PRIMERA_ERROR_MESSAGE());
212 // Something went wrong
213 loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_PAYOUT_FAILED'), GET_PRIMERA_ERROR_MESSAGE()));
218 default: // Invalid mode!
219 logDebugMessage(__FILE__, __LINE__, sprintf("Invalid mode %s detected.", getRequestElement('mode')));
220 loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_MODE_INVALID'), getRequestElement('mode')));
225 // Prepare mode for template name
226 $mode = sprintf("member_primera_mode_%s", getRequestElement('mode'));
229 loadTemplate($mode, false, $content);