Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
65.62% covered (warning)
65.62%
21 / 32
14.29% covered (danger)
14.29%
1 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
type_string_common
65.62% covered (warning)
65.62%
21 / 32
14.29% covered (danger)
14.29%
1 / 7
53.46
0.00% covered (danger)
0.00%
0 / 1
 validate_options
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 get_default_field_value
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validate_string_profile_field
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
12.05
 get_profile_value
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 get_profile_value_raw
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
4.59
 get_profile_contact_value
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 prepare_options_form
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\profilefields\type;
15
16abstract class type_string_common extends type_base
17{
18    protected $validation_options = array(
19        'CHARS_ANY'            => '.*',
20        'NUMBERS_ONLY'        => '[0-9]+',
21        'ALPHA_ONLY'        => '[a-zA-Z0-9]+',
22        'ALPHA_UNDERSCORE'    => '[\w]+',
23        'ALPHA_DOTS'        => '[a-zA-Z0-9.]+',
24        'ALPHA_SPACERS'        => '[\w\x20+\-\[\]]+',
25        'ALPHA_PUNCTUATION' => '[a-zA-Z][\w\.,\-]+',
26        'LETTER_NUM_ONLY'            => '[\p{Lu}\p{Ll}0-9]+',
27        'LETTER_NUM_UNDERSCORE'        => '[\p{Lu}\p{Ll}0-9_]+',
28        'LETTER_NUM_DOTS'            => '[\p{Lu}\p{Ll}0-9.]+',
29        'LETTER_NUM_SPACERS'        => '[\p{Lu}\p{Ll}0-9\x20_+\-\[\]]+',
30        'LETTER_NUM_PUNCTUATION'    => '[\p{Lu}\p{Ll}][\p{Lu}\p{Ll}0-9.,\-_]+',
31    );
32
33    /**
34    * Return possible validation options
35    */
36    public function validate_options($field_data)
37    {
38        $validate_options = '';
39        foreach ($this->validation_options as $lang => $value)
40        {
41            $selected = ($field_data['field_validation'] == $value) ? ' selected="selected"' : '';
42            $validate_options .= '<option value="' . $value . '"' . $selected . '>' . $this->user->lang[$lang] . '</option>';
43        }
44
45        return $validate_options;
46    }
47
48    /**
49    * {@inheritDoc}
50    */
51    public function get_default_field_value($field_data)
52    {
53        return $field_data['lang_default_value'];
54    }
55
56    /**
57    * Validate entered profile field data
58    *
59    * @param string    $field_type            Field type (string or text)
60    * @param mixed    $field_value        Field value to validate
61    * @param array    $field_data            Array with requirements of the field
62    * @return mixed        String with key of the error language string, false otherwise
63    */
64    public function validate_string_profile_field($field_type, &$field_value, $field_data)
65    {
66        if (trim($field_value ?? '') === '')
67        {
68            return $field_data['field_required'] ? $this->user->lang('FIELD_REQUIRED', $this->get_field_name($field_data['lang_name'])) : false;
69        }
70
71        if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen'])
72        {
73            return $this->user->lang('FIELD_TOO_SHORT', (int) $field_data['field_minlen'], $this->get_field_name($field_data['lang_name']));
74        }
75        else if ($field_data['field_maxlen'] && utf8_strlen(html_entity_decode($field_value)) > $field_data['field_maxlen'])
76        {
77            return $this->user->lang('FIELD_TOO_LONG', (int) $field_data['field_maxlen'], $this->get_field_name($field_data['lang_name']));
78        }
79
80        if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*')
81        {
82            $field_validate = ($field_type != 'text') ? $field_value : bbcode_nl2br($field_value);
83            if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#iu', $field_validate))
84            {
85                $validation = array_search($field_data['field_validation'], $this->validation_options);
86                if ($validation)
87                {
88                    return $this->user->lang('FIELD_INVALID_CHARS_' . $validation, $this->get_field_name($field_data['lang_name']));
89                }
90                return $this->user->lang('FIELD_INVALID_CHARS_INVALID', $this->get_field_name($field_data['lang_name']));
91            }
92        }
93
94        return false;
95    }
96
97    /**
98    * {@inheritDoc}
99    */
100    public function get_profile_value($field_value, $field_data)
101    {
102        if (($field_value === null || $field_value === '') && !$field_data['field_show_novalue'])
103        {
104            return null;
105        }
106
107        $field_value = make_clickable($field_value);
108        $field_value = censor_text($field_value);
109        $field_value = bbcode_nl2br($field_value);
110        return $field_value;
111    }
112
113    /**
114    * {@inheritDoc}
115    */
116    public function get_profile_value_raw($field_value, $field_data)
117    {
118        if (($field_value === null || $field_value === '') && !$field_data['field_show_novalue'])
119        {
120            return null;
121        }
122
123        return $field_value;
124    }
125
126    /**
127    * {@inheritDoc}
128    */
129    public function get_profile_contact_value($field_value, $field_data)
130    {
131        return $this->get_profile_value_raw($field_value, $field_data);
132    }
133
134    /**
135    * {@inheritDoc}
136    */
137    public function prepare_options_form(&$exclude_options, &$visibility_options)
138    {
139        $exclude_options[1][] = 'lang_default_value';
140
141        return $this->request->variable('lang_options', '', true);
142    }
143}