phpBB
Statistics
| Revision:

root / trunk / phpBB / includes / functions_profile_fields.php

History | View | Annotate | Download (34.1 kB)

1
<?php
2
/**
3
*
4
* @package phpBB3
5
* @copyright (c) 2005 phpBB Group
6
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
*
8
*/
9
10
/**
11
* @ignore
12
*/
13
if (!defined('IN_PHPBB'))
14
{
15
        exit;
16
}
17
18
/**
19
* Custom Profile Fields
20
* @package phpBB3
21
*/
22
class custom_profile
23
{
24
        var $profile_types = array(FIELD_INT => 'int', FIELD_STRING => 'string', FIELD_TEXT => 'text', FIELD_BOOL => 'bool', FIELD_DROPDOWN => 'dropdown', FIELD_DATE => 'date');
25
        var $profile_cache = array();
26
        var $options_lang = array();
27
28
        /**
29
        * Assign editable fields to template, mode can be profile (for profile change) or register (for registration)
30
        * Called by ucp_profile and ucp_register
31
        * @access public
32
        */
33
        function generate_profile_fields($mode, $lang_id)
34
        {
35
                global $db, $template, $auth;
36
37
                $sql_where = '';
38
                switch ($mode)
39
                {
40
                        case 'register':
41
                                // If the field is required we show it on the registration page
42
                                $sql_where .= ' AND f.field_show_on_reg = 1';
43
                        break;
44
45
                        case 'profile':
46
                                // Show hidden fields to moderators/admins
47
                                if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
48
                                {
49
                                        $sql_where .= ' AND f.field_show_profile = 1';
50
                                }
51
                        break;
52
53
                        default:
54
                                trigger_error('Wrong profile mode specified', E_USER_ERROR);
55
                        break;
56
                }
57
58
                $sql = 'SELECT l.*, f.*
59
                        FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f
60
                        WHERE f.field_active = 1
61
                                $sql_where
62
                                AND l.lang_id = $lang_id
63
                                AND l.field_id = f.field_id
64
                        ORDER BY f.field_order";
65
                $result = $db->sql_query($sql);
66
67
                while ($row = $db->sql_fetchrow($result))
68
                {
69
                        // Return templated field
70
                        $tpl_snippet = $this->process_field_row('change', $row);
71
72
                        // Some types are multivalue, we can't give them a field_id as we would not know which to pick
73
                        $type = (int) $row['field_type'];
74
75
                        $template->assign_block_vars('profile_fields', array(
76
                                'LANG_NAME'                => $row['lang_name'],
77
                                'LANG_EXPLAIN'        => $row['lang_explain'],
78
                                'FIELD'                        => $tpl_snippet,
79
                                'FIELD_ID'                => ($type == FIELD_DATE || ($type == FIELD_BOOL && $row['field_length'] == '1')) ? '' : 'pf_' . $row['field_ident'],
80
                                'S_REQUIRED'        => ($row['field_required']) ? true : false)
81
                        );
82
                }
83
                $db->sql_freeresult($result);
84
        }
85
86
        /**
87
        * Validate entered profile field data
88
        * @access public
89
        */
90
        function validate_profile_field($field_type, &$field_value, $field_data)
91
        {
92
                switch ($field_type)
93
                {
94
                        case FIELD_DATE:
95
                                $field_validate = explode('-', $field_value);
96
97
                                $day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0;
98
                                $month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0;
99
                                $year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0;
100
101
                                if ((!$day || !$month || !$year) && !$field_data['field_required'])
102
                                {
103
                                        return false;
104
                                }
105
106
                                if ((!$day || !$month || !$year) && $field_data['field_required'])
107
                                {
108
                                        return 'FIELD_REQUIRED';
109
                                }
110
111
                                if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50)
112
                                {
113
                                        return 'FIELD_INVALID_DATE';
114
                                }
115
116
                                if (checkdate($month, $day, $year) === false)
117
                                {
118
                                        return 'FIELD_INVALID_DATE';
119
                                }
120
                        break;
121
122
                        case FIELD_BOOL:
123
                                $field_value = (bool) $field_value;
124
                        
125
                                if (!$field_value && $field_data['field_required'])
126
                                {
127
                                        return 'FIELD_REQUIRED';
128
                                }
129
                        break;
130
131
                        case FIELD_INT:
132
                                if (trim($field_value) === '' && !$field_data['field_required'])
133
                                {
134
                                        return false;
135
                                }
136
                                
137
                                $field_value = (int) $field_value;
138
139
                                if ($field_value < $field_data['field_minlen'])
140
                                {
141
                                        return 'FIELD_TOO_SMALL';
142
                                }
143
                                else if ($field_value > $field_data['field_maxlen'])
144
                                {
145
                                        return 'FIELD_TOO_LARGE';
146
                                }
147
                        break;
148
149
                        case FIELD_DROPDOWN:
150
                                $field_value = (int) $field_value;
151
152
                                // retrieve option lang data if necessary
153
                                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']]))
154
                                {
155
                                        $this->get_option_lang($field_data['field_id'], $field_data['lang_id'], FIELD_DROPDOWN, false);
156
                                }
157
158
                                if (!isset($this->options_lang[$field_data['field_id']][$field_data['lang_id']][$field_value]))
159
                                {
160
                                        return 'FIELD_INVALID_VALUE';
161
                                }
162
163
                                if ($field_value == $field_data['field_novalue'] && $field_data['field_required'])
164
                                {
165
                                        return 'FIELD_REQUIRED';
166
                                }
167
                        break;
168
169
                        case FIELD_STRING:
170
                        case FIELD_TEXT:
171
                                if (trim($field_value) === '' && !$field_data['field_required'])
172
                                {
173
                                        return false;
174
                                }
175
                                else if (trim($field_value) === '' && $field_data['field_required'])
176
                                {
177
                                        return 'FIELD_REQUIRED';
178
                                }
179
180
                                if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen'])
181
                                {
182
                                        return 'FIELD_TOO_SHORT';
183
                                }
184
                                else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen'])
185
                                {
186
                                        return 'FIELD_TOO_LONG';
187
                                }
188
189
                                if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*')
190
                                {
191
                                        $field_validate = ($field_type == FIELD_STRING) ? $field_value : bbcode_nl2br($field_value);
192
                                        if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate))
193
                                        {
194
                                                return 'FIELD_INVALID_CHARS';
195
                                        }
196
                                }
