Next wave of lesser getMessage() usage and more EL
[mailer.git] / inc / modules / guest / what-sponsor_reg.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 06/10/2005 *
4  * ===================                          Last change: 05/18/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-sponsor_reg.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : Sponsor's registration form                      *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Anmeldeformular fuer Sponsoren                   *
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  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 }
44
45 // Add description as navigation point
46 addMenuDescription('guest', __FILE__);
47
48 if ((!isExtensionActive('sponsor'))) {
49         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('sponsor'));
50         return;
51 } // END - if
52
53 // Create array for form errors (= missing data)
54 $FORM_ERRORS = array();
55
56 if (isFormSent()) {
57         //
58         // Check submitted form data
59         //
60         // 1. Salutation / Surname / family name
61         if (!isPostRequestParameterSet('gender')) {
62                 // Surname is empty
63                 $FORM_ERRORS[] = '{--SPONSOR_NO_GENDER_SELECTED--}';
64         } // END - if
65
66         if (!isPostRequestParameterSet('surname')) {
67                 // Surname is empty
68                 $FORM_ERRORS[] = '{--SPONSOR_SURNAME_IS_EMPTY--}';
69         } // END - if
70
71         if (!isPostRequestParameterSet('family')) {
72                 // Surname is empty
73                 $FORM_ERRORS[] = '{--SPONSOR_FAMILY_IS_EMPTY--}';
74         } // END - if
75
76         // 2. Company name
77         // 012                          3         32    23                         4           43    3                         4          4321    12                         3         32    2                          3           3210
78         if (((!isPostRequestParameterSet('company')) && ((isPostRequestParameterSet('tax_ident')) || (isPostRequestParameterSet('position')))) || ((isPostRequestParameterSet('company')) && (!isPostRequestParameterSet('tax_ident')))) {
79                 if (!isPostRequestParameterSet('company')) {
80                         // Company name is empty
81                         $FORM_ERRORS[] = '{--SPONSOR_COMPANY_IS_EMPTY--}';
82                 } elseif (!isPostRequestParameterSet('tax_ident')) {
83                         // Tax ident number name is empty
84                         $FORM_ERRORS[] = '{--SPONSOR_TAX_IDENT_IS_EMPTY--}';
85                 }
86
87                 if (!isPostRequestParameterSet('position')) {
88                         // Not fatal but not nice: position in company is empty
89                         $FORM_ERRORS[] = '{--SPONSOR_POSITION_IS_EMPTY--}';
90                 } // END - if
91         } // END - if
92
93         // 3. Street and number
94         if (!isPostRequestParameterSet('street_nr1')) {
95                 // Street name and house number are empty
96                 $FORM_ERRORS[] = '{--SPONSOR_STREET_NR1_IS_EMPTY--}';
97         } elseif ((isPostRequestParameterSet('street_nr2')) && (!isPostRequestParameterSet('street_nr1'))) {
98                 // 1st line for street is empty, but 2nd line not
99                 $FORM_ERRORS[] = '{--SPONSOR_STREET_NR1_IS_EMPTY_2_NOT--}';
100         }
101
102         // 4. Country code
103         if (!isPostRequestParameterSet('country')) {
104                 // Country code is empty
105                 $FORM_ERRORS[] = '{--kSPONSOR_COUNTRY_IS_EMPTY--}';
106         } elseif (strlen(postRequestParameter('country')) != 2) {
107                 // Country code is invalid
108                 $FORM_ERRORS[] = '{--SPONSOR_COUNTRY_IS_INVALID--}';
109         }
110
111         // 3. ZIP code
112         if (!isPostRequestParameterSet('zip')) {
113                 // ZIP code is empty
114                 $FORM_ERRORS[] = '{--SPONSOR_ZIP_IS_EMPTY--}';
115         } elseif (bigintval(postRequestParameter('zip')) != postRequestParameter('zip')) {
116                 // ZIP is invalid
117                 $FORM_ERRORS[] = '{--SPONSOR_ZIP_IS_INVALID--}';
118                 setPostRequestParameter('zip', '');
119         }
120
121         // 4. City
122         if (!isPostRequestParameterSet('city')) {
123                 // City is empty
124                 $FORM_ERRORS[] = '{--SPONSOR_CITY_IS_EMPTY--}';
125         } // END - if
126
127         // 5. Phone number
128         if (!isPostRequestParameterSet('phone')) {
129                 // City is empty
130                 $FORM_ERRORS[] = '{--SPONSOR_PHONE_IS_EMPTY--}';
131         } // END - if
132
133         // 6. Homepage URL
134         if (!isPostRequestParameterSet('url')) {
135                 // Homepage URL is empty
136                 $FORM_ERRORS[] = '{--SPONSOR_URL_IS_EMPTY--}';
137         } elseif (!isUrlValid(postRequestParameter('url'))) {
138                 // Homepage URL is invalid
139                 $FORM_ERRORS[] = '{--SPONSOR_URL_IS_INVALID--}';
140                 setPostRequestParameter('url', '');
141         }
142
143         // 7. Light validation of email address
144         if ((!isPostRequestParameterSet('email')) || (postRequestParameter('email') == '@')) {
145                 // Email is invalid/empty
146                 $FORM_ERRORS[] = '{--SPONSOR_EMAIL_IS_INVALID--}';
147         } elseif (isSponsorRegisteredWithEmail(postRequestParameter('email'))) {
148                 // Email already found in database!
149                 $FORM_ERRORS[] = '{--SPONSOR_EMAIL_IS_ALREADY_REGISTERED--}';
150                 setPostRequestParameter('email', '');
151         }
152
153         // 8. Pay type selected?
154         if (!isPostRequestParameterSet('pay_type')) {
155                 // Not pay type selected
156                 $FORM_ERRORS[] = '{--SPONSOR_NO_PAYTYPE_SELECTED--}';
157         } // END - if
158
159         // 9. Interval of mails
160         if (!isPostRequestParameterSet('warning_interval')) {
161                 // No warning interval selected
162                 $FORM_ERRORS[] = '{--SPONSOR_NO_WARNING_INTERVAL_SELECTED--}';
163         } // END - if
164
165         // 10. Mail notifications disabled/enabled
166         if (!isPostRequestParameterSet('receive_warnings')) {
167                 // Option not selected!
168                 $FORM_ERRORS[] = '{--SPONSOR_NO_RECEIVE_WARNINGS_SELECTED--}';
169         } // END - if
170
171         // Did he enter his password twice?
172         if (((!isPostRequestParameterSet('pass1')) || (!isPostRequestParameterSet('pass2'))) || ((postRequestParameter('pass1') != postRequestParameter('pass2')) && (isPostRequestParameterSet('pass1')) && (isPostRequestParameterSet('pass2')))) {
173                 if ((postRequestParameter('pass1') != postRequestParameter('pass2')) && (isPostRequestParameterSet('pass1')) && (isPostRequestParameterSet('pass2'))) {
174                         // Passwords missmatch
175                         $FORM_ERRORS[] = '{--SPONSOR_PASSWORDS_MISMATCH--}';
176                 } else {
177                         if (!isPostRequestParameterSet('pass1')) {
178                                 // Password 1 is empty
179                                 $FORM_ERRORS[] = '{--SPONSOR_PASSWORD1_EMPTY--}';
180                         }
181
182                         if (!isPostRequestParameterSet('pass2')) {
183                                 // Password 2 is empty
184                                 $FORM_ERRORS[] = '{--SPONSOR_PASSWORD2_EMPTY--}';
185                         }
186                 }
187         } elseif (strlen(postRequestParameter('pass1')) < getConfig('pass_len')) {
188                 // Password is to short!
189                 $FORM_ERRORS[] = '{--SPONSOR_PASSWORD_TOO_SHORT--}';
190         }
191
192         // Check if he has accepted the terms&conditions
193         if (!isPostRequestParameterSet('terms')) {
194                 // Homepage URL is empty
195                 $FORM_ERRORS[] = '{--SPONSOR_TERMS_NOT_ACCEPTED--}';
196         } // END - if
197
198         // If there is something wrong/missing stop registration
199         if (count($FORM_ERRORS) > 0) unsetPostRequestParameter('ok');
200 }
201
202 if ((isFormSent()) && (count($FORM_ERRORS) == 0)) {
203         // Generate message array
204         $messageArray = array(
205                 'failed' => getMessage('SPONSOR_REGISTRATION_FAILED'),
206                 'added'  => getMessage('SPONSOR_REGISTRATION_COMPLETED'),
207         );
208
209         // Calulate points
210         $result = SQL_QUERY_ESC("SELECT (`pay_rate` * `pay_min_count`) AS points, `pay_min_count` AS pay, `pay_currency` AS curr
211 FROM `{?_MYSQL_PREFIX?}_sponsor_paytypes`
212 WHERE `id`='%s' LIMIT 1",
213                 array(postRequestParameter('pay_type')), __FILE__, __LINE__);
214         list($points, $pay, $curr) = SQL_FETCHROW($result);
215
216         // Free memory
217         SQL_FREERESULT($result);
218
219         // Add points to array
220         postRequestParameter('points_amount', $points);
221         postRequestParameter('points_used'  , '0.00000');
222         postRequestParameter('last_pay'     , $pay);
223         postRequestParameter('last_curr'    , $curr);
224
225         // Register sponsor but never ever update here!
226         $STATUS = handlSponsorRequest(postRequestArray(), true, $messageArray, true);
227
228         // Check the status of the registration process
229         switch ($STATUS) {
230                 case 'added': // Sponsor successfully added with account status = UNCONFIRMED!
231                         // Check for his id number
232                         $result = SQL_QUERY_ESC("SELECT `id`, `hash` FROM `{?_MYSQL_PREFIX?}_sponsor_data` WHERE `email`='%s' LIMIT 1",
233                                 array(postRequestParameter('email')), __FILE__, __LINE__);
234                         if (SQL_NUMROWS($result) == 1) {
235                                 // id found so let's load it for the confirmation email
236                                 list($id, $hash) = SQL_FETCHROW($result);
237
238                                 // Prepare data for the email template
239                                 $content['id']        = $id;
240                                 $content['hash']      = $hash;
241                                 $content['email']     = secureString(postRequestParameter('email'));
242                                 $content['surname']   = secureString(postRequestParameter('surname'));
243                                 $content['family']    = secureString(postRequestParameter('family'));
244                                 $content['gender']    = translateGender(postRequestParameter('gender'));
245                                 $content['timestamp'] = generateDateTime(time(), 0);
246                                 $content['password']  = secureString(postRequestParameter('pass1'));
247
248                                 // Generate email and send it to the new sponsor
249                                 $message = loadEmailTemplate('sponsor_confirm', $hash);
250                                 sendEmail(postRequestParameter('email'), getMessage('SPONSOR_PLEASE_CONFIRM_SUBJ'), $message);
251
252                                 // Send mail to admin
253                                 sendAdminNotification(getMessage('ADMIN_NEW_SPONSOR'), 'admin_sponsor_reg', $hash);
254
255                                 // Output message: DONE
256                                 $message = $messageArray['added'];
257                         } else {
258                                 // Sponsor account not found???
259                                 $message = getMaskedMessage('SPONSOR_EMAIL_404', postRequestParameter('email'));
260                         }
261
262                         // Free memory
263                         SQL_FREERESULT($result);
264                         break;
265
266                 default:
267                         logDebugMessage(__FILE__, __LINE__, sprintf("Unknown status %s detected.", $STATUS));
268                         if (!isAdmin()) {
269                                 // Message for testing admin
270                                 $message = getMaskedMessage('ADMIN_SPONSOR_UNKNOWN_STATUS', $STATUS);
271                         } else {
272                                 // Message for the guest
273                                 $message = getMaskedMessage('SPONSOR_UNKNOWN_STATUS', $STATUS);
274                         }
275                         break;
276         }
277
278         // Display message
279         loadTemplate('admin_settings_saved', false, $message);
280 } else {
281         // Check for payment types
282         $result = SQL_QUERY('SELECT
283         `id`, `pay_name`, `pay_rate`, `pay_currency`, `pay_min_count`
284 FROM
285         `{?_MYSQL_PREFIX?}_sponsor_paytypes`
286 ORDER BY 
287         pay_name` ASC', __FILE__, __LINE__);
288         if (SQL_NUMROWS($result) > 0) {
289                 // Load all types...
290                 // @TODO Swap this HTML code to templates
291                 $OUT = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"300\" class=\"left right top\">\n";
292                 $SW = 2;
293                 while ($content = SQL_FETCHARRAY($result)) {
294                         // Prepare content for output
295                         $content['sw']  = $SW;
296                         $content['res'] = translateComma($content['pay_rate'] * $content['pay_min_count']);
297
298                         // Load row template and switch color
299                         $OUT .= loadTemplate('guest_sponsor_payment_row', false, $content);
300                         $SW = 3 - $SW;
301                 }
302                 $OUT .= "</table>\n";
303
304                 // Remember the list in table
305                 $content['payment_list'] = $OUT;
306
307                 // Init a lot array elements
308                 foreach (array('company','position','tax_ident','gender_m','gender_f','gender_c','surname','family','street_nr1','street_nr2','country','zip','city','phone','fix','cell','email','url','rec_y','rec_n','form_errors','refid') as $entry) {
309                         $content[$entry]   = '';
310                 } // END - foreach
311
312                 // Check for invalid entries
313                 if (count($FORM_ERRORS) > 0) {
314                         // Some found... :-(
315                         foreach (array('company','position','tax_ident','surname','family','street_nr1','street_nr2','country','zip','city','phone','fix','cell','email','url') as $entry) {
316                                 $content[$entry]    = secureString(postRequestParameter($entry));
317                         } // END - foreach
318
319                         // Init gender
320                         foreach (array('m', 'f', 'c') as $gender) {
321                                 $content['gender_' . $gender] = '';
322                         } // END - foreach
323
324                         // Check for gender selection
325                         $content['gender_' . strtolower(postRequestParameter('gender'))] = ' selected="selected"';
326
327                         // Init receive selection
328                         $content['rec_y'] = '';
329                         $content['rec_n'] = '';
330
331                         // Check for receive_warnings
332                         $content['rec_' . strtolower(postRequestParameter('receive_warnings'))] = ' selected="selected"';
333                         $OUT = '';
334
335                         // Errors found?
336                         if (count($FORM_ERRORS) > 0) {
337                                 $OUT = loadTemplate('admin_settings_saved', true, '{--SPONSOR_REGISTER_SOMETHING_MISSING--}');
338                                 $OUT .= '<ol>';
339                                 foreach ($FORM_ERRORS as $error) {
340                                         $OUT .= '<li>' . $error . '</li>';
341                                 } // END - foreach
342
343                                 $OUT .= '</ol>';
344                         } // END - if
345
346                         $content['form_errors'] = $OUT;
347                         $content['refid']       = bigintval(postRequestParameter('refid'));
348                 } // END - if
349
350                 // Display registration form
351                 loadTemplate('guest_sponsor_reg', false, $content);
352         } else {
353                 // Nothing added so far
354                 loadTemplate('admin_settings_saved', false, '{--SPONSOR_PAYMENT_LIST_IS_EMPTY--}');
355         }
356
357         // Free memory
358         SQL_FREERESULT($result);
359 }
360
361 // [EOF]
362 ?>