aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/StudipInstaller.php
blob: 4b2f341df1cb3233177f36fc17119b567b4186fb (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
<?php
final class StudipInstaller
{
    const USERNAME_REGEX = '/^([a-zA-Z0-9_@.-]{4,})$/';
    const PASSWORD_REGEX = '/^([[:print:]]{8,72})$/';

    private $base_path;

    public function __construct($base_path)
    {
        $this->base_path = rtrim($base_path, '/');
    }

    public function updateConfigInDatabase(PDO $pdo, $key, $value)
    {
        $query = "INSERT INTO `config_values` (
                    `field`, `range_id`, `value`, `mkdate`, `chdate`, `comment`
                  ) VALUES (
                    :field, 'studip', :value, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), ''
                  )
                  ON DUPLICATE KEY
                     UPDATE `value` = VALUES(`value`),
                            `chdate` = VALUES(`chdate`)";
        $statement = $pdo->prepare($query);
        $statement->bindValue(':field', $key);
        $statement->bindValue(':value', $value);
        return $statement->execute();
    }

    public function createConfigLocalInc($host, $user, $password, $database, $env, $uri)
    {
        $template = file_get_contents($this->base_path . '/config/config_local.inc.php.dist');

        $replacements = [
            'DB_STUDIP_HOST'     => $host,
            'DB_STUDIP_USER'     => $user,
            'DB_STUDIP_PASSWORD' => $password,
            'DB_STUDIP_DATABASE' => $database,

            'ABSOLUTE_URI_STUDIP' => rtrim($uri, '/') . '/',
        ];
        foreach ($replacements as $needle => $replacement) {
            $template = $this->replaceVariable($needle, $replacement, $template);
        }

        $template = $this->replaceConst('ENV', $env, $template);

        return $template;
    }

    public function createConfigInc($uni_url, $uni_contact)
    {
        $template = file_get_contents($this->base_path . '/config/config.inc.php.dist');

        $replacements = [
            'UNI_URL'     => $uni_url,
            'UNI_CONTACT' => $uni_contact
        ];
        foreach ($replacements as $needle => $replacement) {
            $template = $this->replaceVariable($needle, $replacement, $template);
        }

        return $template;
    }

    private function replaceVariable($variable, $replacement, $subject)
    {
        return preg_replace(
            '/(?:\/\/\\s*)?(\$' . $variable. '\\s*=\\s*(["\']))(?:.*)(?:\2);/',
            "\${$variable} = '{$replacement}';",
            $subject
        );
    }

    private function replaceConst($constant, $replacement, $subject)
    {
        return preg_replace(
            '/const\\s+' . $constant . '\\s*=\\s*(["\'])(?:.*?)(?:\1);/',
            "const {$constant} = '{$replacement}';",
            $subject
        );

    }
}