Google
Web alhem.net
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

utils.cpp

Go to the documentation of this file.
00001 // utils.cpp 00002 /* 00003 Copyright (C) 2001-2004 Anders Hedstrom (grymse@alhem.net) 00004 00005 This program is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU General Public License 00007 as published by the Free Software Foundation; either version 2 00008 of the License, or (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 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 // set-cookie response 00107 /* 00108 fprintf(out,"Set-Cookie: %s=%s; domain=%s; path=%s; expires=%s\n", 00109 n,v, 00110 COOKIE_DOMAIN, 00111 COOKIE_PATH, 00112 expiredatetime()); 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 void dbsafestr2(char *s) 00128 { 00129 char *slask; 00130 int i,j = 0; 00131 00132 if (s) 00133 { 00134 slask = new char[strlen(s) + 144]; //(char *)malloc(strlen(s) + 144); 00135 *slask = 0; 00136 for (i = 0; i < (int)strlen(s); i++) 00137 { 00138 if (s[i] == '\'' || s[i] == 34) 00139 slask[j++] = '\\'; 00140 slask[j++] = s[i]; 00141 } 00142 slask[j] = 0; 00143 if (strlen(s) != strlen(slask)) 00144 { 00145 //printf("dbsafestr2('%s' ==> '%s')\n",s,slask); 00146 strcpy(s,slask); 00147 } 00148 delete slask;//free(slask); 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 // printf("\nmaking syscall('%s')<br>\n",s); 00181 // fflush(out); 00182 00183 // strcat(s," > /dev/null 2>/dev/null"); 00184 r = system(s); 00185 00186 // if (r) 00187 // printf("System call '%s' returned status %d<br>\n",s,r); 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 // ta emot text fran textareain 00208 // (aaoAAO -> &.....; 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,"&aring;"); 00223 break; 00224 case 0xe4: 00225 strcat(s2,"&auml;"); 00226 break; 00227 case 0xf6: 00228 strcat(s2,"&ouml;"); 00229 break; 00230 case 0xc5: 00231 strcat(s2,"&Aring;"); 00232 break; 00233 case 0xc4: 00234 strcat(s2,"&Auml;"); 00235 break; 00236 case 0xd6: 00237 strcat(s2,"&Ouml;"); 00238 break; 00239 default: 00240 sprintf(s2 + strlen(s2),"%c",s[i]); 00241 } 00242 } 00243 strcpy(s,s2); 00244 /* 00245 char *slask = new char[strlen(s) * 2 + 144]; 00246 int i,cr = 0; 00247 00248 *slask = 0; 00249 for (i = 0; i < (int)strlen(s); i++) 00250 if ((s[i] == 13 || s[i] == 10) && !cr) 00251 { 00252 sprintf(slask + strlen(slask),"<br>"); 00253 cr++; 00254 } else 00255 { 00256 sprintf(slask + strlen(slask),"%c",s[i]); 00257 cr = 0; 00258 } 00259 strcpy(s,slask); 00260 delete slask; 00261 */ 00262 } 00263 00264 00265 // forbered text for visning i textarea 00266 00267 void remove_br(char *s) 00268 { 00269 char *s2 = new char[strlen(s) + 1440]; 00270 int i; 00271 fflush(out); 00272 00273 // for (j = 0; j < (int)strlen(s); j++) 00274 // { 00275 // while (!strncasecmp(s + j,"<br>",4)) 00276 // memmove(s + j,s + j + 4,strlen(s + j) + 1); 00277 // } 00278 *s2 = 0; 00279 for (i = 0; i < (int)strlen(s); i++) 00280 { 00281 if (!strncmp(s + i,"&aring;",7)) 00282 { 00283 sprintf(s2 + strlen(s2),"%c",0xe5); 00284 i += 6; 00285 } else 00286 if (!strncmp(s + i,"&auml;",6)) 00287 { 00288 sprintf(s2 + strlen(s2),"%c",0xe4); 00289 i += 5; 00290 } else 00291 if (!strncmp(s + i,"&ouml;",6)) 00292 { 00293 sprintf(s2 + strlen(s2),"%c",0xf6); 00294 i += 5; 00295 } else 00296 if (!strncmp(s + i,"&Aring;",7)) 00297 { 00298 sprintf(s2 + strlen(s2),"%c",0xc5); 00299 i += 6; 00300 } else 00301 if (!strncmp(s + i,"&Auml;",6)) 00302 { 00303 sprintf(s2 + strlen(s2),"%c",0xc4); 00304 i += 5; 00305 } else 00306 if (!strncmp(s + i,"&Ouml;",6)) 00307 { 00308 sprintf(s2 + strlen(s2),"%c",0xd6); 00309 i += 5; 00310 } else 00311 if (s[i] == '&') 00312 { 00313 strcat(s2,"&amp;"); 00314 } else 00315 { 00316 sprintf(s2 + strlen(s2),"%c",s[i]); 00317 } 00318 } 00319 //printf("strlen(s)=%d strlen(s2)=%d<br>\n",strlen(s),strlen(s2)); 00320 fflush(out); 00321 strcpy(s,s2); 00322 delete s2; 00323 } 00324 00325 00326 /* 00327 void nbprintf(char *format, ...) 00328 { 00329 char slask[5000]; 00330 va_list ap; 00331 00332 va_start(ap, format); 00333 vsnprintf(slask, 5000, format, ap); 00334 va_end(ap); 00335 00336 for (size_t i = 0; i < strlen(slask); i++) 00337 { 00338 if (slask[i] == ' ') 00339 printf("&nbsp;"); 00340 else 00341 printf("%c",slask[i]); 00342 } 00343 } 00344 */ 00345 00346 00347 void pack_and_set_cookies(Cookies *cs) 00348 { 00349 //FILE *fil = fopen("/tmp/dbdlog","at"); 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 //fprintf(fil,"%s(%s)%s\n",p -> name,p -> value,val.c_str()); 00361 if (packed.size()) 00362 packed += ":"; 00363 packed += key + ":" + val; 00364 } 00365 } 00366 //fprintf(fil,"---------\n%s\n", packed.c_str()); 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 //fprintf(fil,"\n"); 00373 //fclose(fil); 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

Generated on Thu Feb 10 22:42:34 2005 for Distributed URL Classification Tool by doxygen 1.3.7