war on old style
[citadel.git] / webcit-ng / server / util.c
1 // Utility functions
2 // Copyright (c) 1996-2022 by the citadel.org team
3 // This program is open source software.  Use, duplication, or disclosure are subject to the GNU General Public License v3.
4
5 #include "webcit.h"
6
7
8 // remove escaped strings from i.e. the url string (like %20 for blanks)
9 int unescape_input(char *buf) {
10         unsigned int a, b;
11         char hex[3];
12         long buflen;
13         long len;
14
15         buflen = strlen(buf);
16
17         while ((buflen > 0) && (isspace(buf[buflen - 1]))) {
18                 buf[buflen - 1] = 0;
19                 buflen--;
20         }
21
22         a = 0;
23         while (a < buflen) {
24                 if (buf[a] == '+')
25                         buf[a] = ' ';
26                 if (buf[a] == '%') {
27                         // don't let % chars through, rather truncate the input.
28                         if (a + 2 > buflen) {
29                                 buf[a] = '\0';
30                                 buflen = a;
31                         }
32                         else {
33                                 hex[0] = buf[a + 1];
34                                 hex[1] = buf[a + 2];
35                                 hex[2] = 0;
36                                 b = 0;
37                                 b = decode_hex(hex);
38                                 buf[a] = (char) b;
39                                 len = buflen - a - 2;
40                                 if (len > 0)
41                                         memmove(&buf[a + 1], &buf[a + 3], len);
42
43                                 buflen -= 2;
44                         }
45                 }
46                 a++;
47         }
48         return a;
49 }
50
51
52 // Supplied with a unix timestamp, generate a textual time/date stamp.
53 // Caller owns the returned memory.
54 char *http_datestring(time_t xtime) {
55
56         // HTTP Months - do not translate - these are not for human consumption
57         static char *httpdate_months[] = {
58                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
59                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
60         };
61
62         // HTTP Weekdays - do not translate - these are not for human consumption
63         static char *httpdate_weekdays[] = {
64                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
65         };
66
67         struct tm t;
68         long offset;
69         char offsign;
70         int n = 40;
71         char *buf = malloc(n);
72         if (!buf) {
73                 return (NULL);
74         }
75
76         localtime_r(&xtime, &t);
77
78         // Convert "seconds west of GMT" to "hours/minutes offset"
79         offset = t.tm_gmtoff;
80         if (offset > 0) {
81                 offsign = '+';
82         }
83         else {
84                 offset = 0L - offset;
85                 offsign = '-';
86         }
87         offset = ((offset / 3600) * 100) + (offset % 60);
88
89         snprintf(buf, n, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
90                  httpdate_weekdays[t.tm_wday],
91                  t.tm_mday, httpdate_months[t.tm_mon], t.tm_year + 1900, t.tm_hour, t.tm_min, t.tm_sec, offsign, offset);
92         return (buf);
93 }