Skip to content

Commit e16b05c

Browse files
Remove ini directives below PATH and HOST sections (#133)
1 parent 964adcd commit e16b05c

6 files changed

Lines changed: 64 additions & 2 deletions

File tree

src/XdebugHandler.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,20 @@ private function writeTmpIni(array $iniFiles, $tmpDir, &$error)
377377
}
378378

379379
$content = '';
380-
$regex = '/^\s*(zend_extension\s*=.*xdebug.*)$/mi';
380+
$sectionRegex = '/^\s*\[(?:PATH|HOST)\s*=/mi';
381+
$xdebugRegex = '/^\s*(zend_extension\s*=.*xdebug.*)$/mi';
381382

382383
foreach ($iniFiles as $file) {
383384
// Check for inaccessible ini files
384385
if (($data = @file_get_contents($file)) === false) {
385386
$error = 'Unable to read ini: '.$file;
386387
return false;
387388
}
388-
$content .= preg_replace($regex, ';$1', $data).PHP_EOL;
389+
// Check and remove directives after HOST and PATH sections
390+
if (preg_match($sectionRegex, $data, $matches, PREG_OFFSET_CAPTURE)) {
391+
$data = substr($data, 0, $matches[0][1]);
392+
}
393+
$content .= preg_replace($xdebugRegex, ';$1', $data).PHP_EOL;
389394
}
390395

391396
// Merge loaded settings into our ini content, if it is valid
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
;
2+
; This file is used for test mocking xdebug-handler, it is not read by php.
3+
;
4+
5+
cli.setting=CLI
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
;
2+
; This file is used for test mocking xdebug-handler, it is not read by php.
3+
;
4+
5+
; Directives below HOST ini section must be removed
6+
[Host= some/where]
7+
cgi.only.setting=HOST
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
;
2+
; This file is used for test mocking xdebug-handler, it is not read by php.
3+
;
4+
5+
; Directives below PATHini section must be removed
6+
[Path=some/where]
7+
cgi.only.setting=PATH

tests/Helpers/IniHelper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ public function setInaccessibleIni()
9090
$this->setEnvironment();
9191
}
9292

93+
public function setSectionInis($sectionName)
94+
{
95+
$this->files = array(
96+
$this->loadedIni,
97+
$this->scanDir.DIRECTORY_SEPARATOR.'section-first.ini',
98+
$this->scanDir.DIRECTORY_SEPARATOR.'section-'.$sectionName.'.ini',
99+
);
100+
101+
$this->setEnvironment();
102+
}
103+
93104
public function getIniFiles()
94105
{
95106
return $this->files;

tests/IniFilesTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,33 @@ public function testInaccessbleIni()
145145
$this->checkNoRestart($xdebug);
146146
}
147147

148+
/**
149+
* Tests that directives below HOST and PATH sections are removed
150+
*
151+
* @dataProvider iniSectionsProvider
152+
*/
153+
public function testIniSections($sectionName)
154+
{
155+
$ini = new IniHelper();
156+
$ini->setSectionInis($sectionName);
157+
158+
$loaded = true;
159+
$xdebug = PartialMock::createAndCheck($loaded);
160+
161+
$content = $this->getTmpIniContent($xdebug);
162+
$config = parse_ini_string($content);
163+
$this->assertArrayHasKey('cli.setting', $config);
164+
$this->assertArrayNotHasKey('cgi.only.setting', $config);
165+
}
166+
167+
public function iniSectionsProvider()
168+
{
169+
return array(
170+
'host-section' => array('host'),
171+
'path-section' => array('path'),
172+
);
173+
}
174+
148175
/**
149176
* Common method to get mocked tmp ini content
150177
*/

0 commit comments

Comments
 (0)