197
                        break;
198
                }
199
200
                return false;
201
        }
202
203
        /**
204
        * Build profile cache, used for display
205
        * @access private
206
        */
207
        function build_cache()
208
        {
209
                global $db, $user, $auth;
210
211
                $this->profile_cache = array();
212
213
                // Display hidden/no_view fields for admin/moderator
214
                $sql = 'SELECT l.*, f.*
215
                        FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
216
                        WHERE l.lang_id = ' . $user->get_iso_lang_id() . '
217
                                AND f.field_active = 1 ' .
218
                                ((!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_')) ? '        AND f.field_hide = 0 ' : '') . '
219
                                AND f.field_no_view = 0
220
                                AND l.field_id = f.field_id
221
                        ORDER BY f.field_order';
222
                $result = $db->sql_query($sql);
223
224
                while ($row = $db->sql_fetchrow($result))
225
                {
226
                        $this->profile_cache[$row['field_ident']] = $row;
227
                }
228
                $db->sql_freeresult($result);
229
        }
230
231
        /**
232
        * Get language entries for options and store them here for later use
233
        */
234
        function get_option_lang($field_id, $lang_id, $field_type, $preview)
235
        {
236
                global $db;
237
238
                if ($preview)
239
                {
240
                        $lang_options = (!is_array($this->vars['lang_options'])) ? explode("\n", $this->vars['lang_options']) : $this->vars['lang_options'];
241
242
                        foreach ($lang_options as $num => $var)
243
                        {
244
                                $this->options_lang[$field_id][$lang_id][($num + 1)] = $var;
245
                        }
246
                }
247
                else
248
                {
249
                        $sql = 'SELECT option_id, lang_value
250
                                FROM ' . PROFILE_FIELDS_LANG_TABLE . "
251
                                        WHERE field_id = $field_id
252
                                        AND lang_id = $lang_id
253
                                        AND field_type = $field_type
254
                                ORDER BY option_id";
255
                        $result = $db->sql_query($sql);
256
257
                        while ($row = $db->sql_fetchrow($result))
258
                        {
259
                                $this->options_lang[$field_id][$lang_id][($row['option_id'] + 1)] = $row['lang_value'];
260
                        }
261
                        $db->sql_freeresult($result);
262
                }
263
        }
264
265
        /**
266
        * Submit profile field for validation
267
        * @access public
268
        */
269
        function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error)
270
        {
271
                global $auth, $db, $user;
272
273
                $sql_where = '';
274
                switch ($mode)
275
                {
276
                        case 'register':
277
                                // If the field is required we show it on the registration page
278
                                $sql_where .= ' AND f.field_show_on_reg = 1';
279
                        break;
280
281
                        case 'profile':
282
                                // Show hidden fields to moderators/admins
283
                                if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
284
                                {
285
                                        $sql_where .= ' AND f.field_show_profile = 1';
286
                                }
287
                        break;
288
289
                        default:
290
                                trigger_error('Wrong profile mode specified', E_USER_ERROR);
291
                        break;
292
                }
293
294
                $sql = 'SELECT l.*, f.*
295
                        FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f
296
                        WHERE l.lang_id = $lang_id
297
                                AND f.field_active = 1
298
                                $sql_where
299
                                AND l.field_id = f.field_id
300
                        ORDER BY f.field_order";
301
                $result = $db->sql_query($sql);
302
303
                while ($row = $db->sql_fetchrow($result))
304
                {
305
                        $cp_data['pf_' . $row['field_ident']] = $this->get_profile_field($row);
306
                        $check_value = $cp_data['pf_' . $row['field_ident']];
307
308
                        if (($cp_result = $this->validate_profile_field($row['field_type'], $check_value, $row)) !== false)
309
                        {
310
                                // If not and only showing common error messages, use this one
311
                                $error = '';
312
                                switch ($cp_result)
313
                                {
314
                                        case 'FIELD_INVALID_DATE':
315
                                        case 'FIELD_INVALID_VALUE':
316
                                        case 'FIELD_REQUIRED':
317
                                                $error = $user->lang($cp_result, $row['lang_name']);
318
                                        break;
319
320
                                        case 'FIELD_TOO_SHORT':
321
                                        case 'FIELD_TOO_SMALL':
322
                                                $error = $user->lang($cp_result, (int) $row['field_minlen'], $row['lang_name']);
323
                                        break;
324
325
                                        case 'FIELD_TOO_LONG':
326
                                        case 'FIELD_TOO_LARGE':
327
                                                $error = $user->lang($cp_result, (int) $row['field_maxlen'], $row['lang_name']);
328
                                        break;
329
330
                                        case 'FIELD_INVALID_CHARS':
331
                                                switch ($row['field_validation'])
332
                                                {
333
                                                        case '[0-9]+':
334
                                                                $error = $user->lang($cp_result . '_NUMBERS_ONLY', $row['lang_name']);
335
                                                        break;
336
337
                                                        case '[\w]+':
338
                                                                $error = $user->lang($cp_result . '_ALPHA_ONLY', $row['lang_name']);
339
                                                        break;
340
341
                                                        case '[\w_\+\. \-\[\]]+':
342
                                                                $error = $user->lang($cp_result . '_SPACERS_ONLY', $row['lang_name']);
343
                                                        break;
344
                                                }
345
                                        break;
346
                                }
347
348
                                if ($error != '')
349
                                {
350
                                        $cp_error[] = $error;
351
                                }
352
                        }
353
                }
354
                $db->sql_freeresult($result);
355
        }
356
357
        /**
358
        * Update profile field data directly
359
        */
360
        function update_profile_field_data($user_id, &$cp_data)
361
        {
362
                global $db;
363
364
                if (!sizeof($cp_data))
365
                {
366
                        return;
367
                }
368
369
                switch ($db->sql_layer)
370
                {
371
                        case 'oracle':
372
                        case 'firebird':
373
                        case 'postgres':
374
                                $right_delim = $left_delim = '"';
375
                        break;
376
377
                        case 'sqlite':
378
                        case 'mssql':
379
                        case 'mssql_odbc':
380
                        case 'mssqlnative':
381
                                $right_delim = ']';
382
                                $left_delim = '[';
383
                        break;
384
385
                        case 'mysql':
386
                        case 'mysql4':
387
                        case 'mysqli':
388
                                $right_delim = $left_delim = '`';
389
                        break;
390
                }
391
392
                // use new array for the UPDATE; changes in the key do not affect the original array
393
                $cp_data_sql = array();
394
                foreach ($cp_data as $key => $value)
395
                {
396
                        // Firebird is case sensitive with delimiter
397
                        $cp_data_sql[$left_delim . (($db->sql_layer == 'firebird' || $db->sql_layer == 'oracle') ? strtoupper($key) : $key) . $right_delim] = $value;
398
                }
399
400
                $sql = 'UPDATE ' . PROFILE_FIELDS_DATA_TABLE . '
401
                        SET ' . $db->sql_build_array('UPDATE', $cp_data_sql) . "
402
                        WHERE user_id = $user_id";
403
                $db->sql_query($sql);
404
405
                if (!$db->sql_affectedrows())
406
                {
407
                        $cp_data_sql['user_id'] = (int) $user_id;
408
409
                        $db->sql_return_on_error(true);
410
411
                        $sql = 'INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $cp_data_sql);
412
                        $db->sql_query($sql);
413
414
                        $db->sql_return_on_error(false);
415
                }
416
        }
417
418
        /**
419
        * Assign fields to template, used for viewprofile, viewtopic and memberlist (if load setting is enabled)
420
        * 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
421
        * @access public
422
        */
423
        function generate_profile_fields_template($mode, $user_id = 0, $profile_row = false)
424
        {
425
                global $db;
426
427
                if ($mode == 'grab')
428
                {
429
                        if (!is_array($user_id))
430
                        {
431
                                $user_id = array($user_id);
432
                        }
433
434
                        if (!sizeof($this->profile_cache))
435
                        {
436
                                $this->build_cache();
437
                        }
438
439
                        if (!sizeof($user_id))
440
                        {
441
                                return array();
442
                        }
443
444
                        $sql = 'SELECT *
445
                                FROM ' . PROFILE_FIELDS_DATA_TABLE . '
446
                                WHERE ' . $db->sql_in_set('user_id', array_map('intval', $user_id));
447
                        $result = $db->sql_query($sql);
448
449
                        $field_data = array();
450
                        while ($row = $db->sql_fetchrow($result))
451
                        {
452
                                $field_data[$row['user_id']] = $row;
453
                        }
454
                        $db->sql_freeresult($result);
455
456
                        $user_fields = array();
457
458
                        // Go through the fields in correct order
459
                        foreach (array_keys($this->profile_cache) as $used_ident)
460
                        {
461
                                foreach ($field_data as $user_id => $row)
462
                                {
463
                                        $user_fields[$user_id][$used_ident]['value'] = $row['pf_' . $used_ident];
464
                                        $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident];
465
                                }
466
                        }
467
468
                        return $user_fields;
469
                }
470
                else if ($mode == 'show')
471
                {
472
                        // $profile_row == $user_fields[$row['user_id']];
473
                        $tpl_fields = array();
474
                        $tpl_fields['row'] = $tpl_fields['blockrow'] = array();
475
476
                        foreach ($profile_row as $ident => $ident_ary)
477
                        {
478
                                $value = $this->get_profile_value($ident_ary);
479
480
                                if ($value === NULL)
481
                                {
482
                                        continue;
483
                                }
484
485
                                $tpl_fields['row'] += array(
486
                                        'PROFILE_' . strtoupper($ident) . '_VALUE'        => $value,
487
                                        'PROFILE_' . strtoupper($ident) . '_TYPE'        => $ident_ary['data']['field_type'],
488
                                        'PROFILE_' . strtoupper($ident) . '_NAME'        => $ident_ary['data']['lang_name'],
489
                                        'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $ident_ary['data']['lang_explain'],
490
491
                                        'S_PROFILE_' . strtoupper($ident)                        => true
492
                                );
493
494
                                $tpl_fields['blockrow'][] = array(
495
                                        'PROFILE_FIELD_VALUE'        => $value,
496
                                        'PROFILE_FIELD_TYPE'        => $ident_ary['data']['field_type'],
497
                                        'PROFILE_FIELD_NAME'        => $ident_ary['data']['lang_name'],
498
                                        'PROFILE_FIELD_EXPLAIN'        => $ident_ary['data']['lang_explain'],
499
500
                                        'S_PROFILE_' . strtoupper($ident)                => true
501
                                );
502
                        }
503
504
                        return $tpl_fields;
505
                }
506
                else
507
                {
508
                        trigger_error('Wrong mode for custom profile', E_USER_ERROR);
509
                }
510
        }
