2a1322dbee11f5106ae0d3a870aa66bec457b8d9
[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  * 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         // Are there some entries?
73         if (fetchUserData(getMemberId())) {
74                 // Fetch id
75                 $content['primera_nickname'] = getUserData('primera_nickname');
76         } // END - if
77
78         // Is there an id?
79         if ((!empty($content['primera_nickname'])) && (!isGetRequestElementSet('mode'))) {
80                 // Then use an other "mode"
81                 setRequestGetElement('mode', 'list');
82
83                 // And load all rows!
84                 $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",
85                         array(getMemberId()), __FILE__, __LINE__);
86
87                 // Load all rows
88                 $content['rows'] = ''; $SW = 2;
89                 while ($data = SQL_FETCHARRAY($result)) {
90                         // Prepare data for output
91                         $rowContent = array(
92                                 'primera_timestamp' => generateDateTime($data['primera_timestamp'], 2),
93                                 'points'            => translateComma($data['primera_amount']),
94                                 'primera_account'   => SQL_ESCAPE($data['primera_account']),
95                                 'status'            => PRIMERA_TRANSFER_STATUS($data['primera_type']),
96                                 'raw_type'          => strtolower($data['primera_type']),
97                                 'sw'                => $SW,
98                         );
99
100                         // Load row template
101                         $content['rows'] .= loadTemplate('member_primera_mode_list_row', true, $rowContent);
102                         $SW = 3 - $SW;
103                 }
104
105                 // Free result
106                 SQL_FREERESULT($result);
107         } else {
108                 // Mode pay
109                 setRequestGetElement('mode', 'pay');
110         }
111 } // END - if
112
113 if (getRequestElement('mode') == 'pay') {
114         // Get total points and check if the user can request a payout
115         $points = countSumTotalData(getMemberId(), 'user_points', 'points') - countSumTotalData(getMemberId(), 'user_data', 'used_points');
116
117         // No dots here...
118         $points = explode('.', $points);
119         $points = bigintval($points[0]);
120
121         // Remove the registration fee
122         $points = $points - getConfig('points_register');
123
124         // Is this enougth for a payout?
125         if ($points < getConfig('primera_min_payout')) {
126                 // No, then abort here
127                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_MIN_PAYOUT_PAYOUT'), translateComma(getConfig('primera_min_payout'))));
128                 return;
129         }
130
131         // Add points to content array
132         $content['points']     = $points;
133         $content['min_points'] = translateComma(getConfig('primera_min_payout'));
134
135         // Get Primera id
136         $content['primera_nickname'] = '';
137
138         // Do we have a user account? (should be!)
139         if (fetchUserData(getMemberId())) {
140                 // Fetch id
141                 $content['primera_nickname'] = getUserData('primera_nickname');
142         } // END - if
143 } else {
144         // Invalid mode!
145         loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_MODE_INVALID'), getRequestElement('mode')));
146         return;
147 }
148
149 // Is the formular sent?
150 if ((isFormSent()) && (isGetRequestElementSet('mode'))) {
151         // Check input data depending on the mode and execute the requested mode
152         switch (getRequestElement('mode')) {
153                 case 'pay': // Payout this exchange -> Primus
154                         // Is the user id and password set?
155                         if (!isPostRequestElementSet(('primera_nickname'))) {
156                                 // Nothing entered in Primus nickname
157                                 loadTemplate('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_EMPTY_USERNAME'));
158                         } elseif (!isPostRequestElementSet(('primera_password'))) {
159                                 // Nothing entered in Primera password
160                                 loadTemplate('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_EMPTY_PASSWORD'));
161                         } elseif (!isPostRequestElementSet(('amount'))) {
162                                 // Nothing entered in amount
163                                 loadTemplate('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_EMPTY_AMOUNT'));
164                         } elseif (postRequestElement('amount') != bigintval(postRequestElement('amount'))) {
165                                 // Only numbers in amount!
166                                 loadTemplate('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_INVALID_AMOUNT'));
167                         } elseif (postRequestElement('amount') < getConfig('primera_min_payout')) {
168                                 // Not enougth entered!
169                                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_AMOUNT_SMALLER_MIN'), getConfig(('primera_min_payout'))));
170                         } elseif (postRequestElement('amount') > $points) {
171                                 // Not enougth points left!
172                                 loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_PAYOUT_POINTS_DEPLETED'), bigintval(postRequestElement('amount')), bigintval($points)));
173                         } else {
174                                 // All is fine here so do the payout
175                                 $success = executePrimeraPayout(postRequestElement('primera_nickname'), md5(postRequestElement('primera_password')), postRequestElement('amount'));
176                                 if ($success) {
177                                         // Default is locked!
178                                         $locked = true;
179
180                                         // Shall I 'pay' the referal points imidiately?
181                                         if (getConfig('ref_payout') == '0') {
182                                                 // Yes, 'pay' it now
183                                                 $locked = false;
184                                         } // END - if
185
186                                         // Remove points from account
187                                         subtractPoints('primera_payout', getMemberId(), postRequestElement('amount'));
188
189                                         // Update primera nickname
190                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `primera_nickname`=%s WHERE `userid`=%s LIMIT 1",
191                                                 array(postRequestElement('primera_nickname'), getMemberId()), __FILE__, __LINE__);
192
193                                         // All done!
194                                         loadTemplate('admin_settings_saved', false, getMessage('PRIMERA_MEMBER_PAYOUT_DONE'));
195                                         return;
196                                 } 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')) {
197                                         // Wrong login data
198                                         loadTemplate('admin_settings_saved', false, GET_PRIMERA_ERROR_MESSAGE());
199                                 } else {
200                                         // Something went wrong
201                                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_PAYOUT_FAILED'), GET_PRIMERA_ERROR_MESSAGE()));
202                                 }
203                         }
204                         break;
205
206                 default: // Invalid mode!
207                         logDebugMessage(__FILE__, __LINE__, sprintf("Invalid mode %s detected.", getRequestElement('mode')));
208                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('PRIMERA_MEMBER_MODE_INVALID'), getRequestElement('mode')));
209                         return;
210         }
211 } // END - if
212
213 // Prepare mode for template name
214 $mode = sprintf("member_primera_mode_%s", getRequestElement('mode'));
215
216 // Load the template
217 loadTemplate($mode, false, $content);
218
219 //
220 ?>