1 |
commit: b167721d5746c0d1791febbb213da0a0aed21aa2 |
2 |
Author: Brian Evans <grknight <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Jan 29 21:07:33 2018 +0000 |
4 |
Commit: Brian Evans <grknight <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon Jan 29 21:07:33 2018 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/bouncer.git/commit/?id=b167721d |
7 |
|
8 |
Modernize the database and mirror libraries |
9 |
|
10 |
php/admin/index.php | 13 ++- |
11 |
php/admin/locations.php | 10 +- |
12 |
php/admin/login.php | 4 +- |
13 |
php/admin/logout.php | 4 +- |
14 |
php/admin/mirror-list.php | 25 +++-- |
15 |
php/admin/mstats.php | 3 +- |
16 |
php/admin/os.php | 10 +- |
17 |
php/admin/products.php | 10 +- |
18 |
php/admin/pstats.php | 3 +- |
19 |
php/admin/regions.php | 11 +- |
20 |
php/admin/users.php | 10 +- |
21 |
php/cfg/admin_init.php | 5 +- |
22 |
php/cfg/init.php | 4 +- |
23 |
php/inc/forms/location.php | 6 +- |
24 |
php/inc/forms/mirror.php | 3 +- |
25 |
php/index-list.php | 25 +++-- |
26 |
php/index.php | 43 ++++---- |
27 |
php/lib/auth.php | 60 +++++++---- |
28 |
php/lib/csv.php | 4 +- |
29 |
php/lib/db.php | 249 +++++++++++--------------------------------- |
30 |
php/lib/mirror.php | 205 ++++++++++++++++++------------------ |
31 |
php/rss/download-counts.php | 6 +- |
32 |
22 files changed, 293 insertions(+), 420 deletions(-) |
33 |
|
34 |
diff --git a/php/admin/index.php b/php/admin/index.php |
35 |
index 8c3db5c..e8feaec 100644 |
36 |
--- a/php/admin/index.php |
37 |
+++ b/php/admin/index.php |
38 |
@@ -9,7 +9,7 @@ require_once('../cfg/init.php'); |
39 |
|
40 |
// add mirror |
41 |
if (!empty($_POST['add-submit'])) { |
42 |
- if (mirror_insert_mirror($_POST['mirror_name'],$_POST['region_id'],$_POST['mirror_baseurl'],$_POST['mirror_rating'])) { |
43 |
+ if (Mirror::insert_mirror($_POST['mirror_name'],$_POST['region_id'],$_POST['mirror_baseurl'],$_POST['mirror_rating'])) { |
44 |
set_msg('Mirror added successfully.'); |
45 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/'); |
46 |
exit; |
47 |
@@ -24,7 +24,7 @@ if (!empty($_POST['submit'])) { |
48 |
switch($_POST['action']) { |
49 |
case 'edit': |
50 |
if (!empty($_POST['doit'])) { |
51 |
- if (mirror_update_mirror($_POST['mirror_id'],$_POST['mirror_name'],$_POST['region_id'],$_POST['mirror_baseurl'],$_POST['mirror_rating'])) { |
52 |
+ if (Mirror::update_mirror($_POST['mirror_id'],$_POST['mirror_name'],$_POST['region_id'],$_POST['mirror_baseurl'],$_POST['mirror_rating'])) { |
53 |
set_msg('Mirror updated successfully.'); |
54 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/'); |
55 |
exit; |
56 |
@@ -36,7 +36,7 @@ if (!empty($_POST['submit'])) { |
57 |
$nav = INC.'/admin_nav.php'; |
58 |
require_once(HEADER); |
59 |
echo '<h2>Edit Mirror</h2>'; |
60 |
- $posts = mirror_get_one_mirror($_POST['mirror_id']); |
61 |
+ $posts = Mirror::get_one_mirror($_POST['mirror_id']); |
62 |
form_start(); |
63 |
include_once(INC.'/forms/mirror.php'); |
64 |
form_hidden('doit','1'); |
65 |
@@ -49,14 +49,14 @@ if (!empty($_POST['submit'])) { |
66 |
} |
67 |
break; |
68 |
case 'delete': |
69 |
- if (mirror_delete_mirror($_POST['mirror_id'])) { |
70 |
+ if (Mirror::delete_mirror($_POST['mirror_id'])) { |
71 |
set_msg('Mirror deleted successfully.'); |
72 |
} else { |
73 |
set_error('Mirror could not be deleted.'); |
74 |
} |
75 |
break; |
76 |
case 'toggle': |
77 |
- if (mirror_toggle($_POST['mirror_id'])) { |
78 |
+ if (Mirror::toggle($_POST['mirror_id'])) { |
79 |
set_msg('Mirror enabled/disabled.'); |
80 |
} else { |
81 |
set_error('Mirror could not be enabled/disabled.'); |
82 |
@@ -75,7 +75,7 @@ echo '<h2>Mirrors</h2>'; |
83 |
show_error(); |
84 |
show_msg(); |
85 |
|
86 |
-$mirrors = mirror_get_mirrors(); |
87 |
+$mirrors = Mirror::get_mirrors(); |
88 |
|
89 |
$_GET['sort']=(!empty($_GET['sort']))?$_GET['sort']:'mirror_active'; |
90 |
$_GET['order']=(!empty($_GET['order']))?$_GET['order']:'ASC'; |
91 |
@@ -108,4 +108,3 @@ form_submit('add-submit','','button1','Add Mirror'); |
92 |
form_end(); |
93 |
|
94 |
require_once(FOOTER); |
95 |
-?> |
96 |
|
97 |
diff --git a/php/admin/locations.php b/php/admin/locations.php |
98 |
index f1bb456..e868d1a 100644 |
99 |
--- a/php/admin/locations.php |
100 |
+++ b/php/admin/locations.php |
101 |
@@ -9,7 +9,7 @@ require_once('../cfg/init.php'); |
102 |
|
103 |
// add mirror |
104 |
if (!empty($_POST['add-submit'])&&!empty($_POST['location_path'])) { |
105 |
- if (mirror_insert_location($_POST['product_id'],$_POST['os_id'],$_POST['location_path'])) { |
106 |
+ if (Mirror::insert_location($_POST['product_id'],$_POST['os_id'],$_POST['location_path'])) { |
107 |
set_msg('Location added successfully.'); |
108 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/locations.php'); |
109 |
exit; |
110 |
@@ -24,7 +24,7 @@ if (!empty($_POST['submit'])) { |
111 |
switch($_POST['action']) { |
112 |
case 'edit': |
113 |
if (!empty($_POST['doit'])) { |
114 |
- if (mirror_update_location($_POST['location_id'],$_POST['product_id'],$_POST['os_id'],$_POST['location_path'])) { |
115 |
+ if (Mirror::update_location($_POST['location_id'],$_POST['product_id'],$_POST['os_id'],$_POST['location_path'])) { |
116 |
set_msg('Location updated successfully.'); |
117 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/locations.php'); |
118 |
exit; |
119 |
@@ -36,7 +36,7 @@ if (!empty($_POST['submit'])) { |
120 |
$nav = INC.'/admin_nav.php'; |
121 |
require_once(HEADER); |
122 |
echo '<h2>Edit Location</h2>'; |
123 |
- $posts = mirror_get_one_location($_POST['location_id']); |
124 |
+ $posts = Mirror::get_one_location($_POST['location_id']); |
125 |
form_start(); |
126 |
include_once(INC.'/forms/location.php'); |
127 |
form_hidden('doit','1'); |
128 |
@@ -49,7 +49,7 @@ if (!empty($_POST['submit'])) { |
129 |
} |
130 |
break; |
131 |
case 'delete': |
132 |
- if (mirror_delete_location($_POST['location_id'])) { |
133 |
+ if (Mirror::delete_location($_POST['location_id'])) { |
134 |
set_msg('Location deleted successfully.'); |
135 |
} else { |
136 |
set_error('Location could not be deleted.'); |
137 |
@@ -68,7 +68,7 @@ echo '<h2>Locations</h2>'; |
138 |
show_error(); |
139 |
show_msg(); |
140 |
|
141 |
-$locations = mirror_get_locations(); |
142 |
+$locations = Mirror::get_locations(); |
143 |
|
144 |
$_GET['sort'] = (!empty($_GET['sort']))?$_GET['sort']:'product_name'; |
145 |
$_GET['order'] = (!empty($_GET['order']))?$_GET['order']:'ASC'; |
146 |
|
147 |
diff --git a/php/admin/login.php b/php/admin/login.php |
148 |
index 5e9d4c3..455c556 100644 |
149 |
--- a/php/admin/login.php |
150 |
+++ b/php/admin/login.php |
151 |
@@ -9,8 +9,8 @@ require_once(LIB.'/auth.php'); |
152 |
|
153 |
// authenticate |
154 |
if (!empty($_POST['submit'])) { |
155 |
- if ($auth = auth_mysql($_POST['username'],$_POST['password'])) { |
156 |
- auth_create_session($auth); |
157 |
+ if ($auth = Auth::query($_POST['username'],$_POST['password'])) { |
158 |
+ Auth::create_session($auth); |
159 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/'); |
160 |
exit; |
161 |
} else { |
162 |
|
163 |
diff --git a/php/admin/logout.php b/php/admin/logout.php |
164 |
index d980d9d..f876efe 100644 |
165 |
--- a/php/admin/logout.php |
166 |
+++ b/php/admin/logout.php |
167 |
@@ -6,7 +6,5 @@ |
168 |
*/ |
169 |
require_once('../cfg/init.php'); |
170 |
require_once(LIB.'/auth.php'); |
171 |
-auth_logout(); |
172 |
+Auth::logout(); |
173 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/login.php'); |
174 |
-exit; |
175 |
-?> |
176 |
|
177 |
diff --git a/php/admin/mirror-list.php b/php/admin/mirror-list.php |
178 |
index c161efd..e0876d4 100644 |
179 |
--- a/php/admin/mirror-list.php |
180 |
+++ b/php/admin/mirror-list.php |
181 |
@@ -9,20 +9,20 @@ require_once('../cfg/init.php'); |
182 |
|
183 |
if (!empty($_GET['os'])&&!empty($_GET['product'])) { |
184 |
// clean in os and product strings |
185 |
- $os_name = mysql_real_escape_string(trim(strtolower($_GET['os']))); |
186 |
- $product_name = mysql_real_escape_string(trim(strtolower($_GET['product']))); |
187 |
+ $os_name = trim(strtolower(filter_input(INPUT_GET,'os'))); |
188 |
+ $product_name = trim(strtolower(filter_input(INPUT_GET,'product'))); |
189 |
// get os and product IDs |
190 |
- $os_id = db_name_to_id('mirror_os','os_id','os_name',$os_name); |
191 |
- $product_id = db_name_to_id('mirror_products','product_id','product_name',$product_name); |
192 |
+ $os_id = DB::name_to_id('mirror_os','os_id','os_name',$os_name); |
193 |
+ $product_id = DB::name_to_id('mirror_products','product_id','product_name',$product_name); |
194 |
} |
195 |
|
196 |
if (!empty($_GET['os_id'])&&!empty($_GET['product_id'])) { |
197 |
|
198 |
- $os_id = intval($_GET['os_id']); |
199 |
- $product_id = intval($_GET['product_id']); |
200 |
+ $os_id = intval(filter_input(INPUT_GET,'os_id',FILTER_SANITIZE_NUMBER_INT)); |
201 |
+ $product_id = intval(filter_input(INPUT_GET,'product_id',FILTER_SANITIZE_NUMBER_INT)); |
202 |
|
203 |
|
204 |
- $mirrors = db_get(" |
205 |
+ $mirrors = DB::get(" |
206 |
SELECT DISTINCT |
207 |
mirror_baseurl |
208 |
FROM |
209 |
@@ -36,11 +36,11 @@ if (!empty($_GET['os_id'])&&!empty($_GET['product_id'])) { |
210 |
ON |
211 |
mirror_location_mirror_map.location_id = mirror_locations.location_id |
212 |
WHERE |
213 |
- mirror_locations.os_id = {$os_id} AND |
214 |
- mirror_locations.product_id = {$product_id} AND |
215 |
+ mirror_locations.os_id = ? AND |
216 |
+ mirror_locations.product_id = ? AND |
217 |
mirror_location_mirror_map.location_active = '1' AND |
218 |
mirror_mirrors.mirror_active = '1' |
219 |
- "); |
220 |
+ ", PDO::FETCH_ASSOC, [$os_id, $product_id]); |
221 |
|
222 |
header("Content-type: text/plain;"); |
223 |
foreach ($mirrors as $mirror) { |
224 |
@@ -58,17 +58,16 @@ if (!empty($_GET['os_id'])&&!empty($_GET['product_id'])) { |
225 |
form_start('list','list','get','./mirror-list.php'); |
226 |
echo '<div>'; |
227 |
form_label('Product', 'product','label-small'); |
228 |
- form_select('product_id','product','',mirror_get_products_select(),$posts['product_id']); |
229 |
+ form_select('product_id','product','',Mirror::get_products_select(),$posts['product_id']); |
230 |
echo ' [<a href="./products.php">edit products</a>]'; |
231 |
echo '</div><br />'; |
232 |
|
233 |
echo '<div>'; |
234 |
form_label('OS', 'os','label-small'); |
235 |
- form_select('os_id','os','',mirror_get_oss_select(),$posts['os_id']); |
236 |
+ form_select('os_id','os','',Mirror::get_oss_select(),$posts['os_id']); |
237 |
echo ' [<a href="./os.php">edit operating systems</a>]'; |
238 |
echo '</div><br />'; |
239 |
form_submit('submit','','button1','Update'); |
240 |
form_end(); |
241 |
require_once(FOOTER); |
242 |
} |
243 |
-?> |
244 |
|
245 |
diff --git a/php/admin/mstats.php b/php/admin/mstats.php |
246 |
index 5778d16..284dcf4 100644 |
247 |
--- a/php/admin/mstats.php |
248 |
+++ b/php/admin/mstats.php |
249 |
@@ -12,7 +12,7 @@ $nav = INC.'/admin_nav.php'; |
250 |
require_once(HEADER); |
251 |
echo '<h2>Mirror Statistics</h2>'; |
252 |
|
253 |
-$stats = mirror_get_mirror_stats(); |
254 |
+$stats = Mirror::get_mirror_stats(); |
255 |
|
256 |
$_GET['sort']=(!empty($_GET['sort']))?$_GET['sort']:'count'; |
257 |
$_GET['order']=(!empty($_GET['order']))?$_GET['order']:'DESC'; |
258 |
@@ -29,4 +29,3 @@ $headers = array( |
259 |
show_list($stats,$headers,'simple'); |
260 |
|
261 |
require_once(FOOTER); |
262 |
-?> |
263 |
|
264 |
diff --git a/php/admin/os.php b/php/admin/os.php |
265 |
index b150245..72bb383 100644 |
266 |
--- a/php/admin/os.php |
267 |
+++ b/php/admin/os.php |
268 |
@@ -9,7 +9,7 @@ require_once('../cfg/init.php'); |
269 |
|
270 |
// add os |
271 |
if (!empty($_POST['add-submit'])&&!empty($_POST['os_name'])) { |
272 |
- if (mirror_insert_os($_POST['os_name'],$_POST['os_priority'])) { |
273 |
+ if (Mirror::insert_os($_POST['os_name'],$_POST['os_priority'])) { |
274 |
set_msg('OS added successfully.'); |
275 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/os.php'); |
276 |
exit; |
277 |
@@ -24,7 +24,7 @@ if (!empty($_POST['submit'])) { |
278 |
switch($_POST['action']) { |
279 |
case 'edit': |
280 |
if (!empty($_POST['doit'])) { |
281 |
- if (mirror_update_os($_POST['os_id'],$_POST['os_name'],$_POST['os_priority'])) { |
282 |
+ if (Mirror::update_os($_POST['os_id'],$_POST['os_name'],$_POST['os_priority'])) { |
283 |
set_msg('OS updated successfully.'); |
284 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/os.php'); |
285 |
exit; |
286 |
@@ -36,7 +36,7 @@ if (!empty($_POST['submit'])) { |
287 |
$nav = INC.'/admin_nav.php'; |
288 |
require_once(HEADER); |
289 |
echo '<h2>Edit OS</h2>'; |
290 |
- $posts = mirror_get_one_os($_POST['os_id']); |
291 |
+ $posts = Mirror::get_one_os($_POST['os_id']); |
292 |
form_start(); |
293 |
include_once(INC.'/forms/os.php'); |
294 |
form_hidden('doit','1'); |
295 |
@@ -49,7 +49,7 @@ if (!empty($_POST['submit'])) { |
296 |
} |
297 |
break; |
298 |
case 'delete': |
299 |
- if (!record_exists('mirror_locations','os_id',$_POST['os_id'])&&mirror_delete_os($_POST['os_id'])) { |
300 |
+ if (!record_exists('mirror_locations','os_id',$_POST['os_id'])&&Mirror::delete_os($_POST['os_id'])) { |
301 |
set_msg('OS deleted successfully.'); |
302 |
} else { |
303 |
set_error('OS cannot be deleted because it is being used by a file location.'); |
304 |
@@ -69,7 +69,7 @@ echo '<h2>Operating Systems</h1>'; |
305 |
show_error(); |
306 |
show_msg(); |
307 |
|
308 |
-$oss = mirror_get_oss(); |
309 |
+$oss = Mirror::get_oss(); |
310 |
|
311 |
$_GET['sort']=(!empty($_GET['sort']))?$_GET['sort']:'os_name'; |
312 |
$_GET['order']=(!empty($_GET['order']))?$_GET['order']:'ASC'; |
313 |
|
314 |
diff --git a/php/admin/products.php b/php/admin/products.php |
315 |
index e93686f..a0d5187 100644 |
316 |
--- a/php/admin/products.php |
317 |
+++ b/php/admin/products.php |
318 |
@@ -9,7 +9,7 @@ require_once('../cfg/init.php'); |
319 |
|
320 |
// add product |
321 |
if (!empty($_POST['add-submit'])&&!empty($_POST['product_name'])) { |
322 |
- if (mirror_insert_product($_POST['product_name'],$_POST['product_priority'])) { |
323 |
+ if (Mirror::insert_product($_POST['product_name'],$_POST['product_priority'])) { |
324 |
set_msg('Product added successfully.'); |
325 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/products.php'); |
326 |
exit; |
327 |
@@ -24,7 +24,7 @@ if (!empty($_POST['submit'])) { |
328 |
switch($_POST['action']) { |
329 |
case 'edit': |
330 |
if (!empty($_POST['doit'])) { |
331 |
- if (mirror_update_product($_POST['product_id'],$_POST['product_name'],$_POST['product_priority'])) { |
332 |
+ if (Mirror::update_product($_POST['product_id'],$_POST['product_name'],$_POST['product_priority'])) { |
333 |
set_msg('Product updated successfully.'); |
334 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/products.php'); |
335 |
exit; |
336 |
@@ -36,7 +36,7 @@ if (!empty($_POST['submit'])) { |
337 |
$nav = INC.'/admin_nav.php'; |
338 |
require_once(HEADER); |
339 |
echo '<h2>Edit Product</h2>'; |
340 |
- $posts = mirror_get_one_product($_POST['product_id']); |
341 |
+ $posts = Mirror::get_one_product($_POST['product_id']); |
342 |
form_start(); |
343 |
include_once(INC.'/forms/product.php'); |
344 |
form_hidden('doit','1'); |
345 |
@@ -49,7 +49,7 @@ if (!empty($_POST['submit'])) { |
346 |
} |
347 |
break; |
348 |
case 'delete': |
349 |
- if (!record_exists('mirror_locations','product_id',$_POST['product_id'])&&mirror_delete_product($_POST['product_id'])) { |
350 |
+ if (!record_exists('mirror_locations','product_id',$_POST['product_id'])&&Mirror::delete_product($_POST['product_id'])) { |
351 |
set_msg('Product deleted successfully.'); |
352 |
} else { |
353 |
set_error('Product cannot be deleted because it is being used by a file location.'); |
354 |
@@ -69,7 +69,7 @@ echo '<h2>Products</h1>'; |
355 |
show_error(); |
356 |
show_msg(); |
357 |
|
358 |
-$products = mirror_get_products(); |
359 |
+$products = Mirror::get_products(); |
360 |
|
361 |
$_GET['sort']=(!empty($_GET['sort']))?$_GET['sort']:'product_name'; |
362 |
$_GET['order']=(!empty($_GET['order']))?$_GET['order']:'ASC'; |
363 |
|
364 |
diff --git a/php/admin/pstats.php b/php/admin/pstats.php |
365 |
index e0b4aff..ef887bd 100644 |
366 |
--- a/php/admin/pstats.php |
367 |
+++ b/php/admin/pstats.php |
368 |
@@ -12,7 +12,7 @@ $nav = INC.'/admin_nav.php'; |
369 |
require_once(HEADER); |
370 |
echo '<h2>Product Statistics</h2>'; |
371 |
|
372 |
-$stats = mirror_get_product_stats(); |
373 |
+$stats = Mirror::get_product_stats(); |
374 |
|
375 |
$_GET['sort']=(!empty($_GET['sort']))?$_GET['sort']:'count'; |
376 |
$_GET['order']=(!empty($_GET['order']))?$_GET['order']:'DESC'; |
377 |
@@ -26,4 +26,3 @@ $headers = array( |
378 |
show_list($stats,$headers,'simple'); |
379 |
|
380 |
require_once(FOOTER); |
381 |
-?> |
382 |
|
383 |
diff --git a/php/admin/regions.php b/php/admin/regions.php |
384 |
index f42c0da..0709e43 100644 |
385 |
--- a/php/admin/regions.php |
386 |
+++ b/php/admin/regions.php |
387 |
@@ -9,7 +9,7 @@ require_once('../cfg/init.php'); |
388 |
|
389 |
// add region |
390 |
if (!empty($_POST['add-submit'])&&!empty($_POST['region_name'])) { |
391 |
- if (mirror_insert_region($_POST['region_name'],$_POST['region_priority'])) { |
392 |
+ if (Mirror::insert_region($_POST['region_name'],$_POST['region_priority'])) { |
393 |
set_msg('Region added successfully.'); |
394 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/regions.php'); |
395 |
exit; |
396 |
@@ -24,7 +24,7 @@ if (!empty($_POST['submit'])) { |
397 |
switch($_POST['action']) { |
398 |
case 'edit': |
399 |
if (!empty($_POST['doit'])) { |
400 |
- if (mirror_update_region($_POST['region_id'],$_POST['region_name'],$_POST['region_priority'])) { |
401 |
+ if (Mirror::update_region($_POST['region_id'],$_POST['region_name'],$_POST['region_priority'])) { |
402 |
set_msg('Region updated successfully.'); |
403 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/regions.php'); |
404 |
exit; |
405 |
@@ -36,7 +36,7 @@ if (!empty($_POST['submit'])) { |
406 |
$nav = INC.'/admin_nav.php'; |
407 |
require_once(HEADER); |
408 |
echo '<h2>Edit Region</h2>'; |
409 |
- $posts = mirror_get_one_region($_POST['region_id']); |
410 |
+ $posts = Mirror::get_one_region($_POST['region_id']); |
411 |
form_start(); |
412 |
include_once(INC.'/forms/region.php'); |
413 |
form_hidden('doit','1'); |
414 |
@@ -49,7 +49,7 @@ if (!empty($_POST['submit'])) { |
415 |
} |
416 |
break; |
417 |
case 'delete': |
418 |
- if (!record_exists('mirror_mirror_region_map','region_id',$_POST['region_id'])&&mirror_delete_region($_POST['region_id'])) { |
419 |
+ if (!record_exists('mirror_mirror_region_map','region_id',$_POST['region_id'])&&Mirror::delete_region($_POST['region_id'])) { |
420 |
set_msg('Region deleted successfully.'); |
421 |
} else { |
422 |
set_error('Region cannot be deleted because it is linked to a mirror.'); |
423 |
@@ -69,7 +69,7 @@ echo '<h2>Regions</h2>'; |
424 |
show_error(); |
425 |
show_msg(); |
426 |
|
427 |
-$regions = mirror_get_regions(); |
428 |
+$regions = Mirror::get_regions(); |
429 |
|
430 |
$_GET['sort']=(!empty($_GET['sort']))?$_GET['sort']:'region_name'; |
431 |
$_GET['order']=(!empty($_GET['order']))?$_GET['order']:'ASC'; |
432 |
@@ -98,4 +98,3 @@ form_submit('add-submit','','button1','Add Region'); |
433 |
form_end(); |
434 |
|
435 |
require_once(FOOTER); |
436 |
-?> |
437 |
|
438 |
diff --git a/php/admin/users.php b/php/admin/users.php |
439 |
index 3017c49..4926117 100644 |
440 |
--- a/php/admin/users.php |
441 |
+++ b/php/admin/users.php |
442 |
@@ -9,7 +9,7 @@ require_once('../cfg/init.php'); |
443 |
|
444 |
// add user |
445 |
if (!empty($_POST['add-submit'])&&!empty($_POST['username'])&&!empty($_POST['password'])&&!empty($_POST['rpassword'])) { |
446 |
- if (mirror_insert_user($_POST['username'],$_POST['password'],$_POST['rpassword'],$_POST['user_firstname'],$_POST['user_lastname'],$_POST['user_email'])) { |
447 |
+ if (Mirror::insert_user($_POST['username'],$_POST['password'],$_POST['rpassword'],$_POST['user_firstname'],$_POST['user_lastname'],$_POST['user_email'])) { |
448 |
set_msg('User added successfully.'); |
449 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/users.php'); |
450 |
exit; |
451 |
@@ -24,7 +24,7 @@ if (!empty($_POST['submit'])) { |
452 |
switch($_POST['action']) { |
453 |
case 'edit': |
454 |
if (!empty($_POST['doit'])) { |
455 |
- if (mirror_update_user($_POST['user_id'],$_POST['username'],$_POST['password'],$_POST['rpassword'],$_POST['user_firstname'],$_POST['user_lastname'],$_POST['user_email'])) { |
456 |
+ if (Mirror::update_user($_POST['user_id'],$_POST['username'],$_POST['password'],$_POST['rpassword'],$_POST['user_firstname'],$_POST['user_lastname'],$_POST['user_email'])) { |
457 |
set_msg('User updated successfully.'); |
458 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/users.php'); |
459 |
exit; |
460 |
@@ -36,7 +36,7 @@ if (!empty($_POST['submit'])) { |
461 |
$nav = INC.'/admin_nav.php'; |
462 |
require_once(HEADER); |
463 |
echo '<h2>Edit User</h2>'; |
464 |
- $posts = mirror_get_one_user($_POST['user_id']); |
465 |
+ $posts = Mirror::get_one_user($_POST['user_id']); |
466 |
form_start(); |
467 |
include_once(INC.'/forms/user.php'); |
468 |
form_hidden('doit','1'); |
469 |
@@ -51,7 +51,7 @@ if (!empty($_POST['submit'])) { |
470 |
case 'delete': |
471 |
if ($_POST['user_id']==$_SESSION['user']['user_id']) { |
472 |
set_error('You cannot delete yourself.'); |
473 |
- } elseif (mirror_delete_user($_POST['user_id'])) { |
474 |
+ } elseif (Mirror::delete_user($_POST['user_id'])) { |
475 |
set_msg('User deleted successfully.'); |
476 |
} else { |
477 |
set_error('User could not be deleted because of an error.'); |
478 |
@@ -71,7 +71,7 @@ echo '<h2>Users</h2>'; |
479 |
show_error(); |
480 |
show_msg(); |
481 |
|
482 |
-$users = mirror_get_users(); |
483 |
+$users = Mirror::get_users(); |
484 |
|
485 |
$_GET['sort'] = (!empty($_GET['sort']))?$_GET['sort']:'user_lastname'; |
486 |
$_GET['order'] = (!empty($_GET['order']))?$_GET['order']:'ASC'; |
487 |
|
488 |
diff --git a/php/cfg/admin_init.php b/php/cfg/admin_init.php |
489 |
index 7d22ab3..68f3063 100644 |
490 |
--- a/php/cfg/admin_init.php |
491 |
+++ b/php/cfg/admin_init.php |
492 |
@@ -6,10 +6,9 @@ |
493 |
*/ |
494 |
require_once(LIB.'/auth.php'); // auth functions |
495 |
require_once(LIB.'/forms.php'); // form library |
496 |
-require_once(LIB.'/list.php'); // list library |
497 |
|
498 |
-if (!auth_is_valid_session()) { |
499 |
+if (!Auth::is_valid_session()) { |
500 |
header('Location: http://'.$_SERVER['HTTP_HOST'].WEBPATH.'/admin/login.php'); |
501 |
exit; |
502 |
} |
503 |
-?> |
504 |
+ |
505 |
|
506 |
diff --git a/php/cfg/init.php b/php/cfg/init.php |
507 |
index 4eb6ed0..3e8f6fb 100644 |
508 |
--- a/php/cfg/init.php |
509 |
+++ b/php/cfg/init.php |
510 |
@@ -10,9 +10,7 @@ require_once(LIB.'/csv.php'); // util file for random functions (no SQL here) |
511 |
$start = microtime_float(); // start timer |
512 |
require_once(LIB.'/mirror.php'); // user and admin functions for the mirror app (some SQL) |
513 |
require_once(LIB.'/db.php'); // core mysql wrappers used in mirror functions |
514 |
-db_connect(DBHOST,DBUSER,DBPASS); // open persistent connection to db |
515 |
-db_select(DBNAME); // select db |
516 |
+DB::connect(DBHOST,DBUSER,DBPASS,DBNAME); // open persistent connection to db |
517 |
if (!empty($protect)) { |
518 |
require_once('admin_init.php'); |
519 |
} |
520 |
-?> |
521 |
|
522 |
diff --git a/php/inc/forms/location.php b/php/inc/forms/location.php |
523 |
index 60e35cb..0d48d2d 100644 |
524 |
--- a/php/inc/forms/location.php |
525 |
+++ b/php/inc/forms/location.php |
526 |
@@ -6,13 +6,13 @@ |
527 |
*/ |
528 |
echo '<div>'; |
529 |
form_label('Product', 'product','label-small'); |
530 |
-form_select('product_id','product','',mirror_get_products_select(),$posts['product_id']); |
531 |
+form_select('product_id','product','',Mirror::get_products_select(),$posts['product_id']); |
532 |
echo ' [<a href="./products.php">edit products</a>]'; |
533 |
echo '</div><br />'; |
534 |
|
535 |
echo '<div>'; |
536 |
form_label('OS', 'os','label-small'); |
537 |
-form_select('os_id','os','',mirror_get_oss_select(),$posts['os_id']); |
538 |
+form_select('os_id','os','',Mirror::get_oss_select(),$posts['os_id']); |
539 |
echo ' [<a href="./os.php">edit operating systems</a>]'; |
540 |
echo '</div><br />'; |
541 |
|
542 |
@@ -20,4 +20,4 @@ echo '<div>'; |
543 |
form_label('File Location', 'floc','label-small'); |
544 |
form_text('location_path', 'floc', '', $posts['location_path'], 30, 100); |
545 |
echo '</div><br />'; |
546 |
-?> |
547 |
+ |
548 |
|
549 |
diff --git a/php/inc/forms/mirror.php b/php/inc/forms/mirror.php |
550 |
index c1e7dcb..35a3ed7 100644 |
551 |
--- a/php/inc/forms/mirror.php |
552 |
+++ b/php/inc/forms/mirror.php |
553 |
@@ -11,7 +11,7 @@ echo '</div><br />'; |
554 |
|
555 |
echo '<div>'; |
556 |
form_label('Region', 'mregion','label-small'); |
557 |
-form_select('region_id','mregion','',mirror_get_regions_select(),$posts['region_id']); |
558 |
+form_select('region_id','mregion','',Mirror::get_regions_select(),$posts['region_id']); |
559 |
echo ' [<a href="./regions.php">edit regions</a>]'; |
560 |
echo '</div><br />'; |
561 |
|
562 |
@@ -24,4 +24,3 @@ echo '<div>'; |
563 |
form_label('Rating', 'mrating','label-small'); |
564 |
form_text('mirror_rating', 'mrating', '', $posts['mirror_rating'], 30, 100); |
565 |
echo '</div><br />'; |
566 |
-?> |
567 |
|
568 |
diff --git a/php/index-list.php b/php/index-list.php |
569 |
index 1705ee6..1b158b7 100644 |
570 |
--- a/php/index-list.php |
571 |
+++ b/php/index-list.php |
572 |
@@ -7,25 +7,24 @@ |
573 |
require_once('./cfg/init.php'); |
574 |
require_once(LIB.'/auth.php'); // auth functions |
575 |
require_once(LIB.'/forms.php'); // form library |
576 |
-require_once(LIB.'/list.php'); // list library |
577 |
error_reporting(E_GET); |
578 |
|
579 |
if (!empty($_GET['os'])&&!empty($_GET['product'])) { |
580 |
// clean in os and product strings |
581 |
- $os_name = mysql_real_escape_string(trim(strtolower($_GET['os']))); |
582 |
- $product_name = mysql_real_escape_string(trim(strtolower($_GET['product']))); |
583 |
+ $os_name = trim(strtolower(filter_input(INPUT_GET,'os'))); |
584 |
+ $product_name = trim(strtolower(filter_input(INPUT_GET,'product'))); |
585 |
// get os and product IDs |
586 |
- $os_id = db_name_to_id('mirror_os','os_id','os_name',$os_name); |
587 |
- $product_id = db_name_to_id('mirror_products','product_id','product_name',$product_name); |
588 |
+ $os_id = DB::name_to_id('mirror_os','os_id','os_name',$os_name); |
589 |
+ $product_id = DB::name_to_id('mirror_products','product_id','product_name',$product_name); |
590 |
} |
591 |
if (!empty($_GET['os_id'])&&!empty($_GET['product_id'])) { |
592 |
- $os_id = intval($_GET['os_id']); |
593 |
- $product_id = intval($_GET['product_id']); |
594 |
+ $os_id = intval(filter_input(INPUT_GET, 'os_id', FILTER_SANITIZE_NUMBER_INT)); |
595 |
+ $product_id = intval(filter_input(INPUT_GET, 'product_id', FILTER_SANITIZE_NUMBER_INT)); |
596 |
} |
597 |
|
598 |
if (!empty($os_id)&&!empty($product_id)) { |
599 |
|
600 |
- $mirrors = db_get(" |
601 |
+ $mirrors = DB::get(" |
602 |
SELECT DISTINCT |
603 |
mirror_baseurl, location_path |
604 |
FROM |
605 |
@@ -39,13 +38,13 @@ if (!empty($os_id)&&!empty($product_id)) { |
606 |
ON |
607 |
mirror_location_mirror_map.location_id = mirror_locations.location_id |
608 |
WHERE |
609 |
- mirror_locations.os_id = {$os_id} AND |
610 |
- mirror_locations.product_id = {$product_id} AND |
611 |
+ mirror_locations.os_id = ? AND |
612 |
+ mirror_locations.product_id = ? AND |
613 |
mirror_location_mirror_map.location_active = '1' AND |
614 |
mirror_mirrors.mirror_active = '1' |
615 |
ORDER BY |
616 |
mirror_rating DESC, mirror_baseurl |
617 |
- "); |
618 |
+ ", PDO::FETCH_ASSOC, [$os_id, $product_id]); |
619 |
|
620 |
header("Content-type: text/plain;"); |
621 |
foreach ($mirrors as $mirror) { |
622 |
@@ -68,13 +67,13 @@ if (!empty($os_id)&&!empty($product_id)) { |
623 |
form_start('list','list','get','./index-list.php'); |
624 |
echo '<div>'; |
625 |
form_label('Product', 'product','label-small'); |
626 |
- form_select('product_id','product','',mirror_get_products_select(),$_GET['product_id']); |
627 |
+ form_select('product_id','product','',Mirror::get_products_select(),$_GET['product_id']); |
628 |
echo ' [<a href="./products.php">edit products</a>]'; |
629 |
echo '</div><br />'; |
630 |
|
631 |
echo '<div>'; |
632 |
form_label('OS', 'os','label-small'); |
633 |
- form_select('os_id','os','',mirror_get_oss_select(),$_GET['os_id']); |
634 |
+ form_select('os_id','os','',Mirror::get_oss_select(),$_GET['os_id']); |
635 |
echo ' [<a href="./os.php">edit operating systems</a>]'; |
636 |
echo '</div><br />'; |
637 |
form_submit('submit','','button1','Update'); |
638 |
|
639 |
diff --git a/php/index.php b/php/index.php |
640 |
index eacb99e..28b29b8 100644 |
641 |
--- a/php/index.php |
642 |
+++ b/php/index.php |
643 |
@@ -15,45 +15,44 @@ if (empty($_GET['os'])) { |
644 |
// do we even have an os or product? |
645 |
if (!empty($_GET['os'])&&!empty($_GET['product'])) { |
646 |
require_once(LIB.'/db.php'); // core mysql wrappers |
647 |
- db_connect(DBHOST,DBUSER,DBPASS); // open persistent connection to db |
648 |
- db_select(DBNAME); // select db |
649 |
+ DB::connect(DBHOST,DBUSER,DBPASS,DBNAME); // open persistent connection to db |
650 |
|
651 |
// clean in os and product strings |
652 |
- $os_name = mysql_real_escape_string(trim(strtolower($_GET['os']))); |
653 |
- $product_name = mysql_real_escape_string(trim(strtolower($_GET['product']))); |
654 |
+ $os_name = trim(strtolower($_GET['os'])); |
655 |
+ $product_name = trim(strtolower($_GET['product'])); |
656 |
|
657 |
// get os and product IDs |
658 |
- $os_id = db_name_to_id('mirror_os','os_id','os_name',$os_name); |
659 |
- $product_id = db_name_to_id('mirror_products','product_id','product_name',$product_name); |
660 |
+ $os_id = DB::name_to_id('mirror_os','os_id','os_name',$os_name); |
661 |
+ $product_id = DB::name_to_id('mirror_products','product_id','product_name',$product_name); |
662 |
|
663 |
// do we have a valid os and product? |
664 |
if (!empty($os_id)&&!empty($product_id)) { |
665 |
- $location = db_get_one("SELECT location_id,location_path FROM mirror_locations WHERE product_id={$product_id} AND os_id={$os_id}"); |
666 |
+ $location = DB::get_one("SELECT location_id,location_path FROM mirror_locations WHERE product_id={$product_id} AND os_id={$os_id}"); |
667 |
|
668 |
// did we get a valid location? |
669 |
if (!empty($location)) { |
670 |
- $mirror = db_get_one("SELECT mirror_mirrors.mirror_id,mirror_baseurl FROM mirror_mirrors, mirror_location_mirror_map WHERE mirror_mirrors.mirror_id = mirror_location_mirror_map.mirror_id AND mirror_location_mirror_map.location_id = {$location['location_id']} AND mirror_active='1' AND location_active ='1' ORDER BY rand()*(1/mirror_rating)"); |
671 |
+ $mirror = DB::get_one("SELECT mirror_mirrors.mirror_id,mirror_baseurl FROM mirror_mirrors, mirror_location_mirror_map WHERE mirror_mirrors.mirror_id = mirror_location_mirror_map.mirror_id AND mirror_location_mirror_map.location_id = {$location['location_id']} AND mirror_active='1' AND location_active ='1' ORDER BY rand()*(1/mirror_rating)"); |
672 |
|
673 |
// did we get a valid mirror? |
674 |
if (!empty($mirror)) { |
675 |
|
676 |
// if logging is enabled, insert log |
677 |
if (LOGGING) { |
678 |
- db_query("UPDATE mirror_mirrors SET mirror_count=mirror_count+1 WHERE mirror_id={$mirror['mirror_id']}"); |
679 |
- db_query("UPDATE mirror_products SET product_count=product_count+1 WHERE product_id={$product_id}"); |
680 |
+ DB::query("UPDATE mirror_mirrors SET mirror_count=mirror_count+1 WHERE mirror_id={$mirror['mirror_id']}"); |
681 |
+ DB::query("UPDATE mirror_products SET product_count=product_count+1 WHERE product_id={$product_id}"); |
682 |
} |
683 |
- |
684 |
+ |
685 |
// LANGUAGE HACK |
686 |
- if (!empty($_GET['lang'])) { |
687 |
+ if (!empty($_GET['lang'])) { |
688 |
//// $location['location_path'] = str_replace('x86',$_GET['lang'],$location['location_path']); |
689 |
- $location['location_path'] = str_replace('en-US',$_GET['lang'],$location['location_path']); |
690 |
- } |
691 |
- |
692 |
- // BitTorrent HACK - robbat2 |
693 |
- if (!empty($_GET['extra'])) { |
694 |
- $extra = $_GET['extra']; |
695 |
- $location['location_path'] .= ereg_replace('\?.*|&.*','',$extra); |
696 |
- } |
697 |
+ $location['location_path'] = str_replace('en-US',$_GET['lang'],$location['location_path']); |
698 |
+ } |
699 |
+ |
700 |
+ // BitTorrent HACK - robbat2 |
701 |
+ if (!empty($_GET['extra'])) { |
702 |
+ $extra = $_GET['extra']; |
703 |
+ $location['location_path'] .= ereg_replace('\?.*|&.*','',$extra); |
704 |
+ } |
705 |
|
706 |
// if we are just testing, then just print and exit. |
707 |
if (!empty($_GET['print'])) { |
708 |
@@ -69,7 +68,5 @@ if (!empty($_GET['os'])&&!empty($_GET['product'])) { |
709 |
} |
710 |
} |
711 |
|
712 |
-// if we get here, the request was invalid; redirect to mozilla home |
713 |
+// if we get here, the request was invalid; redirect to Gentoo home |
714 |
header('Location: http://www.gentoo.org/'); |
715 |
-exit; |
716 |
-?> |
717 |
|
718 |
diff --git a/php/lib/auth.php b/php/lib/auth.php |
719 |
index f03db32..eb6319c 100644 |
720 |
--- a/php/lib/auth.php |
721 |
+++ b/php/lib/auth.php |
722 |
@@ -1,4 +1,6 @@ |
723 |
<?php |
724 |
+ |
725 |
+class Auth { |
726 |
/** |
727 |
* Home-cooked auth libraries - because PEAR is fat. |
728 |
* @package mirror |
729 |
@@ -10,17 +12,18 @@ |
730 |
* Check admin session against sessions table in database. |
731 |
* @return bool |
732 |
*/ |
733 |
-function auth_is_valid_session() |
734 |
+public static function is_valid_session() |
735 |
{ |
736 |
- if (!empty($_COOKIE['mozilla-mirror-admin'])) { // check cookie |
737 |
- $res = db_query("SELECT * FROM mirror_sessions WHERE session_id = '{$_COOKIE['mozilla-mirror-admin']}'"); // check db for id |
738 |
- if ($res && db_numrows($res)>0) { |
739 |
- $buf = db_fetch($res,MYSQL_ASSOC); |
740 |
+ $cookieAdmin = filter_input(INPUT_COOKIE, 'mozilla-mirror-admin'); |
741 |
+ if (!empty($cookieAdmin)) { // check cookie |
742 |
+ $res = DB::query("SELECT * FROM mirror_sessions WHERE session_id = ?", [$cookieAdmin]); // check db for id |
743 |
+ if ($res && DB::numrows($res)>0) { |
744 |
+ $buf = DB::fetch($res,PDO::FETCH_ASSOC); |
745 |
// comment line below to disable gc and allow multiple sessions per username |
746 |
- db_query("DELETE FROM mirror_sessions WHERE username='{$buf['username']}' AND session_id != '{$_COOKIE['mozilla-mirror-admin']}'"); // garbage collection |
747 |
- $user = db_fetch(db_query("SELECT * FROM mirror_users WHERE username='{$buf['username']}'"),MYSQL_ASSOC); |
748 |
+ DB::query("DELETE FROM mirror_sessions WHERE username=? AND session_id != ?", [$buf['username'], $cookieAdmin]); // garbage collection |
749 |
+ $user = DB::fetch(DB::query("SELECT * FROM mirror_users WHERE username=?", [$buf['username']]),PDO::FETCH_ASSOC); |
750 |
if (empty($_SESSION)) { |
751 |
- auth_create_session($user); // if session isn't started, create it and push user data |
752 |
+ static::create_session($user); // if session isn't started, create it and push user data |
753 |
} |
754 |
return true; |
755 |
} |
756 |
@@ -34,42 +37,57 @@ function auth_is_valid_session() |
757 |
* @param string $password |
758 |
* @return array|bool array containing user data or false on failure |
759 |
*/ |
760 |
-function auth_mysql($username,$password) |
761 |
+public static function query($username,$password) |
762 |
{ |
763 |
if (empty($username)||empty($password)) { |
764 |
return false; |
765 |
- } |
766 |
- $username = trim(strip_tags(addslashes($username))); |
767 |
- $password = trim(strip_tags(addslashes($password))); |
768 |
- $res = db_query("SELECT * FROM mirror_users WHERE username='{$username}' AND password=MD5('{$password}')"); |
769 |
- if ($res && db_numrows($res)>0) { |
770 |
- return db_fetch($res,MYSQL_ASSOC); |
771 |
+ } |
772 |
+ $username = trim(strip_tags($username)); |
773 |
+ $password = trim(strip_tags($password)); |
774 |
+ $res = DB::query("SELECT * FROM mirror_users WHERE username=?", [$username]); |
775 |
+ if ($res && DB::numrows($res)>0) { |
776 |
+ $userrow = DB::fetch($res,PDO::FETCH_ASSOC); |
777 |
+ if (!password_verify($password, $userrow['password'])) { |
778 |
+ if ($userrow['password'] !== md5($password)) |
779 |
+ return false; |
780 |
+ static::password_upgrade($userrow, $username, $password); |
781 |
+ } |
782 |
+ if (password_needs_rehash($userrow['password'], PASSWORD_DEFAULT)) |
783 |
+ static::password_upgrade($userrow, $username, $password); |
784 |
+ return $userrow; |
785 |
} else { |
786 |
return false; |
787 |
} |
788 |
} |
789 |
|
790 |
+private static function password_upgrade($userrow, $username, $password) { |
791 |
+ require_once(LIB.'/mirror.php'); //Upgrade password security |
792 |
+ Mirror::update_user($userrow['user_id'],$username,$password,$password,$userrow['firstname'],$userrow['lastname'],$userrow['email']); |
793 |
+} |
794 |
+ |
795 |
/** |
796 |
* Start a valid session. |
797 |
* @param array $user array containing user information. |
798 |
*/ |
799 |
-function auth_create_session($user,$secure=0) |
800 |
+public static function create_session($user,$secure=0) |
801 |
{ |
802 |
session_name('mozilla-mirror-admin'); |
803 |
session_set_cookie_params(0,'/',$_SERVER['HTTP_HOST'],$secure); |
804 |
session_start(); |
805 |
- db_query("INSERT INTO mirror_sessions(session_id,username) VALUES('".session_id()."','{$user['username']}')"); |
806 |
+ DB::query("INSERT INTO mirror_sessions(session_id,username) VALUES(?,?)", [session_id(), $user['username']]); |
807 |
$_SESSION['user']=$user; |
808 |
} |
809 |
|
810 |
/** |
811 |
* Logout. |
812 |
*/ |
813 |
-function auth_logout() |
814 |
+public static function logout() |
815 |
{ |
816 |
// comment line below to keep gc from deleting other sessions for this user |
817 |
- db_query("DELETE FROM mirror_sessions WHERE session_id='{$_COOKIE['mozilla-mirror-admin']}' OR username='{$_SESSION['user']['username']}'"); |
818 |
- $_COOKIE = array(); |
819 |
+ $cookieAdmin = filter_input(INPUT_COOKIE, 'mozilla-mirror-admin'); |
820 |
+ DB::query("DELETE FROM mirror_sessions WHERE session_id=? OR username=?", [$cookieAdmin, $_SESSION['user']['username']]); |
821 |
+ $_COOKIE = array(); |
822 |
$_SESSION = array(); |
823 |
} |
824 |
-?> |
825 |
+ |
826 |
+} |
827 |
|
828 |
diff --git a/php/lib/csv.php b/php/lib/csv.php |
829 |
index e98d37c..12dff67 100644 |
830 |
--- a/php/lib/csv.php |
831 |
+++ b/php/lib/csv.php |
832 |
@@ -5,7 +5,7 @@ |
833 |
* <code> |
834 |
* if ($_GET['csv']) |
835 |
* { |
836 |
- * $res=db_query("SELECT * FROM fic_courses"); |
837 |
+ * $res=DB::query("SELECT * FROM fic_courses"); |
838 |
* csv_send_csv($res); |
839 |
* exit; |
840 |
* } |
841 |
@@ -24,7 +24,7 @@ |
842 |
function csv_export_to_csv($res) |
843 |
{ |
844 |
// parse all results, format for CSV |
845 |
- while ( $buf=db_fetch($res,MYSQL_ASSOC) ) |
846 |
+ while ( $buf=DB::fetch($res,PDO::FETCH_ASSOC) ) |
847 |
{ |
848 |
// get the keys (headers), if not already done |
849 |
if ( empty($heads) ) |
850 |
|
851 |
diff --git a/php/lib/db.php b/php/lib/db.php |
852 |
index 3f9435f..6882064 100644 |
853 |
--- a/php/lib/db.php |
854 |
+++ b/php/lib/db.php |
855 |
@@ -5,60 +5,51 @@ |
856 |
* @subpackage lib |
857 |
*/ |
858 |
|
859 |
+class DB { |
860 |
+ |
861 |
+ private $dsn; |
862 |
+ private $result; |
863 |
/** |
864 |
* Connect to a MySQL database server. |
865 |
* @param string $host db server, defaults to localhost |
866 |
* @param string $user db username |
867 |
- * @param string $password db password |
868 |
- * @return resource dbh |
869 |
+ * @param string $password db password |
870 |
+ * @return PDO dbh |
871 |
*/ |
872 |
-function db_connect($host='localhost',$user=null,$password=null) |
873 |
+public static function connect($host='localhost',$user=null,$password=null,$database=null) |
874 |
{ |
875 |
- static $dbh = null; |
876 |
if (!empty($host) && isset($user) && isset($password)) { |
877 |
- $dbh = @mysql_connect($host,$user,$password); |
878 |
- } |
879 |
- if (is_resource($dbh)) { |
880 |
- return $dbh; |
881 |
+ $dsn = "mysql:host={$host}"; |
882 |
+ if(!empty($database)) $dsn .= ";database={$database}"; |
883 |
+ $options = [PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; |
884 |
+ static::$dbh = new PDO($dsn, $user, $password, $options); |
885 |
} |
886 |
- else die("Unable to create database connection in db_connect()"); |
887 |
-} |
888 |
- |
889 |
-/** |
890 |
- * Select database. |
891 |
- * @param string $database name of the database to select |
892 |
- * @param resource $dbh valid dbh, null if not defined |
893 |
- * @return bool success of command |
894 |
- */ |
895 |
-function db_select($database,$dbh=null) |
896 |
-{ |
897 |
- if(is_resource($dbh)){ |
898 |
- return @mysql_select_db($database); |
899 |
- }else{ |
900 |
- return @mysql_select_db($database, db_connect()); |
901 |
+ if (static::$dbh instanceof PDO) { |
902 |
+ return static::$dbh; |
903 |
} |
904 |
- |
905 |
+ die("Unable to create database connection in DB::connect()"); |
906 |
} |
907 |
|
908 |
/** |
909 |
* Execute a MySQL query. |
910 |
* @param string $qry MySQL query |
911 |
- * @param resource $dbh valid dbh |
912 |
*/ |
913 |
-function db_query($qry=null,$dbh=null) |
914 |
+public static function query($qry=null, $parameters=[]) |
915 |
{ |
916 |
- static $result = null; |
917 |
- if(!is_resource($dbh)) $dbh = db_connect(); |
918 |
+ if(!(static::$dbh instanceof PDO)) static::$dbh = static::connect(); |
919 |
if(is_null($qry)) |
920 |
{ |
921 |
- if(is_resource($result)) return $result; |
922 |
+ if(static::$result instanceof PDOStatement) return static::$result; |
923 |
else return false; |
924 |
} |
925 |
- else |
926 |
- { |
927 |
- $result = @mysql_query($qry,$dbh); |
928 |
- return $result; |
929 |
+ if(!empty($parameters)) { |
930 |
+ static::$result = static::$dbh->prepare($query); |
931 |
+ static::$result->execute($parameters); |
932 |
} |
933 |
+ else { |
934 |
+ static::$result = static::$dbh->query($qry); |
935 |
+ } |
936 |
+ return static::$result; |
937 |
} |
938 |
|
939 |
/** |
940 |
@@ -66,198 +57,90 @@ function db_query($qry=null,$dbh=null) |
941 |
* @param string $result (default to null) |
942 |
* @return array |
943 |
*/ |
944 |
-function db_fetch($result=null,$type=MYSQL_BOTH) |
945 |
+public static function fetch($result=null,$type=PDO::FETCH_BOTH) |
946 |
{ |
947 |
- return (!is_resource($result))? @mysql_fetch_array(db_query()) : @mysql_fetch_array($result,$type); |
948 |
+ if (is_null($result) && static::$result instanceof PDOStatement) |
949 |
+ $result = static::$result; |
950 |
+ if (!$result instanceof PDOStatement) |
951 |
+ throw new InvalidArgumentException("Fetch called before query issued"); |
952 |
+ return $result->fetch($type); |
953 |
} |
954 |
|
955 |
/** |
956 |
- * Fetch an array based on a query. |
957 |
+ * Fetch an array based on a query. |
958 |
* @param string $query database query |
959 |
* @param int $type result type |
960 |
* @param string $col_id if passed it, the values of this column in the result set will be used as the array keys in the returned array |
961 |
* @return array $list array of database rows |
962 |
* Example of returned array: |
963 |
* <code> |
964 |
- * db_get("SELECT * FROM table",MYSQL_ASSOC); |
965 |
+ * DB::get("SELECT * FROM table",PDO::FETCH_ASSOC); |
966 |
* returns... |
967 |
* Array |
968 |
* ( |
969 |
* [0] => Array |
970 |
* ( |
971 |
* [id] => 1 |
972 |
- * [field1] => data1 |
973 |
+ * [field1] => data1 |
974 |
* [field2] => data2 |
975 |
* ) |
976 |
* |
977 |
* ) |
978 |
* </code> |
979 |
*/ |
980 |
-function db_get($query,$type=MYSQL_BOTH,$col_id=NULL) |
981 |
+public static function get($query,$type=PDO::FETCH_BOTH,$col_id=NULL,$parameters=[]) |
982 |
{ |
983 |
- $res = db_query($query); |
984 |
- $list = array(); |
985 |
- if (is_resource($res) && !is_null($col_id) && ($type == MYSQL_BOTH || $type == MYSQL_ASSOC) && @mysql_num_rows($res) !== 0) { |
986 |
- $col_test = db_fetch($res,$type); |
987 |
- @mysql_data_seek($res, 0); |
988 |
+ $res = static::query($query, $parameters); |
989 |
+ $list = []; |
990 |
+ if ($res instanceof PDOStatement && !is_null($col_id) && ($type === PDO::FETCH_BOTH || $type == PDO::FETCH_ASSOC) && $res->rowCount() !== 0) { |
991 |
+ $col_test = static::fetch($res,$type); |
992 |
if (array_key_exists($col_id,$col_test)) { |
993 |
- while ( $buf = db_fetch($res,$type) ) { |
994 |
+ $list[$col_test[$col_id]] = $col_test; |
995 |
+ while ( $buf = static::fetch($res,$type) ) { |
996 |
$list[$buf[$col_id]] = $buf; |
997 |
} |
998 |
return $list; |
999 |
} |
1000 |
} |
1001 |
- while ( $buf = db_fetch($res,$type) ) { |
1002 |
- $list[] = $buf; |
1003 |
+ if ($res instanceof PDOStatement) { |
1004 |
+ $list = $res->fetchAll($type); |
1005 |
} |
1006 |
return $list; |
1007 |
} |
1008 |
|
1009 |
-/** |
1010 |
- * Get all of the fieldnames for the specified table. |
1011 |
- * @param string $table name of table to describe |
1012 |
- * @return array array of column names, must be an array |
1013 |
- */ |
1014 |
-function db_fieldnames($table) |
1015 |
-{ |
1016 |
- $dbh = db_connect(); |
1017 |
- $results = db_query("DESCRIBE $table"); |
1018 |
- if (is_resource($results)) |
1019 |
- { |
1020 |
- while ($buf=db_fetch($results)) |
1021 |
- { |
1022 |
- $field_names[] = $buf[0]; |
1023 |
- } |
1024 |
- } |
1025 |
- else |
1026 |
- { |
1027 |
- $field_names[] = 0; |
1028 |
- } |
1029 |
- return $field_names; |
1030 |
-} |
1031 |
- |
1032 |
-/** |
1033 |
- * Create a MySQL INSERT statement based on $_POST array generated by form submission. |
1034 |
- * <ul> |
1035 |
- * <li>does not work with mysql functions (PASSWORD, etc.) because there are forced double quotes</li> |
1036 |
- * <li>do not use clean_in() before this, or you'll have double the slashes</li> |
1037 |
- * <li>use the function only when it saves you time, not _always_</li> |
1038 |
- * <li>form items not set will not be processed (unchecked radios, checkboxes) - handle these manually, or don't use the func</li> |
1039 |
- * </ul> |
1040 |
- * @param array $vars array of posts |
1041 |
- * @param string $table name of the table that fields will be inserted into |
1042 |
- * @return string $query resulting MySQL insert string |
1043 |
- */ |
1044 |
-function db_makeinsert($vars,$table) |
1045 |
-{ |
1046 |
- $dbh = db_connect(); |
1047 |
- $fields = db_fieldnames($table); |
1048 |
- foreach ($fields as $field) |
1049 |
- { |
1050 |
- if (get_magic_quotes_gpc) $vars[$field] = stripslashes($vars[$field]); |
1051 |
- $vars[$field] = addslashes($vars[$field]); |
1052 |
- if (isset($vars[$field])) |
1053 |
- { |
1054 |
- isset($q1)?$q1 .= ','.$field:$q1='INSERT INTO '.$table.'('.$field; |
1055 |
- isset($q2)?$q2 .= ",'$vars[$field]'":$q2=" VALUES('$vars[$field]'"; |
1056 |
- } |
1057 |
- } |
1058 |
- $q1 .= ')'; |
1059 |
- $q2 .= ')'; |
1060 |
- $query = $q1.$q2; |
1061 |
- return $query; |
1062 |
-} |
1063 |
- |
1064 |
-/** |
1065 |
- * Create a MySQL REPLACE statement based on $_POST array generated by form submission. |
1066 |
- * <ul> |
1067 |
- * <li>does not work with mysql functions (PASSWORD, etc.) because there are forced double quotes</li> |
1068 |
- * <li>do not use clean_in() before this, or you'll have double the slashes</li> |
1069 |
- * <li>use the function only when it saves you time, not _always_</li> |
1070 |
- * <li>form items not set will not be processed (unchecked radios, checkboxes) - handle these manually, or don't use the func</li> |
1071 |
- * </ul> |
1072 |
- * @param array $vars array of posts |
1073 |
- * @param string $table name of the table that fields will be inserted into |
1074 |
- * @return string $query resulting MySQL insert string |
1075 |
- */ |
1076 |
-function db_makereplace($vars,$table) |
1077 |
-{ |
1078 |
- $dbh = db_connect(); |
1079 |
- $fields = db_fieldnames($table); |
1080 |
- foreach ($fields as $field) |
1081 |
- { |
1082 |
- if (get_magic_quotes_gpc) $vars[$field] = stripslashes($vars[$field]); |
1083 |
- $vars[$field] = addslashes($vars[$field]); |
1084 |
- if (isset($vars[$field])) |
1085 |
- { |
1086 |
- isset($q1)?$q1 .= ','.$field:$q1='REPLACE INTO '.$table.'('.$field; |
1087 |
- isset($q2)?$q2 .= ",'$vars[$field]'":$q2=" VALUES('$vars[$field]'"; |
1088 |
- } |
1089 |
- } |
1090 |
- $q1 .= ')'; |
1091 |
- $q2 .= ')'; |
1092 |
- $query = $q1.$q2; |
1093 |
- return $query; |
1094 |
-} |
1095 |
- |
1096 |
-/** |
1097 |
- * Create a MySQL UPDATE statement based on $_POST array generated by form submission. |
1098 |
- * <ul> |
1099 |
- * <li>does not work with mysql functions (PASSWORD, etc.) because there are forced double quotes</li> |
1100 |
- * <li>do not use clean_in() before this, or you'll have double the slashes</li> |
1101 |
- * <li>use the function only when it saves you time, not _always_</li> |
1102 |
- * <li>form items not set will not be processed (unchecked radios, checkboxes) - handle these manually, or don't use the func</li> |
1103 |
- * </ul> |
1104 |
- * @param array $vars array of posts |
1105 |
- * @param string $table name of the table that fields will be inserted into |
1106 |
- * @param string $where where clause, describing which records are to be updated |
1107 |
- */ |
1108 |
-function db_makeupdate($vars,$table,$where) |
1109 |
-{ |
1110 |
- $dbh = db_connect(); |
1111 |
- $fields = db_fieldnames($table); |
1112 |
- foreach ($fields as $field) |
1113 |
- { |
1114 |
- if (isset($vars[$field])) |
1115 |
- { |
1116 |
- if (get_magic_quotes_gpc()) $vars[$field] = stripslashes($vars[$field]); |
1117 |
- $vars[$field]=addslashes($vars[$field]); |
1118 |
- $q1 = isset($q1)?$q1 .= ' ,'.$field."='$vars[$field]'":'UPDATE '.$table.' set '.$field."='$vars[$field]'"; |
1119 |
- } |
1120 |
- } |
1121 |
- $query = $q1.' '.$where; |
1122 |
- return $query; |
1123 |
-} |
1124 |
- |
1125 |
/** |
1126 |
* Since PHP's mysql_insert_id() sometimes throws an error, this is the replacement |
1127 |
- * @param resource $dbh optional dbh to get the last inserted id from |
1128 |
+ * @param PDO $dbh optional dbh to get the last inserted id from |
1129 |
* @return int the return value of MySQL's last_insert_id() |
1130 |
*/ |
1131 |
-function db_insert_id($dbh=null) |
1132 |
+public static function insert_id($dbh=null) |
1133 |
{ |
1134 |
- if(!is_resource($dbh)) $dbh = db_connect(); |
1135 |
- $buf = db_fetch(db_query("SELECT LAST_INSERT_ID()", $dbh)); |
1136 |
- return empty($buf[0]) ? false : $buf[0]; |
1137 |
+ if(!($dbh instanceof PDO)) $dbh = static::connect(); |
1138 |
+ $buf = $dbh->lastInsertId(); |
1139 |
+ return empty($buf) ? false : $buf; |
1140 |
} |
1141 |
|
1142 |
/** |
1143 |
* Determine number of rows in result. |
1144 |
- * @param resource $result mysql result |
1145 |
+ * @param PDOStatement $result mysql result |
1146 |
* @return int number of rows in query result |
1147 |
*/ |
1148 |
-function db_numrows($result=null) |
1149 |
+function numrows($result=null) |
1150 |
{ |
1151 |
- return (!is_resource($result))? @mysql_num_rows(db_query()) : @mysql_num_rows($result); |
1152 |
+ if (is_null($result) && static::$result instanceof PDOStatement) |
1153 |
+ $result = static::$result; |
1154 |
+ if (!$result instanceof PDOStatement) |
1155 |
+ throw new InvalidArgumentException("numrows called before query issued"); |
1156 |
+ return $result->rowCount(); |
1157 |
} |
1158 |
|
1159 |
/** |
1160 |
* Close the db connection. If a dbh is not specified, assume the last opened link. |
1161 |
* @param resource $dbh optional dbh to close |
1162 |
*/ |
1163 |
-function db_close($dbh=null) |
1164 |
+public static function close($dbh=null) |
1165 |
{ |
1166 |
- return is_resource($dbh)?@mysql_close($dbh):@mysql_close(); |
1167 |
+ return ($dbh instanceof PDO)?$dbh=null:static::$dbh=null; |
1168 |
} |
1169 |
|
1170 |
/** |
1171 |
@@ -265,8 +148,8 @@ function db_close($dbh=null) |
1172 |
* @param string $query query |
1173 |
* @param int $type result type |
1174 |
*/ |
1175 |
-function db_get_one($query,$type=MYSQL_ASSOC) { |
1176 |
- $buf = db_get($query.' LIMIT 1',$type); |
1177 |
+public static function get_one($query,$type=PDO::FETCH_ASSOC,$parameters=[]) { |
1178 |
+ $buf = static::get($query.' LIMIT 1',$type,$parameters); |
1179 |
return $buf[0]; |
1180 |
} |
1181 |
|
1182 |
@@ -277,22 +160,10 @@ function db_get_one($query,$type=MYSQL_ASSOC) { |
1183 |
* @param string $name_col |
1184 |
* @param string $name |
1185 |
*/ |
1186 |
-function db_name_to_id($table,$id_col,$name_col,$name) |
1187 |
+public static function name_to_id($table,$id_col,$name_col,$name) |
1188 |
{ |
1189 |
- $buf = db_get_one("SELECT {$id_col} FROM {$table} WHERE {$name_col} = '{$name}'", MYSQL_NUM); |
1190 |
+ $buf = static::get_one("SELECT {$id_col} FROM {$table} WHERE {$name_col} = :name", PDO::FETCH_NUM, [':name' => $name]); |
1191 |
return $buf[0]; |
1192 |
} |
1193 |
|
1194 |
-/** |
1195 |
- * Sets enum booleans to their opposite |
1196 |
- * @param string $table |
1197 |
- * @param string $pri |
1198 |
- * @param string $col |
1199 |
- * @param array $id |
1200 |
- * @return int |
1201 |
- */ |
1202 |
-function db_toggle_bool($table, $pri, $col, $id) |
1203 |
-{ |
1204 |
- return db_query("UPDATE {$table} SET {$col} = IF({$col} = '1', '0', '1') WHERE {$pri} = {$id}"); |
1205 |
} |
1206 |
-?> |
1207 |
|
1208 |
diff --git a/php/lib/mirror.php b/php/lib/mirror.php |
1209 |
index a618a86..ac3e904 100644 |
1210 |
--- a/php/lib/mirror.php |
1211 |
+++ b/php/lib/mirror.php |
1212 |
@@ -6,13 +6,15 @@ |
1213 |
* @todo add transactions once innodb table types are in place |
1214 |
*/ |
1215 |
|
1216 |
+class Mirror { |
1217 |
+ |
1218 |
/** |
1219 |
* Get an alpha-list of regions for select list. |
1220 |
* @return array $regions |
1221 |
*/ |
1222 |
-function mirror_get_regions_select() |
1223 |
+public static function get_regions_select() |
1224 |
{ |
1225 |
- $regions = db_get("SELECT region_id,region_name FROM mirror_regions ORDER BY region_name ASC",MYSQL_ASSOC); |
1226 |
+ $regions = DB::get("SELECT region_id,region_name FROM mirror_regions ORDER BY region_name ASC",PDO::FETCH_ASSOC); |
1227 |
foreach ($regions as $region) { |
1228 |
$retval[$region['region_id']]=$region['region_name']; |
1229 |
} |
1230 |
@@ -23,9 +25,9 @@ function mirror_get_regions_select() |
1231 |
* Get an priority-list of regions for select list. |
1232 |
* @return array $regions |
1233 |
*/ |
1234 |
-function mirror_get_regions_select_priority() |
1235 |
+public static function get_regions_select_priority() |
1236 |
{ |
1237 |
- $regions = db_get("SELECT region_id,region_name FROM mirror_regions ORDER BY region_priority ASC",MYSQL_ASSOC); |
1238 |
+ $regions = DB::get("SELECT region_id,region_name FROM mirror_regions ORDER BY region_priority ASC",PDO::FETCH_ASSOC); |
1239 |
foreach ($regions as $region) { |
1240 |
$retval[$region['region_id']]=$region['region_name']; |
1241 |
} |
1242 |
@@ -37,10 +39,10 @@ function mirror_get_regions_select_priority() |
1243 |
* @param string $name |
1244 |
* @param int $priority |
1245 |
* @return bool |
1246 |
- */ |
1247 |
-function mirror_insert_region($name,$priority) |
1248 |
+ */ |
1249 |
+public static function insert_region($name,$priority) |
1250 |
{ |
1251 |
- return db_query("INSERT INTO mirror_regions(region_name,region_priority) VALUES('{$name}',{$priority})"); |
1252 |
+ return DB::query("INSERT INTO mirror_regions(region_name,region_priority) VALUES(?,?)", [$name, $priority]); |
1253 |
} |
1254 |
|
1255 |
/** |
1256 |
@@ -49,10 +51,10 @@ function mirror_insert_region($name,$priority) |
1257 |
* @param string $name |
1258 |
* @param int $priority |
1259 |
* @return bool |
1260 |
- */ |
1261 |
-function mirror_update_region($id,$name,$priority) |
1262 |
+ */ |
1263 |
+public static function update_region($id,$name,$priority) |
1264 |
{ |
1265 |
- return db_query("UPDATE mirror_regions SET region_name='{$name}',region_priority={$priority} WHERE region_id={$id}"); |
1266 |
+ return DB::query("UPDATE mirror_regions SET region_name=?,region_priority=? WHERE region_id=?", [$name, $priority, $id]); |
1267 |
} |
1268 |
|
1269 |
/** |
1270 |
@@ -60,9 +62,9 @@ function mirror_update_region($id,$name,$priority) |
1271 |
* @param int $id |
1272 |
* @return array |
1273 |
*/ |
1274 |
-function mirror_get_one_region($id) |
1275 |
+public static function get_one_region($id) |
1276 |
{ |
1277 |
- return db_get_one("SELECT * FROM mirror_regions WHERE region_id = {$id}"); |
1278 |
+ return DB::get_one("SELECT * FROM mirror_regions WHERE region_id = ?", PDO::FETCH_ASSOC, [$id]); |
1279 |
} |
1280 |
|
1281 |
/** |
1282 |
@@ -70,18 +72,18 @@ function mirror_get_one_region($id) |
1283 |
* @param int $id |
1284 |
* @return bool |
1285 |
*/ |
1286 |
-function mirror_delete_region($id) |
1287 |
+public static function delete_region($id) |
1288 |
{ |
1289 |
- return db_query("DELETE FROM mirror_regions WHERE region_id={$id}"); |
1290 |
+ return DB::query("DELETE FROM mirror_regions WHERE region_id=?", [$id]); |
1291 |
} |
1292 |
|
1293 |
/** |
1294 |
* Get an alpha-list of mirrors for select list. |
1295 |
* @return array $mirrors |
1296 |
*/ |
1297 |
-function mirror_get_mirrors_select() |
1298 |
+public static function get_mirrors_select() |
1299 |
{ |
1300 |
- $mirrors = db_get("SELECT mirror_id,mirror_name FROM mirror_mirrors ORDER BY mirror_name ASC",MYSQL_ASSOC); |
1301 |
+ $mirrors = DB::get("SELECT mirror_id,mirror_name FROM mirror_mirrors ORDER BY mirror_name ASC",PDO::FETCH_ASSOC); |
1302 |
foreach ($mirrors as $mirror) { |
1303 |
$retval[$mirror['mirror_id']]=$mirror['mirror_name']; |
1304 |
} |
1305 |
@@ -92,21 +94,21 @@ function mirror_get_mirrors_select() |
1306 |
* Get regions. |
1307 |
* @return array |
1308 |
*/ |
1309 |
-function mirror_get_regions() |
1310 |
+public static function get_regions() |
1311 |
{ |
1312 |
- return db_get(" |
1313 |
- SELECT |
1314 |
+ return DB::get(" |
1315 |
+ SELECT |
1316 |
mirror_regions.*, |
1317 |
COUNT(mirror_id) as mirrors |
1318 |
- FROM |
1319 |
+ FROM |
1320 |
mirror_regions |
1321 |
LEFT JOIN |
1322 |
mirror_mirror_region_map |
1323 |
ON |
1324 |
mirror_regions.region_id = mirror_mirror_region_map.region_id |
1325 |
GROUP BY |
1326 |
- mirror_regions.region_id |
1327 |
- ",MYSQL_ASSOC); |
1328 |
+ mirror_regions.region_id |
1329 |
+ ",PDO::FETCH_ASSOC); |
1330 |
} |
1331 |
|
1332 |
/** |
1333 |
@@ -117,9 +119,10 @@ function mirror_get_regions() |
1334 |
* @param int $rating |
1335 |
* @return bool |
1336 |
*/ |
1337 |
-function mirror_insert_mirror($name,$region_id,$baseurl,$rating) |
1338 |
+public static function insert_mirror($name,$region_id,$baseurl,$rating) |
1339 |
{ |
1340 |
- 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; |
1341 |
+ return (DB::query("INSERT INTO mirror_mirrors(mirror_name,mirror_baseurl,mirror_rating) VALUES(?,?,?)", [$name, $baseurl, $rating]) && |
1342 |
+ DB::query("INSERT INTO mirror_mirror_region_map(mirror_id,region_id) VALUES(?,?)", [DB::insert_id(), $region_id]))?true:false; |
1343 |
} |
1344 |
|
1345 |
/** |
1346 |
@@ -130,18 +133,20 @@ function mirror_insert_mirror($name,$region_id,$baseurl,$rating) |
1347 |
* @param int $rating |
1348 |
* @return bool |
1349 |
*/ |
1350 |
-function mirror_update_mirror($id,$name,$region_id,$baseurl,$rating) |
1351 |
+public static function update_mirror($id,$name,$region_id,$baseurl,$rating) |
1352 |
{ |
1353 |
- return (db_query("UPDATE mirror_mirrors SET mirror_name='{$name}',mirror_baseurl='{$baseurl}',mirror_rating='{$rating}' WHERE mirror_id={$id}") && db_query("UPDATE mirror_mirror_region_map SET region_id={$region_id} WHERE mirror_id={$id}"))?true:false; |
1354 |
+ return (DB::query("UPDATE mirror_mirrors SET mirror_name=?,mirror_baseurl=?,mirror_rating=? WHERE mirror_id=?", [$name, $baseurl, $rating, $id]) |
1355 |
+ && DB::query("UPDATE mirror_mirror_region_map SET region_id=? WHERE mirror_id=?", [$region_id, $id]))?true:false; |
1356 |
} |
1357 |
|
1358 |
/** |
1359 |
* Delete mirror. |
1360 |
* @return bool |
1361 |
*/ |
1362 |
-function mirror_delete_mirror($mirror_id) |
1363 |
+public static function delete_mirror($mirror_id) |
1364 |
{ |
1365 |
- 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; |
1366 |
+ return (DB::query("DELETE FROM mirror_mirrors WHERE mirror_id=?", [$mirror_id]) |
1367 |
+ && DB::query("DELETE FROM mirror_mirror_region_map WHERE mirror_id=?", [$mirror_id]))?true:false; |
1368 |
} |
1369 |
|
1370 |
/** |
1371 |
@@ -149,18 +154,18 @@ function mirror_delete_mirror($mirror_id) |
1372 |
* @param int $mirror_id |
1373 |
* @return array mirror information |
1374 |
*/ |
1375 |
-function mirror_get_one_mirror($mirror_id) |
1376 |
+public static function get_one_mirror($mirror_id) |
1377 |
{ |
1378 |
- return db_get_one("SELECT mirror_mirrors.*,region_id FROM mirror_mirrors,mirror_mirror_region_map WHERE mirror_mirrors.mirror_id={$mirror_id} AND mirror_mirrors.mirror_id=mirror_mirror_region_map.mirror_id"); |
1379 |
+ 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]); |
1380 |
} |
1381 |
|
1382 |
/** |
1383 |
* Get list of mirrors. |
1384 |
* @return array |
1385 |
*/ |
1386 |
-function mirror_get_mirrors() |
1387 |
+public static function get_mirrors() |
1388 |
{ |
1389 |
- return db_get(" |
1390 |
+ return DB::get(" |
1391 |
SELECT |
1392 |
mirror_mirrors.*, |
1393 |
IF(mirror_mirrors.mirror_active='0','DISABLED','ok') as mirror_active, |
1394 |
@@ -172,7 +177,7 @@ function mirror_get_mirrors() |
1395 |
WHERE |
1396 |
mirror_regions.region_id = mirror_mirror_region_map.region_id AND |
1397 |
mirror_mirrors.mirror_id = mirror_mirror_region_map.mirror_id |
1398 |
- ",MYSQL_ASSOC); |
1399 |
+ ",PDO::FETCH_ASSOC); |
1400 |
} |
1401 |
|
1402 |
/** |
1403 |
@@ -181,9 +186,9 @@ function mirror_get_mirrors() |
1404 |
* @param int $priority |
1405 |
* @return bool |
1406 |
*/ |
1407 |
-function mirror_insert_product($name,$priority) |
1408 |
+public static function insert_product($name,$priority) |
1409 |
{ |
1410 |
- return db_query("INSERT INTO mirror_products(product_name,product_priority) VALUES('{$name}',{$priority})"); |
1411 |
+ return DB::query("INSERT INTO mirror_products(product_name,product_priority) VALUES(?,?)", [$name, $priority]); |
1412 |
} |
1413 |
|
1414 |
/** |
1415 |
@@ -193,9 +198,9 @@ function mirror_insert_product($name,$priority) |
1416 |
* @param int $priority |
1417 |
* @return bool |
1418 |
*/ |
1419 |
-function mirror_update_product($id,$name,$priority) |
1420 |
+public static function update_product($id,$name,$priority) |
1421 |
{ |
1422 |
- return db_query("UPDATE mirror_products SET product_name='{$name}',product_priority={$priority} WHERE product_id={$id}"); |
1423 |
+ return DB::query("UPDATE mirror_products SET product_name=?,product_priority=? WHERE product_id=?", [$name, $priority, $id]); |
1424 |
} |
1425 |
|
1426 |
/** |
1427 |
@@ -203,9 +208,9 @@ function mirror_update_product($id,$name,$priority) |
1428 |
* @param int $id |
1429 |
* @return array |
1430 |
*/ |
1431 |
-function mirror_get_one_product($id) |
1432 |
+public static function get_one_product($id) |
1433 |
{ |
1434 |
- return db_get_one("SELECT * FROM mirror_products WHERE product_id = {$id}"); |
1435 |
+ return DB::get_one("SELECT * FROM mirror_products WHERE product_id = ?", PDO::FETCH_ASSOC ,[$id]); |
1436 |
} |
1437 |
|
1438 |
/** |
1439 |
@@ -213,23 +218,18 @@ function mirror_get_one_product($id) |
1440 |
* @param int $id |
1441 |
* @return bool |
1442 |
*/ |
1443 |
-function mirror_delete_product($id) |
1444 |
+public static function delete_product($id) |
1445 |
{ |
1446 |
- return db_query("DELETE FROM mirror_products WHERE product_id={$id}"); |
1447 |
+ return DB::query("DELETE FROM mirror_products WHERE product_id=?", [$id]); |
1448 |
} |
1449 |
|
1450 |
/** |
1451 |
* Get products. |
1452 |
* @return array |
1453 |
*/ |
1454 |
-function mirror_get_products() |
1455 |
+public static function get_products() |
1456 |
{ |
1457 |
- return db_get(" |
1458 |
- SELECT |
1459 |
- * |
1460 |
- FROM |
1461 |
- mirror_products |
1462 |
- ",MYSQL_ASSOC); |
1463 |
+ return DB::get("SELECT * FROM mirror_products",PDO::FETCH_ASSOC); |
1464 |
} |
1465 |
|
1466 |
/** |
1467 |
@@ -237,10 +237,10 @@ function mirror_get_products() |
1468 |
* @param string $name |
1469 |
* @param int $priority |
1470 |
* @return bool |
1471 |
- */ |
1472 |
-function mirror_insert_os($name,$priority) |
1473 |
+ */ |
1474 |
+public static function insert_os($name,$priority) |
1475 |
{ |
1476 |
- return db_query("INSERT INTO mirror_os(os_name,os_priority) VALUES('{$name}',{$priority})"); |
1477 |
+ return DB::query("INSERT INTO mirror_os(os_name,os_priority) VALUES(?,?)", [$name, $priority]); |
1478 |
} |
1479 |
|
1480 |
/** |
1481 |
@@ -249,10 +249,10 @@ function mirror_insert_os($name,$priority) |
1482 |
* @param string $name |
1483 |
* @param int $priority |
1484 |
* @return bool |
1485 |
- */ |
1486 |
-function mirror_update_os($id,$name,$priority) |
1487 |
+ */ |
1488 |
+public static function update_os($id,$name,$priority) |
1489 |
{ |
1490 |
- return db_query("UPDATE mirror_os SET os_name='{$name}',os_priority={$priority} WHERE os_id={$id}"); |
1491 |
+ return DB::query("UPDATE mirror_os SET os_name=?,os_priority=? WHERE os_id=?", [$name, $priority, $id]); |
1492 |
} |
1493 |
|
1494 |
/** |
1495 |
@@ -260,9 +260,9 @@ function mirror_update_os($id,$name,$priority) |
1496 |
* @param int $id |
1497 |
* @return array |
1498 |
*/ |
1499 |
-function mirror_get_one_os($id) |
1500 |
+public static function get_one_os($id) |
1501 |
{ |
1502 |
- return db_get_one("SELECT * FROM mirror_os WHERE os_id = {$id}"); |
1503 |
+ return DB::get_one("SELECT * FROM mirror_os WHERE os_id = ?", PDO::FETCH_ASSOC, [$id]); |
1504 |
} |
1505 |
|
1506 |
/** |
1507 |
@@ -270,32 +270,27 @@ function mirror_get_one_os($id) |
1508 |
* @param int $id |
1509 |
* @return bool |
1510 |
*/ |
1511 |
-function mirror_delete_os($id) |
1512 |
+public static function delete_os($id) |
1513 |
{ |
1514 |
- return db_query("DELETE FROM mirror_os WHERE os_id={$id}"); |
1515 |
+ return DB::query("DELETE FROM mirror_os WHERE os_id=?", [$id]); |
1516 |
} |
1517 |
|
1518 |
/** |
1519 |
* Get operating systems. |
1520 |
* @return array |
1521 |
*/ |
1522 |
-function mirror_get_oss() |
1523 |
+public static function get_oss() |
1524 |
{ |
1525 |
- return db_get(" |
1526 |
- SELECT |
1527 |
- * |
1528 |
- FROM |
1529 |
- mirror_os |
1530 |
- ",MYSQL_ASSOC); |
1531 |
+ return DB::get("SELECT * FROM mirror_os",PDO::FETCH_ASSOC); |
1532 |
} |
1533 |
|
1534 |
/** |
1535 |
* Get an alpha-list of operating systems for select list. |
1536 |
* @return array $oss |
1537 |
*/ |
1538 |
-function mirror_get_oss_select() |
1539 |
+public static function get_oss_select() |
1540 |
{ |
1541 |
- $oss = db_get("SELECT os_id,os_name FROM mirror_os ORDER BY os_name ASC",MYSQL_ASSOC); |
1542 |
+ $oss = DB::get("SELECT os_id,os_name FROM mirror_os ORDER BY os_name ASC",PDO::FETCH_ASSOC); |
1543 |
foreach ($oss as $os) { |
1544 |
$retval[$os['os_id']]=$os['os_name']; |
1545 |
} |
1546 |
@@ -306,9 +301,9 @@ function mirror_get_oss_select() |
1547 |
* Get an priority-list of operating systems for select list. |
1548 |
* @return array $oss |
1549 |
*/ |
1550 |
-function mirror_get_oss_select_priority() |
1551 |
+public static function get_oss_select_priority() |
1552 |
{ |
1553 |
- $oss = db_get("SELECT os_id,os_name FROM mirror_os ORDER BY os_priority ASC",MYSQL_ASSOC); |
1554 |
+ $oss = DB::get("SELECT os_id,os_name FROM mirror_os ORDER BY os_priority ASC",PDO::FETCH_ASSOC); |
1555 |
foreach ($oss as $os) { |
1556 |
$retval[$os['os_id']]=$os['os_name']; |
1557 |
} |
1558 |
@@ -319,9 +314,9 @@ function mirror_get_oss_select_priority() |
1559 |
* Get an alpha-list of products for select list. |
1560 |
* @return array $products |
1561 |
*/ |
1562 |
-function mirror_get_products_select() |
1563 |
+public static function get_products_select() |
1564 |
{ |
1565 |
- $products = db_get("SELECT product_id,product_name FROM mirror_products ORDER BY product_name ASC",MYSQL_ASSOC); |
1566 |
+ $products = DB::get("SELECT product_id,product_name FROM mirror_products ORDER BY product_name ASC",PDO::FETCH_ASSOC); |
1567 |
foreach ($products as $product) { |
1568 |
$retval[$product['product_id']]=$product['product_name']; |
1569 |
} |
1570 |
@@ -332,9 +327,9 @@ function mirror_get_products_select() |
1571 |
* Get an priority-list of operating systems for select list. |
1572 |
* @return array $oss |
1573 |
*/ |
1574 |
-function mirror_get_products_select_priority() |
1575 |
+public static function get_products_select_priority() |
1576 |
{ |
1577 |
- $products = db_get("SELECT product_id,product_name FROM mirror_products ORDER BY product_priority ASC",MYSQL_ASSOC); |
1578 |
+ $products = DB::get("SELECT product_id,product_name FROM mirror_products ORDER BY product_priority ASC",PDO::FETCH_ASSOC); |
1579 |
foreach ($products as $product) { |
1580 |
$retval[$product['product_id']]=$product['product_name']; |
1581 |
} |
1582 |
@@ -348,9 +343,9 @@ function mirror_get_products_select_priority() |
1583 |
* @param string $path |
1584 |
* @return bool |
1585 |
*/ |
1586 |
-function mirror_insert_location($product,$os,$path) |
1587 |
+public static function insert_location($product,$os,$path) |
1588 |
{ |
1589 |
- return db_query("INSERT INTO mirror_locations(product_id,os_id,location_path) VALUES({$product},{$os},'{$path}')"); |
1590 |
+ return DB::query("INSERT INTO mirror_locations(product_id,os_id,location_path) VALUES(?,?,?)", [$product, $os, $path]); |
1591 |
} |
1592 |
|
1593 |
/** |
1594 |
@@ -361,9 +356,9 @@ function mirror_insert_location($product,$os,$path) |
1595 |
* @param string $path |
1596 |
* @return bool |
1597 |
*/ |
1598 |
-function mirror_update_location($location,$product,$os,$path) |
1599 |
+public static function update_location($location,$product,$os,$path) |
1600 |
{ |
1601 |
- return db_query("UPDATE mirror_locations SET product_id={$product},os_id={$os},location_path='{$path}' WHERE location_id={$location}"); |
1602 |
+ return DB::query("UPDATE mirror_locations SET product_id=?,os_id=?,location_path=? WHERE location_id=?", [$product, $os, $path, $location]); |
1603 |
} |
1604 |
|
1605 |
/** |
1606 |
@@ -371,18 +366,18 @@ function mirror_update_location($location,$product,$os,$path) |
1607 |
* @param int $id |
1608 |
* @return bool |
1609 |
*/ |
1610 |
-function mirror_delete_location($id) |
1611 |
+public static function delete_location($id) |
1612 |
{ |
1613 |
- return db_query("DELETE FROM mirror_locations WHERE location_id={$id}"); |
1614 |
+ return DB::query("DELETE FROM mirror_locations WHERE location_id=?", [$id]); |
1615 |
} |
1616 |
|
1617 |
/** |
1618 |
* Get locations. |
1619 |
* @return array $locations array containing all location information. |
1620 |
*/ |
1621 |
-function mirror_get_locations() |
1622 |
+public static function get_locations() |
1623 |
{ |
1624 |
- return db_get(" |
1625 |
+ return DB::get(" |
1626 |
SELECT |
1627 |
location_id, |
1628 |
product_name, |
1629 |
@@ -395,7 +390,7 @@ function mirror_get_locations() |
1630 |
WHERE |
1631 |
mirror_locations.product_id = mirror_products.product_id AND |
1632 |
mirror_locations.os_id = mirror_os.os_id |
1633 |
- "); |
1634 |
+ "); |
1635 |
} |
1636 |
|
1637 |
/** |
1638 |
@@ -403,9 +398,9 @@ function mirror_get_locations() |
1639 |
* @param int $id |
1640 |
* @return array |
1641 |
*/ |
1642 |
-function mirror_get_one_location($id) |
1643 |
+public static function get_one_location($id) |
1644 |
{ |
1645 |
- return db_get_one("SELECT * FROM mirror_locations WHERE location_id = {$id}"); |
1646 |
+ return DB::get_one("SELECT * FROM mirror_locations WHERE location_id = ?", PDO::FETCH_ASSOC, [$id]); |
1647 |
} |
1648 |
|
1649 |
/** |
1650 |
@@ -418,10 +413,12 @@ function mirror_get_one_location($id) |
1651 |
* @param string $email |
1652 |
* @return bool |
1653 |
*/ |
1654 |
-function mirror_insert_user($username,$password,$rpassword,$firstname,$lastname,$email) |
1655 |
+public static function insert_user($username,$password,$rpassword,$firstname,$lastname,$email) |
1656 |
{ |
1657 |
if ($password==$rpassword) { |
1658 |
- return db_query("INSERT INTO mirror_users(username,password,user_firstname,user_lastname,user_email) VALUES('{$username}',MD5('{$password}'),'{$firstname}','{$lastname}','{$email}')"); |
1659 |
+ $passwordHash = password_hash($password, PASSWORD_DEFAULT); |
1660 |
+ return DB::query("INSERT INTO mirror_users(username,password,user_firstname,user_lastname,user_email) VALUES(?,?,?,?,?)", |
1661 |
+ [$username, $passwordHash, $firstname, $lastname, $email]); |
1662 |
} else { |
1663 |
set_error('User could not be added because passwords did not match.'); |
1664 |
} |
1665 |
@@ -438,10 +435,15 @@ function mirror_insert_user($username,$password,$rpassword,$firstname,$lastname, |
1666 |
* @param string $email |
1667 |
* @return bool |
1668 |
*/ |
1669 |
-function mirror_update_user($user,$username,$password,$rpassword,$firstname,$lastname,$email) |
1670 |
+public static function update_user($user,$username,$password,$rpassword,$firstname,$lastname,$email) |
1671 |
{ |
1672 |
- $query = ($password==$rpassword&&!empty($password))?"UPDATE mirror_users SET username='{$username}',password=MD5('{$password}'),user_firstname='{$firstname}',user_lastname='{$lastname}',user_email='{$email}' WHERE user_id={$user}":"UPDATE mirror_users SET username='{$username}',user_firstname='{$firstname}',user_lastname='{$lastname}',user_email='{$email}' WHERE user_id={$user}"; |
1673 |
- return db_query($query); |
1674 |
+ if($password==$rpassword&&!empty($password)) { |
1675 |
+ $passwordHash = password_hash($password, PASSWORD_DEFAULT); |
1676 |
+ return DB::query("UPDATE mirror_users SET username=?,password=?,user_firstname=?,user_lastname=?,user_email=? WHERE user_id=?", |
1677 |
+ [$username, $passwordHash, $firstname, $lastname, $email, $user]); |
1678 |
+ } |
1679 |
+ return DB::query("UPDATE mirror_users SET username=?,user_firstname=?,user_lastname=?,user_email=? WHERE user_id=?", |
1680 |
+ [$username, $firstname, $lastname, $email, $user]); |
1681 |
} |
1682 |
|
1683 |
/** |
1684 |
@@ -449,18 +451,18 @@ function mirror_update_user($user,$username,$password,$rpassword,$firstname,$las |
1685 |
* @param int $id |
1686 |
* @return bool |
1687 |
*/ |
1688 |
-function mirror_delete_user($id) |
1689 |
+public static function delete_user($id) |
1690 |
{ |
1691 |
- return db_query("DELETE FROM mirror_users WHERE user_id={$id}"); |
1692 |
+ return DB::query("DELETE FROM mirror_users WHERE user_id=?", [$id]); |
1693 |
} |
1694 |
|
1695 |
/** |
1696 |
* Get users. |
1697 |
* @return array $users array containing all user information. |
1698 |
*/ |
1699 |
-function mirror_get_users() |
1700 |
+public static function get_users() |
1701 |
{ |
1702 |
- return db_get("SELECT * FROM mirror_users"); |
1703 |
+ return DB::get("SELECT * FROM mirror_users"); |
1704 |
} |
1705 |
|
1706 |
/** |
1707 |
@@ -468,9 +470,9 @@ function mirror_get_users() |
1708 |
* @param int $id |
1709 |
* @return array |
1710 |
*/ |
1711 |
-function mirror_get_one_user($id) |
1712 |
+public static function get_one_user($id) |
1713 |
{ |
1714 |
- return db_get_one("SELECT * FROM mirror_users WHERE user_id = {$id}"); |
1715 |
+ return DB::get_one("SELECT * FROM mirror_users WHERE user_id = ?", PDO::FETCH_ASSOC, [$id]); |
1716 |
} |
1717 |
|
1718 |
/** |
1719 |
@@ -478,18 +480,18 @@ function mirror_get_one_user($id) |
1720 |
* @param int $mirror |
1721 |
* @return bool |
1722 |
*/ |
1723 |
-function mirror_toggle($mirror) |
1724 |
+public static function toggle($mirror) |
1725 |
{ |
1726 |
- return (db_toggle_bool('mirror_mirrors','mirror_id','mirror_active',$mirror))?true:false; |
1727 |
+ return (DB::query("UPDATE mirror_mirrors SET mirror_active = IF(mirror_active = '1', '0', '1') WHERE mirror_id = ?", [$mirror]))?true:false; |
1728 |
} |
1729 |
|
1730 |
/** |
1731 |
* Get mirror statistics. |
1732 |
* @return array $stats |
1733 |
*/ |
1734 |
-function mirror_get_mirror_stats() |
1735 |
+public static function get_mirror_stats() |
1736 |
{ |
1737 |
- return db_get(" |
1738 |
+ return DB::get(" |
1739 |
SELECT |
1740 |
*, |
1741 |
COUNT(mirror_log.mirror_id) as count |
1742 |
@@ -511,9 +513,9 @@ function mirror_get_mirror_stats() |
1743 |
* Get product statistics. |
1744 |
* @return array $stats |
1745 |
*/ |
1746 |
-function mirror_get_product_stats() |
1747 |
+public static function get_product_stats() |
1748 |
{ |
1749 |
- return db_get(" |
1750 |
+ return DB::get(" |
1751 |
SELECT |
1752 |
*, |
1753 |
COUNT(mirror_locations.product_id) as count |
1754 |
@@ -528,4 +530,3 @@ function mirror_get_product_stats() |
1755 |
mirror_locations.product_id |
1756 |
"); |
1757 |
} |
1758 |
-?> |
1759 |
|
1760 |
diff --git a/php/rss/download-counts.php b/php/rss/download-counts.php |
1761 |
index 4559647..94d96dc 100644 |
1762 |
--- a/php/rss/download-counts.php |
1763 |
+++ b/php/rss/download-counts.php |
1764 |
@@ -8,11 +8,10 @@ |
1765 |
require_once('../cfg/config.php'); // config file |
1766 |
require_once(LIB.'/db.php'); // core mysql wrappers |
1767 |
|
1768 |
-db_connect(DBHOST,DBUSER,DBPASS); // open persistent connection to db |
1769 |
-db_select(DBNAME); // select db |
1770 |
+DB::connect(DBHOST,DBUSER,DBPASS,DBNAME); // open persistent connection to db |
1771 |
|
1772 |
// get download counts per product |
1773 |
-$data = db_get("SELECT * FROM mirror_products ORDER BY product_name"); |
1774 |
+$data = DB::get("SELECT * FROM mirror_products ORDER BY product_name"); |
1775 |
|
1776 |
// time to go at the end of each item |
1777 |
$now = date('G',time()); |
1778 |
@@ -52,4 +51,3 @@ foreach ($data as $product) { |
1779 |
} |
1780 |
|
1781 |
echo "\n".'</rdf:RDF>'; |
1782 |
-?> |