phpBB Incapsula hook

Somebody called Yoav1987 on the Incapsula support forums asked me about integrating phpBB3 with Incapsula, so I quickly wrapped up the following hook for him. All in seven minutes, including creating the repo and pushing the commits

phpbb3-incapsula-integration01_k0nsl

 
hook_incapsula.php
[php]
<?php
/**
* Copyright (C) 2013 by k0nsl (http://k0nsl.org/blog)
*/

/**
* @ignore
*/
if (!defined(‘IN_PHPBB’))
{
exit;
}

define(‘HEADER_NAME’,’HTTP_INCAP_CLIENT_IP’);

/**
* Our one and only hook
*/
function hook_incapsula(&$hook)
{
try {

//stop process if there is no header
if (empty($_SERVER[HEADER_NAME])) throw new Exception(‘No header defined’, 1);

//validate header value
if (function_exists(‘filter_var’)) {
$ip = filter_var($_SERVER[HEADER_NAME], FILTER_VALIDATE_IP);
if (false === $ip) throw new Exception(‘The value is not a valid IP address’, 2);
}
else {
$ip = trim($_SERVER[HEADER_NAME]);
if (false === preg_match(‘/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/’, $ip)) throw new Exception(‘The value is not a valid IP address’, 2);
}

//At this point the initial IP value is exist and validated
$_SERVER[‘REMOTE_ADDR’] = $ip;
} catch (Exception $e) {}
}

/**
* Initialize our hook
*/

$phpbb_hook->register(array(‘template’, ‘display’), ‘hook_incapsula’);
[/php]
Also located at GitHub for convenience:
https://github.com/k0nsl/phpbb3-incapsula-hook

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *