diff options
Diffstat (limited to 'php/lib/mirror.php')
-rw-r--r-- | php/lib/mirror.php | 205 |
1 files changed, 103 insertions, 102 deletions
diff --git a/php/lib/mirror.php b/php/lib/mirror.php index a618a86..ac3e904 100644 --- a/php/lib/mirror.php +++ b/php/lib/mirror.php @@ -6,13 +6,15 @@ * @todo add transactions once innodb table types are in place */ +class Mirror { + /** * Get an alpha-list of regions for select list. * @return array $regions */ -function mirror_get_regions_select() +public static function get_regions_select() { - $regions = db_get("SELECT region_id,region_name FROM mirror_regions ORDER BY region_name ASC",MYSQL_ASSOC); + $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']; } @@ -23,9 +25,9 @@ function mirror_get_regions_select() * Get an priority-list of regions for select list. * @return array $regions */ -function mirror_get_regions_select_priority() +public static function get_regions_select_priority() { - $regions = db_get("SELECT region_id,region_name FROM mirror_regions ORDER BY region_priority ASC",MYSQL_ASSOC); + $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']; } @@ -37,10 +39,10 @@ function mirror_get_regions_select_priority() * @param string $name * @param int $priority * @return bool - */ -function mirror_insert_region($name,$priority) + */ +public static function insert_region($name,$priority) { - return db_query("INSERT INTO mirror_regions(region_name,region_priority) VALUES('{$name}',{$priority})"); + return DB::query("INSERT INTO mirror_regions(region_name,region_priority) VALUES(?,?)", [$name, $priority]); } /** @@ -49,10 +51,10 @@ function mirror_insert_region($name,$priority) * @param string $name * @param int $priority * @return bool - */ -function mirror_update_region($id,$name,$priority) + */ +public static function update_region($id,$name,$priority) { - return db_query("UPDATE mirror_regions SET region_name='{$name}',region_priority={$priority} WHERE region_id={$id}"); + return DB::query("UPDATE mirror_regions SET region_name=?,region_priority=? WHERE region_id=?", [$name, $priority, $id]); } /** @@ -60,9 +62,9 @@ function mirror_update_region($id,$name,$priority) * @param int $id * @return array */ -function mirror_get_one_region($id) +public static function get_one_region($id) { - return db_get_one("SELECT * FROM mirror_regions WHERE region_id = {$id}"); + return DB::get_one("SELECT * FROM mirror_regions WHERE region_id = ?", PDO::FETCH_ASSOC, [$id]); } /** @@ -70,18 +72,18 @@ function mirror_get_one_region($id) * @param int $id * @return bool */ -function mirror_delete_region($id) +public static function delete_region($id) { - return db_query("DELETE FROM mirror_regions WHERE region_id={$id}"); + return DB::query("DELETE FROM mirror_regions WHERE region_id=?", [$id]); } /** * Get an alpha-list of mirrors for select list. * @return array $mirrors */ -function mirror_get_mirrors_select() +public static function get_mirrors_select() { - $mirrors = db_get("SELECT mirror_id,mirror_name FROM mirror_mirrors ORDER BY mirror_name ASC",MYSQL_ASSOC); + $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']; } @@ -92,21 +94,21 @@ function mirror_get_mirrors_select() * Get regions. * @return array */ -function mirror_get_regions() +public static function get_regions() { - return db_get(" - SELECT + return DB::get(" + SELECT mirror_regions.*, COUNT(mirror_id) as mirrors - FROM + 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 - ",MYSQL_ASSOC); + mirror_regions.region_id + ",PDO::FETCH_ASSOC); } /** @@ -117,9 +119,10 @@ function mirror_get_regions() * @param int $rating * @return bool */ -function mirror_insert_mirror($name,$region_id,$baseurl,$rating) +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; + 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; } /** @@ -130,18 +133,20 @@ function mirror_insert_mirror($name,$region_id,$baseurl,$rating) * @param int $rating * @return bool */ -function mirror_update_mirror($id,$name,$region_id,$baseurl,$rating) +public static function update_mirror($id,$name,$region_id,$baseurl,$rating) { - 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; + 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 */ -function mirror_delete_mirror($mirror_id) +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; + 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; } /** @@ -149,18 +154,18 @@ function mirror_delete_mirror($mirror_id) * @param int $mirror_id * @return array mirror information */ -function mirror_get_one_mirror($mirror_id) +public static function get_one_mirror($mirror_id) { - 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"); + 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 */ -function mirror_get_mirrors() +public static function get_mirrors() { - return db_get(" + return DB::get(" SELECT mirror_mirrors.*, IF(mirror_mirrors.mirror_active='0','DISABLED','ok') as mirror_active, @@ -172,7 +177,7 @@ function mirror_get_mirrors() WHERE mirror_regions.region_id = mirror_mirror_region_map.region_id AND mirror_mirrors.mirror_id = mirror_mirror_region_map.mirror_id - ",MYSQL_ASSOC); + ",PDO::FETCH_ASSOC); } /** @@ -181,9 +186,9 @@ function mirror_get_mirrors() * @param int $priority * @return bool */ -function mirror_insert_product($name,$priority) +public static function insert_product($name,$priority) { - return db_query("INSERT INTO mirror_products(product_name,product_priority) VALUES('{$name}',{$priority})"); + return DB::query("INSERT INTO mirror_products(product_name,product_priority) VALUES(?,?)", [$name, $priority]); } /** @@ -193,9 +198,9 @@ function mirror_insert_product($name,$priority) * @param int $priority * @return bool */ -function mirror_update_product($id,$name,$priority) +public static function update_product($id,$name,$priority) { - return db_query("UPDATE mirror_products SET product_name='{$name}',product_priority={$priority} WHERE product_id={$id}"); + return DB::query("UPDATE mirror_products SET product_name=?,product_priority=? WHERE product_id=?", [$name, $priority, $id]); } /** @@ -203,9 +208,9 @@ function mirror_update_product($id,$name,$priority) * @param int $id * @return array */ -function mirror_get_one_product($id) +public static function get_one_product($id) { - return db_get_one("SELECT * FROM mirror_products WHERE product_id = {$id}"); + return DB::get_one("SELECT * FROM mirror_products WHERE product_id = ?", PDO::FETCH_ASSOC ,[$id]); } /** @@ -213,23 +218,18 @@ function mirror_get_one_product($id) * @param int $id * @return bool */ -function mirror_delete_product($id) +public static function delete_product($id) { - return db_query("DELETE FROM mirror_products WHERE product_id={$id}"); + return DB::query("DELETE FROM mirror_products WHERE product_id=?", [$id]); } /** * Get products. * @return array */ -function mirror_get_products() +public static function get_products() { - return db_get(" - SELECT - * - FROM - mirror_products - ",MYSQL_ASSOC); + return DB::get("SELECT * FROM mirror_products",PDO::FETCH_ASSOC); } /** @@ -237,10 +237,10 @@ function mirror_get_products() * @param string $name * @param int $priority * @return bool - */ -function mirror_insert_os($name,$priority) + */ +public static function insert_os($name,$priority) { - return db_query("INSERT INTO mirror_os(os_name,os_priority) VALUES('{$name}',{$priority})"); + return DB::query("INSERT INTO mirror_os(os_name,os_priority) VALUES(?,?)", [$name, $priority]); } /** @@ -249,10 +249,10 @@ function mirror_insert_os($name,$priority) * @param string $name * @param int $priority * @return bool - */ -function mirror_update_os($id,$name,$priority) + */ +public static function update_os($id,$name,$priority) { - return db_query("UPDATE mirror_os SET os_name='{$name}',os_priority={$priority} WHERE os_id={$id}"); + return DB::query("UPDATE mirror_os SET os_name=?,os_priority=? WHERE os_id=?", [$name, $priority, $id]); } /** @@ -260,9 +260,9 @@ function mirror_update_os($id,$name,$priority) * @param int $id * @return array */ -function mirror_get_one_os($id) +public static function get_one_os($id) { - return db_get_one("SELECT * FROM mirror_os WHERE os_id = {$id}"); + return DB::get_one("SELECT * FROM mirror_os WHERE os_id = ?", PDO::FETCH_ASSOC, [$id]); } /** @@ -270,32 +270,27 @@ function mirror_get_one_os($id) * @param int $id * @return bool */ -function mirror_delete_os($id) +public static function delete_os($id) { - return db_query("DELETE FROM mirror_os WHERE os_id={$id}"); + return DB::query("DELETE FROM mirror_os WHERE os_id=?", [$id]); } /** * Get operating systems. * @return array */ -function mirror_get_oss() +public static function get_oss() { - return db_get(" - SELECT - * - FROM - mirror_os - ",MYSQL_ASSOC); + return DB::get("SELECT * FROM mirror_os",PDO::FETCH_ASSOC); } /** * Get an alpha-list of operating systems for select list. * @return array $oss */ -function mirror_get_oss_select() +public static function get_oss_select() { - $oss = db_get("SELECT os_id,os_name FROM mirror_os ORDER BY os_name ASC",MYSQL_ASSOC); + $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']; } @@ -306,9 +301,9 @@ function mirror_get_oss_select() * Get an priority-list of operating systems for select list. * @return array $oss */ -function mirror_get_oss_select_priority() +public static function get_oss_select_priority() { - $oss = db_get("SELECT os_id,os_name FROM mirror_os ORDER BY os_priority ASC",MYSQL_ASSOC); + $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']; } @@ -319,9 +314,9 @@ function mirror_get_oss_select_priority() * Get an alpha-list of products for select list. * @return array $products */ -function mirror_get_products_select() +public static function get_products_select() { - $products = db_get("SELECT product_id,product_name FROM mirror_products ORDER BY product_name ASC",MYSQL_ASSOC); + $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']; } @@ -332,9 +327,9 @@ function mirror_get_products_select() * Get an priority-list of operating systems for select list. * @return array $oss */ -function mirror_get_products_select_priority() +public static function get_products_select_priority() { - $products = db_get("SELECT product_id,product_name FROM mirror_products ORDER BY product_priority ASC",MYSQL_ASSOC); + $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']; } @@ -348,9 +343,9 @@ function mirror_get_products_select_priority() * @param string $path * @return bool */ -function mirror_insert_location($product,$os,$path) +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}')"); + return DB::query("INSERT INTO mirror_locations(product_id,os_id,location_path) VALUES(?,?,?)", [$product, $os, $path]); } /** @@ -361,9 +356,9 @@ function mirror_insert_location($product,$os,$path) * @param string $path * @return bool */ -function mirror_update_location($location,$product,$os,$path) +public static function update_location($location,$product,$os,$path) { - return db_query("UPDATE mirror_locations SET product_id={$product},os_id={$os},location_path='{$path}' WHERE location_id={$location}"); + return DB::query("UPDATE mirror_locations SET product_id=?,os_id=?,location_path=? WHERE location_id=?", [$product, $os, $path, $location]); } /** @@ -371,18 +366,18 @@ function mirror_update_location($location,$product,$os,$path) * @param int $id * @return bool */ -function mirror_delete_location($id) +public static function delete_location($id) { - return db_query("DELETE FROM mirror_locations WHERE location_id={$id}"); + return DB::query("DELETE FROM mirror_locations WHERE location_id=?", [$id]); } /** * Get locations. * @return array $locations array containing all location information. */ -function mirror_get_locations() +public static function get_locations() { - return db_get(" + return DB::get(" SELECT location_id, product_name, @@ -395,7 +390,7 @@ function mirror_get_locations() WHERE mirror_locations.product_id = mirror_products.product_id AND mirror_locations.os_id = mirror_os.os_id - "); + "); } /** @@ -403,9 +398,9 @@ function mirror_get_locations() * @param int $id * @return array */ -function mirror_get_one_location($id) +public static function get_one_location($id) { - return db_get_one("SELECT * FROM mirror_locations WHERE location_id = {$id}"); + return DB::get_one("SELECT * FROM mirror_locations WHERE location_id = ?", PDO::FETCH_ASSOC, [$id]); } /** @@ -418,10 +413,12 @@ function mirror_get_one_location($id) * @param string $email * @return bool */ -function mirror_insert_user($username,$password,$rpassword,$firstname,$lastname,$email) +public static function insert_user($username,$password,$rpassword,$firstname,$lastname,$email) { if ($password==$rpassword) { - return db_query("INSERT INTO mirror_users(username,password,user_firstname,user_lastname,user_email) VALUES('{$username}',MD5('{$password}'),'{$firstname}','{$lastname}','{$email}')"); + $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.'); } @@ -438,10 +435,15 @@ function mirror_insert_user($username,$password,$rpassword,$firstname,$lastname, * @param string $email * @return bool */ -function mirror_update_user($user,$username,$password,$rpassword,$firstname,$lastname,$email) +public static function update_user($user,$username,$password,$rpassword,$firstname,$lastname,$email) { - $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}"; - return db_query($query); + 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]); } /** @@ -449,18 +451,18 @@ function mirror_update_user($user,$username,$password,$rpassword,$firstname,$las * @param int $id * @return bool */ -function mirror_delete_user($id) +public static function delete_user($id) { - return db_query("DELETE FROM mirror_users WHERE user_id={$id}"); + return DB::query("DELETE FROM mirror_users WHERE user_id=?", [$id]); } /** * Get users. * @return array $users array containing all user information. */ -function mirror_get_users() +public static function get_users() { - return db_get("SELECT * FROM mirror_users"); + return DB::get("SELECT * FROM mirror_users"); } /** @@ -468,9 +470,9 @@ function mirror_get_users() * @param int $id * @return array */ -function mirror_get_one_user($id) +public static function get_one_user($id) { - return db_get_one("SELECT * FROM mirror_users WHERE user_id = {$id}"); + return DB::get_one("SELECT * FROM mirror_users WHERE user_id = ?", PDO::FETCH_ASSOC, [$id]); } /** @@ -478,18 +480,18 @@ function mirror_get_one_user($id) * @param int $mirror * @return bool */ -function mirror_toggle($mirror) +public static function toggle($mirror) { - return (db_toggle_bool('mirror_mirrors','mirror_id','mirror_active',$mirror))?true:false; + 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 */ -function mirror_get_mirror_stats() +public static function get_mirror_stats() { - return db_get(" + return DB::get(" SELECT *, COUNT(mirror_log.mirror_id) as count @@ -511,9 +513,9 @@ function mirror_get_mirror_stats() * Get product statistics. * @return array $stats */ -function mirror_get_product_stats() +public static function get_product_stats() { - return db_get(" + return DB::get(" SELECT *, COUNT(mirror_locations.product_id) as count @@ -528,4 +530,3 @@ function mirror_get_product_stats() mirror_locations.product_id "); } -?> |