511
512
        /**
513
        * Get Profile Value for display
514
        */
515
        function get_profile_value($ident_ary)
516
        {
517
                $value = $ident_ary['value'];
518
                $field_type = $ident_ary['data']['field_type'];
519
520
                switch ($this->profile_types[$field_type])
521
                {
522
                        case 'int':
523
                                if ($value === '')
524
                                {
525
                                        return NULL;
526
                                }
527
                                return (int) $value;
528
                        break;
529
530
                        case 'string':
531
                        case 'text':
532
                                if (!$value)
533
                                {
534
                                        return NULL;
535
                                }
536
537
                                $value = make_clickable($value);
538
                                $value = censor_text($value);
539
                                $value = bbcode_nl2br($value);
540
                                return $value;
541
                        break;
542
543
                        // case 'datetime':
544
                        case 'date':
545
                                $date = explode('-', $value);
546
                                $day = (isset($date[0])) ? (int) $date[0] : 0;
547
                                $month = (isset($date[1])) ? (int) $date[1] : 0;
548
                                $year = (isset($date[2])) ? (int) $date[2] : 0;
549
550
                                if (!$day && !$month && !$year)
551
                                {
552
                                        return NULL;
553
                                }
554
                                else if ($day && $month && $year)
555
                                {
556
                                        global $user;
557
                                        // Date should display as the same date for every user regardless of timezone, so remove offset
558
                                        // to compensate for the offset added by user::format_date()
559
                                        return $user->format_date(gmmktime(0, 0, 0, $month, $day, $year) - ($user->timezone + $user->dst), $user->lang['DATE_FORMAT'], true);
560
                                }
561
562
                                return $value;
563
                        break;
564
565
                        case 'dropdown':
566
                                $field_id = $ident_ary['data']['field_id'];
567
                                $lang_id = $ident_ary['data']['lang_id'];
568
                                if (!isset($this->options_lang[$field_id][$lang_id]))
569
                                {
570
                                        $this->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false);
571
                                }
572
573
                                if ($value == $ident_ary['data']['field_novalue'])
574
                                {
575
                                        return NULL;
576
                                }
577
578
                                $value = (int) $value;
579
580
                                // User not having a value assigned
581
                                if (!isset($this->options_lang[$field_id][$lang_id][$value]))
582
                                {
583
                                        return NULL;
584
                                }
585
586
                                return $this->options_lang[$field_id][$lang_id][$value];
587
                        break;
588
589
                        case 'bool':
590
                                $field_id = $ident_ary['data']['field_id'];
591
                                $lang_id = $ident_ary['data']['lang_id'];
592
                                if (!isset($this->options_lang[$field_id][$lang_id]))
593
                                {
594
                                        $this->get_option_lang($field_id, $lang_id, FIELD_BOOL, false);
595
                                }
596
597
                                if ($ident_ary['data']['field_length'] == 1)
598
                                {
599
                                        return (isset($this->options_lang[$field_id][$lang_id][(int) $value])) ? $this->options_lang[$field_id][$lang_id][(int) $value] : NULL;
600
                                }
601
                                else if (!$value)
602
                                {
603
                                        return NULL;
604
                                }
605
                                else
606
                                {
607
                                        return $this->options_lang[$field_id][$lang_id][(int) ($value) + 1];
608
                                }
609
                        break;
610
611
                        default:
612
                                trigger_error('Unknown profile type', E_USER_ERROR);
613
                        break;
614
                }
