<?php
//==============================================================================
// File: DoComments.php
// Version: 2
// Date: August 21, 2009
// Author: Johnnie Rose, Jr. (hide@address.com)
// Copyright: Jerrata DoComments (c)2009 Jerrata Solutions, all rights reserved.
// Need help? Please contact hide@address.com for assistance.
//==============================================================================
require_once("settings.php");
define("NETWORK_CHUNK_SIZE",8192);
define("DOCOMMENTS_VERSION","2");
define("DOCOMMENTS_HANDLER_PORT",80);
define("DOCOMMENTS_HANDLER_ADDR","jerrata.com");
//==============================================================================
define("RESULT_FAILED","f");
define("RESULT_OK","o");
//==============================================================================
define("DOCOMMENTS_ACTION","a");
define("DOCOMMENTS_ACTION_GET_COMMENTS","g");
define("DOCOMMENTS_ACTION_POST_COMMENT","p");
define("DOCOMMENTS_ACTION_VOTE_COMMENT","v");
//==============================================================================
if(!function_exists("not"))
{
function not($expr)
{
return !$expr;
}
}
//==============================================================================
if(!function_exists("strings_match"))
{
function strings_match($str1,$str2)
{
if(strcmp($str1,$str2) == 0)
return true;
return false;
}
}
//==============================================================================
if(!function_exists("failed"))
{
function failed($expr)
{
return ($expr === false);
}
}
//==============================================================================
if(!function_exists("succeeded"))
{
function succeeded($expr)
{
return ($expr === true);
}
}
//==============================================================================
if(!function_exists("array_from_name_value_pairs"))
{
function array_from_name_value_pairs($anded_name_value_pairs)
{
$array2 = explode("&",$anded_name_value_pairs);
foreach($array2 as $val)
{
$pos = strpos($val,"=");
$key = substr($val,0,$pos);
$array3[$key] = substr($val,$pos + 1,strlen($val));
}
return $array3;
}
}
//==============================================================================
$argv = array_from_name_value_pairs($_SERVER['QUERY_STRING']);
if(not(array_key_exists(DOCOMMENTS_ACTION,$argv)))
{
DocomFail("No action requested.");
return;
}
$valid_actions = array( DOCOMMENTS_ACTION_GET_COMMENTS,
DOCOMMENTS_ACTION_POST_COMMENT,
DOCOMMENTS_ACTION_VOTE_COMMENT);
if(not(in_array($argv[DOCOMMENTS_ACTION],$valid_actions,true)))
{
DocomFail("Invalid action requested.");
return;
}
$handler_funcs = array( DOCOMMENTS_ACTION_GET_COMMENTS => "GetComments",
DOCOMMENTS_ACTION_POST_COMMENT => "PostComment",
DOCOMMENTS_ACTION_VOTE_COMMENT => "VoteComment");
// open a connection to docomments.jerrata.com
if(failed($socket = fsockopen(DOCOMMENTS_HANDLER_ADDR,DOCOMMENTS_HANDLER_PORT)))
{
DocomFail("Unable to establish connection to docomments.jerrata.com");
return;
}
call_user_func($handler_funcs[$argv[DOCOMMENTS_ACTION]]);
//==============================================================================
function VoteComment()
{
$args = array( "comment_id" => "",
"opinion" => "");
$arg_names = array_keys($args);
foreach($arg_names as $arg_name)
{
if(not(array_key_exists($arg_name,$_POST)))
{
DocomFail("The request lacks one or more required arguments.");
return;
}
if(get_magic_quotes_gpc())
$args[$arg_name] = rawurldecode(stripslashes($_POST[$arg_name]));
else
$args[$arg_name] = rawurldecode($_POST[$arg_name]);
}
// sanitize
if(strings_match($args["comment_id"],"") || not(ctype_digit($args["comment_id"])))
{
DocomFail("The supplied comment_id is invalid.");
return;
}
if(strings_match($args["opinion"],"") || not(in_array($args["opinion"],array("good","bad"),true)))
{
DocomFail("The supplied opinion is invalid.");
return;
}
// submit vote to docomments.jerrata.com
$request = DOCOMMENTS_ACTION . "=" . DOCOMMENTS_ACTION_VOTE_COMMENT;
$request .= "&docom_version=" . rawurlencode(DOCOMMENTS_VERSION);
$request .= "&site_name=" . rawurlencode(SITE_NAME);
$request .= "&site_password=" . rawurlencode(SITE_PASSWORD);
$request .= "&comment_id=" . rawurlencode($args["comment_id"]);
$request .= "&opinion=" . rawurlencode($args["opinion"]);
$header = "POST http://docomments.jerrata.com/DoComments.php HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($request) . "\r\n\r\n";
global $socket;
if(failed(fputs($socket,$header . $request)))
{
DocomFail("Unable to send request to post comment.");
return;
}
$response = "";
while(not(feof($socket)))
{
if(failed($partial_response = fread($socket,NETWORK_CHUNK_SIZE)))
{
DocomFail("Unable to read response to request to post comment.");
return;
}
$response .= $partial_response;
}
fclose($socket);
// strip off the HTTP preamble
if(failed($preamble_pos = strpos($response,"\r\n\r\n")))
{
DocomFail("DoComments server returned an unrecognized response.");
return;
}
$response = substr($response,$preamble_pos + strlen("\r\n\r\n"));
echo $response;
}
//==============================================================================
function PostComment()
{
$args = array( "topic_name" => "",
"author_name" => "",
"author_email_addr_exists" => "",
"author_email_addr" => "",
"author_email_on_reply" => "",
"author_url_exists" => "",
"author_url" => "",
"comment_rating" => "",
"comment_text" => "",
"captcha_a" => "",
"captcha_entered" => "",
"is_reply" => "",
"parent_comment_id" => "");
$arg_names = array_keys($args);
foreach($arg_names as $arg_name)
{
if(not(array_key_exists($arg_name,$_POST)))
{
DocomFail("The request lacks one or more required arguments.");
return;
}
if(get_magic_quotes_gpc())
$args[$arg_name] = rawurldecode(stripslashes($_POST[$arg_name]));
else
$args[$arg_name] = rawurldecode($_POST[$arg_name]);
}
// sanitize
if( strings_match($args["topic_name"],"") ||
strings_match($args["author_name"],"") ||
strings_match($args["author_email_addr_exists"],"") ||
strings_match($args["author_email_addr"],"") ||
strings_match($args["author_email_on_reply"],"") ||
strings_match($args["author_url_exists"],"") ||
strings_match($args["author_url"],"") ||
strings_match($args["comment_rating"],"") ||
strings_match($args["comment_text"],"") ||
strings_match($args["captcha_a"],"") ||
strings_match($args["captcha_entered"],"") ||
strings_match($args["is_reply"],"") ||
strings_match($args["parent_comment_id"],"") ||
not(ctype_digit($args["parent_comment_id"])))
{
DocomFail("DoComments did not understand your request.");
return;
}
$valid_values = array("y","n");
if( not(in_array($args["author_email_addr_exists"],$valid_values,true)) ||
not(in_array($args["author_email_on_reply"],$valid_values,true)) ||
not(in_array($args["author_url_exists"],$valid_values,true)) ||
not(in_array($args["is_reply"],$valid_values,true)))
{
DocomFail("DoComments did not understand your request.");
return;
}
if(strings_match($args["author_email_addr_exists"],"y") && (failed(strpos($args["author_email_addr"],"@")) || failed(strpos($args["author_email_addr"],"."))))
{
DocomFail("The value supplied for the author's email address is invalid.");
return;
}
if(strings_match($args["author_url_exists"],"y") && failed(strpos($args["author_url"],"http")))
{
DocomFail("The value supplied for the author's Web address is invalid.");
return;
}
$valid_values = array("none","1","2","3","4","5");
if(not(in_array($args["comment_rating"],$valid_values,true)))
{
DocomFail("DoComments did not understand your request.");
return;
}
// don't check comment_text length b/c don't know COMMENT_MAXLENGTH at this level
// post comment to docomments.jerrata.com
$request = DOCOMMENTS_ACTION . "=" . DOCOMMENTS_ACTION_POST_COMMENT;
$request .= "&docom_version=" . rawurlencode(DOCOMMENTS_VERSION);
$request .= "&site_name=" . rawurlencode(SITE_NAME);
$request .= "&site_password=" . rawurlencode(SITE_PASSWORD);
$request .= "&topic_name=" . rawurlencode($args["topic_name"]);
$request .= "&author_name=" . rawurlencode($args["author_name"]);
$request .= "&author_email_addr_exists=" . rawurlencode($args["author_email_addr_exists"]);
$request .= "&author_email_addr=" . rawurlencode($args["author_email_addr"]);
$request .= "&author_email_on_reply=" . rawurlencode($args["author_email_on_reply"]);
$request .= "&author_url_exists=" . rawurlencode($args["author_url_exists"]);
$request .= "&author_url=" . rawurlencode($args["author_url"]);
$request .= "&comment_rating=" . rawurlencode($args["comment_rating"]);
$request .= "&comment_text=" . rawurlencode($args["comment_text"]);
$request .= "&captcha_a=" . rawurlencode($args["captcha_a"]);
$request .= "&captcha_entered=" . rawurlencode($args["captcha_entered"]);
$request .= "&is_reply=" . rawurlencode($args["is_reply"]);
$request .= "&parent_comment_id=" . rawurlencode($args["parent_comment_id"]);
$header = "POST http://docomments.jerrata.com/DoComments.php HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($request) . "\r\n\r\n";
global $socket;
if(failed(fputs($socket,$header . $request)))
{
DocomFail("Unable to send request to post comment.");
return;
}
$response = "";
while(not(feof($socket)))
{
if(failed($partial_response = fread($socket,NETWORK_CHUNK_SIZE)))
{
DocomFail("Unable to read response to request to post comment.");
return;
}
$response .= $partial_response;
}
fclose($socket);
// strip off the HTTP preamble
if(failed($preamble_pos = strpos($response,"\r\n\r\n")))
{
DocomFail("DoComments server returned an unrecognized response.");
return;
}
$response = substr($response,$preamble_pos + strlen("\r\n\r\n"));
echo $response;
}
//==============================================================================
function GetComments()
{
$args = array( "topic_name" => "",
"pageno" => "",
"latest_comment_id" => "",
"paginate" => "");
$arg_names = array_keys($args);
foreach($arg_names as $arg_name)
{
if(not(array_key_exists($arg_name,$_POST)))
{
DocomFail("The request lacks one or more required arguments.");
return;
}
if(get_magic_quotes_gpc())
$args[$arg_name] = rawurldecode(stripslashes($_POST[$arg_name]));
else
$args[$arg_name] = rawurldecode($_POST[$arg_name]);
}
// sanitize
if(strings_match($args["topic_name"],""))
{
DocomFail("An invalid topic was requested.");
return;
}
if(strings_match($args["pageno"],"") || not(ctype_digit($args["pageno"])) || intval($args["pageno"]) < 1)
{
DocomFail("An invalid page number was requested.");
return;
}
if(strings_match($args["latest_comment_id"],"") || not(ctype_digit($args["latest_comment_id"])))
{
DocomFail("The supplied latest_comment_id is invalid.");
return;
}
if(strings_match($args["paginate"],"") || not(in_array($args["paginate"],array("y","n"),true)))
{
DocomFail("The supplied value for paginate is invalid.");
return;
}
// determine whether latest_comment_id is outdated/retrieve comments
$request = DOCOMMENTS_ACTION . "=" . DOCOMMENTS_ACTION_GET_COMMENTS;
$request .= "&docom_version=" . rawurlencode(DOCOMMENTS_VERSION);
$request .= "&site_name=" . rawurlencode(SITE_NAME);
$request .= "&site_password=" . rawurlencode(SITE_PASSWORD);
$request .= "&topic_name=" . rawurlencode($args["topic_name"]);
$request .= "&pageno=" . rawurlencode($args["pageno"]);
$request .= "&latest_comment_id=" . rawurlencode($args["latest_comment_id"]);
$request .= "&paginate=" . rawurlencode($args["paginate"]);
$header = "POST http://docomments.jerrata.com/DoComments.php HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($request) . "\r\n\r\n";
global $socket;
if(failed(fputs($socket,$header . $request)))
{
DocomFail("Unable to send request to retrieve comments.");
return;
}
$response = "";
while(not(feof($socket)))
{
if(failed($partial_response = fread($socket,NETWORK_CHUNK_SIZE)))
{
DocomFail("Unable to read response to request to retrieve comments.");
return;
}
$response .= $partial_response;
}
fclose($socket);
// strip off the HTTP preamble
if(failed($preamble_pos = strpos($response,"\r\n\r\n")))
{
DocomFail("DoComments server returned an unrecognized response.");
return;
}
$response = substr($response,$preamble_pos + strlen("\r\n\r\n"));
echo $response;
}
//==============================================================================
function DocomFail($message)
{
echo RESULT_FAILED . strlen($message) . "|" . $message;
}
?>