newmmlist.php3 12.5 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2012 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15
# All rights reserved.
#
include("defs.php3");

#
# No Standard Testbed Header; going to spit out a redirect later.
#

#
# Only known and logged in users.
#
16 17
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
18
$dbid      = $this_user->dbid();
19
$isadmin   = ISADMIN();
20

21 22 23 24 25 26
#
# Verify page arguments.
#
$optargs = OptionalPageArguments("submit",       PAGEARG_STRING,
				 "formfields",   PAGEARG_ARRAY);

27 28 29
#
# See what projects the uid can do this in.
#
30
$projlist = $this_user->ProjectAccessList($TB_PROJECT_READINFO);
31 32 33 34 35 36

if (! count($projlist)) {
    USERERROR("You do not appear to be a member of any Projects in which ".
	      "you have permission to create new mailing lists", 1);
}

37 38 39 40 41
#
# Spit the form out using the array of data. 
# 
function SPITFORM($formfields, $errors)
{
42
    global $TBDB_MMLENGTH, $projlist, $OURDOMAIN;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

    PAGEHEADER("Create a new MailMan list");
    
    if ($errors) {
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
              <tr>
                 <th align=center colspan=2>
                   <font size=+1 color=red>
                      &nbsp;Oops, please fix the following errors!&nbsp;
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
58 59
            # XSS prevention.
	    $message = CleanString($message);
60 61 62 63 64 65 66 67 68
	    echo "<tr>
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
                  </tr>\n";
	}
	echo "</table><br>\n";
    }
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    else {
	echo "<blockquote><blockquote>
              <center>
               <font size=+1>
                 Host your own project related mailing lists at $OURDOMAIN
               </font>
              </center><br>
              Use the form below to create a new mailing list. You will
              become the administrator for the new list, and are responsible
              for the list configuration and management, including user
              subscriptions, approval, etc. <b>Note that mailing lists
              should be related to your project in some manner; please,
              no lists discussing the latest episode of your favorite TV
              show.</b>
	      </blockquote></blockquote>\n";
    }

    echo "<SCRIPT LANGUAGE=JavaScript>
              function Changed(theform) 
              {
                  var pidx   = theform['formfields[pid]'].selectedIndex;
                  var pid    = theform['formfields[pid]'].options[pidx].value;
                  var list   = theform['formfields[listname]'].value;

                  if (pid == '') {
                      theform['formfields[fullname]'].value = '';
                  }
                  else if (list == '') {
                      theform['formfields[fullname]'].value = pid + '-';
                  }
                  else {
                      theform['formfields[fullname]'].value =
                        pid + '-' + theform['formfields[listname]'].value +
                        '@' + '$OURDOMAIN';
                  }
              }
          </SCRIPT>\n";
106

107 108 109 110
    # XSS prevention.
    while (list ($key, $val) = each ($formfields)) {
	$formfields[$key] = CleanString($val);
    }
111 112 113 114 115 116 117
    echo "<br>
          <table align=center border=1> 
          <tr>
             <td align=center colspan=2>
                 <em>(Fields marked with * are required)</em>
             </td>
          </tr>
118 119 120 121 122 123 124 125 126 127 128 129 130 131
          <form action='newmmlist.php3' method=post name=myform>\n";

    #
    # Select Project
    #
    echo "<tr>
              <td>*Select Project:</td>
              <td><select name=\"formfields[pid]\"
                          onChange='Changed(myform);'>
                      <option value=''>Please Select &nbsp</option>\n";
    
    while (list($project) = each($projlist)) {
	$selected = "";

132
	if ($formfields["pid"] == $project)
133 134 135 136 137 138 139
	    $selected = "selected";
	
	echo "        <option $selected value='$project'>$project </option>\n";
    }
    echo "       </select>";
    echo "    </td>
          </tr>\n";
140 141 142 143 144 145 146 147

    #
    # Select List Name
    #
    echo "<tr>
              <td>*List Name (no blanks):</td>
              <td class=left>
                  <input type=text
148
                         onChange='Changed(myform);'
149
                         name=\"formfields[listname]\"
150
                         value=\"" . $formfields["listname"] . "\"
151 152 153 154 155
	                 size=$TBDB_MMLENGTH
                         maxlength=$TBDB_MMLENGTH>
              </td>
          </tr>\n";

156 157 158 159 160 161 162 163 164
    #
    # This is auto filled in by the javascript above.
    # 
    echo "<tr>
              <td>EMail Address (will be):</td>
              <td class=left>
                  <input type=text
                         readonly 
                         name=\"formfields[fullname]\"
