Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 5a81f43

Browse files
author
mattpass
committed
Move and use serializedFileData from class, remove dormant functions, code tidy
1 parent b0a524f commit 5a81f43

File tree

4 files changed

+43
-112
lines changed

4 files changed

+43
-112
lines changed

classes/Settings.php

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,7 @@ public function getConfigGlobalSettings()
7878
$settings['docRoot'] = $this->docRoot;
7979
// Get global config file details
8080
$fullPath = $this->getConfigGlobalFileDetails()['fullPath'];
81-
// Load serialized data from the global config and convert to an array
82-
if (function_exists('opcache_invalidate')) {
83-
opcache_invalidate($fullPath, true);
84-
}
85-
$settingsFromFile = file_get_contents($fullPath);
86-
$settingsFromFile = str_replace("<?php\n/*\n\n", "", $settingsFromFile);
87-
$settingsFromFile = str_replace("\n\n*/\n?>", "", $settingsFromFile);
88-
$settingsFromFile = unserialize($settingsFromFile);
81+
$settingsFromFile = $this->serializedFileData("get", $fullPath);
8982
// Merge that with the array we started with and return
9083
$settings = array_merge($settings, $settingsFromFile);
9184
return $settings;
@@ -101,12 +94,8 @@ public function setConfigGlobalSettings($settings)
10194
if (is_array($settings)) {
10295
unset($settings['versionNo']);
10396
unset($settings['docRoot']);
104-
$settings = "<?php\n/*\n\n" . serialize($settings) . "\n\n*/\n?" . ">";
10597
}
106-
// Now we have a serialized string, save it in the global config file
107-
fwrite($fConfigSettings, $settings);
108-
fclose($fConfigSettings);
109-
return true;
98+
return $this->serializedFileData("set", $fullPath, $settings);
11099
} else {
111100
return false;
112101
}
@@ -158,14 +147,7 @@ public function getConfigUsersSettings($fileName)
158147
{
159148
// Get users config file details
160149
$fullPath = $this->getConfigUsersFileDetails($fileName)['fullPath'];
161-
// Load serialized data from the users config and convert to an array
162-
if (function_exists('opcache_invalidate')) {
163-
opcache_invalidate($fullPath, true);
164-
}
165-
$settingsFromFile = file_get_contents($fullPath);
166-
$settingsFromFile = str_replace("<?php\n/*\n\n", "", $settingsFromFile);
167-
$settingsFromFile = str_replace("\n\n*/\n?>", "", $settingsFromFile);
168-
$settingsFromFile = unserialize($settingsFromFile);
150+
$settingsFromFile = $this->serializedFileData("get", $fullPath);
169151
// Now return
170152
return $settingsFromFile;
171153
}
@@ -175,15 +157,7 @@ public function setConfigUsersSettings($fileName, $settings)
175157
// Get the users config file details
176158
$fullPath = $this->getConfigUsersFileDetails($fileName)['fullPath'];
177159
if ($fConfigSettings = fopen($fullPath, 'w')) {
178-
// If the settings we've received aren't in serialized format yet, do that now
179-
// As $settings could be a serialized string or array
180-
if (is_array($settings)) {
181-
$settings = "<?php\n/*\n\n" . serialize($settings) . "\n\n*/\n?" . ">";
182-
}
183-
// Now we have a serialized string, save it in the users config file
184-
fwrite($fConfigSettings, $settings);
185-
fclose($fConfigSettings);
186-
return true;
160+
return $this->serializedFileData("set", $fullPath, $settings);
187161
} else {
188162
return false;
189163
}
@@ -234,4 +208,24 @@ public function createIPSettingsFileIfNotExist(): void
234208
}
235209
}
236210
}
211+
212+
public function serializedFileData($do, $fullPath, $output=null)
213+
{
214+
if ("get" === $do) {
215+
if (function_exists('opcache_invalidate')) {
216+
opcache_invalidate($fullPath, true);
217+
}
218+
$data = file_get_contents($fullPath);
219+
$data = str_replace("<"."?php\n/*\n\n", "", $data);
220+
$data = str_replace("\n\n*/\n?".">", "", $data);
221+
$data = unserialize($data);
222+
return $data;
223+
}
224+
if ("set" === $do) {
225+
if (true === is_array($output)) {
226+
$output = serialize($output);
227+
}
228+
return file_put_contents($fullPath, "<"."?php\n/*\n\n" . $output . "\n\n*/\n?" . ">");
229+
}
230+
}
237231
}

lib/ftp-manager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
die("FTP Manager now offline");
3+
24
include "headers.php";
35
include "settings.php";
46
$t = $text['ftp-manager'];

