Completed
Push — master ( 4333f7...89e795 )
by Daniel
02:49
created

ReadingSecurity::getMySqlUserGrants()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 13
rs 9.2
cc 4
eloc 9
nc 5
nop 1
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2015 Daniel Popiniuc <[email protected]>.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace danielgp\rights_mysql;
28
29
/**
30
 * This script has been made to extract all users and their grants from Productrion network
31
 * for an MySQL major version upgrade (5.5.30 to 5.6.10)
32
 */
33
class ReadingSecurity
34
{
35
36
    use configurationRightsMySQL,
37
        \danielgp\common_lib\CommonCode;
38
39
    public function __construct() {
40
        $this->setHeaderNoCache('text/html');
41
        echo $this->setHeaderCommon();
42
        $this->connectToMySql($this->configuredMySqlServer());
43
        echo $this->getMySqlUserGrants($this->getMySqlUsers());
44
        echo $this->setFooterCommon();
45
    }
46
47
    private function getMySqlUsers() {
48
        return $this->setMySQLquery2Server($this->queryMySqlUsersList(), 'array_numbered')['result'];
49
    }
50
51
    private function getMySqlUserGrants($listOfMySqlUsers) {
52
        $sRtn = [];
53
        foreach ($listOfMySqlUsers as $uVal) {
54
            if ($this->mySQLconnection->server_version >= 50708) {
55
                $sRtn[] = $this->setMySQLquery2Server($this->queryShowMySqlUsersCreate([$uVal]), 'value')['result'];
56
            }
57
            $universalMySqlGrants = $this->getMySqlUserGrantsUniversal($uVal);
58
            foreach ($universalMySqlGrants as $crtUserValue) {
59
                $sRtn[] = $crtUserValue;
60
            }
61
        }
62
        return implode(';<br/>', $sRtn) . ';';
63
    }
64
65
    private function getMySqlUserGrantsUniversal($userValue) {
66
        $sReturn = [];
67
        $qry     = $this->queryShowMySqlUsersGrants([$userValue]);
68
        $result  = $this->setMySQLquery2Server($qry, 'full_array_key_numbered')['result'];
69
        foreach ($result as $value2) {
70
            foreach ($value2 as $value3) {
71
                $sReturn[] = $value3;
72
            }
73
        }
74
        return $sReturn;
75
    }
76
77
    private function queryMySqlUsersList() {
78
        return 'SELECT CONCAT("\"", `User`, "\"@\"", `Host`, "\"") AS `UserHost` '
79
                . 'FROM `mysql`.`user` '
80
                . 'ORDER BY `host`, `user`;';
81
    }
82
83
    private function queryShowMySqlUsersCreate($parameters) {
84
        return 'SHOW CREATE USER ' . filter_var($parameters[0], FILTER_SANITIZE_STRING) . ';';
85
    }
86
87
    private function queryShowMySqlUsersGrants($parameters) {
88
        return 'SHOW GRANTS FOR ' . filter_var($parameters[0], FILTER_SANITIZE_STRING) . ';';
89
    }
90
}
91