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