xref: /web-master/fetch/events.php (revision 3f5dc40d)
1<?php
2
3require_once __DIR__ . '/../include/functions.inc';
4
5$valid_vars = ['token','cm','cy','cd','nm'];
6foreach($valid_vars as $k) {
7    if(isset($_GET[$k])) $$k = $_GET[$k];
8}
9
10# token required, since this should only get accessed from rsync.php.net
11if (!isset($_REQUEST['token']) || md5($_REQUEST['token']) != "19a3ec370affe2d899755f005e5cd90e")
12  die("token not correct.");
13
14db_connect();
15
16// Set default values
17if (!isset($cm)) $cm = (int)strftime('%m');
18if (!isset($cy)) $cy = (int)strftime('%Y');
19if (!isset($cd)) $cd = (int)strftime('%d');
20if (!isset($nm)) $nm = 3;
21
22// Fix sql injection. args must be integer
23$cm = (int) $cm;
24$cy = (int) $cy;
25$cd = (int) $cd;
26$nm = (int) $nm;
27
28// Collect events for $nm number of months
29while ($nm) {
30	for($cat=1; $cat<=3; $cat++) {
31        $entries = load_month($cy, $cm, $cat);
32        $last    = strftime('%e', mktime(12, 0, 0, $cm+1, 0, $cy));
33        for ($i = $cd; $i <= $last; $i++) {
34            if (isset($entries[$i]) && is_array($entries[$i])) {
35                foreach($entries[$i] as $row) {
36                    echo "$i,$cm,$cy," . '"' . $row['country'].'","' .
37                        addslashes($row['sdesc']) . '",' .
38                        $row['id'] . ',"' . base64_encode($row['ldesc']) . '","' .
39                        $row['url'] . '",' . $row['recur'] . ',' .
40                        $row['tipo'] . ',' . $row['sdato'] . ',' .
41                        $row['edato'] . ',' . $row['category'] . "\n";
42                }
43            }
44        }
45    }
46    $nm--;
47    $cd = 1;
48    if ($nm) {
49        $cm++;
50        if ($cm == 13) { $cy++; $cm = 1; }
51    }
52}
53
54/**
55 * Find the first, second, third, last, second-last etc. weekday of a month
56 *
57 * args: day   1 = Monday
58 *       which 1 = first
59 *             2 = second
60 *             3 = third
61 *             4 = fourth
62 *            -1 = last
63 *            -2 = second-last
64 *            -3 = third-last
65 *
66 * @param int $year
67 * @param int $month
68 * @param int $day
69 * @param int $which
70 */
71function weekday($year, $month, $day, $which)
72{
73    $ts = mktime(12, 0, 0, $month+(($which>0)?0:1), ($which>0)?1:0, $year);
74    $done = FALSE;
75    $match = 0;
76    $inc = 3600*24;
77    while (!$done) {
78        if (strftime('%w', $ts) == $day-1) {
79            $match++;
80        }
81        if ($match == abs($which)) { $done = TRUE; }
82        else { $ts += (($which>0)?1:-1)*$inc; }
83    }
84    return $ts;
85}
86
87// Get events for one month in one year to be listed
88function load_month($year, $month, $cat)
89{
90    // Empty events array
91    $events = [];
92
93    // Get approved events starting or ending in the
94    // specified year/month, and all recurring events
95    $result = db_query_safe(
96        "SELECT * FROM phpcal WHERE (
97            (
98                (MONTH(sdato) = ? OR MONTH(edato) = ?)
99                AND
100                (YEAR(sdato) = ? OR YEAR(edato) = ?)
101                AND tipo < 3
102            ) OR tipo = 3) AND category = ? AND approved = 1",
103        [$month, $month, $year, $year, $cat]
104    );
105
106    // Cannot get results, return with event's not found
107    if (!$result) { echo mysql_error(); return []; }
108
109    // Go through found events
110    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
111
112        switch($row['tipo']) {
113
114            // One day event
115            case 1:
116                list(, , $dd) = explode('-', $row['sdato']);
117                $events[(int)$dd][] = $row;
118            break;
119
120            // Multiple-day event
121            case 2:
122                list(, $mm, $dd) = explode('-', $row['sdato']);
123                list(, $m2, $d2) = explode('-', $row['edato']);
124                if ((int)$mm == (int)$m2) {
125                    for ($i = (int)$dd; $i <= (int)$d2; $i++) {
126                        $events[$i][] = $row;
127                    }
128                } elseif ((int)$mm == $month) {
129                    for ($i = (int)$dd; $i < 32; $i++) {
130                        $events[$i][] = $row;
131                    }
132                } else {
133                    for ($i = 1; $i <= (int)$d2; $i++) {
134                        $events[$i][] = $row;
135                    }
136                }
137                break;
138
139            // Recurring event
140            case 3:
141                list($which,$dd) = explode(':', $row['recur']);
142                $ts = weekday((int)$year, (int)$month, (int)$dd, (int)$which);
143                $events[(int)strftime('%d', $ts)][] = $row;
144                break;
145        }
146    }
147
148    // Return events found
149    return $events;
150}
151