aboutsummaryrefslogtreecommitdiff
blob: ac3e9042f648a98ecca30b0b54e789af3c022f3f (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
85
86
87
88
89
90
91
92
93
94
95
96
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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
447
448
449
450
451
452
453
454
455
456
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
<?php
/**
 *  Application functions.  Dependent on lib/db.php!
 *  @package mirror
 *  @subpackage lib
 *  @todo add transactions once innodb table types are in place 
 */

class Mirror {

/**
 *  Get an alpha-list of regions for select list.
 *  @return array $regions
 */
public static function get_regions_select()
{
    $regions = DB::get("SELECT region_id,region_name FROM mirror_regions ORDER BY region_name ASC",PDO::FETCH_ASSOC);
    foreach ($regions as $region) {
        $retval[$region['region_id']]=$region['region_name'];
    }
    return $retval;
}

/**
 *  Get an priority-list of regions for select list.
 *  @return array $regions
 */
public static function get_regions_select_priority()
{
    $regions = DB::get("SELECT region_id,region_name FROM mirror_regions ORDER BY region_priority ASC",PDO::FETCH_ASSOC);
    foreach ($regions as $region) {
        $retval[$region['region_id']]=$region['region_name'];
    }
    return $retval;
}

/**
 *  Insert region.
 *  @param string $name
 *  @param int $priority
 *  @return bool
 */
public static function insert_region($name,$priority)
{
    return DB::query("INSERT INTO mirror_regions(region_name,region_priority) VALUES(?,?)", [$name, $priority]);
}

/**
 *  Update region.
 *  @param int $id
 *  @param string $name
 *  @param int $priority
 *  @return bool
 */
public static function update_region($id,$name,$priority)
{
    return DB::query("UPDATE mirror_regions SET region_name=?,region_priority=? WHERE region_id=?", [$name, $priority, $id]);
}

/**
 *  Get one region.
 *  @param int $id
 *  @return array
 */
public static function get_one_region($id)
{
    return DB::get_one("SELECT * FROM mirror_regions WHERE region_id = ?", PDO::FETCH_ASSOC, [$id]);
}

/**
 *  Delete a region.
 *  @param int $id
 *  @return bool
 */
public static function delete_region($id)
{
    return DB::query("DELETE FROM mirror_regions WHERE region_id=?", [$id]);
}

/**
 *  Get an alpha-list of mirrors for select list.
 *  @return array $mirrors
 */
public static function get_mirrors_select()
{
    $mirrors = DB::get("SELECT mirror_id,mirror_name FROM mirror_mirrors ORDER BY mirror_name ASC",PDO::FETCH_ASSOC);
    foreach ($mirrors as $mirror) {
        $retval[$mirror['mirror_id']]=$mirror['mirror_name'];
    }
    return $retval;
}

/**
 *  Get regions.
 *  @return array
 */
public static function get_regions()
{
    return DB::get("
        SELECT
            mirror_regions.*,
            COUNT(mirror_id) as mirrors
        FROM
            mirror_regions
        LEFT JOIN
            mirror_mirror_region_map
        ON
            mirror_regions.region_id = mirror_mirror_region_map.region_id
        GROUP BY
            mirror_regions.region_id
    ",PDO::FETCH_ASSOC);
}

/**
 *  Insert mirror.
 *  @param string $name
 *  @param int $region_id
 *  @param string $baseurl
 *  @param int $rating
 *  @return bool
 */
public static function insert_mirror($name,$region_id,$baseurl,$rating)
{
    return (DB::query("INSERT INTO mirror_mirrors(mirror_name,mirror_baseurl,mirror_rating) VALUES(?,?,?)", [$name, $baseurl, $rating]) &&
	DB::query("INSERT INTO mirror_mirror_region_map(mirror_id,region_id) VALUES(?,?)", [DB::insert_id(), $region_id]))?true:false;
}

/**
 *  Update mirror.
 *  @param string $name
 *  @param int $region_id
 *  @param string $baseurl
 *  @param int $rating
 *  @return bool
 */
public static function update_mirror($id,$name,$region_id,$baseurl,$rating)
{
    return (DB::query("UPDATE mirror_mirrors SET mirror_name=?,mirror_baseurl=?,mirror_rating=? WHERE mirror_id=?", [$name, $baseurl, $rating, $id])
	&& DB::query("UPDATE mirror_mirror_region_map SET region_id=? WHERE mirror_id=?", [$region_id, $id]))?true:false;
}

/**
 *  Delete mirror.
 *  @return bool
 */
public static function delete_mirror($mirror_id)
{
    return (DB::query("DELETE FROM mirror_mirrors WHERE mirror_id=?", [$mirror_id])
	&& DB::query("DELETE FROM mirror_mirror_region_map WHERE mirror_id=?", [$mirror_id]))?true:false;
}

/**
 *  Get one mirror record.
 *  @param int $mirror_id
 *  @return array mirror information
 */
public static function get_one_mirror($mirror_id)
{
    return DB::get_one("SELECT mirror_mirrors.*,region_id FROM mirror_mirrors JOIN mirror_mirror_region_map USING(mirror_id) WHERE mirror_mirrors.mirror_id=?", PDO::FETCH_ASSOC, [$mirror_id]);
}

/**
 *  Get list of mirrors.
 *  @return array
 */
public static function get_mirrors()
{
    return DB::get("
        SELECT 
            mirror_mirrors.*,
    		IF(mirror_mirrors.mirror_active='0','DISABLED','ok') as mirror_active,
            region_name
        FROM 
            mirror_mirrors,
            mirror_regions,
            mirror_mirror_region_map
        WHERE
            mirror_regions.region_id = mirror_mirror_region_map.region_id AND
            mirror_mirrors.mirror_id = mirror_mirror_region_map.mirror_id
    ",PDO::FETCH_ASSOC);
}

/**
 *  Insert product.
 *  @param string $name
 *  @param int $priority
 *  @return bool
 */ 
public static function insert_product($name,$priority)
{
    return DB::query("INSERT INTO mirror_products(product_name,product_priority) VALUES(?,?)", [$name, $priority]);
}

/**
 *  Update product.
 *  @param int $id
 *  @param string $name
 *  @param int $priority
 *  @return bool
 */ 
public static function update_product($id,$name,$priority)
{
    return DB::query("UPDATE mirror_products SET product_name=?,product_priority=? WHERE product_id=?", [$name, $priority, $id]);
}

/**
 *  Get one product.
 *  @param int $id
 *  @return array
 */
public static function get_one_product($id)
{
    return DB::get_one("SELECT * FROM mirror_products WHERE product_id = ?", PDO::FETCH_ASSOC ,[$id]);
}

/**
 *  Delete a product.
 *  @param int $id
 *  @return bool
 */
public static function delete_product($id)
{
    return DB::query("DELETE FROM mirror_products WHERE product_id=?", [$id]);
}

/**
 *  Get products.
 *  @return array
 */
public static function get_products()
{
    return DB::get("SELECT * FROM mirror_products",PDO::FETCH_ASSOC);
}

/**
 *  Insert os.
 *  @param string $name
 *  @param int $priority
 *  @return bool
 */
public static function insert_os($name,$priority)
{
    return DB::query("INSERT INTO mirror_os(os_name,os_priority) VALUES(?,?)", [$name, $priority]);
}

/**
 *  Update os.
 *  @param int $id
 *  @param string $name
 *  @param int $priority
 *  @return bool
 */
public static function update_os($id,$name,$priority)
{
    return DB::query("UPDATE mirror_os SET os_name=?,os_priority=? WHERE os_id=?", [$name, $priority, $id]);
}

/**
 *  Get one os.
 *  @param int $id
 *  @return array
 */
public static function get_one_os($id)
{
    return DB::get_one("SELECT * FROM mirror_os WHERE os_id = ?", PDO::FETCH_ASSOC, [$id]);
}

/**
 *  Delete a os.
 *  @param int $id
 *  @return bool
 */
public static function delete_os($id)
{
    return DB::query("DELETE FROM mirror_os WHERE os_id=?", [$id]);
}

/**
 *  Get operating systems.
 *  @return array
 */
public static function get_oss()
{
    return DB::get("SELECT * FROM mirror_os",PDO::FETCH_ASSOC);
}

/**
 *  Get an alpha-list of operating systems for select list.
 *  @return array $oss
 */
public static function get_oss_select()
{
    $oss = DB::get("SELECT os_id,os_name FROM mirror_os ORDER BY os_name ASC",PDO::FETCH_ASSOC);
    foreach ($oss as $os) {
        $retval[$os['os_id']]=$os['os_name'];
    }
    return $retval;
}

/**
 *  Get an priority-list of operating systems for select list.
 *  @return array $oss
 */
public static function get_oss_select_priority()
{
    $oss = DB::get("SELECT os_id,os_name FROM mirror_os ORDER BY os_priority ASC",PDO::FETCH_ASSOC);
    foreach ($oss as $os) {
        $retval[$os['os_id']]=$os['os_name'];
    }
    return $retval;
}

/**
 *  Get an alpha-list of products for select list.
 *  @return array $products
 */
public static function get_products_select()
{
    $products = DB::get("SELECT product_id,product_name FROM mirror_products ORDER BY product_name ASC",PDO::FETCH_ASSOC); 
    foreach ($products as $product) {
        $retval[$product['product_id']]=$product['product_name'];
    }
    return $retval;
}

/**
 *  Get an priority-list of operating systems for select list.
 *  @return array $oss
 */
public static function get_products_select_priority()
{
    $products = DB::get("SELECT product_id,product_name FROM mirror_products ORDER BY product_priority ASC",PDO::FETCH_ASSOC);
    foreach ($products as $product) {
        $retval[$product['product_id']]=$product['product_name'];
    }
    return $retval;
}

/**
 *  Insert a new location.
 *  @param int $product
 *  @param int $os
 *  @param string $path
 *  @return bool
 */
public static function insert_location($product,$os,$path)
{
    return DB::query("INSERT INTO mirror_locations(product_id,os_id,location_path) VALUES(?,?,?)", [$product, $os, $path]);
}

/**
 *  Update a location.
 *  @param int $location
 *  @param int $product
 *  @param int $os
 *  @param string $path
 *  @return bool
 */
public static function update_location($location,$product,$os,$path)
{
    return DB::query("UPDATE mirror_locations SET product_id=?,os_id=?,location_path=? WHERE location_id=?", [$product, $os, $path, $location]);
}

/**
 *  Delete a location.
 *  @param int $id
 *  @return bool
 */
public static function delete_location($id)
{
    return DB::query("DELETE FROM mirror_locations WHERE location_id=?", [$id]);
}

/**
 *  Get locations.
 *  @return array $locations array containing all location information.
 */
public static function get_locations()
{
    return DB::get("
        SELECT 
            location_id,
            product_name,
            os_name,
            location_path 
        FROM 
            mirror_locations,
            mirror_products,
            mirror_os
        WHERE
            mirror_locations.product_id = mirror_products.product_id AND
            mirror_locations.os_id = mirror_os.os_id
    ");
}

/**
 *  Get one location.
 *  @param int $id
 *  @return array
 */
public static function get_one_location($id)
{
    return DB::get_one("SELECT * FROM mirror_locations WHERE location_id = ?", PDO::FETCH_ASSOC, [$id]);
}

/**
 *  Insert a new user.
 *  @param string $username
 *  @param string $password
 *  @param string $rpassword (re-entered password)
 *  @param string $firstname
 *  @param string $lastname
 *  @param string $email
 *  @return bool
 */
public static function insert_user($username,$password,$rpassword,$firstname,$lastname,$email)
{
    if ($password==$rpassword) {
	$passwordHash = password_hash($password, PASSWORD_DEFAULT);
        return DB::query("INSERT INTO mirror_users(username,password,user_firstname,user_lastname,user_email) VALUES(?,?,?,?,?)",
		[$username, $passwordHash, $firstname, $lastname, $email]);
    } else {
        set_error('User could not be added because passwords did not match.');
    }
}

/**
 *  Update a user.
 *  @param int $user
 *  @param string $username
 *  @param string $password
 *  @param string $rpassword (re-entered password)
 *  @param string $firstname
 *  @param string $lastname
 *  @param string $email
 *  @return bool
 */
public static function update_user($user,$username,$password,$rpassword,$firstname,$lastname,$email)
{
    if($password==$rpassword&&!empty($password)) {
        $passwordHash = password_hash($password, PASSWORD_DEFAULT);
	return DB::query("UPDATE mirror_users SET username=?,password=?,user_firstname=?,user_lastname=?,user_email=? WHERE user_id=?",
           [$username, $passwordHash, $firstname, $lastname, $email, $user]);
    }
    return DB::query("UPDATE mirror_users SET username=?,user_firstname=?,user_lastname=?,user_email=? WHERE user_id=?",
        [$username, $firstname, $lastname, $email, $user]);
}

/**
 *  Delete a user.
 *  @param int $id
 *  @return bool
 */
public static function delete_user($id)
{
    return DB::query("DELETE FROM mirror_users WHERE user_id=?", [$id]);
}

/**
 *  Get users.
 *  @return array $users array containing all user information.
 */
public static function get_users()
{
    return DB::get("SELECT * FROM mirror_users");
}

/**
 *  Get one user.
 *  @param int $id
 *  @return array
 */
public static function get_one_user($id)
{
    return DB::get_one("SELECT * FROM mirror_users WHERE user_id = ?", PDO::FETCH_ASSOC, [$id]);
}

/**
 *  Enable or disable a mirror.
 *  @param int $mirror
 *  @return bool
 */
public static function toggle($mirror)
{
    return (DB::query("UPDATE mirror_mirrors SET mirror_active = IF(mirror_active = '1', '0', '1') WHERE mirror_id = ?", [$mirror]))?true:false;
}

/**
 *  Get mirror statistics.
 *  @return array $stats
 */
public static function get_mirror_stats()
{
    return DB::get("
        SELECT
            *,
            COUNT(mirror_log.mirror_id) as count
        FROM
            mirror_mirrors,
            mirror_log,
            mirror_regions,
            mirror_mirror_region_map
        WHERE
            mirror_log.mirror_id = mirror_mirrors.mirror_id AND
            mirror_mirrors.mirror_id = mirror_mirror_region_map.mirror_id AND
            mirror_regions.region_id = mirror_mirror_region_map.region_id
        GROUP BY
            mirror_log.mirror_id 
    ");
}

/**
 *  Get product statistics.
 *  @return array $stats
 */
public static function get_product_stats()
{
    return DB::get("
        SELECT
            *,
            COUNT(mirror_locations.product_id) as count
        FROM
            mirror_log,
            mirror_locations,
            mirror_products
        WHERE
            mirror_log.location_id = mirror_locations.location_id AND
            mirror_locations.product_id = mirror_products.product_id
        GROUP BY
            mirror_locations.product_id
    ");
}