-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·146 lines (121 loc) · 4.24 KB
/
index.php
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
<?php
//password to access the entries
//change this to whatever you want it to be
//TODO: have the password set in an external file instead of in this file
$pass = "password";
//ensure proper time zone
//change this ot your timezone
date_default_timezone_set('UTC');
$entriesdir = "./entries/"; //location of the entries files
$entriesext = ".json"; //exntension of the entries file
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Daily Journal</title>
<link rel="stylesheet" type="text/css" href="./journal.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?php
//if a password is set or if the date and entry are set
if((array_key_exists("pass",$_POST) and $_POST['pass']==$pass) or (array_key_exists('date',$_POST) and array_key_exists("entry",$_POST))) {
//if data is present, write to the file
if(array_key_exists("date",$_POST) and array_key_exists("entry",$_POST)) {
//TODO: clean up inputs
$date = $_POST['date'];
$jsondata = array(
"entry" => $_POST['entry'],
"rating" => $_POST['rating']
);
//TODO: have it create a new month and/or year directory too
//ensure that the date is correctly formatted
if(DateTime::createFromFormat('Y-m-d', $date)) {
//if so, then write to the file
$y = substr($date,0,4);
$m = substr($date,5,2);
if(!is_dir($entriesdir.$y."/".$m)) {
mkdir($entriesdir.$y."/".$m,0777,true);
}
$entryfile = $entriesdir.$y."/".$m."/".$date.$entriesext;
$wrote = file_put_contents($entryfile,json_encode($jsondata));
if($wrote) {
echo '<p style="color: green;">Saved entry '.$date.'</p>';
} else {
echo '<p style="color: red;">Error saving entry '.$date.'</p>';
}
} else {
echo "<p>Incorrectly formatted date</p>";
var_dump($date);
exit();
}
}
//load the list of entries (after the write occurs)
//$years = glob($entriesdir."*",GLOB_ONLYDIR);
foreach(glob($entriesdir."*",GLOB_ONLYDIR) as $e) {
$years[] = basename($e);
}
if(array_key_exists('date',$_GET)) {
$displayeddate = $_GET['date'];
} else {
$displayeddate = date("Y-m-d");
}
$y = substr($displayeddate,0,4);
$m = substr($displayeddate,5,2);
//read the file if it exists, else use the placeholder text
$jsondata = array(
"entry" => "Today I ",
"rating" => 5
);
if(in_array($y,$years)) {
foreach(glob($entriesdir.$y."/"."*",GLOB_ONLYDIR) as $e) {
$months[] = basename($e);
}
if(in_array($m,$months)) {
$dates = glob($entriesdir.$y."/".$m."/"."*");
$entryfile = $entriesdir.$y."/".$m."/".$displayeddate.$entriesext;
if(file_exists($entryfile)) {
$jsondata = json_decode(file_get_contents($entryfile),true);
}
}
}
?>
<form name="journalEntry" method="post">
<input name="date" type="date" value="<?php echo $displayeddate; ?>" readonly title="Note: if the date is of a previous entry, it will overwrite it">
<br>
<p>What did you do today?</p>
<textarea name="entry" autofocus><?php echo $jsondata['entry']; ?></textarea>
<p>Rate Your Day</p>
<div class="stars">
<?php
for ($i=1;$i<=5;$i++) {
?>
<label class="star star-<?php echo $i; ?>" for="star-<?php echo $i;?>"></label>
<input class="star star-1" id="star-<?php echo $i;?>" value=<?php echo $i; ?> type="radio" name="rating" <?php if($i==$jsondata['rating']) { echo 'checked="checked"'; } ?>/>
<?php
}
?>
</div>
<br>
<button type="submit">Done</button>
</form>
<div class="break"></div>
<p><b>Previous Entries</b></p>
<?php
echo '<div id="prevents">';
echo '<div class="linkwrapper">';
foreach($years as $y) {
echo '<p><a href="javascript:;" onclick="getMonth(this);">'.$y.'</a></p><div class="linkwrapper"></div>';
}
echo '</div>';
echo '</div>';
echo '<script src="./journal.js"></script>';
} else {
//no password or incorrect password given
?>
<form name="pw" method="post">
<p id="passline">Password: <input name="pass" type="password" autofocus><button type="submit" >Submit</button></p>
</form>
<?php } ?>
</body>
</html>