-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
101 lines (92 loc) · 3.52 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
<?php
// Remove trailing slashes (if present), and add one manually.
// Note: This avoids a problem where some servers might add a trailing slash, and others not..
define('BASE_PATH', rtrim(realpath(dirname(__FILE__)), "/") . '/');
//require BASE_PATH . 'includes/global_functions.php';
//require BASE_PATH . 'includes/settings.php'; // Note. Include a file in same directory without slash in front of it!
//require BASE_PATH . 'lib/translator_class.php';
$category_json_file = 'category_data.json';
//require BASE_PATH . 'includes/dependency_checker.php';
// <<<<<<<<<<<<<<<<<<<<
// Validate the _GET category input for security and error handling
// >>>>>>>>>>>>>>>>>>>>
//$HTML_navigation = '<a href="index.php">Home</a>';
// <<<<<<<<<<<<<<<<<<<<
// Fetch categories, and include them in a HTML ul list
// >>>>>>>>>>>>>>>>>>>>
$settings = array();
$requested_category = 'Gallery';
$categories = list_directories();
if (count($categories) >= 1) {
$HTML_cup = '';
foreach ($categories as &$category_name) {
$category_preview_images = category_previews($category_name, $category_json_file);
// echo 'cats:'.$category_preview_images; // Testing category views
$HTML_cup .= '<a class="cardthumbnail" href="categories.php?category=' . $category_name . '"><div class="cardtext flexible">' . $category_preview_images . '</div>' . space_or_dash('-', $category_name) . '</a>' . "\n";
}
$HTML_cup .= '';
} else {
$HTML_cup = '<p>There are no categories yet...</p>';
}
//$HTML_navigation = '<div class="breadcrumbs">' . $HTML_navigation . '</div>';
// ====================
// Functions
// ====================
function space_or_dash($replace_this = '-', $in_this) {
if ($replace_this == '-') {
return preg_replace('/([-]+)/', ' ', $in_this);
} elseif ($replace_this == ' ') {
return preg_replace('/([ ]+)/', '-', $in_this);
}
}
function category_previews($category, $category_json_file)
{
$thumbs_directory = BASE_PATH . 'thumbnails/' . $category;
$previews_html = '';
if (file_exists($thumbs_directory)) {
if (file_exists($thumbs_directory . '/' . $category_json_file)) {
$category_data = json_decode(file_get_contents($thumbs_directory . '/' . $category_json_file), true);
$previews_html = '<img src="thumbnails/' . $category . '/' . rawurlencode($category_data['preview_image']) . '">';
} else {
// Automatically try to select preview image if none was choosen
$item_arr = array_diff(scandir($thumbs_directory), array('..', '.'));
foreach ($item_arr as $key => $value) {
$previews_html = '<img src="thumbnails/' . $category . '/' . rawurlencode($item_arr["$key"]) . '">'; // add a dot in front of = to return all images
}
$category_data = json_encode(array('preview_image' => $item_arr["$key"]));
file_put_contents($thumbs_directory . '/' . $category_json_file, $category_data);
}
}
return $previews_html;
}
function list_directories()
{
$item_arr = array_diff(scandir(BASE_PATH . 'gallery/'), array('..', '.'));
foreach ($item_arr as $key => $value) {
if (is_dir(BASE_PATH . 'gallery/' . $value) === false) {
unset($item_arr["$key"]);
}
}
return $item_arr;
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>PHP Photo Gallery</title>
<link rel="stylesheet" href="templates/default/gallery.css">
</head>
<body>
<header class="header">
<span class="logo">PHP Photo Gallery</span>
</header>
<div class="container">
<div class="catetory"><?php echo $requested_category; ?></div>
<div class="row-flex">
<?php echo $HTML_cup; ?>
</div>
</div>
</body>
</html>