615
        }
616
617
        /**
618
        * Get field value for registration/profile
619
        * @access private
620
        */
621
        function get_var($field_validation, &$profile_row, $default_value, $preview)
622
        {
623
                global $user;
624
                global $request;
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->variable($profile_row['field_ident'], '') === '') ? NULL : $request->variable($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
                global $request;
915
916
                $var_name = 'pf_' . $profile_row['field_ident'];
917
918
                switch ($profile_row['field_type'])
919
                {
920
                        case FIELD_DATE:
921
922
                                if (!isset($_REQUEST[$var_name . '_day']))
923
                                {
924
                                        if ($profile_row['field_default_value'] == 'now')
925
                                        {
926
                                                $now = getdate();
927
                                                $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
928
                                        }
929
                                        list($day, $month, $year) = explode('-', $profile_row['field_default_value']);
930
                                }
931
                                else
932
                                {
933
                                        $day = request_var($var_name . '_day', 0);
934
                                        $month = request_var($var_name . '_month', 0);
935
                                        $year = request_var($var_name . '_year', 0);
936
                                }
937
938
                                $var = sprintf('%2d-%2d-%4d', $day, $month, $year);
939
                        break;
940
941
                        case FIELD_BOOL:
942
                                // Checkbox
943
                                if ($profile_row['field_length'] == 2)
944
                                {
945
                                        $var = (isset($_REQUEST[$var_name])) ? 1 : 0;
946
                                }
947
                                else
948
                                {
949
                                        $var = request_var($var_name, (int) $profile_row['field_default_value']);
950
                                }
951
                        break;
952
953
                        case FIELD_STRING:
954
                        case FIELD_TEXT:
955
                                $var = utf8_normalize_nfc(request_var($var_name, (string) $profile_row['field_default_value'], true));
956
                        break;
957
958
                        case FIELD_INT:
959
                                if (isset($_REQUEST[$var_name]) && $request->variable($var_name, '') === '')
960
                                {
961
                                        $var = NULL;
962
                                }
963
                                else
964
                                {
965
                                        $var = request_var($var_name, (int) $profile_row['field_default_value']);
966
                                }
967
                        break;
968
969
                        case FIELD_DROPDOWN:
970
                                $var = request_var($var_name, (int) $profile_row['field_default_value']);
971
                        break;
972
973
                        default:
974
                                $var = request_var($var_name, $profile_row['field_default_value']);
975
                        break;
976
                }
977
978
                return $var;
979
        }
