aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Evans <grknight@gentoo.org>2018-01-29 16:04:13 -0500
committerBrian Evans <grknight@gentoo.org>2018-01-29 16:05:35 -0500
commit3c56284f7fe1926f6bf6ebd97ec72805d94df1e4 (patch)
tree805a3fb4cfb14519c0bebc3f1863f4759e916a82 /php/lib/mirror.php
downloadbouncer-3c56284f7fe1926f6bf6ebd97ec72805d94df1e4.tar.gz
bouncer-3c56284f7fe1926f6bf6ebd97ec72805d94df1e4.tar.bz2
bouncer-3c56284f7fe1926f6bf6ebd97ec72805d94df1e4.zip
Import existing bouncer.g.o into gitv1.0-20181208-IMPORT
Diffstat (limited to 'php/lib/mirror.php')
-rw-r--r--php/lib/mirror.php531
1 files changed, 531 insertions, 0 deletions
diff --git a/php/lib/mirror.php b/php/lib/mirror.php
new file mode 100644
index 0000000..a618a86
--- /dev/null
+++ b/php/lib/mirror.php
@@ -0,0 +1,531 @@
+<?php
+/**
+ * Application functions. Dependent on lib/db.php!
+ * @package mirror
+ * @subpackage lib
+ * @todo add transactions once innodb table types are in place
+ */
+
+/**
+ * Get an alpha-list of regions for select list.
+ * @return array $regions
+ */
+function mirror_get_regions_select()
+{
+ $regions = db_get("SELECT region_id,region_name FROM mirror_regions ORDER BY region_name ASC",MYSQL_ASSOC);
+ foreach ($regions as $region) {
+ $retval[$region['region_id']]=$region['region_name'];
+ }
+ return $retval;
+}
+
+/**
+ * Get an priority-list of regions for select list.
+ * @return array $regions
+ */
+function mirror_get_regions_select_priority()
+{
+ $regions = db_get("SELECT region_id,region_name FROM mirror_regions ORDER BY region_priority ASC",MYSQL_ASSOC);
+ foreach ($regions as $region) {
+ $retval[$region['region_id']]=$region['region_name'];
+ }
+ return $retval;
+}
+
+/**
+ * Insert region.
+ * @param string $name
+ * @param int $priority
+ * @return bool
+ */
+function mirror_insert_region($name,$priority)
+{
+ return db_query("INSERT INTO mirror_regions(region_name,region_priority) VALUES('{$name}',{$priority})");
+}
+
+/**
+ * Update region.
+ * @param int $id
+ * @param string $name
+ * @param int $priority
+ * @return bool
+ */
+function mirror_update_region($id,$name,$priority)
+{
+ return db_query("UPDATE mirror_regions SET region_name='{$name}',region_priority={$priority} WHERE region_id={$id}");
+}
+
+/**
+ * Get one region.
+ * @param int $id
+ * @return array
+ */
+function mirror_get_one_region($id)
+{
+ return db_get_one("SELECT * FROM mirror_regions WHERE region_id = {$id}");
+}
+
+/**
+ * Delete a region.
+ * @param int $id
+ * @return bool
+ */
+function mirror_delete_region($id)
+{
+ return db_query("DELETE FROM mirror_regions WHERE region_id={$id}");
+}
+
+/**
+ * Get an alpha-list of mirrors for select list.
+ * @return array $mirrors
+ */
+function mirror_get_mirrors_select()
+{
+ $mirrors = db_get("SELECT mirror_id,mirror_name FROM mirror_mirrors ORDER BY mirror_name ASC",MYSQL_ASSOC);
+ foreach ($mirrors as $mirror) {
+ $retval[$mirror['mirror_id']]=$mirror['mirror_name'];
+ }
+ return $retval;
+}
+
+/**
+ * Get regions.
+ * @return array
+ */
+function mirror_get_regions()
+{
+ return db_get("
+ SELECT
+ mirror_regions.*,
+ COUNT(mirror_id) as mirrors
+ FROM
+ mirror_regions
+ LEFT JOIN
+ mirror_mirror_region_map
+ ON
+ mirror_regions.region_id = mirror_mirror_region_map.region_id
+ GROUP BY
+ mirror_regions.region_id
+ ",MYSQL_ASSOC);
+}
+
+/**
+ * Insert mirror.
+ * @param string $name
+ * @param int $region_id
+ * @param string $baseurl
+ * @param int $rating
+ * @return bool
+ */
+function mirror_insert_mirror($name,$region_id,$baseurl,$rating)
+{
+ return (db_query("INSERT INTO mirror_mirrors(mirror_name,mirror_baseurl,mirror_rating) VALUES('{$name}','{$baseurl}','{$rating}')") && db_query("INSERT INTO mirror_mirror_region_map(mirror_id,region_id) VALUES('".db_insert_id()."','$region_id')"))?true:false;
+}
+
+/**
+ * Update mirror.
+ * @param string $name
+ * @param int $region_id
+ * @param string $baseurl
+ * @param int $rating
+ * @return bool
+ */
+function mirror_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;
+}
+
+/**
+ * Delete mirror.
+ * @return bool
+ */
+function mirror_delete_mirror($mirror_id)
+{
+ return (db_query("DELETE FROM mirror_mirrors WHERE mirror_id={$mirror_id}")&&db_query("DELETE FROM mirror_mirror_region_map WHERE mirror_id={$mirror_id}"))?true:false;
+}
+
+/**
+ * Get one mirror record.
+ * @param int $mirror_id
+ * @return array mirror information
+ */
+function mirror_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");
+}
+
+/**
+ * Get list of mirrors.
+ * @return array
+ */
+function mirror_get_mirrors()
+{
+ return db_get("
+ SELECT
+ mirror_mirrors.*,
+ IF(mirror_mirrors.mirror_active='0','DISABLED','ok') as mirror_active,
+ region_name
+ FROM
+ mirror_mirrors,
+ mirror_regions,
+ mirror_mirror_region_map
+ WHERE
+ mirror_regions.region_id = mirror_mirror_region_map.region_id AND
+ mirror_mirrors.mirror_id = mirror_mirror_region_map.mirror_id
+ ",MYSQL_ASSOC);
+}
+
+/**
+ * Insert product.
+ * @param string $name
+ * @param int $priority
+ * @return bool
+ */
+function mirror_insert_product($name,$priority)
+{
+ return db_query("INSERT INTO mirror_products(product_name,product_priority) VALUES('{$name}',{$priority})");
+}
+
+/**
+ * Update product.
+ * @param int $id
+ * @param string $name
+ * @param int $priority
+ * @return bool
+ */
+function mirror_update_product($id,$name,$priority)
+{
+ return db_query("UPDATE mirror_products SET product_name='{$name}',product_priority={$priority} WHERE product_id={$id}");
+}
+
+/**
+ * Get one product.
+ * @param int $id
+ * @return array
+ */
+function mirror_get_one_product($id)
+{
+ return db_get_one("SELECT * FROM mirror_products WHERE product_id = {$id}");
+}
+
+/**
+ * Delete a product.
+ * @param int $id
+ * @return bool
+ */
+function mirror_delete_product($id)
+{
+ return db_query("DELETE FROM mirror_products WHERE product_id={$id}");
+}
+
+/**
+ * Get products.
+ * @return array
+ */
+function mirror_get_products()
+{
+ return db_get("
+ SELECT
+ *
+ FROM
+ mirror_products
+ ",MYSQL_ASSOC);
+}
+
+/**
+ * Insert os.
+ * @param string $name
+ * @param int $priority
+ * @return bool
+ */
+function mirror_insert_os($name,$priority)
+{
+ return db_query("INSERT INTO mirror_os(os_name,os_priority) VALUES('{$name}',{$priority})");
+}
+
+/**
+ * Update os.
+ * @param int $id
+ * @param string $name
+ * @param int $priority
+ * @return bool
+ */
+function mirror_update_os($id,$name,$priority)
+{
+ return db_query("UPDATE mirror_os SET os_name='{$name}',os_priority={$priority} WHERE os_id={$id}");
+}
+
+/**
+ * Get one os.
+ * @param int $id
+ * @return array
+ */
+function mirror_get_one_os($id)
+{
+ return db_get_one("SELECT * FROM mirror_os WHERE os_id = {$id}");
+}
+
+/**
+ * Delete a os.
+ * @param int $id
+ * @return bool
+ */
+function mirror_delete_os($id)
+{
+ return db_query("DELETE FROM mirror_os WHERE os_id={$id}");
+}
+
+/**
+ * Get operating systems.
+ * @return array
+ */
+function mirror_get_oss()
+{
+ return db_get("
+ SELECT
+ *
+ FROM
+ mirror_os
+ ",MYSQL_ASSOC);
+}
+
+/**
+ * Get an alpha-list of operating systems for select list.
+ * @return array $oss
+ */
+function mirror_get_oss_select()
+{
+ $oss = db_get("SELECT os_id,os_name FROM mirror_os ORDER BY os_name ASC",MYSQL_ASSOC);
+ foreach ($oss as $os) {
+ $retval[$os['os_id']]=$os['os_name'];
+ }
+ return $retval;
+}
+
+/**
+ * Get an priority-list of operating systems for select list.
+ * @return array $oss
+ */
+function mirror_get_oss_select_priority()
+{
+ $oss = db_get("SELECT os_id,os_name FROM mirror_os ORDER BY os_priority ASC",MYSQL_ASSOC);
+ foreach ($oss as $os) {
+ $retval[$os['os_id']]=$os['os_name'];
+ }
+ return $retval;
+}
+
+/**
+ * Get an alpha-list of products for select list.
+ * @return array $products
+ */
+function mirror_get_products_select()
+{
+ $products = db_get("SELECT product_id,product_name FROM mirror_products ORDER BY product_name ASC",MYSQL_ASSOC);
+ foreach ($products as $product) {
+ $retval[$product['product_id']]=$product['product_name'];
+ }
+ return $retval;
+}
+
+/**
+ * Get an priority-list of operating systems for select list.
+ * @return array $oss
+ */
+function mirror_get_products_select_priority()
+{
+ $products = db_get("SELECT product_id,product_name FROM mirror_products ORDER BY product_priority ASC",MYSQL_ASSOC);
+ foreach ($products as $product) {
+ $retval[$product['product_id']]=$product['product_name'];
+ }
+ return $retval;
+}
+
+/**
+ * Insert a new location.
+ * @param int $product
+ * @param int $os
+ * @param string $path
+ * @return bool
+ */
+function mirror_insert_location($product,$os,$path)
+{
+ return db_query("INSERT INTO mirror_locations(product_id,os_id,location_path) VALUES({$product},{$os},'{$path}')");
+}
+
+/**
+ * Update a location.
+ * @param int $location
+ * @param int $product
+ * @param int $os
+ * @param string $path
+ * @return bool
+ */
+function mirror_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}");
+}
+
+/**
+ * Delete a location.
+ * @param int $id
+ * @return bool
+ */
+function mirror_delete_location($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()
+{
+ return db_get("
+ SELECT
+ location_id,
+ product_name,
+ os_name,
+ location_path
+ FROM
+ mirror_locations,
+ mirror_products,
+ mirror_os
+ WHERE
+ mirror_locations.product_id = mirror_products.product_id AND
+ mirror_locations.os_id = mirror_os.os_id
+ ");
+}
+
+/**
+ * Get one location.
+ * @param int $id
+ * @return array
+ */
+function mirror_get_one_location($id)
+{
+ return db_get_one("SELECT * FROM mirror_locations WHERE location_id = {$id}");
+}
+
+/**
+ * Insert a new user.
+ * @param string $username
+ * @param string $password
+ * @param string $rpassword (re-entered password)
+ * @param string $firstname
+ * @param string $lastname
+ * @param string $email
+ * @return bool
+ */
+function mirror_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}')");
+ } else {
+ set_error('User could not be added because passwords did not match.');
+ }
+}
+
+/**
+ * Update a user.
+ * @param int $user
+ * @param string $username
+ * @param string $password
+ * @param string $rpassword (re-entered password)
+ * @param string $firstname
+ * @param string $lastname
+ * @param string $email
+ * @return bool
+ */
+function mirror_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);
+}
+
+/**
+ * Delete a user.
+ * @param int $id
+ * @return bool
+ */
+function mirror_delete_user($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()
+{
+ return db_get("SELECT * FROM mirror_users");
+}
+
+/**
+ * Get one user.
+ * @param int $id
+ * @return array
+ */
+function mirror_get_one_user($id)
+{
+ return db_get_one("SELECT * FROM mirror_users WHERE user_id = {$id}");
+}
+
+/**
+ * Enable or disable a mirror.
+ * @param int $mirror
+ * @return bool
+ */
+function mirror_toggle($mirror)
+{
+ return (db_toggle_bool('mirror_mirrors','mirror_id','mirror_active',$mirror))?true:false;
+}
+
+/**
+ * Get mirror statistics.
+ * @return array $stats
+ */
+function mirror_get_mirror_stats()
+{
+ return db_get("
+ SELECT
+ *,
+ COUNT(mirror_log.mirror_id) as count
+ FROM
+ mirror_mirrors,
+ mirror_log,
+ mirror_regions,
+ mirror_mirror_region_map
+ WHERE
+ mirror_log.mirror_id = mirror_mirrors.mirror_id AND
+ mirror_mirrors.mirror_id = mirror_mirror_region_map.mirror_id AND
+ mirror_regions.region_id = mirror_mirror_region_map.region_id
+ GROUP BY
+ mirror_log.mirror_id
+ ");
+}
+
+/**
+ * Get product statistics.
+ * @return array $stats
+ */
+function mirror_get_product_stats()
+{
+ return db_get("
+ SELECT
+ *,
+ COUNT(mirror_locations.product_id) as count
+ FROM
+ mirror_log,
+ mirror_locations,
+ mirror_products
+ WHERE
+ mirror_log.location_id = mirror_locations.location_id AND
+ mirror_locations.product_id = mirror_products.product_id
+ GROUP BY
+ mirror_locations.product_id
+ ");
+}
+?>