~azzar1/unity/add-show-desktop-key

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