980
}
981
982
/**
983
* Custom Profile Fields ACP
984
* @package phpBB3
985
*/
986
class custom_profile_admin extends custom_profile
987
{
988
        var $vars = array();
989
990
        /**
991
        * Return possible validation options
992
        */
993
        function validate_options()
994
        {
995
                global $user;
996
997
                $validate_ary = array('CHARS_ANY' => '.*', 'NUMBERS_ONLY' => '[0-9]+', 'ALPHA_ONLY' => '[\w]+', 'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+');
998
999
                $validate_options = '';
1000
                foreach ($validate_ary as $lang => $value)
1001
                {
1002
                        $selected = ($this->vars['field_validation'] == $value) ? ' selected="selected"' : '';
1003
                        $validate_options .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$lang] . '</option>';
1004
                }
1005
1006
                return $validate_options;
1007
        }
1008
1009
        /**
1010
        * Get string options for second step in ACP
1011
        */
1012
        function get_string_options()
1013
        {
1014
                global $user;
1015
1016
                $options = array(
1017
                        0 => array('TITLE' => $user->lang['FIELD_LENGTH'],                'FIELD' => '<input type="text" name="field_length" size="5" value="' . $this->vars['field_length'] . '" />'),
1018
                        1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'],        'FIELD' => '<input type="text" name="field_minlen" size="5" value="' . $this->vars['field_minlen'] . '" />'),
1019
                        2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'],        'FIELD' => '<input type="text" name="field_maxlen" size="5" value="' . $this->vars['field_maxlen'] . '" />'),
1020
                        3 => array('TITLE' => $user->lang['FIELD_VALIDATION'],        'FIELD' => '<select name="field_validation">' . $this->validate_options() . '</select>')
1021
                );
1022
1023
                return $options;
1024
        }
1025
1026
        /**
1027
        * Get text options for second step in ACP
1028
        */
1029
        function get_text_options()
1030
        {
1031
                global $user;
1032
1033
                $options = array(
1034
                        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'] . '" />'),
1035
                        1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'],        'FIELD' => '<input type="text" name="field_minlen" size="10" value="' . $this->vars['field_minlen'] . '" />'),
1036
                        2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'],        'FIELD' => '<input type="text" name="field_maxlen" size="10" value="' . $this->vars['field_maxlen'] . '" />'),
1037
                        3 => array('TITLE' => $user->lang['FIELD_VALIDATION'],        'FIELD' => '<select name="field_validation">' . $this->validate_options() . '</select>')
1038
                );
1039
1040
                return $options;
1041
        }
1042
1043
        /**
1044
        * Get int options for second step in ACP
1045
        */
1046
        function get_int_options()
1047
        {
1048
                global $user;
1049
1050
                $options = array(
1051
                        0 => array('TITLE' => $user->lang['FIELD_LENGTH'],                'FIELD' => '<input type="text" name="field_length" size="5" value="' . $this->vars['field_length'] . '" />'),
1052
                        1 => array('TITLE' => $user->lang['MIN_FIELD_NUMBER'],        'FIELD' => '<input type="text" name="field_minlen" size="5" value="' . $this->vars['field_minlen'] . '" />'),
1053
                        2 => array('TITLE' => $user->lang['MAX_FIELD_NUMBER'],        'FIELD' => '<input type="text" name="field_maxlen" size="5" value="' . $this->vars['field_maxlen'] . '" />'),
1054
                        3 => array('TITLE' => $user->lang['DEFAULT_VALUE'],                'FIELD' => '<input type="post" name="field_default_value" value="' . $this->vars['field_default_value'] . '" />')
1055
                );
1056
1057
                return $options;
1058
        }
