Commit e5200c17 authored by Ghislain Loas's avatar Ghislain Loas

Merge branch 'origin-stable' into origin-hotline-master

Conflicts:
	VERSIONS
	library/startup.php
parents 120d5d03 d74489cb
......@@ -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
......
......@@ -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');
......
<?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')));
......
......@@ -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();
......
<?php
/**
* Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
* 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
......@@ -18,46 +18,69 @@
* 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);
}
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;
}
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,
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);
}
}
function isUserAgentBotAndNotAllowedBetweenHours($agent, $start_hour, $end_hour) {
if (!isUserAgentBot($agent))
return false;
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);
$now = time();
$start = mktime($start_hour, 0, 0);
$end = mktime($end_hour, 0, 0);
return ($now < $start) && ($now > $end);
}
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
......@@ -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");
......
......@@ -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()));
}
......
......@@ -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
......@@ -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() {
......
......@@ -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());
}
}
......
Markdown is supported
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