/*
this script is made for my personal use, but feel free to use it without any warranty whatsoever
*/

function must_be() { // validate given arguments:
                        // arg1 = variable with the value that has to be checked
                        // arg2 = Name of the Value (will be returned in an Array in case of an error)
                        // arg3 = validation:"str" = must be a string (may not be empty and not contain any whitespace)
                        //                     "num" = must be an integer value
                        //                   "email = must be a valid email
                        //                    "name" = may contain letters, numbers and whitespaces, but may not start with a whitespace
    $anzahl = func_num_args();
    if (intval($anzahl/3) != ($anzahl/3)) die("wrong number of arguments (3 per field required)");
    $felder = func_get_args();
    $zaehler = 1;
    $field[0] = "OK";
    for ($i=0; $i < $anzahl; $i++) {
        switch($felder[$i+2]) {
            case "name":
                $match = "/^[a-zA-Z0-9][a-zA-Z0-9 ]*$/";
            break;
            case "str":
                $match = "/^\\S{1,}$/";
            break;
            case "num":
                $match = "/^[0-9]{1,}$/";
            break;
            case "email":
                $match = "/^.{1,}@.{2,}\\..{2,4}$/";
                
            break;
        }
        if (!preg_match($match,$felder[$i])) {
            /*if (_DEBUG) echo "($zaehler): {$felder[$i]} = {$felder[$i+1]} - {$felder[$i+2]} = $match<br />\n";*/
            $field[$zaehler] = $felder[$i+1];
            $field[0] = "Error:";
            $zaehler++;
        }
        $i = $i+2;
    }
    return $field;
}

function spamcheck($wert, $bezeichnung) { // checks $wert for known spammer addresses
    $spamarray = array(
        "angelrrsmr@aol.com",
        "batts1005@aol.com",
        "battsl1005@aol.com",
        "bbarnholtz@aol.com",
        "beacon5919@aol.com",
        "bergkoch8@aol.com",
        "caitl57421@aol.com",
        "cameronmtc@aol.com",
        "charleslegbe@aol.com",
        "charleselegb@aol.com",
        "charleselegbed@aol.com",
        "charleses3299@aol.com",
        "damnitmayn@aol.com",
        "goldfarb88@aol.com",
        "homeigoldstein@aol.com",
        "Homeiragtime@aol.com",
        "Homeragtime@aol.com",
        "jrubin3456@aol.com",
        "jrubin3546@aol.com",
        "jshmng@aol.com",
        "killerhamster@aol.com",
        "kolyathekid1@aol.com",
        "kshmng@aol.com",
        "lIlIIlIl00lOI1II@aol.com",
        "lshmng@aol.com",
        "mhkoch321@aol.com",
        "MichaelHorowitzz@aol.com",
        "michaelhorowitzzzz@SoftHome.net",
        "PeiCanteenMc@aol.com",
        "wnacyiplay@aol.com",
        "wolfione@aol.com",
        "wwjdkid14@aol.com");
    foreach($spamarray AS $key => $val) {
        if (eregi($val,$wert)) {
            $msg[0] = "Error:";
            $msg[1] = "found Spammer $val in $bezeichnung";
            return $msg;
        }
    }
    $msg[0] = "OK";
    return $msg;
}

/* ###########################################*\
# the further values may come from any form       #
# and just show how the function could be used #
\*############################################*/

if ($name)        $checkfield[name]         =    must_be($name,"name","name");
if ($telefon)     $checkfield[telefon]     =    must_be($telefon,"telefon","telefon");
if ($email)        $checkfield[email]         =    must_be($email,"email","email");
if ($bemerkung)    $checkfield[bemerkung]     =     spamcheck($bemerkung,"bemerkung");

foreach($checkfield AS $key => $val) {
    if ($checkfield[$key][0] == "Error:") { // must_be returned an error, lets follow it
        ob_start();
        print_r($checkfield);
        $checkval = ob_get_contents();
        ob_end_clean();
        // the error values are now in $checkval, do what you think you need to do with it
        $location = "Location: error.php"; // this is the page to display in case of such an error
        header($location);
        exit;
    }
}

// here comes the rest if no error occured