<commit_before><?php
/**
 * DominionEnterprises\SolveMedia\Service class for accessing the Solve Media API service.
 * This component has been heavily modified from it's original form to
 * encapsulate the functionality in a class based structure that is
 * compatible with class autoloading functionality.
 *
 * @author <NAME> <<EMAIL>>
 */
namespace DominionEnterprises\SolveMedia;
use Guzzle\Http\Client as GuzzleClient;

final class Service
{
    /**
     * The solvemedia server URL's
     */
    const ADCOPY_API_SERVER = 'http://api.solvemedia.com';
    const ADCOPY_API_SECURE_SERVER = 'https://api-secure.solvemedia.com';
    const ADCOPY_VERIFY_SERVER = 'http://verify.solvemedia.com/papi/verify';
    const ADCOPY_SIGNUP = 'http://api.solvemedia.com/public/signup';

    private $_pubkey;
    private $_privkey;
    private $_hashkey;

    /**
     * Construct a Service object with the required api key values.
     *
     * @param string $pubkey A public key for solvemedia
     * @param string $privkey A private key for solvemedia
     * @param string $hashkey An optional hash key for verification
     * @throws \Exception
     */
    public function __construct($pubkey, $privkey, $hashkey = '')
    {
        if (empty($pubkey) || empty($privkey)) {
            throw new \Exception(
                "To use solvemedia you must get an API key from <a href='" . self::ADCOPY_SIGNUP . "'>" . self::ADCOPY_SIGNUP . '</a>'
            );
        }

        $this->_pubkey = $pubkey;
        $this->_privkey = $privkey;
        $this->_hashkey = $hashkey;
    }

    /**
     * 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 $error The error given by solvemedia (optional, default is null)
     * @param boolean $useSsl Should the request be made over ssl? (optional, default is false)
     * @return string The HTML to be embedded in the user's form.
     */
    public function getHtml($error = null, $useSsl = false)
    {
        $server = self::ADCOPY_API_SERVER;
        if ($useSsl) {
            $server = self::ADCOPY_API_SECURE_SERVER;
        }

        $errorpart = '';
        if ($error) {
            $errorpart = ';error=1';
        }

        return '<script type="text/javascript" src="' . $server . '/papi/challenge.script?k=' . $this->_pubkey . $errorpart . '"></script>

<noscript>
    <iframe src="' . $server . '/papi/challenge.noscript?k=' . $this->_pubkey . $errorpart
            . '" height="300" width="500" frameborder="0"></iframe><br/>
    <textarea name="adcopy_challenge" rows="3" cols="40"></textarea>
    <input type="hidden" name="adcopy_response" value="manual_challenge"/>
</noscript>';
    }

    /**
     * Calls an HTTP POST function to verify if the user's guess was correct
     *
     * @param string $remoteip
     * @param string $challenge
     * @param string $response
     * @throws \Exception
     * @return DominionEnterprises\SolveMedia\Response
     */
    public function checkAnswer($remoteip, $challenge, $response)
    {
        if ($remoteip == null || $remoteip == '') {
            throw new \Exception('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) {
            return new Response(false, 'incorrect-solution');
        }

        $client = new GuzzleClient();
        $httpResponse = $client->post(
            self::ADCOPY_VERIFY_SERVER,
            ['User-Agent' => 'solvemedia/PHP'],
            ['privatekey' => $this->_privkey, 'remoteip' => $remoteip, 'challenge' => $challenge, 'response' => $response]
        )->send();<commit_msg>use Exception so it doesn't have to be qualified.<commit_after><?php
/**
 * DominionEnterprises\SolveMedia\Service class for accessing the Solve Media API service.
 * This component has been heavily modified from it's original form to
 * encapsulate the functionality in a class based structure that is
 * compatible with class autoloading functionality.
 *
 * @author <NAME> <<EMAIL>>
 */
namespace DominionEnterprises\SolveMedia;
use Guzzle\Http\Client as GuzzleClient;
use Exception;

final class Service
{
    /**
     * The solvemedia server URL's
     */
    const ADCOPY_API_SERVER = 'http://api.solvemedia.com';
    const ADCOPY_API_SECURE_SERVER = 'https://api-secure.solvemedia.com';
    const ADCOPY_VERIFY_SERVER = 'http://verify.solvemedia.com/papi/verify';
    const ADCOPY_SIGNUP = 'http://api.solvemedia.com/public/signup';

    private $_pubkey;
    private $_privkey;
    private $_hashkey;

    /**
     * Construct a Service object with the required api key values.
     *
     * @param string $pubkey A public key for solvemedia
     * @param string $privkey A private key for solvemedia
     * @param string $hashkey An optional hash key for verification
     * @throws Exception
     */
    public function __construct($pubkey, $privkey, $hashkey = '')
    {
        if (empty($pubkey) || empty($privkey)) {
            throw new Exception(
                "To use solvemedia you must get an API key from <a href='" . self::ADCOPY_SIGNUP . "'>" . self::ADCOPY_SIGNUP . '</a>'
            );
        }

        $this->_pubkey = $pubkey;
        $this->_privkey = $privkey;
        $this->_hashkey = $hashkey;
    }

    /**
     * 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 $error The error given by solvemedia (optional, default is null)
     * @param boolean $useSsl Should the request be made over ssl? (optional, default is false)
     * @return string The HTML to be embedded in the user's form.
     */
    public function getHtml($error = null, $useSsl = false)
    {
        $server = self::ADCOPY_API_SERVER;
        if ($useSsl) {
            $server = self::ADCOPY_API_SECURE_SERVER;
        }

        $errorpart = '';
        if ($error) {
            $errorpart = ';error=1';
        }

        return '<script type="text/javascript" src="' . $server . '/papi/challenge.script?k=' . $this->_pubkey . $errorpart . '"></script>

<noscript>
    <iframe src="' . $server . '/papi/challenge.noscript?k=' . $this->_pubkey . $errorpart
            . '" height="300" width="500" frameborder="0"></iframe><br/>
    <textarea name="adcopy_challenge" rows="3" cols="40"></textarea>
    <input type="hidden" name="adcopy_response" value="manual_challenge"/>
</noscript>';
    }

    /**
     * Calls an HTTP POST function to verify if the user's guess was correct
     *
     * @param string $remoteip
     * @param string $challenge
     * @param string $response
     * @throws Exception
     * @return DominionEnterprises\SolveMedia\Response
     */
    public function checkAnswer($remoteip, $challenge, $response)
    {
        if ($remoteip == null || $remoteip == '') {
            throw new Exception('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) {
            return new Response(false, 'incorrect-solution');
        }

        $client = new GuzzleClient();
        $httpResponse = $client->post(
            self::ADCOPY_VERIFY_SERVER,
            ['User-Agent' => 'solvemedia/PHP'],
            ['privatekey' => $this->_privkey, 'remoteip' => $remoteip, 'challenge' => $challenge, 'response' => $response]
        )->send();
{'fraction_non_alphanumeric': 0.12777136998590286, 'fraction_numerical': 0.003332051774958349, 'mean_word_length': 2.6915799432355723, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 59, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1303982', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2203, 'n_words': 673}