Back to index

opendkim  2.6.6
repute.php
Go to the documentation of this file.
00001 <?php
00002 ###
00003 ### Copyright (c) 2011, 2012, The OpenDKIM Project.  All rights reserved.
00004 ###
00005 
00006 #
00007 # PHP code to query a reputation database and generate a reputon
00008 #
00009 
00010 #
00011 # load local configuration for databae values
00012 #
00013 require "repute-config.php";
00014 
00015 #
00016 # extract query values and build the SQL query
00017 #
00018 if (!isset($_GET["application"]) ||
00019     !isset($_GET["assertion"]) ||
00020     !isset($_GET["service"]) ||
00021     !isset($_GET["subject"]))
00022        die("Malformed query");
00023 
00024 $application = $_GET["application"];
00025 $assertion = $_GET["assertion"];
00026 $service = $_GET["service"];
00027 $subject = $_GET["subject"];
00028 
00029 if (strtolower($application) != "email-id")
00030        die("Unrecognized application");
00031 if (strtolower($assertion) != "spam")
00032        die("Unrecognized assertion");
00033 
00034 if (isset($_GET["reporter"]))
00035        $reporter = $_GET["reporter"];
00036 else
00037        $reporter = 0;
00038 
00039 if (isset($_GET["format"]))
00040 {
00041        $format = $_GET["format"];
00042        if (strtolower($format) != "xml" &&
00043            strtolower($format) != "json")
00044               die("Unrecognized format");
00045        else if (strtolower($format) == "xml")
00046               $use_json = 0;
00047        else
00048               $use_json = 1;
00049 }
00050 
00051 $query1 = "SELECT    ratio_high,
00052                      UNIX_TIMESTAMP(updated),
00053                      rate_samples
00054            FROM             predictions
00055            WHERE     name = '$subject'
00056            AND          reporter = 0";
00057  
00058 $query2 = "SELECT    daily_limit_low
00059            FROM             predictions
00060            WHERE     name = '$subject'
00061            AND          reporter = $reporter";
00062  
00063 #
00064 # connect to the DB
00065 #
00066 if (!($connection = mysql_connect($repute_db, $repute_user, $repute_pwd)))
00067        die("Unable to connect to database server");
00068 
00069 # 
00070 # select the DB
00071 # 
00072 if (!mysql_select_db($repute_dbname, $connection))
00073        die("Unable to connect to database");
00074 
00075 #
00076 # run the first query
00077 #
00078 if (!($result = mysql_query($query1, $connection)))
00079        die("Query failed");
00080 
00081 #
00082 # extract results
00083 #
00084 $row = mysql_fetch_array($result, MYSQL_NUM);
00085 if (!$row)
00086        die("No data available");
00087 $rating = $row[0];
00088 $updated = $row[1];
00089 $samples = $row[2];
00090 
00091 #
00092 # run the second query
00093 #
00094 if (!($result = mysql_query($query2, $connection)))
00095        die("Query failed");
00096 
00097 $row = mysql_fetch_array($result, MYSQL_NUM);
00098 if (!$row)
00099        die("No data available");
00100 $rate = $row[0];
00101 
00102 #
00103 # MIME header
00104 #
00105 
00106 printf("Content-Type: application/reputon\n");
00107 printf("\n");
00108 
00109 #
00110 # Construct the reputon
00111 #
00112 
00113 if (isset($use_json) && $use_json == 1)
00114 {
00115        printf("{\n");
00116        printf("\t\"rater\": \"$service\",\n");
00117        printf("\t\"rater-authenticity\": 1.0,\n");
00118        printf("\t\"assertion\": \"SPAM\",\n");
00119        printf("\t\"identity\": \"DKIM\",\n");
00120        printf("\t\"rate\": $rate\n");
00121        printf("\t\"rated\": \"$subject\",\n");
00122        printf("\t\"rating\": $rating,\n");
00123        printf("\t\"sample-size\": $samples,\n");
00124        printf("\t\"updated\": $updated\n");
00125        printf("}\n");
00126 }
00127 else
00128 {
00129        printf("<reputation>\n");
00130        printf(" <reputon>\n");
00131        printf("  <rater>$service</rater>\n");
00132        printf("  <rater-authenticity>1</rater-authenticity>\n");
00133        printf("  <assertion>SPAM</assertion>\n");
00134        printf("  <identity>DKIM</identity>\n");
00135        printf("  <rate>$rate</rate>\n");
00136        printf("  <rated>$subject</rated>\n");
00137        printf("  <rating>$rating</rating>\n");
00138        printf("  <sample-size>$samples</sample-size>\n");
00139        printf("  <updated>$updated</updated>\n");
00140        printf(" </reputon>\n");
00141        printf("</reputation>\n");
00142 }
00143 
00144 # all done!
00145 ?>