165
                         value=\"" . $formfields["fullname"] . "\"
166 167 168 169 170
	                 size=$TBDB_MMLENGTH
                         maxlength=$TBDB_MMLENGTH>
              </td>
          </tr>\n";

171 172 173 174 175 176 177 178 179
    #
    # Password. Note that we do not resend the password. User
    # must retype on error.
    #
    echo "<tr>
              <td colspan>*Admin Password:</td>
              <td class=left>
                  <input type=password
                         name=\"formfields[password1]\"
180
                         value=\"" . $formfields["password1"] . "\"
181 182 183 184 185 186 187 188
                         size=8></td>
          </tr>\n";

    echo "<tr>
              <td colspan>*Retype Password:</td>
              <td class=left>
                  <input type=password
                         name=\"formfields[password2]\"
189
                         value=\"" . $formfields["password2"] . "\"
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
                         size=8></td>
         </tr>\n";

    echo "<tr>
              <td align=center colspan=2>
                  <b><input type=submit name=submit value=Submit></b>
              </td>
          </tr>\n";

    echo "</form>
          </table>\n";

    echo "<br>
          <blockquote><blockquote>
          After you click submit, the mailing list will be created on our
          server, and you will be automatically redirected to the list
          configuration page. Feel free to edit the configuration as you like.
207 208 209 210 211 212 213 214 215 216
          <br><br>
          Emulab mailing lists are maintained using the open source
          <a href=http://www.gnu.org/software/mailman/index.html>Mailman</a>
          package. You can find documentation for
          <a href=http://www.gnu.org/software/mailman/users.html>Users</a>
          and documentation for 
          <a href=http://www.gnu.org/software/mailman/admins.html>
          List Managers</a> on the Mailman
          <a href=http://www.gnu.org/software/mailman/docs.html>
          documentation</a> page.
217 218 219 220 221 222
          </blockquote></blockquote>\n";
}

#
# On first load, display a virgin form and exit.
#
223
if (!isset($submit)) {
224
    $defaults = array();
225 226 227 228 229
    $defaults["pid"]  = "";
    $defaults["password1"]   = "";
    $defaults["password2"]   = "";
    $defaults["listname"]    = "";
    $defaults["fullname"]    = "";
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

    #
    # Allow formfields that are already set to override defaults
    #
    if (isset($formfields)) {
	while (list ($field, $value) = each ($formfields)) {
	    $defaults[$field] = $formfields[$field];
	}
    }
    
    SPITFORM($defaults, 0);
    PAGEFOOTER();
    return;
}

#
# Otherwise, must validate and redisplay if errors
#
248 249
$errors  = array();
$project = null;
250 251

#
252
# Project:
253 254 255
#
if (!isset($formfields["pid"]) ||
    strcmp($formfields["pid"], "") == 0) {
256 257
    $errors["Project"] = "Not Selected";
}
258 259
elseif (!TBvalid_pid($formfields["pid"])) {
    $errors["Project"] = "Invalid project name";
260
}
261 262
elseif (! ($project = Project::Lookup($formfields["pid"]))) {
    $errors["Project"] = "Invalid project name";
263
}
264
elseif (! $project->AccessCheck($this_user, $TB_PROJECT_READINFO)) {
265
    $errors["Project"] = "Not enough permission";
266
}
267 268 269 270 271 272 273

#
# List Name, but only if pid was okay.
#
if ($project) {
    if (!isset($formfields["listname"]) ||
	strcmp($formfields["listname"], "") == 0) {
274
	$errors["List Name"] = "Missing Field";
275 276
    }
    else {
277
	$listname = $project->pid() . "-" . $formfields["listname"];
278
	
279 280 281 282 283 284 285 286 287 288 289 290 291
	if (! TBvalid_mailman_listname($listname)) {
	    $errors["List Name"] =
		"Must be alphanumeric and must begin with an alphanumeric";
	}
	elseif (strlen($listname) > $TBDB_MMLENGTH) {
	    $errors["List Name"] =
		"Too long! ".
		"Must be less than or equal to $TBDB_MMLENGTH";
	}
	else {
            #
            # Before we proceed, lets see if the list already exists.
            #
292 293
	    $safe_name = addslashes($listname);
	    
294 295
	    $query_result =
		DBQueryFatal("select * from mailman_listnames ".
296
			     "where listname='$safe_name'");
297
	
298 299 300
	    if (mysql_num_rows($query_result)) {
		$errors["List Name"] = "Name already in use; pick another";
	    }
301 302 303 304 305 306 307
	}
    }
}

