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

Commit c082a1a

Browse files
author
mattpass
committed
Adding, extending and fixing class methods in Settings.php
1 parent afd75f9 commit c082a1a

File tree

1 file changed

+129
-40
lines changed

1 file changed

+129
-40
lines changed

classes/Settings.php

Lines changed: 129 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,41 @@ public function __construct()
1111
$this->docRoot = $_SERVER['DOCUMENT_ROOT'];
1212
}
1313

14+
public function getDataDirDetails()
15+
{
16+
clearstatcache();
17+
18+
// Return details about the data dir
19+
$fullPath = dirname(__FILE__) . "/../data/";
20+
$exists = file_exists($fullPath);
21+
$readable = is_readable($fullPath);
22+
$writable = is_writable($fullPath);
23+
return [
24+
"fullPath" => $fullPath,
25+
"exists" => $exists,
26+
"readable" => $readable,
27+
"writable" => $writable,
28+
];
29+
}
30+
1431
public function getConfigGlobalFileDetails()
1532
{
33+
clearstatcache();
34+
1635
// Return details about the global config file
1736
$fileName = 'config-global.php';
1837
$fullPath = dirname(__FILE__) . "/../data/" . $fileName;
1938
$exists = file_exists($fullPath);
2039
$readable = is_readable($fullPath);
2140
$writable = is_writable($fullPath);
41+
$filemtime = filemtime($fullPath);
2242
return [
2343
"fileName" => $fileName,
2444
"fullPath" => $fullPath,
2545
"exists" => $exists,
2646
"readable" => $readable,
2747
"writable" => $writable,
48+
"filemtime" => $filemtime
2849
];
2950
}
3051

@@ -82,52 +103,96 @@ public function setConfigGlobalSettings($settings)
82103
}
83104
}
84105

85-
public function updateConfigCreateDate(): void
106+
public function getConfigUsersFileDetails($fileName)
86107
{
87-
global $settingsFile, $ICEcoderUserSettings;
108+
// Return details about the users config file
109+
$fullPath = dirname(__FILE__) . "/../data/" . $fileName;
110+
$exists = file_exists($fullPath);
111+
$readable = is_readable($fullPath);
112+
$writable = is_writable($fullPath);
113+
$filemtime = filemtime($fullPath);
114+
return [
115+
"fileName" => $fileName,
116+
"fullPath" => $fullPath,
117+
"exists" => $exists,
118+
"readable" => $readable,
119+
"writable" => $writable,
120+
"filemtime" => $filemtime,
121+
];
122+
}
88123

