1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Pickle |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
* @license |
8
|
|
|
* |
9
|
|
|
* New BSD License |
10
|
|
|
* |
11
|
|
|
* Copyright © 2015-2015, Pickle community. All rights reserved. |
12
|
|
|
* |
13
|
|
|
* Redistribution and use in source and binary forms, with or without |
14
|
|
|
* modification, are permitted provided that the following conditions are met: |
15
|
|
|
* * Redistributions of source code must retain the above copyright |
16
|
|
|
* notice, this list of conditions and the following disclaimer. |
17
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
18
|
|
|
* notice, this list of conditions and the following disclaimer in the |
19
|
|
|
* documentation and/or other materials provided with the distribution. |
20
|
|
|
* * Neither the name of the Hoa nor the names of its contributors may be |
21
|
|
|
* used to endorse or promote products derived from this software without |
22
|
|
|
* specific prior written permission. |
23
|
|
|
* |
24
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
25
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
26
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
27
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE |
28
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
29
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
30
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
31
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
32
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
33
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
namespace Pickle\Engine\PHP; |
38
|
|
|
|
39
|
|
|
use Pickle\Base\Interfaces; |
40
|
|
|
use Pickle\Base\Abstracts; |
41
|
|
|
|
42
|
|
|
class Ini extends Abstracts\Engine\Ini implements Interfaces\Engine\Ini |
43
|
|
|
{ |
44
|
|
|
public function __construct(Interfaces\Engine $php) |
45
|
|
|
{ |
46
|
1 |
|
parent::__construct($php); |
47
|
|
|
|
48
|
1 |
|
$this->setupPickleSectionPositions(); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $pickleSection |
53
|
|
|
* @param array $dlls |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
protected function rebuildPickleParts($pickleSection, array $dlls) |
58
|
|
|
{ |
59
|
1 |
|
$lines = explode("\n", $pickleSection); |
60
|
1 |
|
$new = []; |
61
|
1 |
|
foreach ($lines as $l) { |
62
|
1 |
|
$l = trim($l); |
63
|
1 |
|
if (0 !== strpos($l, 'extension')) { |
64
|
1 |
|
continue; |
65
|
|
|
} |
66
|
1 |
|
list(, $dllname) = explode('=', $l); |
67
|
1 |
|
if (in_array(trim($dllname), $dlls)) { |
68
|
1 |
|
continue; |
69
|
|
|
} |
70
|
1 |
|
$new[] = $l; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return implode("\n", $new); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function setupPickleSectionPositions() |
77
|
|
|
{ |
78
|
1 |
|
$posHeader = strpos($this->raw, self::PICKLE_HEADER); |
79
|
1 |
|
if (false === $posHeader) { |
80
|
|
|
/* no pickle section here yet */ |
81
|
1 |
|
$this->pickleHeaderStartPos = strlen($this->raw); |
82
|
|
|
|
83
|
1 |
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
$this->pickleHeaderStartPos = $posHeader; |
87
|
1 |
|
$this->pickleHeaderEndPos = $this->pickleHeaderStartPos + strlen(self::PICKLE_HEADER); |
88
|
|
|
|
89
|
1 |
|
$posFooter = strpos($this->raw, self::PICKLE_FOOTER); |
90
|
1 |
|
if (false === $posFooter) { |
91
|
|
|
/* This is bad, no end of section marker, will have to lookup. The strategy is |
92
|
|
|
- look for the last extension directve after the header |
93
|
|
|
- extension directives are expected to come one after another one per line |
94
|
|
|
- comments are not expected inbetveen |
95
|
|
|
- mark the next pos after the last extension directive as the footer pos |
96
|
|
|
*/ |
97
|
1 |
|
$pos = $this->pickleHeaderEndPos; |
98
|
|
|
do { |
99
|
1 |
|
$pos = strpos($this->raw, 'extension', $pos); |
100
|
1 |
|
if (false !== $pos) { |
101
|
1 |
|
$this->pickleFooterStartPos = $pos; |
102
|
1 |
|
$pos++; |
103
|
|
|
} |
104
|
1 |
|
} while (false !== $pos); |
105
|
|
|
|
106
|
1 |
|
$this->pickleFooterStartPos = strpos($this->raw, "\n", $this->pickleFooterStartPos); |
107
|
|
|
} else { |
108
|
1 |
|
$this->pickleFooterStartPos = $posFooter; |
109
|
1 |
|
$this->pickleFooterEndPos = $this->pickleFooterStartPos + strlen(self::PICKLE_FOOTER); |
110
|
|
|
} |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
public function updatePickleSection(array $dlls) |
114
|
|
|
{ |
115
|
1 |
|
$before = ''; |
116
|
1 |
|
$after = ''; |
117
|
|
|
|
118
|
1 |
|
$pickleSection = ''; |
119
|
1 |
|
foreach ($dlls as $dll) { |
120
|
1 |
|
$pickleSection .= 'extension='.$dll."\n"; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
if ($this->pickleHeaderStartPos > 0) { |
124
|
1 |
|
$pickleSection = $this->rebuildPickleParts($this->getPickleSection(), $dlls)."\n".$pickleSection; |
125
|
|
|
|
126
|
1 |
|
$before = substr($this->raw, 0, $this->pickleHeaderStartPos); |
127
|
|
|
|
128
|
|
|
/* If the footer end pos is < 0, there was no footer in php.ini. In this case the footer start pos |
129
|
|
|
means the end of the last extension directive after the header start, where the footer should be */ |
130
|
1 |
|
if ($this->pickleFooterEndPos > 0) { |
131
|
1 |
|
$after = substr($this->raw, $this->pickleFooterEndPos); |
132
|
|
|
} else { |
133
|
1 |
|
$after = substr($this->raw, $this->pickleFooterStartPos); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
$before = rtrim($before); |
137
|
1 |
|
$after = ltrim($after); |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
$this->raw = $before."\n\n".self::PICKLE_HEADER."\n".trim($pickleSection)."\n".self::PICKLE_FOOTER."\n\n".$after; |
141
|
1 |
|
if (!@file_put_contents($this->path, $this->raw)) { |
142
|
|
|
throw new \Exception('Cannot update php.ini'); |
143
|
|
|
} |
144
|
1 |
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */ |
148
|
|
|
|