1059
1060
        /**
1061
        * Get bool options for second step in ACP
1062
        */
1063
        function get_bool_options()
1064
        {
1065
                global $user, $config, $lang_defs;
1066
1067
                $default_lang_id = $lang_defs['iso'][$config['default_lang']];
1068
1069
                $profile_row = array(
1070
                        'var_name'                                => 'field_default_value',
1071
                        'field_id'                                => 1,
1072
                        'lang_name'                                => $this->vars['lang_name'],
1073
                        'lang_explain'                        => $this->vars['lang_explain'],
1074
                        'lang_id'                                => $default_lang_id,
1075
                        'field_default_value'        => $this->vars['field_default_value'],
1076
                        'field_ident'                        => 'field_default_value',
1077
                        'field_type'                        => FIELD_BOOL,
1078
                        'field_length'                        => $this->vars['field_length'],
1079
                        'lang_options'                        => $this->vars['lang_options']
1080
                );
1081
1082
                $options = array(
1083
                        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>'),
1084
                        1 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row))
1085
                );
1086
1087
                return $options;
1088
        }
1089
1090
        /**
1091
        * Get dropdown options for second step in ACP
1092
        */
1093
        function get_dropdown_options()
1094
        {
1095
                global $user, $config, $lang_defs;
1096
1097
                $default_lang_id = $lang_defs['iso'][$config['default_lang']];
1098
1099
                $profile_row[0] = array(
1100
                        'var_name'                                => 'field_default_value',
1101
                        'field_id'                                => 1,
1102
                        'lang_name'                                => $this->vars['lang_name'],
1103
                        'lang_explain'                        => $this->vars['lang_explain'],
1104
                        'lang_id'                                => $default_lang_id,
1105
                        'field_default_value'        => $this->vars['field_default_value'],
1106
                        'field_ident'                        => 'field_default_value',
1107
                        'field_type'                        => FIELD_DROPDOWN,
1108
                        'lang_options'                        => $this->vars['lang_options']
1109
                );
1110
1111
                $profile_row[1] = $profile_row[0];
1112
                $profile_row[1]['var_name'] = 'field_novalue';
1113
                $profile_row[1]['field_ident'] = 'field_novalue';
1114
                $profile_row[1]['field_default_value']        = $this->vars['field_novalue'];
1115
1116
                $options = array(
1117
                        0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row[0])),
1118
                        1 => array('TITLE' => $user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->process_field_row('preview', $profile_row[1]))
1119
                );
1120
1121
                return $options;
1122
        }
1123
1124
        /**
1125
        * Get date options for second step in ACP
1126
        */
1127
        function get_date_options()
1128
        {
1129
                global $user, $config, $lang_defs;
1130
1131
                $default_lang_id = $lang_defs['iso'][$config['default_lang']];
1132
1133
                $profile_row = array(
1134
                        'var_name'                                => 'field_default_value',
1135
                        'lang_name'                                => $this->vars['lang_name'],
1136
                        'lang_explain'                        => $this->vars['lang_explain'],
1137
                        'lang_id'                                => $default_lang_id,
1138
                        'field_default_value'        => $this->vars['field_default_value'],
1139
                        'field_ident'                        => 'field_default_value',
1140
                        'field_type'                        => FIELD_DATE,
1141
                        'field_length'                        => $this->vars['field_length']
1142
                );
1143
1144
                $always_now = request_var('always_now', -1);
1145
                if ($always_now == -1)
1146
                {
1147
                        $s_checked = ($this->vars['field_default_value'] == 'now') ? true : false;
1148
                }
1149
                else
1150
                {
1151
                        $s_checked = ($always_now) ? true : false;
1152
                }
1153
1154
                $options = array(
1155
                        0 => array('TITLE' => $user->lang['DEFAULT_VALUE'],        'FIELD' => $this->process_field_row('preview', $profile_row)),
1156
                        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>'),
1157
                );
1158
1159
                return $options;
1160
        }
1161
}