lib/indexer.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
$systemClass->invalidateOPCache($docRoot . $ICEcoderDir . "/data/index.php");
1515
$prevIndexData = file_get_contents($docRoot . $ICEcoderDir . "/data/index.php");
1616
if (false !== strpos($prevIndexData, "<?php")) {
17-
$prevIndexData = str_replace("<?php\n/*\n\n", "", $prevIndexData);
18-
$prevIndexData = str_replace("\n\n*/\n?>", "", $prevIndexData);
19-
$prevIndexData = unserialize($prevIndexData);
17+
$prevIndexData = $settingsClass->serializedFileData("get", $docRoot . $ICEcoderDir . "/data/index.php");
2018
}
2119
}
2220

@@ -212,10 +210,7 @@ function phpGrep($path, $base) {
212210
$systemClass->invalidateOPCache($docRoot . $ICEcoderDir . "/data/git-diff.php");
213211
$gitDiffData = file_get_contents($docRoot . $ICEcoderDir . "/data/git-diff.php");
214212
if (strpos($gitDiffData, "<?php") !== false) {
215-
$gitDiffData = str_replace("<?php\n/*\n\n", "", $gitDiffData);
216-
$gitDiffData = str_replace("\n\n*/\n?>", "", $gitDiffData);
217-
$gitDiffData = unserialize($gitDiffData);
218-
$output["gitDiff"] = $gitDiffData;
213+
$output["gitDiff"] = $settingsClass->serializedFileData("get", $docRoot . $ICEcoderDir . "/data/git-diff.php");
219214
}
220215
}
221216

@@ -225,15 +220,12 @@ function phpGrep($path, $base) {
225220
$systemClass->invalidateOPCache($docRoot . $ICEcoderDir . "/data/git-content.php");
226221
$gitContent = file_get_contents($docRoot . $ICEcoderDir . "/data/git-content.php");
227222
if (strpos($gitContent, "<?php") !== false) {
228-
$gitContent = str_replace("<?php\n/*\n\n", "", $gitContent);
229-
$gitContent = str_replace("\n\n*/\n?>", "", $gitContent);
230-
$gitContent = unserialize($gitContent);
231-
$output["gitContent"] = $gitContent;
223+
$output["gitContent"] = $settingsClass->serializedFileData("get", $docRoot . $ICEcoderDir . "/data/git-content.php");
232224
}
233225
}
234226

