Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
bot_update
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 depends_on
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update_data
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 add_duckduckgo_bot
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 1
20
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\db\migration\data\v33x;
15
16class bot_update extends \phpbb\db\migration\migration
17{
18    public static function depends_on()
19    {
20        return ['\phpbb\db\migration\data\v330\v330'];
21    }
22
23    public function update_data()
24    {
25        return [
26            ['custom', [[$this, 'add_duckduckgo_bot']]],
27        ];
28    }
29
30    public function add_duckduckgo_bot()
31    {
32        $bot_name = 'DuckDuckGo [Bot]';
33        $bot_name_clean = utf8_clean_string($bot_name);
34
35        $sql = 'SELECT user_id
36            FROM ' . $this->table_prefix . 'users
37            WHERE ' . $this->db->sql_build_array('SELECT', ['username_clean' => $bot_name_clean]);
38        $result = $this->db->sql_query($sql);
39        $bot_exists = (bool) $this->db->sql_fetchfield('user_id');
40        $this->db->sql_freeresult($result);
41
42        if (!$bot_exists)
43        {
44            $bot_agent = 'DuckDuckBot/';
45            $bot_ip = '';
46            $sql = 'SELECT group_id, group_colour
47                FROM ' . $this->table_prefix . 'groups
48                WHERE ' . $this->db->sql_build_array('SELECT', ['group_name' => 'BOTS']);
49            $result = $this->db->sql_query($sql);
50            $group_row = $this->db->sql_fetchrow($result);
51            $this->db->sql_freeresult($result);
52
53            // Default fallback, should never get here
54            if (!$group_row)
55            {
56                $group_row['group_id'] = 6;
57                $group_row['group_colour'] = '9E8DA7';
58            }
59
60            if (!function_exists('user_add'))
61            {
62                include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
63            }
64
65            $user_row = [
66                'user_type'                => USER_IGNORE,
67                'group_id'                => $group_row['group_id'],
68                'username'                => $bot_name,
69                'user_regdate'            => time(),
70                'user_password'            => '',
71                'user_colour'            => $group_row['group_colour'],
72                'user_email'            => '',
73                'user_lang'                => $this->config['default_lang'],
74                'user_style'            => $this->config['default_style'],
75                'user_timezone'            => 0,
76                'user_dateformat'        => $this->config['default_dateformat'],
77                'user_allow_massemail'    => 0,
78            ];
79
80            $user_id = user_add($user_row);
81            $sql = 'INSERT INTO ' . $this->table_prefix . 'bots ' . $this->db->sql_build_array('INSERT', [
82                'bot_active'    => 1,
83                'bot_name'        => (string) $bot_name,
84                'user_id'        => (int) $user_id,
85                'bot_agent'        => (string) $bot_agent,
86                'bot_ip'        => (string) $bot_ip,
87            ]);
88            $this->db->sql_query($sql);
89        }
90    }
91}