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