Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
9.30% covered (danger)
9.30%
8 / 86
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
bot_update
9.30% covered (danger)
9.30%
8 / 86
50.00% covered (danger)
50.00%
2 / 4
101.28
0.00% covered (danger)
0.00%
0 / 1
 depends_on
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 update_data
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 update_bing_bot
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
20
 update_bots
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
30
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\v310;
15
16class bot_update extends \phpbb\db\migration\migration
17{
18    public static function depends_on()
19    {
20        return array(
21            '\phpbb\db\migration\data\v310\rc6',
22            '\phpbb\db\migration\data\v310\avatars',
23        );
24    }
25
26    public function update_data()
27    {
28        return array(
29            array('custom', array(array(&$this, 'update_bing_bot'))),
30            array('custom', array(array(&$this, 'update_bots'))),
31        );
32    }
33
34    public function update_bing_bot()
35    {
36        $bot_name = 'Bing [Bot]';
37        $bot_name_clean = utf8_clean_string($bot_name);
38
39        $sql = 'SELECT user_id
40            FROM ' . USERS_TABLE . "
41            WHERE username_clean = '" . $this->db->sql_escape($bot_name_clean) . "'";
42        $result = $this->db->sql_query($sql);
43        $bing_already_added = (bool) $this->db->sql_fetchfield('user_id');
44        $this->db->sql_freeresult($result);
45
46        if (!$bing_already_added)
47        {
48            $bot_agent = 'bingbot/';
49            $bot_ip = '';
50            $sql = 'SELECT group_id, group_colour
51                FROM ' . GROUPS_TABLE . "
52                WHERE group_name = 'BOTS'";
53            $result = $this->db->sql_query($sql);
54            $group_row = $this->db->sql_fetchrow($result);
55            $this->db->sql_freeresult($result);
56
57            if (!$group_row)
58            {
59                // default fallback, should never get here
60                $group_row = [];
61                $group_row['group_id'] = 6;
62                $group_row['group_colour'] = '9E8DA7';
63            }
64
65            if (!function_exists('user_add'))
66            {
67                include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
68            }
69
70            $user_row = array(
71                'user_type'                => USER_IGNORE,
72                'group_id'                => $group_row['group_id'],
73                'username'                => $bot_name,
74                'user_regdate'            => time(),
75                'user_password'            => '',
76                'user_colour'            => $group_row['group_colour'],
77                'user_email'            => '',
78                'user_lang'                => $this->config['default_lang'],
79                'user_style'            => $this->config['default_style'],
80                'user_timezone'            => 0,
81                'user_dateformat'        => $this->config['default_dateformat'],
82                'user_allow_massemail'    => 0,
83            );
84
85            $user_id = user_add($user_row);
86
87            $sql = 'INSERT INTO ' . BOTS_TABLE . ' ' . $this->db->sql_build_array('INSERT', array(
88                    'bot_active'    => 1,
89                    'bot_name'        => (string) $bot_name,
90                    'user_id'        => (int) $user_id,
91                    'bot_agent'        => (string) $bot_agent,
92                    'bot_ip'        => (string) $bot_ip,
93                ));
94
95            $this->sql_query($sql);
96        }
97    }
98
99    public function update_bots()
100    {
101        // Update bots
102        if (!function_exists('user_delete'))
103        {
104            include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
105        }
106
107        $bots_updates = array(
108            // Bot Deletions
109            'NG-Search [Bot]'        => false,
110            'Nutch/CVS [Bot]'        => false,
111            'OmniExplorer [Bot]'    => false,
112            'Seekport [Bot]'        => false,
113            'Synoo [Bot]'            => false,
114            'WiseNut [Bot]'            => false,
115
116            // Bot Updates
117            // Bot name to bot user agent map
118            'Baidu [Spider]'    => 'Baiduspider',
119            'Exabot [Bot]'        => 'Exabot',
120            'Voyager [Bot]'        => 'voyager/',
121            'W3C [Validator]'    => 'W3C_Validator',
122        );
123
124        foreach ($bots_updates as $bot_name => $bot_agent)
125        {
126            $sql = 'SELECT user_id
127                FROM ' . USERS_TABLE . '
128                WHERE user_type = ' . USER_IGNORE . "
129                    AND username_clean = '" . $this->db->sql_escape(utf8_clean_string($bot_name)) . "'";
130            $result = $this->db->sql_query($sql);
131            $bot_user_id = (int) $this->db->sql_fetchfield('user_id');
132            $this->db->sql_freeresult($result);
133
134            if ($bot_user_id)
135            {
136                if ($bot_agent === false)
137                {
138                    $sql = 'DELETE FROM ' . BOTS_TABLE . "
139                        WHERE user_id = $bot_user_id";
140                    $this->sql_query($sql);
141
142                    user_delete('retain', $bot_user_id);
143                }
144                else
145                {
146                    $sql = 'UPDATE ' . BOTS_TABLE . "
147                        SET bot_agent = '" .  $this->db->sql_escape($bot_agent) . "'
148                        WHERE user_id = $bot_user_id";
149                    $this->sql_query($sql);
150                }
151            }
152        }
153    }
154}