-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.bat
More file actions
448 lines (403 loc) · 15.6 KB
/
setup.bat
File metadata and controls
448 lines (403 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
@echo off
REM ==========================================================
REM CSS Refiner - Post-Clone Setup Script (Windows)
REM ==========================================================
REM
REM Run this after cloning to configure CSS Refiner.
REM
REM What it does:
REM 1. Renames "public/" to match your vhost (e.g. www, public_html)
REM 2. Renames "secure/" for obscurity (e.g. backend, engine)
REM 3. Sets a URL prefix/space (e.g. "space" -> http://domain/space/)
REM - Updates init.php constants automatically
REM
REM Usage:
REM setup.bat (interactive)
REM setup.bat www.example.com (rename public folder)
REM ==========================================================
setlocal enabledelayedexpansion
set "SCRIPT_DIR=%~dp0"
set "PUBLIC_DIR=%SCRIPT_DIR%public"
set "SECURE_DIR=%SCRIPT_DIR%secure"
set "PUBLIC_FOLDER_NAME=public"
set "SECURE_FOLDER_NAME=secure"
set "PUBLIC_SPACE="
REM Read existing config if available (re-run detection)
set "CONF_FILE=%SCRIPT_DIR%.cssrefiner.conf"
if exist "%CONF_FILE%" (
for /f "usebackq tokens=1,2 delims==" %%a in ("%CONF_FILE%") do (
if "%%a"=="PUBLIC_FOLDER_NAME" set "PUBLIC_FOLDER_NAME=%%b"
if "%%a"=="SECURE_FOLDER_NAME" set "SECURE_FOLDER_NAME=%%b"
if "%%a"=="PUBLIC_SPACE" set "PUBLIC_SPACE=%%b"
)
set "PUBLIC_DIR=!SCRIPT_DIR!!PUBLIC_FOLDER_NAME!"
set "SECURE_DIR=!SCRIPT_DIR!!SECURE_FOLDER_NAME!"
)
echo.
echo ========================================
echo CSS Refiner Setup (Windows)
echo ========================================
echo.
REM ==========================================================
REM Step 1: Rename public folder
REM ==========================================================
echo Step 1 - Public folder name
echo.
REM Auto-detect if configured folder doesn't exist (crash recovery)
if not exist "%PUBLIC_DIR%" (
for /d %%d in ("%SCRIPT_DIR%*") do (
if exist "%%d\init.php" (
set "PUBLIC_FOLDER_NAME=%%~nxd"
set "PUBLIC_DIR=!SCRIPT_DIR!!PUBLIC_FOLDER_NAME!"
)
)
)
set "NEW_PUBLIC_NAME=%~1"
if not "%NEW_PUBLIC_NAME%"=="" goto :do_public_check
echo Your vhost DocumentRoot should point to the public folder.
echo If your vhost expects a specific name (e.g. "www",
echo "www.example.com"), you can rename it now.
echo.
echo Current name: %PUBLIC_FOLDER_NAME%
echo.
set /p "NEW_PUBLIC_NAME= New name (Enter to keep '%PUBLIC_FOLDER_NAME%'): "
if "%NEW_PUBLIC_NAME%"=="" (
echo OK Keeping "%PUBLIC_FOLDER_NAME%"
echo.
goto :step2
)
:do_public_check
REM Validate
echo "%NEW_PUBLIC_NAME%" | findstr /r "[/\\]" >nul 2>&1
if %errorlevel% equ 0 (
echo X Error: folder name cannot contain slashes
goto :eof
)
if "%NEW_PUBLIC_NAME%"=="%SECURE_FOLDER_NAME%" (
echo X Error: cannot use the same name as the secure folder
goto :eof
)
if "%NEW_PUBLIC_NAME%"=="%PUBLIC_FOLDER_NAME%" (
echo OK Keeping "%PUBLIC_FOLDER_NAME%"
goto :step2
)
if not exist "%PUBLIC_DIR%" (
echo X Error: public folder "%PUBLIC_FOLDER_NAME%" not found
goto :eof
)
set "NEW_PUBLIC_DIR=%SCRIPT_DIR%%NEW_PUBLIC_NAME%"
if exist "%NEW_PUBLIC_DIR%" (
echo X Error: folder "%NEW_PUBLIC_NAME%" already exists
goto :eof
)
REM Do the rename
echo Renaming: public -^> %NEW_PUBLIC_NAME%
rename "%PUBLIC_DIR%" "%NEW_PUBLIC_NAME%"
if errorlevel 1 (
echo X Failed to rename. Check permissions or close open files.
goto :eof
)
echo + Renamed to %NEW_PUBLIC_NAME%
set "PUBLIC_DIR=%NEW_PUBLIC_DIR%"
set "PUBLIC_FOLDER_NAME=%NEW_PUBLIC_NAME%"
REM Update PUBLIC_FOLDER_NAME in init.php (account for URL space)
if not "%PUBLIC_SPACE%"=="" (
set "INIT_FILE=%SCRIPT_DIR%%NEW_PUBLIC_NAME%\%PUBLIC_SPACE%\init.php"
) else (
set "INIT_FILE=%SCRIPT_DIR%%NEW_PUBLIC_NAME%\init.php"
)
if not exist "!INIT_FILE!" goto :step2
call :update_init_constant "!INIT_FILE!" "PUBLIC_FOLDER_NAME" "%NEW_PUBLIC_NAME%"
REM Save config after step 1 (crash recovery)
(
echo PUBLIC_FOLDER_NAME=%PUBLIC_FOLDER_NAME%
echo SECURE_FOLDER_NAME=%SECURE_FOLDER_NAME%
echo PUBLIC_SPACE=%PUBLIC_SPACE%
) > "%CONF_FILE%"
echo.
REM ==========================================================
REM Step 2: Rename secure folder
REM ==========================================================
:step2
REM Save config after step 1 even if public wasn't renamed
if not exist "%CONF_FILE%" (
(
echo PUBLIC_FOLDER_NAME=%PUBLIC_FOLDER_NAME%
echo SECURE_FOLDER_NAME=%SECURE_FOLDER_NAME%
echo PUBLIC_SPACE=%PUBLIC_SPACE%
) > "%CONF_FILE%"
)
echo.
echo Step 2 - Secure folder name
echo.
REM Detect current secure folder (may already be renamed)
if not exist "%SECURE_DIR%" (
REM Try to find it via init.php
if not "%PUBLIC_SPACE%"=="" (
set "DETECT_INIT=%PUBLIC_DIR%\%PUBLIC_SPACE%\init.php"
) else (
set "DETECT_INIT=%PUBLIC_DIR%\init.php"
)
if exist "!DETECT_INIT!" (
for /f "tokens=2 delims='" %%a in ('findstr /c:"SECURE_FOLDER_NAME" "!DETECT_INIT!" 2^>nul') do (
if exist "%SCRIPT_DIR%%%a" (
set "SECURE_DIR=%SCRIPT_DIR%%%a"
set "SECURE_FOLDER_NAME=%%a"
)
)
)
)
echo The secure folder holds PHP logic and translations.
echo It sits outside the web root and is not accessible via HTTP.
echo Rename it for obscurity, or nest it in a subdirectory.
echo Examples: "backend", "engine", "private/cssrefiner"
echo.
echo Current name: %SECURE_FOLDER_NAME%
echo.
set "NEW_SECURE_NAME="
set /p "NEW_SECURE_NAME= New name (Enter to keep '%SECURE_FOLDER_NAME%'): "
if "%NEW_SECURE_NAME%"=="" (
echo OK Keeping "%SECURE_FOLDER_NAME%"
goto :step3
)
if "%NEW_SECURE_NAME%"=="%SECURE_FOLDER_NAME%" (
echo OK Keeping "%SECURE_FOLDER_NAME%"
goto :step3
)
REM Validate
if "%NEW_SECURE_NAME%"=="%PUBLIC_FOLDER_NAME%" (
echo X Error: cannot use the same name as the public folder
goto :eof
)
if not exist "%SECURE_DIR%" (
echo X Error: secure folder "%SECURE_FOLDER_NAME%" not found
goto :eof
)
REM Move via PowerShell (supports nested paths like private/cssrefiner)
echo Renaming: %SECURE_FOLDER_NAME% -^> %NEW_SECURE_NAME%
set "PS_SEC_TEMP=%TEMP%\cr_setup_secure.ps1"
echo $src = '%SECURE_DIR%' -replace '/','\' > "%PS_SEC_TEMP%"
echo $name = '%NEW_SECURE_NAME%' -replace '\\','/' >> "%PS_SEC_TEMP%"
echo $root = '%SCRIPT_DIR%'.TrimEnd('\') >> "%PS_SEC_TEMP%"
echo $dest = Join-Path $root ($name -replace '/','\') >> "%PS_SEC_TEMP%"
echo if (Test-Path $dest) { >> "%PS_SEC_TEMP%"
echo if ($src.StartsWith($dest + '\')) { >> "%PS_SEC_TEMP%"
echo } else { >> "%PS_SEC_TEMP%"
echo Write-Host ' X Error: destination already exists'; exit 1 >> "%PS_SEC_TEMP%"
echo } >> "%PS_SEC_TEMP%"
echo } >> "%PS_SEC_TEMP%"
echo $segments = ($name -split '/').Count >> "%PS_SEC_TEMP%"
echo if ($segments -gt 5) { Write-Host ' X Error: path too deep (max 5 levels)'; exit 1 } >> "%PS_SEC_TEMP%"
echo $parent = Split-Path $dest >> "%PS_SEC_TEMP%"
echo if ($src.StartsWith($dest + '\')) { >> "%PS_SEC_TEMP%"
echo $tmp = Join-Path $root '.secure_move_tmp' >> "%PS_SEC_TEMP%"
echo Move-Item $src $tmp >> "%PS_SEC_TEMP%"
echo $cleanDir = Split-Path $src >> "%PS_SEC_TEMP%"
echo while ($cleanDir -ne $root -and (Test-Path $cleanDir)) { >> "%PS_SEC_TEMP%"
echo if ((Get-ChildItem $cleanDir -Force).Count -eq 0) { >> "%PS_SEC_TEMP%"
echo Remove-Item $cleanDir >> "%PS_SEC_TEMP%"
echo $cleanDir = Split-Path $cleanDir >> "%PS_SEC_TEMP%"
echo } else { break } >> "%PS_SEC_TEMP%"
echo } >> "%PS_SEC_TEMP%"
echo Move-Item $tmp $dest >> "%PS_SEC_TEMP%"
echo } elseif ($dest.StartsWith($src + '\')) { >> "%PS_SEC_TEMP%"
echo $tmp = Join-Path $root '.secure_move_tmp' >> "%PS_SEC_TEMP%"
echo Move-Item $src $tmp >> "%PS_SEC_TEMP%"
echo if ($parent) { New-Item -ItemType Directory -Path $parent -Force ^| Out-Null } >> "%PS_SEC_TEMP%"
echo Move-Item $tmp $dest >> "%PS_SEC_TEMP%"
echo } elseif ($parent -and -not (Test-Path $parent)) { >> "%PS_SEC_TEMP%"
echo New-Item -ItemType Directory -Path $parent -Force ^| Out-Null >> "%PS_SEC_TEMP%"
echo Move-Item $src $dest >> "%PS_SEC_TEMP%"
echo } else { >> "%PS_SEC_TEMP%"
echo Move-Item $src $dest >> "%PS_SEC_TEMP%"
echo } >> "%PS_SEC_TEMP%"
echo $oldParent = Split-Path $src >> "%PS_SEC_TEMP%"
echo while ($oldParent -ne $root -and (Test-Path $oldParent)) { >> "%PS_SEC_TEMP%"
echo if ((Get-ChildItem $oldParent -Force).Count -eq 0) { >> "%PS_SEC_TEMP%"
echo Remove-Item $oldParent >> "%PS_SEC_TEMP%"
echo $oldParent = Split-Path $oldParent >> "%PS_SEC_TEMP%"
echo } else { break } >> "%PS_SEC_TEMP%"
echo } >> "%PS_SEC_TEMP%"
echo Write-Host " + Renamed to $name" >> "%PS_SEC_TEMP%"
echo exit 0 >> "%PS_SEC_TEMP%"
powershell -NoProfile -ExecutionPolicy Bypass -File "%PS_SEC_TEMP%" 2>nul
if errorlevel 1 (
echo X Failed to rename. Check permissions or close open files.
del "%PS_SEC_TEMP%" 2>nul
goto :eof
)
del "%PS_SEC_TEMP%" 2>nul
set "SECURE_FOLDER_NAME=%NEW_SECURE_NAME%"
set "SECURE_DIR=%SCRIPT_DIR%%NEW_SECURE_NAME%"
REM Update SECURE_FOLDER_NAME in init.php (account for URL space)
if not "%PUBLIC_SPACE%"=="" (
set "INIT_FILE=%PUBLIC_DIR%\%PUBLIC_SPACE%\init.php"
) else (
set "INIT_FILE=%PUBLIC_DIR%\init.php"
)
if exist "!INIT_FILE!" (
call :update_init_constant "!INIT_FILE!" "SECURE_FOLDER_NAME" "%NEW_SECURE_NAME%"
)
REM Save config after step 2 (crash recovery)
(
echo PUBLIC_FOLDER_NAME=%PUBLIC_FOLDER_NAME%
echo SECURE_FOLDER_NAME=%SECURE_FOLDER_NAME%
echo PUBLIC_SPACE=%PUBLIC_SPACE%
) > "%CONF_FILE%"
echo.
REM ==========================================================
REM Step 3: Set URL space / prefix
REM ==========================================================
:step3
echo.
echo Step 3 - URL space / prefix
echo.
echo A URL space serves the app from a subdirectory.
echo Example: "space" makes the site http://domain/space/
if "%PUBLIC_SPACE%"=="" goto :step3_no_space
echo.
echo Current space: %PUBLIC_SPACE%
echo.
echo Enter a new space, type "none" to remove it,
echo or press Enter to keep the current space.
echo.
set "SPACE_INPUT="
set /p "SPACE_INPUT= Space: "
if "!SPACE_INPUT!"=="" (
echo OK Keeping "%PUBLIC_SPACE%"
goto :done
)
if /i "!SPACE_INPUT!"=="none" (
set "DESIRED_SPACE="
goto :step3_apply
)
if "!SPACE_INPUT!"=="%PUBLIC_SPACE%" (
echo OK Keeping "%PUBLIC_SPACE%"
goto :done
)
set "DESIRED_SPACE=!SPACE_INPUT!"
goto :step3_apply
:step3_no_space
echo.
echo No space currently set.
echo Press Enter to skip, or enter a space name.
echo.
set "SPACE_INPUT="
set /p "SPACE_INPUT= Space (Enter for none): "
if "!SPACE_INPUT!"=="" (
echo OK No space - serving from root
goto :done
)
set "DESIRED_SPACE=!SPACE_INPUT!"
:step3_apply
REM Apply space change via PowerShell
set "PS_SPACE_TEMP=%TEMP%\cr_setup_space.ps1"
echo $publicDir = '%PUBLIC_DIR%' > "%PS_SPACE_TEMP%"
echo $oldSpace = '%PUBLIC_SPACE%' >> "%PS_SPACE_TEMP%"
echo $newSpace = '!DESIRED_SPACE!' >> "%PS_SPACE_TEMP%"
echo $newSpace = $newSpace.Trim('/\') >> "%PS_SPACE_TEMP%"
echo. >> "%PS_SPACE_TEMP%"
echo # Validate new space >> "%PS_SPACE_TEMP%"
echo if ($newSpace -and $newSpace -notmatch '^[a-zA-Z0-9._/\-]+$') { >> "%PS_SPACE_TEMP%"
echo Write-Host ' X Error: invalid characters in space name' >> "%PS_SPACE_TEMP%"
echo Write-Host ' Allowed: a-z A-Z 0-9 . - _ /' >> "%PS_SPACE_TEMP%"
echo exit 1 >> "%PS_SPACE_TEMP%"
echo } >> "%PS_SPACE_TEMP%"
echo if ($newSpace -and ($newSpace -split '/').Count -gt 5) { >> "%PS_SPACE_TEMP%"
echo Write-Host ' X Error: path too deep (max 5 levels)' >> "%PS_SPACE_TEMP%"
echo exit 1 >> "%PS_SPACE_TEMP%"
echo } >> "%PS_SPACE_TEMP%"
echo. >> "%PS_SPACE_TEMP%"
echo # Remove old space (move files back to public root) >> "%PS_SPACE_TEMP%"
echo if ($oldSpace) { >> "%PS_SPACE_TEMP%"
echo $oldDir = Join-Path $publicDir ($oldSpace -replace '/','\' ) >> "%PS_SPACE_TEMP%"
echo if (Test-Path $oldDir) { >> "%PS_SPACE_TEMP%"
echo Get-ChildItem $oldDir -Force ^| ForEach-Object { Move-Item $_.FullName $publicDir -Force } >> "%PS_SPACE_TEMP%"
echo $top = ($oldSpace -split '/')[0] >> "%PS_SPACE_TEMP%"
echo Remove-Item (Join-Path $publicDir $top) -Recurse -Force -ErrorAction SilentlyContinue >> "%PS_SPACE_TEMP%"
echo } >> "%PS_SPACE_TEMP%"
echo } >> "%PS_SPACE_TEMP%"
echo. >> "%PS_SPACE_TEMP%"
echo # Set new space (move files into space dir) >> "%PS_SPACE_TEMP%"
echo if ($newSpace) { >> "%PS_SPACE_TEMP%"
echo $newDir = Join-Path $publicDir ($newSpace -replace '/','\') >> "%PS_SPACE_TEMP%"
echo if (Test-Path $newDir) { Write-Host " X Error: '$newSpace' already exists"; exit 1 } >> "%PS_SPACE_TEMP%"
echo New-Item -ItemType Directory -Path $newDir -Force ^| Out-Null >> "%PS_SPACE_TEMP%"
echo $top = ($newSpace -split '/')[0] >> "%PS_SPACE_TEMP%"
echo Get-ChildItem $publicDir -Force ^| Where-Object { $_.Name -ne $top } ^| ForEach-Object { >> "%PS_SPACE_TEMP%"
echo Move-Item $_.FullName $newDir -Force >> "%PS_SPACE_TEMP%"
echo } >> "%PS_SPACE_TEMP%"
echo } >> "%PS_SPACE_TEMP%"
echo. >> "%PS_SPACE_TEMP%"
echo # Update init.php >> "%PS_SPACE_TEMP%"
echo $initDir = if ($newSpace) { Join-Path $publicDir ($newSpace -replace '/','\') } else { $publicDir } >> "%PS_SPACE_TEMP%"
echo $initFile = Join-Path $initDir 'init.php' >> "%PS_SPACE_TEMP%"
echo if (Test-Path $initFile) { >> "%PS_SPACE_TEMP%"
echo $c = Get-Content $initFile -Raw >> "%PS_SPACE_TEMP%"
echo $c = $c -replace "define\('PUBLIC_FOLDER_SPACE',\s*'[^']*'\)", "define('PUBLIC_FOLDER_SPACE', '$newSpace')" >> "%PS_SPACE_TEMP%"
echo [IO.File]::WriteAllText($initFile, $c, [System.Text.Encoding]::UTF8) >> "%PS_SPACE_TEMP%"
echo } >> "%PS_SPACE_TEMP%"
echo. >> "%PS_SPACE_TEMP%"
echo if ($newSpace) { Write-Host " + Space set: $newSpace" } else { Write-Host ' + Space removed - serving from root' } >> "%PS_SPACE_TEMP%"
echo exit 0 >> "%PS_SPACE_TEMP%"
powershell -NoProfile -ExecutionPolicy Bypass -File "%PS_SPACE_TEMP%" 2>nul
if errorlevel 1 (
echo X Failed to update URL space
del "%PS_SPACE_TEMP%" 2>nul
goto :eof
)
set "PUBLIC_SPACE=!DESIRED_SPACE!"
del "%PS_SPACE_TEMP%" 2>nul
echo.
:done
REM Save config for re-run detection
(
echo PUBLIC_FOLDER_NAME=%PUBLIC_FOLDER_NAME%
echo SECURE_FOLDER_NAME=%SECURE_FOLDER_NAME%
echo PUBLIC_SPACE=%PUBLIC_SPACE%
) > "%CONF_FILE%"
echo.
echo ========================================
echo Setup complete
echo ========================================
echo.
echo Public folder: %PUBLIC_FOLDER_NAME%
echo Secure folder: %SECURE_FOLDER_NAME%
if not "%PUBLIC_SPACE%"=="" (
echo URL space: %PUBLIC_SPACE%
)
echo.
echo Next steps:
echo 1. Ensure your vhost DocumentRoot points to the '%PUBLIC_FOLDER_NAME%' folder
echo 2. Restart your web server
if not "%PUBLIC_SPACE%"=="" (
echo 3. Visit http://your-domain/%PUBLIC_SPACE%/
) else (
echo 3. Visit http://your-domain/
)
echo.
pause
goto :eof
REM ==========================================================
REM Helper: Update a define() constant in init.php
REM Usage: call :update_init_constant "file.php" "CONSTANT_NAME" "new_value"
REM ==========================================================
:update_init_constant
set "UIC_FILE=%~1"
set "UIC_NAME=%~2"
set "UIC_VALUE=%~3"
set "PS_TEMP=%TEMP%\cr_setup_const.ps1"
echo $f = '%UIC_FILE%' > "%PS_TEMP%"
echo $n = '%UIC_NAME%' >> "%PS_TEMP%"
echo $v = '%UIC_VALUE%' >> "%PS_TEMP%"
echo $c = Get-Content $f -Raw >> "%PS_TEMP%"
echo $c = $c -replace "define\('$n',\s*'[^']*'\)", "define('$n', '$v')" >> "%PS_TEMP%"
echo [IO.File]::WriteAllText($f, $c, [System.Text.Encoding]::UTF8) >> "%PS_TEMP%"
powershell -NoProfile -ExecutionPolicy Bypass -File "%PS_TEMP%" 2>nul
if errorlevel 1 (
echo ? Could not update %UIC_NAME% in init.php - edit manually
) else (
echo + Updated %UIC_NAME% in init.php
)
del "%PS_TEMP%" 2>nul
goto :eof