Final fixes for SQL_ALTER_TABLE() wrapper, INSERT INTO queries are still not wrapped...
[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 - 2008 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         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
42         require($INC);
43 } elseif (!IS_MEMBER()) {
44         // User is not logged in
45         LOAD_URL("modules.php?module=index");
46 } elseif ((!EXT_IS_ACTIVE("primera")) && (!IS_ADMIN())) {
47         // Extension "primera" is not activated
48         addFatalMessage(__FILE__, __LINE__, getMessage('EXTENSION_PROBLEM_EXT_INACTIVE'), "primera");
49         return;
50 }
51
52 // Add description as navigation point
53 ADD_DESCR("member", __FILE__);
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         LOAD_TEMPLATE("admin_settings_saved", false, getMessage('PRIMERA_MEMBER_API_DATA_MISSING'));
59         return;
60 }
61
62 // Init the content array and points
63 $content = array(); $points = false;
64
65 // Is the mode set (payout only)
66 if (!REQUEST_ISSET_GET(('mode'))) {
67         // Get referal id
68         $content['refid'] = getConfig(('primera_refid'));
69
70         // Get Primus id
71         $result = SQL_QUERY_ESC("SELECT primera_userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
72                 array(getUserId()), __FILE__, __LINE__);
73
74         // Are there some entries?
75         if (SQL_NUMROWS($result) == 1) {
76                 // Fetch ID
77                 list($content['primera_nickname']) = SQL_FETCHROW($result);
78         } // END - if
79
80         // Free result
81         SQL_FREERESULT($result);
82
83         // Is there an ID?
84         if ((!empty($content['primera_nickname'])) && (!REQUEST_ISSET_GET(('mode')))) {
85                 // Then use an other "mode"
86                 REQUEST_SET_GET('mode', "list");
87
88                 // And load all rows!
89                 $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",
90                         array(getUserId()), __FILE__, __LINE__);
91
92                 // Load all rows
93                 $content['rows'] = ""; $SW = 2;
94                 while ($data = SQL_FETCHARRAY($result)) {
95                         // Prepare data for output
96                         $rowContent = array(
97                                 'stamp'         => MAKE_DATETIME($data['primera_timestamp'], "2"),
98                                 'points'                => TRANSLATE_COMMA($data['primera_amount']),
99                                 'acc'           => SQL_ESCAPE($data['primera_account']),
100                                 'status'                => PRIMERA_TRANSFER_STATUS($data['primera_type']),
101                                 'raw_type'      => strtolower($data['primera_type']),
102                                 'sw'                    => $SW,
103                         );
104
105                         // Load row template
106                         $content['rows'] .= LOAD_TEMPLATE("member_primera_mode_list_row", true, $rowContent);
107                         $SW = 3 - $SW;
108                 }
109
110                 // Free result
111                 SQL_FREERESULT($result);
112         } else {
113                 // Mode pay
114                 REQUEST_SET_GET('mode', "pay");
115         }
116 } // END - if
117 if (REQUEST_GET('mode') == "pay") {
118         // Get total points and check if the user can request a payout
119         $points = GET_TOTAL_DATA(getUserId(), "user_points", "points") - GET_TOTAL_DATA(getUserId(), "user_data", "used_points");
120
121         // No dots here...
122         $points = explode(".", $points);
123         $points = bigintval($points[0]);
124
125         // Remove the registration fee
126         $points = $points - getConfig('points_register');
127
128         // Is this enougth for a payout?
129         if ($points < getConfig('primera_min_payout')) {
130                 // No, then abort here
131                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(getMessage('PRIMERA_MEMBER_MIN_PAYOUT_PAYOUT'), TRANSLATE_COMMA(getConfig('primera_min_payout'))));
132                 return;
133         }
134
135         // Add points to content array
136         $content['points']              = $points;
137         $content['min_points']  = TRANSLATE_COMMA(getConfig('primera_min_payout'));
138
139         // Get Primera id
140         $content['primera_nickname'] = "";
141         $result = SQL_QUERY_ESC("SELECT primera_userid FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
142                 array(getUserId()), __FILE__, __LINE__);
143
144         // Are there some entries?
145         if (SQL_NUMROWS($result) == 1) {
146                 // Fetch ID
147                 list($content['primera_nickname']) = SQL_FETCHROW($result);
148         }
149
150         // Free result
151         SQL_FREERESULT($result);
152 } else {
153         // Invalid mode!
154         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(getMessage('PRIMERA_MEMBER_MODE_INVALID'), REQUEST_GET(('mode'))));
155         return;
156 }
157
158 // Is the formular sent?
159 if ((IS_FORM_SENT()) && (REQUEST_ISSET_GET(('mode')))) {
160         // Check input data depending on the mode and execute the requested mode
161         switch (REQUEST_GET('mode')) {
162                 case "pay": // Payout this exchange -> Primus
163                         // Is the user ID and password set?
164                         if (!REQUEST_ISSET_POST(('primera_nickname'))) {
165                                 // Nothing entered in Primus nickname
166                                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('PRIMERA_MEMBER_EMPTY_USERNAME'));
167                         } elseif (!REQUEST_ISSET_POST(('primera_password'))) {
168                                 // Nothing entered in Primera password
169                                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('PRIMERA_MEMBER_EMPTY_PASSWORD'));
170                         } elseif (!REQUEST_ISSET_POST(('amount'))) {
171                                 // Nothing entered in amount
172                                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('PRIMERA_MEMBER_EMPTY_AMOUNT'));
173                         } elseif (REQUEST_POST('amount') != bigintval(REQUEST_POST('amount'))) {
174                                 // Only numbers in amount!
175                                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('PRIMERA_MEMBER_INVALID_AMOUNT'));
176                         } elseif (REQUEST_POST('amount') < getConfig('primera_min_payout')) {
177                                 // Not enougth entered!
178                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(getMessage('PRIMERA_MEMBER_AMOUNT_SMALLER_MIN'), getConfig(('primera_min_payout'))));
179                         } elseif (REQUEST_POST('amount') > $points) {
180                                 // Not enougth points left!
181                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(getMessage('PRIMERA_MEMBER_PAYOUT_POINTS_DEPLETED'), bigintval(REQUEST_POST('amount')), bigintval($points)));
182                         } else {
183                                 // All is fine here so do the payout
184                                 $success = PRIMERA_EXECUTE_PAYOUT(REQUEST_POST('primera_nickname'), md5(REQUEST_POST('primera_password')), REQUEST_POST('amount'));
185                                 if ($success) {
186                                         // Default is locked!
187                                         $locked = true;
188
189                                         // Shall I "pay" the referal points imidiately?
190                                         if (getConfig('ref_payout') == "0") {
191                                                 // Yes, "pay" it now
192                                                 $locked = false;
193                                         } // END - if
194
195                                         // Remove points from account
196                                         SUB_POINTS("primera_payout", getUserId(), REQUEST_POST('amount'));
197
198                                         // Update primera nickname
199                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET `primera_userid`=%s WHERE userid=%s LIMIT 1",
200                                                 array(REQUEST_POST('primera_nickname'), getUserId()), __FILE__, __LINE__);
201
202                                         // All done!
203                                         LOAD_TEMPLATE("admin_settings_saved", false, getMessage('PRIMERA_MEMBER_PAYOUT_DONE'));
204                                         return;
205                                 } 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")) {
206                                         // Wrong login data
207                                         LOAD_TEMPLATE("admin_settings_saved", false, GET_PRIMERA_ERROR_MESSAGE());
208                                 } else {
209                                         // Something went wrong
210                                         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(getMessage('PRIMERA_MEMBER_PAYOUT_FAILED'), GET_PRIMERA_ERROR_MESSAGE()));
211                                 }
212                         }
213                         break;
214
215                 default: // Invalid mode!
216                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Invalid mode %s detected.", REQUEST_GET('mode')));
217                         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(getMessage('PRIMERA_MEMBER_MODE_INVALID'), REQUEST_GET(('mode'))));
218                         return;
219         }
220 } // END - if
221
222 // Prepare mode for template name
223 $mode = sprintf("member_primera_mode_%s", REQUEST_GET(('mode')));
224
225 // Load the template
226 LOAD_TEMPLATE($mode, false, $content);
227
228 //
229 ?>