<commit_before><?php
// (c) Copyright 2002-2011 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

//this script may only be included - so its better to die if called directly.
global $access;
$access->check_script($_SERVER["SCRIPT_NAME"],basename(__FILE__));

/**
 * A simple class to switch between Zend_Captcha_Image and
 * Zend_Captcha_ReCaptcha based on admin preference
 */
class Captcha
{

	/**
	 * The type of the captch ('default' when using Zend_Captcha_Image
	 * or 'recaptcha' when using Zend_Captcha_ReCaptcha)
	 *
	 * @var string 
	 */
	public $type = '';

	/**
	 * An instance of Zend_Captcha_Image or Zend_Captcha_ReCaptcha
	 * depending on the value of $this->type
	 *
	 * @var object
	 */
	public $captcha = '';

	/**
	 * Class constructor: decides wheter to create an instance of
	 * Zend_Captcha_Image or Zend_Captcha_ReCaptcha
	 *
	 * @return null
	 */
	function __construct() {
		global $prefs;
		
		if ($prefs['recaptcha_enabled'] == 'y' && !empty($prefs['recaptcha_privkey']) && !empty($prefs['recaptcha_pubkey'])) {
			require_once('lib/core/Zend/Captcha/ReCaptcha.php');
			$this->captcha = new Zend_Captcha_ReCaptcha(array(
				'privkey' => $prefs['recaptcha_privkey'],
				'pubkey' => $prefs['recaptcha_pubkey'],
				'theme' => 'clean'
			));

			$this->type = 'recaptcha';

			$this->recaptchaCustomTranslations();
		} else if (extension_loaded('gd') && function_exists('imagepng') && function_exists('imageftbbox')) {
			$this->captcha = new Zend_Captcha_Image(array(
				'wordLen' => $prefs['captcha_wordLen'],
				'timeout' => 600,
				'font' => dirname(__FILE__) . '/DejaVuSansMono.ttf',
				'imgdir' => 'temp/public/',
				'suffix' => '.captcha.png',
				'width' => $prefs['captcha_width'],
				'dotNoiseLevel' => $prefs['captcha_noise'],
			));
			$this->type = 'default';
		} else {
			require_once('lib/core/Zend/Captcha/Dumb.php');
			$this->captcha = new Zend_Captcha_Dumb;
			$this->type = 'dumb';
		}

		$this->setErrorMessages();
	}

	/**
	 * Create the default captcha
	 *
	 * @return void
	 */
	function generate() {
		try {
			$this->captcha->generate();
			if ($this->type == 'default') {
				// the following needed to keep session active for ajax checking 
				$session = $this->captcha->getSession();
				$session->setExpirationHops(2, null, true);
				$this->captcha->setSession($session);
				$this->captcha->setKeepSession(false);
			}
		} catch (Zend_Exception $e) {
		}
	}

	/** Return captcha ID
	 *
	 * @return string captcha ID
	 */
	function getId() {
		return $this->captcha->getId();
	}

	/**
	 * HTML code for the captcha
	 *
	 * @return string<commit_msg>[ENH] captcha: Make it possible to create a captcha of any type, independent of prefs, and return the captcha id from the generate function (should make no difference to existing code)<commit_after><?php
// (c) Copyright 2002-2011 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

//this script may only be included - so its better to die if called directly.
global $access;
$access->check_script($_SERVER["SCRIPT_NAME"],basename(__FILE__));

/**
 * A simple class to switch between Zend_Captcha_Image and
 * Zend_Captcha_ReCaptcha based on admin preference
 */
class Captcha
{

	/**
	 * The type of the captch ('default' when using Zend_Captcha_Image
	 * or 'recaptcha' when using Zend_Captcha_ReCaptcha)
	 *
	 * @var string 
	 */
	public $type = '';

	/**
	 * An instance of Zend_Captcha_Image or Zend_Captcha_ReCaptcha
	 * depending on the value of $this->type
	 *
	 * @var object
	 */
	public $captcha = '';

	/**
	 * Class constructor: decides wheter to create an instance of
	 * Zend_Captcha_Image or Zend_Captcha_ReCaptcha
	 *
	 * @return null
	 */
	function __construct( $type = '' ) {
		global $prefs;

		if (empty($type)) {
			if ($prefs['recaptcha_enabled'] == 'y' && !empty($prefs['recaptcha_privkey']) && !empty($prefs['recaptcha_pubkey'])) {
				$type = 'recaptcha';
			} else if (extension_loaded('gd') && function_exists('imagepng') && function_exists('imageftbbox')) {
				$type = 'default';
			} else {
				$type = 'dumb';
			}
		}

		if ($type === 'recaptcha') {
			require_once('lib/core/Zend/Captcha/ReCaptcha.php');
			$this->captcha = new Zend_Captcha_ReCaptcha(array(
				'privkey' => $prefs['recaptcha_privkey'],
				'pubkey' => $prefs['recaptcha_pubkey'],
				'theme' => 'clean'
			));

			$this->type = 'recaptcha';

			$this->recaptchaCustomTranslations();
		} else if ($type === 'default') {
			$this->captcha = new Zend_Captcha_Image(array(
				'wordLen' => $prefs['captcha_wordLen'],
				'timeout' => 600,
				'font' => dirname(__FILE__) . '/DejaVuSansMono.ttf',
				'imgdir' => 'temp/public/',
				'suffix' => '.captcha.png',
				'width' => $prefs['captcha_width'],
				'dotNoiseLevel' => $prefs['captcha_noise'],
			));
			$this->type = 'default';
		} else {		// implied $type==='dumb'
			require_once('lib/core/Zend/Captcha/Dumb.php');
			$this->captcha = new Zend_Captcha_Dumb;
			$this->type = 'dumb';
		}

		$this->setErrorMessages();
	}

	/**
	 * Create the default captcha
	 *
	 * @return void
	 */
	function generate() {
		try {
			$key = $this->captcha->generate();
			if ($this->type == 'default') {
				// the following needed to keep session active for ajax checking 
				$session = $this->captcha->getSession();
				$session->setExpirationHops(2, null, true);
				$this->captcha->setSession($session);
				$this->captcha->setKeepSession(false);
			}
			return $key;
		} catch (Zend_Exception $e) {
		}
	}

	/** Return captcha ID
	 *
	 * @return string captcha ID
	 */
	function getId() {
		return $this->captcha->getId();
	}

	/**
	 * HTML code for the captcha
	 *
	 * @return string
{'fraction_non_alphanumeric': 0.15585495675316036, 'fraction_numerical': 0.003992015968063872, 'mean_word_length': 3.6468315301391034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, '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': '6856646', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2000, 'n_words': 501}