Skip to content

Commit

Permalink
Merge pull request #3373 from nworr/lizmap-version-in-user-agent
Browse files Browse the repository at this point in the history
add lizmap version to proxy request
  • Loading branch information
rldhont authored Aug 21, 2023
2 parents 718b8b9 + c543795 commit 09d64ce
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 14 deletions.
13 changes: 8 additions & 5 deletions lizmap/modules/lizmap/classes/lizmapServices.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,17 @@ public function __construct($readConfigPath, $globalConfig, $ldapEnabled, $varPa
$this->allowUserAccountRequests = false;
}

// set it for external requests, needed for file_get_contents
$userAgent = 'Lizmap';
// set user_agent for external requests, needed for file_get_contents
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_set('user_agent', 'Lizmap');
if ($userAgent && !ini_get('user_agent')) {
ini_set('user_agent', $userAgent);
}
}

Expand Down Expand Up @@ -323,7 +326,7 @@ public function hideSensitiveProperties()
{
if (isset($this->data['hideSensitiveServicesProperties'])
&& $this->data['hideSensitiveServicesProperties'] != '0'
) {
) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion lizmap/modules/lizmap/lib/Request/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions tests/units/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
2 changes: 1 addition & 1 deletion tests/units/classes/Project/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions tests/units/classes/lizmapServicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/units/testslib/QgisProjectForTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 09d64ce

Please sign in to comment.