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 ) {
29 function get_and_print_site($site_array) {
31 $sites = $api->GetSites( $site_array, array( "name", "site_id",
32 "url", "enabled", "node_ids", "person_ids", "date_created",
33 "slice_ids", "max_slivers", "max_slices", "login_base" ) );
34 if ( count($sites) > 0 ) {
35 foreach( $sites as $site) {
36 $name = $site['name'];
37 $site_id = $site['site_id'];
39 $enabled = $site['enabled'];
40 $node_ids = $site['node_ids'];
41 $slice_ids_count = count($site['slice_ids']);
42 $person_ids = $site['person_ids'];
43 $date_created = date("M j G:i Y", $site['date_created']);
44 $max_slivers = $site['max_slivers'];
45 $max_slices = $site['max_slices'];
46 $login_base = $site['login_base'];
50 echo "<td WIDTH='35%'><a href='/db/sites/index.php?id=$site_id'>$name</a>\n";
51 echo "(<a href='$url'>home</a>)</td>\n";
52 echo "<td NOWRAP>[ $slice_ids_count of $max_slices ]</td>\n";
55 if (!$enabled) echo "<font color=red>Not Enabled</font><br>\n";
56 //else echo "<td NOWRAP><i>$date_created</i></td>\n";
57 echo "<i>$date_created</i></td>\n";
59 echo "<td><strong>Nodes:</strong> ";
60 foreach ($site['node_ids'] as $node_id)
62 echo " <a href='/db/nodes/index.php?id=$node_id'>$node_id</a>, ";
70 function get_and_print_hostname($host_array) {
72 $nodes = $api->GetNodes( $host_array, array( "hostname", "node_id",
73 "site_id", "date_created", "last_contact", "boot_state" ) );
74 if ( count ($nodes) > 0 ) {
75 foreach($nodes as $node) {
76 $hostname= $node['hostname'];
77 $node_id = $node['node_id'];
78 $boot_state= $node['boot_state'];
79 $last_contact = $node['last_contact'];
80 $date_created = date("M j G:i Y", $node['date_created']);
82 echo "<td><a href='/db/nodes/index.php?id=$node_id'>$node_id</a></td> ";
83 echo "<td><a href='/db/nodes/index.php?id=$node_id'>$hostname</a></td>";
84 echo "<td>$boot_state</td>";
85 if( $last_contact != NULL ) {
86 $last_contact_str = timeDiff($last_contact);
88 $last_contact_str = "Never";
90 echo "<td NOWRAP>$last_contact_str</td>\n";
91 echo "<td NOWRAP>$date_created</td>\n";
99 function get_and_print_user ($user_array) {
101 $persons= $api->GetPersons( $user_array, array( "person_id", "first_name",
102 "last_name", "email", "roles", "enabled", "date_created",
105 if ( count($persons) > 0 ) {
106 foreach($persons as $person) {
107 $first= $person['first_name'];
108 $last = $person['last_name'];
109 $person_id= $person['person_id'];
110 $email= $person['email'];
111 $enabled= $person['enabled'];
112 $roles = $person['roles'];
113 $role = $person['roles'][0];
114 $date_created = date("M j G:i Y", $person['date_created']);
115 $site_ids= $person['site_ids'];
119 echo "<a href='/db/persons/index.php?id=$person_id'>";
120 echo "$first $last</a></td>";
121 echo "<td>($person_id)</td> ";
122 echo "<td><a href='mailto:$email'>$email</a> </td>";
125 if (!$enabled) echo "<font color=red>Not Enabled</font><br>\n";
126 //else echo "<td NOWRAP><i>$date_created</i></td>\n";
127 echo "<i>$date_created</i></td>\n";
128 //if (!$enabled) echo "<td><font color=red> Not Enabled </font></td>";
129 //else echo "<td><i>$date</i></td>";
131 echo "<td> <strong>roles:</strong> ";
132 foreach ($person['roles'] as $role) { echo " $role, "; }
143 $_person= $plc->person;
144 $_roles= $_person['role_ids'];
147 ///////////////////////////////////////////////////////////////////////
148 // Initially, we need a search form, and blank information after that.
149 // Based on the fields of the search form, we should be able to populate a lot
150 // of information, related to user, site, keys,
152 // from a simple domain name search, it would display the site,
154 // users grouped by role,
155 // from a user search, it would find,
156 // user info link, site, roles, etc.
158 // userquery will be one of these types:
159 // part of a user name, email, user_id
160 // part of a site name, site alias, site_id
161 // part of a node name, domain, node_id
163 // we should be able to return results based on these guesses. If they're
164 // good, we'll get hits, if nothing comes back, we won't display anything.
168 <form method=post action='/db/adminsearch.php'>\n";
170 if( $_POST['userquery'] or $_GET['userquery']) {
171 if ( $_POST['userquery'] ) {
172 $query = $_POST['userquery'];
174 $query = $_GET['userquery'];
178 echo "<p><label for='testinput'>User or Site Name/loginbase/Email/User or Site ID:</label> (Separate with commas for multiple queries.)\n
179 <input type='text' id='testinput' name='userquery' size=50 value='$query' />\n
180 <input type=submit value='Search' />\n
185 // if userquery then search based on string
186 if( $_POST['userquery'] or $_GET['userquery']) {
187 if ( $_POST['userquery'] ) {
188 $query = $_POST['userquery'];
190 $query = $_GET['userquery'];
192 echo "<table cellspacing=2 cellpadding=1 width=100%><thead><caption><b>Search Results:</b><caption></thead><tbody>\n";
194 $f_commas = explode(",", $query);
195 // PHASE 1: query contains email, user_id, or part of a user name
196 foreach($f_commas as $tok) {
197 $tok = trim($tok); // strip white space.
201 if ( is_valid_email_addr($tok) ){
202 $u = get_and_print_user(array("email"=>$tok));
204 if( count( $u ) == 0 ) $none = 1;
207 // find user by user_id
208 if ( is_numeric($tok) ){
209 $u = get_and_print_user(array("person_id"=>intval($tok)));
211 if( count( $u ) == 0 ) $none = 1;
213 // neither of the above,
215 // split on spaces, and search each part.
216 // TODO: search upper and lower-case
217 $f_spaces = explode(" ", $tok);
218 foreach($f_spaces as $stok) {
219 // assume $tok is part of a name
221 $a = get_and_print_user(array("first_name"=>$stok));
222 $b = get_and_print_user(array("last_name"=>$stok));
223 // c = intersect_users(a, b)
229 if( count( $a ) == 0 && count( $b ) == 0 ) $none = 1;
232 // PHASE 2: query contains login_base, site_id, or part of a site name
234 // find site by login_base
236 $s = get_and_print_site(array("login_base"=>strtolower($tok)));
237 if( count( $s ) == 0 ) $none = 1;
241 // find site by site_id
242 if ( is_numeric($tok) ){
243 $s = get_and_print_site(array("site_id"=>intval($tok)));
245 if( count( $s ) == 0 ) $none = 1;
249 $f_spaces = explode(" ", $tok);
250 foreach($f_spaces as $stok) {
251 $a = get_and_print_site(array("name"=>$stok));
253 if( count($a) == 0 ) $none = 1;
256 // PHASE 3: query contains part of a node name, domain, node_id
258 //if( $none == 1 ) echo "<tr><td>No Results. </td></tr>\n";
259 if ( is_possible_domainname($tok) ) {
260 $n = get_and_print_hostname(array("hostname"=>$tok));
263 echo "</tbody></table>\n";
267 // if a site_id is given, display the site nodes only
268 if( $_GET['site_id'] ) {
269 $site_id= $_GET['site_id'];
272 $site_info= $api->GetSites( array( intval( $site_id ) ), array( "name", "person_ids" ) );
275 $persons= $api->GetPersons( $site_info[0]['person_ids'], array( "person_id", "first_name", "last_name", "email", "roles", "peer_id", "enabled" ) );
277 drupal_set_title("People with " . $site_info[0]['name']);
279 sort_persons( $persons );
281 echo paginate_trash ( $persons, "person_id", "Persons", 25, "email" );
284 // if no person id, display list of persons to choose
285 elseif( !$_GET['id'] ) {
287 // GetPersons API call
288 $persons= $api->GetPersons( NULL, array( "person_id", "first_name", "last_name", "email", "roles" , "peer_id", "enabled" ) );
290 sort_persons( $persons );
293 <form method=post action='/db/persons/allinfo.php'>\n";
294 if( $pers_email ) echo "<font color=red>'$pers_email' is not a valid person email.</font>\n";
295 echo "<p><label for='testinput'>Query: </label>\n
296 <input type='text' id='testinput' name='userquery' size=50 value='' />\n
297 <input type=submit value='Search' />\n
301 // get the person_id from the URL
302 $person_id= intval( $_GET['id'] );
304 // GetPersons API call for this person
305 $person_info= $api->GetPersons( array( $person_id ) );
307 if( empty( $person_info ) ) {
308 echo "No such person.";
312 $first_name= $person_info[0]['first_name'];
313 $last_name= $person_info[0]['last_name'];
314 $title= $person_info[0]['title'];
315 $url= $person_info[0]['url'];
316 $phone= $person_info[0]['phone'];
317 $email= $person_info[0]['email'];
318 $enabled= $person_info[0]['enabled'];
319 $peer_id=$person_info[0]['peer_id'];
322 $role_ids= $person_info[0]['role_ids'];
323 $roles= $person_info[0]['roles'];
324 $site_ids= $person_info[0]['site_ids'];
325 $slice_ids= $person_info[0]['slice_ids'];
326 $key_ids= $person_info[0]['key_ids'];
328 // gets more data from API calls
329 $site_info= $api->GetSites( $site_ids, array( "site_id", "name" ) );
330 $slice_info= $api->GetSlices( $slice_ids, array( "slice_id", "name" ) );
331 $key_info= $api->GetKeys( $key_ids );
333 drupal_set_title("$first_name $last_name Account Information");
337 echo "<form action='/db/persons/person_action.php' method='post'>\n";
339 echo "<div class='plc-foreign'>";
341 echo "<input type=hidden name='person_id' value='$person_id'>\n";
344 # if ( Admin, PI, or user ) allow deletion
345 if( in_array( 10, $_roles ) ||
346 ( in_array( 20, $_roles ) && in_array( $site_ids[0], $_person['site_ids'] ) ) ||
347 $_person['person_id'] == $person_id) {
349 // list to take person action
350 echo "<p><select name='actions' onChange=\"submit();\">\n";
352 $actions= array( ''=>'Choose Action', 'delete'=>"Delete $first_name" );
353 $select_end = "</select>\n";
355 # if ( Admin or PI ) check whether to allow # 'enabling/disabling'.
356 if( in_array( 10, $_roles ) ||
357 ( in_array( 20, $_roles ) && in_array( $site_ids[0], $_person['site_ids'] ) ) ) {
359 if( $enabled == true ) {
360 $actions['disable']= "Disable $first_name";
362 $actions['enable']= "Enable $first_name";
363 $select_end = $select_end . " <font color=red size=-1>".
364 "<- This user is not enabled. Choose here to enable or delete.</font>";
366 if ( in_array( 10, $_roles )) {
367 $actions['su'] = "Become $first_name";
371 # for all cases, list each 'select' key
372 foreach( $actions as $key => $val ) {
373 echo "<option value='$key'";
375 if( $key == $_POST['actions'] )
386 echo "<p><table border=0>\n";
387 echo "<tr><td>First Name: </td><td> $first_name</td></tr>\n";
388 echo "<tr><td>Last Name: </td><td> $last_name</td></tr>\n";
389 echo "<tr><td>Title: </td><td> $title</td></tr>\n";
390 echo "<tr><td>Email: </td><td><a href='mailto:$email'>$email</a></td></tr>\n";
391 echo "<tr><td>Password: </td><td>***********</td></tr>\n";
392 echo "<tr><td>Phone: </td><td>$phone</td></tr>\n";
393 echo "<tr><td>URL: </td><td>$url</td></tr>\n";
396 if( in_array( 10, $_roles ) || $_person['person_id'] == $person_id )
397 echo "<br /><a href='/db/persons/update.php?id=$person_id'>Update account information</a>\n";
402 $can_manage_keys = ( ( ! $peer_id ) && (in_array( "10", $_roles ) || $person_id == $_person['person_id']));
403 echo "<h3>Keys</h3>\n";
404 if( !empty( $key_ids ) ) {
405 echo "<p><table border=0 width=450>\n";
406 echo "<thead><tr><th>Type</th><th>Key</th></tr></thead><tbody>\n";
408 foreach( $key_info as $key ) {
409 $key_type= $key['key_type'];
410 $key_id= $key['key_id'];
411 $key_html= wordwrap( $key['key'], 70, "<br />\n", 1 );
412 echo "<tr><td>$key_type</td><td>$key_html</td></tr>\n";
415 echo "</tbody></table>\n";
419 echo "<span class='plc-warning'> This user has no known key</span>";
422 if( $can_manage_keys )
423 echo "<p><a href='/db/persons/keys.php?id=$person_id'>Manage Keys</a>\n";
428 echo "<h3>Sites</h3>\n";
429 if( !empty( $site_info ) ) {
430 echo "<table cellpadding=3><tbody>\n";
432 foreach( $site_info as $site ) {
433 $site_name= $site['name'];
434 $site_id= $site['site_id'];
436 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";
438 echo "</tbody></table>\n";
439 echo "<input type=submit value='Remove Sites'>\n";
442 echo "<span class='plc-warning'> This user is not affiliated with a site !!</span>";
445 // diplay site select list to add another site for user
446 if( ! $peer_id && in_array( 10, $_roles ) ) {
448 $full_site_info= $api->GetSites( NULL, array( "site_id", "name" ) );
451 $person_site= arr_diff( $full_site_info, $site_info );
453 $person_site= $full_site_info;
455 sort_sites( $person_site );
457 echo "<p>Select a site to add this user to: ";
458 echo "<select name='site_add' onChange='submit()'>\n<option value=''>Choose a site to add:</option>\n";
460 foreach( $person_site as $site ) {
461 echo "<option value=". $site['site_id'] .">". $site['name'] ."</option>\n";
471 echo "<h3>Roles</h3>\n";
473 echo "<thead><tr><th>Role</th>";
474 if( in_array( "10", $_roles ) )
475 echo "<th>Remove</th>";
476 echo "</tr></thead><tbody>\n";
478 // construct role array
479 for( $n=0; $n<count($roles); $n++ ) {
480 $proles[]= array( 'role_id'=>$role_ids[$n], 'name'=>$roles[$n] );
484 if ( !empty ($roles) ) {
485 foreach( $proles as $role ) {
486 $role_name= $role['name'];
487 $role_id= $role['role_id'];
489 echo "<tr><td>$role_name";
492 if( in_array( 10, $_roles ) ) {
493 echo "</td><td><input type=checkbox name='rem_role[]' value='$role_id'>";
500 echo "<span class='plc-warning'> This user has no known role !!</span>";
503 echo "</tbody></table>\n";
505 if ( in_array( 10, $_roles ) )
506 echo "<input type=submit value='Update Roles'><br />\n";
510 // if admin show roles to add
511 if( in_array( 10, $_roles ) ) {
512 $all_roles= $api->GetRoles();
513 $addable_roles= arr_diff( $all_roles, $proles );
515 if( !empty( $addable_roles ) ) {
516 echo "<p><p>Add role: <select name='add_role' onChange='submit()'>\n<option value=''>Choose a Role to add:</option>\n";
518 foreach( $addable_roles as $arole ) {
519 echo "<option value=". $arole['role_id'] .">". $arole['name'] ."</option>\n";
531 echo "<h3>Slices</h3>\n";
532 if( !empty( $slice_info ) ) {
534 foreach( $slice_info as $slice ) {
535 $slice_name= $slice['name'];
536 $slice_id= $slice['slice_id'];
538 echo "<a href='/db/slices/index.php?id=$slice_id'>$slice_name</a><br />\n";
542 echo "No slices found for that user";
552 echo "<br /><hr /><p><a href='/db/persons/index.php'>Back to person list</a></div>";
559 include 'plc_footer.php';