-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_base.php
More file actions
192 lines (150 loc) · 4.95 KB
/
functions_base.php
File metadata and controls
192 lines (150 loc) · 4.95 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
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
191
192
<?php
/*** connect_db ***/
function connect_db(){
global $connection;
$config = include("config.php");
$connection = mysqli_connect($config["host"], $config["user"], $config["pass"], $config["db"]) or die("Can't connect to database - ".mysqli_error());
mysqli_query($connection, "SET CHARACTER SET UTF8") or die("Cannot set character set - ".mysqli_error($connection));
}
/*** login ***/
function login(){
global $connection;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['login'])){
// check if form fields are filled
if (empty($_POST['username'])){
$errors[] = 'Kasutajanimi on puudu';
}
if (empty($_POST['password'])){
$errors[] = 'Parool on puudu';
}
// if errors then do errors on login page
if (!empty($errors)){
include_once('views/login.php');
die();
}
else { // if no errors then try to login
$u = mysqli_real_escape_string($connection, $_POST['username']);
$p = mysqli_real_escape_string($connection, $_POST['password']);
$sql = "SELECT * FROM tvari_kodu_users WHERE BINARY name = '".$u."' AND password = SHA1('".$p."')";
$result = mysqli_query($connection, $sql);
if ($result && mysqli_num_rows($result) == 1){
$row = mysqli_fetch_assoc($result);
$_SESSION['username'] = $row['name'];
$_SESSION['role'] = $row['role'];
header("Location: ?page=start");
die();
}
else {
include_once('views/login.php');
}
}
}
else {
include_once('views/login.php');
}
}
/*** logout ***/
function logout(){
$_SESSION = array();
session_destroy();
header("Location: ?");
}
/*** check_admin_permissions ***/
function check_admin_permissions(){
if (!isset($_SESSION['username']) || !isset($_SESSION['role'])){
header("Location: ?page=login");
die();
}
if ($_SESSION['role'] != 'owner'){
header("Location: ?page=login");
die();
}
}
/*** fetch_categories ***/
function fetch_categories(){
global $connection;
$categories = array();
$sql = "SELECT id, category FROM tvari_kodu_categories ORDER BY category";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)){
$categories[] = $row;
}
return $categories;
}
/*** fetch_systems ***/
function fetch_systems(){
global $connection;
$systems = array();
$sql = "SELECT id, description FROM tvari_kodu_systems ORDER BY description";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)){
$systems[] = $row;
}
return $systems;
}
/*** fetch_borrowers ***/
function fetch_borrowers(){
global $connection;
$borrowers = array();
$sql = "SELECT id, name FROM tvari_kodu_users WHERE role = 'guest' ORDER BY name";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)){
$borrowers[] = $row;
}
return $borrowers;
}
/*** upload ***/
function upload($name, $year){
global $errors;
$allowedExts = array("jpg", "jpeg", "gif", "png");
$allowedTypes = array("image/gif", "image/jpeg", "image/png","image/pjpeg");
$tmp = explode('.', $_FILES[$name]["name"]);
$extension = end($tmp);
$file_name = "";
for ( $i = 0; $i < count($tmp) - 1; $i++){
$file_name = $file_name.$tmp[$i];
}
$file_name = sanitize_filename($file_name)."-".$year.".".$extension;
if ($_FILES[$name]["size"] > 200000){
$errors[] = "Fail on liiga suur, otsi pisem pilt (väiksem kui 200KB)";
return "";
}
if ( in_array($_FILES[$name]["type"], $allowedTypes)
&& in_array($extension, $allowedExts)) {
// fail õiget tüüpi ja suurusega
if ($_FILES[$name]["error"] > 0) {
$_SESSION['notices'][]= "Return Code: " . $_FILES[$name]["error"];
return "";
}
else {
// vigu ei ole
if (file_exists("covers/" .$file_name)) {
// fail olemas ära uuesti lae, tagasta failinimi
$_SESSION['notices'][]= $file_name. " juba eksisteerib. ";
return "covers/" .$file_name;
} else {
// kõik ok
move_uploaded_file($_FILES[$name]["tmp_name"], "covers/" .$file_name);
return "covers/" .$file_name;
}
}
} else {
return "";
}
}
/*** upload ***/
/*** https://stackoverflow.com/questions/2668854/sanitizing-strings-to-make-them-url-and-filename-safe ***/
function sanitize_filename($string)
{
$string = htmlentities($string, ENT_QUOTES, 'UTF-8');
$string = preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', $string);
$string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
$string = preg_replace(array('~[^0-9a-z]~i', '~[ -]+~'), '', $string);
return trim($string, ' -');
}
/*
echo "<pre>";
print_r($categories);
echo "</pre>";
*/
?>