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