DBquery.php 16 KB
Newer Older
jlopez's avatar
jlopez committed
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: jimmy
 * Date: 29/01/19
 * Time: 16:38
 */

remy's avatar
remy committed
9
require_once(__DIR__.'/../inc/conf/Conf.php');
jlopez's avatar
jlopez committed
10
11
12
require_once(__DIR__.'/../model/Section.php');
require_once(__DIR__.'/../model/Manager.php');
require_once(__DIR__.'/../model/Distribution.php');
jlopez's avatar
jlopez committed
13
require_once(__DIR__.'/../model/Package.php');
jlopez's avatar
jlopez committed
14
require_once(__DIR__.'/../model/Container.php');
jlopez's avatar
jlopez committed
15
16
17
18
require_once(__DIR__.'/../model/Grade.php');
require_once(__DIR__.'/../model/Label.php');
require_once(__DIR__.'/../model/User.php');
require_once(__DIR__.'/../model/ContainerLabel.php');
jlopez's avatar
jlopez committed
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

class DBquery
{

    var $dbh = NULL;

    public function __construct() {
        $this->openConnection();
    }

    /**
     * For open a database connection
     **/
    public function openConnection() {
        $this->dbh = mysqli_connect(Conf::$DB_HOSTNAME, conf::$DB_USERNAME, Conf::$DB_PP, Conf::$DB_NAME);

        if(!$this->dbh){
            die('Could not connect: ' . mysqli_error($this->dbh));
        }

        if (mysqli_connect_errno()) {
            printf("Echec connection : %s\n", mysqli_connect_error());
            exit();
        }
    }

    /**
     * Escapes special characters in a string for use in an SQL statement
     * @param string $value
     * @return string
     */
    public function escape($value) {
        return mysqli_real_escape_string($this->dbh, $value);
    }

jlopez's avatar
jlopez committed
54
    public function getDistributions() {
jlopez's avatar
jlopez committed
55
56
57
58
        $sql = "SELECT * FROM `Distrib` ORDER BY `Distrib`.`name` ASC;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
jlopez's avatar
jlopez committed
59
60
            $d = new Distribution($row['ID'], $row['name'], $row['version'], $row['manager'], $row['active']);
            array_push($results, $d);
jlopez's avatar
jlopez committed
61
62
63
64
        }
        return $results;
    }

jlopez's avatar
jlopez committed
65
66
67
68
69
70
71
72
73
74
75
    public function getActiveDistributions() {
        $sql = "SELECT * FROM `Distrib` WHERE active = 1 ORDER BY `Distrib`.`name` ASC;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            $d = new Distribution($row['ID'], $row['name'], $row['version'], $row['manager'], $row['active']);
            array_push($results, $d);
        }
        return $results;
    }

jlopez's avatar
jlopez committed
76
77
78
79
80
81
82
83
84
85
    public function getActiveDistributionsFullName() {
        $sql = "SELECT name,version  FROM `Distrib` WHERE active = 1 ORDER BY `Distrib`.`name` ASC;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            array_push($results, $row['name'].":".$row['version']);
        }
        return $results;
    }

jlopez's avatar
jlopez committed
86
    public function getManagers() {
jlopez's avatar
jlopez committed
87
        $sql = "SELECT * FROM `Manager`;";
jlopez's avatar
jlopez committed
88
89
90
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
jlopez's avatar
jlopez committed
91
92
            $m = new Manager($row['ID'], $row['name'], $row['value']);
            array_push($results, $m);
jlopez's avatar
jlopez committed
93
94
95
96
        }
        return $results;
    }

