Test Setup Failed
Push — master ( f1e81a...a627c0 )
by Tomasz
12:28
created

updateFromMonitor::updateInstAdminTable()   B

Complexity

Conditions 9
Paths 38

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 21
c 1
b 0
f 0
nc 38
nop 1
dl 0
loc 23
rs 8.0555
1
<?php
2
/*
3
 * *****************************************************************************
4
 * Contributions to this work were made on behalf of the GÉANT project, a 
5
 * project that has received funding from the European Union’s Framework 
6
 * Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus),
7
 * Horizon 2020 research and innovation programme under Grant Agreements No. 
8
 * 691567 (GN4-1) and No. 731122 (GN4-2).
9
 * On behalf of the aforementioned projects, GEANT Association is the sole owner
10
 * of the copyright in all material which was developed by a member of the GÉANT
11
 * project. GÉANT Vereniging (Association) is registered with the Chamber of 
12
 * Commerce in Amsterdam with registration number 40535155 and operates in the 
13
 * UK as a branch of GÉANT Vereniging.
14
 * 
15
 * Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. 
16
 * UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK
17
 *
18
 * License: see the web/copyright.inc.php file in the file structure or
19
 *          <base_url>/copyright.php after deploying the software
20
 */
21
22
/**
23
 * This script will download all data from the views in eduroam database and
24
 * update the local copy
25
 */
26
27
require_once dirname(dirname(dirname(__FILE__))) . "/config/_config.php";
28
setlocale(LC_CTYPE, "en_US.UTF-8");
29
require 'config.php';
30
31
32
class updateFromMonitor {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
Class name "updateFromMonitor" is not in PascalCase format

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
33
    private $db;
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
34
    private $db_local;
35
    public $databases;
36
   
37
    public $fields = [
38
        'eduroam' => [
39
            'admin' => [
40
                ['id', 'i'],
41
                ['eptid', 's'],
42
                ['email', 's'],
43
                ['common_name', 's'],
44
                ['id_role', 'i'],
45
                ['role', 's'],
46
                ['realm', 's']
47
            ],
48
            'active_institution' => [
49
                ['id_institution', 'i'],
50
                ['ROid', 's'],
51
                ['inst_realm', 's'],
52
                ['country', 's'],
53
                ['name', 's'],
54
                ['contact', 's'],
55
                ['type', 's']
56
            ],
57
        ],
58
        'eduroamv2' => [
59
            'tls_ro' => [
60
                ['ROid', 's'],
61
                ['country', 's'],
62
                ['stage', 'i'],
63
                ['servers', 's'],
64
                ['contacts', 's'],
65
                ['ts', 's']
66
            ],
67
            'tls_inst' => [
68
                ['ROid', 's'],
69
                ['country', 's'],
70
                ['instid', 's'],
71
                ['stage', 'i'],
72
                ['type', 'i'],
73
                ['inst_name', 's'],
74
                ['servers', 's'],
75
                ['contacts', 's'],
76
                ['ts', 's']
77
            ],
78
            'active_institution' => [
79
                ['instid', 's'],
80
                ['ROid', 's'],
81
                ['inst_realm', 's'],
82
                ['country', 's'],
83
                ['name', 's'],
84
                ['contact', 's'],
85
                ['type', 'i'],
86
                ['last_change', 's']
87
            ],          
88
        ]
89
    ];
90
91
    public function __construct($databases) {
92
            $DB_EXT = \config\Master::DB['EXTERNAL_SOURCE'];
93
            $DB_LOCAL = \config\Master::DB['EXTERNAL'];
94
            $this->db_ext = new mysqli($DB_EXT['host'], $DB_EXT['user'], $DB_EXT['pass']);
95
            $this->db_local = new mysqli($DB_LOCAL['host'], $DB_LOCAL['user'], $DB_LOCAL['pass']);
96
            $this->databases = $databases;
97
    }
98
    
99
    /**
100
     * Creates a temporary table with data collected from the source
101
     * 
102
     * @param type $db
103
     * @param type $table_name
104
     */
105
106
    public function updateTable($db, $table_name) {
107
        print "Updating $db.$table_name\n";
108
        $this->db_local->select_db($this->databases[$db]);
0 ignored issues
show
Bug Best Practice introduced by
The property databases does not exist on updateFromMonitor. Did you maybe forget to declare it?
Loading history...
109
        $table = 'view_'.$table_name;
110
        $tmpTable = 'tmp_'.$table_name;
111
        $this->db_local->query("CREATE TEMPORARY TABLE $tmpTable SELECT * FROM $table LIMIT 0");
112
        $this->db_ext->select_db($db);
0 ignored issues
show
Bug Best Practice introduced by
The property db_ext does not exist on updateFromMonitor. Did you maybe forget to declare it?
Loading history...
113
        $this->db_ext->query("SET NAMES 'utf8'");
114
        $this->db_local->query("SET NAMES 'utf8mb4'");
115
        $result = $this->db_ext->query("SELECT * FROM $table");
116
        $queryFields = implode(',', array_column($this->fields[$db][$table_name],0));
117
        while ($row = $result->fetch_assoc()) {
118
            $v = [];
119
            foreach ($this->fields[$db][$table_name] as $field) {
120
                if ($field[1] === 's') {
121
                    if (isset($row[$field[0]])) {
122
                        $v[] = $this->escape($row[$field[0]]);
123
                    } else {
124
                        $v[] = "NULL";
125
                    }
126
                } else {
127
                    if (isset($row[$field[0]])) {
128
                        $v[] = $row[$field[0]];
129
                    } else {
130
                        $v[] = "NULL";
131
                    }
132
                }
133
            }
134
            $queryValues = implode(',',$v);
135
            $query = "INSERT INTO $tmpTable (".$queryFields.") VALUES (".$queryValues.")";
136
            $this->db_local->query($query);
137
        }
138
    }
139
    
140
    public function updateInstAdminTable($db) {
141
        $this->db_local->select_db($this->databases[$db]);
0 ignored issues
show
Bug Best Practice introduced by
The property databases does not exist on updateFromMonitor. Did you maybe forget to declare it?
Loading history...
142
        if ($db == 'eduroam') {
143
            $instid = "id_institution";
144
            $q = "SELECT convert(contact using utf8mb4), inst_realm, id_institution FROM view_active_institution";
0 ignored issues
show
Unused Code introduced by
The assignment to $q is dead and can be removed.
Loading history...
145
        } else {
146
            $instid = "instid";
147
        }
148
        $q = "SELECT convert(contact using utf8mb4), inst_realm, $instid FROM view_active_institution";
149
        $this->db_local->query("CREATE TEMPORARY TABLE tmp_institution_admins SELECT * FROM view_institution_admins LIMIT 0");
150
        $result = $this->db_local->query($q);
151
        while ($row = $result->fetch_row()) {
152
            $contacts = \core\ExternalEduroamDBData::dissectCollapsedContacts($row[0]);
153
            $realms = explode(',', $row[1]);
154
            foreach ($contacts as $contact) {
155
                foreach ($realms as $realm) {
156
                    $email = empty($contact['mail']) ? 'NULL' :'"'.$contact['mail'].'"';
157
                    $name = empty($contact['name']) ? 'NULL' :'"'.$contact['name'].'"';
158
                    $phone = empty($contact['phone']) ? 'NULL' :'"'.$contact['phone'].'"';
159
                    $id = $db == 'eduroam' ? $row[2] : '"'.$row[2].'"';
160
                    $query = "INSERT INTO tmp_institution_admins (name, email, phone, inst_realm, $instid)"
161
                            . ' VALUES ('.$name.','.$email.','.$phone.',"'.$realm.'",'.$id.')';
162
                    $this->db_local->query($query);
163
                }
164
            }
165
        }
166
    }
167
168
    public function fillTable($db, $table_name) {
169
        $table = 'view_'.$table_name;
170
        $tmpTable = 'tmp_'.$table_name;
171
        $this->db_local->select_db($this->databases[$db]);
0 ignored issues
show
Bug Best Practice introduced by
The property databases does not exist on updateFromMonitor. Did you maybe forget to declare it?
Loading history...
172
        $this->db_local->query("SET NAMES 'utf8mb4'");
173
        $this->db_local->query("DELETE FROM $table");
174
        $this->db_local->query("INSERT INTO $table SELECT * from $tmpTable");
175
    }
176
177
    private function escape($inp) {
178
        $out=str_replace('\\','\\\\',$inp);
179
        $out=str_replace('"','\"',$out);
180
        $out=str_replace('?','\?',$out);
181
        $out = 'convert(cast(convert("'.$out.'" using latin1) as binary) using utf8)';
182
        return($out);
183
    }
184
}
185
186
187
$myDB = new updateFromMonitor($databases);
0 ignored issues
show
Unused Code introduced by
The call to updateFromMonitor::__construct() has too many arguments starting with $databases. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

