Even more rewrites/fixes from EL branch (please report any broken part after you...
[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[] = getMessage('SPONSOR_NO_GENDER_SELECTED');
64         } // END - if
65
66         if (!isPostRequestParameterSet('surname')) {
67                 // Surname is empty
68                 $FORM_ERRORS[] = getMessage('SPONSOR_SURNAME_IS_EMPTY');
69         } // END - if
70
71         if (!isPostRequestParameterSet('family')) {
72                 // Surname is empty
73                 $FORM_ERRORS[] = getMessage('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[] = getMessage('SPONSOR_COMPANY_IS_EMPTY');
82                 } elseif (!isPostRequestParameterSet('tax_ident')) {
83                         // Tax ident number name is empty
84                         $FORM_ERRORS[] = getMessage('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[] = getMessage('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[] = getMessage('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[] = getMessage('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[] = getMessage('SPONSOR_COUNTRY_IS_EMPTY');
106         } elseif (strlen(postRequestParameter('country')) != 2) {
107                 // Country code is invalid
108                 $FORM_ERRORS[] = getMessage('SPONSOR_COUNTRY_IS_INVALID');
109         }
110
111         // 3. ZIP code
112         if (!isPostRequestParameterSet('zip')) {
113                 // ZIP code is empty
114                 $FORM_ERRORS[] = getMessage('SPONSOR_ZIP_IS_EMPTY');
115         } elseif (bigintval(postRequestParameter('zip')) != postRequestParameter('zip')) {
116                 // ZIP is invalid
117                 $FORM_ERRORS[] = getMessage('SPONSOR_ZIP_IS_INVALID');
118                 setPostRequestParameter('zip', '');
119         }
120
121         // 4. City
122         if (!isPostRequestParameterSet('city')) {
123                 // City is empty
124                 $FORM_ERRORS[] = getMessage('SPONSOR_CITY_IS_EMPTY');
125         } // END - if
126
127         // 5. Phone number
128         if (!isPostRequestParameterSet('phone')) {
129                 // City is empty
130                 $FORM_ERRORS[] = getMessage('SPONSOR_PHONE_IS_EMPTY');
131         } // END - if
132
133         // 6. Homepage URL
134         if (!isPostRequestParameterSet('url')) {
135                 // Homepage URL is empty
136                 $FORM_ERRORS[] = getMessage('SPONSOR_URL_IS_EMPTY');
137         } elseif (!isUrlValid(postRequestParameter('url'))) {
138                 // Homepage URL is invalid
139                 $FORM_ERRORS[] = getMessage('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[] = getMessage('SPONSOR_EMAIL_IS_INVALID');
147         } elseif (isSponsorRegisteredWithEmail(postRequestParameter('email'))) {
148                 // Email already found in database!
149                 $FORM_ERRORS[] = getMessage('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[] = getMessage('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[] = getMessage('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[] = getMessage('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[] = getMessage('SPONSOR_PASSWORDS_MISMATCH');
176                 } else {
177                         if (!isPostRequestParameterSet('pass1')) {
178                                 // Password 1 is empty
179                                 $FORM_ERRORS[] = getMessage('SPONSOR_PASSWORD1_EMPTY');
180                         }
181
182                         if (!isPostRequestParameterSet('pass2')) {
183                                 // Password 2 is empty
184                                 $FORM_ERRORS[] = getMessage('SPONSOR_PASSWORD2_EMPTY');
185                         }
186                 }
187         } elseif (strlen(postRequestParameter('pass1')) < getConfig('pass_len')) {
188                 // Password is to short!
189                 $FORM_ERRORS[] = getMessage('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[] = getMessage('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 `id`, `pay_name`, `pay_rate`, `pay_currency`, `pay_min_count`
283 FROM `{?_MYSQL_PREFIX?}_sponsor_paytypes`
284 ORDER BY `pay_name`", __FILE__, __LINE__);
285         if (SQL_NUMROWS($result) > 0) {
286                 // Load all types...
287                 // @TODO Swap this HTML code to templates
288                 $OUT = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"300\" class=\"left right top\">\n";
289                 $SW = 2;
290                 while ($content = SQL_FETCHARRAY($result)) {
291                         // Prepare content for output
292                         $content['sw']  = $SW;
293                         $content['res'] = translateComma($content['pay_rate'] * $content['pay_min_count']);
294
295                         // Load row template and switch color
296                         $OUT .= loadTemplate('guest_sponsor_payment_row', false, $content);
297                         $SW = 3 - $SW;
298                 }
299                 $OUT .= "</table>\n";
300
301                 // Remember the list in table
302                 $content['payment_list'] = $OUT;
303
304                 // Init a lot array elements
305                 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) {
306                         $content[$entry]   = '';
307                 } // END - foreach
308
309                 // Check for invalid entries
310                 if (count($FORM_ERRORS) > 0) {
311                         // Some found... :-(
312                         foreach (array('company','position','tax_ident','surname','family','street_nr1','street_nr2','country','zip','city','phone','fix','cell','email','url') as $entry) {
313                                 $content[$entry]    = secureString(postRequestParameter($entry));
314                         } // END - foreach
315
316                         // Init gender
317                         foreach (array('m', 'f', 'c') as $gender) {
318                                 $content['gender_' . $gender] = '';
319                         } // END - foreach
320
321                         // Check for gender selection
322                         $content['gender_' . strtolower(postRequestParameter('gender'))] = ' selected="selected"';
323
324                         // Init receive selection
325                         $content['rec_y'] = '';
326                         $content['rec_n'] = '';
327
328                         // Check for receive_warnings
329                         $content['rec_' . strtolower(postRequestParameter('receive_warnings'))] = ' selected="selected"';
330                         $OUT = '';
331
332                         // Errors found?
333                         if (count($FORM_ERRORS) > 0) {
334                                 $OUT = loadTemplate('admin_settings_saved', true, getMessage('SPONSOR_REGISTER_SOMETHING_MISSING');
335                                 $OUT .= '<ol>';
336                                 foreach ($FORM_ERRORS as $error) {
337                                         $OUT .= '<li>' . $error . '</li>';
338                                 } // END - foreach
339
340                                 $OUT .= '</ol>';
341                         } // END - if
342
343                         $content['form_errors'] = $OUT;
344                         $content['refid']       = bigintval(postRequestParameter('refid'));
345                 } // END - if
346
347                 // Prepare referal id
348                 $content['refid'] = determineReferalId();
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, getMessage('SPONSOR_PAYMENT_LIST_IS_EMPTY'));
355         }
356
357         // Free memory
358         SQL_FREERESULT($result);
359 }
360
361 // [EOF]
362 ?>