Renamed function so it might be more understandable
[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  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
20  * For more information visit: http://www.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')) {
40         die();
41 } elseif (!isMember()) {
42         // User is not logged in
43         redirectToIndexMemberOnlyModule();
44 }
45
46 // Add description as navigation point
47 addYouAreHereLink('member', __FILE__);
48
49 if ((!isExtensionActive('primera')) && (!isAdmin())) {
50         // Extension 'primera' is not activated
51         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('primera'));
52         return;
53 } // END - if
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         loadTemplate('admin_settings_saved', false, '{--MEMBER_PRIMERA_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 (!isGetRequestParameterSet('mode')) {
68         // Is there an id?
69         if ((!empty($content['primera_nickname'])) && (!isGetRequestParameterSet('mode'))) {
70                 // Then use an other "mode"
71                 setGetRequestParameter('mode', 'list');
72
73                 // And load all rows!
74                 $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",
75                         array(getMemberId()), __FILE__, __LINE__);
76
77                 // Load all rows
78                 $content['rows'] = '';
79                 while ($data = SQL_FETCHARRAY($result)) {
80                         // Prepare data for output
81                         $rowContent['primera_timestamp'] = generateDateTime($data['primera_timestamp'], 2);
82
83                         // Load row template
84                         $content['rows'] .= loadTemplate('member_primera_mode_list_row', true, $rowContent);
85                 } // END - while
86
87                 // Free result
88                 SQL_FREERESULT($result);
89         } else {
90                 // Mode pay
91                 setGetRequestParameter('mode', 'pay');
92         }
93 } // END - if
94
95 if (getRequestParameter('mode') == 'pay') {
96         // Get total points and check if the user can request a payout
97         $points = getTotalPoints(getMemberId());
98
99         // No dots here...
100         $points = explode('.', $points);
101         $points = bigintval($points[0]);
102
103         // Remove the registration fee
104         $points = $points - getPointsRegister();
105
106         // Is this enougth for a payout?
107         if ($points < getConfig('primera_min_payout')) {
108                 // No, then abort here
109                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PRIMERA_MIN_PAYOUT_PAYOUT--}');
110                 return;
111         } // END - if
112
113         // Add points to content array
114         $content['points'] = $points;
115 } else {
116         // Invalid mode!
117         loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_PRIMERA_MODE_INVALID', getRequestParameter('mode')));
118         return;
119 }
120
121 // Is the formular sent?
122 if ((isFormSent()) && (isGetRequestParameterSet('mode'))) {
123         // Check input data depending on the mode and execute the requested mode
124         switch (getRequestParameter('mode')) {
125                 case 'pay': // Payout this exchange -> Primus
126                         // Is the user id and password set?
127                         if (!isPostRequestParameterSet('primera_nickname')) {
128                                 // Nothing entered in Primus nickname
129                                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PRIMERA_EMPTY_USERNAME--}');
130                         } elseif (!isPostRequestParameterSet('primera_password')) {
131                                 // Nothing entered in Primera password
132                                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PRIMERA_EMPTY_PASSWORD--}');
133                         } elseif (!isPostRequestParameterSet('amount')) {
134                                 // Nothing entered in amount
135                                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PRIMERA_EMPTY_AMOUNT--}');
136                         } elseif (postRequestParameter('amount') != bigintval(postRequestParameter('amount'))) {
137                                 // Only numbers in amount!
138                                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PRIMERA_INVALID_AMOUNT--}');
139                         } elseif (postRequestParameter('amount') < getConfig('primera_min_payout')) {
140                                 // Not enougth entered!
141                                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PRIMERA_AMOUNT_SMALLER_MIN--}');
142                         } elseif (postRequestParameter('amount') > $points) {
143                                 // Not enougth points left!
144                                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('MEMBER_PRIMERA_PAYOUT_POINTS_DEPLETED'), bigintval(postRequestParameter('amount')), bigintval($points)));
145                         } else {
146                                 // All is fine here so do the payout
147                                 $success = executePrimeraPayout(postRequestParameter('primera_nickname'), md5(postRequestParameter('primera_password')), postRequestParameter('amount'));
148                                 if ($success) {
149                                         // Default is locked!
150                                         $locked = true;
151
152                                         // Shall I 'pay' the referal points imidiately?
153                                         if (getConfig('ref_payout') == '0') {
154                                                 // Yes, 'pay' it now
155                                                 $locked = false;
156                                         } // END - if
157
158                                         // Remove points from account
159                                         subtractPoints('primera_payout', getMemberId(), postRequestParameter('amount'));
160
161                                         // Update primera nickname
162                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `primera_nickname`=%s WHERE `userid`=%s LIMIT 1",
163                                                 array(postRequestParameter('primera_nickname'), getMemberId()), __FILE__, __LINE__);
164
165                                         // All done!
166                                         loadTemplate('admin_settings_saved', false, '{--MEMBER_PRIMERA_PAYOUT_DONE--}');
167                                         return;
168                                 } 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')) {
169                                         // Wrong login data
170                                         loadTemplate('admin_settings_saved', false, GET_PRIMERA_ERROR_MESSAGE());
171                                 } else {
172                                         // Something went wrong
173                                         loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_PRIMERA_PAYOUT_FAILED', GET_PRIMERA_ERROR_MESSAGE()));
174                                 }
175                         }
176                         break;
177
178                 default: // Invalid mode!
179                         logDebugMessage(__FILE__, __LINE__, sprintf("Invalid mode %s detected.", getRequestParameter('mode')));
180                         loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_PRIMERA_MODE_INVALID', getRequestParameter('mode')));
181                         return;
182         }
183 } // END - if
184
185 // Prepare mode for template name
186 $mode = sprintf("member_primera_mode_%s", getRequestParameter('mode'));
187
188 // Load the template
189 loadTemplate($mode, false, $content);
190
191 //
192 ?>