-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
189 lines (156 loc) · 5.57 KB
/
functions.php
File metadata and controls
189 lines (156 loc) · 5.57 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
<?
// Establish database connection to SQL
$db = new DB\SQL('mysql:host=localhost;dbname='.$f3->get('DB_NAME'), $f3->get('DB_USER'), $f3->get('DB_PASS'));
$f3->set('db_events', new DB\SQL\Mapper($db, 'thcal'));
$f3->set('db_notes', new DB\SQL\Mapper($db, 'thcal_notes'));
$f3->set('db_todos', new DB\SQL\Mapper($db, 'thcal_todos'));
function logged_in() {
global $f3;
$db = new \DB\Jig ("data/");
$db_mapper = new \DB\Jig\Mapper($db, 'users');
$auth = new \Auth($db_mapper, array('id' => 'user', 'pw' => 'password'));
if ($f3->get('REQUEST.username') <> "") {
// User tries to log in
if($auth->login($f3->get('REQUEST.username'), $f3->get('REQUEST.password'))) {
// user/pw combination correct
$user = $db_mapper->load(array('@user=?', $f3->get('REQUEST.username')));
$f3->set('SESSION.token', $user['token']);
$f3->set('SESSION.user_level', $user['level']);
setcookie('userToken', $f3->get('SESSION.token'), time()+60*60*24*10);
return true;
} else {
// user/pw combination incorrect
$f3->set('SESSION.token', '');
$f3->set('SESSION.user_level', '');
echo Template::instance()->render('login_denied.html');
return false;
}
} elseif ($f3->get('SESSION.token') <> "") {
// A token is present.
if($db_mapper->count(array('@token=?', $f3->get('SESSION.token'))) == 1) {
// The token belongs to an actual user.
return true;
} else {
// The SESSION.token does not match the user DB.;
echo Template::instance()->render('session_expired.html');
return false;
}
} elseif ($f3->get('COOKIE.userToken') <> "") {
// User has a saved token cookie
if ($db_mapper->count(array('@token=?', $f3->get('COOKIE.userToken'))) == 1) {
// The token matches a user from the DB.
$f3->set('SESSION.token', $f3->get('COOKIE.userToken'));
$user = $db_mapper->load(array('@token=?', $f3->get('COOKIE.userToken')));
$f3->set('SESSION.user_level', $user['level']);
// Renew cookie.
setcookie('userToken', $f3->get('SESSION.token'), time()+60*60*24*10);
return true;
} else {
// The cookie was forged.
echo Template::instance()->render('session_expired.html');
return false;
}
} else {
// User is not logged in.
$f3->set('SESSION.user_level', '');
echo Template::instance()->render('session_expired.html');
return false;
}
}
function get_events_month() {
global $f3;
// Construct PREV and NEXT Link
$f3->set('next_month', date('Y-m', strtotime($f3->get('date')." +1 month")));
$f3->set('prev_month', date('Y-m', strtotime($f3->get('date')." -1 month")));
// Get entries from DB
$sql_limits = "date >= \"".$f3->get('date')."-01\" AND date < \"".$f3->get('next_month')."-01\"";
$f3->set('dates', $f3->get('db_events')->find($sql_limits, array("order" => "date ASC")));
}
function get_events_day() {
global $f3;
// Construct PREV and NEXT Link
$f3->set('next_day', date('Y-m-d', strtotime($f3->get('date')." +1 day")));
$f3->set('prev_day', date('Y-m-d', strtotime($f3->get('date')." -1 day")));
// Get entries from DB
$sql_limits = "date = \"".$f3->get('date')."\"";
$f3->set('dates', $f3->get('db_events')->find($sql_limits, array("order" => "showtime ASC")));
}
function get_events_search() {
global $f3;
// Get entries from DB
$s = "%".$f3->get('search')."%";
$f3->set('dates', $f3->get('db_events')->find(array('(veranstaltung LIKE ?) OR (bemerkung LIKE ?) OR (agentur LIKE ?) OR (tech_kontakt LIKE ?)', $s, $s, $s, $s), array("order" => "date ASC")));
}
function get_events_search_note() {
global $f3;
// Get entries from DB
$s = "%".$f3->get('search')."%";
$f3->set('dates', $f3->get('db_events')->find(array('veranstaltung LIKE ?', $s), array("order" => "date DESC")));
}
function get_notes_search() {
global $f3;
// Get entries from DB
$s = "%".$f3->get('search')."%";
$f3->set('notes', $f3->get('db_notes')->find(array('(title LIKE ?) OR (note LIKE ?)', $s, $s), array("order" => "date DESC")));
}
function get_todos_search() {
global $f3;
// Get entries from DB
$s = "%".$f3->get('search')."%";
$f3->set('todos', $f3->get('db_todos')->find(array('(title LIKE ?) OR (kommentar LIKE ?)', $s, $s), array("order" => "date DESC")));
}
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
$return = '';
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
return $return;
}
function get_notes() {
global $f3;
$f3->set('notes', $f3->get('db_notes')->find('', array("order" => "date DESC")));
}
function get_todos() {
global $f3;
$f3->set('todos', $f3->get('db_todos')->find('', array("order" => "status DESC")));
}
?>