All resets rewritten, missing svn:properties added
[mailer.git] / inc / modules / member / what-primera.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } elseif (!isMember()) {
44         // User is not logged in
45         redirectToIndexMemberOnlyModule();
46 }
47
48 // Add description as navigation point
49 addMenuDescription('member', __FILE__);
50
51 if ((!isExtensionActive('primera')) && (!isAdmin())) {
52         // Extension 'primera' is not activated
53         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('primera'));
54         return;
55 } // END - if
56
57 // Check if the admin has entered the data
58 if ((getConfig('primera_api_name') == '') || (getConfig('primera_api_md5') == '')) {
59         // Something important is missing...
60         loadTemplate('admin_settings_saved', false, getMessage('MEMBER_PRIMERA_API_DATA_MISSING'));
61         return;
62 } // END - if
63
64 // Init the content array and points
65 $content = array();
66 $points = false;
67
68 // Is the mode set (payout only)
69 if (!isGetRequestParameterSet('mode')) {
70         // Get referal id
71         $content['refid'] = getConfig(('primera_refid'));
72
73         // Is there an id?
74         if ((!empty($content['primera_nickname'])) && (!isGetRequestParameterSet('mode'))) {
75                 // Then use an other "mode"
76                 setGetRequestParameter('mode', 'list');
77
78                 // And load all rows!
79                 $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",
80                         array(getMemberId()), __FILE__, __LINE__);
81
82                 // Load all rows
83                 $content['rows'] = ''; $SW = 2;
84                 while ($data = SQL_FETCHARRAY($result)) {
85                         // Prepare data for output
86                         $rowContent = array(
87                                 'primera_timestamp' => generateDateTime($data['primera_timestamp'], 2),
88                                 'points'            => translateComma($data['primera_amount']),
89                                 'primera_account'   => SQL_ESCAPE($data['primera_account']),
90                                 'status'            => PRIMERA_TRANSFER_STATUS($data['primera_type']),
91                                 'raw_type'          => strtolower($data['primera_type']),
92                                 'sw'                => $SW,
93                         );
94
95                         // Load row template
96                         $content['rows'] .= loadTemplate('member_primera_mode_list_row', true, $rowContent);
97                         $SW = 3 - $SW;
98                 }
99
100                 // Free result
101                 SQL_FREERESULT($result);
102         } else {
103                 // Mode pay
104                 setGetRequestParameter('mode', 'pay');
105         }
106 } // END - if
107
108 if (getRequestParameter('mode') == 'pay') {
109         // Get total points and check if the user can request a payout
110         $points = countSumTotalData(getMemberId(), 'user_points', 'points') - countSumTotalData(getMemberId(), 'user_data', 'used_points');
111
112         // No dots here...
113         $points = explode('.', $points);
114         $points = bigintval($points[0]);
115
116         // Remove the registration fee
117         $points = $points - getConfig('points_register');
118
119         // Is this enougth for a payout?
120         if ($points < getConfig('primera_min_payout')) {
121                 // No, then abort here
122                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_PRIMERA_MIN_PAYOUT_PAYOUT'));
123                 return;
124         }
125
126         // Add points to content array
127         $content['points'] = $points;
128 } else {
129         // Invalid mode!
130         loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_PRIMERA_MODE_INVALID', getRequestParameter('mode')));
131         return;
132 }
133
134 // Is the formular sent?
135 if ((isFormSent()) && (isGetRequestParameterSet('mode'))) {
136         // Check input data depending on the mode and execute the requested mode
137         switch (getRequestParameter('mode')) {
138                 case 'pay': // Payout this exchange -> Primus
139                         // Is the user id and password set?
140                         if (!isPostRequestParameterSet(('primera_nickname'))) {
141                                 // Nothing entered in Primus nickname
142                                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_PRIMERA_EMPTY_USERNAME'));
143                         } elseif (!isPostRequestParameterSet(('primera_password'))) {
144                                 // Nothing entered in Primera password
145                                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_PRIMERA_EMPTY_PASSWORD'));
146                         } elseif (!isPostRequestParameterSet(('amount'))) {
147                                 // Nothing entered in amount
148                                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_PRIMERA_EMPTY_AMOUNT'));
149                         } elseif (postRequestParameter('amount') != bigintval(postRequestParameter('amount'))) {
150                                 // Only numbers in amount!
151                                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_PRIMERA_INVALID_AMOUNT'));
152                         } elseif (postRequestParameter('amount') < getConfig('primera_min_payout')) {
153                                 // Not enougth entered!
154                                 loadTemplate('admin_settings_saved', false, getMessage('MEMBER_PRIMERA_AMOUNT_SMALLER_MIN'));
155                         } elseif (postRequestParameter('amount') > $points) {
156                                 // Not enougth points left!
157                                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('MEMBER_PRIMERA_PAYOUT_POINTS_DEPLETED'), bigintval(postRequestParameter('amount')), bigintval($points)));
158                         } else {
159                                 // All is fine here so do the payout
160                                 $success = executePrimeraPayout(postRequestParameter('primera_nickname'), md5(postRequestParameter('primera_password')), postRequestParameter('amount'));
161                                 if ($success) {
162                                         // Default is locked!
163                                         $locked = true;
164
165                                         // Shall I 'pay' the referal points imidiately?
166                                         if (getConfig('ref_payout') == '0') {
167                                                 // Yes, 'pay' it now
168                                                 $locked = false;
169                                         } // END - if
170
171                                         // Remove points from account
172                                         subtractPoints('primera_payout', getMemberId(), postRequestParameter('amount'));
173
174                                         // Update primera nickname
175                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `primera_nickname`=%s WHERE `userid`=%s LIMIT 1",
176                                                 array(postRequestParameter('primera_nickname'), getMemberId()), __FILE__, __LINE__);
177
178                                         // All done!
179                                         loadTemplate('admin_settings_saved', false, getMessage('MEMBER_PRIMERA_PAYOUT_DONE'));
180                                         return;
181                                 } 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')) {
182                                         // Wrong login data
183                                         loadTemplate('admin_settings_saved', false, GET_PRIMERA_ERROR_MESSAGE());
184                                 } else {
185                                         // Something went wrong
186                                         loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_PRIMERA_PAYOUT_FAILED', GET_PRIMERA_ERROR_MESSAGE()));
187                                 }
188                         }
189                         break;
190
191                 default: // Invalid mode!
192                         logDebugMessage(__FILE__, __LINE__, sprintf("Invalid mode %s detected.", getRequestParameter('mode')));
193                         loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_PRIMERA_MODE_INVALID', getRequestParameter('mode')));
194                         return;
195         }
196 } // END - if
197
198 // Prepare mode for template name
199 $mode = sprintf("member_primera_mode_%s", getRequestParameter('mode'));
200
201 // Load the template
202 loadTemplate($mode, false, $content);
203
204 //
205 ?>