jlopez's avatar
jlopez committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    public function getUsers() {
        $sql = "SELECT * FROM `User`;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            $u = new User($row['ID'], $row['login'], $row['gradeId']);
            array_push($results, $u);
        }
        return $results;
    }

    public function getGradeWithLogin($login) {
        $sql = "SELECT * FROM `User` WHERE `login` = '$login';";
        $result = mysqli_query($this->dbh, $sql);

        while($row = mysqli_fetch_assoc($result)){
            $u = new User($row['ID'], $row['login'], $row['gradeId']);
            return $this->getGradeWithId($u->gradeId);
        }
  
        return $this->getLowerGrades();
    }

    public function getLabelsWithGrade($grade) {

        $labels = $this->getLabels();

        $results = array();

        foreach ($labels as $label){ 

            $grd = $this->getGradeWithId($label->gradeId);

            if($grd->level >= $grade->level) {
                array_push($results, $label);
            }

        }

        return $results;
    }

    public function getGrades() {
        $sql = "SELECT * FROM `Grade` ORDER BY `level` ASC;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            $m = new Grade($row['ID'], $row['name'], $row['level']);
            array_push($results, $m);
        }
        return $results;
    }

    public function getGradeWithId($ID) {
        $sql = "SELECT * FROM `Grade` WHERE `ID` = '$ID';";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            $m = new Grade($row['ID'], $row['name'], $row['level']);
            return $m;
        }
        return $results;
    }

    public function getLowerGrades() {
        $sql = "SELECT * FROM `Grade` ORDER BY `level` DESC LIMIT 1;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            $m = new Grade($row['ID'], $row['name'], $row['level']);
jlopez's avatar
jlopez committed
167
168
169
170
171
172
173
174
175
176
177
178
            return $m;
        }
        return $results;
    }

    public function getMaxGrades() {
        $sql = "SELECT * FROM `Grade` ORDER BY `level` ASC LIMIT 1;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            $m = new Grade($row['ID'], $row['name'], $row['level']);
            return $m;
jlopez's avatar
jlopez committed
179
180
181
182
        }
        return $results;
    }

jlopez's avatar
jlopez committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    public function getContainers($type, $author, $labels, $tags, $descriptions) {
        $sql = "SELECT * FROM `Container`";

        if($type == "Singularity" || $type == "Docker") {
            $sql .= " WHERE `type` = '$type'";
        } else {
            $sql .= " WHERE 1";
        }

        if($author != NULL) {
            $sql .= " AND `author` = '$author'";
        }

        if(sizeof($labels) > 0) {
            $subsql = "(SELECT DISTINCT containerId FROM `ContainerLabel` WHERE labelId IN (";

            $index = 0;
            
            foreach ($labels as $label) {
                if($index != 0) {
                    $subsql .= ","; 
                } 

                $subsql .= $label;

                $index++;
            }
            $subsql .= "))";

            $sql .= " AND ID IN " . $subsql;
        }

        if(sizeof($tags) > 0) {
            foreach ($tags as $tag) {
                $sql .= " AND tags LIKE '%".$tag."%'";
            }
        }

        if(sizeof($descriptions) > 0) {
            foreach ($descriptions as $d) {
                $sql .= " AND description LIKE '%".$d."%'";
            }
        }

        $sql .= " ORDER BY `Container`.`date` DESC;";
        
jlopez's avatar
jlopez committed
229
230
231
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
jlopez's avatar
jlopez committed
232
            $cnt = new Container($row['ID'], $row['name'], $row['value'], $row['type'], $row['visibility'], $row['description'], $row['author'], $row['date'], $row['tags']);
jlopez's avatar
jlopez committed
233
234
235
236
237
            array_push($results, $cnt);
        }
        return $results;
    }

jlopez's avatar
jlopez committed
238
239
240
241
242
243
244
245
246
247
248
    public function getContainerWithId($id) {
        $sql = "SELECT * FROM `Container` WHERE `ID` = '$id';";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            $cnt = new Container($row['ID'], $row['name'], $row['value'], $row['type'], $row['visibility'], $row['description'], $row['author'], $row['date']);
            return $cnt;
        }
        return $results;
    }

jlopez's avatar
jlopez committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    public function getLabels() {
        $sql = "SELECT * FROM `Label`;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            $l = new Label($row['ID'], $row['name'], $row['color'], $row['gradeId']);
            array_push($results, $l);
        }
        return $results;
    }

    public function getLabelsWithId($id) {
        $sql = "SELECT * FROM `Label` WHERE `ID` = $id;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            $l = new Label($row['ID'], $row['name'], $row['color'], $row['gradeId']);
            return $l;
        }
        return $results;
    }

jlopez's avatar
jlopez committed
271
272
273
274
275
276
    public function getMaxSectionArrangement() {
        $sql = "SELECT MAX(arrangement) FROM `Section` WHERE `Section`.`parent` IS NULL;";

        
    }

