-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
75 lines (59 loc) · 1.68 KB
/
functions.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
<?php
function getUsersList()
{
$users = include __DIR__ . '/users.php';
return $users;
}
assert(false !== (bool)getUsersList());
function existsUser($login): bool
{
$users = getUsersList();
return isset($users[$login]);
}
assert(true === existsUser('eug'));
assert(true === existsUser('tmp'));
assert(true === existsUser('admin'));
assert(false === existsUser('none'));
function checkPassword($login, $password): bool
{
if (existsUser($login)) {
$users = getUsersList();
return password_verify($password, $users[$login]);
}
return false;
}
assert(true === checkPassword('eug', '123'));
assert(true === checkPassword('tmp', 'qwerty'));
assert(true === checkPassword('admin', 'iphone'));
assert(false === checkPassword('none', 'test'));
assert(false === checkPassword('admin', 'test3'));
assert(false === checkPassword('none2', ''));
function getCurrentUser()
{
if (isset($_SESSION['user'])) {
$user = $_SESSION['user'];
if (existsUser($user)) {
return $user;
}
}
return null;
}
function writeLog($fileName, $userName, $imageName)
{
$date = date(DATE_ATOM);
$logString = [$date, $userName, 'save image', $imageName];
file_put_contents($fileName, implode(' | ', $logString) . PHP_EOL, FILE_APPEND);
}
function getImagesAtDir(string $pathToImagesFolder): array
{
$dirContents = scandir($pathToImagesFolder, SCANDIR_SORT_NONE);
$images = [];
foreach ($dirContents as $item) {
$fileType = mime_content_type($pathToImagesFolder . $item);
$isImage = strpos($fileType, 'image') === 0;
if ($isImage) {
$images[] = $item;
}
}
return $images;
}