Skip to content
Snippets Groups Projects
Commit e0bb1679 authored by llaffont's avatar llaffont
Browse files

Ajout filtrage des bots par plage horaire

parent ae1aca0d
Branches
Tags
No related merge requests found
......@@ -2715,9 +2715,9 @@ library/fonctions/array.php -text
library/fonctions/error.php -text
library/fonctions/file_system.php -text
library/fonctions/fonctions.php -text
library/fonctions/phone.php -text
library/fonctions/sql.php -text
library/fonctions/string.php -text
library/fonctions/useragent.php -text
library/requires.php -text
library/startup.php -text
library/translation/en.mo -text
......@@ -5200,4 +5200,5 @@ tests/library/ZendAfi/View/Helper/TreeViewFixtures.php -text
tests/library/ZendAfi/View/Helper/TreeViewTest.php -text
tests/library/ZendAfi/View/Helper/ViewHelperTestCase.php -text
tests/library/ZendAfi/View/Helper/WebThumbnailTest.php -text
tests/library/fonctions/UserAgentTest.php -text
tests/phpunit.xml -text
......@@ -22,6 +22,9 @@
require('includes.php');
try {
if (isUserAgentBotAndNotAllowed())
exit;
setupOpac()->dispatch();
} catch(Exception $e) {
echo $e->getMessage();
......
......@@ -22,5 +22,5 @@ include_once( "string.php" );
include_once( "error.php" );
include_once( "sql.php" );
include_once( "array.php" );
include_once( "phone.php" );
include_once( "useragent.php" );
?>
\ No newline at end of file
......@@ -18,7 +18,6 @@
* along with AFI-OPAC 2.0; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
function isTelephone() {
if (!array_key_exists('HTTP_USER_AGENT', $_SERVER))
return false;
......@@ -34,4 +33,31 @@ function isTelephone() {
return (isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT'])));
}
function isUserAgentBot($useragent) {
return false !== strpos(strtoupper($useragent), 'BOT/');
}
function isUserAgentBotAndNotAllowed() {
defineConstant('BOT_ALLOWED_START_HOUR', 20);
defineConstant('BOT_ALLOWED_END_HOUR', 8);
return isUserAgentBotAndNotAllowedBetweenHours(isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
BOT_ALLOWED_START_HOUR,
BOT_ALLOWED_END_HOUR);
}
function isUserAgentBotAndNotAllowedBetweenHours($agent, $start_hour, $end_hour) {
if (!isUserAgentBot($agent))
return false;
$now = time();
$start = mktime($start_hour, 0, 0);
$end = mktime($end_hour, 0, 0);
return ($now < $start) && ($now > $end);
}
?>
\ No newline at end of file
<?php
/**
* Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
*
* AFI-OPAC 2.0 is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* AFI-OPAC 2.0 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with AFI-OPAC 2.0; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class BotUserAgentTest extends PHPUnit_Framework_TestCase {
/** @test */
public function ifBotBetweenAllowedHoursShouldReturnFalse() {
$this->assertFalse(isUserAgentBotAndNotAllowedBetweenHours('googlebot/',
date('H')-1,
date('H')+2));
}
/** @test */
public function ifBotOutsideAllowedHoursShouldReturnTrue() {
$this->assertTrue(isUserAgentBotAndNotAllowedBetweenHours('googlebot/',
date('H')+1,
date('H')-2));
}
/** @test */
public function ifNotBotOutsideAllowedHoursShouldReturnFalse() {
$this->assertFalse(isUserAgentBotAndNotAllowedBetweenHours('firefox',
date('H')+1,
date('H')+2));
}
}
?>
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment