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