From fbbaf4cc55774a24441b7dc8ef964b3c58a5888a Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Thu, 29 Jan 2009 10:28:05 +0000 Subject: [PATCH] imported adminsearch (and required plc_objects) from plcwww as-is --- planetlab/adminsearch.php | 560 +++++++++++++++++++++++++++ planetlab/includes/plc_functions.php | 2 +- planetlab/includes/plc_objects.php | 366 +++++++++++++++++ 3 files changed, 927 insertions(+), 1 deletion(-) create mode 100644 planetlab/adminsearch.php create mode 100644 planetlab/includes/plc_objects.php diff --git a/planetlab/adminsearch.php b/planetlab/adminsearch.php new file mode 100644 index 0000000..3f182a9 --- /dev/null +++ b/planetlab/adminsearch.php @@ -0,0 +1,560 @@ += 2 ) { + return true; + } else { + return false; + } +} +function get_and_print_site($site_array) { + global $api; + $sites = $api->GetSites( $site_array, array( "name", "site_id", + "url", "enabled", "node_ids", "person_ids", "date_created", + "slice_ids", "max_slivers", "max_slices", "login_base" ) ); + if( count($sites) > 0 ) { + foreach( $sites as $site) { + $name = $site['name']; + $site_id = $site['site_id']; + $url = $site['url']; + $enabled = $site['enabled']; + $node_ids = $site['node_ids']; + $slice_ids_count = count($site['slice_ids']); + $person_ids = $site['person_ids']; + $date_created = date("M j G:i Y", $site['date_created']); + $max_slivers = $site['max_slivers']; + $max_slices = $site['max_slices']; + $login_base = $site['login_base']; + + echo "\n"; + + echo "$name\n"; + echo "(home)\n"; + echo "[ $slice_ids_count of $max_slices ]\n"; + + echo ""; + if (!$enabled) echo "Not Enabled
\n"; + //else echo "$date_created\n"; + echo "$date_created\n"; + + echo "Nodes: "; + foreach ($site['node_ids'] as $node_id) + { + echo " $node_id, "; + } + + echo "\n"; + } + } + +} +function get_and_print_hostname($host_array) { + global $api; + $nodes = $api->GetNodes( $host_array, array( "hostname", "node_id", + "site_id", "date_created", "last_contact", "boot_state" ) ); + if ( count ($nodes) > 0 ) { + foreach($nodes as $node) { + $hostname= $node['hostname']; + $node_id = $node['node_id']; + $boot_state= $node['boot_state']; + $last_contact = $node['last_contact']; + $date_created = date("M j G:i Y", $node['date_created']); + echo ""; + echo "$node_id "; + echo "$hostname"; + echo "$boot_state"; + if( $last_contact != NULL ) { + $last_contact_str = timeDiff($last_contact); + } else { + $last_contact_str = "Never"; + } + echo "$last_contact_str\n"; + echo "$date_created\n"; + echo "\n"; + } + } + + return $nodes; +} + +function get_and_print_user($user_array) { + global $api; + $persons= $api->GetPersons( $user_array, array( "person_id", "first_name", + "last_name", "email", "roles", "enabled", "date_created", + "site_ids" ) ); + + if ( count($persons) > 0 ) { + foreach($persons as $person) { + $first= $person['first_name']; + $last = $person['last_name']; + $person_id= $person['person_id']; + $email= $person['email']; + $enabled= $person['enabled']; + $roles = $person['roles']; + $role = $person['roles'][0]; + $date_created = date("M j G:i Y", $person['date_created']); + $site_ids= $person['site_ids']; + + echo "\n"; + + echo ""; + echo "$first $last"; + echo "($person_id) "; + echo "$email "; + + echo ""; + if (!$enabled) echo "Not Enabled
\n"; + //else echo "$date_created\n"; + echo "$date_created\n"; + //if (!$enabled) echo " Not Enabled "; + //else echo "$date"; + + echo " roles: "; + foreach ($person['roles'] as $role) { echo " $role, "; } + + echo "\n"; + + } + + } + return $persons; +} + +// find person roles +$_person= $plc->person; +$_roles= $_person['role_ids']; + + +/////////////////////////////////////////////////////////////////////// +// Initially, we need a search form, and blank information after that. +// Based on the fields of the search form, we should be able to populate a lot +// of information, related to user, site, keys, +// +// from a simple domain name search, it would display the site, +// nodes -> pcu +// users grouped by role, +// from a user search, it would find, +// user info link, site, roles, etc. +// +// userquery will be one of these types: +// part of a user name, email, user_id +// part of a site name, site alias, site_id +// part of a node name, domain, node_id +// +// we should be able to return results based on these guesses. If they're +// good, we'll get hits, if nothing comes back, we won't display anything. + + +echo "
\n +
\n"; + +if( $_POST['userquery'] or $_GET['userquery']) { + if ( $_POST['userquery'] ) { + $query = $_POST['userquery']; + } else { + $query = $_GET['userquery']; + } + } + +echo "

(Separate with commas for multiple queries.)\n + \n + \n +

\n +
\n +
\n"; + +// if userquery then search based on string +if( $_POST['userquery'] or $_GET['userquery']) { + if ( $_POST['userquery'] ) { + $query = $_POST['userquery']; + } else { + $query = $_GET['userquery']; + } + echo "\n"; + + $f_commas = explode(",", $query); + // PHASE 1: query contains email, user_id, or part of a user name + foreach($f_commas as $tok) { + $tok = trim($tok); // strip white space. + + $e = false; + //find user by email + if ( is_valid_email_addr($tok) ){ + $u = get_and_print_user(array("email"=>$tok)); + $e = true; + if( count( $u ) == 0 ) $none = 1; + } + $n = false; + // find user by user_id + if ( is_numeric($tok) ){ + $u = get_and_print_user(array("person_id"=>intval($tok))); + $n = true; + if( count( $u ) == 0 ) $none = 1; + } + // neither of the above, + if (!$n && !$e) { + // split on spaces, and search each part. + // TODO: search upper and lower-case + $f_spaces = explode(" ", $tok); + foreach($f_spaces as $stok) { + // assume $tok is part of a name + // get_user + $a = get_and_print_user(array("first_name"=>$stok)); + $b = get_and_print_user(array("last_name"=>$stok)); + // c = intersect_users(a, b) + // if c + // print_user(c), + // else + // print_user(a,b) + } + if( count( $a ) == 0 && count( $b ) == 0 ) $none = 1; + } + + // PHASE 2: query contains login_base, site_id, or part of a site name + + // find site by login_base + $lb = false; + $s = get_and_print_site(array("login_base"=>strtolower($tok))); + if( count( $s ) == 0 ) $none = 1; + else $lb = true; + + $n = false; + // find site by site_id + if ( is_numeric($tok) ){ + $s = get_and_print_site(array("site_id"=>intval($tok))); + $n = true; + if( count( $s ) == 0 ) $none = 1; + } + + if( !$lb && !$n ) { + $f_spaces = explode(" ", $tok); + foreach($f_spaces as $stok) { + $a = get_and_print_site(array("name"=>$stok)); + } + if( count($a) == 0 ) $none = 1; + } + + // PHASE 3: query contains part of a node name, domain, node_id + + //if( $none == 1 ) echo "\n"; + if ( is_possible_domainname($tok) ) { + $n = get_and_print_hostname(array("hostname"=>$tok)); + } + } + echo "
Search Results:
No Results.
\n"; + } + +if (false) { + // if a site_id is given, display the site nodes only + if( $_GET['site_id'] ) { + $site_id= $_GET['site_id']; + + // Get site info + $site_info= $api->GetSites( array( intval( $site_id ) ), array( "name", "person_ids" ) ); + + // Get site nodes + $persons= $api->GetPersons( $site_info[0]['person_ids'], array( "person_id", "first_name", "last_name", "email", "roles", "peer_id", "enabled" ) ); + + drupal_set_title("People with " . $site_info[0]['name']); + + sort_persons( $persons ); + + echo paginate( $persons, "person_id", "Persons", 25, "email" ); + + } + // if no person id, display list of persons to choose + elseif( !$_GET['id'] ) { + + // GetPersons API call + $persons= $api->GetPersons( NULL, array( "person_id", "first_name", "last_name", "email", "roles" , "peer_id", "enabled" ) ); + + sort_persons( $persons ); + + echo "
\n +
\n"; + if( $pers_email ) echo "'$pers_email' is not a valid person email.\n"; + echo "

\n + \n + \n +

\n +
\n"; + } else { + // get the person_id from the URL + $person_id= intval( $_GET['id'] ); + + // GetPersons API call for this person + $person_info= $api->GetPersons( array( $person_id ) ); + + if( empty( $person_info ) ) { + echo "No such person."; + + } else { + // vars from api + $first_name= $person_info[0]['first_name']; + $last_name= $person_info[0]['last_name']; + $title= $person_info[0]['title']; + $url= $person_info[0]['url']; + $phone= $person_info[0]['phone']; + $email= $person_info[0]['email']; + $enabled= $person_info[0]['enabled']; + $peer_id=$person_info[0]['peer_id']; + + // arrays from api + $role_ids= $person_info[0]['role_ids']; + $roles= $person_info[0]['roles']; + $site_ids= $person_info[0]['site_ids']; + $slice_ids= $person_info[0]['slice_ids']; + $key_ids= $person_info[0]['key_ids']; + + // gets more data from API calls + $site_info= $api->GetSites( $site_ids, array( "site_id", "name" ) ); + $slice_info= $api->GetSlices( $slice_ids, array( "slice_id", "name" ) ); + $key_info= $api->GetKeys( $key_ids ); + + drupal_set_title("$first_name $last_name Account Information"); + + // start form + if ( ! $peer_id ) { + echo "\n"; + } else { + echo "
"; + } + echo "\n"; + + if ( ! $peer_id ) { +# if ( Admin, PI, or user ) allow deletion + if( in_array( 10, $_roles ) || + ( in_array( 20, $_roles ) && in_array( $site_ids[0], $_person['site_ids'] ) ) || + $_person['person_id'] == $person_id) { + + // list to take person action + echo "

\n"; + +# if ( Admin or PI ) check whether to allow # 'enabling/disabling'. + if( in_array( 10, $_roles ) || + ( in_array( 20, $_roles ) && in_array( $site_ids[0], $_person['site_ids'] ) ) ) { + + if( $enabled == true ) { + $actions['disable']= "Disable $first_name"; + } else { + $actions['enable']= "Enable $first_name"; + $select_end = $select_end . "   ". + "<- This user is not enabled. Choose here to enable or delete."; + } + if ( in_array( 10, $_roles )) { + $actions['su'] = "Become $first_name"; + } + } + +# for all cases, list each 'select' key + foreach( $actions as $key => $val ) { + echo "

\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "
First Name: $first_name
Last Name: $last_name
Title: $title
Email: $email
Password: ***********
Phone: $phone
URL: $url
\n"; + + if( in_array( 10, $_roles ) || $_person['person_id'] == $person_id ) + echo "
Update account information\n"; + + echo "


\n"; + + // keys + $can_manage_keys = ( ( ! $peer_id ) && (in_array( "10", $_roles ) || $person_id == $_person['person_id'])); + echo "

Keys

\n"; + if( !empty( $key_ids ) ) { + echo "

\n"; + echo "\n"; + + foreach( $key_info as $key ) { + $key_type= $key['key_type']; + $key_id= $key['key_id']; + $key_text= wordwrap( $key['key'], 70, "
\n", 1 ); + echo "\n"; + } + + echo "
TypeKey
$key_type$key_text
\n"; + + + } else { + echo " This user has no known key"; + } + + if( $can_manage_keys ) + echo "

Manage Keys\n"; + echo "


"; + + + // sites + echo "

Sites

\n"; + if( !empty( $site_info ) ) { + echo "\n"; + + foreach( $site_info as $site ) { + $site_name= $site['name']; + $site_id= $site['site_id']; + + echo "\n"; + } + echo "
$site_name ( remove)
\n"; + echo "\n"; + + } else { + echo " This user is not affiliated with a site !!"; + } + + // diplay site select list to add another site for user + if( ! $peer_id && in_array( 10, $_roles ) ) { + // get site info + $full_site_info= $api->GetSites( NULL, array( "site_id", "name" ) ); + + if( $site_info ) + $person_site= arr_diff( $full_site_info, $site_info ); + else + $person_site= $full_site_info; + + sort_sites( $person_site ); + + echo "

Select a site to add this user to: "; + echo ""; + + } + echo "


\n"; + + // roles + echo "

Roles

\n"; + echo "

\n"; + echo ""; + if( in_array( "10", $_roles ) ) + echo ""; + echo "\n"; + + // construct role array + for( $n=0; $n$role_ids[$n], 'name'=>$roles[$n] ); + } + + + if ( !empty ($roles) ) { + foreach( $proles as $role ) { + $role_name= $role['name']; + $role_id= $role['role_id']; + + echo "\n"; + } + } else { + echo " This user has no known role !!"; + } + + echo "
RoleRemove
$role_name"; + + + if( in_array( 10, $_roles ) ) { + echo ""; + + } + + echo "
\n"; + + if ( in_array( 10, $_roles ) ) + echo "
\n"; + + + + // if admin show roles to add + if( in_array( 10, $_roles ) ) { + $all_roles= $api->GetRoles(); + $addable_roles= arr_diff( $all_roles, $proles ); + + if( !empty( $addable_roles ) ) { + echo "

Add role: \n"; + + } + } + + echo "


\n"; + + // slices + echo "

Slices

\n"; + if( !empty( $slice_info ) ) { + + foreach( $slice_info as $slice ) { + $slice_name= $slice['name']; + $slice_id= $slice['slice_id']; + + echo "$slice_name
\n"; + } + + } else { + echo "No slices found for that user"; + } + + if ( ! $peer_id ) { + echo "\n"; + } else { + echo "
\n"; + } + + } + echo "

Back to person list"; + + } + } + + +// Print footer +include 'plc_footer.php'; + +?> diff --git a/planetlab/includes/plc_functions.php b/planetlab/includes/plc_functions.php index 7e43d39..7a8ac86 100644 --- a/planetlab/includes/plc_functions.php +++ b/planetlab/includes/plc_functions.php @@ -95,7 +95,7 @@ function l_sirius() { return "/db/sirius/index.php"; } function l_about() { return "/db/about.php"; } function l_doc_plcapi() { return "/db/doc/PLCAPI.php"; } function l_doc_nmapi() { return "/db/doc/NMAPI.php"; } -function l_admin() { return "/db/admin-not-yet-implemented-see-plcwww"; } +function l_admin() { return "/db/adminsearch.php"; } function l_logout() { return "/planetlab/logout.php"; } function l_sulogout() { return "/planetlab/sulogout.php"; } diff --git a/planetlab/includes/plc_objects.php b/planetlab/includes/plc_objects.php new file mode 100644 index 0000000..1d3b7eb --- /dev/null +++ b/planetlab/includes/plc_objects.php @@ -0,0 +1,366 @@ + += $now) ? $action = 'away' : $action = 'ago'; + //echo "Away: $action
\n"; + //if ( $timestamp >= $now //) + //{ + // echo "Val: greater $timestamp : $now
\n"; + //} else{ + // echo "Val: less than $timestamp : $now
\n"; + //} + + + $diff = ($action == 'away' ? $timestamp - $now : $now - $timestamp); + +# Set the periods of time + $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); + $lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600); + +# Go from decades backwards to seconds + $i = sizeof($lengths) - 1; # Size of the lengths / periods in case you change them + $time = ""; # The string we will hold our times in + while($i >= $n) { +# if the difference is greater than the length we are checking... continue + if ($diff > $lengths[$i-1]) { +# 65 / 60 = 1. That means one minute. 130 / 60 = 2. Two minutes.. etc + $val = floor($diff / $lengths[$i-1]); +# The value, then the name associated, then add 's' if plural + $time .= $val ." ". $periods[$i-1].($val > 1 ? 's ' : ' '); +# subtract the values we just used from the overall diff so we can +# find the rest of the information + $diff -= ($val * $lengths[$i-1]); +# if detailed is turn off (default) only show the first set found, +# else show all information + if(!$detailed) { $i = 0; } + } + $i--; + } + +# Basic error checking. + if ($time == "") { + return "error: bad time"; + } else { + return $time.$action; + } +} + +class PlcObject { + public static function constructList($cname, $list) { + $ret_list = array(); + foreach ($list as $item) { + $ret_list[] = new $cname ($item); + } + return $ret_list; + } +} + + +class Person { + var $roles; + var $person_id; + var $first_name; + var $last_name; + var $email; + var $enabled; + + function Person($person) { + $this->roles = $person['role_ids']; + $this->person_id = $person['person_id']; + $this->first_name = $person['first_name']; + $this->last_name = $person['last_name']; + $this->email = $person['email']; + $this->enabled = $person['enabled']; + $this->data = $person; + } + + public static function getPIs($persons) { + $pis = array(); + foreach( $persons as $person ) { + $role_ids= $person->roles; + + if ( in_array( '20', $role_ids ) && $person->enabled ) + $pis[$person->person_id]= $person->email; + } + return $pis; + } + + public static function getTechs($persons) { + $techs = array(); + foreach( $persons as $person ) { + $role_ids= $person->roles; + if( in_array( '40', $role_ids ) && $person->enabled ) + $techs[$person->person_id]= $person->email; + } + return $techs; + } + + function getSites() { + return $this->data['site_ids']; + } + function isMember($site_id) { + return in_array($site_id, $this->data['site_ids']); + } + + function isAdmin() { + return in_array( '10', $this->roles); + } + function isPI() { + return in_array( '20', $this->roles); + } + function isUser() { + return in_array( '30', $this->roles); + } + function isTech() { + return in_array( '40', $this->roles); + } + + function link($str) { + return "" . $str . ""; + } + + function display() { + $person = array(); + $person[] = $this->first_name . " " . $this->last_name; + $person[] = $this->link($this->email); + return $person; + } +} + + +class PCU { + var $data; + + function PCU($pcu) { + $this->data = $pcu; + } + + function deletePCUlink($node) { + return " Remove from PCU"; + } + function pcu_name() { + if ( $this->data['hostname'] != NULL and $this->data['hostname'] != "" ): + return $this->data['hostname']; + else: + if ( $this->data['ip'] != NULL and $this->data['ip'] != "" ): + return $this->data['ip']; + else: + return "NO-HOSTNAME-OR-IP"; + endif; + endif; + } + + function link($str) { + return "" . $str . ""; + } + + function host() { + return substr($this->data['hostname'], 0, strpos($this->data['hostname'], '.')); + } +} + +class Address { + var $data; + + function Address($address) { + $this->data = $address; + } + + function link($str) { + return "" . $str . ""; + } + + function label() { + $label = ""; + $comma= sizeof( $this->data['address_types'] ); + $count= 0; + foreach( $this->data['address_types'] as $add_type ) { + $label .= $add_type; + $count++; + if ( $comma > 0 && $count != $comma ) + $label .= ", "; + } + return $label; + } + +} + + +class Node extends PlcObject { + var $node_id; + var $hostname; + var $boot_state; + var $date_created; + var $last_updated; + var $last_contact; + var $site_id; + var $pcu_ids; + var $data; + + function Node($node) { + global $plc, $api, $adm; + $this->data = $node; + $this->model = $node['model']; + $this->node_id = $node['node_id']; + $this->hostname = $node['hostname']; + $this->boot_state = $node['boot_state']; + $this->date_created = $node['date_created']; + $this->last_updated = $node['last_updated']; + $this->last_contact = $node['last_contact']; + $this->site_id = $node['site_id']; + $this->pcu_ids = $node['pcu_ids']; + $this->nn = $api->GetNodeNetworks($node['nodenetwork_ids']); + foreach ($this->nn as $nnet) + { + if ( $nnet['is_primary'] == true ) + { + $this->ip = $nnet['ip']; + $this->netmask = $nnet['netmask']; + $this->network = $nnet['network']; + $this->gateway= $nnet['gateway']; + $this->broadcast = $nnet['broadcast']; + $this->dns1 = $nnet['dns1']; + $this->dns2 = $nnet['dns2']; + $this->method = $nnet['method']; + } + } + } + + public static function filter($nodes, $nodes_listed) { + $ret = array(); + foreach ($nodes as $node) { + if ( ! in_array($node, $nodes_listed) ) { + $ret[] = $node; + } + } + return $ret; + } + + function link($str) { + return "" . $str . ""; + } + function pcuport($pcu) { + $count = 0; + foreach ( $pcu->data['node_ids'] as $node_id ) { + if ( $node_id == $this->node_id ) { + return $pcu->data['ports'][$count]; + } + $count += 1; + } + return 0; + } + + function hasPCU($pcu) { + $pcu_id = $pcu->data['pcu_id']; + return in_array( $pcu_id, $this->pcu_ids ); + } + function dateCreated() { + $date_created = date("M j, Y", $this->date_created); + return $date_created; + } + function lastUpdated() { + return $this->timeaway($this->last_updated); + } + function lastContact() { + return $this->timeaway($this->last_contact); + } + function timeaway($val) { + if ( $val != NULL ) { + $ret = timeDiff(intval($val)); + } else { + $ret = "Never"; + } + return $ret; + + } +} + +class Slice { + var $data; + + function Slice($val) { + $this->data = $val; + } + + // + function dateCreated() { + $date_created = date("M j, Y", $this->data['created']); + return $date_created; + } + + function expires() { + if ( $this->data['expires'] != 0 ) { + $expires = timeDiff(intval($this->data['expires'])); + } else { + $expires = "Never"; + } + return $expires; + } +} + +class Site extends PlcObject { + var $address_ids; + var $pcu_ids; + var $node_ids; + var $person_ids; + var $slice_ids; + var $enabled; + var $peer_id; + var $site_id; + var $data; + + function Site($site_id) { + global $plc, $api, $adm; + $site_info= $adm->GetSites( array( intval($site_id) ) ); + $this->data = $site_info[0]; + + $this->site_id = intval($site_id); + $this->site_name = $site_info[0]['name']; + $this->address_ids = $site_info[0]['address_ids']; + $this->pcu_ids = $site_info[0]['pcu_ids']; + $this->node_ids = $site_info[0]['node_ids']; + $this->person_ids = $site_info[0]['person_ids']; + $this->slice_ids = $site_info[0]['slice_ids']; + $this->enabled = $site_info[0]['enabled']; + $this->peer_id = $site_info[0]['peer_id']; + } + + function getSiteObjects() { + global $plc, $api, $adm; + $adm->begin(); + $adm->GetAddresses( $this->address_ids ); + $adm->GetPCUs( $this->pcu_ids ); + $adm->GetNodes( $this->node_ids, array( "node_id", "hostname", "boot_state", + "date_created", "last_updated", "last_contact", "site_id", "pcu_ids" ) ); + $adm->GetPersons( $this->person_ids, array( "role_ids", "person_id", "first_name", + "last_name", "email", "enabled" ) ); + $adm->GetSlices( $this->slice_ids, array( "name", "slice_id", "instantiation", "created", "expires" ) ); + return $adm->commit(); + } +} + +/* class Blue extends PlcObject + { + var $val; + function Blue($arg) + { + $this->val = $arg; + } + } + + $cl = PlcObject::constructList('Blue', array('this', 'is', 'a', 'test')); + echo sizeof($cl) . "\n"; + foreach ($cl as $obj) + { + echo get_class( $obj) . "\n"; + echo $obj->val . "\n"; + }*/ + + +?> -- 2.43.0