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