<?php
// whitelist.php
// copyright 2004 Scott Merrill (skippy@skippy.net)
// released under the GNU GPL, v2 or later

// variables
// ***** EDIT *****
$dbdatabase = 'postfix';
$dbhost = 'localhost';
$dbuser = 'whitelist';
$dbpass = 'whitelist';
$strProtectedTable = 'protected_users';
$strWhitelistTable = 'whitelist';
// ***** STOP EDITING *****

// variables
$wl_count = 0;
$protected_count = 0;
$form = basename($_SERVER['PHP_SELF']);

// ***** FUNCTIONS *****
// check for valid email, courtesy of WordPress
//   /wp-includes/functions-formatting.php: is_email($user_email)
function IsEmail($strEmail) {
        $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
        if(strstr($strEmail, '@') && strstr($strEmail, '.')) {
                if (preg_match($chars, $strEmail)) {
                        return true;
                } else {
                        return false;
                }
        } else {
                return false;
        }
}

function IsInWhitelist ($strEmail = "") {
global $strWhitelistTable;

        // set the default value for intResult
        $intRows = 0;

        $dbquery = "SELECT action FROM $strWhitelistTable WHERE sender = '$strEm
ail'";
        $dbresult = mysql_query($dbquery);
        if ($dbresult) {
                $intRows = mysql_num_rows($dbresult);
        }
        return $intRows;
} // end function IsInWhitelist

function AddToWhitelist ($strEmail = "") {
global $strWhitelistTable;

        // set the default value for intResult
        $intRows = 0;

        $dbquery = "INSERT INTO $strWhitelistTable (sender, action) VALUES ('$st
rEmail', 'OK')";
        $dbresult = mysql_query($dbquery);
        if ($dbresult) {
                $intRows = mysql_affected_rows();
        }
        return $intRows;
} // end function AddToWhitelist

function DeleteFromWhitelist ($strEmail = "") {
global $strWhitelistTable;

        // set the default value for intResult
        $intRows = 0;

        $dbquery = "DELETE FROM $strWhitelistTable WHERE sender = '$strEmail'";
        $dbresult = mysql_query($dbquery);
        if ($dbresult) {
                $intRows = mysql_affected_rows();
        }
        return $intRows;
} // end function DeleteFromWhitelist

function IsProtectedUser ($strEmail = "") {
global $strProtectedTable;

        // set the default value for intResult
        $intRows = 0;

        $dbquery  = "SELECT class FROM $strProtectedTable WHERE recipient='$strE
mail'";
        $dbresult = mysql_query($dbquery);
        if ($dbresult) {
                $intRows = mysql_num_rows($dbresult);
        }
        return $intRows;
} // end function CheckProtectedUser

function AddToProtected ($strEmail = "") {
global $strProtectedTable;

        // set the default value for intResult
        $intRows = 0;

        $dbquery = "INSERT INTO $strProtectedTable (recipient, class) VALUES ('$
strEmail', 'whitelist')";
        $dbresult = mysql_query($dbquery);
        if ($dbresult) {
                $intRows = mysql_affected_rows();
        }
        return $intRows;
} // end function AddProtectedUser;

function DeleteFromProtected ($strEmail = "") {
global $strProtectedTable;

        // set the default value for intResult
        $intRows = 0;

        $dbquery = "DELETE FROM $strProtectedTable WHERE recipient='$strEmail'";
        $dbresult = mysql_query($dbquery);
        if ($dbresult) {
                $intRows = mysql_affected_rows();
        }
        return $intRows;
} // end function DeleteFromProtected

// START MAIN PROGRAM BLOCK
// connect to the database
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Could not connect: " . mysql_error());
$dbselect = mysql_select_db($dbdatabase);
if (! $dbselect) {
        die ("Unable to select $dbdatabase: " . mysql_error());
}