jlopez's avatar
jlopez committed
277
278
279
280
281
282
283
284
285
286
287
    public function getLabelWithContainerId($id) {
        $sql = "SELECT * FROM `ContainerLabel` WHERE `containerId` = $id";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            $l = $this->getLabelsWithId($row['labelId']);
            array_push($results, $l);
        }
        return $results;
    }

jlopez's avatar
jlopez committed
288
289
290
291
292
293
294
295
296
297
298
    public function getAllActiveSectionParent() {
        $sql = "SELECT * FROM `Section` WHERE `Section`.`parent` IS NULL AND active = 1 ORDER BY `Section`.`arrangement` ASC;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            $s = new Section($row['ID'], $row['name'], $row['visual'], $row['active'], $row['color'], $row['arrangement'], $row['parent']);
            array_push($results,$s);
        }
        return $results;
    }

jlopez's avatar
jlopez committed
299
    public function getAllSectionParent() {
jlopez's avatar
jlopez committed
300
301
302
303
        $sql = "SELECT * FROM `Section` WHERE `Section`.`parent` IS NULL ORDER BY `Section`.`arrangement` ASC;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
jlopez's avatar
jlopez committed
304
            $s = new Section($row['ID'], $row['name'], $row['visual'], $row['active'], $row['color'], $row['arrangement'], $row['parent']);
jlopez's avatar
jlopez committed
305
            array_push($results,$s);
jlopez's avatar
jlopez committed
306
307
308
309
310
        }
        return $results;
    }

    public function getSectionWhithParent($parent) {
jlopez's avatar
jlopez committed
311
        $sql = "SELECT * FROM `Section` WHERE `Section`.`parent` = '$parent' AND active = 1 ORDER BY `Section`.`arrangement` ASC;";
jlopez's avatar
jlopez committed
312
313
314
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
jlopez's avatar
jlopez committed
315
            array_push($results,array($row['name'], $row['visual'], $row['active'], $row['color']));
jlopez's avatar
jlopez committed
316
317
318
319
        }
        return $results;
    }

jlopez's avatar
jlopez committed
320
321
322
323
324
325
326
327
328
329
    public function getNameSectionWhithParent($parent) {
        $sql = "SELECT name FROM `Section` WHERE `Section`.`parent` = '$parent' AND active = 1 ORDER BY `Section`.`arrangement` ASC;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            array_push($results,$row['name']);
        }
        return $results;
    }

jlopez's avatar
jlopez committed
330
331
332
333
334
    public function getAllSectionWhithParent($parent) {
        $sql = "SELECT * FROM `Section` WHERE `Section`.`parent` = '$parent' ORDER BY `Section`.`arrangement` ASC;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
jlopez's avatar
jlopez committed
335
336
337
338
339
340
341
342
343
344
345
346
            $s = new Section($row['ID'], $row['name'], $row['visual'], $row['active'], $row['color'], $row['arrangement'], $row['parent']);
            array_push($results,$s);
        }
        return $results;
    }

    public function getActiveSection() {
        $sql = "SELECT * FROM `Section` WHERE active = 1 ORDER BY `Section`.`arrangement` ASC;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
            $s = new Section($row['ID'], $row['name'], $row['visual'], $row['active'], $row['color'], $row['arrangement'], $row['parent']);
jlopez's avatar
jlopez committed
347
348
349
350
351
            array_push($results,$s);
        }
        return $results;
    }

jlopez's avatar
jlopez committed
352
353
354
355
356
    public function getSection() {
        $sql = "SELECT * FROM `Section` ORDER BY `Section`.`arrangement` ASC;";
        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
jlopez's avatar
jlopez committed
357
            $s = new Section($row['ID'], $row['name'], $row['visual'], $row['active'], $row['color'], $row['arrangement'], $row['parent']);
jlopez's avatar
jlopez committed
358
            array_push($results,$s);
jlopez's avatar
jlopez committed
359
360
361
362
        }
        return $results;
    }

jlopez's avatar
jlopez committed
363
364
365
366
367
368
369
370
371
372
373
    public function getNumberPackage($name) {

        $sql = "SELECT COUNT(*) as number FROM `$name` WHERE 1 ";

        $result = mysqli_query($this->dbh, $sql);
        while($row = mysqli_fetch_assoc($result)){
            return $row['number'];
        }
        return 0;
    }

