-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add FormHoneypot DB model * Add form honeypot in templates * Check for form honeypots in contact controller * Add timestamp to honeypot catches * Add datetime in model
- Loading branch information
Showing
5 changed files
with
171 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<label for="<?= $this->trap ?>" style="width: 0px; height: 0px; margin: 0px; padding: 0px; opacity: 0; display: block;"> | ||
<?= $this->text('contact-email-field') ?> | ||
</label> | ||
<br style="width: 0px; height: 0px; margin: 0px; padding: 0px; opacity: 0; display: block;" /> | ||
<input id="<?= $this->trap ?>" name="<?= $this->trap ?>" value="" type="text" class="short" style="width: 0px; height: 0px; margin: 0px; padding: 0px; opacity: 0; display: block;" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/** | ||
* Migration Task class. | ||
*/ | ||
class GoteoFormHoneypot | ||
{ | ||
public function preUp() | ||
{ | ||
// add the pre-migration code here | ||
} | ||
|
||
public function postUp() | ||
{ | ||
// add the post-migration code here | ||
} | ||
|
||
public function preDown() | ||
{ | ||
// add the pre-migration code here | ||
} | ||
|
||
public function postDown() | ||
{ | ||
// add the post-migration code here | ||
} | ||
|
||
/** | ||
* Return the SQL statements for the Up migration | ||
* | ||
* @return string The SQL string to execute for the Up migration. | ||
*/ | ||
public function getUpSQL() | ||
{ | ||
return " | ||
CREATE TABLE `form_honeypot` ( | ||
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, | ||
`trap` VARCHAR(50) NOT NULL, | ||
`prey` TEXT, | ||
`template` TEXT, | ||
`datetime` TIMESTAMP, | ||
PRIMARY KEY (`id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; | ||
"; | ||
} | ||
|
||
/** | ||
* Return the SQL statements for the Down migration | ||
* | ||
* @return string The SQL string to execute for the Down migration. | ||
*/ | ||
public function getDownSQL() | ||
{ | ||
return "DROP TABLE `form_honeypot`"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Goteo\Model; | ||
|
||
use Goteo\Core\Model; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class FormHoneypot extends Model | ||
{ | ||
public $id; | ||
|
||
/** | ||
* This value should be left blank by humans but non-blank by robots | ||
*/ | ||
public $trap; | ||
|
||
/** | ||
* This value was, most-likely, introduced by a robot | ||
*/ | ||
public $prey; | ||
|
||
public $params; | ||
|
||
/** | ||
* The template for the trap field | ||
*/ | ||
public $template = 'partials/form/honeypot'; | ||
|
||
/** | ||
* The date at wich the trap was laid | ||
*/ | ||
public $datetime; | ||
|
||
protected $Table = 'form_honeypot'; | ||
static protected $Table_static = 'form_honeypot'; | ||
|
||
public function save(&$errors = array()) | ||
{ | ||
if (!$this->validate($errors)) return false; | ||
|
||
$this->dbInsertUpdate(['id', 'trap', 'prey', 'template', 'datetime']); | ||
} | ||
|
||
public function validate(&$errors = array()) | ||
{ | ||
if (empty($errors)) | ||
return true; | ||
else | ||
return false; | ||
} | ||
|
||
/** | ||
* Get a trapped form field that is invisible to humans and juicy for robots to fill | ||
*/ | ||
public static function layTrap() | ||
{ | ||
$honeypot = new FormHoneypot; | ||
$honeypot->trap = "email_addr_confirm"; | ||
$honeypot->prey = ""; | ||
$honeypot->datetime = new \DateTime(); | ||
$honeypot->params = [ | ||
'trap' => $honeypot->trap, | ||
'prey' => $honeypot->prey | ||
]; | ||
|
||
return $honeypot; | ||
} | ||
|
||
/** | ||
* Checks if something got caught in the trap | ||
* @return bool `true` if caught something, `false` if not | ||
*/ | ||
public static function checkTrap(string $trap, $data): bool | ||
{ | ||
if ($data instanceof Request) { | ||
return $data->request->get($trap) !== ""; | ||
} | ||
|
||
return false; | ||
} | ||
} |