<commit_before><?php
/**
 * Akismet class
 *
 * This source file can be used to communicate with Akismet (http://akismet.com)
 *
 * The class is documented in the file itself. If you find any bugs help me out and report them. Reporting can be done by sending an email to php-akismet-bugs[at]verkoyen[dot]eu.
 * If you report a bug, make sure you give me enough information (include your code).
 *
 * Changelog since 1.0.0
 * - some fields (blog) don't have to be urlencoded
 * - submitHam and submitSpam return a boolean instead of void. When an error occors it will still throw an exception
 *
 * Changelog since 1.0.1
 * - fixed some comments
 *
 * Changelog since 1.0.2
 * - when authenticating the key will be validate if it is not empty
 *
 * Changelog since 1.0.3
 * - added a check for safe-mode
 * - fixed some code-styling issues
 *
 * License
 * Copyright (c) <NAME>. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * This software is provided by the author "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the author be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
 *
 * @author			<NAME> <<EMAIL>>
 * @version			1.0.4
 *
 * @copyright		Copyright (c) <NAME>. All rights reserved.
 * @license			BSD License
 */
class Akismet
{
	// internal constant to enable/disable debugging
	const DEBUG = false;

	// url for the api
	const API_URL = 'http://rest.akismet.com';

	// port for the api
	const API_PORT = 80;

	// version of the api
	const API_VERSION = '1.1';

	// current version
	const VERSION = '1.0.4';


	/**
	 * The key for the API
	 *
	 * @var	string
	 */
	private $apiKey;


	/**
	 * The timeout
	 *
	 * @var	int
	 */
	private $timeOut = 60;


	/**
	 * The user agent
	 *
	 * @var	string
	 */
	private $userAgent;


	/**
	 * The url
	 *
	 * @var	string
	 */
	private $url;


// class methods
	/**
	 * Default constructor
	 * Creates an instance of the Akismet Class.
	 *
	 * @return	void
	 * @param	string $apiKey	The API key being verified for use with the API.
	 * @param	string $url		The front page or home URL of the instance making the request. For a blog or wiki this would be the front page. Note: Must be a full URI, including http://.
	 */
	public function __construct($apiKey, $url)
	{
		$this->setApiKey($apiKey);
		$this->setUrl($url);
	}


	/**
	 * Make the call
	 *
	 * @return	string
	 * @param	string $url						The URL to call.
	 * @param	array[optional] $aParameters	The parameters to pass.
	 * @param	bool[optional] $authenticate	Should we authenticate?
	 */
	private function doCall($url, $aParameters = array(), $authenticate = true)
	{
		// redefine
		$url = (string) $url;
		$aParameters = (array) $aParameters;
		$authenticate = (bool) $authenticate;

		// build url
		$url = self::API_URL .'/'. self::API_VERSION .'/'. $url;


		// add key in front of url
		if($authenticate)
		{
			// get api key
			$apiKey = $this->getApiKey();

			// validate apiKey
			if($apiKey == '') throw new AkismetException('Invalid API-key');

			// prepend key
			$url = str_replace('http://', 'http://'. $apiKey .'.', $url);
		}

		// add url into the parameters
		$aParameters['blog'] = $this->getUrl();

		// some parameters shouldn't be encoded
		$aDontEncode = array('blog');

		// rebuild parameters
		foreach($aParameters as $key => $value)
		{
			// some keys shouldn't be encoded
			if(!in_array($key, $aDontEncode)) $aParameters[$key] = urlencode($value);
		}

		// set options
		$options[CURLOPT_URL] = $url;
		$options[CURLOPT_PORT] = self::API_PORT;
		$options[CURLOPT_USERAGENT] = $this->getUserAgent();
		if(ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) $options[CURLOPT_FOLLOWLOCATION] = true;
		$options[CURLOPT_RETURNTRANSFER] = true;
		$options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut();
		$options[CURLOPT_POST] = true;
		$options[CURLOPT_POSTFIELDS] = $aParameters;

		// speed up things, use HTTP 1.0
		$options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;

		// init
		$curl = curl_init();

		// set options
		curl_setopt_array($curl, $options);

		// execute
		$response = curl_exec($curl);
		$headers = curl_getinfo($curl);

		// fetch errors
		$errorNumber = curl_errno($curl);
		$errorMessage = curl_error($curl);

		// close
		curl_close($curl);

		// invalid headers
		if(!in_array($headers['http_code'], array(0, 200)))
		{
			// should we provide debug information
			if(self::DEBUG)
			{
				// make it output proper
				echo '<pre>';

				// dump the header-information
				var_dump($headers);

				// dump the raw response
				var_dump($response);

				// end proper format
				echo '</pre>';

				// stop the script
				exit;
			}

			// throw error
			throw new AkismetException(null, (int) $headers['http_code']);
		}

		// error?
		if($errorNumber != '') throw new AkismetException($errorMessage, $errorNumber);

		// return
		return $response;
	}


	/**
	 * Get the API-key that will be used
	 *
	 * @return	string
	 */
	private function getApiKey()
	{
		return (string) $this->apiKey;
	}


	/**
	 * Get the timeout that will be used
	 *
	 * @return	int
	 */
	public function getTimeOut()
	{
		return (int) $this->timeOut;
	}


	/**
	 * Get the url of the instance making the request
	 *
	 * @return	string
	 */
	public function getUrl()
	{
		return (string) $this->url;
	}


	/**
	 * Get the useragent that will be used. Our version will be prepended to yours.
	 * It will look like: "PHP Akismet/<version> <your-user-agent>"
	 *
	 * @return	string
	 */
	public function getUserAgent()
	{
		return (string) 'PHP Akismet/'. self::VERSION .' '. $this->userAgent;
	}


	/**
	 * Set API key that has to be used
	 *
	 * @return	void
	 * @param	string $apiKey		The API key to use.
	 */
	private function setApiKey($apiKey)
	{
		$this->apiKey = (string) $apiKey;
	}


	/**
	 * Set the timeout
	 * After this time the request will stop. You should handle any errors triggered by this.
	 *
	 * @return	void
	 * @param	int $seconds	The timeout in seconds.
	 */
	public function setTimeOut($seconds)
	{
		$this->timeOut = (int) $seconds;
	}


	/**
	 * Set the url of the instance making the request
	 *
	 * @return	void
	 * @param	string $url		The URL making the request.
	 */
	private function setUrl($url)
	{
		$this->url = (string) $url;
	}


	/**
	 * Set the user-agent for you application
	 * It will be appended to ours, the result will look like: "PHP Akismet/<version> <your-user-agent>"
	 *
	 * @return	void
	 * @param	string $userAgent	Your user-agent, it should look like <app-name>/<app-version>.
	 */
	public function setUserAgent($userAgent)
	{
		$this->userAgent = (string) $userAgent;
	}


// api methods
	/**
	 * Verifies the key
	 *
	 * @return	bool	If the key is valid it will return true, otherwise false will be returned.
	 */
	public function verifyKey()
	{
		// possible answers
		$aPossibleResponses = array('valid', 'invalid');

		// build parameters
		$aParameters['key'] = $this->getApiKey();

		// make the call
		$response = $this->doCall('verify-key', $aParameters, false);

		// validate response
		if(!in_array($response, $aPossibleResponses)) throw new AkismetException($response, 400);

		// valid key
		if($response == 'valid') return true;

		// fallback
		return false;
	}