187
$myDB = /** @scrutinizer ignore-call */ new updateFromMonitor($databases);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
188
189
foreach (array_keys($myDB->fields['eduroamv2']) as $table) {
190
    $myDB->updateTable('eduroamv2', $table);
0 ignored issues
show
Bug introduced by
The method updateTable() does not exist on updateFromMonitor. Did you maybe mean update_table()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
    $myDB->/** @scrutinizer ignore-call */ 
191
           updateTable('eduroamv2', $table);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
191
}
192
193
print "Starting filling tables for eduroamv2\n";
194
foreach (array_keys($myDB->fields['eduroamv2']) as $table) {
195
    $myDB->fillTable('eduroamv2', $table);
0 ignored issues
show
Bug introduced by
The method fillTable() does not exist on updateFromMonitor. Did you maybe mean fill_table()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

195
    $myDB->/** @scrutinizer ignore-call */ 
196
           fillTable('eduroamv2', $table);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
196
}
197
print "Finished filling tables for eduroamv2\n";
198
199
foreach (array_keys($myDB->fields['eduroam']) as $table) {
200
    $myDB->updateTable('eduroam', $table);
201
}
202
print "Starting filling tables for eduroam\n";
203
204
foreach (array_keys($myDB->fields['eduroam']) as $table) {
205
    $myDB->fillTable('eduroam', $table);
206
}
207
print "Finished filling tables for eduroam\n";
208
209
print "Starting filling tables for eduroam\n";
210
$myDB->updateInstAdminTable('eduroam');
0 ignored issues
show
Bug introduced by
The method updateInstAdminTable() does not exist on updateFromMonitor. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

210
$myDB->/** @scrutinizer ignore-call */ 
211
       updateInstAdminTable('eduroam');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
211
212
$myDB->fillTable('eduroam', 'institution_admins');
213
print "Finished filling tables for eduroam\n";
214
215
$myDB->updateInstAdminTable('eduroamv2');
216
217
$myDB->fillTable('eduroamv2', 'institution_admins');
218
print "Finished filling tables for eduroam\n";
219