Skip to content

Commit

Permalink
Merge pull request #5 from BCcampus/honey
Browse files Browse the repository at this point in the history
adding honey pot, changing field value dependin on cert/prod env
  • Loading branch information
alex-418 committed Mar 22, 2017
2 parents d2edfd8 + 7ff8f07 commit a777d89
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
11 changes: 9 additions & 2 deletions includes/class-validate-by-domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ private function load_dependencies() {
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-validate-by-domain-public.php';

/**
* Class responsible for a honey pot, to thwart spam accounts from being created
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-honey-pot.php';

$this->loader = new Validate_By_Domain_Loader();

}
Expand Down Expand Up @@ -144,13 +149,15 @@ private function define_admin_hooks() {
private function define_public_hooks() {

$plugin_public = new Validate_By_Domain_Public( $this->get_bc_validate(), $this->get_version() );
$honey_pot = new HoneyPot();

// $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
// $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
$this->loader->add_filter( 'bp_signup_validate', $plugin_public, 'signupUserBC' );
// $this->loader->add_action( 'bp_signup_profile_fields', $plugin_public, 'signupExtraBC' );
$this->loader->add_filter( 'bp_signup_usermeta', $plugin_public, 'signupMetaBC' );
$this->loader->add_filter( 'bp_core_activate_account', $plugin_public, 'mapRoleToCapability' );
$this->loader->add_action( 'bp_after_signup_profile_fields', $honey_pot, 'addHoneyPot' );
$this->loader->add_filter( 'bp_core_validate_user_signup', $honey_pot, 'checkHoneyPot' );

}

/**
Expand Down
63 changes: 63 additions & 0 deletions public/class-honey-pot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Project: pd
* Project Sponsor: BCcampus <https://bccampus.ca>
* Date: 2017-03-22
* Licensed under GPLv3, or any later version
*
* @author Brad Payne
* @package OPENTEXTBOOKS
* @license https://www.gnu.org/licenses/gpl-3.0.txt
*
*
* Modified from original 'BudddyPress-HoneyPot' version 1.1
* @copyright Pixel Jar
* https://github.com/pixeljar/BuddyPress-Honeypot/
*/
class HoneyPot {
/**
* default values for the honeypot
* change these via filters if you
* start getting spam registrations
*/
CONST VBD_HONEYPOT_NAME = 'RoodElbowBallsTamerFistHem';
CONST VBD_HONEYPOT_ID = 'PilotFamousVenialNewSpiceNoisy';

function __construct() {

}

/**
* Add a hidden text input that users won't see
* so it should always be empty. If it's filled out
* we know it's a spambot or some other hooligan
*
* @filter vpd_honeypot_name
* @filter vpd_honeypot_id
*/
function addHoneyPot() {

echo '<div style="display: none;">';
echo '<input type="text" name="' . apply_filters( 'vbd_honeypot_name', self::VBD_HONEYPOT_NAME ) . '" id="' . apply_filters( 'vbd_honeypot_id', self::VBD_HONEYPOT_ID ) . '" />';
echo '</div>';
}

/**
* Check to see if the honeypot field has a value.
* If it does, return an error
*
* @filter vpd_honeypot_name
* @filter vpd_honeypot_fail_message
*/
function checkHoneyPot( $result = array() ) {
global $bp;
$vpd_honeypot_name = apply_filters( 'vbd_honeypot_name', self::VBD_HONEYPOT_NAME );
if ( isset( $_POST[ $vpd_honeypot_name ] ) && ! empty( $_POST[ $vpd_honeypot_name ] ) ) {
$result['errors']->add( 'vbd_honeypot', apply_filters( 'vpd_honeypot_fail_message', __( "unhelpful error message." ) ) );
}

return $result;
}
}

29 changes: 25 additions & 4 deletions public/class-validate-by-domain-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ class Validate_By_Domain_Public {
'pacific-care.bc.ca',
);

private $field_val;

/**
* Initialize the class and set its properties.
*
Expand All @@ -246,6 +248,24 @@ public function __construct( $bc_validate, $version ) {

$this->bc_validate = $bc_validate;
$this->version = $version;
$this->setFieldNum();

}

/**
* set different field values depending on prod or dev env
*/
public function setFieldNum() {
$host = parse_url( network_site_url(), PHP_URL_HOST );

if ( strcmp( 'earlyyearsbc.ca', $host ) ) {
$field_val = '155';
} else {
$field_val = '3';
}

$this->field_val = $field_val;

}

/**
Expand Down Expand Up @@ -296,14 +316,14 @@ public function enqueue_scripts() {
*/
public function signupUserBC() {
global $bp;

$field_val = 'field_' . $this->field_val;
if ( isset( $_POST ) && ( 'request-details' != $bp->signup->step ) ) {
return;
}

// Only filter email addresses for Organizers
// (must be from a recognized agency)
if ( 0 === strcmp( $_POST['field_155'], 'Organizer' ) ) {
if ( 0 === strcmp( $_POST[ $field_val ], 'Organizer' ) ) {
$domain = $this->parseEmail( $_POST['signup_email'] );
$ok = $this->checkDomain( $domain );

Expand Down Expand Up @@ -397,9 +417,10 @@ public function signupExtraBC( $errors ) {
* @return array $usermeta
*/
public function signupMetaBC( $usermeta ) {
$field_val = 'field_' . $this->field_val;

if ( isset( $_POST['field_155'] ) ) {
$usermeta['eypd_role'] = $_POST['field_155'];
if ( isset( $_POST[ $field_val ] ) ) {
$usermeta['eypd_role'] = $_POST[ $field_val ];
}

return $usermeta;
Expand Down

0 comments on commit a777d89

Please sign in to comment.