aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/db/migration/data/v310/passwords_convert_p1.php
blob: 295f2d2a1461d5cdc2fa2acb1cffbc5bdc6bb152 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\db\migration\data\v310;

class passwords_convert_p1 extends \phpbb\db\migration\migration
{
	static public function depends_on()
	{
		return array('\phpbb\db\migration\data\v310\passwords_p2');
	}

	public function update_data()
	{
		return array(
			array('custom', array(array($this, 'update_passwords'))),
		);
	}

	public function update_passwords($start)
	{
		// Nothing to do if user_pass_convert column doesn't exist
		if (!$this->db_tools->sql_column_exists($this->table_prefix . 'users', 'user_pass_convert'))
		{
			return;
		}

		$start = (int) $start;
		$limit = 1000;
		$converted_users = 0;

		$sql = 'SELECT user_password, user_id
			FROM ' . $this->table_prefix . 'users
			WHERE user_pass_convert = 1
			ORDER BY user_id';
		$result = $this->db->sql_query_limit($sql, $limit, $start);

		$update_users = array();
		while ($row = $this->db->sql_fetchrow($result))
		{
			$converted_users++;

			$user_id = (int) $row['user_id'];
			// Only prefix passwords without proper prefix
			if (!isset($update_users[$user_id]) && !preg_match('#^\$([a-zA-Z0-9\\\]*?)\$#', $row['user_password']))
			{
				// Use $CP$ prefix for passwords that need to
				// be converted and set pass convert to false.
				$update_users[$user_id] = '$CP$' . $row['user_password'];
			}
		}
		$this->db->sql_freeresult($result);

		foreach ($update_users as $user_id => $user_password)
		{
			$sql = 'UPDATE ' . $this->table_prefix . "users
				SET user_password = '" . $this->db->sql_escape($user_password) . "'
				WHERE user_id = $user_id";
			$this->sql_query($sql);
		}

		if ($converted_users < $limit)
		{
			// There are no more users to be converted
			return;
		}

		// There are still more users to query, return the next start value
		return $start + $limit;
	}
}