Файл: apwa/includes/hooks/hook_referrers.php
Строк: 34
<?php
//
// file: includes/hooks/hook_referrers.php
// author: abdev
// begin: 07/13/2013
// version: 0.0.5 - 07/16/2013
// licence: http://opensource.org/licenses/gpl-license.php GNU Public License
//
// ignore
if ( !defined('IN_PHPBB') )
{
exit;
}
// define constant
if ( !defined('REFERRERS_TABLE') )
{
define('REFERRERS_TABLE', $table_prefix . 'referrers');
}
// define main function
function get_ref()
{
global $user, $config, $db;
global $phpEx;
// get the referer
$ref_url = $user->referer;
$ref_url = strtolower($ref_url);
// remove the sid, if the website runs phpbb too !
if ( strpos($ref_url, 'sid=') !== false )
{
$ref_url = preg_replace('/(?)?(&|&)?sid=[a-z0-9]+/', '', $ref_url);
$ref_url = preg_replace("/$phpEx(&|&)+?/", "$phpEx?", $ref_url);
}
// get the host
$cur_host = $config['server_name'] . $config['script_path'];
if ( !empty($ref_url) && (strpos($ref_url, $cur_host) === false) )
{
$ref_host = substr($ref_url, strpos($ref_url, '//') + 2);
if ( strpos($ref_host, '/') === false )
{
$ref_url .= '/';
}
else
{
$ref_host = substr($ref_host, 0, strpos($ref_host, '/'));
}
if ( substr($ref_host, -1) == '.' )
{
$ref_host = substr($ref_host, 0, -1);
}
$sql = 'SELECT ref_url FROM ' . REFERRERS_TABLE . '
WHERE ref_url = '' . $db->sql_escape($ref_url) . ''';
$result = $db->sql_query_limit($sql, 1);
$found = ($row = $db->sql_fetchrow($result));
$db->sql_freeresult($result);
// get timestamp
$now = time();
$fields = array('ref_ip' => $user->ip, 'ref_last' => $now);
if ( !$found )
{
$fields += array(
'ref_host' => $ref_host,
'ref_url' => $ref_url,
'ref_hits' => 1,
'ref_first' => $now,
);
$sql = 'INSERT INTO ' . REFERRERS_TABLE . ' ' . $db->sql_build_array('INSERT', $fields);
}
else
{
$sql = 'UPDATE ' . REFERRERS_TABLE . '
SET ref_hits = ref_hits + 1, ' . $db->sql_build_array('UPDATE', $fields) . '
WHERE ref_url = '' . $db->sql_escape($ref_url) . ''';
}
$db->sql_query($sql);
}
}
// start hook
$phpbb_hook->register('phpbb_user_session_handler', 'get_ref');