	/**
	 * Check if the comment is spam or not
	 * This is basically the core of everything. This call takes a number of arguments and characteristics about the submitted content and then returns a thumbs up or thumbs down.
	 * Almost everything is optional, but performance can drop dramatically if you exclude certain elements.
	 * REMARK: If you are having trouble triggering you can send "viagra-test-123" as the author and it will trigger a true response, always.
	 *
	 * @return	bool	If the comment is spam true will be returned, otherwise false.
	 * @param	string[optional] $content	The content that was submitted.
	 * @param	string[optional] $author	Commenters name.
	 * @param	string[optional] $email		Commenters email address.
	 * @param	string[optional] $url		Commenters URL.
	 * @param	string[optional] $permalink	The permanent location of the entry the comment was submitted to.
	 * @param	string[optional] $type		May be blank, comment, trackback, pingback, or a made up value like "registration".
	 */
	public function isSpam($content, $author = null, $email = null, $url = null, $permalink = null, $type = null)
	{
		// possible answers
		$aPossibleResponses = array('true', 'false');

		// redefine
		$content = (string) $content;
		$author = (string) $author;
		$email = (string) $email;
		$url = (string) $url;
		$permalink = (string) $permalink;
		$type = (string) $type;

		// get stuff from the $_SERVER-array
		$ip = (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
		$userAgent = (isset($_SERVER['HTTP_USER_AGENT'])) ? (string) $_SERVER['HTTP_USER_AGENT'] : '';
		$referrer = (isset($_SERVER['HTTP_REFERER'])) ? (string) $_SERVER['HTTP_REFERER'] : '';

		// build parameters
		$aParameters['user_ip'] = $ip;
		$aParameters['user_agent'] = $userAgent;
		if($referrer != '') $aParameters['referrer'] = $referrer;
		if($permalink != '') $aParameters['permalink'] = $permalink;
		if($type != '') $aParameters['comment_type'] = $type;
		if($author != '') $aParameters['comment_author'] = $author;
		if($email != '') $aParameters['comment_author_email'] = $email;
		if($url != '') $aParameters['comment_author_url'] = $url;
		$aParameters['comment_content'] = $content;

		// add all stuff from $_SERVER
		foreach($_SERVER as $key => $value)
		{<commit_msg>Fallback when IP is not available<commit_after><?php
/**
 * Akismet class
 *
 * This source file can be used to communicate with Akismet (http://akismet.com)
 *
 * The class is documented in the file itself. If you find any bugs help me out and report them. Reporting can be done by sending an email to php-akismet-bugs[at]verkoyen[dot]eu.
 * If you report a bug, make sure you give me enough information (include your code).
 *
 * Changelog since 1.0.0
 * - some fields (blog) don't have to be urlencoded
 * - submitHam and submitSpam return a boolean instead of void. When an error occors it will still throw an exception
 *
 * Changelog since 1.0.1
 * - fixed some comments
 *
 * Changelog since 1.0.2
 * - when authenticating the key will be validate if it is not empty
 *
 * Changelog since 1.0.3
 * - added a check for safe-mode
 * - fixed some code-styling issues
 *
 * License
 * Copyright (c) <NAME>. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * This software is provided by the author "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the author be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
 *
 * @author			<NAME> <<EMAIL>>
 * @version			1.0.4
 *
 * @copyright		Copyright (c) <NAME>. All rights reserved.
 * @license			BSD License
 */
class Akismet
{
	// internal constant to enable/disable debugging
	const DEBUG = false;

	// url for the api
	const API_URL = 'http://rest.akismet.com';

	// port for the api
	const API_PORT = 80;

	// version of the api
	const API_VERSION = '1.1';

	// current version
	const VERSION = '1.0.4';


	/**
	 * The key for the API
	 *
	 * @var	string
	 */
	private $apiKey;


	/**
	 * The timeout
	 *
	 * @var	int
	 */
	private $timeOut = 60;


	/**
	 * The user agent
	 *
	 * @var	string
	 */
	private $userAgent;


	/**
	 * The url
	 *
	 * @var	string
	 */
	private $url;


// class methods
	/**
	 * Default constructor
	 * Creates an instance of the Akismet Class.
	 *
	 * @return	void
	 * @param	string $apiKey	The API key being verified for use with the API.
	 * @param	string $url		The front page or home URL of the instance making the request. For a blog or wiki this would be the front page. Note: Must be a full URI, including http://.
	 */
	public function __construct($apiKey, $url)
	{
		$this->setApiKey($apiKey);
		$this->setUrl($url);
	}


	/**
	 * Make the call
	 *
	 * @return	string
	 * @param	string $url						The URL to call.
	 * @param	array[optional] $aParameters	The parameters to pass.
	 * @param	bool[optional] $authenticate	Should we authenticate?
	 */
	private function doCall($url, $aParameters = array(), $authenticate = true)
	{
		// redefine
		$url = (string) $url;
		$aParameters = (array) $aParameters;
		$authenticate = (bool) $authenticate;

		// build url
		$url = self::API_URL .'/'. self::API_VERSION .'/'. $url;


		// add key in front of url
		if($authenticate)
		{
			// get api key
			$apiKey = $this->getApiKey();

			// validate apiKey
			if($apiKey == '') throw new AkismetException('Invalid API-key');

			// prepend key
			$url = str_replace('http://', 'http://'. $apiKey .'.', $url);
		}

		// add url into the parameters
		$aParameters['blog'] = $this->getUrl();

		// some parameters shouldn't be encoded
		$aDontEncode = array('blog');

		// rebuild parameters
		foreach($aParameters as $key => $value)
		{
			// some keys shouldn't be encoded
			if(!in_array($key, $aDontEncode)) $aParameters[$key] = urlencode($value);
		}

		// set options
		$options[CURLOPT_URL] = $url;
		$options[CURLOPT_PORT] = self::API_PORT;
		$options[CURLOPT_USERAGENT] = $this->getUserAgent();
		if(ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) $options[CURLOPT_FOLLOWLOCATION] = true;
		$options[CURLOPT_RETURNTRANSFER] = true;
		$options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut();
		$options[CURLOPT_POST] = true;
		$options[CURLOPT_POSTFIELDS] = $aParameters;

		// speed up things, use HTTP 1.0
		$options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;

		// init
		$curl = curl_init();

		// set options
		curl_setopt_array($curl, $options);

		// execute
		$response = curl_exec($curl);
		$headers = curl_getinfo($curl);

		// fetch errors
		$errorNumber = curl_errno($curl);
		$errorMessage = curl_error($curl);

		// close
		curl_close($curl);

		// invalid headers
		if(!in_array($headers['http_code'], array(0, 200)))
		{
			// should we provide debug information
			if(self::DEBUG)
			{
				// make it output proper
				echo '<pre>';

				// dump the header-information
				var_dump($headers);

				// dump the raw response
				var_dump($response);

				// end proper format
				echo '</pre>';

				// stop the script
				exit;
			}

			// throw error
			throw new AkismetException(null, (int) $headers['http_code']);
		}

		// error?
		if($errorNumber != '') throw new AkismetException($errorMessage, $errorNumber);

		// return
		return $response;
	}


	/**
	 * Get the API-key that will be used
	 *
	 * @return	string
	 */
	private function getApiKey()
	{
		return (string) $this->apiKey;
	}


	/**
	 * Get the timeout that will be used
	 *
	 * @return	int
	 */
	public function getTimeOut()
	{
		return (int) $this->timeOut;
	}


	/**
	 * Get the url of the instance making the request
	 *
	 * @return	string
	 */
	public function getUrl()
	{
		return (string) $this->url;
	}


	/**
	 * Get the useragent that will be used. Our version will be prepended to yours.
	 * It will look like: "PHP Akismet/<version> <your-user-agent>"
	 *
	 * @return	string
	 */
	public function getUserAgent()
	{
		return (string) 'PHP Akismet/'. self::VERSION .' '. $this->userAgent;
	}


	/**
	 * Set API key that has to be used
	 *
	 * @return	void
	 * @param	string $apiKey		The API key to use.
	 */
	private function setApiKey($apiKey)
	{
		$this->apiKey = (string) $apiKey;
	}


	/**
	 * Set the timeout
	 * After this time the request will stop. You should handle any errors triggered by this.
	 *
	 * @return	void
	 * @param	int $seconds	The timeout in seconds.
	 */
	public function setTimeOut($seconds)
	{
		$this->timeOut = (int) $seconds;
	}


	/**
	 * Set the url of the instance making the request
	 *
	 * @return	void
	 * @param	string $url		The URL making the request.
	 */
	private function setUrl($url)
	{
		$this->url = (string) $url;
	}


	/**
	 * Set the user-agent for you application
	 * It will be appended to ours, the result will look like: "PHP Akismet/<version> <your-user-agent>"
	 *
	 * @return	void
	 * @param	string $userAgent	Your user-agent, it should look like <app-name>/<app-version>.
	 */
	public function setUserAgent($userAgent)
	{
		$this->userAgent = (string) $userAgent;
	}


// api methods
	/**
	 * Verifies the key
	 *
	 * @return	bool	If the key is valid it will return true, otherwise false will be returned.
	 */
	public function verifyKey()
	{
		// possible answers
		$aPossibleResponses = array('valid', 'invalid');

		// build parameters
		$aParameters['key'] = $this->getApiKey();

		// make the call
		$response = $this->doCall('verify-key', $aParameters, false);

		// validate response
		if(!in_array($response, $aPossibleResponses)) throw new AkismetException($response, 400);

		// valid key
		if($response == 'valid') return true;

		// fallback
		return false;
	}


	/**
	 * Check if the comment is spam or not
	 * This is basically the core of everything. This call takes a number of arguments and characteristics about the submitted content and then returns a thumbs up or thumbs down.
	 * Almost everything is optional, but performance can drop dramatically if you exclude certain elements.
	 * REMARK: If you are having trouble triggering you can send "viagra-test-123" as the author and it will trigger a true response, always.
	 *
	 * @return	bool	If the comment is spam true will be returned, otherwise false.
	 * @param	string[optional] $content	The content that was submitted.
	 * @param	string[optional] $author	Commenters name.
	 * @param	string[optional] $email		Commenters email address.
	 * @param	string[optional] $url		Commenters URL.
	 * @param	string[optional] $permalink	The permanent location of the entry the comment was submitted to.
	 * @param	string[optional] $type		May be blank, comment, trackback, pingback, or a made up value like "registration".
	 */
	public function isSpam($content, $author = null, $email = null, $url = null, $permalink = null, $type = null)
	{
		// possible answers
		$aPossibleResponses = array('true', 'false');

		// redefine
		$content = (string) $content;
		$author = (string) $author;
		$email = (string) $email;
		$url = (string) $url;
		$permalink = (string) $permalink;
		$type = (string) $type;

		// get stuff from the $_SERVER-array
		if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		elseif(isset($_SERVER['REMOTE_ADDR'])) $ip = $_SERVER['REMOTE_ADDR'];
		else $ip = '';
		$userAgent = (isset($_SERVER['HTTP_USER_AGENT'])) ? (string) $_SERVER['HTTP_USER_AGENT'] : '';
		$referrer = (isset($_SERVER['HTTP_REFERER'])) ? (string) $_SERVER['HTTP_REFERER'] : '';

		// build parameters
		$aParameters['user_ip'] = $ip;
		$aParameters['user_agent'] = $userAgent;
		if($referrer != '') $aParameters['referrer'] = $referrer;
		if($permalink != '') $aParameters['permalink'] = $permalink;
		if($type != '') $aParameters['comment_type'] = $type;
		if($author != '') $aParameters['comment_author'] = $author;
		if($email != '') $aParameters['comment_author_email'] = $email;
		if($url != '') $aParameters['comment_author_url'] = $url;
		$aParameters['comment_content'] = $content;

		// add all stuff from $_SERVER
		foreach($_SERVER as $key => $value)
		{
{'fraction_non_alphanumeric': 0.12641641408253673, 'fraction_numerical': 0.00382373513639543, 'mean_word_length': 3.4913089005235602, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 65, 'https://': 0, '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': '1615995', 'n_tokens_mistral': 7335, 'n_tokens_neox': 6571, 'n_words': 2341}