-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
299 lines (251 loc) · 6.81 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* convert timeframe array to normal time array
*
* @param timestamp $lecture_date
* @param array $timeframe_time
* @return array
*/
function build_times_array_from_timeframe($lecture_date, $timeframe_time)
{
return array(0 => $timeframe_time[0],
1 => $timeframe_time[1],
2 => date('j.n.Y', $lecture_date),
3 => $timeframe_time[2],
4 => $timeframe_time[3],
5 => $timeframe_time[4],
6 => $timeframe_time[5],
7 => $timeframe_time[6],
8 => $timeframe_time[7],
9 => $timeframe_time[8],
10 => $timeframe_time[9],
11 => $timeframe_time[10]
);
}
/**
* insert time to lectures array
*
* @param array $lectures
* @param timestamp $current_time
* @param bool $old_items
* @param array $times
* @param string $subject
* @return boolean
*/
function insert_time(&$lectures, $current_time, $old_items, $times, $subject)
{
if (!strlen($times[6])) $times[6] = '00';
if (!strlen($times[10])) $times[10] = '00';
$start = strtotime($times[2].' '.$times[4].':'.$times[6]);
$end = strtotime($times[2].' '.$times[8].':'.$times[10]);
$year = (int)date('Y', $start);
$week = (int)date('W', $start);
$day = (int)date('w', $start);
$length = $end-$start;
if (!$start || !$end || !$week || !$year || !$length) return false;
if (!$old_items && $current_time > $end) return true;
$lectures[$year][$week][$day][$times[4]][$times[6]][$subject] = array('start_h' => $times[4],
'start_m' => $times[6],
'end_h' => $times[8],
'end_m' => $times[10],
'info' => $times[11],
'start' => $start,
'end' => $end,
'length' => $length,
);
return true;
}
/**
* Check if $day array has overlapping items with $item
*
* @param array $day
* @param array $item
* @return bool
*/
function items_in_day_array_overlap($day, $item)
{
foreach ($day as $hour)
{
if (items_in_hour_array_overlap($hour, $item)) return true;
}
return false;
}
/**
* Check if $hour array has overlapping times with $item
*
* @param array $hour
* @param array $item
* @return bool
*/
function items_in_hour_array_overlap($hour, $item)
{
foreach ($hour as $mu_h_minute => $mu_h_subjects)
{
foreach ($mu_h_subjects as $mu_h_subject => $mu_h_item)
{
if ($mu_h_item == $item) continue;
if (items_overlap($item, $mu_h_item)) return true;
}
}
return false;
}
/**
* check if items overlap
*
* @param array $item1
* @param array $item2
* @return bool
*/
function items_overlap($item1, $item2)
{
if ($item1['start'] < $item2['start'] && $item2['start'] < $item1['end']) return true;
if ($item2['start'] < $item1['start'] && $item1['start'] < $item2['end']) return true;
return false;
}
/**
* get class for hour slot
*
* @param int $reserved_space
* @param bool $red
* @param int $next_ending
* @param bool $subject
* @param int $extra_slots
* @return string
*/
function get_class_for_hour_slot($reserved_space, &$red, $next_ending, $subject = false, $extra_slots = false)
{
static $last_red = false;
static $last_red2 = false;
$red = (($red || ($last_red2 && $reserved_space > 0 )) && $reserved_space >= 0);
$class = ($reserved_space >= 0 || $subject) ? (($reserved_space > 0) ? 'subject_continues' : 'subject') : 'subject empty';
$class .= $red ? ' red' : '';
$class .= ($red && $next_ending == 0 && $reserved_space > 0) ? ' dashed' : '';
$class .= ($last_red && $red && $extra_slots !== false) ? ' dashed_top' : '';
$last_red2 = $last_red = $red;
if ($reserved_space <= 0) $last_red2 = false;
//echo $reserved_space.' '.$next_ending;
//echo $class;
return $class;
}
/**
* Get max hour
*
* @param array $week
*/
function get_max_hour($week)
{
$max = 0;
for ($day=1; $day<6; $day++)
{
if (!isset($week[$day])) continue;
for($hour=1; $hour<25; $hour++)
{
if (!isset($week[$day][$hour])) continue;
foreach ($week[$day][$hour] as $minute => $subjects)
{
if ($max < $hour) $max = $hour;
foreach ($subjects as $item)
{
if ($item['end_h'] > $max && $item['end_m'] != '00') $max = $item['end_h'];
if ($item['end_h'] > $max && $item['end_m'] == '00') $max = $item['end_h']-1;
}
}
}
}
return $max;
}
/**
* echo page break if needed
*
* @param int $week_counter
* @param int $max_hours
* @param int $page_hours
* @param bool $no_break
*/
function echo_page_break(&$week_counter, $max_hours, &$page_hours, &$no_break)
{
$page_hours += $max_hours;
if ($page_hours > 48)
{
echo '<span class="page_break "/>';
$week_counter = 1;
$page_hours = $max_hours;
}
else if ($week_counter++%3 === 0 && !$no_break)
{
echo '<span class="page_break "/>';
$page_hours = $max_hours;
}
else $no_break = false;
}
/**
* Authenticate user using http digest authentication
* (http://php.net/manual/en/features.http-auth.php)
*
*/
function authenticate()
{
global $valid_users, $l;
if (empty($_SERVER['PHP_AUTH_DIGEST']))
{
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Digest realm="'.$l->auth_realm.
'",qop="auth",nonce="'.uniqid().'",opaque="'.md5($l->auth_realm).'"');
die($l->auth_cancel);
}
if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) || !isset($valid_users[$data['username']])) die($l->auth_wrong_credentials);
// generate the valid response
$A1 = md5($data['username'] . ':' . $l->auth_realm . ':' . $valid_users[$data['username']]);
$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
if ($data['response'] != $valid_response) die($l->auth_wrong_credentials);
// ok, valid username & password
}
/**
* parse http basic authentication
* (http://php.net/manual/en/features.http-auth.php)
*
* @param string $txt
* @return array
*/
function http_digest_parse($txt)
{
// protect against missing data
$needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
$data = array();
$keys = implode('|', array_keys($needed_parts));
preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);
foreach ($matches as $m)
{
$data[$m[1]] = $m[3] ? $m[3] : $m[4];
unset($needed_parts[$m[1]]);
}
return $needed_parts ? false : $data;
}
/**
* echo html header
*
* @param string $file_prefix
*/
function echo_header($file_prefix = '')
{
echo <<<END
<html>
<head>
<title>Timetable</title>
<link href="{$file_prefix}style.css" rel="stylesheet" type="text/css">
</head>
<body>
END;
}
/**
* Echo html footer
*
*/
function echo_footer()
{
echo <<<END
</body>
</html>
END;
}