Passed
Branch main (2a1ad3)
by Thierry
06:07 queued 03:57
created

Server::moveTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\Sqlite\Db;
4
5
use Lagdo\DbAdmin\Driver\Db\Server as AbstractServer;
6
7
use DirectoryIterator;
8
use Exception;
9
10
class Server extends AbstractServer
11
{
12
    use ConfigTrait;
13
14
    /**
15
     * The database file extensions
16
     *
17
     * @var string
18
     */
19
    protected $extensions = "db|sdb|sqlite";
20
21
    /**
22
     * @inheritDoc
23
     */
24
    public function databases(bool $flush)
25
    {
26
        $databases = [];
27
        $directory = rtrim($this->driver->options('directory'), '/\\');
28
        $iterator = new DirectoryIterator($directory);
29
        // Iterate on dir content
30
        foreach($iterator as $file)
31
        {
32
            // Skip everything except Sqlite files
33
            if(!$file->isFile() || !$this->validateName($filename = $file->getFilename()))
34
            {
35
                continue;
36
            }
37
            $databases[] = $filename;
38
        }
39
        return $databases;
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function databaseSize(string $database)
46
    {
47
        $connection = $this->driver->createConnection(); // New connection
48
        $connection->open($database);
49
        $pageSize = 0;
50
        $statement = $connection->query('pragma page_size');
51
        if (is_object($statement) && ($row = $statement->fetchRow())) {
52
            $pageSize = intval($row[0]);
53
        }
54
        $pageCount = 0;
55
        $statement = $connection->query('pragma page_count');
56
        if (is_object($statement) && ($row = $statement->fetchRow())) {
57
            $pageCount = intval($row[0]);
58
        }
59
        return $pageSize * $pageCount;
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function databaseCollation(string $database, array $collations)
66
    {
67
        // there is no database list so $database == $this->driver->database()
68
        return $this->connection->result("PRAGMA encoding");
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function collations()
75
    {
76
        $create = $this->util->input()->hasTable();
77
        return ($create) ? $this->driver->values("PRAGMA collation_list", 1) : [];
78
    }
79
80
    /**
81
     * Validate a name
82
     *
83
     * @param string $name
84
     *
85
     * @return bool
86
     */
87
    private function validateName(string $name)
88
    {
89
        // Avoid creating PHP files on unsecured servers
90
        return preg_match("~^[^\\0]*\\.({$this->extensions})\$~", $name) > 0;
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    public function createDatabase(string $database, string $collation)
97
    {
98
        $options = $this->driver->options();
99
        $filename = $this->filename($database, $options);
100
        if (file_exists($filename)) {
101
            $this->driver->setError($this->trans->lang('File exists.'));
102
            return false;
103
        }
104
        if (!$this->validateName($filename)) {
105
            $this->driver->setError($this->trans->lang('Please use one of the extensions %s.',
106
                str_replace("|", ", ", $this->extensions)));
107
            return false;
108
        }
109
        try {
110
            $connection = $this->driver->createConnection(); // New connection
111
            $connection->open($database);
112
        } catch (Exception $ex) {
113
            $this->driver->setError($ex->getMessage());
114
            return false;
115
        }
116
        $connection->query('PRAGMA encoding = "UTF-8"');
117
        $connection->query('CREATE TABLE adminer (i)'); // otherwise creates empty file
118
        $connection->query('DROP TABLE adminer');
119
        return true;
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public function dropDatabases(array $databases)
126
    {
127
        $options = $this->driver->options();
128
        foreach ($databases as $database) {
129
            $filename = $this->filename($database, $options);
130
            if (!@unlink($filename)) {
131
                $this->driver->setError($this->trans->lang('File exists.'));
132
                return false;
133
            }
134
        }
135
        return true;
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    public function renameDatabase(string $database, string $collation)
142
    {
143
        $options = $this->driver->options();
144
        $filename = $this->filename($database, $options);
145
        if (!$this->validateName($filename)) {
146
            $this->driver->setError($this->trans->lang('Please use one of the extensions %s.',
147
                str_replace("|", ", ", $this->extensions)));
148
            return false;
149
        }
150
        $this->driver->setError($this->trans->lang('File exists.'));
151
        return @rename($this->filename($this->driver->database(), $options), $filename);
152
    }
153
154
    /**
155
     * @inheritDoc
156
     */
157
    public function variables()
158
    {
159
        $variables = [];
160
        foreach (["auto_vacuum", "cache_size", "count_changes", "default_cache_size", "empty_result_callbacks", "encoding", "foreign_keys", "full_column_names", "fullfsync", "journal_mode", "journal_size_limit", "legacy_file_format", "locking_mode", "page_size", "max_page_count", "read_uncommitted", "recursive_triggers", "reverse_unordered_selects", "secure_delete", "short_column_names", "synchronous", "temp_store", "temp_store_directory", "schema_version", "integrity_check", "quick_check"] as $key) {
161
            $variables[$key] = $this->connection->result("PRAGMA $key");
162
        }
163
        return $variables;
164
    }
165
166
    /**
167
     * @inheritDoc
168
     */
169
    public function statusVariables()
170
    {
171
        $variables = [];
172
        if (!($options = $this->driver->values("PRAGMA compile_options"))) {
173
            return [];
174
        }
175
        foreach ($options as $option) {
176
            $values = explode("=", $option, 2);
177
            $variables[$values[0]] = count($values) > 1 ? $values[1] : "true";
178
        }
179
        return $variables;
180
    }
181
}
182