#
# Password
#
308 309
if (!isset($formfields["password1"]) ||
    strcmp($formfields["password1"], "") == 0) {
310 311
    $errors["Password"] = "Missing Field";
}
312 313
if (!isset($formfields["password2"]) ||
    strcmp($formfields["password2"], "") == 0) {
314 315
    $errors["Confirm Password"] = "Missing Field";
}
316
elseif (strcmp($formfields["password1"], $formfields["password2"])) {
317 318
    $errors["Confirm Password"] = "Does not match Password";
}
319
elseif (! TBvalid_userdata($formfields["password1"])) {
320 321 322 323 324 325 326 327 328 329 330 331 332 333
    $errors["Password"] = "Invalid Characters";
}

#
# If any errors, respit the form with the current values and the
# error messages displayed. Iterate until happy.
# 
if (count($errors)) {
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
}

#
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
# Build up argument array to pass along.
#
$args = array();

if (isset($formfields["password1"]) && $formfields["password1"] != "") {
    $args["password1"]	= $formfields["password1"];
}
if (isset($formfields["password2"]) && $formfields["password2"] != "") {
    $args["password2"]	= $formfields["password2"];
}
if (isset($formfields["listname"]) && $formfields["listname"] != "") {
    $args["listname"]	= $formfields["listname"];
}
if (isset($formfields["fullname"]) && $formfields["fullname"] != "") {
    $args["fullname"]	= $formfields["fullname"];
}

351
if (! ($result = NewMmList($uid, $project, $args, $errors))) {
352 353
    # Always respit the form so that the form fields are not lost.
    # I just hate it when that happens so lets not be guilty of it ourselves.
354 355 356 357 358 359
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
}

#
360
# Okay, redirect the user over to the listadmin page to finish configuring.
361
#
362
header("Location: ${MAILMANURL}/admin/${listname}/?adminpw=${formfields["password1"]}");
363 364

#
365
# When there's an MmList class, this will be a Class function to make a new one...
366
#
367
function NewMmList($uid, $project, $args, &$errors) {
368 369 370 371 372 373 374 375
    global $suexec_output, $suexec_output_array, $TBADMINGROUP;

    #
    # Generate a temporary file and write in the XML goo.
    #
    $xmlname = tempnam("/tmp", "newmmlist");
    if (! $xmlname) {
	TBERROR("Could not create temporary filename", 0);
376
	$errors[] = "Transient error(1); please try again later.";
377 378 379 380
	return null;
    }
    if (! ($fp = fopen($xmlname, "w"))) {
	TBERROR("Could not open temp file $xmlname", 0);
381
	$errors[] = "Transient error(2); please try again later.";
382 383 384
	return null;
    }

385 386 387
    # Add these. Maybe caller should do this?
    $args["project"]  = $project->pid_idx();

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    fwrite($fp, "<MmList>\n");
    foreach ($args as $name => $value) {
	fwrite($fp, "<attribute name=\"$name\">");
	fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	fwrite($fp, "</attribute>\n");
    }
    fwrite($fp, "</MmList>\n");
    fclose($fp);
    chmod($xmlname, 0666);

    $retval = SUEXEC($uid, $TBADMINGROUP, "webnewmmlist $xmlname",
		     SUEXEC_ACTION_IGNORE);

    if ($retval) {
	if ($retval < 0) {
403
	    $errors[] = "Transient error(3, $retval); please try again later.";
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	}
	else {
	    # unlink($xmlname);
	    if (count($suexec_output_array)) {
		for ($i = 0; $i < count($suexec_output_array); $i++) {
		    $line = $suexec_output_array[$i];
		    if (preg_match("/^([-\w]+):\s*(.*)$/",
				   $line, $matches)) {
			$errors[$matches[1]] = $matches[2];
		    }
		    else
			$errors[] = $line;
		}
	    }
	    else
420
		$errors[] = "Transient error(4, $retval); please try again later.";
421 422 423 424 425 426 427 428 429 430 431 432 433
	}
	return null;
    }

    # There are no return value(s) to parse at the end of the output.

    # Unlink this here, so that the file is left behind in case of error.
    # We can then create the MmList by hand from the xmlfile, if desired.
    unlink($xmlname);

    return true; 
}

434
?>