4 require_once 'plc_login.php';
6 // Get session and API handles
7 require_once 'plc_session.php';
11 require_once 'plc_drupal.php';
13 drupal_set_title('DB Search');
14 include 'plc_header.php';
17 require_once 'plc_functions.php';
18 require_once 'plc_objects.php';
19 require_once 'plc_sorts.php';
21 function is_possible_domainname($token) {
22 if( strpos ( $token, "@" ) === False && substr_count($token, ".") >= 2 ) {
28 function get_and_print_site($site_array) {
30 $sites = $api->GetSites( $site_array, array( "name", "site_id",
31 "url", "enabled", "node_ids", "person_ids", "date_created",
32 "slice_ids", "max_slivers", "max_slices", "login_base" ) );
33 if( count($sites) > 0 ) {
34 foreach( $sites as $site) {
35 $name = $site['name'];
36 $site_id = $site['site_id'];
38 $enabled = $site['enabled'];
39 $node_ids = $site['node_ids'];
40 $slice_ids_count = count($site['slice_ids']);
41 $person_ids = $site['person_ids'];
42 $date_created = date("M j G:i Y", $site['date_created']);
43 $max_slivers = $site['max_slivers'];
44 $max_slices = $site['max_slices'];
45 $login_base = $site['login_base'];
49 echo "<td WIDTH='35%'><a href='/db/sites/index.php?id=$site_id'>$name</a>\n";
50 echo "(<a href='$url'>home</a>)</td>\n";
51 echo "<td NOWRAP>[ $slice_ids_count of $max_slices ]</td>\n";
54 if (!$enabled) echo "<font color=red>Not Enabled</font><br>\n";
55 //else echo "<td NOWRAP><i>$date_created</i></td>\n";
56 echo "<i>$date_created</i></td>\n";
58 echo "<td><strong>Nodes:</strong> ";
59 foreach ($site['node_ids'] as $node_id)
61 echo " <a href='/db/nodes/index.php?id=$node_id'>$node_id</a>, ";
69 function get_and_print_hostname($host_array) {
71 $nodes = $api->GetNodes( $host_array, array( "hostname", "node_id",
72 "site_id", "date_created", "last_contact", "boot_state" ) );
73 if ( count ($nodes) > 0 ) {
74 foreach($nodes as $node) {
75 $hostname= $node['hostname'];
76 $node_id = $node['node_id'];
77 $boot_state= $node['boot_state'];
78 $last_contact = $node['last_contact'];
79 $date_created = date("M j G:i Y", $node['date_created']);
81 echo "<td><a href='/db/nodes/index.php?id=$node_id'>$node_id</a></td> ";
82 echo "<td><a href='/db/nodes/index.php?id=$node_id'>$hostname</a></td>";
83 echo "<td>$boot_state</td>";
84 if( $last_contact != NULL ) {
85 $last_contact_str = timeDiff($last_contact);
87 $last_contact_str = "Never";
89 echo "<td NOWRAP>$last_contact_str</td>\n";
90 echo "<td NOWRAP>$date_created</td>\n";
98 function get_and_print_user($user_array) {
100 $persons= $api->GetPersons( $user_array, array( "person_id", "first_name",
101 "last_name", "email", "roles", "enabled", "date_created",
104 if ( count($persons) > 0 ) {
105 foreach($persons as $person) {
106 $first= $person['first_name'];
107 $last = $person['last_name'];
108 $person_id= $person['person_id'];
109 $email= $person['email'];
110 $enabled= $person['enabled'];
111 $roles = $person['roles'];
112 $role = $person['roles'][0];
113 $date_created = date("M j G:i Y", $person['date_created']);
114 $site_ids= $person['site_ids'];
118 echo "<a href='/db/persons/index.php?id=$person_id'>";
119 echo "$first $last</a></td>";
120 echo "<td>($person_id)</td> ";
121 echo "<td><a href='mailto:$email'>$email</a> </td>";
124 if (!$enabled) echo "<font color=red>Not Enabled</font><br>\n";
125 //else echo "<td NOWRAP><i>$date_created</i></td>\n";
126 echo "<i>$date_created</i></td>\n";
127 //if (!$enabled) echo "<td><font color=red> Not Enabled </font></td>";
128 //else echo "<td><i>$date</i></td>";
130 echo "<td> <strong>roles:</strong> ";
131 foreach ($person['roles'] as $role) { echo " $role, "; }
142 $_person= $plc->person;
143 $_roles= $_person['role_ids'];
146 ///////////////////////////////////////////////////////////////////////
147 // Initially, we need a search form, and blank information after that.
148 // Based on the fields of the search form, we should be able to populate a lot
149 // of information, related to user, site, keys,
151 // from a simple domain name search, it would display the site,
153 // users grouped by role,
154 // from a user search, it would find,
155 // user info link, site, roles, etc.
157 // userquery will be one of these types:
158 // part of a user name, email, user_id
159 // part of a site name, site alias, site_id
160 // part of a node name, domain, node_id
162 // we should be able to return results based on these guesses. If they're
163 // good, we'll get hits, if nothing comes back, we won't display anything.
167 <form method=post action='/db/adminsearch.php'>\n";
169 if( $_POST['userquery'] or $_GET['userquery']) {
170 if ( $_POST['userquery'] ) {
171 $query = $_POST['userquery'];
173 $query = $_GET['userquery'];
177 echo "<p><label for='testinput'>User or Site Name/loginbase/Email/User or Site ID:</label> (Separate with commas for multiple queries.)\n
178 <input type='text' id='testinput' name='userquery' size=50 value='$query' />\n
179 <input type=submit value='Search' />\n
184 // if userquery then search based on string
185 if( $_POST['userquery'] or $_GET['userquery']) {
186 if ( $_POST['userquery'] ) {
187 $query = $_POST['userquery'];
189 $query = $_GET['userquery'];
191 echo "<table cellspacing=2 cellpadding=1 width=100%><thead><caption><b>Search Results:</b><caption></thead><tbody>\n";
193 $f_commas = explode(",", $query);
194 // PHASE 1: query contains email, user_id, or part of a user name
195 foreach($f_commas as $tok) {
196 $tok = trim($tok); // strip white space.
200 if ( is_valid_email_addr($tok) ){
201 $u = get_and_print_user(array("email"=>$tok));
203 if( count( $u ) == 0 ) $none = 1;
206 // find user by user_id
207 if ( is_numeric($tok) ){
208 $u = get_and_print_user(array("person_id"=>intval($tok)));
210 if( count( $u ) == 0 ) $none = 1;
212 // neither of the above,
214 // split on spaces, and search each part.
215 // TODO: search upper and lower-case
216 $f_spaces = explode(" ", $tok);
217 foreach($f_spaces as $stok) {
218 // assume $tok is part of a name
220 $a = get_and_print_user(array("first_name"=>$stok));
221 $b = get_and_print_user(array("last_name"=>$stok));
222 // c = intersect_users(a, b)
228 if( count( $a ) == 0 && count( $b ) == 0 ) $none = 1;
231 // PHASE 2: query contains login_base, site_id, or part of a site name
233 // find site by login_base
235 $s = get_and_print_site(array("login_base"=>strtolower($tok)));
236 if( count( $s ) == 0 ) $none = 1;
240 // find site by site_id
241 if ( is_numeric($tok) ){
242 $s = get_and_print_site(array("site_id"=>intval($tok)));
244 if( count( $s ) == 0 ) $none = 1;
248 $f_spaces = explode(" ", $tok);
249 foreach($f_spaces as $stok) {
250 $a = get_and_print_site(array("name"=>$stok));
252 if( count($a) == 0 ) $none = 1;
255 // PHASE 3: query contains part of a node name, domain, node_id
257 //if( $none == 1 ) echo "<tr><td>No Results. </td></tr>\n";
258 if ( is_possible_domainname($tok) ) {
259 $n = get_and_print_hostname(array("hostname"=>$tok));
262 echo "</tbody></table>\n";
266 // if a site_id is given, display the site nodes only
267 if( $_GET['site_id'] ) {
268 $site_id= $_GET['site_id'];
271 $site_info= $api->GetSites( array( intval( $site_id ) ), array( "name", "person_ids" ) );
274 $persons= $api->GetPersons( $site_info[0]['person_ids'], array( "person_id", "first_name", "last_name", "email", "roles", "peer_id", "enabled" ) );
276 drupal_set_title("People with " . $site_info[0]['name']);
278 sort_persons( $persons );
280 echo paginate( $persons, "person_id", "Persons", 25, "email" );
283 // if no person id, display list of persons to choose
284 elseif( !$_GET['id'] ) {
286 // GetPersons API call
287 $persons= $api->GetPersons( NULL, array( "person_id", "first_name", "last_name", "email", "roles" , "peer_id", "enabled" ) );
289 sort_persons( $persons );
292 <form method=post action='/db/persons/allinfo.php'>\n";
293 if( $pers_email ) echo "<font color=red>'$pers_email' is not a valid person email.</font>\n";
294 echo "<p><label for='testinput'>Query: </label>\n
295 <input type='text' id='testinput' name='userquery' size=50 value='' />\n
296 <input type=submit value='Search' />\n
300 // get the person_id from the URL
301 $person_id= intval( $_GET['id'] );
303 // GetPersons API call for this person
304 $person_info= $api->GetPersons( array( $person_id ) );
306 if( empty( $person_info ) ) {
307 echo "No such person.";
311 $first_name= $person_info[0]['first_name'];
312 $last_name= $person_info[0]['last_name'];
313 $title= $person_info[0]['title'];
314 $url= $person_info[0]['url'];
315 $phone= $person_info[0]['phone'];
316 $email= $person_info[0]['email'];
317 $enabled= $person_info[0]['enabled'];
318 $peer_id=$person_info[0]['peer_id'];
321 $role_ids= $person_info[0]['role_ids'];
322 $roles= $person_info[0]['roles'];
323 $site_ids= $person_info[0]['site_ids'];
324 $slice_ids= $person_info[0]['slice_ids'];
325 $key_ids= $person_info[0]['key_ids'];
327 // gets more data from API calls
328 $site_info= $api->GetSites( $site_ids, array( "site_id", "name" ) );
329 $slice_info= $api->GetSlices( $slice_ids, array( "slice_id", "name" ) );
330 $key_info= $api->GetKeys( $key_ids );
332 drupal_set_title("$first_name $last_name Account Information");
336 echo "<form action='/db/persons/person_action.php' method='post'>\n";
338 echo "<div class='plc-foreign'>";
340 echo "<input type=hidden name='person_id' value='$person_id'>\n";
343 # if ( Admin, PI, or user ) allow deletion
344 if( in_array( 10, $_roles ) ||
345 ( in_array( 20, $_roles ) && in_array( $site_ids[0], $_person['site_ids'] ) ) ||
346 $_person['person_id'] == $person_id) {
348 // list to take person action
349 echo "<p><select name='actions' onChange=\"submit();\">\n";
351 $actions= array( ''=>'Choose Action', 'delete'=>"Delete $first_name" );
352 $select_end = "</select>\n";
354 # if ( Admin or PI ) check whether to allow # 'enabling/disabling'.
355 if( in_array( 10, $_roles ) ||
356 ( in_array( 20, $_roles ) && in_array( $site_ids[0], $_person['site_ids'] ) ) ) {
358 if( $enabled == true ) {
359 $actions['disable']= "Disable $first_name";
361 $actions['enable']= "Enable $first_name";
362 $select_end = $select_end . " <font color=red size=-1>".
363 "<- This user is not enabled. Choose here to enable or delete.</font>";
365 if ( in_array( 10, $_roles )) {
366 $actions['su'] = "Become $first_name";
370 # for all cases, list each 'select' key
371 foreach( $actions as $key => $val ) {
372 echo "<option value='$key'";
374 if( $key == $_POST['actions'] )
385 echo "<p><table border=0>\n";
386 echo "<tr><td>First Name: </td><td> $first_name</td></tr>\n";
387 echo "<tr><td>Last Name: </td><td> $last_name</td></tr>\n";
388 echo "<tr><td>Title: </td><td> $title</td></tr>\n";
389 echo "<tr><td>Email: </td><td><a href='mailto:$email'>$email</a></td></tr>\n";
390 echo "<tr><td>Password: </td><td>***********</td></tr>\n";
391 echo "<tr><td>Phone: </td><td>$phone</td></tr>\n";
392 echo "<tr><td>URL: </td><td>$url</td></tr>\n";
395 if( in_array( 10, $_roles ) || $_person['person_id'] == $person_id )
396 echo "<br /><a href='/db/persons/update.php?id=$person_id'>Update account information</a>\n";
401 $can_manage_keys = ( ( ! $peer_id ) && (in_array( "10", $_roles ) || $person_id == $_person['person_id']));
402 echo "<h3>Keys</h3>\n";
403 if( !empty( $key_ids ) ) {
404 echo "<p><table border=0 width=450>\n";
405 echo "<thead><tr><th>Type</th><th>Key</th></tr></thead><tbody>\n";
407 foreach( $key_info as $key ) {
408 $key_type= $key['key_type'];
409 $key_id= $key['key_id'];
410 $key_text= wordwrap( $key['key'], 70, "<br />\n", 1 );
411 echo "<tr><td>$key_type</td><td>$key_text</td></tr>\n";
414 echo "</tbody></table>\n";
418 echo "<span class='plc-warning'> This user has no known key</span>";
421 if( $can_manage_keys )
422 echo "<p><a href='/db/persons/keys.php?id=$person_id'>Manage Keys</a>\n";
427 echo "<h3>Sites</h3>\n";
428 if( !empty( $site_info ) ) {
429 echo "<table cellpadding=3><tbody>\n";
431 foreach( $site_info as $site ) {
432 $site_name= $site['name'];
433 $site_id= $site['site_id'];
435 echo "<tr><td><a href='/db/sites/index.php?id=$site_id'>$site_name</a> </td><td> (<input type=checkbox name='rem_site[]' value='$site_id'> remove)</td></tr>\n";
437 echo "</tbody></table>\n";
438 echo "<input type=submit value='Remove Sites'>\n";
441 echo "<span class='plc-warning'> This user is not affiliated with a site !!</span>";
444 // diplay site select list to add another site for user
445 if( ! $peer_id && in_array( 10, $_roles ) ) {
447 $full_site_info= $api->GetSites( NULL, array( "site_id", "name" ) );
450 $person_site= arr_diff( $full_site_info, $site_info );
452 $person_site= $full_site_info;
454 sort_sites( $person_site );
456 echo "<p>Select a site to add this user to: ";
457 echo "<select name='site_add' onChange='submit()'>\n<option value=''>Choose a site to add:</option>\n";
459 foreach( $person_site as $site ) {
460 echo "<option value=". $site['site_id'] .">". $site['name'] ."</option>\n";
470 echo "<h3>Roles</h3>\n";
472 echo "<thead><tr><th>Role</th>";
473 if( in_array( "10", $_roles ) )
474 echo "<th>Remove</th>";
475 echo "</tr></thead><tbody>\n";
477 // construct role array
478 for( $n=0; $n<count($roles); $n++ ) {
479 $proles[]= array( 'role_id'=>$role_ids[$n], 'name'=>$roles[$n] );
483 if ( !empty ($roles) ) {
484 foreach( $proles as $role ) {
485 $role_name= $role['name'];
486 $role_id= $role['role_id'];
488 echo "<tr><td>$role_name";
491 if( in_array( 10, $_roles ) ) {
492 echo "</td><td><input type=checkbox name='rem_role[]' value='$role_id'>";
499 echo "<span class='plc-warning'> This user has no known role !!</span>";
502 echo "</tbody></table>\n";
504 if ( in_array( 10, $_roles ) )
505 echo "<input type=submit value='Update Roles'><br />\n";
509 // if admin show roles to add
510 if( in_array( 10, $_roles ) ) {
511 $all_roles= $api->GetRoles();
512 $addable_roles= arr_diff( $all_roles, $proles );
514 if( !empty( $addable_roles ) ) {
515 echo "<p><p>Add role: <select name='add_role' onChange='submit()'>\n<option value=''>Choose a Role to add:</option>\n";
517 foreach( $addable_roles as $arole ) {
518 echo "<option value=". $arole['role_id'] .">". $arole['name'] ."</option>\n";
530 echo "<h3>Slices</h3>\n";
531 if( !empty( $slice_info ) ) {
533 foreach( $slice_info as $slice ) {
534 $slice_name= $slice['name'];
535 $slice_id= $slice['slice_id'];
537 echo "<a href='/db/slices/index.php?id=$slice_id'>$slice_name</a><br />\n";
541 echo "No slices found for that user";
551 echo "<br /><hr /><p><a href='/db/persons/index.php'>Back to person list</a></div>";
558 include 'plc_footer.php';