#!/usr/bin/perl

use strict;
use warnings;
use CGI qw/:standard/;

# File: uploadpatch.cgi
# Author: Ben Hearsum
# Description:
#   This cgi script displays a simple form that allows a user to submit a diff
#   that will eventually be uploaded to a Buildbot installation.
#   Along with the patch this script generates a .info file that contains the
#   name of the submitter, as read from $ENV['REMOTE_USER'], the date, in unix
#   time, the name of the patchfile, and the description read from the form.
#   This information is used by the download script to generate the
#   'buildbot sendchange' command.


# where the patches will go after being submitted
my $PATCH_DIR = "/var/www/patches";
# the size limit for the file, in bytes
my $SIZELIMIT = 10*1024*1024;

$CGI::POST_MAX = $SIZELIMIT;

sub write_page {
    my $name = shift;
    my $err = shift;

    my $limit = $SIZELIMIT / 1024;

    print "Content-type: text/html\n\n";
    print <<ENDOFHTML;
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE html PUBLIC 
               '-//W3C//DTD XHTML 1.0 Frameset//EN'
               'http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>Upload a Patch to Buildbot</title>
<style type="text/css">
  body {
    margin-left: auto;
    margin-right: auto;
  }

  div {
    text-align: center;
  }

  p {
    text-align: center;
    color: red;
    font-weight: bold;
  }

  table {
    width: 35%;
    margin-left: auto;
    margin-right: auto;
  }

  td {
    margin-top: 2px;
    margin-bottom: 2px;
    border: none;
    width: 50%;
  }

  tr#title {
    padding-bottom: 6px;
  }

  th {
    text-align: center;
  }

  td.lbl {
    text-align: right;
    vertical-align: top;
    padding-top: 5px;
  }

  td.field {
    text-align: left;
  }
</style>
</head>

<body>

<form action="/cgi-bin/uploadpatch.cgi" method="post"
      enctype="multipart/form-data">
<div>
<table>
  <tr id="title">
    <th colspan="2">Upload a patch to Buildbot</th>
  </tr>
  <tr>
    <td class="lbl">Description:</td>
    <td class="field">
      <textarea name="description" cols="35" rows="6"></textarea>
    </td>
  </tr>
  <tr>
    <td class="lbl">File:</td>
    <td class="field">
      <input type="file" name="patchfile" value="Send Patch">
    </td>
  </tr>
  <tr>
    <td colspan="2" style="text-align: center">
      <input type="submit" />
    </td>
  </tr>
</table>
</div>

</form>
<p>Note: Uploaded files must be less than ${limit}kB</p>
ENDOFHTML

    if ($err) {
        print "<p>$err</p>";
    }
    print "</body>\n</html>";
}

sub process {
    my $time = time();
    # get the parameters
    my $name = "";
    $name = $ENV{'REMOTE_USER'};
    if ($name eq "") {
        $name = "Noname";
    }

    my $description = param("description");
    if ($description eq "") {
        $description = "No description given.";
    }
    else {
        if ($description =~ /([`]|\$\()/) {
            write_page($name,
                       "Description must not contain backticks or '\$('");
        }
        $description =~ s/\n//g;
    }

    my $patchfile = param("patchfile");

    # only allow alphanumeric, hyphens, and single dots
    if ($patchfile !~ /^([\w-]|\.[\w-])+$/) {
        # ends the script
        
        write_page($name,
                   "Invalid filename. Please use only alphanumeric, -, _, "
                   . "and single dots");
        return;
    }

    # if we get here the file is small enough and passes the filename test

    # pull all of the contents of the file
    my $patch_handle = upload("patchfile");

    # strip off everything except the filename itself
    $patchfile =~ s/.*[\/\\](.*)/$1/;

    # generate the filenames
    $patchfile = "$time-$patchfile";
    my $infofile = "$patchfile.info";

    # write the patch
    my $filename = "$PATCH_DIR/$patchfile";
    open PATCH, ">$filename";
    binmode PATCH;
    
    while ( <$patch_handle> ) {
      print PATCH;
    }
    close PATCH;

    # now write the infofile
    $filename = "$PATCH_DIR/$infofile";

    open INFO, ">$filename";

    print INFO "submitter: $name\n";
    print INFO "date: $time\n";
    print INFO "patchfile: $patchfile\n";
    print INFO "description: $description\n";

    close INFO;

    print "Content-Type: text/html\n\n";
    print "Patch uploaded successfully";
}

if (param()) {
    process();
}
else {
    write_page();
}
