brute-force changed access to $_GET['key'] to use get_array instead
[plewww.git] / planetlab / sites / join_request.php
1 <?php
2 // $Id$
3 //
4 // page for administration of pending site registration requests
5 //
6
7 // Require login
8 require_once 'plc_login.php';
9
10 // Get session and API handles
11 require_once 'plc_session.php';
12 require_once 'plc_api.php';
13 global $plc, $api, $adm;
14
15 // Print header
16 require_once 'plc_drupal.php';
17 include 'plc_header.php';
18
19 // Common functions
20 require_once 'plc_functions.php';
21 require_once 'details.php';
22 require_once 'nifty.php';
23 require_once 'table.php';
24 require_once 'nifty.php';
25
26 include 'site_form.php';
27
28 ////////////////////////////////////////
29 function render_all_join_requests($api) {
30   global $PENDING_CONSORTIUM_ID;
31   $filter = array("peer_id" => NULL, "ext_consortium_id" => $PENDING_CONSORTIUM_ID);
32   $columns=array('site_id','name','enabled','date_created');
33   $sites = $api->GetSites( $filter, $columns);
34   if (empty($sites)) {
35     print("<p> No open join requests </p>");
36     return;
37   }
38   $headers = array();
39   $headers['id']='int';
40   $headers['Site Name']='string';
41   # trying the sortEnglishDateTime stuff - not too lucky so far
42   # http://www.frequency-decoder.com/demo/table-sort-revisited/custom-sort-functions/
43   $headers['submitted']='sortEnglishDateTime';
44   $headers['enabled']='string';
45
46   $nifty=new PlekitNifty ('pending','sites-pending','medium');
47   $nifty->start();
48   $table=new PlekitTable ('pending',$headers,2,
49                           array('pagesize_area'=>FALSE,'pagesize'=>10000));
50   $table->start();
51   foreach($sites as $site) {
52     $site_id=$site['site_id'];
53     $link = $site['enabled'] ? l_site($site_id) : l_site_review_pending($site_id);
54     $table->row_start();
55     $table->cell(href($link,$site['site_id']));
56     $table->cell(href($link,$site['name']));
57     $table->cell(date("dS F Y",$site['date_created']) . " at " . date("G:i",$site['date_created']));
58     $table->cell( $site['enabled'] ? plc_warning_html('yes') : 'no');
59     $table->row_end();
60   }
61   $table->end();
62   $nifty->end();
63 }
64
65 function render_join_request_review($api, $site_id) {
66   $sites = $api->GetSites( array(intval($site_id)) );
67   if (empty($sites)) {
68       print("<p class='plc-warning'> Invalid request with site_id=$site_id</p> ");
69       return;
70   }
71   $site = $sites[0];
72   if ($site['enabled'] && $site['ext_consortium_id'] === $PENDING_CONSORTIUM_ID) {
73     print("<p class='plc-warning'> This site is already enabled </p>");
74     return;
75   }
76   $addresses = $api->GetAddresses ($site['address_ids']);
77   if (empty ($addresses)) {
78     drupal_set_message("Site $site_id has no address - never mind");
79     $address=array('line1'=>'');
80     $address_id=0;
81   } else {
82     $address = $addresses[0];
83     $address_id=$address['address_id'];
84   }
85 # just in case there is no person attached yet
86   if (empty ($site['person_ids'])) {
87     $persons=array();
88   } else {
89     $person_ids = $site['person_ids'];
90     $persons = $api->GetPersons( $person_ids, array( "person_id", "role_ids", "first_name", "last_name", "title", "email" , "phone") );
91   }
92   $tech = Null;
93   $pi = Null;
94   foreach($persons as $person) {
95     if ( in_array('20',  $person['role_ids']) ) {
96       $pi = $person;
97     }
98     if ( in_array('40',  $person['role_ids']) ) {
99       $tech = $person;
100     }
101   }
102   $pi_id = $pi['person_id'];
103   $tech_id = $tech['person_id'];
104
105   print <<< EOF
106 <p> Please review the join request below.</p>
107     <p> <b> Warning:</b> the PI email address that was provided in this form will <b> not be checked</b> automatically. We expect that as part of the handshake with the site, the support team has had an opportunity to use this address so it can be considered safe. Please check it manually if this is not the case.</p>
108 <form name="join_request" method="post" action="/db/sites/join_request.php">
109 <input type="hidden" name="pi_id" value="$pi_id">
110 <input type="hidden" name="address_id" value="$address_id">
111 <input type="hidden" name="tech_id" value="$tech_id">
112 <input type="hidden" name="site_id" value="$site_id">
113 EOF;
114
115   // don't render the tech part if that was the same as the pi
116   $site_form = build_site_form(FALSE);
117   $input = array ('site' => $site, 'address'=> $address, 'pi' => $pi, 'tech' => $tech);
118
119   $nifty=new PlekitNifty ('pending','site-pending','medium');
120
121   $nifty->start();
122   $details = new PlekitDetails(TRUE);
123   $details->start();
124
125   // display the buttons
126   $buttons_row =<<<EOF
127     <table width="100%" border=0 cellspacing="0" cellpadding="5"> <tr>
128     <td align=center><input type="submit" name="submitted" value="Delete"></td>
129     <td align=center><input type="submit" name="submitted" value="Update"></td>
130     <td align=center><input type="submit" name="submitted" value="Approve"></td>
131     </tr> </table>
132 EOF;
133
134   $details->tr($buttons_row,'center');
135   // render the form - not supposed to be empty
136   form_render_details ($details,$site_form, $input, TRUE);
137
138   // display the buttons again
139   $details->tr($buttons_row,'center');
140
141   $details->end();
142   $nifty->end();
143
144 }
145
146 function notify_enabled_pi ($api, $pi_id, $pi, $site_id, $site) {
147   // notify the PI
148   $template= <<<EOF
149 You have filed a site registration with the %s platform.
150
151 This registration has been approved, and your account was enabled
152 You were granted a PI role, meaning that you will be responsible
153 for managing personal accounts and slices for your site
154
155 You can now enter the system at
156 https://%s:%d/
157 with %s as a login,
158 and the password that you provided at registration-time
159
160 You can directly access your site information at
161 https://%s:%d/db/sites/index.php?id=%d
162
163 Please start with adding nodes for this site with
164 https://%s:%d/db/nodes/node_add.php
165
166 Our support team will be glad to answer any question that you might have
167 They are reachable at mailto:%s
168 EOF;
169
170  $body=sprintf($template,
171                PLC_NAME,
172                PLC_WWW_HOST,PLC_WWW_SSL_PORT,
173                $pi['email'],
174                PLC_WWW_HOST,PLC_WWW_SSL_PORT,$site_id,
175                PLC_WWW_HOST,PLC_WWW_SSL_PORT,
176                PLC_MAIL_SUPPORT_ADDRESS);
177
178  $subject="Site registration for " . $site['name'] . " has been approved by " . PLC_NAME;
179  $adm->NotifyPersons(array($pi_id),$subject,$body);
180 }
181
182
183 // find person roles
184 $_person= $plc->person;
185 $_roles= $_person['role_ids'];
186
187 // only admins are allowed to view this page
188 if( !in_array( '10', $_roles ) ) {
189     print("<p> not allowed to view this page </p>");
190 } else if (get_array($_GET, 'review')) {
191     //print review page
192     drupal_set_title('Join Request - Review');
193     render_join_request_review($api, get_array($_GET, 'site_id'));
194 } else if (get_array($_POST, 'submitted')) {
195   // parse the form
196   $site_form = build_site_form(FALSE);
197   $input = parse_form ($site_form, $_REQUEST, $input);
198   // xxx should not happen ?
199   $empty_form = $input['is_empty'];
200   $error = "";
201   $messages= array();
202   if ( $empty_form ) {
203     $error .= '<div class-"plc-warning">Internal error - empty form !?!</div>';
204   }
205   if (empty ($error)) {
206     $site=$input['site'];
207     $address=$input['address'];
208     $pi=$input['pi'];
209     $tech=$input['tech'];
210
211     // Look for missing/blank entries
212     $error .= form_check_required ($site_form, $input);
213   }
214
215   if (empty($error)) {
216     // get objects id from the request
217     $site_id = intval(trim($_POST['site_id']));
218     $address_id = intval(trim($_POST['address_id']));
219     $pi_id = intval(trim($_POST['pi_id']));
220     $tech_id = intval(trim($_POST['tech_id']));
221
222     switch ($_POST['submitted']) {
223     case 'Delete': {
224       $api->DeleteSite ($site_id);
225       $api_error=$api->error();
226       if (!empty($api_error)) {
227         $error .= $api->error();
228       } else {
229         $messages [] = "Site " . $site['name'] . " deleted";
230       }
231       break;
232     }
233     case 'Update': {
234       $api->begin();
235       $api->UpdateSite($site_id,$site);
236       if ($address_id) $api->UpdateAddress($address_id,$address);
237       else if(!empty($address)) $api->AddSiteAddress($site_id,$address);
238       $api->UpdatePerson($pi_id,$pi);
239       if ($tech_id != $pi_id) $api->UpdatePerson($tech_id,$tech);
240       $api->commit();
241       $api_error=$api->error();
242       if (!empty($api_error)) {
243         $error .= $api->error();
244       } else {
245         $messages [] = "Join request updated for site " . $site['name'] ;
246       }
247
248       break;
249     }
250     case 'Approve': {
251       // Thierry - august 22 2007
252       // keep it simple - the admin who approves is now supposed to check
253       // the PI's email address, which makes the whole thing *a lot* simpler
254       // enable the site, enable the PI, and VerifyPerson the thec if different from the PI
255       $site['enabled'] = True;
256       global $APPROVED_CONSORTIUM_ID;
257       $site['ext_consortium_id'] = $APPROVED_CONSORTIUM_ID;
258       $api->UpdateSite ($site_id,$site);
259       $api_error=$api->error();
260       if (!empty($api_error)) {
261         $error .= $api->error();
262         $messages [] = "Could not enable site";
263       } else {
264         $messages[] = l_site_t ($site_id,"Site '" . $site['name'] . "' enabled");
265       }
266
267       if (empty ($error) && $address_id) {
268         // Update Address
269         $api->UpdateAddress($address_id,$address);
270         $api_error=$api->error();
271         if ( ! empty($api_error)) {
272           $error .= $api->error();
273           $messages [] = "Could not update address";
274         }
275
276         foreach ( array("Billing","Shipping") as $address_type) {
277           $api->AddAddressTypeToAddress($address_type,$address_id);
278           $api_error=$api->error();
279           if ( ! empty($api_error)) {
280             $error .= $api->error();
281             $messages [] = "Could not add address type " . $address_type;
282           }
283         }
284
285         // Update pi, and enable him
286         $api->UpdatePerson ($pi_id,$pi);
287         if ( $pi ['enabled' ] ) {
288           $messages [] = "PI already enabled";
289         } else {
290           $api->UpdatePerson ($pi_id,array("enabled"=>True));
291           $api_error=$api->error();
292           if (empty($api_error)) {
293             $messages[] = "Enabled PI as " . $pi['email'] ;
294             notify_enabled_pi ($api, $pi_id,$pi,$site_id, $site);
295             $messages[] = "Notified PI by email";
296           } else {
297             $error .= $api->error();
298             $messages [] = "Could not update PI";
299           }
300         }
301
302         if ($pi_id != $tech_id) {
303           // Update tech, and VerifyPerson him if needed
304           $api->UpdatePerson ($tech_id,$tech);
305           if ( $tech [ 'enabled' ] ) {
306             $messages [] = "Tech already enabled";
307           } else {
308             $api->VerifyPerson($tech_id);
309             $api_error=$api->error();
310             if (empty($api_error)) {
311               $messages[] = "Created account registration for Tech as " . $tech['email'];
312             } else {
313               $error .= $api->error();
314               $messages [] = "Could not verify Tech";
315             }
316           }
317         }
318       }
319
320       break;
321     }
322     default: {
323       $error .= '<div class-"plc-warning">Internal error - unexpected request</div>';
324       break;
325     }
326
327     } // end switch
328   }
329
330   // Show messages
331   if (!empty($messages)) {
332     print '<div class="messages status"><ul>';
333     foreach ($messages as $line) {
334       print "<li> $line";
335     }
336     print "</ul></div>";
337   }
338
339   // Show errors if any
340   if (!empty($error)) {
341     print '<div class="messages error">' . $error . '</div>';
342     drupal_set_title('Join Request - Review');
343     render_join_request_review($api, $_POST['site_id']);
344   } else {
345     drupal_set_title('All currently pending join requests');
346     render_all_join_requests($api);
347   }
348
349  }
350  else // list all pending requests
351 {
352
353     drupal_set_title('All currently pending join requests');
354     render_all_join_requests($api);
355 }
356
357 // Print footer
358 include 'plc_footer.php';
359
360 ?>