Example #1
0
	/**
	 * Validator:
	 * Validates $value for $field->required and other rules
	 * Override
	 *
	 * @param  FieldTable  $field
	 * @param  UserTable   $user        RETURNED populated: touch only variables related to saving this field (also when not validating for showing re-edit)
	 * @param  string      $columnName  Column to validate
	 * @param  string      $value       (RETURNED:) Value to validate, Returned Modified if needed !
	 * @param  array       $postdata    Typically $_POST (but not necessarily), filtering required.
	 * @param  string      $reason      'edit' for save user edit, 'register' for save registration
	 * @return boolean                  True if validate, $this->_setErrorMSG if False
	 */
	public function validate( &$field, &$user, $columnName, &$value, &$postdata, $reason )
	{
		if ( ( ! Application::Cms()->getClientId() ) && ( ! Application::MyUser()->isGlobalModerator() ) && in_array( $reason, array( 'register', 'edit' ) ) ) {
			if ( parent::validate( $field, $user, $columnName, $value, $postdata, $reason ) ) {
				$captcha	=	cbantispamCaptcha::getInstance( $field->get( 'name' ), $field->params->get( 'cbantispam_captcha_mode', null ) );

				if ( ! $captcha->validateCaptcha() ) {
					$this->_setValidationError( $field, $user, $reason, ( $captcha->error ? $captcha->error : CBTxt::T( 'Invalid Captcha Code' ) ) );

					return false;
				}
			}
		}

		return true;
	}
	/**
	 * Generates a captcha audio file
	 *
	 * @param string $id
	 */
	public function captchaAudio( $id )
	{
		global $_PLUGINS;

		if ( $id ) {
			$absPath			=	$_PLUGINS->getPluginPath( $this->getPluginId() );
			$captcha			=	cbantispamCaptcha::getInstance( $id );
			$code				=	$captcha->getCaptchaCode();
			$sounds				=	array();

			for( $i = 0; $i < cbIsoUtf_strlen( $code ); $i++ ) {
				$file			=	$absPath . '/audio/' . $code{$i} . '.mp3';

				if ( ! file_exists( $file ) ) {
					exit( CBTxt::T( 'CAPTCHA_AUDIO_FILE_FAILED', 'failed to locate "[file]" audio file', array( '[file]' => $file ) ) );
				}

				$sounds[]		=	$file;
			}

			header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
			header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
			header( 'Cache-Control: no-store, no-cache, must-revalidate' );
			header( 'Cache-Control: post-check=0, pre-check=0', false );
			header( 'Pragma: no-cache' );
			header( 'Content-Type: audio/mpeg' );
			header( 'Content-Disposition: inline; filename=cbcaptcha.mp3;' );
			header( 'Content-Transfer-Encoding: binary' );

			$out				=	'';
			$count				=	count( $sounds );
			$i					=	0;

			foreach ( $sounds as $sound ) {
				$i++;

				if ( $i != $count ) {
					$offset		=	128;
				} else {
					$offset		=	0;
				}

				$fh				=	fopen( $sound, 'rb' );
				$size			=	filesize( $sound );

				$out			.=	fread( $fh, ( $size - $offset ) );

				fclose( $fh );
			}

			header( 'Content-Length: ' . cbIsoUtf_strlen( $out ) );

			echo $out;

			exit();
		}
	}