if (isset($_POST['action'])) {
        // add new address(es) to the whitelist
        if ('Whitelist' == $_POST['action']) {
                // split the input into an array
                // and check each address to make sure it's valid
                foreach (preg_split ("/[\s,]+/", $_POST['addresses']) as $strEmail) {
                        if (IsEmail($strEmail)) {
                                if (! IsInWhitelist($strEmail)) {
                                        AddToWhitelist($strEmail);
                                }
                        }
                } // foreach...
        }
        // add new user to protected recipients list
        elseif (('Protect' == $_POST['action']) && (isset($_POST['email'])))  {
                if (( ! empty($_POST['email']) && ! (IsProtectedUser($_POST['email'])))) {
                        AddToProtected($_POST['email']);
                }
        }
// end if $_POST
} elseif (isset($_GET['delete'])) {
        // delete a whitelisted user
        if (('whitelist' == $_GET['delete']) && (isset($_GET['email'])) && (IsEmail($_GET['email']))) {
                if (IsInWhitelist ($_GET['email'])) {
                        DeleteFromWhitelist ($_GET['email']);
                        header("Location: $form");
                }
        } elseif (('protected' == $_GET['delete']) && (isset($_GET['email']))) {
                if (IsProtectedUser($_POST['email'])) {
                        DeleteFromProtected ($_GET['email']);
                        header("Location: $form");
                }
        }
}

// begin main page display

// get the whitelisted senders
$dbquery = "SELECT sender FROM $strWhitelistTable WHERE action = 'OK' ORDER BY s
ender ASC";
$dbresult = mysql_query($dbquery);
if ($dbresult) {
        $wl_count = mysql_num_rows($dbresult);
        while ($dbrow = mysql_fetch_row($dbresult)) {
                $whitelist[] = $dbrow[0];
        }
}

// get the users protected by the whitelist
$dbquery = "SELECT recipient FROM $strProtectedTable WHERE class = 'whitelist' ORDER BY recipient ASC";
$dbresult = mysql_query($dbquery);
if ($dbresult) {
        $protected_count = mysql_num_rows($dbresult);
        while ($dbrow = mysql_fetch_row($dbresult)) {
                $protected[] = $dbrow[0];
        }
}
// main output
?>
<head><title>Whitelist</title></head>
<style type="text/css">
a:link, a:visited {
        text-decoration: none;
        color: #000;
}

.header {
        /* background-color: #c0c0c0; */
        background-color: #000;
        color: #fff;
        font-weight: bold;
}

.green {
        /* background-color: #9fc;
        color: #000; */
        background-color: #cf9;
        color: #000;
}

.white {
        backgorund-color: #fff;
        color: #000;
}

.delete a:hover {
        background-color: #f00;
        color: #fff;
        text-decoration: none;
        padding-left: 5px;
        padding-right: 5px;
}

#container {
        width: 100%;
}

#left {
        width: 60%;
        align: left;
        float: left;
        padding-left: 1px;
        padding-right: 1px;
        border-right: 1px solid #000;
        border-bottom: 1px solid #000;

}

#right {
        width: 35%;
        float: left;
        padding-left: 5px;
        text-align: right;
}
</style>
<div id='container'>
<div id='left'>
<table width='100%' cellspacing='0' cellpadding='5'>
<tr class='header'><th align='left'>Whitelisted address<th align='right'>action</tr>
<?php
// show the whitelist
$greenbar = 'white';
for ($i = 0; $i < $wl_count; $i++) {
        echo "<tr class='$greenbar'><td width='80%'>$whitelist[$i]</td>";
        echo "<td width='20%' align='right'class='delete'>";
        echo "<a href='$form?delete=whitelist&email=$whitelist[$i]'>delete</a></td></tr>\n";
        ("green" == $greenbar) ? $greenbar = "white" : $greenbar = "green";
}
?>
</table>
</div>
<div id='right'>
<form action='<?php echo "$form"; ?>' method='POST'>
Add to Whitelist:
<br />
<textarea rows='10' cols='49' name='addresses'></textarea>
<br />
<input type='submit' name='action' value='Whitelist'>
<br /><br />
Protect User: <input type='text' size='20' name='email'>
<input type='submit' name='action' value='Protect'>
</form>
<br />
<table width="100%" cellspacing='0' cellpadding='5'>
<tr class='header'><th align='left'>Protected Recipient<th align='right'>action</tr>
<?php
// show the list of protected users
for ($i = 0; $i < $protected_count; $i++) {
        echo "<tr bgcolor='#FFFFCC'><td width='80%'>$protected[$i]</td>";
        echo "<td class='delete' align='right'>";
        echo "<a href='?delete=protected&email=$protected[$i]'>delete</a></td></tr>\n";
}
?>
</table>
</div>
</div>

?>
