includes/functions_profile_fields.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Fri, 09 Mar 2012 16:14:29 +0400
changeset 13 118ae93ae3fe
parent 0 ceef6d64c21d
permissions -rw-r--r--
Added Art Air Green style
     1 <?php
     2 /**
     3 *
     4 * @package phpBB3
     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 * Custom Profile Fields
    21 * @package phpBB3
    22 */
    23 class custom_profile
    24 {
    25 	var $profile_types = array(FIELD_INT => 'int', FIELD_STRING => 'string', FIELD_TEXT => 'text', FIELD_BOOL => 'bool', FIELD_DROPDOWN => 'dropdown', FIELD_DATE => 'date');
    26 	var $profile_cache = array();
    27 	var $options_lang = array();
    28 
    29 	/**
    30 	* Assign editable fields to template, mode can be profile (for profile change) or register (for registration)
    31 	* Called by ucp_profile and ucp_register
    32 	* @access public
    33 	*/
    34 	function generate_profile_fields($mode, $lang_id)
    35 	{
    36 		global $db, $template, $auth;
    37 
    38 		$sql_where = '';
    39 		switch ($mode)
    40 		{
    41 			case 'register':
    42 				// If the field is required we show it on the registration page
    43 				$sql_where .= ' AND f.field_show_on_reg = 1';
    44 			break;
    45 
    46 			case 'profile':
    47 				// Show hidden fields to moderators/admins
    48 				if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
    49 				{
    50 					$sql_where .= ' AND f.field_show_profile = 1';
    51 				}
    52 			break;
    53 
    54 			default:
    55 				trigger_error('Wrong profile mode specified', E_USER_ERROR);
    56 			break;
    57 		}
    58 
    59 		$sql = 'SELECT l.*, f.*
    60 			FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f
    61 			WHERE f.field_active = 1
    62 				$sql_where
    63 				AND l.lang_id = $lang_id
    64 				AND l.field_id = f.field_id
    65 			ORDER BY f.field_order";
    66 		$result = $db->sql_query($sql);
    67 
    68 		while ($row = $db->sql_fetchrow($result))
    69 		{
    70 			// Return templated field
    71 			$tpl_snippet = $this->process_field_row('change', $row);
    72 
    73 			// Some types are multivalue, we can't give them a field_id as we would not know which to pick
    74 			$type = (int) $row['field_type'];
    75 
    76 			$template->assign_block_vars('profile_fields', array(
    77 				'LANG_NAME'		=> $row['lang_name'],
    78 				'LANG_EXPLAIN'	=> $row['lang_explain'],
    79 				'FIELD'			=> $tpl_snippet,
    80 				'FIELD_ID'		=> ($type == FIELD_DATE || ($type == FIELD_BOOL && $row['field_length'] == '1')) ? '' : 'pf_' . $row['field_ident'],
    81 				'S_REQUIRED'	=> ($row['field_required']) ? true : false)
    82 			);
    83 		}
    84 		$db->sql_freeresult($result);
    85 	}
    86 
    87 	/**
    88 	* Validate entered profile field data
    89 	* @access public
    90 	*/
    91 	function validate_profile_field($field_type, &$field_value, $field_data)
    92 	{
    93 		switch ($field_type)
    94 		{
    95 			case FIELD_DATE:
    96 				$field_validate = explode('-', $field_value);
    97 
    98 				$day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0;
    99 				$month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0;
   100 				$year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0;
   101 
   102 				if ((!$day || !$month || !$year) && !$field_data['field_required'])
   103 				{
   104 					return false;
   105 				}
   106 
   107 				if ((!$day || !$month || !$year) && $field_data['field_required'])
   108 				{
   109 					return 'FIELD_REQUIRED';
   110 				}
   111 
   112 				if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50)
   113 				{
   114 					return 'FIELD_INVALID_DATE';
   115 				}
   116 
   117 				if (checkdate($month, $day, $year) === false)
   118 				{
   119 					return 'FIELD_INVALID_DATE';
   120 				}
   121 			break;
   122 
   123 			case FIELD_BOOL:
   124 				$field_value = (bool) $field_value;
   125 			
   126 				if (!$field_value && $field_data['field_required'])
   127 				{
   128 					return 'FIELD_REQUIRED';
   129 				}
   130 			break;
   131 
   132 			case FIELD_INT:
   133 				if (trim($field_value) === '' && !$field_data['field_required'])
   134 				{
   135 					return false;
   136 				}
   137 				
   138 				$field_value = (int) $field_value;
   139 
   140 				if ($field_value < $field_data['field_minlen'])
   141 				{
   142 					return 'FIELD_TOO_SMALL';
   143 				}
   144 				else if ($field_value > $field_data['field_maxlen'])
   145 				{
   146 					return 'FIELD_TOO_LARGE';
   147 				}
   148 			break;
   149 
   150 			case FIELD_DROPDOWN:
   151 				$field_value = (int) $field_value;
   152 
   153 				// retrieve option lang data if necessary
   154 				if (!isset($this->options_lang[$field_data['field_id']]) || !isset($this->options_lang[$field_data['field_id']][$field_data['lang_id']]) || !sizeof($this->options_lang[$file_data['field_id']][$field_data['lang_id']]))
   155 				{
   156 					$this->get_option_lang($field_data['field_id'], $field_data['lang_id'], FIELD_DROPDOWN, false);
   157 				}
   158 
   159 				if (!isset($this->options_lang[$field_data['field_id']][$field_data['lang_id']][$field_value]))
   160 				{
   161 					return 'FIELD_INVALID_VALUE';
   162 				}
   163 
   164 				if ($field_value == $field_data['field_novalue'] && $field_data['field_required'])
   165 				{
   166 					return 'FIELD_REQUIRED';
   167 				}
   168 			break;
   169 
   170 			case FIELD_STRING:
   171 			case FIELD_TEXT:
   172 				if (trim($field_value) === '' && !$field_data['field_required'])
   173 				{
   174 					return false;
   175 				}
   176 				else if (trim($field_value) === '' && $field_data['field_required'])
   177 				{
   178 					return 'FIELD_REQUIRED';
   179 				}
   180 
   181 				if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen'])
   182 				{
   183 					return 'FIELD_TOO_SHORT';
   184 				}
   185 				else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen'])
   186 				{
   187 					return 'FIELD_TOO_LONG';
   188 				}
   189 
   190 				if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*')
   191 				{
   192 					$field_validate = ($field_type == FIELD_STRING) ? $field_value : bbcode_nl2br($field_value);
   193 					if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate))
   194 					{
   195 						return 'FIELD_INVALID_CHARS';
   196 					}
   197 				}
   198 			break;
   199 		}
   200 
   201 		return false;
   202 	}
   203 
   204 	/**
   205 	* Build profile cache, used for display
   206 	* @access private
   207 	*/
   208 	function build_cache()
   209 	{
   210 		global $db, $user, $auth;
   211 
   212 		$this->profile_cache = array();
   213 
   214 		// Display hidden/no_view fields for admin/moderator
   215 		$sql = 'SELECT l.*, f.*
   216 			FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
   217 			WHERE l.lang_id = ' . $user->get_iso_lang_id() . '
   218 				AND f.field_active = 1 ' .
   219 				((!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_')) ? '	AND f.field_hide = 0 ' : '') . '
   220 				AND f.field_no_view = 0
   221 				AND l.field_id = f.field_id
   222 			ORDER BY f.field_order';
   223 		$result = $db->sql_query($sql);
   224 
   225 		while ($row = $db->sql_fetchrow($result))
   226 		{
   227 			$this->profile_cache[$row['field_ident']] = $row;
   228 		}
   229 		$db->sql_freeresult($result);
   230 	}
   231 
   232 	/**
   233 	* Get language entries for options and store them here for later use
   234 	*/
   235 	function get_option_lang($field_id, $lang_id, $field_type, $preview)
   236 	{
   237 		global $db;
   238 
   239 		if ($preview)
   240 		{
   241 			$lang_options = (!is_array($this->vars['lang_options'])) ? explode("\n", $this->vars['lang_options']) : $this->vars['lang_options'];
   242 
   243 			foreach ($lang_options as $num => $var)
   244 			{
   245 				$this->options_lang[$field_id][$lang_id][($num + 1)] = $var;
   246 			}
   247 		}
   248 		else
   249 		{
   250 			$sql = 'SELECT option_id, lang_value
   251 				FROM ' . PROFILE_FIELDS_LANG_TABLE . "
   252 					WHERE field_id = $field_id
   253 					AND lang_id = $lang_id
   254 					AND field_type = $field_type
   255 				ORDER BY option_id";
   256 			$result = $db->sql_query($sql);
   257 
   258 			while ($row = $db->sql_fetchrow($result))
   259 			{
   260 				$this->options_lang[$field_id][$lang_id][($row['option_id'] + 1)] = $row['lang_value'];
   261 			}
   262 			$db->sql_freeresult($result);
   263 		}
   264 	}
   265 
   266 	/**
   267 	* Submit profile field for validation
   268 	* @access public
   269 	*/
   270 	function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error)
   271 	{
   272 		global $auth, $db, $user;
   273 
   274 		$sql_where = '';
   275 		switch ($mode)
   276 		{
   277 			case 'register':
   278 				// If the field is required we show it on the registration page
   279 				$sql_where .= ' AND f.field_show_on_reg = 1';
   280 			break;
   281 
   282 			case 'profile':
   283 				// Show hidden fields to moderators/admins
   284 				if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
   285 				{
   286 					$sql_where .= ' AND f.field_show_profile = 1';
   287 				}
   288 			break;
   289 
   290 			default:
   291 				trigger_error('Wrong profile mode specified', E_USER_ERROR);
   292 			break;
   293 		}
   294 
   295 		$sql = 'SELECT l.*, f.*
   296 			FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f
   297 			WHERE l.lang_id = $lang_id
   298 				AND f.field_active = 1
   299 				$sql_where
   300 				AND l.field_id = f.field_id
   301 			ORDER BY f.field_order";
   302 		$result = $db->sql_query($sql);
   303 
   304 		while ($row = $db->sql_fetchrow($result))
   305 		{
   306 			$cp_data['pf_' . $row['field_ident']] = $this->get_profile_field($row);
   307 			$check_value = $cp_data['pf_' . $row['field_ident']];
   308 
   309 			if (($cp_result = $this->validate_profile_field($row['field_type'], $check_value, $row)) !== false)
   310 			{
   311 				// If not and only showing common error messages, use this one
   312 				$error = '';
   313 				switch ($cp_result)
   314 				{
   315 					case 'FIELD_INVALID_DATE':
   316 					case 'FIELD_INVALID_VALUE':
   317 					case 'FIELD_REQUIRED':
   318 						$error = sprintf($user->lang[$cp_result], $row['lang_name']);
   319 					break;
   320 
   321 					case 'FIELD_TOO_SHORT':
   322 					case 'FIELD_TOO_SMALL':
   323 						$error = sprintf($user->lang[$cp_result], $row['lang_name'], $row['field_minlen']);
   324 					break;
   325 
   326 					case 'FIELD_TOO_LONG':
   327 					case 'FIELD_TOO_LARGE':
   328 						$error = sprintf($user->lang[$cp_result], $row['lang_name'], $row['field_maxlen']);
   329 					break;
   330 
   331 					case 'FIELD_INVALID_CHARS':
   332 						switch ($row['field_validation'])
   333 						{
   334 							case '[0-9]+':
   335 								$error = sprintf($user->lang[$cp_result . '_NUMBERS_ONLY'], $row['lang_name']);
   336 							break;
   337 
   338 							case '[\w]+':
   339 								$error = sprintf($user->lang[$cp_result . '_ALPHA_ONLY'], $row['lang_name']);
   340 							break;
   341 
   342 							case '[\w_\+\. \-\[\]]+':
   343 								$error = sprintf($user->lang[$cp_result . '_SPACERS_ONLY'], $row['lang_name']);
   344 							break;
   345 						}
   346 					break;
   347 				}
   348 
   349 				if ($error != '')
   350 				{
   351 					$cp_error[] = $error;
   352 				}
   353 			}
   354 		}
   355 		$db->sql_freeresult($result);
   356 	}
   357 
   358 	/**
   359 	* Update profile field data directly
   360 	*/
   361 	function update_profile_field_data($user_id, &$cp_data)
   362 	{
   363 		global $db;
   364 
   365 		if (!sizeof($cp_data))
   366 		{
   367 			return;
   368 		}
   369 
   370 		switch ($db->sql_layer)
   371 		{
   372 			case 'oracle':
   373 			case 'firebird':
   374 			case 'postgres':
   375 				$right_delim = $left_delim = '"';
   376 			break;
   377 
   378 			case 'sqlite':
   379 			case 'mssql':
   380 			case 'mssql_odbc':
   381 			case 'mssqlnative':
   382 				$right_delim = ']';
   383 				$left_delim = '[';
   384 			break;
   385 
   386 			case 'mysql':
   387 			case 'mysql4':
   388 			case 'mysqli':
   389 				$right_delim = $left_delim = '`';
   390 			break;
   391 		}
   392 
   393 		// use new array for the UPDATE; changes in the key do not affect the original array
   394 		$cp_data_sql = array();
   395 		foreach ($cp_data as $key => $value)
   396 		{
   397 			// Firebird is case sensitive with delimiter
   398 			$cp_data_sql[$left_delim . (($db->sql_layer == 'firebird' || $db->sql_layer == 'oracle') ? strtoupper($key) : $key) . $right_delim] = $value;
   399 		}
   400 
   401 		$sql = 'UPDATE ' . PROFILE_FIELDS_DATA_TABLE . '
   402 			SET ' . $db->sql_build_array('UPDATE', $cp_data_sql) . "
   403 			WHERE user_id = $user_id";
   404 		$db->sql_query($sql);
   405 
   406 		if (!$db->sql_affectedrows())
   407 		{
   408 			$cp_data_sql['user_id'] = (int) $user_id;
   409 
   410 			$db->sql_return_on_error(true);
   411 
   412 			$sql = 'INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $cp_data_sql);
   413 			$db->sql_query($sql);
   414 
   415 			$db->sql_return_on_error(false);
   416 		}
   417 	}
   418 
   419 	/**
   420 	* Assign fields to template, used for viewprofile, viewtopic and memberlist (if load setting is enabled)
   421 	* This is directly connected to the user -> mode == grab is to grab the user specific fields, mode == show is for assigning the row to the template
   422 	* @access public
   423 	*/
   424 	function generate_profile_fields_template($mode, $user_id = 0, $profile_row = false)
   425 	{
   426 		global $db;
   427 
   428 		if ($mode == 'grab')
   429 		{
   430 			if (!is_array($user_id))
   431 			{
   432 				$user_id = array($user_id);
   433 			}
   434 
   435 			if (!sizeof($this->profile_cache))
   436 			{
   437 				$this->build_cache();
   438 			}
   439 
   440 			if (!sizeof($user_id))
   441 			{
   442 				return array();
   443 			}
   444 
   445 			$sql = 'SELECT *
   446 				FROM ' . PROFILE_FIELDS_DATA_TABLE . '
   447 				WHERE ' . $db->sql_in_set('user_id', array_map('intval', $user_id));
   448 			$result = $db->sql_query($sql);
   449 
   450 			$field_data = array();
   451 			while ($row = $db->sql_fetchrow($result))
   452 			{
   453 				$field_data[$row['user_id']] = $row;
   454 			}
   455 			$db->sql_freeresult($result);
   456 
   457 			$user_fields = array();
   458 
   459 			// Go through the fields in correct order
   460 			foreach (array_keys($this->profile_cache) as $used_ident)
   461 			{
   462 				foreach ($field_data as $user_id => $row)
   463 				{
   464 					$user_fields[$user_id][$used_ident]['value'] = $row['pf_' . $used_ident];
   465 					$user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident];
   466 				}
   467 			}
   468 
   469 			return $user_fields;
   470 		}
   471 		else if ($mode == 'show')
   472 		{
   473 			// $profile_row == $user_fields[$row['user_id']];
   474 			$tpl_fields = array();
   475 			$tpl_fields['row'] = $tpl_fields['blockrow'] = array();
   476 
   477 			foreach ($profile_row as $ident => $ident_ary)
   478 			{
   479 				$value = $this->get_profile_value($ident_ary);
   480 
   481 				if ($value === NULL)
   482 				{
   483 					continue;
   484 				}
   485 
   486 				$tpl_fields['row'] += array(
   487 					'PROFILE_' . strtoupper($ident) . '_VALUE'	=> $value,
   488 					'PROFILE_' . strtoupper($ident) . '_TYPE'	=> $ident_ary['data']['field_type'],
   489 					'PROFILE_' . strtoupper($ident) . '_NAME'	=> $ident_ary['data']['lang_name'],
   490 					'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $ident_ary['data']['lang_explain'],
   491 
   492 					'S_PROFILE_' . strtoupper($ident)			=> true
   493 				);
   494 
   495 				$tpl_fields['blockrow'][] = array(
   496 					'PROFILE_FIELD_VALUE'	=> $value,
   497 					'PROFILE_FIELD_TYPE'	=> $ident_ary['data']['field_type'],
   498 					'PROFILE_FIELD_NAME'	=> $ident_ary['data']['lang_name'],
   499 					'PROFILE_FIELD_EXPLAIN'	=> $ident_ary['data']['lang_explain'],
   500 
   501 					'S_PROFILE_' . strtoupper($ident)		=> true
   502 				);
   503 			}
   504 
   505 			return $tpl_fields;
   506 		}
   507 		else
   508 		{
   509 			trigger_error('Wrong mode for custom profile', E_USER_ERROR);
   510 		}
   511 	}
   512 
   513 	/**
   514 	* Get Profile Value for display
   515 	*/
   516 	function get_profile_value($ident_ary)
   517 	{
   518 		$value = $ident_ary['value'];
   519 		$field_type = $ident_ary['data']['field_type'];
   520 
   521 		switch ($this->profile_types[$field_type])
   522 		{
   523 			case 'int':
   524 				if ($value === '')
   525 				{
   526 					return NULL;
   527 				}
   528 				return (int) $value;
   529 			break;
   530 
   531 			case 'string':
   532 			case 'text':
   533 				if (!$value)
   534 				{
   535 					return NULL;
   536 				}
   537 
   538 				$value = make_clickable($value);
   539 				$value = censor_text($value);
   540 				$value = bbcode_nl2br($value);
   541 				return $value;
   542 			break;
   543 
   544 			// case 'datetime':
   545 			case 'date':
   546 				$date = explode('-', $value);
   547 				$day = (isset($date[0])) ? (int) $date[0] : 0;
   548 				$month = (isset($date[1])) ? (int) $date[1] : 0;
   549 				$year = (isset($date[2])) ? (int) $date[2] : 0;
   550 
   551 				if (!$day && !$month && !$year)
   552 				{
   553 					return NULL;
   554 				}
   555 				else if ($day && $month && $year)
   556 				{
   557 					global $user;
   558 					// Date should display as the same date for every user regardless of timezone, so remove offset
   559 					// to compensate for the offset added by user::format_date()
   560 					return $user->format_date(gmmktime(0, 0, 0, $month, $day, $year) - ($user->timezone + $user->dst), $user->lang['DATE_FORMAT'], true);
   561 				}
   562 
   563 				return $value;
   564 			break;
   565 
   566 			case 'dropdown':
   567 				$field_id = $ident_ary['data']['field_id'];
   568 				$lang_id = $ident_ary['data']['lang_id'];
   569 				if (!isset($this->options_lang[$field_id][$lang_id]))
   570 				{
   571 					$this->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false);
   572 				}
   573 
   574 				if ($value == $ident_ary['data']['field_novalue'])
   575 				{
   576 					return NULL;
   577 				}
   578 
   579 				$value = (int) $value;
   580 
   581 				// User not having a value assigned
   582 				if (!isset($this->options_lang[$field_id][$lang_id][$value]))
   583 				{
   584 					return NULL;
   585 				}
   586 
   587 				return $this->options_lang[$field_id][$lang_id][$value];
   588 			break;
   589 
   590 			case 'bool':
   591 				$field_id = $ident_ary['data']['field_id'];
   592 				$lang_id = $ident_ary['data']['lang_id'];
   593 				if (!isset($this->options_lang[$field_id][$lang_id]))
   594 				{
   595 					$this->get_option_lang($field_id, $lang_id, FIELD_BOOL, false);
   596 				}
   597 
   598 				if ($ident_ary['data']['field_length'] == 1)
   599 				{
   600 					return (isset($this->options_lang[$field_id][$lang_id][(int) $value])) ? $this->options_lang[$field_id][$lang_id][(int) $value] : NULL;
   601 				}
   602 				else if (!$value)
   603 				{
   604 					return NULL;
   605 				}
   606 				else
   607 				{
   608 					return $this->options_lang[$field_id][$lang_id][(int) ($value) + 1];
   609 				}
   610 			break;
   611 
   612 			default:
   613 				trigger_error('Unknown profile type', E_USER_ERROR);
   614 			break;
   615 		}
   616 	}
   617 
   618 	/**
   619 	* Get field value for registration/profile
   620 	* @access private
   621 	*/
   622 	function get_var($field_validation, &$profile_row, $default_value, $preview)
   623 	{
   624 		global $user;
   625 
   626 		$profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident'];
   627 		$user_ident = $profile_row['field_ident'];
   628 		// checkbox - only testing for isset
   629 		if ($profile_row['field_type'] == FIELD_BOOL && $profile_row['field_length'] == 2)
   630 		{
   631 			$value = (isset($_REQUEST[$profile_row['field_ident']])) ? true : ((!isset($user->profile_fields[$user_ident]) || $preview) ? $default_value : $user->profile_fields[$user_ident]);
   632 		}
   633 		else if ($profile_row['field_type'] == FIELD_INT)
   634 		{
   635 			if (isset($_REQUEST[$profile_row['field_ident']]))
   636 			{
   637 				$value = ($_REQUEST[$profile_row['field_ident']] === '') ? NULL : request_var($profile_row['field_ident'], $default_value);
   638 			}
   639 			else
   640 			{
   641 				if (!$preview && array_key_exists($user_ident, $user->profile_fields) && is_null($user->profile_fields[$user_ident]))
   642 				{
   643 					$value = NULL;
   644 				}
   645 				else if (!isset($user->profile_fields[$user_ident]) || $preview)
   646 				{
   647 					$value = $default_value;
   648 				}
   649 				else
   650 				{
   651 					$value = $user->profile_fields[$user_ident];
   652 				}
   653 			}
   654 
   655 			return (is_null($value) || $value === '') ? '' : (int) $value;
   656 		}
   657 		else
   658 		{
   659 			$value = (isset($_REQUEST[$profile_row['field_ident']])) ? request_var($profile_row['field_ident'], $default_value, true) : ((!isset($user->profile_fields[$user_ident]) || $preview) ? $default_value : $user->profile_fields[$user_ident]);
   660 
   661 			if (gettype($value) == 'string')
   662 			{
   663 				$value = utf8_normalize_nfc($value);
   664 			}
   665 		}
   666 
   667 		switch ($field_validation)
   668 		{
   669 			case 'int':
   670 				return (int) $value;
   671 			break;
   672 		}
   673 
   674 		return $value;
   675 	}
   676 
   677 	/**
   678 	* Process int-type
   679 	* @access private
   680 	*/
   681 	function generate_int($profile_row, $preview = false)
   682 	{
   683 		global $template;
   684 
   685 		$profile_row['field_value'] = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
   686 		$template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
   687 	}
   688 
   689 	/**
   690 	* Process date-type
   691 	* @access private
   692 	*/
   693 	function generate_date($profile_row, $preview = false)
   694 	{
   695 		global $user, $template;
   696 
   697 		$profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident'];
   698 		$user_ident = $profile_row['field_ident'];
   699 
   700 		$now = getdate();
   701 
   702 		if (!isset($_REQUEST[$profile_row['field_ident'] . '_day']))
   703 		{
   704 			if ($profile_row['field_default_value'] == 'now')
   705 			{
   706 				$profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
   707 			}
   708 			list($day, $month, $year) = explode('-', ((!isset($user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $user->profile_fields[$user_ident]));
   709 		}
   710 		else
   711 		{
   712 			if ($preview && $profile_row['field_default_value'] == 'now')
   713 			{
   714 				$profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
   715 				list($day, $month, $year) = explode('-', ((!isset($user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $user->profile_fields[$user_ident]));
   716 			}
   717 			else
   718 			{
   719 				$day = request_var($profile_row['field_ident'] . '_day', 0);
   720 				$month = request_var($profile_row['field_ident'] . '_month', 0);
   721 				$year = request_var($profile_row['field_ident'] . '_year', 0);
   722 			}
   723 		}
   724 
   725 		$profile_row['s_day_options'] = '<option value="0"' . ((!$day) ? ' selected="selected"' : '') . '>--</option>';
   726 		for ($i = 1; $i < 32; $i++)
   727 		{
   728 			$profile_row['s_day_options'] .= '<option value="' . $i . '"' . (($i == $day) ? ' selected="selected"' : '') . ">$i</option>";
   729 		}
   730 
   731 		$profile_row['s_month_options'] = '<option value="0"' . ((!$month) ? ' selected="selected"' : '') . '>--</option>';
   732 		for ($i = 1; $i < 13; $i++)
   733 		{
   734 			$profile_row['s_month_options'] .= '<option value="' . $i . '"' . (($i == $month) ? ' selected="selected"' : '') . ">$i</option>";
   735 		}
   736 
   737 		$profile_row['s_year_options'] = '<option value="0"' . ((!$year) ? ' selected="selected"' : '') . '>--</option>';
   738 		for ($i = $now['year'] - 100; $i <= $now['year'] + 100; $i++)
   739 		{
   740 			$profile_row['s_year_options'] .= '<option value="' . $i . '"' . (($i == $year) ? ' selected="selected"' : '') . ">$i</option>";
   741 		}
   742 		unset($now);
   743 
   744 		$profile_row['field_value'] = 0;
   745 		$template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
   746 	}
   747 
   748 	/**
   749 	* Process bool-type
   750 	* @access private
   751 	*/
   752 	function generate_bool($profile_row, $preview = false)
   753 	{
   754 		global $template;
   755 
   756 		$value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
   757 
   758 		$profile_row['field_value'] = $value;
   759 		$template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
   760 
   761 		if ($profile_row['field_length'] == 1)
   762 		{
   763 			if (!isset($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']]) || !sizeof($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']]))
   764 			{
   765 				$this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview);
   766 			}
   767 
   768 			foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value)
   769 			{
   770 				$template->assign_block_vars('bool.options', array(
   771 					'OPTION_ID'	=> $option_id,
   772 					'CHECKED'	=> ($value == $option_id) ? ' checked="checked"' : '',
   773 					'VALUE'		=> $option_value)
   774 				);
   775 			}
   776 		}
   777 	}
   778 
   779 	/**
   780 	* Process string-type
   781 	* @access private
   782 	*/
   783 	function generate_string($profile_row, $preview = false)
   784 	{
   785 		global $template;
   786 
   787 		$profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview);
   788 		$template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
   789 	}
   790 
   791 	/**
   792 	* Process text-type
   793 	* @access private
   794 	*/
   795 	function generate_text($profile_row, $preview = false)
   796 	{
   797 		global $template;
   798 		global $user, $phpEx, $phpbb_root_path;
   799 
   800 		$field_length = explode('|', $profile_row['field_length']);
   801 		$profile_row['field_rows'] = $field_length[0];
   802 		$profile_row['field_cols'] = $field_length[1];
   803 
   804 		$profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview);
   805 		$template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
   806 	}
   807 
   808 	/**
   809 	* Process dropdown-type
   810 	* @access private
   811 	*/
   812 	function generate_dropdown($profile_row, $preview = false)
   813 	{
   814 		global $user, $template;
   815 
   816 		$value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
   817 
   818 		if (!isset($this->options_lang[$profile_row['field_id']]) || !isset($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']]) || !sizeof($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']]))
   819 		{
   820 			$this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_DROPDOWN, $preview);
   821 		}
   822 
   823 		$profile_row['field_value'] = $value;
   824 		$template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
   825 
   826 		foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value)
   827 		{
   828 			$template->assign_block_vars('dropdown.options', array(
   829 				'OPTION_ID'	=> $option_id,
   830 				'SELECTED'	=> ($value == $option_id) ? ' selected="selected"' : '',
   831 				'VALUE'		=> $option_value)
   832 			);
   833 		}
   834 	}
   835 
   836 	/**
   837 	* Return Templated value/field. Possible values for $mode are:
   838 	* change == user is able to set/enter profile values; preview == just show the value
   839 	* @access private
   840 	*/
   841 	function process_field_row($mode, $profile_row)
   842 	{
   843 		global $template;
   844 
   845 		$preview = ($mode == 'preview') ? true : false;
   846 
   847 		// set template filename
   848 		$template->set_filenames(array(
   849 			'cp_body'		=> 'custom_profile_fields.html')
   850 		);
   851 
   852 		// empty previously filled blockvars
   853 		foreach ($this->profile_types as $field_case => $field_type)
   854 		{
   855 			$template->destroy_block_vars($field_type);
   856 		}
   857 
   858 		// Assign template variables
   859 		$type_func = 'generate_' . $this->profile_types[$profile_row['field_type']];
   860 		$this->$type_func($profile_row, $preview);
   861 
   862 		// Return templated data
   863 		return $template->assign_display('cp_body');
   864 	}
   865 
   866 	/**
   867 	* Build Array for user insertion into custom profile fields table
   868 	*/
   869 	function build_insert_sql_array($cp_data)
   870 	{
   871 		global $db, $user, $auth;
   872 
   873 		$sql_not_in = array();
   874 		foreach ($cp_data as $key => $null)
   875 		{
   876 			$sql_not_in[] = (strncmp($key, 'pf_', 3) === 0) ? substr($key, 3) : $key;
   877 		}
   878 
   879 		$sql = 'SELECT f.field_type, f.field_ident, f.field_default_value, l.lang_default_value
   880 			FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
   881 			WHERE l.lang_id = ' . $user->get_iso_lang_id() . '
   882 				' . ((sizeof($sql_not_in)) ? ' AND ' . $db->sql_in_set('f.field_ident', $sql_not_in, true) : '') . '
   883 				AND l.field_id = f.field_id';
   884 		$result = $db->sql_query($sql);
   885 
   886 		while ($row = $db->sql_fetchrow($result))
   887 		{
   888 			if ($row['field_default_value'] == 'now' && $row['field_type'] == FIELD_DATE)
   889 			{
   890 				$now = getdate();
   891 				$row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
   892 			}
   893 			else if ($row['field_default_value'] === '' && $row['field_type'] == FIELD_INT)
   894 			{
   895 				// We cannot insert an empty string into an integer column.
   896 				$row['field_default_value'] = NULL;
   897 			}
   898 
   899 			$cp_data['pf_' . $row['field_ident']] = (in_array($row['field_type'], array(FIELD_TEXT, FIELD_STRING))) ? $row['lang_default_value'] : $row['field_default_value'];
   900 		}
   901 		$db->sql_freeresult($result);
   902 
   903 		return $cp_data;
   904 	}
   905 
   906 	/**
   907 	* Get profile field value on submit
   908 	* @access private
   909 	*/
   910 	function get_profile_field($profile_row)
   911 	{
   912 		global $phpbb_root_path, $phpEx;
   913 		global $config;
   914 
   915 		$var_name = 'pf_' . $profile_row['field_ident'];
   916 
   917 		switch ($profile_row['field_type'])
   918 		{
   919 			case FIELD_DATE:
   920 
   921 				if (!isset($_REQUEST[$var_name . '_day']))
   922 				{
   923 					if ($profile_row['field_default_value'] == 'now')
   924 					{
   925 						$now = getdate();
   926 						$profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
   927 					}
   928 					list($day, $month, $year) = explode('-', $profile_row['field_default_value']);
   929 				}
   930 				else
   931 				{
   932 					$day = request_var($var_name . '_day', 0);
   933 					$month = request_var($var_name . '_month', 0);
   934 					$year = request_var($var_name . '_year', 0);
   935 				}
   936 
   937 				$var = sprintf('%2d-%2d-%4d', $day, $month, $year);
   938 			break;
   939 
   940 			case FIELD_BOOL:
   941 				// Checkbox
   942 				if ($profile_row['field_length'] == 2)
   943 				{
   944 					$var = (isset($_REQUEST[$var_name])) ? 1 : 0;
   945 				}
   946 				else
   947 				{
   948 					$var = request_var($var_name, (int) $profile_row['field_default_value']);
   949 				}
   950 			break;
   951 
   952 			case FIELD_STRING:
   953 			case FIELD_TEXT:
   954 				$var = utf8_normalize_nfc(request_var($var_name, (string) $profile_row['field_default_value'], true));
   955 			break;
   956 
   957 			case FIELD_INT:
   958 				if (isset($_REQUEST[$var_name]) && $_REQUEST[$var_name] === '')
   959 				{
   960 					$var = NULL;
   961 				}
   962 				else
   963 				{
   964 					$var = request_var($var_name, (int) $profile_row['field_default_value']);
   965 				}
   966 			break;
   967 
   968 			case FIELD_DROPDOWN:
   969 				$var = request_var($var_name, (int) $profile_row['field_default_value']);
   970 			break;
   971 
   972 			default:
   973 				$var = request_var($var_name, $profile_row['field_default_value']);
   974 			break;
   975 		}
   976 
   977 		return $var;
   978 	}
   979 }
   980 
   981 /**
   982 * Custom Profile Fields ACP
   983 * @package phpBB3
   984 */
   985 class custom_profile_admin extends custom_profile
   986 {
   987 	var $vars = array();
   988 
   989 	/**
   990 	* Return possible validation options
   991 	*/
   992 	function validate_options()
   993 	{
   994 		global $user;
   995 
   996 		$validate_ary = array('CHARS_ANY' => '.*', 'NUMBERS_ONLY' => '[0-9]+', 'ALPHA_ONLY' => '[\w]+', 'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+');
   997 
   998 		$validate_options = '';
   999 		foreach ($validate_ary as $lang => $value)
  1000 		{
  1001 			$selected = ($this->vars['field_validation'] == $value) ? ' selected="selected"' : '';
  1002 			$validate_options .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$lang] . '</option>';
  1003 		}
  1004 
  1005 		return $validate_options;
  1006 	}
  1007 
  1008 	/**
  1009 	* Get string options for second step in ACP
  1010 	*/
  1011 	function get_string_options()
  1012 	{
  1013 		global $user;
  1014 
  1015 		$options = array(
  1016 			0 => array('TITLE' => $user->lang['FIELD_LENGTH'],		'FIELD' => '<input type="text" name="field_length" size="5" value="' . $this->vars['field_length'] . '" />'),
  1017 			1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'],	'FIELD' => '<input type="text" name="field_minlen" size="5" value="' . $this->vars['field_minlen'] . '" />'),
  1018 			2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'],	'FIELD' => '<input type="text" name="field_maxlen" size="5" value="' . $this->vars['field_maxlen'] . '" />'),
  1019 			3 => array('TITLE' => $user->lang['FIELD_VALIDATION'],	'FIELD' => '<select name="field_validation">' . $this->validate_options() . '</select>')
  1020 		);
  1021 
  1022 		return $options;
  1023 	}
  1024 
  1025 	/**
  1026 	* Get text options for second step in ACP
  1027 	*/
  1028 	function get_text_options()
  1029 	{
  1030 		global $user;
  1031 
  1032 		$options = array(
  1033 			0 => array('TITLE' => $user->lang['FIELD_LENGTH'],		'FIELD' => '<input name="rows" size="5" value="' . $this->vars['rows'] . '" /> ' . $user->lang['ROWS'] . '</dd><dd><input name="columns" size="5" value="' . $this->vars['columns'] . '" /> ' . $user->lang['COLUMNS'] . ' <input type="hidden" name="field_length" value="' . $this->vars['field_length'] . '" />'),
  1034 			1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'],	'FIELD' => '<input type="text" name="field_minlen" size="10" value="' . $this->vars['field_minlen'] . '" />'),
  1035 			2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'],	'FIELD' => '<input type="text" name="field_maxlen" size="10" value="' . $this->vars['field_maxlen'] . '" />'),
  1036 			3 => array('TITLE' => $user->lang['FIELD_VALIDATION'],	'FIELD' => '<select name="field_validation">' . $this->validate_options() . '</select>')
  1037 		);
  1038 
  1039 		return $options;
  1040 	}
  1041 
  1042 	/**
  1043 	* Get int options for second step in ACP
  1044 	*/
  1045 	function get_int_options()
  1046 	{
  1047 		global $user;
  1048 
  1049 		$options = array(
  1050 			0 => array('TITLE' => $user->lang['FIELD_LENGTH'],		'FIELD' => '<input type="text" name="field_length" size="5" value="' . $this->vars['field_length'] . '" />'),
  1051 			1 => array('TITLE' => $user->lang['MIN_FIELD_NUMBER'],	'FIELD' => '<input type="text" name="field_minlen" size="5" value="' . $this->vars['field_minlen'] . '" />'),
  1052 			2 => array('TITLE' => $user->lang['MAX_FIELD_NUMBER'],	'FIELD' => '<input type="text" name="field_maxlen" size="5" value="' . $this->vars['field_maxlen'] . '" />'),
  1053 			3 => array('TITLE' => $user->lang['DEFAULT_VALUE'],		'FIELD' => '<input type="post" name="field_default_value" value="' . $this->vars['field_default_value'] . '" />')
  1054 		);
  1055 
  1056 		return $options;
  1057 	}
  1058 
  1059 	/**
  1060 	* Get bool options for second step in ACP
  1061 	*/
  1062 	function get_bool_options()
  1063 	{
  1064 		global $user, $config, $lang_defs;
  1065 
  1066 		$default_lang_id = $lang_defs['iso'][$config['default_lang']];
  1067 
  1068 		$profile_row = array(
  1069 			'var_name'				=> 'field_default_value',
  1070 			'field_id'				=> 1,
  1071 			'lang_name'				=> $this->vars['lang_name'],
  1072 			'lang_explain'			=> $this->vars['lang_explain'],
  1073 			'lang_id'				=> $default_lang_id,
  1074 			'field_default_value'	=> $this->vars['field_default_value'],
  1075 			'field_ident'			=> 'field_default_value',
  1076 			'field_type'			=> FIELD_BOOL,
  1077 			'field_length'			=> $this->vars['field_length'],
  1078 			'lang_options'			=> $this->vars['lang_options']
  1079 		);
  1080 
  1081 		$options = array(
  1082 			0 => array('TITLE' => $user->lang['FIELD_TYPE'], 'EXPLAIN' => $user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => '<label><input type="radio" class="radio" name="field_length" value="1"' . (($this->vars['field_length'] == 1) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $user->lang['RADIO_BUTTONS'] . '</label><label><input type="radio" class="radio" name="field_length" value="2"' . (($this->vars['field_length'] == 2) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $user->lang['CHECKBOX'] . '</label>'),
  1083 			1 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row))
  1084 		);
  1085 
  1086 		return $options;
  1087 	}
  1088 
  1089 	/**
  1090 	* Get dropdown options for second step in ACP
  1091 	*/
  1092 	function get_dropdown_options()
  1093 	{
  1094 		global $user, $config, $lang_defs;
  1095 
  1096 		$default_lang_id = $lang_defs['iso'][$config['default_lang']];
  1097 
  1098 		$profile_row[0] = array(
  1099 			'var_name'				=> 'field_default_value',
  1100 			'field_id'				=> 1,
  1101 			'lang_name'				=> $this->vars['lang_name'],
  1102 			'lang_explain'			=> $this->vars['lang_explain'],
  1103 			'lang_id'				=> $default_lang_id,
  1104 			'field_default_value'	=> $this->vars['field_default_value'],
  1105 			'field_ident'			=> 'field_default_value',
  1106 			'field_type'			=> FIELD_DROPDOWN,
  1107 			'lang_options'			=> $this->vars['lang_options']
  1108 		);
  1109 
  1110 		$profile_row[1] = $profile_row[0];
  1111 		$profile_row[1]['var_name'] = 'field_novalue';
  1112 		$profile_row[1]['field_ident'] = 'field_novalue';
  1113 		$profile_row[1]['field_default_value']	= $this->vars['field_novalue'];
  1114 
  1115 		$options = array(
  1116 			0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row[0])),
  1117 			1 => array('TITLE' => $user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->process_field_row('preview', $profile_row[1]))
  1118 		);
  1119 
  1120 		return $options;
  1121 	}
  1122 
  1123 	/**
  1124 	* Get date options for second step in ACP
  1125 	*/
  1126 	function get_date_options()
  1127 	{
  1128 		global $user, $config, $lang_defs;
  1129 
  1130 		$default_lang_id = $lang_defs['iso'][$config['default_lang']];
  1131 
  1132 		$profile_row = array(
  1133 			'var_name'				=> 'field_default_value',
  1134 			'lang_name'				=> $this->vars['lang_name'],
  1135 			'lang_explain'			=> $this->vars['lang_explain'],
  1136 			'lang_id'				=> $default_lang_id,
  1137 			'field_default_value'	=> $this->vars['field_default_value'],
  1138 			'field_ident'			=> 'field_default_value',
  1139 			'field_type'			=> FIELD_DATE,
  1140 			'field_length'			=> $this->vars['field_length']
  1141 		);
  1142 
  1143 		$always_now = request_var('always_now', -1);
  1144 		if ($always_now == -1)
  1145 		{
  1146 			$s_checked = ($this->vars['field_default_value'] == 'now') ? true : false;
  1147 		}
  1148 		else
  1149 		{
  1150 			$s_checked = ($always_now) ? true : false;
  1151 		}
  1152 
  1153 		$options = array(
  1154 			0 => array('TITLE' => $user->lang['DEFAULT_VALUE'],	'FIELD' => $this->process_field_row('preview', $profile_row)),
  1155 			1 => array('TITLE' => $user->lang['ALWAYS_TODAY'],	'FIELD' => '<label><input type="radio" class="radio" name="always_now" value="1"' . (($s_checked) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $user->lang['YES'] . '</label><label><input type="radio" class="radio" name="always_now" value="0"' . ((!$s_checked) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $user->lang['NO'] . '</label>'),
  1156 		);
  1157 
  1158 		return $options;
  1159 	}
  1160 }
  1161 
  1162 ?>