Put the site name and room name in the top header bar
[citadel.git] / webcit-ng / util.c
1 //
2 // Utility functions
3 //
4 // Copyright (c) 1996-2018 by the citadel.org team
5 //
6 // This program is open source software.  It runs great on the
7 // Linux operating system (and probably elsewhere).  You can use,
8 // copy, and run it under the terms of the GNU General Public
9 // License version 3.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 #include "webcit.h"
17
18
19 // remove escaped strings from i.e. the url string (like %20 for blanks)
20 int unescape_input(char *buf) {
21         unsigned int a, b;
22         char hex[3];
23         long buflen;
24         long len;
25
26         buflen = strlen(buf);
27
28         while ((buflen > 0) && (isspace(buf[buflen - 1]))) {
29                 buf[buflen - 1] = 0;
30                 buflen--;
31         }
32
33         a = 0;
34         while (a < buflen) {
35                 if (buf[a] == '+')
36                         buf[a] = ' ';
37                 if (buf[a] == '%') {
38                         // don't let % chars through, rather truncate the input.
39                         if (a + 2 > buflen) {
40                                 buf[a] = '\0';
41                                 buflen = a;
42                         }
43                         else {
44                                 hex[0] = buf[a + 1];
45                                 hex[1] = buf[a + 2];
46                                 hex[2] = 0;
47                                 b = 0;
48                                 b = decode_hex(hex);
49                                 buf[a] = (char) b;
50                                 len = buflen - a - 2;
51                                 if (len > 0)
52                                         memmove(&buf[a + 1], &buf[a + 3], len);
53
54                                 buflen -= 2;
55                         }
56                 }
57                 a++;
58         }
59         return a;
60 }
61
62
63 // Supplied with a unix timestamp, generate a textual time/date stamp.
64 // Caller owns the returned memory.
65 char *http_datestring(time_t xtime) {
66
67         // HTTP Months - do not translate - these are not for human consumption
68         static char *httpdate_months[] = {
69                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
70                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
71         };
72
73         // HTTP Weekdays - do not translate - these are not for human consumption
74         static char *httpdate_weekdays[] = {
75                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
76         };
77
78         struct tm t;
79         long offset;
80         char offsign;
81         int n = 40;
82         char *buf = malloc(n);
83         if (!buf)
84                 return (NULL);
85
86         localtime_r(&xtime, &t);
87
88         // Convert "seconds west of GMT" to "hours/minutes offset"
89         offset = t.tm_gmtoff;
90         if (offset > 0) {
91                 offsign = '+';
92         } else {
93                 offset = 0L - offset;
94                 offsign = '-';
95         }
96         offset = ((offset / 3600) * 100) + (offset % 60);
97
98         snprintf(buf, n, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
99                  httpdate_weekdays[t.tm_wday],
100                  t.tm_mday, httpdate_months[t.tm_mon], t.tm_year + 1900, t.tm_hour, t.tm_min, t.tm_sec, offsign, offset);
101         return (buf);
102 }