Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 104
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
acp_captcha
0.00% covered (danger)
0.00%
0 / 102
0.00% covered (danger)
0.00%
0 / 2
420
0.00% covered (danger)
0.00%
0 / 1
 main
0.00% covered (danger)
0.00%
0 / 96
0.00% covered (danger)
0.00%
0 / 1
380
 deliver_demo
0.00% covered (danger)
0.00%
0 / 6
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
14/**
15* @ignore
16*/
17if (!defined('IN_PHPBB'))
18{
19    exit;
20}
21
22class acp_captcha
23{
24    var $u_action;
25
26    /** @var string Template name */
27    public $tpl_name = 'acp_captcha';
28
29    /** @var string Page title language variable */
30    public $page_title = 'ACP_VC_SETTINGS';
31
32    function main($id, $mode)
33    {
34        global $user, $template, $phpbb_log, $request;
35        global $config, $phpbb_container;
36
37        $user->add_lang('acp/board');
38
39        /* @var $factory \phpbb\captcha\factory */
40        $factory = $phpbb_container->get('captcha.factory');
41        $captchas = $factory->get_captcha_types();
42
43        $selected = $request->variable('select_captcha', $config['captcha_plugin']);
44        $selected = (isset($captchas['available'][$selected]) || isset($captchas['unavailable'][$selected])) ? $selected : $config['captcha_plugin'];
45        $configure = $request->variable('configure', false);
46
47        // Oh, they are just here for the view
48        if (isset($_GET['captcha_demo']))
49        {
50            $this->deliver_demo($selected);
51        }
52
53        // Delegate
54        if ($configure)
55        {
56            $config_captcha = $factory->get_instance($selected);
57            $config_captcha->acp_page($id, $this);
58        }
59        else
60        {
61            $config_vars = array(
62                'enable_confirm'        => array(
63                    'tpl'        => 'REG_ENABLE',
64                    'default'    => false,
65                    'validate'    => 'bool',
66                    'lang'        => 'VISUAL_CONFIRM_REG',
67                ),
68                'enable_post_confirm'    => array(
69                    'tpl'        => 'POST_ENABLE',
70                    'default'    => false,
71                    'validate'    => 'bool',
72                    'lang'        => 'VISUAL_CONFIRM_POST',
73                ),
74                'confirm_refresh'        => array(
75                    'tpl'        => 'CONFIRM_REFRESH',
76                    'default'    => false,
77                    'validate'    => 'bool',
78                    'lang'        => 'VISUAL_CONFIRM_REFRESH',
79                ),
80                'max_reg_attempts'        => array(
81                    'tpl'        => 'REG_LIMIT',
82                    'default'    => 0,
83                    'validate'    => 'int:0:99999',
84                    'lang'        => 'REG_LIMIT',
85                ),
86                'max_login_attempts'    => array(
87                    'tpl'        => 'MAX_LOGIN_ATTEMPTS',
88                    'default'    => 0,
89                    'validate'    => 'int:0:99999',
90                    'lang'        => 'MAX_LOGIN_ATTEMPTS',
91                ),
92            );
93
94            $form_key = 'acp_captcha';
95            add_form_key($form_key);
96
97            $submit = $request->variable('main_submit', false);
98            $errors = $cfg_array = array();
99
100            if ($submit)
101            {
102                foreach ($config_vars as $config_var => $options)
103                {
104                    $cfg_array[$config_var] = $request->variable($config_var, $options['default']);
105                }
106                validate_config_vars($config_vars, $cfg_array, $errors);
107
108                if (!check_form_key($form_key))
109                {
110                    $errors[] = $user->lang['FORM_INVALID'];
111                }
112                if ($errors)
113                {
114                    $submit = false;
115                }
116            }
117
118            if ($submit)
119            {
120                foreach ($cfg_array as $key => $value)
121                {
122                    $config->set($key, $value);
123                }
124
125                if ($selected !== $config['captcha_plugin'])
126                {
127                    // sanity check
128                    if (isset($captchas['available'][$selected]))
129                    {
130                        $old_captcha = $factory->get_instance($config['captcha_plugin']);
131                        $old_captcha->garbage_collect();
132
133                        $config->set('captcha_plugin', $selected);
134
135                        $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_CONFIG_VISUAL');
136                    }
137                    else
138                    {
139                        trigger_error($user->lang['CAPTCHA_UNAVAILABLE'] . adm_back_link($this->u_action), E_USER_WARNING);
140                    }
141                }
142                trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action));
143            }
144            else
145            {
146                $captcha_options = [];
147                foreach ($captchas['available'] as $value => $title)
148                {
149                    $captcha_options[] = [
150                        'value'        => $value,
151                        'label'        => $user->lang($title),
152                        'selected'    => $selected !== false && $value == $selected,
153                    ];
154                }
155
156                foreach ($captchas['unavailable'] as $value => $title)
157                {
158                    $captcha_options[] = [
159                        'value'        => $value,
160                        'label'        => $user->lang($title),
161                        'selected'    => $selected !== false && $value == $selected,
162                        'class'        => 'disabled-option',
163                    ];
164                }
165
166                $demo_captcha = $factory->get_instance($selected);
167
168                foreach ($config_vars as $config_var => $options)
169                {
170                    $template->assign_var($options['tpl'], (isset($_POST[$config_var])) ? $request->variable($config_var, $options['default']) : $config[$config_var]) ;
171                }
172
173                $template->assign_vars(array(
174                    'CAPTCHA_PREVIEW_TPL'    => $demo_captcha->get_demo_template($id),
175                    'S_CAPTCHA_HAS_CONFIG'    => $demo_captcha->has_config(),
176                    'CAPTCHA_SELECT'        => [
177                        'tag'        => 'select',
178                        'name'        => 'select_captcha',
179                        'options'    => $captcha_options,
180                    ],
181                    'ERRORS'                => $errors,
182
183                    'U_ACTION'                => $this->u_action,
184                ));
185            }
186        }
187    }
188
189    /**
190    * Entry point for delivering image CAPTCHAs in the ACP.
191    */
192    function deliver_demo($selected)
193    {
194        global $phpbb_container;
195
196        $captcha = $phpbb_container->get('captcha.factory')->get_instance($selected);
197        $captcha->init(CONFIRM_REG);
198        $captcha->execute_demo();
199
200        garbage_collection();
201        exit_handler();
202    }
203}