00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
#ifdef WIN32
00020
#include <Config-win.h>
00021
static int getpid() {
return 0; }
00022
#endif
00023
#include <stdio.h>
00024
#include <stdlib.h>
00025
#include <string.h>
00026
#include <time.h>
00027
#include <mysql/mysql.h>
00028
#include <libmysqlwrapped.h>
00029
#ifndef WIN32
00030
#include <unistd.h>
00031
#endif
00032
#include <ctype.h>
00033
#include <stdarg.h>
00034
00035
#include "utils.h"
00036
#include "dbd.h"
00037
00038 void zap(
char *s,
char c)
00039 {
00040
int i;
00041
00042
for (i = 0; i < (
int)strlen(s); i++)
00043
if (s[i] == c)
00044 s[i] = 0;
00045 }
00046
00047 char *
fget(
char *s,
int l,FILE *f)
00048 {
00049
char *i = fgets(s,l,f);
00050 s[strlen(s) - 1] = 0;
00051
return i;
00052 }
00053
00054 char *
datetime(
void)
00055 {
00056
static char dt[40];
00057 time_t t = time(NULL);
00058
struct tm * tp = localtime(&t);
00059
00060 sprintf(dt,
"%04d-%02d-%02d %02d:%02d:%02d",
00061 tp -> tm_year + 1900,
00062 tp -> tm_mon + 1,
00063 tp -> tm_mday,
00064 tp -> tm_hour,
00065 tp -> tm_min,
00066 tp -> tm_sec);
00067
return dt;
00068 }
00069
00070 char *
expiredatetime(
void)
00071 {
00072
static char dt[80];
00073
static char *days[7] = {
"Sunday",
"Monday",
00074
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"};
00075
static char *months[12] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
00076
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dev"};
00077 time_t t = time(NULL);
00078
struct tm * tp = localtime(&t);
00079
00080 sprintf(dt,
"%s, %02d-%s-%04d %02d:%02d:%02d GMT",
00081 days[tp -> tm_wday],
00082 tp -> tm_mday,
00083 months[tp -> tm_mon],
00084 tp -> tm_year + 1910,
00085 tp -> tm_hour,
00086 tp -> tm_min,
00087 tp -> tm_sec);
00088
return dt;
00089 }
00090
00091
#ifndef WIN32
00092 void strlwr(
char *s)
00093 {
00094
int i = 0;
00095
while (s[i])
00096 {
00097
if (s[i] >=
'A' && s[i] <=
'Z')
00098 s[i] += 32;
00099 i++;
00100 }
00101 }
00102
#endif
00103
00104 void set_cookie(Cookies *cs,
char *n,
char *v)
00105 {
00106
00107
00108
00109
00110
00111
00112
00113
00114 cs -> replacevalue(n,v);
00115 }
00116
00117 void set_cookie(Cookies *cs,
char *n,
long v)
00118 {
00119
char slask[20];
00120
00121 sprintf(slask,
"%ld",v);
00122
set_cookie(cs,n,slask);
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
#ifndef WIN32
00155 void strupr(
char *s)
00156 {
00157
int i;
00158
00159
for (i = 0; i < (
int)strlen(s); i++)
00160
if (s[i] > 96 && s[i] <=
'z')
00161 s[i] -= 32;
00162 }
00163
#endif
00164
00165 void strupr(std::string& str)
00166 {
00167
for (size_t i = 0; i < str.size(); i++)
00168 {
00169
if (str[i] >=
'a' && str[i] <=
'z')
00170 {
00171 str[i] -= 32;
00172 }
00173 }
00174 }
00175
00176 int syscall(
char *s)
00177 {
00178
int r;
00179
00180
00181
00182
00183
00184 r = system(s);
00185
00186
00187
00188
return r;
00189 }
00190
00191 int name_ok(
char *s)
00192 {
00193
int r = 0;
00194
int i;
00195
00196
if (isalpha(*s) || *s ==
'_')
00197 {
00198 r = 1;
00199
for (i = 0; i < (
int)strlen(s); i++)
00200
if (!isalnum(s[i]) && s[i] !=
'_')
00201
return 0;
00202 }
00203
return r;
00204 }
00205
00206
00207
00208
00209
00210 void textareain(
char *s)
00211 {
00212
char *s2 =
new char[strlen(s) + 1440];
00213
int i;
00214 fflush(
out);
00215
00216 *s2 = 0;
00217
for (i = 0; i < (
int)strlen(s); i++)
00218 {
00219
switch ( (
unsigned char)s[i])
00220 {
00221
case 0xe5:
00222 strcat(s2,
"å");
00223
break;
00224
case 0xe4:
00225 strcat(s2,
"ä");
00226
break;
00227
case 0xf6:
00228 strcat(s2,
"ö");
00229
break;
00230
case 0xc5:
00231 strcat(s2,
"Å");
00232
break;
00233
case 0xc4:
00234 strcat(s2,
"Ä");
00235
break;
00236
case 0xd6:
00237 strcat(s2,
"Ö");
00238
break;
00239
default:
00240 sprintf(s2 + strlen(s2),
"%c",s[i]);
00241 }
00242 }
00243 strcpy(s,s2);
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 }
00263
00264
00265
00266
00267 void remove_br(
char *s)
00268 {
00269
char *s2 =
new char[strlen(s) + 1440];
00270
int i;
00271 fflush(
out);
00272
00273
00274
00275
00276
00277
00278 *s2 = 0;
00279
for (i = 0; i < (
int)strlen(s); i++)
00280 {
00281
if (!strncmp(s + i,
"å",7))
00282 {
00283 sprintf(s2 + strlen(s2),
"%c",0xe5);
00284 i += 6;
00285 }
else
00286
if (!strncmp(s + i,
"ä",6))
00287 {
00288 sprintf(s2 + strlen(s2),
"%c",0xe4);
00289 i += 5;
00290 }
else
00291
if (!strncmp(s + i,
"ö",6))
00292 {
00293 sprintf(s2 + strlen(s2),
"%c",0xf6);
00294 i += 5;
00295 }
else
00296
if (!strncmp(s + i,
"Å",7))
00297 {
00298 sprintf(s2 + strlen(s2),
"%c",0xc5);
00299 i += 6;
00300 }
else
00301
if (!strncmp(s + i,
"Ä",6))
00302 {
00303 sprintf(s2 + strlen(s2),
"%c",0xc4);
00304 i += 5;
00305 }
else
00306
if (!strncmp(s + i,
"Ö",6))
00307 {
00308 sprintf(s2 + strlen(s2),
"%c",0xd6);
00309 i += 5;
00310 }
else
00311
if (s[i] ==
'&')
00312 {
00313 strcat(s2,
"&");
00314 }
else
00315 {
00316 sprintf(s2 + strlen(s2),
"%c",s[i]);
00317 }
00318 }
00319
00320 fflush(
out);
00321 strcpy(s,s2);
00322
delete s2;
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 void pack_and_set_cookies(Cookies *cs)
00348 {
00349
00350 std::string packed;
00351 cgi::Base64 b(
'@');
00352
for (cookie_v::iterator it = cs -> GetCookies().begin(); it != cs -> GetCookies().end(); it++)
00353 {
00354 COOKIE *p = *it;
00355
if (strcmp(p -> name,
"dbpacked"))
00356 {
00357 std::string key = p -> name;
00358 std::string valx = p -> value;
00359 std::string val = b.encode_buffer(valx.size() ? valx :
" null");
00360
00361
if (packed.size())
00362 packed +=
":";
00363 packed += key +
":" + val;
00364 }
00365 }
00366
00367 fprintf(
out,
"Set-Cookie: %s=%s; domain=%s; path=%s; expires=%s\r\n",
00368
"fdc",packed.c_str(),
00369
COOKIE_DOMAIN,
00370
COOKIE_PATH,
00371
expiredatetime());
00372
00373
00374 }
00375
00376
00377 void unpack_cookie(Cookies *cs)
00378 {
00379
char slask[10000];
00380
if (cs && cs -> getvalue(
"fdc",slask,10000))
00381 {
00382 cgi::Parse pa(slask,
":");
00383
char key[100];
00384
char value[1000];
00385 cgi::Base64 b(
'@');
00386 pa.getword(key);
00387 pa.getword(value);
00388
while (*key)
00389 {
00390 b.decode_to_buffer(value, slask, 10000);
00391
if (!strcmp(slask,
" null"))
00392 *slask = 0;
00393 cs -> replacevalue(key, slask);
00394
00395 pa.getword(key);
00396 pa.getword(value);
00397 }
00398 }
00399 }
00400
00401