abstract class BotchaRecipe in BOTCHA Spam Prevention 7.2
Same name and namespace in other branches
- 6.2 controller/botcha_recipe.controller.inc \BotchaRecipe
- 6.3 controller/recipe/botcha.recipe.controller.inc \BotchaRecipe
- 7.3 controller/recipe/botcha.recipe.controller.inc \BotchaRecipe
Abstract class to describe recipe data structure.
Hierarchy
- class \BotchaRecipe
Expanded class hierarchy of BotchaRecipe
File
- controller/
botcha_recipe.controller.inc, line 11 - Controller layer of the BotchaRecipe objects.
View source
abstract class BotchaRecipe {
/**
* Identifier of the recipe.
*/
public $id;
/**
* Brief description of the recipe.
* It should contain explanation of how bots would fail with it
* and what the recipe exactly does.
*/
protected $description;
/**
* Options that received as parameters turned into settings
* by merging with default values.
*/
protected $settings = array();
/**
* Secret.
*/
protected $secret;
/**
* Method of recipe genration.
*/
protected $method;
/**
* CSS to add to the page.
*/
protected $css;
/**
* Javascript to add to the page.
*/
protected $js;
/**
* @todo Do we really need it? Probably the best way is to provide mail field always - it hides our fields.
* Name of the field in the form to use in error messages
* (to mask botcha fields).
*/
public $error_field;
/**
* Text to give users if botcha recipe blocks submission.
* It should give some help to real human users in cases
* of disabled Javascript or CSS.
*/
public $error_text;
protected $recipebooks = array();
/**
* Status of the spam check.
*/
public $status = 'none';
public static function getRecipe($id, $create = TRUE) {
$r = BotchaRecipeModel::getRecipe($id);
$classname = $r->classname;
$recipe = new $classname($id);
$recipe
->setTitle($r->title)
->setDescription($r->description);
return $recipe;
}
public function setRecipebook($rbid) {
$this->recipebooks[$rbid] = $rbid;
// Save changed state.
Botcha::setRecipe($this);
return $this;
}
public function save() {
BotchaRecipeModel::save($this);
// Clean session to fetch new values.
Botcha::clean();
}
function setTitle($title) {
$this->title = $title;
// Save changed state.
Botcha::setRecipe($this);
return $this;
}
function getTitle() {
return $this->title;
}
function setDescription($description) {
$this->description = $description;
// Save changed state.
Botcha::setRecipe($this);
return $this;
}
function getDescription() {
return $this->description;
}
function setSecret($secret) {
$this->secret = $secret;
// Save changed state.
Botcha::setRecipe($this);
return $this;
}
function getSecret() {
return $this->secret;
}
function setMethod($method) {
$this->method = $method;
// Save changed state.
Botcha::setRecipe($this);
return $this;
}
function getMethod() {
return $this->method;
}
function setStatus($status) {
$this->status = $status;
// Save changed state.
Botcha::setRecipe($this);
return $this;
}
function getStatus() {
return $this->status;
}
function getSetting($key) {
return $this->settings[$key];
}
function setSetting($key, $value) {
$this->settings[$key] = $value;
// Save changed state.
Botcha::setRecipe($this);
return $this;
}
/**
* Magic method __construct.
*/
function __construct($id) {
$this->id = $id;
// Get human-readable description about this recipe
// to clarify its work process.
$this
->getInfo();
}
/**
* Used to get information about the recipe.
* Must be overridden.
*/
function getInfo() {
$this->error_field = 'mail';
$this->error_text = t('You must be a human, not a spam bot, to submit forms on this website.') . ' ' . t('If you insist that you are a human, please try again.') . ' ' . t('If error persists, contact webmaster using contact link at the bottom of this page and give all the details of this error (your browser, version, OS).');
}
/**
* Used to get default recipe data structure.
*/
function getDefaultSettings() {
return array(
'fields' => $this
->getFields(),
'css' => $this
->getCss(),
'js' => $this
->getJs(),
);
}
/**
* Universal getter.
* Wrapper getProperty is used to let class methods be used not only in getting
* default settings. It gives flexibility to make calls to the class methods
* in any order: the first of them will always calculate the property value
* and set the setting, while others will just get this already calculated value.
* It also provides consistency: we are sure that when we get some property,
* it is set appropriately.
*/
function getProperty(&$value, $getter_callback, $parameters = NULL) {
if (empty($value)) {
$value = $this
->{$getter_callback}($parameters);
}
return $value;
}
/**
* Spam check.
*
* @param type $form
* @param type $form_state
*/
function isSpam($form, $form_state) {
}
/**
* Handle form depending on the result of spam check.
*
* @param type $result
* @param type $form
* @param type $form_state
*/
function handle($result, $form, $form_state) {
// !!~ @todo handle Implement real logic of handling.
switch ($result) {
case 'success':
break;
case 'spam':
default:
break;
}
}
function getSeed() {
return md5(get_class($this) . substr($this->secret, 0, -4));
}
function getFields() {
$fields_count = $this
->getFieldCount();
$fields = array();
for ($i = 0; $i < $fields_count; $i++) {
$fields[$i] = $this
->getField($i);
}
return $fields;
}
// @todo BotchaRecipe getField Replace deltas with machine names.
function getField($delta) {
return array(
'name' => $this
->getProperty($this->settings['fields'][$delta]['name'], 'getFieldName', $delta),
'class' => $this
->getProperty($this->settings['fields'][$delta]['class'], 'getFieldClass', $delta),
'prefix' => $this
->getProperty($this->settings['fields'][$delta]['prefix'], 'getFieldPrefix', $delta),
);
}
/**
* Should be overridden.
*
* @return string
*/
public function getCss() {
}
/**
* Should be overridden.
*
* @return array
*/
public function getJs() {
}
// @todo BotchaRecipe getFieldName Replace deltas with machine names.
function getFieldName($delta) {
return substr($this
->getProperty($this->seed, 'getSeed'), 0, 3) . '_name';
}
// @todo BotchaRecipe getFieldClass Replace deltas with machine names.
function getFieldClass($delta) {
// 'a' fix for Firefox - it breaks on ".<number>" class in CSS filter!
return 'a' . substr($this
->getProperty($this->seed, 'getSeed'), 1, 4) . '_field';
}
// @todo BotchaRecipe getFieldPrefix Replace deltas with machine names.
function getFieldPrefix($delta) {
return substr($this
->getProperty($this->seed, 'getSeed'), 10, mt_rand(3, 6));
}
/**
* Used to get information about the recipe.
* Must be overridden with calling to parent::generateFormElements.
* @todo BotchaRecipe generateFormElements Switch from indexed array to associative.
*/
function generateFormElements() {
$css = $this
->getProperty($this->settings['css'], 'getCss');
if (!empty($css)) {
drupal_add_css('' . $this->settings['css'] . '', array(
'type' => 'inline',
));
}
return array();
}
/*
* Apply recipe modifying form properties.
*/
public function applyRecipe(&$form, &$form_state) {
// Add BOTCHA fields to the form.
$form_elements = $this
->generateFormElements();
foreach ($form_elements as $field_name => $field_properties) {
unset($field_properties['!valid_token']);
$form[$field_name] = $field_properties;
if ($this->method == 'build_id_submit') {
// Save submitted values in our stash for later use in _validate,
// as we have to reset them here at _form_alter stage.
// It won't be possible to reset after validation as there is no
// reliable mechanism in Form API, i.e. form_set_value() does not
// change rendered form and form errors disable whole 'rebuild' business.
if (isset($_POST[$field_name])) {
$form_state['botcha_submit_values'][$field_name] = $_POST[$field_name];
}
if (isset($field_properties['#default_value'])) {
// Reset our controls to defaults here (as explained above).
$form[$field_name]['#value'] = $field_properties['#default_value'];
$form_state['post'][$field_name] = $field_properties['#default_value'];
$_POST[$field_name] = $field_properties['#default_value'];
}
}
else {
//unset($field_properties['!valid_token']);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BotchaRecipe:: |
protected | property | CSS to add to the page. | |
BotchaRecipe:: |
protected | property | Brief description of the recipe. It should contain explanation of how bots would fail with it and what the recipe exactly does. | |
BotchaRecipe:: |
public | property | @todo Do we really need it? Probably the best way is to provide mail field always - it hides our fields. Name of the field in the form to use in error messages (to mask botcha fields). | |
BotchaRecipe:: |
public | property | Text to give users if botcha recipe blocks submission. It should give some help to real human users in cases of disabled Javascript or CSS. | |
BotchaRecipe:: |
public | property | Identifier of the recipe. | |
BotchaRecipe:: |
protected | property | Javascript to add to the page. | |
BotchaRecipe:: |
protected | property | Method of recipe genration. | |
BotchaRecipe:: |
protected | property | ||
BotchaRecipe:: |
protected | property | Secret. | |
BotchaRecipe:: |
protected | property | Options that received as parameters turned into settings by merging with default values. | |
BotchaRecipe:: |
public | property | Status of the spam check. | |
BotchaRecipe:: |
public | function | 2 | |
BotchaRecipe:: |
function | Used to get information about the recipe. Must be overridden with calling to parent::generateFormElements. @todo BotchaRecipe generateFormElements Switch from indexed array to associative. | 2 | |
BotchaRecipe:: |
public | function | Should be overridden. | 1 |
BotchaRecipe:: |
function | Used to get default recipe data structure. | ||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | 1 | ||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | 3 | ||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | Used to get information about the recipe. Must be overridden. | 3 | |
BotchaRecipe:: |
public | function | Should be overridden. | 1 |
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | Universal getter. Wrapper getProperty is used to let class methods be used not only in getting default settings. It gives flexibility to make calls to the class methods in any order: the first of them will always calculate the property value and set… | ||
BotchaRecipe:: |
public static | function | ||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | Handle form depending on the result of spam check. | 1 | |
BotchaRecipe:: |
function | Spam check. | 4 | |
BotchaRecipe:: |
public | function | ||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
public | function | ||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | |||
BotchaRecipe:: |
function | Magic method __construct. |