-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
190 lines (170 loc) · 6.35 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
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
<?php
//setting varibles for config file, title, headings
session_start();
$config = fopen("config.php", "r");
$title = "";
$heading1 = "";
$heading2 = "";
$footer = "";
$preview_len = 300; //entry item preview length
$items = 5; //number of items to be displayed on main page
//$_SESSION['page']; current page on main page
//$_SESSION['num_of_pages']; total number of pages with items
$timeout = 600;//session timeout time
$_SESSION['last_activity'] = time(); //set 'last_activity' time to now
//checking for activity timeout to destroy session()
if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > $timeout)) {
// new array + destroy session
$_SESSION = array();
session_destroy();
}
//loop for reading varibles from config file
while (!feof($config)) {
$config_line = fgets($config);
if (match_config_line($config_line, "title:")) {
global $title;
$title = substr($config_line, 6);
} elseif (match_config_line($config_line, "heading1:")) {
global $heading1;
$heading1 = substr($config_line, 9);
} elseif (match_config_line($config_line, "heading2:")) {
global $heading2;
$heading2 = substr($config_line, 9);
} elseif (match_config_line($config_line, "footer:")) {
global $footer;
$footer = substr($config_line, 7);
} elseif (match_config_line($config_line, "preview_len:")) {
global $preview_len;
$temp = intval(substr($config_line, 12));
if ($temp >= 0 && $temp !== $preview_len) {
$preview_len = $temp;
}
} elseif (match_config_line($config_line, "items:")) {
global $items;
$temp = intval(substr($config_line, 6));
if ($temp >= 1 && $temp !== $items && $temp <= 1000) {
$items = $temp;
}
}
}
//setting $_SESSION varibles:
$_SESSION['page'] = isset($_GET['page']) ? $_GET['page'] : 1;//setting current page, getting it from get 'page'
if (empty($_SESSION['num_of_pages'])) {
$_SESSION['num_of_pages'] = ceil(number_of_files() / $items);
}
//function for searching for parameter in config file
function match_config_line($config_line, $config_parameter) {
if (strpos($config_line, $config_parameter) !== false || strpos($config_line, $config_parameter) === 0) {
return true;
}
return false;
}
//function counting number of files in pages directory, only .txt files!
function number_of_files() {
$count = 0;
$dir = "pages";
$files = glob($dir . "/*.txt");
foreach ($files as $file) {
$count++;
}
return $count;
}
//function is creating array with pages filenames and ordering them from newset to oldest by creation time
function create_pages_array() {
$_SESSION['pages_array'] = glob("pages/*.txt");
usort($_SESSION['pages_array'], function($a, $b) {
if (filectime($b) < filemtime($b)) {
return filectime($b) - filectime($a);
}
return filemtime($b) - filemtime($a);
}
);
}
//generating main content - showing header and description of $items items per page
function main_contents_preview($preview_len, $items) {
$first_index = ($_SESSION['page'] - 1) * $items;
//debug infos in this echo, they should be removed
//echo "
// <p>Page number {$_SESSION['page']} / {$_SESSION['num_of_pages']}</p>
// <p>First index: {$first_index}</p>
//";
if ($first_index <= count($_SESSION['pages_array'])) {
for ($i = $first_index; $i < $first_index + 5; $i++) {
if (!empty($_SESSION['pages_array'][$i])) {
entry_preview($preview_len, $i);
}
}
}
}
//navigation through pages
function navigation() {
for ($i = 1; $i <= ($_SESSION['num_of_pages']); $i++){
if ($_SESSION['page'] == $i) {
echo "<b>$i</b> ";
} else {
echo "<a href='index.php?page=$i'>$i </a>";
}
}
}
//function is generating single entry preview for main page; need to add limit of displaying number of numbers
function entry_preview($preview_len, $entry_index) {
$page_file = fopen($_SESSION['pages_array'][$entry_index], "r");
$preview_string = "";
$preview_body = "";
$letters_count = 0;
while(!feof($page_file)) {
$page_line = fgets($page_file);
if (match_config_line($page_line, "=title:")) {
$preview_string .= "<h3><a href=page.php?entry=" . prepare_link_to_get($entry_index) .">" . substr($page_line, 7) . "</a></h3>";
} elseif ($letters_count <= $preview_len) {
$preview_body .= $page_line;
$letters_count += strlen($page_line) ;
}
}
$preview_string .= substr($preview_body, 0, $preview_len);
echo $preview_string;
}
function prepare_link_to_get($index) {
$filename = $_SESSION['pages_array'][$index];
$filename = substr($filename, 6, -4);
return $filename;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<?php
//setting title
echo "<title>{$title}</title>";
?>
</head>
<body>
<h1 style="margin-top: 1.5em;"><?php echo $heading1; ?></h1>
<h2 style="margin-top: -0.5em;"><?php echo $heading2; ?></h2>
<!-- <debug>
<p>Number of files: <?php echo number_of_files(); ?></p>
<p>Number of pages: <?php echo $_SESSION['num_of_pages']; ?></p>
<p>Current page: <?php echo $_SESSION['page']; ?></p>
<p>Newest page: <?php create_pages_array();
echo $_SESSION['pages_array'][0]; ?></p>
</debug> -->
<main style="margin-top: 6em;">
<?php
main_contents_preview($preview_len, $items);
?>
</main>
<nav style="margin-top: 1.5em;">
<?php
navigation();
?>
</nav>
<footer style="margin-top: 10em;">
<?php
echo $footer;
?>
</footer>
</body>
</html>