-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileHandler.php
More file actions
64 lines (53 loc) · 1.78 KB
/
fileHandler.php
File metadata and controls
64 lines (53 loc) · 1.78 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
<?php
const siteName = "Test site with form";
const userFilesDirPath = "./userFiles/";
const sitePatternsDirPath = "./site-patterns/";
define("isPostDataReceived", isset($_POST['email']));
define("isGetDataReceived", isset($_GET['email']));
/**
* Make form section for the site
* @return array|bool|string
*/
function getSiteMainForm(): array|bool|string {
// if(isGetDataReceived) return ""; //TODO Must be uncommented
return file_get_contents(sitePatternsDirPath . "fileChecker/_fileChecker-form.html");
}
/**
* Make text content
* @return string
*/
function getTextContent(): string {
if(isGetDataReceived) return getFileContent();
if(isPostDataReceived) return saveUserData();
return "";
}
/**
* Get file content
* @return string
*/
function getFileContent(): string {
$fileName = userFilesDirPath . $_GET['email'] . ".txt";
if(!file_exists($fileName)) return "Данного файла не существует";
return nl2br(file_get_contents($fileName));
}
/**
* Make site
* @return array|bool|string
*/
function makeSiteFromPatterns(): array|bool|string {
$strFile = file_get_contents(sitePatternsDirPath . "fileChecker/_fileChecker.html");
return str_replace(["{%site-title%}", "{%main-form%}", "{%text-content%}"], [siteName, getSiteMainForm(), getTextContent()], $strFile);
}
/**
* Save user data to file
* @return string
*/
function saveUserData(): string {
$text = "";
if(!isPostDataReceived) return $text;
foreach ($_POST as $key => $value) $text .= $key . ": " . $value . "\n";
file_put_contents(userFilesDirPath . $_POST['email'] . ".txt", $text);
return "Данные " . $_POST['email'] . " успешно сохранены";
}
if (!(isPostDataReceived || isGetDataReceived)) return;
echo makeSiteFromPatterns();