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