diff --git a/VERSIONS b/VERSIONS index 7e87849a456444a5bc53e5af0defc5fcc87b5164..2c115a558b685e30000b71626d8fad42b3249105 100644 --- a/VERSIONS +++ b/VERSIONS @@ -12,6 +12,8 @@ - ticket #43916 : Recherche : tri par ordre alphabétique des favoris. + - ticket #49353 : Administration : bloquage de l'indexation du bloque ajax CVS dans la notice par les bots + 25/10/2016 - v7.7.12 - v7.7.13 diff --git a/application/modules/opac/controllers/NoticeajaxController.php b/application/modules/opac/controllers/NoticeajaxController.php index 3a4c9a5df19a34ae5ad3f5b4c4a9e06d1004832d..7c2fd4fc152efdb53fea0fe82794668f5125c599 100644 --- a/application/modules/opac/controllers/NoticeajaxController.php +++ b/application/modules/opac/controllers/NoticeajaxController.php @@ -470,6 +470,10 @@ class NoticeAjaxController extends Zend_Controller_Action { function cvsSearchAction() { + if ((new Class_UserAgent())->isBot()) + return $this->_sendResponse(''); + + $this->preferences = Class_Profil::getCurrentProfil()->getCfgModulesPreferences('recherche', 'resultat', 'simple'); diff --git a/application/modules/opac/views/scripts/portail.phtml b/application/modules/opac/views/scripts/portail.phtml index 475652bbe572c823e28d700e63b9d14b8e3e7b52..27c6e6dc9b16613fb2bf9b54331801b6d583e27d 100644 --- a/application/modules/opac/views/scripts/portail.phtml +++ b/application/modules/opac/views/scripts/portail.phtml @@ -1,7 +1,7 @@ <?php ob_start(); echo '<body '.$this->bodyParam.'>'; -if (isTelephone() && Class_Profil::isAPhoneProfilEnabled()) +if ((new Class_UserAgent())->isMobile() && Class_Profil::isAPhoneProfilEnabled()) echo sprintf('<div class="back_to_phone">%s</div>', $this->tagAnchor($this->url(['module' => 'telephone'], null, true), $this->_('Afficher le site en mode mobile'))); diff --git a/index.php b/index.php index 9f3e7136edd15d23694c711d2916b1ca247401f2..5698f917f75f1e88ec486df0ac8ce3d12eca2828 100644 --- a/index.php +++ b/index.php @@ -27,7 +27,8 @@ if ((!file_exists('local.php') || !file_exists('config.ini') || !file_exists('co require('includes.php'); try { - if (isUserAgentBotAndNotAllowed()) + require_once(__DIR__ . '/library/Class/UserAgent.php'); + if ((new Class_UserAgent())->isBotAndNotAllowed()) exit; setupOpac()->dispatch(); diff --git a/library/Class/UserAgent.php b/library/Class/UserAgent.php new file mode 100644 index 0000000000000000000000000000000000000000..e0421a8ab34966cea22df3c4810ca63b4404e02b --- /dev/null +++ b/library/Class/UserAgent.php @@ -0,0 +1,86 @@ +<?php +/** + * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH 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). + * + * BOKEH 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 BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class Class_UserAgent { + protected + $_user_agent_string; + + public function __construct($useragent = null) { + $this->_user_agent_string = $useragent + ? $useragent + : $this->_readUserAgentString(); + } + + + public function isBot() { + return 0 !== preg_match('/(bot\/|sistrix|voilabot|slurp)/i', + $this->_user_agent_string); + } + + + protected function _readUserAgentString() { + return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; + } + + + public function getUserAgentString() { + return $this->_user_agent_string; + } + + + public function isBotAndNotAllowed() { + defineConstant('BOT_ALLOWED_START_HOUR', 20); + defineConstant('BOT_ALLOWED_END_HOUR', 8); + + return $this->isBotAndNotAllowedBetweenHours(BOT_ALLOWED_START_HOUR, + BOT_ALLOWED_END_HOUR); + } + + + public function isBotAndNotAllowedBetweenHours($start_hour, $end_hour) { + if (!$this->isBot()) + return false; + + $now = time(); + $start = mktime($start_hour, 0, 0); + $end = mktime($end_hour, 0, 0); + + return ($now < $start) && ($now > $end); + } + + + public function isMobile() { + if (!$this->_user_agent_string) + return false; + + $regex_match="/(mobile|nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|"; + $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|"; + $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|"; + $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|"; + $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220"; + $regex_match.=")/i"; + + return (isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) + or preg_match($regex_match, strtolower($this->_user_agent_string))); + } +} +?> \ No newline at end of file diff --git a/library/ZendAfi/Controller/Action/Helper/ViewRenderer.php b/library/ZendAfi/Controller/Action/Helper/ViewRenderer.php index 4f53a1003ecd02502d18c7596498ba02debe3b60..5399f66fb9db6d94af2cb35f73b3672abc3b3870 100644 --- a/library/ZendAfi/Controller/Action/Helper/ViewRenderer.php +++ b/library/ZendAfi/Controller/Action/Helper/ViewRenderer.php @@ -83,7 +83,7 @@ class ZendAfi_Controller_Action_Helper_ViewRenderer extends Zend_Controller_Acti return $this; } - (isTelephone() or $this->isEmbedded()) + ((new Class_UserAgent())->isMobile() or $this->isEmbedded()) ? $this->setLayoutScript("main.phtml") : $this->setLayoutScript("iphone.phtml"); diff --git a/library/ZendAfi/Controller/Plugin/DefineURLs.php b/library/ZendAfi/Controller/Plugin/DefineURLs.php index d2872958adddbd35aa57b72aad759fddf136c217..fc7433fd483041912762479ef0e9372fe428ef35 100644 --- a/library/ZendAfi/Controller/Plugin/DefineURLs.php +++ b/library/ZendAfi/Controller/Plugin/DefineURLs.php @@ -41,7 +41,9 @@ class ZendAfi_Controller_Plugin_DefineURLs extends Zend_Controller_Plugin_Abstra public function shouldSelectTelephone($request) { - return ($request->getModuleName()=='telephone') || (isTelephone() and ('admin' !== $request->getModuleName())); + return + ($request->getModuleName()=='telephone') + || ((new Class_UserAgent())->isMobile() and ('admin' !== $request->getModuleName())); } diff --git a/library/fonctions/fonctions.php b/library/fonctions/fonctions.php index 1cf786c3fc463294d8b13c3470220573b863467d..1a4dedc96117fa133225678683f3259203995b39 100644 --- a/library/fonctions/fonctions.php +++ b/library/fonctions/fonctions.php @@ -22,7 +22,6 @@ include_once( "string.php" ); include_once( "error.php" ); include_once( "sql.php" ); include_once( "array.php" ); -include_once( "useragent.php" ); include_once( "numbers.php" ); include_once( 'cosmogramme/php/fonctions/date_heure.php'); ?> \ No newline at end of file diff --git a/library/fonctions/useragent.php b/library/fonctions/useragent.php deleted file mode 100644 index 689fe0673b25188b5f618ee175a52e30bc629e6d..0000000000000000000000000000000000000000 --- a/library/fonctions/useragent.php +++ /dev/null @@ -1,63 +0,0 @@ -<?php -/** - * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved. - * - * BOKEH 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). - * - * BOKEH 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 BOKEH; 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; - - // Test sur le user-agent - $regex_match="/(mobile|nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|"; - $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|"; - $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|"; - $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|"; - $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220"; - $regex_match.=")/i"; - - 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 0 !== preg_match('/(bot\/|sistrix|voilabot|slurp)/i', $useragent); -} - - -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 diff --git a/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php b/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php index 633ec9009984130ffd46b9a316734cb729dcc9dc..33e751c02ca1ee748075f336500162c929b22e19 100644 --- a/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php +++ b/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php @@ -1630,6 +1630,33 @@ abstract class NoticeAjaxControllerCvsSearchTestCase extends AbstractControllerT + +class NoticeAjaxControllerCvsSearchFromGooglebotTest extends NoticeAjaxControllerCvsSearchTestCase { + protected $_backup_useragent; + + public function setUp() { + parent::setUp(); + $_SERVER['HTTP_USER_AGENT'] = 'Googlebot/2.1'; + } + + + + /** @test */ + public function responseShouldBeEmpty() { + $this->dispatch('/opac/noticeajax/cvs-search/expressionRecherche/Cuisson', true); + $this->assertEmpty($this->_response->getBody()); + } + + + public function tearDown() { + unset($_SERVER['HTTP_USER_AGENT']); + parent::tearDown(); + } +} + + + + class NoticeAjaxControllerCvsSearchWithNoRecordTest extends NoticeAjaxControllerCvsSearchTestCase { public function setUp() { diff --git a/tests/library/fonctions/UserAgentTest.php b/tests/library/fonctions/UserAgentTest.php index daa5808bf06f2f020e5cbdd8b8b106b85646659f..4c8c213fb3428b010d9f164178b767737fab4e60 100644 --- a/tests/library/fonctions/UserAgentTest.php +++ b/tests/library/fonctions/UserAgentTest.php @@ -16,31 +16,31 @@ * * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE * along with BOKEH; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * 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)); + $this->assertFalse((new Class_UserAgent('googlebot/')) + ->isBotAndNotAllowedBetweenHours(date('H')-1, + date('H')+2)); } /** @test */ public function ifBotOutsideAllowedHoursShouldReturnTrue() { - $this->assertTrue(isUserAgentBotAndNotAllowedBetweenHours('googlebot/', - date('H')+1, - date('H')-2)); + $this->assertTrue((new Class_UserAgent('googlebot/')) + ->isBotAndNotAllowedBetweenHours(date('H')+1, + date('H')-2)); } /** @test */ public function ifNotBotOutsideAllowedHoursShouldReturnFalse() { - $this->assertFalse(isUserAgentBotAndNotAllowedBetweenHours('firefox', - date('H')+1, - date('H')+2)); + $this->assertFalse((new Class_UserAgent('firefox')) + ->isBotAndNotAllowedBetweenHours(date('H')+1, + date('H')+2)); } @@ -52,12 +52,12 @@ class BotUserAgentTest extends PHPUnit_Framework_TestCase { ]; } - /** + /** * @dataProvider botUserAgents - * @test + * @test */ public function isUserAgentShouldReturnTrueForAgent($agent) { - $this->assertTrue(isUserAgentBot($agent)); + $this->assertTrue((new Class_UserAgent($agent))->isBot()); } @@ -69,12 +69,12 @@ class BotUserAgentTest extends PHPUnit_Framework_TestCase { ]; } - /** + /** * @dataProvider browserUserAgents - * @test + * @test */ public function isUserAgentShouldReturnFalseForAgent($agent) { - $this->assertFalse(isUserAgentBot($agent)); + $this->assertFalse((new Class_UserAgent($agent))->isBot()); } }