]> git.mxchange.org Git - mailer.git/blob - inc/libs/register_functions.php
This query got broken on rewrite phase
[mailer.git] / inc / libs / register_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 07/10/2004 *
4  * ===================                          Last change: 07/10/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : register_functions.php                           *
8  * -------------------------------------------------------------------- *
9  * Short description : Special functions for register extension         *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Spezielle Funktion fuer register-Erweiterung     *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 //
44 function ifRequiredRegisterFieldsAreSet (&$array) {
45         // By default all is fine
46         $ret = true;
47         foreach ($array as $key => $value) {
48                 // Check all fields that must register
49                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_must_register` WHERE `field_name`='%s' AND `field_required`='Y' LIMIT 1",
50                         array($key), __FUNCTION__, __LINE__);
51
52                 // Entry found?
53                 if (SQL_NUMROWS($result) == 1) {
54                         // Check if extension country is not found (you have to enter the 2-chars long country code) or
55                         // if extensions is present check if country code was selected
56                         //         01              2         21    12             3         32    234     5      54    4               43    34                      4    4      5      5432    2      3                      3210
57                         $country = ((!isExtensionActive('country')) || ((isExtensionActive('country')) && (((empty($value)) && ($key == 'cntry')) || (($key == 'country_code') && (!empty($value)))) && (!empty($array['country_code']))));
58                         if ((empty($value)) && ($country === false)) {
59                                 // Required field not set
60                                 $array[$key] = '!';
61                                 $ret = false;
62                         } // END - if
63                 } // END - if
64
65                 // Free result
66                 SQL_FREERESULT($result);
67         } // END - foreach
68
69         // Return result
70         return $ret;
71 }
72
73 // Generates a 'category table' for the registration form
74 function registerGenerateCategoryTable ($mode) {
75         // Init output
76         $OUT = '';
77
78         // Guests are mostly not interested in how many members has
79         // choosen an individual category
80         $whereStatement = "WHERE `visible`='Y' ";
81
82         // Admins are allowed to see every category...
83         if (isAdmin()) $whereStatement = '';
84
85         // Look for categories
86         $result = SQL_QUERY('SELECT `id`,`cat`,`visible` FROM `{?_MYSQL_PREFIX?}_cats` ' . $whereStatement . ' ORDER BY `sort` ASC',
87                 __FUNCTION__, __LINE__);
88
89         if (!SQL_HASZERONUMS($result)) {
90                 // List alle visible modules (or all to the admin)
91                 $OUT .= '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
92                 while ($content = SQL_FETCHARRAY($result)) {
93                         // Prepare array for the template
94                         $content['default_yes'] = '';
95                         $content['default_no']  = '';
96
97                         // Mark categories
98                         if ((postRequestElement('cat', $content['id']) == 'Y') || ((isRegisterDefaultEnabled()) && (!isPostRequestElementSet('cat', $content['id'])))) {
99                                 $content['default_yes'] = ' checked="checked"';
100                         } else {
101                                 $content['default_no']  = ' checked="checked"';
102                         }
103
104                         // Load template and switch color
105                         $OUT .= loadTemplate('guest_cat_row', true, $content);
106                 } // END - while
107                 $OUT .= '</table>';
108
109                 // Free memory
110                 SQL_FREERESULT($result);
111         } else {
112                 // No categories setted up so far...
113                 $OUT .= displayMessage('{--NO_CATEGORIES_VISIBLE--}', true);
114         }
115
116         // Return generated HTML code
117         return $OUT;
118 }
119
120 // Outputs a 'failed message'
121 function registerOutputFailedMessage ($messageId, $extra='') {
122         if (empty($messageId)) {
123                 outputHtml('<div class="bad">' . $extra . '</div>');
124         } else {
125                 outputHtml('<div class="bad">{--' . $messageId . '--}' . $extra . '</div>');
126         }
127 }
128
129 // Checks wether the registration data is complete
130 function isRegistrationDataComplete () {
131         // Init elements
132         $GLOBALS['registration_ip_timeout']     = false;
133         $GLOBALS['registration_short_password'] = false;
134         $GLOBALS['registration_selected_cats']  = '0';
135
136         // Default is okay
137         $isOkay = true;
138
139         // First we only check the submitted data then we continue... :)
140         //
141         // Did he agree to our Terms Of Usage?
142         if (postRequestElement('agree') != 'Y') {
143                 setPostRequestElement('agree', '!');
144                 $isOkay = false;
145         } // END - if
146
147         // Did he enter a valid email address? (we really don't care about
148         // that, he has to click on a confirmation link :P )
149         if ((!isPostRequestElementSet('email')) || (!isEmailValid(postRequestElement('email')))) {
150                 setPostRequestElement('email', '!');
151                 $isOkay = false;
152         } // END - if
153
154         // And what about surname and family's name?
155         if (!isPostRequestElementSet('surname')) {
156                 setPostRequestElement('surname', '!');
157                 $isOkay = false;
158         } // END - if
159         if (!isPostRequestElementSet('family')) {
160                 setPostRequestElement('family', '!');
161                 $isOkay = false;
162         } // END - if
163
164         // Get temporary array for modification
165         $postArray = postRequestArray();
166
167         // Check for required fields
168         $isOkay = ($isOkay && ifRequiredRegisterFieldsAreSet($postArray));
169
170         // Set it back in request
171         setPostRequestArray($postArray);
172
173         // Are both passwords zero length?
174         if ((strlen(postRequestElement('pass1')) == 0) && (strlen(postRequestElement('pass2')) == 0) && ($isOkay === true)) {
175                 // Is the extension 'register' newer or equal 0.5.5?
176                 if ((isExtensionInstalledAndNewer('register', '0.5.5')) && (isRegisterGeneratePasswordEmptyEnabled())) {
177                         // Generate a random password
178                         $randomPassword = generatePassword();
179
180                         // Set it in both entries
181                         setPostRequestElement('pass1', $randomPassword);
182                         setPostRequestElement('pass2', $randomPassword);
183                 } else {
184                         // Not allowed or no recent extension version
185                         setPostRequestElement('pass1', '!');
186                         setPostRequestElement('pass2', '!');
187
188                         // ... which is both not okay
189                         $isOkay = false;
190                 }
191         } // END - if
192
193         // Did he enter his password twice?
194         if (((!isPostRequestElementSet('pass1')) || (!isPostRequestElementSet('pass2'))) || ((postRequestElement('pass1') != postRequestElement('pass2')) && (isPostRequestElementSet('pass1')) && (isPostRequestElementSet('pass2')))) {
195                 if ((postRequestElement('pass1') != postRequestElement('pass2')) && (isPostRequestElementSet('pass1')) && (isPostRequestElementSet('pass2'))) {
196                         setPostRequestElement('pass1', '!');
197                         setPostRequestElement('pass2', '!');
198                 } else {
199                         if (!isPostRequestElementSet('pass1')) {
200                                 setPostRequestElement('pass1', '!');
201                         } else {
202                                 setPostRequestElement('pass1', '');
203                         }
204                         if (!isPostRequestElementSet('pass2')) {
205                                 setPostRequestElement('pass2', '!');
206                         } else {
207                                 setPostRequestElement('pass2', '');
208                         }
209                 }
210                 $isOkay = false;
211         } // END - if
212
213         // Is the password long enouth?
214         if ((strlen(postRequestElement('pass1')) < getPassLen()) && ($isOkay === true)) {
215                 $GLOBALS['registration_short_password'] = true;
216                 $isOkay = false;
217         } // END - if
218
219         // Do this check only when no admin is logged in
220         if (is_array(postRequestElement('cat'))) {
221                 // Only continue with array
222                 foreach (postRequestElement('cat') as $id => $answer) {
223                         // Is this category choosen?
224                         if ($answer == 'Y') {
225                                 $GLOBALS['registration_selected_cats']++;
226                         } // END - if
227                 } // END - foreach
228         } // END - if
229
230         // Enougth categories selected?
231         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isOkay='.intval($isOkay).',selected='.$GLOBALS['registration_selected_cats'].'/'.getLeastCats());
232         $isOkay = (($isOkay) && ($GLOBALS['registration_selected_cats'] >= getLeastCats()));
233
234         if ((postRequestElement('email') != '!') && (isCheckDoubleEmailEnabled())) {
235                 // Does the email address already exists in our database?
236                 if ((isEmailTaken(postRequestElement('email'))) && (!isAdmin())) {
237                         setPostRequestElement('email', '?');
238                         $isOkay = false;
239                 } // END - if
240         } // END - if
241
242         // Check for IP timeout?
243         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isOkay='.intval($isOkay));
244         if ((!isAdmin()) && (getIpTimeout() > 0)) {
245                 // Check his IP number
246                 $GLOBALS['registration_ip_timeout'] = (countSumTotalData(detectRemoteAddr()  , 'user_data', 'userid', 'REMOTE_ADDR', true, ' AND ((UNIX_TIMESTAMP() - `joined`) < {?ip_timeout?} OR (UNIX_TIMESTAMP() - `last_update`) < {?ip_timeout?})') == 1);
247                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isOkay='.intval($isOkay).',timeout='.intval($GLOBALS['registration_ip_timeout']));
248                 $isOkay = (($isOkay) && (!$GLOBALS['registration_ip_timeout']));
249         } // END - if
250
251         // Return result
252         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isOkay='.intval($isOkay));
253         return $isOkay;
254 }
255
256 // Do the registration
257 function doRegistration () {
258         // Generate hash which will be inserted into confirmation mail
259         $hash = generateHash(sha1(
260                 // Get total confirmed, ...
261                 getTotalConfirmedUser() . getEncryptSeparator() .
262                 // ... unconfirmed ...
263                 getTotalUnconfirmedUser() . getEncryptSeparator() .
264                 // ... and locked users
265                 getTotalLockedUser() . getEncryptSeparator() .
266                 postRequestElement('month') . '-' .
267                 postRequestElement('day') . '-' .
268                 postRequestElement('year') . getEncryptSeparator() .
269                 detectServerName() . getEncryptSeparator() .
270                 detectRemoteAddr() . getEncryptSeparator() .
271                 detectUserAgent() . '/' .
272                 getSiteKey() . '/' .
273                 getDateKey() . '/' .
274                 getConfig('CACHE_BUSTER')
275         ));
276
277         // Old way with enterable two-char-code
278         $countryRow = '`country`';
279         $countryData = substr(postRequestElement('cntry'), 0, 2);
280
281         // Add design when extension ext-theme is v0.0.8 or greater
282         // @TODO Rewrite these all to a single filter
283         $GLOBALS['register_sql_columns'] = '';
284         $GLOBALS['register_sql_data']    = '';
285         if (isExtensionInstalledAndNewer('theme', '0.0.8')) {
286                 // Okay, add design here
287                 $GLOBALS['register_sql_columns'] .= ', `curr_theme`';
288                 $GLOBALS['register_sql_data']    .= ", '{%%pipe,getCurrentTheme%%}'";
289         } // END - if
290
291         // Check if I shall disable sending mail to newly registered members out about active/begging rallye
292         //
293         // First comes first: begging rallye
294         if ((isExtensionInstalledAndNewer('beg', '0.2.7')) && (!isBegNewMemberNotifyEnabled())) {
295                 $GLOBALS['register_sql_columns'] .= ', `beg_rallye_enable_notify`, `beg_rallye_disable_notify`';
296                 $GLOBALS['register_sql_data']    .= ', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()';
297         } // END - if
298
299         // Second: active rallye
300         if ((isExtensionActive('bonus')) && (!isBonusNewMemberNotifyEnabled())) {
301                 $GLOBALS['register_sql_columns'] .= ', `bonus_rallye_enable_notify`, `bonus_rallye_disable_notify`';
302                 $GLOBALS['register_sql_data']    .= ', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()';
303         } // END - if
304
305         // Write user data to table
306         if (isExtensionActive('country')) {
307                 // Save with new selectable country code
308                 $countryRow = '`country_code`';
309                 $countryData = bigintval(postRequestElement('country_code'));
310         } // END - if
311
312         // Create user's account...
313         SQL_QUERY_ESC("INSERT INTO
314         `{?_MYSQL_PREFIX?}_user_data`
315 (
316         `gender`,
317         `surname`,
318         `family`,
319         `street_nr`,
320         %s,
321         `zip`,
322         `city`,
323         `email`,
324         `birth_day`,
325         `birth_month`,
326         `birth_year`,
327         `password`,
328         `max_mails`,
329         `receive_mails`,
330         `refid`,
331         `status`,
332         `user_hash`,
333         `REMOTE_ADDR`,
334         `joined`,
335         `last_update`,
336         `ref_payout`
337         ".$GLOBALS['register_sql_columns']."
338 ) VALUES (
339         '%s',
340         '%s',
341         '%s',
342         '%s',
343         '%s',
344         %s,
345         '%s',
346         '%s',
347         %s,
348         %s,
349         %s,
350         '%s',
351         %s,
352         %s,
353         %s,
354         'UNCONFIRMED',
355         '%s',
356         '{%%pipe,detectRemoteAddr%%}',
357         UNIX_TIMESTAMP(),
358         UNIX_TIMESTAMP(),
359         {?ref_payout?}
360         ".$GLOBALS['register_sql_data'].")",
361         array(
362                 $countryRow,
363                 substr(postRequestElement('gender'), 0, 1),
364                 postRequestElement('surname'),
365                 postRequestElement('family'),
366                 postRequestElement('street_nr'),
367                 $countryData,
368                 bigintval(postRequestElement('zip')),
369                 postRequestElement('city'),
370                 postRequestElement('email'),
371                 bigintval(postRequestElement('day')),
372                 bigintval(postRequestElement('month')),
373                 bigintval(postRequestElement('year')),
374                 generateHash(postRequestElement('pass1')),
375                 bigintval(postRequestElement('max_mails')),
376                 bigintval(postRequestElement('max_mails')),
377                 convertZeroToNull(postRequestElement('refid')),
378                 $hash
379         ), __FUNCTION__, __LINE__);
380
381         // Get his userid
382         $userid = bigintval(SQL_INSERTID());
383
384         // Did this work?
385         if ($userid == '0') {
386                 // Something bad happened!
387                 displayMessage('{--USER_NOT_REGISTERED--}');
388
389                 // Stop here
390                 return;
391         } // END - if
392
393         // Shall we reset random refid? Only possible with latest ext-user
394         if (isExtensionInstalledAndNewer('user', '0.3.4')) {
395                 // Reset all accounts, registration is done
396                 SQL_QUERY('UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `rand_confirmed`=0', __FUNCTION__, __LINE__);
397         } // END - if
398
399         // Update referral table
400         updateReferralCounter($userid);
401
402         // Write his welcome-points
403         initReferralSystem();
404         addPointsThroughReferralSystem('register_welcome', $userid, getPointsRegister());
405
406         // Write catgories
407         if ((is_array(postRequestElement('cat'))) && (count(postRequestElement('cat')))) {
408                 foreach (postRequestElement('cat') as $categoryId => $joined) {
409                         if ($joined == 'Y') {
410                                 // Insert category entry
411                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_cats` (`userid`,`cat_id`) VALUES (%s, %s)",
412                                         array(
413                                                 $userid,
414                                                 bigintval($categoryId)
415                                         ), __FUNCTION__, __LINE__);
416                         } // END - if
417                 } // END - foreach
418         } // END - if
419
420         // ... rewrite a zero referral id to the main title
421         if (!isValidUserId(postRequestElement('refid'))) {
422                 setPostRequestElement('refid', getMainTitle());
423         } // END - if
424
425         // Is ZIP code set?
426         if (isPostRequestElementSet('zip')) {
427                 // Prepare data array for the email template
428                 // Start with the gender...
429                 $content = array(
430                         'hash'     => $hash,
431                         'userid'   => $userid,
432                         'gender'   => SQL_ESCAPE(postRequestElement('gender')),
433                         'surname'  => SQL_ESCAPE(postRequestElement('surname')),
434                         'family'   => SQL_ESCAPE(postRequestElement('family')),
435                         'email'    => SQL_ESCAPE(postRequestElement('email')),
436                         'street'   => SQL_ESCAPE(postRequestElement('street_nr')),
437                         'city'     => SQL_ESCAPE(postRequestElement('city')),
438                         'zip'      => bigintval(postRequestElement('zip')),
439                         'country'  => $countryData,
440                         'refid'    => SQL_ESCAPE(postRequestElement('refid')),
441                         'password' => SQL_ESCAPE(postRequestElement('pass1')),
442                 );
443         } else {
444                 // No ZIP code entered
445                 $content = array(
446                         'hash'     => $hash,
447                         'userid'   => $userid,
448                         'gender'   => SQL_ESCAPE(postRequestElement('gender')),
449                         'surname'  => SQL_ESCAPE(postRequestElement('surname')),
450                         'family'   => SQL_ESCAPE(postRequestElement('family')),
451                         'email'    => SQL_ESCAPE(postRequestElement('email')),
452                         'street'   => SQL_ESCAPE(postRequestElement('street_nr')),
453                         'city'     => SQL_ESCAPE(postRequestElement('city')),
454                         'zip'      => '',
455                         'country'  => $countryData,
456                         'refid'    => SQL_ESCAPE(postRequestElement('refid')),
457                         'password' => SQL_ESCAPE(postRequestElement('pass1')),
458                 );
459         }
460
461         // Continue with birthday...
462         switch (getLanguage()) {
463                 case 'de':
464                         $content['birthday'] = bigintval(postRequestElement('day')) . '.' . bigintval(postRequestElement('month')) . '.' . bigintval(postRequestElement('year'));
465                         break;
466
467                 default:
468                         $content['birthday'] = bigintval(postRequestElement('month')) . '/' . bigintval(postRequestElement('day')) . '/' . bigintval(postRequestElement('year'));
469                         break;
470         } // END - switch
471
472         // Display information to the user that he got mail and send it away
473         $messageGuest = loadEmailTemplate('guest_register_done', $content, $userid, false);
474
475         // Send mail to user (confirmation link!)
476         sendEmail($userid, '{--GUEST_CONFIRM_LINK_SUBJECT--}', $messageGuest);
477
478         // Send mail to admin
479         sendAdminNotification('{--ADMIN_NEW_ACCOUNT_SUBJECT--}', 'admin_register_done', $content, $userid);
480 }
481
482 //-----------------------------------------------------------------------------
483 //                      Wrapper functions for ext-register
484 //-----------------------------------------------------------------------------
485
486 // Getter for 'display_refid'
487 function getDisplayRefid () {
488         // Is the cache entry set?
489         if (!isset($GLOBALS[__FUNCTION__])) {
490                 // No, so determine it
491                 $GLOBALS[__FUNCTION__] = getConfig('display_refid');
492         } // END - if
493
494         // Return cached entry
495         return $GLOBALS[__FUNCTION__];
496 }
497
498 // Checks wether 'display_refid' is "YES"
499 function isDisplayRefidEnabled () {
500         // Is the cache entry set?
501         if (!isset($GLOBALS[__FUNCTION__])) {
502                 // No, so determine it
503                 $GLOBALS[__FUNCTION__] = (getDisplayRefid() == 'Y');
504         } // END - if
505
506         // Return cached entry
507         return $GLOBALS[__FUNCTION__];
508 }
509
510 // Getter for 'ip_timeout'
511 function getIpTimeout () {
512         // Is the cache entry set?
513         if (!isset($GLOBALS[__FUNCTION__])) {
514                 // No, so determine it
515                 $GLOBALS[__FUNCTION__] = getConfig('ip_timeout');
516         } // END - if
517
518         // Return cached entry
519         return $GLOBALS[__FUNCTION__];
520 }
521
522 // Getter for 'register_default'
523 function getRegisterDefault () {
524         // Is the cache entry set?
525         if (!isset($GLOBALS[__FUNCTION__])) {
526                 // No, so determine it
527                 $GLOBALS[__FUNCTION__] = getConfig('register_default');
528         } // END - if
529
530         // Return cached entry
531         return $GLOBALS[__FUNCTION__];
532 }
533
534 // Checks wether 'register_default' is "YES"
535 function isRegisterDefaultEnabled () {
536         // Is the cache entry set?
537         if (!isset($GLOBALS[__FUNCTION__])) {
538                 // No, so determine it
539                 $GLOBALS[__FUNCTION__] = (getRegisterDefault() == 'Y');
540         } // END - if
541
542         // Return cached entry
543         return $GLOBALS[__FUNCTION__];
544 }
545
546 // Getter for 'register_generate_password_empty'
547 function getRegisterGeneratePasswordEmpty () {
548         // Is the cache entry set?
549         if (!isset($GLOBALS[__FUNCTION__])) {
550                 // No, so determine it
551                 $GLOBALS[__FUNCTION__] = getConfig('register_generate_password_empty');
552         } // END - if
553
554         // Return cached entry
555         return $GLOBALS[__FUNCTION__];
556 }
557
558 // Checks wether 'register_generate_password_empty' is "YES"
559 function isRegisterGeneratePasswordEmptyEnabled () {
560         // Is the cache entry set?
561         if (!isset($GLOBALS[__FUNCTION__])) {
562                 // No, so determine it
563                 $GLOBALS[__FUNCTION__] = (getRegisterGeneratePasswordEmpty() == 'Y');
564         } // END - if
565
566         // Return cached entry
567         return $GLOBALS[__FUNCTION__];
568 }
569
570 // [EOF]
571 ?>