235227
// Store the serialized array in PHP comment block for next time
236-
file_put_contents($docRoot . $ICEcoderDir . "/data/index.php", "<?php\n/*\n\n" . serialize($output) . "\n\n*/\n?" . ">");
228+
$settingsClass->serializedFileData("set", $docRoot . $ICEcoderDir . "/data/index.php", $output);
237229
// Output what we have in our index...
238230
} else {
239231
$output = $prevIndexData;

lib/settings-common.php

Lines changed: 13 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,16 @@ function getData($url, $type='fopen', $dieMessage = false, $timeout = 60) {
129129
// Require a re-index dir/file data next time we index
130130
function requireReIndexNextTime() {
131131
// If we have a data/index.php file
132-
global $docRoot, $ICEcoderDir, $systemClass;
132+
global $docRoot, $ICEcoderDir, $settingsClass, $systemClass;
133133
if (true === file_exists($docRoot . $ICEcoderDir . "/data/index.php")) {
134134
// Get serialized array back out of PHP file inside a comment block as prevIndexData
135135
$systemClass->invalidateOPCache($docRoot . $ICEcoderDir . "/data/index.php");
136136
$prevIndexData = file_get_contents($docRoot . $ICEcoderDir . "/data/index.php");
137137
if (strpos($prevIndexData, "<?php") !== false) {
138-
$prevIndexData = str_replace("<?php\n/*\n\n", "", $prevIndexData);
139-
$prevIndexData = str_replace("\n\n*/\n?>", "", $prevIndexData);
140-
$prevIndexData = unserialize($prevIndexData);
141-
138+
$prevIndexData = $settingsClass->serializedFileData("get", $docRoot . $ICEcoderDir . "/data/index.php");
142139
// Set timestamp back to epoch to force a re-index next time
143140
$prevIndexData['timestamps']['indexed'] = 0;
144-
145-
file_put_contents($docRoot . $ICEcoderDir . "/data/index.php", "<?php\n/*\n\n".serialize($prevIndexData)."\n\n*/\n?" . ">");
141+
$settingsClass->serializedFileData("set", $docRoot . $ICEcoderDir . "/data/index.php", $prevIndexData);
146142
}
147143
}
148144
}
@@ -184,34 +180,34 @@ function numClean($var) {
184180
}
185181

186182
// Clean XSS attempts using different contexts
187-
function xssClean($data,$type) {
183+
function xssClean($data, $type) {
188184

189185
// === html ===
190-
if ($type == "html") {
186+
if ("html" === $type) {
191187
$bad = array("<", ">");
192188
$good = array("&lt;", "&gt;");
193189
}
194190

195191
// === style ===
196-
if ($type == "style") {
192+
if ("style" === $type) {
197193
$bad = array("<", ">", "\"", "'", "``", "(", ")", "&", "\\\\");
198194
$good = array("&lt;", "&gt;", "&quot;", "&apos;", "&grave;", "&lpar;", "&rpar;", "&amp;", "&bsol;");
199195
}
200196

201197
// === attribute ===
202-
if ($type == "attribute") {
198+
if ("attribute" === $type) {
203199
$bad = array("\"", "'", "``");
204200
$good = array("&quot;", "&apos;", "&grave;");
205201
}
206202

207203
// === script ===
208-
if ($type == "script") {
204+
if ("script" === $type) {
209205
$bad = array("<", ">", "\"", "'", "\\\\", "%", "&");
210206
$good = array("&lt;", "&gt;", "&quot;", "&apos;", "&bsol;", "&percnt;", "&amp;");
211207
}
212208

213209
// === url ===
214-
if ($type == "url") {
210+
if ("url" === $type) {
215211
if(preg_match("#^(?:(?:https?|ftp):{1})\/\/[^\"\s\\\\]*.[^\"\s\\\\]*$#iu", (string)$data, $match)) {
216212
return $match[0];
217213
} else {
@@ -223,13 +219,6 @@ function xssClean($data,$type) {
223219
return $output;
224220
}
225221

226-
227-
// Clean PHP code injection attempts
228-
function injClean($data) {
229-
$output = str_replace("(", "", str_replace(")", "", str_replace(";", "", $data)));
230-
return $output;
231-
}
232-
233222
// returns a UTF8 based string with any UFT8 BOM removed
234223
function toUTF8noBOM($string, $message = false) {
235224
global $text;
@@ -269,48 +258,18 @@ function toUTF8noBOM($string, $message = false) {
269258
return $string;
270259
}
271260

272-
// Polyfill for array_replace_recursive, which is in PHP 5.3+
273-
if (!function_exists('array_replace_recursive')) {
274-
function array_replace_recursive($base, $replacements) {
275-
foreach (array_slice(func_get_args(), 1) as $replacements) {
276-
$bref_stack = array(&$base);
277-
$head_stack = array($replacements);
278-
279-
do {
280-
end($bref_stack);
281-
282-
$bref = &$bref_stack[key($bref_stack)];
283-
$head = array_pop($head_stack);
284-
285-
unset($bref_stack[key($bref_stack)]);
286-
287-
foreach (array_keys($head) as $key) {
288-
if (isset($key, $bref) && is_array($bref[$key]) && is_array($head[$key])) {
289-
$bref_stack[] = &$bref[$key];
290-
$head_stack[] = $head[$key];
291-
} else {
292-
$bref[$key] = $head[$key];
293-
}
294-
}
295-
} while(count($head_stack));
296-
}
297-
298-
return $base;
299-
}
300-
}
301-
302261
// Get number of versions total for a file
303262
function getVersionsCount($fileLoc, $fileName) {
304263
global $context;
305264
$count = 0;
306-
$dateCounts = array();
307-
$backupDateDirs = array();
265+
$dateCounts = [];
266+
$backupDateDirs = [];
308267
// Establish the base, host and date dirs within...
309268
$backupDirBase = str_replace("\\", "/", dirname(__FILE__)) . "/../data/backups/";
310269
$backupDirHost = isset($ftpSite) ? parse_url($ftpSite, PHP_URL_HOST) : "localhost";
311270
// check if folder exists if local before enumerating contents
312-
if(!isset($ftpSite)) {
313-
if(is_dir($backupDirBase . $backupDirHost)) {
271+
if (false === isset($ftpSite)) {
272+
if (true === is_dir($backupDirBase . $backupDirHost)) {
314273
$backupDateDirs = scandir($backupDirBase . $backupDirHost, 1);
315274
}
316275
} else {
@@ -352,19 +311,3 @@ function getVersionsCount($fileLoc, $fileName) {
352311
"dateCounts" => $dateCounts
353312
);
354313
}
355-
356-
function serializedFileData($do, $path, $output=null) {
357-
global $systemClass;
358-
359-
if ($do === "get") {
360-
$systemClass->invalidateOPCache($path);
361-
$data = file_get_contents($path);
362-
$data = str_replace("<"."?php\n/*\n\n", "", $data);
363-
$data = str_replace("\n\n*/\n?".">", "", $data);
364-
$data = unserialize($data);
365-
return $data;
366-
}
367-
if ($do === "set") {
368-
file_put_contents($path, "<"."?php\n/*\n\n" . serialize($output) . "\n\n*/\n?" . ">");
369-
}
370-
}

0 commit comments

Comments
 (0)