imported adminsearch (and required plc_objects) from plcwww as-is
[plewww.git] / planetlab / adminsearch.php
1 <?php
2
3   // Require login
4 require_once 'plc_login.php';
5
6 // Get session and API handles
7 require_once 'plc_session.php';
8 global $plc, $api;
9
10 // Print header
11 require_once 'plc_drupal.php';
12 // set default 
13 drupal_set_title('DB Search');
14 include 'plc_header.php'; 
15
16 // Common functions
17 require_once 'plc_functions.php';
18 require_once 'plc_objects.php';
19 require_once 'plc_sorts.php';
20
21 function is_possible_domainname($token) {
22   if( strpos ( $token, "@" ) === False && substr_count($token, ".") >= 2 ) {
23     return true;
24   } else {
25     return false;
26   }
27 }
28 function get_and_print_site($site_array) {
29   global $api;
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'];
37       $url =  $site['url'];
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'];
46
47       echo "<tr>\n";
48
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";
52
53       echo "<td NOWRAP>";
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";
57                         
58       echo "<td><strong>Nodes:</strong> ";
59       foreach ($site['node_ids'] as $node_id) 
60         { 
61           echo " <a href='/db/nodes/index.php?id=$node_id'>$node_id</a>, "; 
62         }
63
64       echo "</td></tr>\n";
65     }
66   }
67
68 }
69 function get_and_print_hostname($host_array) {
70   global $api;
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']);
80       echo "<tr>";
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);
86       } else {
87         $last_contact_str = "Never";
88       }
89       echo "<td NOWRAP>$last_contact_str</td>\n";
90       echo "<td NOWRAP>$date_created</td>\n";
91       echo "</tr>\n";
92     }
93   }
94
95   return $nodes;
96 }
97
98 function get_and_print_user($user_array) {
99   global $api;
100   $persons= $api->GetPersons( $user_array, array( "person_id", "first_name", 
101                                                   "last_name", "email", "roles", "enabled", "date_created", 
102                                                   "site_ids" ) );
103
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'];
115
116       echo "<tr><td>\n";
117
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>";
122
123       echo "<td NOWRAP>";
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>";
129                         
130       echo "<td> <strong>roles:</strong> ";
131       foreach ($person['roles'] as $role) { echo " $role, "; }
132
133       echo "</td></tr>\n";
134
135     }
136                 
137   }
138   return $persons;
139 }
140
141 // find person roles
142 $_person= $plc->person;
143 $_roles= $_person['role_ids'];
144
145
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, 
150 // 
151 //      from a simple domain name search, it would display the site, 
152 //              nodes -> pcu
153 //              users grouped by role, 
154 //  from a user search, it would find,
155 //              user info link, site, roles, etc.
156 //
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
161 //
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.
164
165
166 echo "<div>\n
167         <form method=post action='/db/adminsearch.php'>\n";
168
169 if( $_POST['userquery'] or $_GET['userquery']) {
170   if ( $_POST['userquery'] ) {
171     $query = $_POST['userquery'];
172   } else {
173     $query = $_GET['userquery'];
174   }
175  }
176
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
180                 </form>\n
181         </div>\n
182         <br />\n";
183
184 // if userquery then search based on string
185 if( $_POST['userquery'] or $_GET['userquery']) {
186   if ( $_POST['userquery'] ) {
187     $query = $_POST['userquery'];
188   } else {
189     $query = $_GET['userquery'];
190   }
191   echo "<table cellspacing=2 cellpadding=1 width=100%><thead><caption><b>Search Results:</b><caption></thead><tbody>\n";
192
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.
197
198     $e = false;
199     //find user by email
200     if ( is_valid_email_addr($tok) ){
201       $u = get_and_print_user(array("email"=>$tok));
202       $e = true;
203       if( count( $u ) == 0 ) $none = 1;
204     }
205     $n = false;
206     // find user by user_id
207     if ( is_numeric($tok) ){
208       $u = get_and_print_user(array("person_id"=>intval($tok)));
209       $n = true;
210       if( count( $u ) == 0 ) $none = 1;
211     }
212     // neither of the above, 
213     if (!$n && !$e) {
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
219         // get_user
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)
223         // if c 
224         //     print_user(c), 
225         // else 
226         //     print_user(a,b)
227       }
228       if( count( $a ) == 0 && count( $b ) == 0 ) $none = 1;
229     }
230
231     // PHASE 2: query contains login_base, site_id, or part of a site name
232
233     // find site by login_base
234     $lb = false;
235     $s = get_and_print_site(array("login_base"=>strtolower($tok)));
236     if( count( $s ) == 0 ) $none = 1;
237     else $lb = true;
238
239     $n = false;
240     // find site by site_id
241     if ( is_numeric($tok) ){
242       $s = get_and_print_site(array("site_id"=>intval($tok)));
243       $n = true;
244       if( count( $s ) == 0 ) $none = 1;
245     }
246
247     if( !$lb && !$n ) {
248       $f_spaces = explode(" ", $tok);
249       foreach($f_spaces as $stok) {
250         $a = get_and_print_site(array("name"=>$stok));
251       }
252       if( count($a) == 0 ) $none = 1;
253     }
254
255     // PHASE 3: query contains part of a node name, domain, node_id
256
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));
260     }
261   }
262   echo "</tbody></table>\n";
263  }
264
265 if (false) {
266   // if a site_id is given, display the site nodes only
267   if( $_GET['site_id'] ) {
268     $site_id= $_GET['site_id'];
269
270     // Get site info
271     $site_info= $api->GetSites( array( intval( $site_id ) ), array( "name", "person_ids" ) );
272
273     // Get site nodes
274     $persons= $api->GetPersons( $site_info[0]['person_ids'], array( "person_id", "first_name", "last_name", "email", "roles", "peer_id", "enabled" ) );
275           
276     drupal_set_title("People with " . $site_info[0]['name']);
277
278     sort_persons( $persons );
279
280     echo paginate( $persons, "person_id", "Persons", 25, "email" );
281
282   }
283   // if no person id, display list of persons to choose
284   elseif( !$_GET['id'] ) {
285
286     // GetPersons API call
287     $persons= $api->GetPersons( NULL, array( "person_id", "first_name", "last_name", "email", "roles" , "peer_id", "enabled" ) );
288
289     sort_persons( $persons );
290
291     echo "<div>\n
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
297                         </div>\n
298                         <br />\n";
299   } else {
300     // get the person_id from the URL
301     $person_id= intval( $_GET['id'] );
302
303     // GetPersons API call for this person
304     $person_info= $api->GetPersons( array( $person_id ) );
305           
306     if( empty( $person_info ) ) {
307       echo "No such person.";
308                         
309     } else {
310       // vars from api
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'];
319                         
320       // arrays from api
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'];
326                         
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 );
331                         
332       drupal_set_title("$first_name $last_name Account Information");
333
334       // start form
335       if ( ! $peer_id ) {
336         echo "<form action='/db/persons/person_action.php' method='post'>\n";
337       } else {
338         echo "<div class='plc-foreign'>";
339       }
340       echo "<input type=hidden name='person_id' value='$person_id'>\n";
341                 
342       if ( ! $peer_id ) {
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) {
347
348           // list to take person action
349           echo "<p><select name='actions' onChange=\"submit();\">\n";
350                                 
351           $actions= array( ''=>'Choose Action', 'delete'=>"Delete $first_name" );
352           $select_end = "</select>\n";
353
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'] ) ) ) { 
357
358             if( $enabled == true ) {
359               $actions['disable']= "Disable $first_name";
360             } else {
361               $actions['enable']= "Enable $first_name";
362               $select_end = $select_end . " &nbsp; <font color=red size=-1>".
363                 "<- This user is not enabled. Choose here to enable or delete.</font>";
364             }
365             if ( in_array( 10, $_roles )) {
366               $actions['su'] = "Become $first_name";
367             }
368           } 
369                                 
370 # for all cases, list each 'select' key
371           foreach( $actions as $key => $val ) {
372             echo "<option value='$key'";
373                 
374             if( $key == $_POST['actions'] )
375               echo " selected";
376                 
377             echo ">$val\n";
378           }
379                 
380           echo $select_end;
381         }
382       } 
383                 
384       // basic person info
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";
393       echo "</table>\n";
394                 
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";
397
398       echo "<hr />\n";
399                         
400       // keys
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";
406                                 
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";
412         }
413                 
414         echo "</tbody></table>\n";
415
416                 
417       } else {
418         echo "<span class='plc-warning'> This user has no known key</span>";
419       }
420                         
421       if( $can_manage_keys )
422         echo "<p><a href='/db/persons/keys.php?id=$person_id'>Manage Keys</a>\n";
423       echo "<hr />";
424                 
425                 
426       // sites
427       echo "<h3>Sites</h3>\n";
428       if( !empty( $site_info ) ) {
429         echo "<table cellpadding=3><tbody>\n";
430                 
431         foreach( $site_info as $site ) {
432           $site_name= $site['name'];
433           $site_id= $site['site_id'];
434                 
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";
436         }
437         echo "</tbody></table>\n";
438         echo "<input type=submit value='Remove Sites'>\n";
439                 
440       } else {
441         echo "<span class='plc-warning'> This user is not affiliated with a site !!</span>";
442       }
443                 
444       // diplay site select list to add another site for user
445       if( ! $peer_id && in_array( 10, $_roles ) ) {
446         // get site info
447         $full_site_info= $api->GetSites( NULL, array( "site_id", "name" ) );
448                                 
449         if( $site_info )
450           $person_site= arr_diff( $full_site_info, $site_info );
451         else
452           $person_site= $full_site_info;
453                                 
454         sort_sites( $person_site );
455                 
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";
458                 
459         foreach( $person_site as $site ) {
460           echo "<option value=". $site['site_id'] .">". $site['name'] ."</option>\n";
461                 
462         }
463                 
464         echo "</select>";
465                 
466       }
467       echo "<hr />\n";
468                 
469       // roles
470       echo "<h3>Roles</h3>\n";
471       echo "<p><table>\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";
476                 
477       // construct role array
478       for( $n=0; $n<count($roles); $n++ ) {
479         $proles[]= array( 'role_id'=>$role_ids[$n], 'name'=>$roles[$n] );
480       }
481                         
482
483       if ( !empty ($roles) ) {
484         foreach( $proles as $role ) {
485           $role_name= $role['name'];
486           $role_id= $role['role_id'];
487                                 
488           echo "<tr><td>$role_name";
489                                 
490
491           if( in_array( 10, $_roles ) ) {
492             echo "</td><td><input type=checkbox name='rem_role[]' value='$role_id'>";
493
494           }
495                                         
496           echo "</td></tr>\n";
497         }
498       } else {
499         echo "<span class='plc-warning'> This user has no known role !!</span>";
500       }
501                         
502       echo "</tbody></table>\n";
503
504       if ( in_array( 10, $_roles ) )
505         echo "<input type=submit value='Update Roles'><br />\n";
506
507
508                 
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 );
513                 
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";
516                         
517           foreach( $addable_roles as $arole ) {
518             echo "<option value=". $arole['role_id'] .">". $arole['name'] ."</option>\n";
519                         
520           }
521                         
522           echo "</select>\n";
523                 
524         }
525       }
526                 
527       echo "<hr />\n";
528                         
529       // slices
530       echo "<h3>Slices</h3>\n";
531       if( !empty( $slice_info ) ) {
532                           
533         foreach( $slice_info as $slice ) {
534           $slice_name= $slice['name'];
535           $slice_id= $slice['slice_id'];
536                 
537           echo "<a href='/db/slices/index.php?id=$slice_id'>$slice_name</a><br />\n";
538         }
539                 
540       } else {
541         echo "No slices found for that user";
542       }
543
544       if ( ! $peer_id ) {
545         echo "</form>\n";
546       } else {
547         echo "</div>\n";
548       }
549
550     }
551     echo "<br /><hr /><p><a href='/db/persons/index.php'>Back to person list</a></div>";
552
553   }
554  }
555
556
557 // Print footer
558 include 'plc_footer.php';
559
560 ?>