newmmlist.php3 13.2 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2012 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32
#
include("defs.php3");

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

#
# Only known and logged in users.
#
33 34
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
35
$dbid      = $this_user->dbid();
36
$isadmin   = ISADMIN();
37

38 39 40 41 42 43
#
# Verify page arguments.
#
$optargs = OptionalPageArguments("submit",       PAGEARG_STRING,
				 "formfields",   PAGEARG_ARRAY);

44 45 46
#
# See what projects the uid can do this in.
#
47
$projlist = $this_user->ProjectAccessList($TB_PROJECT_READINFO);
48 49 50 51 52 53

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);
}

54 55 56 57 58
#
# Spit the form out using the array of data. 
# 
function SPITFORM($formfields, $errors)
{
59
    global $TBDB_MMLENGTH, $projlist, $OURDOMAIN;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    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)) {
75 76
            # XSS prevention.
	    $message = CleanString($message);
77 78 79 80 81 82 83 84 85
	    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";
    }
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    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";
123

124 125 126 127
    # XSS prevention.
    while (list ($key, $val) = each ($formfields)) {
	$formfields[$key] = CleanString($val);
    }
128 129 130 131 132 133 134
    echo "<br>
          <table align=center border=1> 
          <tr>
             <td align=center colspan=2>
                 <em>(Fields marked with * are required)</em>
             </td>
          </tr>
135 136 137 138 139 140 141 142 143 144 145 146 147 148
          <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 = "";

149
	if ($formfields["pid"] == $project)
150 151 152 153 154 155 156
	    $selected = "selected";
	
	echo "        <option $selected value='$project'>$project </option>\n";
    }
    echo "       </select>";
    echo "    </td>
          </tr>\n";
157 158 159 160 161 162 163 164

    #
    # Select List Name
    #
    echo "<tr>
              <td>*List Name (no blanks):</td>
              <td class=left>
                  <input type=text
165
                         onChange='Changed(myform);'
166
                         name=\"formfields[listname]\"
167
                         value=\"" . $formfields["listname"] . "\"
168 169 170 171 172
	                 size=$TBDB_MMLENGTH
                         maxlength=$TBDB_MMLENGTH>
              </td>
          </tr>\n";

173 174 175 176 177 178 179 180 181
    #
    # 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]\"
182
                         value=\"" . $formfields["fullname"] . "\"
183 184 185 186 187
	                 size=$TBDB_MMLENGTH
                         maxlength=$TBDB_MMLENGTH>
              </td>
          </tr>\n";

188 189 190 191 192 193 194 195 196
    #
    # 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]\"
197
                         value=\"" . $formfields["password1"] . "\"
198 199 200 201 202 203 204 205
                         size=8></td>
          </tr>\n";

    echo "<tr>
              <td colspan>*Retype Password:</td>
              <td class=left>
                  <input type=password
                         name=\"formfields[password2]\"
206
                         value=\"" . $formfields["password2"] . "\"
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
                         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.
224 225 226 227 228 229 230 231 232 233
          <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.
234 235 236 237 238 239
          </blockquote></blockquote>\n";
}

#
# On first load, display a virgin form and exit.
#
240
if (!isset($submit)) {
241
    $defaults = array();
242 243 244 245 246
    $defaults["pid"]  = "";
    $defaults["password1"]   = "";
    $defaults["password2"]   = "";
    $defaults["listname"]    = "";
    $defaults["fullname"]    = "";
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

    #
    # 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
#
265 266
$errors  = array();
$project = null;
267 268

#
269
# Project:
270 271 272
#
if (!isset($formfields["pid"]) ||
    strcmp($formfields["pid"], "") == 0) {
273 274
    $errors["Project"] = "Not Selected";
}
275 276
elseif (!TBvalid_pid($formfields["pid"])) {
    $errors["Project"] = "Invalid project name";
277
}
278 279
elseif (! ($project = Project::Lookup($formfields["pid"]))) {
    $errors["Project"] = "Invalid project name";
280
}
281
elseif (! $project->AccessCheck($this_user, $TB_PROJECT_READINFO)) {
282
    $errors["Project"] = "Not enough permission";
283
}
284 285 286 287 288 289 290

#
# List Name, but only if pid was okay.
#
if ($project) {
    if (!isset($formfields["listname"]) ||
	strcmp($formfields["listname"], "") == 0) {
291
	$errors["List Name"] = "Missing Field";
292 293
    }
    else {
294
	$listname = $project->pid() . "-" . $formfields["listname"];
295
	
296 297 298 299 300 301 302 303 304 305 306 307 308
	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.
            #
309 310
	    $safe_name = addslashes($listname);
	    
311 312
	    $query_result =
		DBQueryFatal("select * from mailman_listnames ".
313
			     "where listname='$safe_name'");
314
	
315 316 317
	    if (mysql_num_rows($query_result)) {
		$errors["List Name"] = "Name already in use; pick another";
	    }
318 319 320 321 322 323 324
	}
    }
}

#
# Password
#
325 326
if (!isset($formfields["password1"]) ||
    strcmp($formfields["password1"], "") == 0) {
327 328
    $errors["Password"] = "Missing Field";
}
329 330
if (!isset($formfields["password2"]) ||
    strcmp($formfields["password2"], "") == 0) {
331 332
    $errors["Confirm Password"] = "Missing Field";
}
333
elseif (strcmp($formfields["password1"], $formfields["password2"])) {
334 335
    $errors["Confirm Password"] = "Does not match Password";
}
336
elseif (! TBvalid_userdata($formfields["password1"])) {
337 338 339 340 341 342 343 344 345 346 347 348 349 350
    $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;
}

#
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
# 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"];
}

368
if (! ($result = NewMmList($uid, $project, $args, $errors))) {
369 370
    # 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.
371 372 373 374 375 376
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
}

#
377
# Okay, redirect the user over to the listadmin page to finish configuring.
378
#
379
header("Location: ${MAILMANURL}/admin/${listname}/?adminpw=${formfields["password1"]}");
380 381

#
382
# When there's an MmList class, this will be a Class function to make a new one...
383
#
384
function NewMmList($uid, $project, $args, &$errors) {
385 386 387 388 389 390 391 392
    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);
393
	$errors[] = "Transient error(1); please try again later.";
394 395 396 397
	return null;
    }
    if (! ($fp = fopen($xmlname, "w"))) {
	TBERROR("Could not open temp file $xmlname", 0);
398
	$errors[] = "Transient error(2); please try again later.";
399 400 401
	return null;
    }

402 403 404
    # Add these. Maybe caller should do this?
    $args["project"]  = $project->pid_idx();

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    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) {
420
	    $errors[] = "Transient error(3, $retval); please try again later.";
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	    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
437
		$errors[] = "Transient error(4, $retval); please try again later.";
438 439 440 441 442 443 444 445 446 447 448 449 450
	}
	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; 
}

451
?>