Welcome to mirror list, hosted at ThFree Co, Russian Federation.

2.0.4-b5.php « Updates « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a96d9e4cc35e0c4068b742399402d50cace7e9ff (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Updates;

use Piwik\Common;
use Piwik\Config;
use Piwik\Date;
use Piwik\Db;
use Piwik\Plugins\UsersManager\API as UsersManagerApi;
use Piwik\Updater;
use Piwik\UpdaterErrorException;
use Piwik\Updates;
use Piwik\Updater\Migration\Factory as MigrationFactory;

/**
 */
class Updates_2_0_4_b5 extends Updates
{
    /**
     * @var MigrationFactory
     */
    private $migration;

    public function __construct(MigrationFactory $factory)
    {
        $this->migration = $factory;
    }

    public function getMigrations(Updater $updater)
    {
        return array(
            $this->migration->db->addColumn('user', 'superuser_access', "TINYINT(2) UNSIGNED NOT NULL DEFAULT '0'")
        );
    }

    public function doUpdate(Updater $updater)
    {
        $updater->executeMigrations(__FILE__, $this->getMigrations($updater));

        try {
            self::migrateConfigSuperUserToDb();
        } catch (\Exception $e) {
            throw new UpdaterErrorException($e->getMessage());
        }
    }

    private static function migrateConfigSuperUserToDb()
    {
        $config = Config::getInstance();

        if (!$config->existsLocalConfig()) {
            return;
        }

        try {
            $superUser = $config->superuser;
        } catch (\Exception $e) {
            $superUser = null;
        }

        if (!empty($superUser['bridge'])
            || empty($superUser)
            || empty($superUser['login'])
        ) {
            // there is a super user which is not from the config but from the bridge, that means we already have
            // a super user in the database
            return;
        }

        $userApi = UsersManagerApi::getInstance();

        try {
            Db::get()->insert(Common::prefixTable('user'), array(
                    'login'      => $superUser['login'],
                    'password'   => $superUser['password'],
                    'alias'      => $superUser['login'],
                    'email'      => $superUser['email'],
                    'token_auth' => md5(Common::getRandomString(32)),
                    'date_registered'  => Date::now()->getDatetime(),
                    'superuser_access' => 1
                )
            );
        } catch (\Exception $e) {
            echo "There was an issue, but we proceed: " . $e->getMessage();
        }

        if (array_key_exists('salt', $superUser)) {
            $salt = $superUser['salt'];
        } else {
            $salt = Common::generateUniqId();
        }

        $config->General['salt'] = $salt;
        $config->superuser       = array();
        $config->forceSave();
    }
}