2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 07/10/2004 *
4 * =================== Last change: 07/10/2004 *
6 * -------------------------------------------------------------------- *
7 * File : register_functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Special functions for register extension *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Spezielle Funktion fuer register-Erweiterung *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 *
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. *
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. *
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, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
45 function ifRequiredRegisterFieldsAreSet (&$array) {
46 // By default all is fine
48 foreach ($array as $key => $value) {
49 // Check all fields that must register
50 $result = SQL_QUERY("SELECT `id` FROM `{?_MYSQL_PREFIX?}_must_register` WHERE `field_name`='".$key."' AND `field_required`='Y' LIMIT 1",
51 __FUNCTION__, __LINE__);
52 if (SQL_NUMROWS($result) == 1) {
53 // Check if extension country is not found (you have to enter the 2-chars long country code) or
54 // if extensions is present check if country code was selected
55 // 01 2 21 12 3 32 234 5 54 4 43 34 4 4 5 5432 2 3 3210
56 $country = ((!isExtensionActive('country')) || ((isExtensionActive('country')) && (((empty($value)) && ($key == 'cntry')) || (($key == 'country_code') && (!empty($value)))) && (!empty($array['country_code']))));
57 if ((empty($value)) && ($country === false)) {
58 // Required field not set
65 SQL_FREERESULT($result);
72 // Generates a 'category table' for the registration form
73 function registerGenerateCategoryTable ($mode, $return=false) {
76 // Guests are mostly not interested in how many members has
77 // choosen an individual category
78 $AND = "WHERE `visible`='Y' ";
80 // Admins are allowed to see every category...
81 if (isAdmin()) $AND = '';
83 // Look for categories
84 $result = SQL_QUERY("SELECT `id`, `cat`, `visible` FROM `{?_MYSQL_PREFIX?}_cats` ".$AND." ORDER BY `sort` ASC",
85 __FUNCTION__, __LINE__);
87 if (SQL_NUMROWS($result) > 0) {
88 // List alle visible modules (or all to the admin)
90 $OUT .= '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
91 while ($content = SQL_FETCHARRAY($result)) {
92 // Prepare array for the template
95 'cat' => $content['cat'],
98 'id' => $content['id'],
102 if ((postRequestParameter('cat', $content['id']) == 'Y') || ((getConfig('register_default') == 'Y') && (!isPostRequestParameterSet('cat', $content['id'])))) {
103 $content['def_y'] = ' checked="checked"';
105 $content['def_n'] = ' checked="checked"';
108 // Load template and switch color
109 $OUT .= loadTemplate('guest_cat_row', true, $content);
115 SQL_FREERESULT($result);
117 // No categories setted up so far...
118 $OUT .= loadTemplate('admin_settings_saved', true, getMessage('NO_CATEGORIES_VISIBLE'));
121 if ($return === true) {
122 // Return generated HTML code
125 // Output directly (default)
130 // Outputs a 'failed message'
131 function registerOutputFailedMessage ($messageId, $extra='') {
132 if (empty($messageId)) {
133 outputHtml('<div class="register_failed">' . $extra . '</div>');
135 outputHtml('<div class="register_failed">{--' . $messageId . '--}' . $extra . '</div>');
139 // Run a filter for must-fillout fields
140 function FILTER_REGISTER_MUST_FILLOUT ($content) {
141 // Get all fields for output
142 $result = SQL_QUERY("SELECT `field_name`, `field_required` FROM `{?_MYSQL_PREFIX?}_must_register` ORDER BY `id` ASC",
143 __FUNCTION__, __LINE__);
145 // Walk through all entries
146 while ($row = SQL_FETCHARRAY($result)) {
147 // Must the user fill out this element?
149 if ($row['field_required'] == 'Y') $value = '<span class="guest_failed">(*)</span>';
152 $content['must_fillout_'.strtolower($row['field_name']).''] = $value;
156 SQL_FREERESULT($result);
162 // Checks wether the registration data is complete
163 function isRegistrationDataComplete () {
165 $GLOBALS['registration_ip_timeout'] = false;
166 $GLOBALS['registration_short_password'] = false;
167 $GLOBALS['register_selected_cats'] = '0';
172 // First we only check the submitted data then we continue... :)
174 // Did he agree to our Terms Of Usage?
175 if (postRequestParameter('agree') != 'Y') {
176 setPostRequestParameter('agree', '!');
180 // Did he enter a valid email address? (we really don't care about
181 // that, he has to click on a confirmation link :P )
182 if ((!isPostRequestParameterSet('email')) || (!isEmailValid(postRequestParameter('email')))) {
183 setPostRequestParameter('email', '!');
187 // And what about surname and family's name?
188 if (!isPostRequestParameterSet('surname')) {
189 setPostRequestParameter('surname', '!');
192 if (!isPostRequestParameterSet('family')) {
193 setPostRequestParameter('family', '!');
197 // Get temporary array for modification
198 $postArray = postRequestArray();
200 // Check for required fields
201 $isOkay = ($isOkay && ifRequiredRegisterFieldsAreSet($postArray));
203 // Set it back in request
204 setPostRequestArray($postArray);
206 // Did he enter his password twice?
207 if (((!isPostRequestParameterSet('pass1')) || (!isPostRequestParameterSet('pass2'))) || ((postRequestParameter('pass1') != postRequestParameter('pass2')) && (isPostRequestParameterSet('pass1')) && (isPostRequestParameterSet('pass2')))) {
208 if ((postRequestParameter('pass1') != postRequestParameter('pass2')) && (isPostRequestParameterSet('pass1')) && (isPostRequestParameterSet('pass2'))) {
209 setPostRequestParameter('pass1', '!');
210 setPostRequestParameter('pass2', '!');
212 if (!isPostRequestParameterSet('pass1')) { setPostRequestParameter('pass1', '!'); } else { setPostRequestParameter('pass1', ''); }
213 if (!isPostRequestParameterSet('pass2')) { setPostRequestParameter('pass2', '!'); } else { setPostRequestParameter('pass2', ''); }
218 // Is the password long enouth?
219 if ((strlen(postRequestParameter('pass1')) < getConfig('pass_len')) && ($isOkay === true)) {
220 $GLOBALS['registration_short_password'] = true;
224 // Do this check only when no admin is logged in
225 foreach (postRequestParameter('cat') as $id => $answer) {
226 if ($answer == 'Y') $GLOBALS['register_selected_cats']++;
229 // Enougth categories selected?
230 $isOkay = (($isOkay) && ($GLOBALS['register_selected_cats'] >= getConfig('least_cats')));
232 if ((postRequestParameter('email') != '!') && (getConfig('check_double_email') == 'Y')) {
233 // Does the email address already exists in our database?
234 if ((!isAdmin()) && (isEmailTaken(postRequestParameter('email')))) {
235 setPostRequestParameter('email', '?');
240 // Check for IP timeout?
241 if ((!isAdmin()) && (getConfig('ip_timeout') > 0)) {
242 // Check his IP number
243 $GLOBALS['registration_ip_timeout'] = (countSumTotalData(detectRemoteAddr() , 'user_data', 'userid', 'REMOTE_ADDR', true, " AND (`joined` > (UNIX_TIMESTAMP() - {?ip_timeout?}) OR `last_update` > (UNIX_TIMESTAMP() - {?ip_timeout?}))") == 1);
251 // Do the registration
252 function doRegistration () {
253 // Prepapre month and day of birth
254 if (strlen(postRequestParameter('day')) == 1) setPostRequestParameter('day' , '0' . postRequestParameter('day'));
255 if (strlen(postRequestParameter('month')) == 1) setPostRequestParameter('month', '0' . postRequestParameter('month'));
258 // ... confirmed, ...
259 $confirmedUsers = countSumTotalData('CONFIRMED' , 'user_data', 'userid', 'status', true);
260 // ... unconfirmed ...
261 $unconfirmedUsers = countSumTotalData('UNCONFIRMED', 'user_data', 'userid', 'status', true);
262 // ... and locked users!
263 $lockedUsers = countSumTotalData('LOCKED' , 'user_data', 'userid', 'status', true);
265 // Generate hash which will be inserted into confirmation mail
266 $hash = generateHash(sha1(
267 $confirmedUsers . getConfig('ENCRYPT_SEPERATOR') .
268 $unconfirmedUsers . getConfig('ENCRYPT_SEPERATOR') .
269 $lockedUsers . getConfig('ENCRYPT_SEPERATOR') .
270 postRequestParameter('month') . '-' .
271 postRequestParameter('day') . '-' .
272 postRequestParameter('year') . getConfig('ENCRYPT_SEPERATOR') .
273 detectServerName() . getConfig('ENCRYPT_SEPERATOR') .
274 detectRemoteAddr() . getConfig('ENCRYPT_SEPERATOR') .
275 detectUserAgent() . '/' .
276 getConfig('SITE_KEY') . '/' .
277 getConfig('DATE_KEY') . '/' .
278 getConfig('CACHE_BUSTER')
281 // Old way with enterable two-char-code
282 $countryRow = '`country`';
283 $countryData = substr(postRequestParameter('cntry'), 0, 2);
285 // Add design when extension sql_patches is v0.2.7 or greater
286 // @TODO Rewrite these all to a single filter
287 $GLOBALS['register_sql_columns'] = '';
288 $GLOBALS['register_sql_data'] = '';
289 if (isExtensionInstalledAndNewer('theme', '0.0.8')) {
290 // Okay, add design here
291 $GLOBALS['register_sql_columns'] = ', `curr_theme`';
292 $GLOBALS['register_sql_data'] = ", '" . getCurrentTheme() . "'";
295 // Check if I shall disable sending mail to newly registered members out about active/begging rallye
297 // First comes first: begging rallye
298 if (isExtensionInstalledAndNewer('beg', '0.1.7')) {
299 // Okay, shall I disable now?
300 if (getConfig('beg_new_mem_notify') != 'Y') {
301 $GLOBALS['register_sql_columns'] .= ', `beg_ral_notify`, `beg_ral_en_notify`';
302 $GLOBALS['register_sql_data'] .= ', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()';
306 // Second: active rallye
307 if (isExtensionInstalledAndNewer('bonus', '0.7.7')) {
308 // Okay, shall I disable now?
309 if (getConfig('bonus_new_mem_notify') != 'Y') {
310 $GLOBALS['register_sql_columns'] .= ', `bonus_ral_notify`, `bonus_ral_en_notify`';
311 $GLOBALS['register_sql_data'] .= ', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()';
315 // Write user data to table
316 if (isExtensionActive('country')) {
317 // Save with new selectable country code
318 $countryRow = '`country_code`';
319 $countryData = bigintval(postRequestParameter('country_code'));
322 //////////////////////////////
323 // Create user's account... //
324 //////////////////////////////
326 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_data` (gender, surname, family, street_nr,%s, zip, city, email, birth_day, birth_month, birth_year, password, max_mails, receive_mails, refid, status, user_hash, REMOTE_ADDR, joined, last_update".$GLOBALS['register_sql_columns'].")
327 VALUES ('%s','%s','%s','%s','%s',%s,'%s','%s',%s, %s,%s,'%s',%s, %s,'%s','UNCONFIRMED','%s','%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()".$GLOBALS['register_sql_data'].")",
330 substr(postRequestParameter('gender'), 0, 1),
331 postRequestParameter('surname'),
332 postRequestParameter('family'),
333 postRequestParameter('street_nr'),
335 bigintval(postRequestParameter('zip')),
336 postRequestParameter('city'),
337 postRequestParameter('email'),
338 bigintval(postRequestParameter('day')),
339 bigintval(postRequestParameter('month')),
340 bigintval(postRequestParameter('year')),
341 generateHash(postRequestParameter('pass1')),
342 bigintval(postRequestParameter('max_mails')),
343 bigintval(postRequestParameter('max_mails')),
344 bigintval(postRequestParameter('refid')),
347 ), __FILE__, __LINE__);
350 $userid = bigintval(SQL_INSERTID());
353 if ($userid == '0') {
354 // Something bad happened!
355 loadTemplate('admin_settings_saved', false, getMessage('USER_NOT_REGISTERED'));
361 // Is the refback extension there?
362 // @TODO Rewrite this to a filter
363 if (isExtensionActive('refback')) {
364 // Update refback table
365 updateRefbackTable($userid);
368 // Write his welcome-points
369 // @TODO Rewrite this whole if() block to addPointsThroughReferalSystem(). This will also make following if() block obsolete
370 // @TODO Wether the registration bonus should only be added to user directly or through referal system should be configurable
371 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_points` WHERE `userid`=%s AND `ref_depth`=0 LIMIT 1",
372 array($userid), __FILE__, __LINE__);
373 if (SQL_NUMROWS($result) == '0') {
374 // Add only when the line was not found (maybe some more secure?)
377 // Pay him later. First he has to confirm some mails!
378 if (getConfig('ref_payout') > 0) $locked = 'locked_points';
380 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_points` (`userid`, `ref_depth`, `%s`) VALUES (%s,0,'{?points_register?}')",
381 array($locked, $userid), __FILE__, __LINE__);
383 // Update mediadata as well
384 if ((isExtensionInstalledAndNewer('mediadata', '0.0.4')) && ($locked == 'points')) {
386 updateMediadataEntry(array('total_points'), 'add', getConfig('points_register'));
391 if ((is_array(postRequestParameter('cat'))) && (count(postRequestParameter('cat')))) {
392 foreach (postRequestParameter('cat') as $cat => $joined) {
393 if ($joined == 'Y') {
394 // Insert category entry
395 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_cats` (`userid`, `cat_id`) VALUES (%s, %s)",
396 array($userid, bigintval($cat)), __FILE__, __LINE__);
401 // ... rewrite a zero referal id to the main title
402 if (postRequestParameter('refid') == '0') setPostRequestParameter('refid', getConfig('MAIN_TITLE'));
405 if (isPostRequestParameterSet('zip')) {
406 // Prepare data array for the email template
407 // Start with the gender...
411 'gender' => translateGender(postRequestParameter('gender')),
412 'surname' => SQL_ESCAPE(postRequestParameter('surname')),
413 'family' => SQL_ESCAPE(postRequestParameter('family')),
414 'email' => SQL_ESCAPE(postRequestParameter('email')),
415 'street' => SQL_ESCAPE(postRequestParameter('street_nr')),
416 'city' => SQL_ESCAPE(postRequestParameter('city')),
417 'zip' => bigintval(postRequestParameter('zip')),
418 'country' => $countryData,
419 'refid' => SQL_ESCAPE(postRequestParameter('refid')),
420 'pass' => SQL_ESCAPE(postRequestParameter('pass1')),
423 // No ZIP code entered
427 'gender' => translateGender(postRequestParameter('gender')),
428 'surname' => SQL_ESCAPE(postRequestParameter('surname')),
429 'family' => SQL_ESCAPE(postRequestParameter('family')),
430 'email' => SQL_ESCAPE(postRequestParameter('email')),
431 'street' => SQL_ESCAPE(postRequestParameter('street_nr')),
432 'city' => SQL_ESCAPE(postRequestParameter('city')),
434 'country' => $countryData,
435 'refid' => SQL_ESCAPE(postRequestParameter('refid')),
436 'pass' => SQL_ESCAPE(postRequestParameter('pass1')),
440 // Continue with birthday...
441 switch (getLanguage()) {
443 $content['birthday'] = bigintval(postRequestParameter('day')) . '.' . bigintval(postRequestParameter('month')) . '.' . bigintval(postRequestParameter('year'));
447 $content['birthday'] = bigintval(postRequestParameter('month')) . '/' . bigintval(postRequestParameter('day')) . '/' . bigintval(postRequestParameter('year'));
451 // Display information to the user that he got mail and send it away
452 $messageGuest = loadEmailTemplate('register-member', $content, $userid);
454 // Send mail to user (confirmation link!)
455 $email = $content['email'];
456 sendEmail($content['email'], getMessage('GUEST_SUBJECT_CONFIRM_LINK'), $messageGuest);
457 $content['email'] = $email;
459 // Send mail to admin
460 sendAdminNotification(getMessage('ADMIN_SUBJECT_NEW_ACCOUNT'), 'register-admin', $content, $userid);