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