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 Exception; |
||||
40 | use Pickle\Base\Abstracts; |
||||
41 | use Pickle\Base\Interfaces; |
||||
42 | |||||
43 | class Ini extends Abstracts\Engine\Ini implements Interfaces\Engine\Ini |
||||
44 | { |
||||
45 | public function __construct(Interfaces\Engine $php) |
||||
46 | { |
||||
47 | 1 | parent::__construct($php); |
|||
48 | |||||
49 | 1 | $this->setupPickleSectionPositions(); |
|||
50 | 1 | } |
|||
51 | |||||
52 | public function updatePickleSection(array $dlls_add, array $dlls_del = []) |
||||
53 | { |
||||
54 | 1 | $before = ''; |
|||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||||
55 | 1 | $after = ''; |
|||
56 | |||||
57 | 1 | $pickleSection = $this->rebuildPickleParts($this->getPickleSection(), $dlls_add, $dlls_del); |
|||
58 | |||||
59 | 1 | $before = substr($this->raw, 0, $this->pickleHeaderStartPos); |
|||
0 ignored issues
–
show
It seems like
$this->raw can also be of type boolean ; however, parameter $string of substr() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
60 | |||||
61 | /* If the footer end pos is < 0, there was no footer in php.ini. In this case the footer start pos |
||||
62 | means the end of the last extension directive after the header start, where the footer should be */ |
||||
63 | 1 | if ($this->pickleFooterEndPos > 0) { |
|||
64 | 1 | $after = substr($this->raw, $this->pickleFooterEndPos); |
|||
65 | } else { |
||||
66 | 1 | $after = substr($this->raw, $this->pickleFooterStartPos); |
|||
67 | } |
||||
68 | |||||
69 | 1 | $before = rtrim($before); |
|||
70 | 1 | $after = ltrim($after); |
|||
71 | |||||
72 | 1 | $this->raw = $before . "\n\n" . self::PICKLE_HEADER . "\n" . trim($pickleSection) . "\n" . self::PICKLE_FOOTER . "\n\n" . $after; |
|||
73 | 1 | if (!@file_put_contents($this->path, $this->raw)) { |
|||
74 | throw new Exception('Cannot update php.ini'); |
||||
75 | } |
||||
76 | 1 | } |
|||
77 | |||||
78 | /** |
||||
79 | * @param string $pickleSection |
||||
80 | * |
||||
81 | * @return string |
||||
82 | */ |
||||
83 | protected function rebuildPickleParts($pickleSection, array $dlls_add, array $dlls_del = []) |
||||
84 | { |
||||
85 | 1 | $lines = explode("\n", $pickleSection); |
|||
86 | 1 | $new = []; |
|||
87 | |||||
88 | /* First add the lines for exts that are requested to be added. */ |
||||
89 | 1 | foreach ($dlls_add as $dll) { |
|||
90 | 1 | $new[] = $this->buildDllIniLine($dll); |
|||
91 | } |
||||
92 | |||||
93 | /* Then, go over the existing lines, restore those that are not |
||||
94 | requested to be deleted and not already added. */ |
||||
95 | 1 | foreach ($lines as $l) { |
|||
96 | 1 | $l = trim($l); |
|||
97 | 1 | if (strpos($l, 'extension') !== 0) { |
|||
98 | 1 | continue; |
|||
99 | } |
||||
100 | 1 | [, $dllname] = explode('=', $l); |
|||
101 | |||||
102 | 1 | if (in_array(trim($dllname), $dlls_add)) { |
|||
103 | /* don't create a duplicated item */ |
||||
104 | 1 | continue; |
|||
105 | } |
||||
106 | 1 | if (in_array(trim($dllname), $dlls_del)) { |
|||
107 | /* don't restore as it should be deleted */ |
||||
108 | 1 | continue; |
|||
109 | } |
||||
110 | 1 | $new[] = $l; |
|||
111 | } |
||||
112 | |||||
113 | 1 | sort($new); |
|||
114 | |||||
115 | 1 | return implode("\n", $new); |
|||
116 | } |
||||
117 | |||||
118 | protected function setupPickleSectionPositions() |
||||
119 | { |
||||
120 | 1 | $posHeader = strpos($this->raw, self::PICKLE_HEADER); |
|||
0 ignored issues
–
show
It seems like
$this->raw can also be of type boolean ; however, parameter $haystack of strpos() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
121 | 1 | if ($posHeader === false) { |
|||
122 | /* no pickle section here yet */ |
||||
123 | 1 | $this->pickleHeaderStartPos = strlen($this->raw); |
|||
0 ignored issues
–
show
It seems like
$this->raw can also be of type boolean ; however, parameter $string of strlen() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
124 | |||||
125 | 1 | return; |
|||
126 | } |
||||
127 | |||||
128 | 1 | $this->pickleHeaderStartPos = $posHeader; |
|||
129 | 1 | $this->pickleHeaderEndPos = $this->pickleHeaderStartPos + strlen(self::PICKLE_HEADER); |
|||
130 | |||||
131 | 1 | $posFooter = strpos($this->raw, self::PICKLE_FOOTER); |
|||
132 | 1 | if ($posFooter === false) { |
|||
133 | /* This is bad, no end of section marker, will have to lookup. The strategy is |
||||
134 | - look for the last extension directve after the header |
||||
135 | - extension directives are expected to come one after another one per line |
||||
136 | - comments are not expected inbetveen |
||||
137 | - mark the next pos after the last extension directive as the footer pos |
||||
138 | */ |
||||
139 | 1 | $pos = $this->pickleHeaderEndPos; |
|||
140 | do { |
||||
141 | 1 | $pos = strpos($this->raw, 'extension', $pos); |
|||
142 | 1 | if ($pos !== false) { |
|||
143 | 1 | $this->pickleFooterStartPos = $pos; |
|||
144 | 1 | $pos++; |
|||
145 | } |
||||
146 | 1 | } while ($pos !== false); |
|||
147 | |||||
148 | 1 | $this->pickleFooterStartPos = strpos($this->raw, "\n", $this->pickleFooterStartPos); |
|||
149 | } else { |
||||
150 | 1 | $this->pickleFooterStartPos = $posFooter; |
|||
151 | 1 | $this->pickleFooterEndPos = $this->pickleFooterStartPos + strlen(self::PICKLE_FOOTER); |
|||
152 | } |
||||
153 | 1 | } |
|||
154 | |||||
155 | protected function buildDllIniLine($dll) |
||||
156 | { |
||||
157 | 1 | return 'extension=' . $dll; |
|||
158 | } |
||||
159 | } |
||||
160 | |||||
161 | /* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */ |
||||
162 |