89-
$settingsContents = getData(dirname(__FILE__) . "/../data/" . $settingsFile);
90-
clearstatcache();
91-
$configfilemtime = filemtime(dirname(__FILE__) . "/../data/" . $settingsFile);
124+
public function getConfigUsersTemplate()
125+
{
126+
// Return the serialized users config template
127+
$fileName = 'template-config-users.php';
128+
$fullPath = dirname(__FILE__) . "/../lib/" . $fileName;
129+
if (function_exists('opcache_invalidate')) {
130+
opcache_invalidate($fullPath, true);
131+
}
132+
$settings = file_get_contents($fullPath);
133+
return $settings;
134+
}
135+
136+
public function getConfigUsersSettings($fileName)
137+
{
138+
// Get users config file details
139+
$fullPath = $this->getConfigUsersFileDetails($fileName)['fullPath'];
140+
// Load serialized data from the users config and convert to an array
141+
if (function_exists('opcache_invalidate')) {
142+
opcache_invalidate($fullPath, true);
143+
}
144+
$settingsFromFile = file_get_contents($fullPath);
145+
$settingsFromFile = str_replace("<?php\n/*\n\n", "", $settingsFromFile);
146+
$settingsFromFile = str_replace("\n\n*/\n?>", "", $settingsFromFile);
147+
$settingsFromFile = unserialize($settingsFromFile);
148+
// Now return
149+
return $settingsFromFile;
150+
}
151+
152+
public function setConfigUsersSettings($fileName, $settings)
153+
{
154+
// Get the users config file details
155+
$fullPath = $this->getConfigUsersFileDetails($fileName)['fullPath'];
156+
if ($fConfigSettings = fopen($fullPath, 'w')) {
157+
// If the settings we've received aren't in serialized format yet, do that now
158+
// As $settings could be a serialized string or array
159+
if (is_array($settings)) {
160+
$settings = "<?php\n/*\n\n" . serialize($settings) . "\n\n*/\n?" . ">";
161+
}
162+
// Now we have a serialized string, save it in the users config file
163+
fwrite($fConfigSettings, $settings);
164+
fclose($fConfigSettings);
165+
return true;
166+
} else {
167+
return false;
168+
}
169+
}
170+
171+
public function updateConfigUsersCreateDate($fileName): void
172+
{
173+
// Get users config file details
174+
$filemtime = $this->getConfigUsersFileDetails($fileName)['filemtime'];
92175
// Make it a number (avoids null, undefined etc)
93-
$configfilemtime = intval($configfilemtime);
176+
$filemtime = intval($filemtime);
94177
// Set it to the epoch time now if we don't have a real value
95-
if (0 === $configfilemtime) {
96-
$configfilemtime = time();
178+
if (0 === $filemtime) {
179+
$filemtime = time();
97180
}
98-
$settingsContents = str_replace('"configCreateDate" => 0,', '"configCreateDate" => ' . $configfilemtime . ',', $settingsContents);
99-
// Now update the config file
100-
if (!$fh = fopen(dirname(__FILE__) . "/../data/" . $settingsFile, 'w')) {
101-
$reqsPassed = false;
102-
$reqsFailures = ["phpUpdateSettings"];
103-
include dirname(__FILE__) . "/../lib/requirements.php";
104-
}
105-
fwrite($fh, $settingsContents);
106-
fclose($fh);
181+
// Update users config settings file
182+
$ICEcoderSettingsFromFile = $this->getConfigUsersSettings($fileName);
183+
$ICEcoderSettingsFromFile['configCreateDate'] = $filemtime;
184+
$this->setConfigUsersSettings($fileName, $ICEcoderSettingsFromFile);
107185
// Set the new value in array
108-
$ICEcoderUserSettings['configCreateDate'] = $configfilemtime;
109-
}
110-
111-
public function updatePasswordCheckUpdates(): void
112-
{
113-
global $settingsFile, $password;
114-
115-
$settingsContents = getData("../data/" . $settingsFile);
116-
// Replace our empty password with the one submitted by user
117-
$settingsContents = str_replace('"password" => "",','"password" => "' . $password . '",', $settingsContents);
118-
// Also set the update checker preference
119-
$checkUpdates = $_POST['checkUpdates'] == "true" ? "true" : "false";
120-
// once to cover the true setting, once to cover false
121-
$settingsContents = str_replace('"checkUpdates" => true,','"checkUpdates" => ' . $checkUpdates . ',', $settingsContents);
122-
$settingsContents = str_replace('"checkUpdates" => false,','"checkUpdates" => ' . $checkUpdates . ',', $settingsContents);
123-
// Now update the config file
124-
if (!$fh = fopen(dirname(__FILE__) . "/../data/" . $settingsFile, 'w')) {
125-
$reqsPassed = false;
126-
$reqsFailures = ["phpUpdateSettings"];
127-
include(dirname(__FILE__) . "/../lib/requirements.php");
128-
}
129-
fwrite($fh, $settingsContents);
130-
fclose($fh);
186+
$ICEcoderUserSettings['configCreateDate'] = $filemtime;
187+
}
188+
189+
public function updatePasswordCheckUpdates($fileName, $password, $checkUpdates): void
190+
{
191+
// Update users config settings file
192+
$ICEcoderSettingsFromFile = $this->getConfigUsersSettings($fileName);
193+
$ICEcoderSettingsFromFile['password'] = $password;
194+
$ICEcoderSettingsFromFile['checkUpdates'] = $checkUpdates;
195+
$this->setConfigUsersSettings($fileName, $ICEcoderSettingsFromFile);
131196
}
132197

133198
public function createIPSettingsFileIfNotExist(): void
@@ -159,4 +224,28 @@ public function disableFurtherRegistration(): void
159224
$this->setConfigGlobalSettings($ICEcoderSettingsFromFile);
160225
}
161226
}
227+
228+
public function turnOffTutorialOnLogin($fileName): bool
229+
{
230+
// Update users config settings file
231+
$ICEcoderSettingsFromFile = $this->getConfigUsersSettings($fileName);
232+
$ICEcoderSettingsFromFile['tutorialOnLogin'] = false;
233+
return $this->setConfigUsersSettings($fileName, $ICEcoderSettingsFromFile);
234+
}
235+
236+
public function savePreviousFiles($fileName, $files): bool
237+
{
238+
// Update users config settings file
239+
$ICEcoderSettingsFromFile = $this->getConfigUsersSettings($fileName);
240+
$ICEcoderSettingsFromFile['previousFiles'] = $files;
241+
return $this->setConfigUsersSettings($fileName, $ICEcoderSettingsFromFile);
242+
}
243+
244+
public function savelast10Files($fileName, $files): bool
245+
{
246+
// Update users config settings file
247+
$ICEcoderSettingsFromFile = $this->getConfigUsersSettings($fileName);
248+
$ICEcoderSettingsFromFile['last10Files'] = $files;
249+
return $this->setConfigUsersSettings($fileName, $ICEcoderSettingsFromFile);
250+
}
162251
}

0 commit comments

Comments
 (0)