ef332bd4269cd7ec99e177898709ed26a7e20c72
[plewww.git] / planetlab / sites / register.php
1 <?php
2 //
3 // Site registration and verification form.
4 //
5 // Thierry Parmentelat -- INRIA
6 // based on persons/register.php
7 //
8
9 // Get session and API handles
10 require_once 'plc_session.php';
11 global $plc, $api, $adm;
12
13 // Print header
14 require_once 'plc_drupal.php';
15 drupal_set_title('New Site Registration');
16 include 'plc_header.php';
17
18 require_once 'site_form.php';
19 require_once 'details.php';
20 require_once 'nifty.php';
21
22 $verbose = FALSE;
23
24 // initialize objects
25 $objectnames = array ('site','pi','tech','address');
26
27 $site_form = build_site_form(TRUE);
28 $input = parse_form ($site_form, $_REQUEST, $input);
29 $empty_form = $input['is_empty'];
30
31 $site=$input['site'];
32 $pi=$input['pi'];
33 $tech=$input['tech'];
34 $address=$input['address'];
35
36 # allow address to be left out
37 function non_empty_address ($address) {
38   return !empty($address['line1']);
39 }
40
41 if (! $empty_form ) {
42   // Look for missing/blank entries
43   $error = form_check_required ($site_form, $input);
44   $messages= array();
45   $verboses= array();
46
47   if (empty($error)) {
48
49     // I considered using begin/commit
50     // but first it was incorrectly reporting errors - which I fixed
51     // second: you need site_id to perform AddSiteAddress
52     // but you cannot write
53     // begin()
54     // site_id=AddSite()
55     // AddSiteAddress(site_id,..)
56     // because site_id would not be evaluated at the time you use it
57     //
58     // and you cannot either write
59     // begin
60     // AddSite(..)
61     // AddSiteAddress('login_base',...)
62     // because if the first call fails because login_base is already taken,
63     // then you're going to add the new address to the wrong site
64     //
65     // the bottom line is, there's no advantage in using begin/commit at all
66
67     // creating the site
68     $site['enabled']=FALSE;
69     global $PENDING_CONSORTIUM_ID;
70     $site['ext_consortium_id']=$PENDING_CONSORTIUM_ID;
71     $site_id=$adm->AddSite($site);
72     $api_error .= $adm->error();
73     if (empty($api_error)) {
74       $verboses [] = "Site created as disabled, with ext_consortium_id=".$PENDING_CONSORTIUM_ID;
75     } else {
76       $error .= $api_error;
77     }
78   }
79
80   if (empty($error)) {
81
82     // Thierry on august 22 2007
83     // use NotifySupport to send this message, and get rid of the fake account
84
85     $subject = "Site registration form : new site " . $site['name'];
86     // should use GetMessages and stuff, but I do not trust the way
87     // templates are created in db-config, for upgrades notably
88     $template = <<< EOF
89 We received a site registration form for site name %s
90
91 To review, please visit https://%s:%d/db/sites/join_request.php?review=t&site_id=%d.
92 EOF;
93     $body=sprintf($template,$site['name'],PLC_WWW_HOST,PLC_WWW_SSL_PORT,$site_id);
94     $adm->NotifySupport($subject,$body);
95
96     $messages [] = "Your registration request has been received.";
97     $messages [] = "A mail was sent to the operations team, your application should be processed shortly.";
98     $messages [] = "Upon approval, the PI will receive an information e-mail";
99     $messages [] = "Please send a message to " . PLC_MAIL_SUPPORT_ADDRESS . " if this request is not instructed within a few days.";
100
101   // creating address
102     if (non_empty_address($address)) {
103       $adm->AddSiteAddress($site_id,$address);
104       $api_error = $adm->error();
105       if (empty($api_error)) {
106               $verboses [] = "Address created";
107       } else {
108               $error .= $api_error;
109       }
110     }
111
112     // creating PI
113     // Thierry 23 august 2007
114     // avoid using a pre-existing federated account
115     $known_pi = $adm->GetPersons(array("email"=>$pi['email'],
116                                                                "peer_id"=>NULL),
117                                  array("person_id"));
118     if ($known_pi) {
119       $messages [] = " Note: PI was already known";
120       $pi_id=$known_pi[0]['person_id'];
121     } else {
122       $pi['enabled']=FALSE;
123       $pi_id=$adm->AddPerson($pi);
124       $api_error = $adm->error();
125       if (empty($api_error)) {
126               $verboses [] = "PI created as disabled</p>";
127       } else {
128               $error .= $api_error;
129       }
130     }
131     if ($adm->AddPersonToSite($pi_id,$site_id)) {
132       $verboses [] = "PI attached to new site";
133     }
134     if ($adm->AddRoleToPerson('pi',$pi_id)) {
135       $verboses [] = $pi['email'] . " granted PI role</p>";
136     }
137
138     if ($pi['email'] == $tech['email']) {
139       // need to assign tech role so the registration request gets filled properly
140       if ($adm->AddRoleToPerson('tech',$pi_id)) {
141               $verboses [] = $pi['email'] . " granted Tech role</p>";
142       }
143     } else {
144       // creating TECH
145       $known_tech = $adm->GetPersons(array("email"=>$tech['email'],
146                                                                    "peer_id"=>NULL),
147                                      array("person_id"));
148       // jan-2013 with improvements in plcapi-5.1-6 about managing persons and tags,
149       // AddPerson has gone more picky and we need to remove some fields
150       // that no longer are silently ignored by AddPerson
151       $user_role_required=$tech['user-role'];
152       unset($tech['user-role']);
153       if ($known_tech) {
154               $messages [] = " Note: Tech was already known";
155               $tech_id=$known_tech[0]['person_id'];
156       } else {
157               $tech['enabled']=FALSE;
158         $tech_id=$adm->AddPerson($tech);
159         $api_error = $adm->error();
160         if (empty($api_error)) {
161           $verboses [] = "Tech created as disabled</p>";
162         } else {
163           $error .= $api_error;
164         }
165       }
166       if ($adm->AddPersonToSite($tech_id,$site_id)) {
167         $verboses [] = "Tech attached to new site";
168       }
169       if ($adm->AddRoleToPerson('tech',$tech_id)) {
170         $verboses [] = $tech['email'] . " granted Tech role";
171       }
172       if ( $user_role_required && $adm->AddRoleToPerson('user',$tech_id) ) {
173         $verboses [] = $tech['email'] . " granted User role";
174       }
175     }
176   }
177
178
179   // Show messages
180   if (!empty($messages)) {
181     print '<div class="messages status"><ul>';
182     foreach ($messages as $line) {
183       print "<li> $line";
184     }
185     print "</ul></div>";
186   }
187
188   if ($verbose && !empty($verboses)) {
189     print '<div class="messages status"><ul>';
190     print "<p> Verbose messages</p>";
191     foreach ($verboses as $line) {
192       print "<li> $line";
193     }
194     print "</ul></div>";
195   }
196
197   if (!empty($error)) {
198     print '<div class="messages error">' . $error . '</div>';
199   } else {
200     // to prevent resubmit
201     $site['site_id']="XXX";
202   }
203 }
204
205 $self = $_SERVER['PHP_SELF'];
206 if (!empty($_SERVER['QUERY_STRING'])) {
207   $self .= "?" . $_SERVER['QUERY_STRING'];
208 }
209
210 print <<<EOF
211 <div class="content">
212
213 <form action="$self" method="post">
214
215 EOF;
216
217 $nifty=new PlekitNifty ('register','site-register','medium');
218 $nifty->start();
219 $details = new PlekitDetails(TRUE);
220 $details -> start();
221
222 $register_button='<input type="submit" name="op" value="Register"  class="form-submit" />';
223
224 // Do not allow resubmits
225 if (empty($site['site_id'])) {
226   $details->tr($register_button,'center');
227 }
228
229 form_render_details ($details, $site_form, $input, ! $empty_form);
230
231 // Do not allow resubmits
232 if (empty($site['site_id'])) {
233   // some space
234   $details->space();
235   $details->tr($register_button,'center');
236 }
237
238 $details -> end();
239 $nifty->end();
240 print "</form></div>";
241
242 include 'plc_footer.php';
243
244 ?>