$value ) $req .= $key . '=' . urlencode( stripslashes($value) ) . '&'; // Cut the last '&' $req=substr($req,0,strlen($req)-1); return $req; } /** * Submits an HTTP POST to a solvemedia server * @param string $host * @param string $path * @param array $data * @param int port * @return array response */ function _adcopy_http_post($host, $path, $data, $port = 80) { $req = _adcopy_qsencode ($data); $http_request = "POST $path HTTP/1.0\r\n"; $http_request .= "Host: $host\r\n"; $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n"; $http_request .= "Content-Length: " . strlen($req) . "\r\n"; $http_request .= "User-Agent: solvemedia/PHP\r\n"; $http_request .= "\r\n"; $http_request .= $req; $response = ''; if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) { die ('Could not open socket'); } fwrite($fs, $http_request); while ( !feof($fs) ) $response .= fgets($fs, 1024); // One TCP-IP packet [sic] fclose($fs); $response = explode("\r\n\r\n", $response, 2); return $response; } /** * Gets the challenge HTML (javascript and non-javascript version). * This is called from the browser, and the resulting solvemedia HTML widget * is embedded within the HTML form it was called from. * @param string $pubkey A public key for solvemedia * @param string $error The error given by solvemedia (optional, default is null) * @param boolean $use_ssl Should the request be made over ssl? (optional, default is false) * @return string - The HTML to be embedded in the user's form. */ function solvemedia_get_html ($pubkey, $error = null, $use_ssl = false) { if ($pubkey == null || $pubkey == '') { die ("To use solvemedia you must get an API key from " . ADCOPY_SIGNUP . ""); } if ($use_ssl) { $server = ADCOPY_API_SECURE_SERVER; } else { $server = ADCOPY_API_SERVER; } $errorpart = ""; if ($error) { $errorpart = ";error=1"; } return ' '; } /** * A SolveMediaResponse is returned from solvemedia_check_answer() */ class SolveMediaResponse { var $is_valid; var $error; } /** * Calls an HTTP POST function to verify if the user's guess was correct * @param string $privkey * @param string $remoteip * @param string $challenge * @param string $response * @param string $hashkey * @return SolveMediaResponse */ function solvemedia_check_answer ($privkey, $remoteip, $challenge, $response, $hashkey = '' ) { if ($privkey == null || $privkey == '') { die ("To use solvemedia you must get an API key from " . ADCOPY_SIGNUP . ""); } if ($remoteip == null || $remoteip == '') { die ("For security reasons, you must pass the remote ip to solvemedia"); } //discard spam submissions if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) { $adcopy_response = new SolveMediaResponse(); $adcopy_response->is_valid = false; $adcopy_response->error = 'incorrect-solution'; return $adcopy_response; } $response = _adcopy_http_post (ADCOPY_VERIFY_SERVER, "/papi/verify", [ 'privatekey' => $privkey, 'remoteip' => $remoteip, 'challenge' => $challenge, 'response' => $response, ], ); $answers = explode ("\n", $response [1]); $adcopy_response = new SolveMediaResponse(); if( strlen($hashkey) ){ # validate message authenticator $hash = sha1( $answers[0] . $challenge . $hashkey ); if( $hash != $answers[2] ){ $adcopy_response->is_valid = false; $adcopy_response->error = 'hash-fail'; return $adcopy_response; } } if (trim ($answers [0]) == 'true') { $adcopy_response->is_valid = true; } else { $adcopy_response->is_valid = false; $adcopy_response->error = $answers [1]; } return $adcopy_response; } /** * gets a URL where the user can sign up for solvemedia. If your application * has a configuration page where you enter a key, you should provide a link * using this function. * @param string $domain The domain where the page is hosted * @param string $appname The name of your application */ function solvemedia_get_signup_url ($domain = null, $appname = null) { return ADCOPY_SIGNUP . "?" . _adcopy_qsencode ( ['domain' => $domain, 'app' => $appname]); } /** * Calls an HTTP POST function to verify if the user's response was correct * @param string $privkey * @param string $$verifycode * @return SolveMediaResponse */ function solvemedia_precheck_response ($privkey, $verifycode) { //discard spam submissions if ($verifycode == null || strlen($verifycode) == 0 ) { $adcopy_response = new SolveMediaResponse(); $adcopy_response->is_valid = false; $adcopy_response->error = 'incorrect-solution'; return $adcopy_response; } $response = _adcopy_http_post (ADCOPY_VERIFY_SERVER, "/papi/verify.precheck.server", [ 'privatekey' => $privkey, 'verify_code' => $verifycode, ], ); $answers = explode ("\n", $response [1]); $adcopy_response = new SolveMediaResponse(); if (trim ($answers [0]) == 'true') { $adcopy_response->is_valid = true; } else { $adcopy_response->is_valid = false; $adcopy_response->error = $answers [1]; } return $adcopy_response; }