jlopez's avatar
jlopez committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
    public function getPackages($distrib, $filter, $patternName, $patternDescription, $limit) {
        if($patternName and $patternDescription) {
            $sql = "SELECT * FROM `$distrib` WHERE `name` LIKE '%$filter%' or `description` LIKE '%$filter%' ORDER BY CASE  WHEN `name` LIKE '$filter' THEN 1 WHEN `name` LIKE '$filter%' THEN 2 WHEN `name` LIKE '%$filter' THEN 4 ELSE 3 END LIMIT $limit;";
        }
        elseif ($patternName) {
            $sql = " SELECT * FROM `$distrib` WHERE `name` LIKE '%$filter%' ORDER BY CASE  WHEN `name` LIKE '$filter' THEN 1 WHEN `name` LIKE '$filter%' THEN 2 WHEN `name` LIKE '%$filter' THEN 4 ELSE 3 END LIMIT $limit;";
        }
        elseif ($patternDescription) {
            $sql = "SELECT * FROM `$distrib` WHERE `description` LIKE '%$filter%' LIMIT $limit;";
        } else {
            return array();
        }

        $result = mysqli_query($this->dbh, $sql);
        $results = array();
        while($row = mysqli_fetch_assoc($result)){
jlopez's avatar
jlopez committed
390
            $package = new Package($row['name'], $row['version'], $row['description'], $row['doc'], $row['other']);
jlopez's avatar
jlopez committed
391
392
393
394
395
            array_push($results,$package);
        }
        return $results;
    }

jlopez's avatar
jlopez committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
    public function checkTableExist($name) {
        $sql = "SELECT count(*) as number FROM information_schema.TABLES WHERE (TABLE_SCHEMA = 'wicopa') AND (TABLE_NAME = '$name')";
        
        $result = mysqli_query($this->dbh, $sql);
        if($result) {

            while($row = mysqli_fetch_assoc($result)){
                return $row['number'];
            }

        } else {
            var_dump(mysqli_error($this->dbh));
            die();
        }
    }

    public function create($object) {

        $result = mysqli_query($this->dbh, $object->getInsert());

        if($result) {
            
        } else {
            var_dump(mysqli_error($this->dbh));
            die();
        }
    }



    public function update($object) {
        $result = mysqli_query($this->dbh, $object->getUpdate());
        if($result) {
            
        } else {
            var_dump(mysqli_error($this->dbh));
            die();
        }
    }


    public function delete($object) {
        $result = mysqli_query($this->dbh, $object->getDelete());
        if($result) {
            
        } else {
            var_dump(mysqli_error($this->dbh));
            die();
        }
    }

jlopez's avatar
jlopez committed
447
    public function verifyPass($username, $pass) {
jlopez's avatar
jlopez committed
448
        if($username == "admin" && Conf::dbEncodePass($pass)==Conf::$KK) {
jlopez's avatar
jlopez committed
449
450
            return 1;
        } else {
jlopez's avatar
jlopez committed
451
452
453
454
            # if you want guest admin
            //if($username == "guest" && $pass == "2019mbb") {
            //    return 2;
            //} else {
jlopez's avatar
jlopez committed
455
                return 0;
jlopez's avatar
jlopez committed
456
            //}
jlopez's avatar
jlopez committed
457
458
459
        }
    }

460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
    public function getNumberDockerContainer() {
        $sql = 'SELECT count(*) as docker FROM `Container` WHERE type = "Docker" AND visibility = 1;';
        $result = mysqli_query($this->dbh, $sql);
        if($result) {

            while($row = mysqli_fetch_assoc($result)){
                return $row['docker'];
            }

        }

        return 0;
    }

    public function getNumberSingularityContainer() {
        $sql = 'SELECT count(*) as singularity FROM `Container` WHERE type = "Singularity" AND visibility = 1';
        $result = mysqli_query($this->dbh, $sql);
        if($result) {

            while($row = mysqli_fetch_assoc($result)){
                return $row['singularity'];
            }

        }

        return 0;
    }

jlopez's avatar
jlopez committed
488

jlopez's avatar
jlopez committed
489
490


jlopez's avatar
jlopez committed
491
    //LOAD DATA LOCAL INFILE "/home/jimmy/jimmy/web/wicopa/packages/fedora:28/packages.csv" INTO TABLE fedora COLUMNS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\n';
jlopez's avatar
jlopez committed
492
}