|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\UnderConstruction\Api; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Assets\Image; |
|
6
|
|
|
|
|
7
|
|
|
use SilverStripe\Control\Controller; |
|
8
|
|
|
|
|
9
|
|
|
use SilverStripe\Control\Director; |
|
10
|
|
|
|
|
11
|
|
|
use SilverStripe\Core\Config\Config; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use SilverStripe\ORM\ArrayList; |
|
15
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
use SilverStripe\View\ArrayData; |
|
18
|
|
|
use SilverStripe\View\Requirements; |
|
19
|
|
|
use SilverStripe\View\SSViewer; |
|
20
|
|
|
use SilverStripe\View\ViewableData; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
use Sunnysideup\UnderConstruction\Tasks\GoOffline; |
|
24
|
|
|
|
|
25
|
|
|
use Sunnysideup\UnderConstruction\Tasks\GoOnline; |
|
26
|
|
|
|
|
27
|
|
|
class CalculatedValues extends ViewableData |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private const UNDER_CONSTRUCTION_FOLDER_NAME = 'offline'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private const UNDER_CONSTRUCTION_FILE_NAME = 'offline.php'; |
|
38
|
|
|
|
|
39
|
|
|
protected $sc; |
|
40
|
|
|
|
|
41
|
|
|
private static $under_construction_bg_options = [ |
|
42
|
|
|
'#000' => 'black', |
|
43
|
|
|
'#222' => 'off black', |
|
44
|
|
|
'#ddd' => 'off white', |
|
45
|
|
|
'#fff' => 'white', |
|
46
|
|
|
'linear-gradient(to left, rgb(195, 20, 50), rgb(36, 11, 54))' => 'witching hour', |
|
47
|
|
|
'linear-gradient(to left, rgb(189, 195, 199), rgb(44, 62, 80))' => 'grade grey', |
|
48
|
|
|
'linear-gradient(to left, rgb(55, 59, 68), rgb(66, 134, 244))' => 'dark ocean', |
|
49
|
|
|
'linear-gradient(to left, rgb(30, 150, 0), rgb(255, 242, 0), rgb(255, 0, 0))' => 'rastafari', |
|
50
|
|
|
'linear-gradient(to left, rgb(253, 200, 48), rgb(243, 115, 53))' => 'citrus peel', |
|
51
|
|
|
'linear-gradient(to left, rgb(0, 0, 0), rgb(15, 155, 15))' => 'terminal', |
|
52
|
|
|
'linear-gradient(to left, rgb(0, 0, 70), rgb(28, 181, 224))' => 'vision of grandeur', |
|
53
|
|
|
'linear-gradient(to left, rgb(247, 151, 30), rgb(255, 210, 0))' => 'learning and leading', |
|
54
|
|
|
'linear-gradient(to left, rgb(93, 65, 87), rgb(168, 202, 186))' => 'forever lost', |
|
55
|
|
|
'linear-gradient(to left, rgb(0, 4, 40), rgb(0, 78, 146))' => 'frost', |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
private static $under_construction_fg_options = [ |
|
59
|
|
|
'#fff' => 'white', |
|
60
|
|
|
'#ddd' => 'off white', |
|
61
|
|
|
'#222' => 'off black', |
|
62
|
|
|
'#000' => 'black', |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
public function __construct(SiteConfig $siteConfig) |
|
66
|
|
|
{ |
|
67
|
|
|
parent::__construct(); |
|
68
|
|
|
$this->sc = $siteConfig; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public static function go_offline_link(): string |
|
72
|
|
|
{ |
|
73
|
|
|
return '/dev/tasks/' . Config::inst()->get(GoOffline::class, 'segment'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public static function go_online_link(): string |
|
77
|
|
|
{ |
|
78
|
|
|
return '/dev/tasks/' . Config::inst()->get(GoOnline::class, 'segment'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getSiteConfig(): SiteConfig |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->sc; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function CreateFiles() |
|
87
|
|
|
{ |
|
88
|
|
|
//create html |
|
89
|
|
|
if ($this->CreateDirAndTest()) { |
|
90
|
|
|
$this->CreateFilesInner(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function CreateDirAndTest(): bool |
|
95
|
|
|
{ |
|
96
|
|
|
$this->CreateFilesInner(); |
|
97
|
|
|
$dir = dirname($this->UnderConstructionFilePath()); |
|
98
|
|
|
@mkdir($dir); |
|
|
|
|
|
|
99
|
|
|
$messages = []; |
|
100
|
|
|
if (!file_exists($dir)) { |
|
101
|
|
|
$messages[] = 'Could not create offline folder (' . $this->UnderConstructionFolderName() . ').'; |
|
102
|
|
|
} |
|
103
|
|
|
if (!is_writable($dir)) { |
|
104
|
|
|
$messages[] = 'Could not writes files in offline folder (' . $this->UnderConstructionFolderName() . ').'; |
|
105
|
|
|
} |
|
106
|
|
|
if (!file_exists($this->getHtAccessPath())) { |
|
107
|
|
|
$messages[] = 'Could not find .htaccess file (' . $this->getHtAccessPath() . ').'; |
|
108
|
|
|
} |
|
109
|
|
|
if (!is_writable($this->getHtAccessPath())) { |
|
110
|
|
|
$messages[] = 'Could not write .htaccess file (' . $this->getHtAccessPath() . ').'; |
|
111
|
|
|
} |
|
112
|
|
|
if (!file_exists($this->UnderConstructionFilePath())) { |
|
113
|
|
|
$messages[] = 'Offline file does not exist yet (' . $this->UnderConstructionFilePath() . ').'; |
|
114
|
|
|
} |
|
115
|
|
|
if (!is_writable($this->UnderConstructionFilePath())) { |
|
116
|
|
|
$messages[] = 'Offline file can not be altered (' . $this->UnderConstructionFilePath() . ').'; |
|
117
|
|
|
} |
|
118
|
|
|
if (count($messages) === 0) { |
|
119
|
|
|
$outcome = true; |
|
120
|
|
|
$messages[] = 'All files are in order.'; |
|
121
|
|
|
} else { |
|
122
|
|
|
$outcome = false; |
|
123
|
|
|
} |
|
124
|
|
|
$this->sc->UnderConstructionOutcome = implode(' ', $messages); |
|
125
|
|
|
$this->sc->write(); |
|
126
|
|
|
|
|
127
|
|
|
return $outcome; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* arraylist of ips with two values: Ip and IpEscaped |
|
132
|
|
|
* @return ArrayList [description] |
|
133
|
|
|
*/ |
|
134
|
|
|
public function UnderConstructionIpAddresses(): ArrayList |
|
135
|
|
|
{ |
|
136
|
|
|
$array = explode(',', $this->sc->UnderConstructionExcludedIps); |
|
137
|
|
|
$al = ArrayList::create(); |
|
138
|
|
|
foreach ($array as $ip) { |
|
139
|
|
|
$ip = trim($ip); |
|
140
|
|
|
$ipEscaped = str_replace('.', '\\.', $ip); |
|
141
|
|
|
$al->push(ArrayData::create(['Ip' => $ip, 'IpEscaped' => $ipEscaped])); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $al; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* something like /var/www/mysite/public/offline/offline.php |
|
149
|
|
|
*/ |
|
150
|
|
|
public function UnderConstructionFilePath(): string |
|
151
|
|
|
{ |
|
152
|
|
|
return Controller::join_links( |
|
153
|
|
|
Director::baseFolder(), |
|
154
|
|
|
Director::publicDir(), |
|
155
|
|
|
$this->UnderConstructionFolderName(), |
|
156
|
|
|
$this->UnderConstructionFileName() |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* something like https://mysite.com/offline/offline.php. |
|
162
|
|
|
*/ |
|
163
|
|
|
public function UnderConstructionUrlPath(): string |
|
164
|
|
|
{ |
|
165
|
|
|
return Controller::join_links( |
|
166
|
|
|
Director::absoluteBaseURL(), |
|
167
|
|
|
$this->UnderConstructionFolderName(), |
|
168
|
|
|
$this->UnderConstructionFileName() |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* something like /var/www/mysite/public/offline/offline.php.img |
|
174
|
|
|
*/ |
|
175
|
|
|
public function UnderConstructionImagePath(): string |
|
176
|
|
|
{ |
|
177
|
|
|
$extension = $this->sc->UnderConstructionImage()->getExtension(); |
|
178
|
|
|
|
|
179
|
|
|
return $this->UnderConstructionFilePath() . '.' . $extension; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* something like offline |
|
184
|
|
|
*/ |
|
185
|
|
|
public function UnderConstructionFolderName(): string |
|
186
|
|
|
{ |
|
187
|
|
|
return self::UNDER_CONSTRUCTION_FOLDER_NAME; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* something like offline |
|
192
|
|
|
*/ |
|
193
|
|
|
public function UnderConstructionFileName(): string |
|
194
|
|
|
{ |
|
195
|
|
|
return self::UNDER_CONSTRUCTION_FILE_NAME; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* something like offline.php.png. |
|
200
|
|
|
*/ |
|
201
|
|
|
public function UnderConstructionImageName(): string |
|
202
|
|
|
{ |
|
203
|
|
|
$path = $this->UnderConstructionImagePath(); |
|
204
|
|
|
if (file_exists($path)) { |
|
205
|
|
|
return basename($path); |
|
206
|
|
|
} |
|
207
|
|
|
return ''; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* something like offline.php.png. |
|
212
|
|
|
*/ |
|
213
|
|
|
public function UnderConstructionOriginalImagePath(): string |
|
214
|
|
|
{ |
|
215
|
|
|
if ($this->sc->UnderConstructionImageID) { |
|
216
|
|
|
if ($this->sc->UnderConstructionImage()->exists()) { |
|
217
|
|
|
$name = $this->sc->UnderConstructionImage()->getFilename(); |
|
218
|
|
|
return Controller::join_links( |
|
219
|
|
|
Director::baseFolder(), |
|
220
|
|
|
Director::publicDir(), |
|
221
|
|
|
ASSETS_DIR, |
|
222
|
|
|
$name |
|
223
|
|
|
); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
return ''; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public function getHtAccessContent(): string |
|
230
|
|
|
{ |
|
231
|
|
|
// SSViewer::config()->update('theme_enabled', true); |
|
232
|
|
|
Requirements::clear(); |
|
233
|
|
|
Config::modify()->set(SSViewer::class, 'source_file_comments', false); |
|
234
|
|
|
$txt = $this->renderWith('Sunnysideup\\UnderConstruction\\UnderConstructionHtAccess'); |
|
235
|
|
|
// SSViewer::config()->update('theme_enabled', false); |
|
236
|
|
|
|
|
237
|
|
|
$array = explode(PHP_EOL, $txt); |
|
238
|
|
|
|
|
239
|
|
|
return PHP_EOL . implode(PHP_EOL, $array) . PHP_EOL; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function UnderConstructionIsReady(): bool |
|
243
|
|
|
{ |
|
244
|
|
|
return file_exists($this->UnderConstructionFilePath()); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function UnderConstructionForegroundColour() |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->sc->UnderConstructionForegroundColour ?: '#333'; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
public function UnderConstructionBackgroundColour() |
|
253
|
|
|
{ |
|
254
|
|
|
return $this->sc->UnderConstructionBackgroundColour ?: '#333'; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function getHtAccessPath(): string |
|
258
|
|
|
{ |
|
259
|
|
|
return Controller::join_links(Director::baseFolder(), Director::publicDir(), '.htaccess'); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
public function CreateFilesInner() |
|
263
|
|
|
{ |
|
264
|
|
|
// SSViewer::config()->update('theme_enabled', false); |
|
265
|
|
|
Config::modify()->set(SSViewer::class, 'source_file_comments', false); |
|
266
|
|
|
Requirements::clear(); |
|
267
|
|
|
$html = $this->renderWith('Sunnysideup\\UnderConstruction\\UnderConstructionPage'); |
|
268
|
|
|
// SSViewer::config()->update('theme_enabled', false); |
|
269
|
|
|
$fileName = $this->UnderConstructionFilePath(); |
|
270
|
|
|
if (file_exists($fileName)) { |
|
271
|
|
|
unlink($fileName); |
|
272
|
|
|
} |
|
273
|
|
|
//delete timestamp |
|
274
|
|
|
if (file_exists($fileName . '.txt')) { |
|
275
|
|
|
unlink($fileName . '.txt'); |
|
276
|
|
|
} |
|
277
|
|
|
//create image |
|
278
|
|
|
file_put_contents($fileName, $html); |
|
279
|
|
|
$image = $this->sc->UnderConstructionImage(); |
|
280
|
|
|
if ($image && $image->exists()) { |
|
281
|
|
|
$originalImagePath = $this->UnderConstructionOriginalImagePath(); |
|
282
|
|
|
$newImagePath = $this->UnderConstructionImagePath(); |
|
283
|
|
|
if (file_exists($newImagePath)) { |
|
284
|
|
|
unlink($newImagePath); |
|
285
|
|
|
} |
|
286
|
|
|
copy($originalImagePath, $newImagePath); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths