bugfix: the slice page was broken when nobody is in slice
[plewww.git] / planetlab / pub / feedback.php
1 <?php
2 //
3 // Provide feedback about slice(s)
4 //
5 // Mark Huang <mlhuang@cs.princeton.edu>
6 // Copyright (C) 2006 The Trustees of Princeton University
7 //
8 // $Id$
9 //
10
11 // Get API handle
12 require_once 'plc_session.php';
13 global $adm;
14
15 // Print header
16 require_once 'plc_drupal.php';
17 drupal_set_title('Slice Feedback');
18 include 'plc_header.php';
19
20 if (isset($_REQUEST['slices']) && is_array($_REQUEST['slices'])) {
21   $slice_names = $_REQUEST['slices'];
22   $selected = 'selected="selected"';
23 } else {
24   $slice_names = array();
25 }
26
27 // Return all slices and users of those slices
28 $slices = $adm->GetSlices($slice_names, array('name', 'person_ids'));
29
30 $email = "";
31 if (isset($_REQUEST['email'])) {
32   // Email Address Verification with PHP
33   // http://www.devshed.com/c/a/PHP/Email-Address-Verification-with-PHP/
34   // checks proper syntax
35   $email = $_REQUEST['email'];  
36   if (preg_match("/^[a-zA-Z0-9_\.-]+@[a-zA-Z0-9\.-]+$/", $email)) {
37     list($username, $domain) = explode('@', $email);
38     // prevent default domain from being appended
39     $domain .= ".";
40     // check if the domain has MX records, or is at least resolvable
41     if (getmxrr($domain, $mxhosts) || checkdnsrr($domain, 'ANY')) {
42       $valid_email = $email;
43     }
44   }
45 }
46
47 $comments = "";
48 if (isset($_REQUEST['comments'])) {
49   $comments = $_REQUEST['comments'];
50 }
51
52 if (isset($_REQUEST['submitted']) && isset($valid_email) && $slices) {
53   foreach ($slices as $slice) {
54     $to = array();
55
56     if (defined('PLC_MAIL_SLICE_ADDRESS')) {
57       $to[] = str_replace('SLICE', $slice['name'], PLC_MAIL_SLICE_ADDRESS);
58     } else {
59       // Get all users in the slices
60       foreach ($adm->GetPersons($slice['person_ids'], array('person_id', 'email'))
61                as $person) {
62         $to[] = $person['email'];
63       }
64     }
65
66     $to = array_unique($to);
67
68     $headers  = "Content-type: text/plain\r\n";
69     $headers .= "From: $valid_email\r\n";
70     $headers .= "Reply-To: $valid_email\r\n";
71     $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
72
73     $subject  = PLC_NAME . " Slice Feedback - " . $slice['name'];
74
75     $message  = $subject . "\r\n";
76     $message .= "\r\n";
77
78     // Print comments
79     $message .= PLC_NAME . " has received feedback from $valid_email regarding\r\n";
80     $message .= "your slice:\r\n";
81     $message .= "\r\n";
82     $message .= strip_tags($comments) . "\r\n";
83     $message .= "\r\n";
84     
85     $message .= "Please contact $valid_email at your convenience.\r\n";
86
87     // XXX API should handle this, since it handles Unicode
88     // properly. Since neither PHP4 nor PHP5 can pass Unicode via XML-RPC right
89     // now anyway, though, it doesn't really ematter.
90     if (PLC_MAIL_ENABLED) {
91       mail(implode(", ", $to), $subject, $message, $headers);
92     }
93   }
94    
95   // Pause to limit spams
96   sleep(5);
97
98   echo <<<EOF
99
100 <h1>Thank You</h1>
101
102 <p>Thank you for your feedback. The principals responsible for each
103 slice will respond to your concerns as soon as possible.</p>
104
105 EOF;
106
107   if (PLC_WWW_DEBUG) {
108     print '<pre>';
109     print "To: " . implode(", ", $to) . "\r\n";
110     print htmlspecialchars($headers);
111     print "\r\n";
112     print htmlspecialchars($message);
113     print '</pre>';
114   }
115
116 } else {
117
118   $self = $_SERVER['PHP_SELF'];
119   if (!empty($_SERVER['QUERY_STRING'])) {
120     $self .= "?" . $_SERVER['QUERY_STRING'];
121   }
122
123   echo <<<EOF
124
125 <p>Select the slices for which you would like to provide feedback.</p>
126
127 <form method="post" action="$self">
128
129 EOF;
130
131 $select_size = min(10, count($slices));
132 print "<select name=\"slices[]\" size=\"$select_size\" multiple=\"multiple\">";
133 foreach ($slices as $slice) {
134   $slice_name = htmlspecialchars($slice['name']);
135   print "<option $selected value=\"$slice_name\">$slice_name</option>";
136 }
137 print '</select>';
138
139 $email = htmlspecialchars($email);
140 $comments = htmlspecialchars($comments);
141
142 echo <<<EOF
143
144 <p>Briefly describe your concerns in the <b>Comments</b> field, provide a
145 <b>Contact E-mail</b> address, and click <b>Send</b>.</p>
146
147 <p>
148 <b>Contact E-mail</b><br />
149 <input type="text" name="email" size="40" value="$email" /><br />
150
151 <b>Comments</b><br />
152 <textarea rows="10" cols="80" name="comments">$comments</textarea><br />
153
154 <input type="submit" name="submitted" value="Send" />
155
156 </form>
157
158 EOF;
159
160 }
161
162 include 'plc_footer.php';