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