4088ab3b1ad618560546706f280c8a5f930d0f8e
[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 - 2009 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         die();
42 } elseif (!isMember()) {
43         // User is not logged in
44         redirectToIndexMemberOnlyModule();
45 }
46
47 // Add description as navigation point
48 addMenuDescription('member', __FILE__);
49
50 if ((!isExtensionActive('primera')) && (!isAdmin())) {
51         // Extension 'primera' is not activated
52         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('primera'));
53         return;
54 } // END - if
55
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'));
60         return;
61 } // END - if
62
63 // Init the content array and points
64 $content = array();
65 $points = false;
66
67 // Is the mode set (payout only)
68 if (!isGetRequestElementSet('mode')) {
69         // Get referal id
70         $content['refid'] = getConfig(('primera_refid'));
71
72         // Get Primus id
73         $result = SQL_QUERY_ESC("SELECT `primera_userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
74                 array(getUserId()), __FILE__, __LINE__);
75
76         // Are there some entries?
77         if (SQL_NUMROWS($result) == 1) {
78                 // Fetch ID
79                 list($content['primera_nickname']) = SQL_FETCHROW($result);
80         } // END - if
81
82         // Free result
83         SQL_FREERESULT($result);
84
85         // Is there an ID?
86         if ((!empty($content['primera_nickname'])) && (!isGetRequestElementSet('mode'))) {
87                 // Then use an other "mode"
88                 setRequestGetElement('mode', 'list');
89
90                 // And load all rows!
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__);
93
94                 // Load all rows
95                 $content['rows'] = ''; $SW = 2;
96                 while ($data = SQL_FETCHARRAY($result)) {
97                         // Prepare data for output
98                         $rowContent = array(
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']),
104                                 'sw'                    => $SW,
105                         );
106
107                         // Load row template
108                         $content['rows'] .= loadTemplate('member_primera_mode_list_row', true, $rowContent);
109                         $SW = 3 - $SW;
110                 }
111
112                 // Free result
113                 SQL_FREERESULT($result);
114         } else {
115                 // Mode pay
116                 setRequestGetElement('mode', 'pay');
117         }
118 } // END - if
119
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');
123
124         // No dots here...
125         $points = explode('.', $points);
126         $points = bigintval($points[0]);
127
128         // Remove the registration fee
129         $points = $points - getConfig('points_register');
130
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'))));
135                 return;
136         }
137
138         // Add points to content array
139         $content['points']     = $points;
140         $content['min_points'] = translateComma(getConfig('primera_min_payout'));
141
142         // Get Primera id
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__);
146
147         // Are there some entries?
148         if (SQL_NUMROWS($result) == 1) {
149                 // Fetch ID
150                 list($content['primera_nickname']) = SQL_FETCHROW($result);
151         }
152
153         // Free result
154         SQL_FREERESULT($result);
155 } else {
156         // Invalid mode!
157         loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_MODE_INVALID'), getRequestElement('mode')));
158         return;
159 }
160
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)));
185                         } else {
186                                 // All is fine here so do the payout
187                                 $success = executePrimeraPayout(postRequestElement('primera_nickname'), md5(postRequestElement('primera_password')), postRequestElement('amount'));
188                                 if ($success) {
189                                         // Default is locked!
190                                         $locked = true;
191
192                                         // Shall I 'pay' the referal points imidiately?
193                                         if (getConfig('ref_payout') == '0') {
194                                                 // Yes, 'pay' it now
195                                                 $locked = false;
196                                         } // END - if
197
198                                         // Remove points from account
199                                         subtractPoints('primera_payout', getUserId(), postRequestElement('amount'));
200
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__);
204
205                                         // All done!
206                                         loadTemplate('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_PAYOUT_DONE'));
207                                         return;
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')) {
209                                         // Wrong login data
210                                         loadTemplate('admin_settings_saved', false, GET_PRIMERA_ERROR_MESSAGE());
211                                 } else {
212                                         // Something went wrong
213                                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_PAYOUT_FAILED'), GET_PRIMERA_ERROR_MESSAGE()));
214                                 }
215                         }
216                         break;
217
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')));
221                         return;
222         }
223 } // END - if
224
225 // Prepare mode for template name
226 $mode = sprintf("member_primera_mode_%s", getRequestElement('mode'));
227
228 // Load the template
229 loadTemplate($mode, false, $content);
230
231 //
232 ?>