includes/ucp/ucp_register.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 23:05:07 +0400
changeset 4 e9e194e102a1
parent 0 ceef6d64c21d
permissions -rw-r--r--
Config
     1 <?php
     2 /**
     3 *
     4 * @package ucp
     5 * @version $Id$
     6 * @copyright (c) 2005 phpBB Group
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     8 *
     9 */
    10 
    11 /**
    12 * @ignore
    13 */
    14 if (!defined('IN_PHPBB'))
    15 {
    16 	exit;
    17 }
    18 
    19 /**
    20 * ucp_register
    21 * Board registration
    22 * @package ucp
    23 */
    24 class ucp_register
    25 {
    26 	var $u_action;
    27 
    28 	function main($id, $mode)
    29 	{
    30 		global $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx;
    31 
    32 		//
    33 		if ($config['require_activation'] == USER_ACTIVATION_DISABLE)
    34 		{
    35 			trigger_error('UCP_REGISTER_DISABLE');
    36 		}
    37 
    38 		include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
    39 
    40 		$coppa			= (isset($_REQUEST['coppa'])) ? ((!empty($_REQUEST['coppa'])) ? 1 : 0) : false;
    41 		$agreed			= (!empty($_POST['agreed'])) ? 1 : 0;
    42 		$submit			= (isset($_POST['submit'])) ? true : false;
    43 		$change_lang	= request_var('change_lang', '');
    44 		$user_lang		= request_var('lang', $user->lang_name);
    45 
    46 		if ($agreed)
    47 		{
    48 			add_form_key('ucp_register');
    49 		}
    50 		else
    51 		{
    52 			add_form_key('ucp_register_terms');
    53 		}
    54 
    55 		if ($change_lang || $user_lang != $config['default_lang'])
    56 		{
    57 			$use_lang = ($change_lang) ? basename($change_lang) : basename($user_lang);
    58 
    59 			if (!validate_language_iso_name($use_lang))
    60 			{
    61 				if ($change_lang)
    62 				{
    63 					$submit = false;
    64 
    65 					// Setting back agreed to let the user view the agreement in his/her language
    66 					$agreed = (empty($_GET['change_lang'])) ? 0 : $agreed;
    67 				}
    68 
    69 				$user->lang_name = $user_lang = $use_lang;
    70 				$user->lang = array();
    71 				$user->data['user_lang'] = $user->lang_name;
    72 				$user->add_lang(array('common', 'ucp'));
    73 			}
    74 			else
    75 			{
    76 				$change_lang = '';
    77 				$user_lang = $user->lang_name;
    78 			}
    79 		}
    80 
    81 
    82 		$cp = new custom_profile();
    83 
    84 		$error = $cp_data = $cp_error = array();
    85 
    86 		if (!$agreed || ($coppa === false && $config['coppa_enable']) || ($coppa && !$config['coppa_enable']))
    87 		{
    88 			$add_lang = ($change_lang) ? '&amp;change_lang=' . urlencode($change_lang) : '';
    89 			$add_coppa = ($coppa !== false) ? '&amp;coppa=' . $coppa : '';
    90 
    91 			$s_hidden_fields = array(
    92 				'change_lang'	=> $change_lang,
    93 			);
    94 
    95 			// If we change the language, we want to pass on some more possible parameter.
    96 			if ($change_lang)
    97 			{
    98 				// We do not include the password
    99 				$s_hidden_fields = array_merge($s_hidden_fields, array(
   100 					'username'			=> utf8_normalize_nfc(request_var('username', '', true)),
   101 					'email'				=> strtolower(request_var('email', '')),
   102 					'email_confirm'		=> strtolower(request_var('email_confirm', '')),
   103 					'lang'				=> $user->lang_name,
   104 					'tz'				=> request_var('tz', (float) $config['board_timezone']),
   105 				));
   106 
   107 			}
   108 
   109 			// Checking amount of available languages
   110 			$sql = 'SELECT lang_id
   111 				FROM ' . LANG_TABLE;
   112 			$result = $db->sql_query($sql);
   113 
   114 			$lang_row = array();
   115 			while ($row = $db->sql_fetchrow($result))
   116 			{
   117 				$lang_row[] = $row;
   118 			}
   119 			$db->sql_freeresult($result);
   120 
   121 			if ($coppa === false && $config['coppa_enable'])
   122 			{
   123 				$now = getdate();
   124 				$coppa_birthday = $user->format_date(mktime($now['hours'] + $user->data['user_dst'], $now['minutes'], $now['seconds'], $now['mon'], $now['mday'] - 1, $now['year'] - 13), $user->lang['DATE_FORMAT']);
   125 				unset($now);
   126 
   127 				$template->assign_vars(array(
   128 					'S_LANG_OPTIONS'	=> (sizeof($lang_row) > 1) ? language_select($user_lang) : '',
   129 					'L_COPPA_NO'		=> sprintf($user->lang['UCP_COPPA_BEFORE'], $coppa_birthday),
   130 					'L_COPPA_YES'		=> sprintf($user->lang['UCP_COPPA_ON_AFTER'], $coppa_birthday),
   131 
   132 					'U_COPPA_NO'		=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=register&amp;coppa=0' . $add_lang),
   133 					'U_COPPA_YES'		=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=register&amp;coppa=1' . $add_lang),
   134 
   135 					'S_SHOW_COPPA'		=> true,
   136 					'S_HIDDEN_FIELDS'	=> build_hidden_fields($s_hidden_fields),
   137 					'S_UCP_ACTION'		=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=register' . $add_lang),
   138 				));
   139 			}
   140 			else
   141 			{
   142 				$template->assign_vars(array(
   143 					'S_LANG_OPTIONS'	=> (sizeof($lang_row) > 1) ? language_select($user_lang) : '',
   144 					'L_TERMS_OF_USE'	=> sprintf($user->lang['TERMS_OF_USE_CONTENT'], $config['sitename'], generate_board_url()),
   145 
   146 					'S_SHOW_COPPA'		=> false,
   147 					'S_REGISTRATION'	=> true,
   148 					'S_HIDDEN_FIELDS'	=> build_hidden_fields($s_hidden_fields),
   149 					'S_UCP_ACTION'		=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=register' . $add_lang . $add_coppa),
   150 					)
   151 				);
   152 			}
   153 			unset($lang_row);
   154 
   155 			$this->tpl_name = 'ucp_agreement';
   156 			return;
   157 		}
   158 
   159 
   160 		// The CAPTCHA kicks in here. We can't help that the information gets lost on language change. 
   161 		if ($config['enable_confirm'])
   162 		{
   163 			include($phpbb_root_path . 'includes/captcha/captcha_factory.' . $phpEx);
   164 			$captcha =& phpbb_captcha_factory::get_instance($config['captcha_plugin']);
   165 			$captcha->init(CONFIRM_REG);
   166 		}
   167 
   168 		// Try to manually determine the timezone and adjust the dst if the server date/time complies with the default setting +/- 1
   169 		$timezone = date('Z') / 3600;
   170 		$is_dst = date('I');
   171 
   172 		if ($config['board_timezone'] == $timezone || $config['board_timezone'] == ($timezone - 1))
   173 		{
   174 			$timezone = ($is_dst) ? $timezone - 1 : $timezone;
   175 
   176 			if (!isset($user->lang['tz_zones'][(string) $timezone]))
   177 			{
   178 				$timezone = $config['board_timezone'];
   179 			}
   180 		}
   181 		else
   182 		{
   183 			$is_dst = $config['board_dst'];
   184 			$timezone = $config['board_timezone'];
   185 		}
   186 
   187 		$data = array(
   188 			'username'			=> utf8_normalize_nfc(request_var('username', '', true)),
   189 			'new_password'		=> request_var('new_password', '', true),
   190 			'password_confirm'	=> request_var('password_confirm', '', true),
   191 			'email'				=> strtolower(request_var('email', '')),
   192 			'email_confirm'		=> strtolower(request_var('email_confirm', '')),
   193 			'lang'				=> basename(request_var('lang', $user->lang_name)),
   194 			'tz'				=> request_var('tz', (float) $timezone),
   195 		);
   196 
   197 		// Check and initialize some variables if needed
   198 		if ($submit)
   199 		{
   200 			$error = validate_data($data, array(
   201 				'username'			=> array(
   202 					array('string', false, $config['min_name_chars'], $config['max_name_chars']),
   203 					array('username', '')),
   204 				'new_password'		=> array(
   205 					array('string', false, $config['min_pass_chars'], $config['max_pass_chars']),
   206 					array('password')),
   207 				'password_confirm'	=> array('string', false, $config['min_pass_chars'], $config['max_pass_chars']),
   208 				'email'				=> array(
   209 					array('string', false, 6, 60),
   210 					array('email')),
   211 				'email_confirm'		=> array('string', false, 6, 60),
   212 				'tz'				=> array('num', false, -14, 14),
   213 				'lang'				=> array('language_iso_name'),
   214 			));
   215 
   216 			if (!check_form_key('ucp_register'))
   217 			{
   218 				$error[] = $user->lang['FORM_INVALID'];
   219 			}
   220 
   221 			// Replace "error" strings with their real, localised form
   222 			$error = preg_replace('#^([A-Z_]+)$#e', "(!empty(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '\\1'", $error);
   223 
   224 			if ($config['enable_confirm'])
   225 			{
   226 				$vc_response = $captcha->validate($data);
   227 				if ($vc_response !== false)
   228 				{
   229 					$error[] = $vc_response;
   230 				}
   231 
   232 				if ($config['max_reg_attempts'] && $captcha->get_attempt_count() > $config['max_reg_attempts'])
   233 				{
   234 					$error[] = $user->lang['TOO_MANY_REGISTERS'];
   235 				}
   236 			}
   237 
   238 			// DNSBL check
   239 			if ($config['check_dnsbl'])
   240 			{
   241 				if (($dnsbl = $user->check_dnsbl('register')) !== false)
   242 				{
   243 					$error[] = sprintf($user->lang['IP_BLACKLISTED'], $user->ip, $dnsbl[1]);
   244 				}
   245 			}
   246 
   247 			// validate custom profile fields
   248 			$cp->submit_cp_field('register', $user->get_iso_lang_id(), $cp_data, $error);
   249 
   250 			if (!sizeof($error))
   251 			{
   252 				if ($data['new_password'] != $data['password_confirm'])
   253 				{
   254 					$error[] = $user->lang['NEW_PASSWORD_ERROR'];
   255 				}
   256 
   257 				if ($data['email'] != $data['email_confirm'])
   258 				{
   259 					$error[] = $user->lang['NEW_EMAIL_ERROR'];
   260 				}
   261 			}
   262 
   263 			if (!sizeof($error))
   264 			{
   265 				$server_url = generate_board_url();
   266 
   267 				// Which group by default?
   268 				$group_name = ($coppa) ? 'REGISTERED_COPPA' : 'REGISTERED';
   269 
   270 				$sql = 'SELECT group_id
   271 					FROM ' . GROUPS_TABLE . "
   272 					WHERE group_name = '" . $db->sql_escape($group_name) . "'
   273 						AND group_type = " . GROUP_SPECIAL;
   274 				$result = $db->sql_query($sql);
   275 				$row = $db->sql_fetchrow($result);
   276 				$db->sql_freeresult($result);
   277 
   278 				if (!$row)
   279 				{
   280 					trigger_error('NO_GROUP');
   281 				}
   282 
   283 				$group_id = $row['group_id'];
   284 
   285 				if (($coppa ||
   286 					$config['require_activation'] == USER_ACTIVATION_SELF ||
   287 					$config['require_activation'] == USER_ACTIVATION_ADMIN) && $config['email_enable'])
   288 				{
   289 					$user_actkey = gen_rand_string(mt_rand(6, 10));
   290 					$user_type = USER_INACTIVE;
   291 					$user_inactive_reason = INACTIVE_REGISTER;
   292 					$user_inactive_time = time();
   293 				}
   294 				else
   295 				{
   296 					$user_type = USER_NORMAL;
   297 					$user_actkey = '';
   298 					$user_inactive_reason = 0;
   299 					$user_inactive_time = 0;
   300 				}
   301 
   302 				$user_row = array(
   303 					'username'				=> $data['username'],
   304 					'user_password'			=> phpbb_hash($data['new_password']),
   305 					'user_email'			=> $data['email'],
   306 					'group_id'				=> (int) $group_id,
   307 					'user_timezone'			=> (float) $data['tz'],
   308 					'user_dst'				=> $is_dst,
   309 					'user_lang'				=> $data['lang'],
   310 					'user_type'				=> $user_type,
   311 					'user_actkey'			=> $user_actkey,
   312 					'user_ip'				=> $user->ip,
   313 					'user_regdate'			=> time(),
   314 					'user_inactive_reason'	=> $user_inactive_reason,
   315 					'user_inactive_time'	=> $user_inactive_time,
   316 				);
   317 
   318 				if ($config['new_member_post_limit'])
   319 				{
   320 					$user_row['user_new'] = 1;
   321 				}
   322 
   323 				// Register user...
   324 				$user_id = user_add($user_row, $cp_data);
   325 
   326 				// This should not happen, because the required variables are listed above...
   327 				if ($user_id === false)
   328 				{
   329 					trigger_error('NO_USER', E_USER_ERROR);
   330 				}
   331 
   332 				// Okay, captcha, your job is done.
   333 				if ($config['enable_confirm'] && isset($captcha))
   334 				{
   335 					$captcha->reset();
   336 				}
   337 
   338 				if ($coppa && $config['email_enable'])
   339 				{
   340 					$message = $user->lang['ACCOUNT_COPPA'];
   341 					$email_template = 'coppa_welcome_inactive';
   342 				}
   343 				else if ($config['require_activation'] == USER_ACTIVATION_SELF && $config['email_enable'])
   344 				{
   345 					$message = $user->lang['ACCOUNT_INACTIVE'];
   346 					$email_template = 'user_welcome_inactive';
   347 				}
   348 				else if ($config['require_activation'] == USER_ACTIVATION_ADMIN && $config['email_enable'])
   349 				{
   350 					$message = $user->lang['ACCOUNT_INACTIVE_ADMIN'];
   351 					$email_template = 'admin_welcome_inactive';
   352 				}
   353 				else
   354 				{
   355 					$message = $user->lang['ACCOUNT_ADDED'];
   356 					$email_template = 'user_welcome';
   357 				}
   358 
   359 				if ($config['email_enable'])
   360 				{
   361 					include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
   362 
   363 					$messenger = new messenger(false);
   364 
   365 					$messenger->template($email_template, $data['lang']);
   366 
   367 					$messenger->to($data['email'], $data['username']);
   368 
   369 					$messenger->anti_abuse_headers($config, $user);
   370 
   371 					$messenger->assign_vars(array(
   372 						'WELCOME_MSG'	=> htmlspecialchars_decode(sprintf($user->lang['WELCOME_SUBJECT'], $config['sitename'])),
   373 						'USERNAME'		=> htmlspecialchars_decode($data['username']),
   374 						'PASSWORD'		=> htmlspecialchars_decode($data['new_password']),
   375 						'U_ACTIVATE'	=> "$server_url/ucp.$phpEx?mode=activate&u=$user_id&k=$user_actkey")
   376 					);
   377 
   378 					if ($coppa)
   379 					{
   380 						$messenger->assign_vars(array(
   381 							'FAX_INFO'		=> $config['coppa_fax'],
   382 							'MAIL_INFO'		=> $config['coppa_mail'],
   383 							'EMAIL_ADDRESS'	=> $data['email'])
   384 						);
   385 					}
   386 
   387 					$messenger->send(NOTIFY_EMAIL);
   388 
   389 					if ($config['require_activation'] == USER_ACTIVATION_ADMIN)
   390 					{
   391 						// Grab an array of user_id's with a_user permissions ... these users can activate a user
   392 						$admin_ary = $auth->acl_get_list(false, 'a_user', false);
   393 						$admin_ary = (!empty($admin_ary[0]['a_user'])) ? $admin_ary[0]['a_user'] : array();
   394 
   395 						// Also include founders
   396 						$where_sql = ' WHERE user_type = ' . USER_FOUNDER;
   397 
   398 						if (sizeof($admin_ary))
   399 						{
   400 							$where_sql .= ' OR ' . $db->sql_in_set('user_id', $admin_ary);
   401 						}
   402 
   403 						$sql = 'SELECT user_id, username, user_email, user_lang, user_jabber, user_notify_type
   404 							FROM ' . USERS_TABLE . ' ' .
   405 							$where_sql;
   406 						$result = $db->sql_query($sql);
   407 
   408 						while ($row = $db->sql_fetchrow($result))
   409 						{
   410 							$messenger->template('admin_activate', $row['user_lang']);
   411 							$messenger->to($row['user_email'], $row['username']);
   412 							$messenger->im($row['user_jabber'], $row['username']);
   413 
   414 							$messenger->assign_vars(array(
   415 								'USERNAME'			=> htmlspecialchars_decode($data['username']),
   416 								'U_USER_DETAILS'	=> "$server_url/memberlist.$phpEx?mode=viewprofile&u=$user_id",
   417 								'U_ACTIVATE'		=> "$server_url/ucp.$phpEx?mode=activate&u=$user_id&k=$user_actkey")
   418 							);
   419 
   420 							$messenger->send($row['user_notify_type']);
   421 						}
   422 						$db->sql_freeresult($result);
   423 					}
   424 				}
   425 
   426 				$message = $message . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' . append_sid("{$phpbb_root_path}index.$phpEx") . '">', '</a>');
   427 				trigger_error($message);
   428 			}
   429 		}
   430 
   431 		$s_hidden_fields = array(
   432 			'agreed'		=> 'true',
   433 			'change_lang'	=> 0,
   434 		);
   435 
   436 		if ($config['coppa_enable'])
   437 		{
   438 			$s_hidden_fields['coppa'] = $coppa;
   439 		}
   440 
   441 		if ($config['enable_confirm'])
   442 		{
   443 			$s_hidden_fields = array_merge($s_hidden_fields, $captcha->get_hidden_fields());
   444 		}
   445 		$s_hidden_fields = build_hidden_fields($s_hidden_fields);
   446 		$confirm_image = '';
   447 
   448 		// Visual Confirmation - Show images
   449 		if ($config['enable_confirm'])
   450 		{
   451 			$template->assign_vars(array(
   452 				'CAPTCHA_TEMPLATE'		=> $captcha->get_template(),
   453 			));
   454 		}
   455 
   456 		//
   457 		$l_reg_cond = '';
   458 		switch ($config['require_activation'])
   459 		{
   460 			case USER_ACTIVATION_SELF:
   461 				$l_reg_cond = $user->lang['UCP_EMAIL_ACTIVATE'];
   462 			break;
   463 
   464 			case USER_ACTIVATION_ADMIN:
   465 				$l_reg_cond = $user->lang['UCP_ADMIN_ACTIVATE'];
   466 			break;
   467 		}
   468 
   469 		$template->assign_vars(array(
   470 			'ERROR'				=> (sizeof($error)) ? implode('<br />', $error) : '',
   471 			'USERNAME'			=> $data['username'],
   472 			'PASSWORD'			=> $data['new_password'],
   473 			'PASSWORD_CONFIRM'	=> $data['password_confirm'],
   474 			'EMAIL'				=> $data['email'],
   475 			'EMAIL_CONFIRM'		=> $data['email_confirm'],
   476 
   477 			'L_REG_COND'				=> $l_reg_cond,
   478 			'L_USERNAME_EXPLAIN'		=> sprintf($user->lang[$config['allow_name_chars'] . '_EXPLAIN'], $config['min_name_chars'], $config['max_name_chars']),
   479 			'L_PASSWORD_EXPLAIN'		=> sprintf($user->lang[$config['pass_complex'] . '_EXPLAIN'], $config['min_pass_chars'], $config['max_pass_chars']),
   480 
   481 			'S_LANG_OPTIONS'	=> language_select($data['lang']),
   482 			'S_TZ_OPTIONS'		=> tz_select($data['tz']),
   483 			'S_CONFIRM_REFRESH'	=> ($config['enable_confirm'] && $config['confirm_refresh']) ? true : false,
   484 			'S_REGISTRATION'	=> true,
   485 			'S_COPPA'			=> $coppa,
   486 			'S_HIDDEN_FIELDS'	=> $s_hidden_fields,
   487 			'S_UCP_ACTION'		=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=register'),
   488 		));
   489 
   490 		//
   491 		$user->profile_fields = array();
   492 
   493 		// Generate profile fields -> Template Block Variable profile_fields
   494 		$cp->generate_profile_fields('register', $user->get_iso_lang_id());
   495 
   496 		//
   497 		$this->tpl_name = 'ucp_register';
   498 		$this->page_title = 'UCP_REGISTRATION';
   499 	}
   500 }
   501 
   502 ?>