From 971183c5f996729999343a9fe0fff749a75fbfd5 Mon Sep 17 00:00:00 2001 From: rmartin Date: Fri, 16 Dec 2022 12:10:01 +0100 Subject: [PATCH 1/4] add lizmap version to user_agent --- .../modules/lizmap/classes/lizmapServices.class.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lizmap/modules/lizmap/classes/lizmapServices.class.php b/lizmap/modules/lizmap/classes/lizmapServices.class.php index 4a18ba4083..5c73544498 100644 --- a/lizmap/modules/lizmap/classes/lizmapServices.class.php +++ b/lizmap/modules/lizmap/classes/lizmapServices.class.php @@ -281,14 +281,15 @@ public function __construct($readConfigPath, $globalConfig, $ldapEnabled, $varPa $this->allowUserAccountRequests = false; } - // set it for external requests, needed for file_get_contents - $userAgent = 'Lizmap'; + $appInfos = \Jelix\Core\Infos\AppInfos::load(); + // set user_agent for external requests, needed for file_get_contents + $userAgent = 'Lizmap '.$appInfos->version; if (isset($readConfigPath['services']['userAgent'])) { // may be set to false if already set in the php.ini $userAgent = $readConfigPath['services']['userAgent']; } - if ($userAgent) { - ini_set('user_agent', 'Lizmap'); + if ($userAgent && !ini_get('user_agent')) { + ini_set('user_agent', $userAgent); } } @@ -323,7 +324,7 @@ public function hideSensitiveProperties() { if (isset($this->data['hideSensitiveServicesProperties']) && $this->data['hideSensitiveServicesProperties'] != '0' - ) { + ) { return true; } From 2c0d163f552913f1e4755db9a6c606a8787998f6 Mon Sep 17 00:00:00 2001 From: rmartin Date: Fri, 16 Dec 2022 12:36:39 +0100 Subject: [PATCH 2/4] user_agent always in ini --- lizmap/modules/lizmap/lib/Request/Proxy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lizmap/modules/lizmap/lib/Request/Proxy.php b/lizmap/modules/lizmap/lib/Request/Proxy.php index 3b019f32be..9aeda04c0a 100644 --- a/lizmap/modules/lizmap/lib/Request/Proxy.php +++ b/lizmap/modules/lizmap/lib/Request/Proxy.php @@ -298,7 +298,7 @@ protected static function buildHeaders($url, $options) $options['headers'] = array_merge(array( 'Connection' => 'close', - 'User-Agent' => ini_get('user_agent') ?: 'Lizmap', + 'User-Agent' => ini_get('user_agent'), 'Accept' => '*/*', ), $options['headers']); From 2766624a400322e422368e1f7cba753bed065a71 Mon Sep 17 00:00:00 2001 From: rmartin Date: Tue, 10 Jan 2023 12:19:51 +0100 Subject: [PATCH 3/4] check version only when ua not set in conf cache lizmap version from xml to globalconfig --- lizmap/modules/lizmap/classes/lizmapServices.class.php | 6 ++++-- .../lizmapconfig/lizmapconfig.configcompiler.php | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lizmap/modules/lizmap/classes/lizmapServices.class.php b/lizmap/modules/lizmap/classes/lizmapServices.class.php index 5c73544498..4b157a0426 100644 --- a/lizmap/modules/lizmap/classes/lizmapServices.class.php +++ b/lizmap/modules/lizmap/classes/lizmapServices.class.php @@ -281,12 +281,14 @@ public function __construct($readConfigPath, $globalConfig, $ldapEnabled, $varPa $this->allowUserAccountRequests = false; } - $appInfos = \Jelix\Core\Infos\AppInfos::load(); // set user_agent for external requests, needed for file_get_contents - $userAgent = 'Lizmap '.$appInfos->version; if (isset($readConfigPath['services']['userAgent'])) { // may be set to false if already set in the php.ini $userAgent = $readConfigPath['services']['userAgent']; + } elseif (property_exists($globalConfig, 'lizmap')) { + $userAgent = $globalConfig->lizmap['version']; + } else { + $userAgent = 'lizmap'; } if ($userAgent && !ini_get('user_agent')) { ini_set('user_agent', $userAgent); diff --git a/lizmap/plugins/configcompiler/lizmapconfig/lizmapconfig.configcompiler.php b/lizmap/plugins/configcompiler/lizmapconfig/lizmapconfig.configcompiler.php index 42c4cf1594..681e6b8a0b 100644 --- a/lizmap/plugins/configcompiler/lizmapconfig/lizmapconfig.configcompiler.php +++ b/lizmap/plugins/configcompiler/lizmapconfig/lizmapconfig.configcompiler.php @@ -27,6 +27,13 @@ public function atStart($config) public function onModule($config, $moduleName, $modulePath, $xml) { + if ($moduleName == 'lizmap') { + // we store the version into the configuration file, it avoids + // to read it from the project.xml file, as it is an heavy process. + if (property_exists($config, 'lizmap')) { + $config->lizmap['version'] = (string) $xml->info->version; + } + } } public function atEnd($config) From c5437952ccbc002c3d8a1b746fabb108c3f7ec22 Mon Sep 17 00:00:00 2001 From: rmartin Date: Fri, 16 Dec 2022 12:54:17 +0100 Subject: [PATCH 4/4] [unit test] add lizmap version + force object type in lizmapServices --- tests/units/bootstrap.php | 6 ++++-- tests/units/classes/Project/ProjectTest.php | 2 +- tests/units/classes/lizmapServicesTest.php | 9 +++++---- tests/units/testslib/QgisProjectForTests.php | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/units/bootstrap.php b/tests/units/bootstrap.php index d8f29975c3..dd074642fd 100644 --- a/tests/units/bootstrap.php +++ b/tests/units/bootstrap.php @@ -9,8 +9,10 @@ } }); - - +// set user_agent with Lizmap Version +$appInfos = \Jelix\Core\Infos\AppInfos::load(); +$userAgent = 'Lizmap '.$appInfos->version; +ini_set('user_agent', $userAgent); jApp::setEnv('lizmaptests'); if (file_exists(jApp::tempPath())) { diff --git a/tests/units/classes/Project/ProjectTest.php b/tests/units/classes/Project/ProjectTest.php index c7b781e955..c830ba6d0c 100644 --- a/tests/units/classes/Project/ProjectTest.php +++ b/tests/units/classes/Project/ProjectTest.php @@ -84,7 +84,7 @@ public function testGetRelativeQgisPath($relative, $root, $file, $expectedPath) array('services' => array('relativeWMSPath' => $relative, 'rootRepositories' => $root) - ), null, false, null, null); + ), (object) array(), false, null, null); $proj = new ProjectForTests(); $proj->setRepo(new Project\Repository(null, array('path' => ''), null, null, null)); $proj->setServices($services); diff --git a/tests/units/classes/lizmapServicesTest.php b/tests/units/classes/lizmapServicesTest.php index b7b1867a0c..f34a25b7f2 100644 --- a/tests/units/classes/lizmapServicesTest.php +++ b/tests/units/classes/lizmapServicesTest.php @@ -253,7 +253,7 @@ public function getModifyLocalData() */ public function testModifyLocal($localConfig, $newConfig, $changedProperty, $changedValue, $expectedReturnValue) { - $testLizmapServices = new LizmapServices($localConfig, null, false, '', null); + $testLizmapServices = new LizmapServices($localConfig, (object) array(), false, '', null); $this->assertEquals($expectedReturnValue, $testLizmapServices->modify($newConfig)); if (isset($changedProperty)) { $this->assertEquals($changedValue, $testLizmapServices->{$changedProperty}); @@ -327,7 +327,8 @@ public function testSaveIntoIni($dataModification, $expectedIniValues, $expected array('hideSensitiveServicesProperties' => $hide), (object) array( 'lizmap' => [ - 'setAdminContactEmailAsReplyTo' => false + 'setAdminContactEmailAsReplyTo' => false, + 'version' => 'unit-test-3' ] ), false, '', null); @@ -445,8 +446,8 @@ public function getMetricsEnabled() public function testGetMetricsEnabled($testValue, $expectedValue) { $ini_tab = array('hideSensitiveServicesProperties' => '0', - 'services' => array( - 'appName' => 'Lizmap' ), + 'services' => array( + 'appName' => 'Lizmap', ), ); if ($testValue !== null) { diff --git a/tests/units/testslib/QgisProjectForTests.php b/tests/units/testslib/QgisProjectForTests.php index a2072283fa..54d6c76f3f 100644 --- a/tests/units/testslib/QgisProjectForTests.php +++ b/tests/units/testslib/QgisProjectForTests.php @@ -7,7 +7,7 @@ class QgisProjectForTests extends QgisProject public function __construct($data = null) { if ($data) { - parent::__construct(null, new lizmapServices(null, null, false, '', ''), new ContextForTests(), $data); + parent::__construct(null, new lizmapServices(null, (object) array(), false, '', ''), new ContextForTests(), $data); } }