1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/*
+----------------------------------------------------------------------+
| PHP HTML Embedded Scripting Language Version 3.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997,1998 PHP Development Team (See Credits file) |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of one of the following licenses: |
| |
| A) the GNU General Public License as published by the Free Software |
| Foundation; either version 2 of the License, or (at your option) |
| any later version. |
| |
| B) the PHP License as published by the PHP Development Team and |
| included in the distribution in the file: LICENSE |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of both licenses referred to here. |
| If you did not, or have any questions about PHP licensing, please |
| contact core@php.net. |
+----------------------------------------------------------------------+
| Authors: |
+----------------------------------------------------------------------+
*/
/* $Id: log_db.c,v 1.5 1998/05/21 23:57:18 zeev Exp $ */
#include "phpdl.h"
#include "log.h"
#include <stdlib.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/stat.h>
#include <time.h>
#include <errno.h>
#include "functions/pageinfo.h"
#include "functions/db.h"
#include "log.h"
void _php3_log_db() {
#if LOGGING
char path[1024];
char key[32];
char buf[512];
char *host = NULL;
char *email, *ref, *browser;
dbm_info *dbm;
#if NOLOGSUCCESSIVE
/* this prevents logging multiple consecutive hits from the
same host */
char *lasthost = NULL;
#endif
struct stat sb;
time_t t;
int try = 0, retries = 0;
int ret, es;
char *lref = NULL, *s;
/* verify that the logging directory exists */
if (!log_stats.filled)
_php3_load_log_info();
/* SAFE_MODE concern: should make sure that the user is
logging somewhere safe */
if (stat(log_conf.dbm_log_dir, &sb) == -1) {
if (mkdir(log_conf.dbm_log_dir, 0755)==-1) {
php3_error(E_WARNING, "Trying to create main log directory [%s]: %d [%s]",log_conf.dbm_log_dir,errno,strerror(errno));
return;
}
}
/* see if the log directory exists for this page-owner's uid */
sprintf(path, "%s/%ld", log_conf.dbm_log_dir, _php3_getuid());
if (stat(path, &sb) == -1) {
if (mkdir(path, 0755)==-1) {
php3_error(E_WARNING, "Trying to create user log directory [%s]: %d [%s]",path,errno,strerror(errno));
return;
}
log_stats.start_logging = time(NULL);
}
/* construct the full path for the database */
sprintf(path, "%s/%ld/%s.log", log_conf.dbm_log_dir, _php3_getuid(), log_stats.logfile);
es = error_reporting;
error_reporting = 0;
dbm = _php3_dbmopen(path, "w");
error_reporting = es;
if (!dbm) {
dbm = _php3_dbmopen(path, "n");
if (!dbm) {
php3_error(E_WARNING, "Unable to create %s", path);
return;
}
log_stats.total_count = 0;
log_stats.today_count = 0;
t = time(NULL);
sprintf(key,"%ld",(long)t);
/* retry a few times for some reason */
while(!_php3_dbminsert(dbm, "first", key)) {
retries++;
if (retries > 20) {
retries = 0;
break;
}
}
}
host = php_env.remote_host;
#if NOLOGSUCCESSIVE
lasthost = getlasthost();
if (strncmp(host, lasthost, 128)==0) {
_php3_dbmclose(dbm);
return;
}
#endif
email = php_env.email;
ref = php_env.referrer;
if (ref) {
lref = estrdup(ref);
s = strchr(lref,'&');
if (s) *s='\0';
if (strlen(lref)>128) lref[127]='\0';
}
browser = php_env.useragent;
sprintf(buf,"%s%c%s%c%s%c%s",host?host:"",27,email?email:"",27,lref?lref:"",27,browser?browser:"");
t = time(NULL);
sprintf(key,"%ld",(long)t);
ret=1;
while(ret) {
ret = _php3_dbminsert(dbm, key, buf);
if (ret) {
sprintf(key,"%ld%c",(long)t,' '+try++);
}
retries++;
if (retries>20) break;
}
log_stats.total_count++;
log_stats.today_count++;
sprintf(buf,"%s %ld %ld %ld %s%c%s%c%s%c%s",
php_env.filename,
(long)t,
log_stats.total_count,
log_stats.today_count,
host ? host : "" ,
27,
email ? email : "",
27,
lref ? lref : "",
27,
browser ? browser : "");
_php3_dbmreplace(dbm, "last", buf);
_php3_dbmclose(dbm);
#endif
}
void _php3_load_log_info_db() {
#if LOGGING
dbm_info *dbm;
char *value;
char *s=NULL, *ss;
char path[1024];
char key[32];
int ret, es;
time_t t;
struct tm *tm1;
int day1, day2;
int retries=0;
/* construct the full path for the database */
sprintf(path, "%s/%ld/%s.log", log_conf.dbm_log_dir, _php3_getuid(), log_stats.logfile);
es = error_reporting;
error_reporting = 0;
dbm = _php3_dbmopen(path, "r");
error_reporting = es;
if (!dbm) {
log_stats.filled = 1;
log_stats.total_count = 0;
log_stats.today_count = 0;
log_stats.last_access = 0;
log_stats.last_host = estrdup("none");
log_stats.last_ref = estrdup("none");
log_stats.last_browser = estrdup("none");
log_stats.last_email = estrdup("none");
log_stats.start_logging = time(NULL);
return;
}
value = s = _php3_dbmfetch(dbm, "last");
ss = (char *)strchr(s,' ');
if (ss) {
s = ss+1;
ss=(char *)strchr(s,' ');
}
if (ss) {
*ss='\0';
log_stats.last_access = atol(s);
s = ss+1;
ss = (char *)strchr(s,' ');
if (ss) {
*ss='\0';
log_stats.total_count = atol(s);
s = ss+1;
ss = (char *)strchr(s,' ');
if (ss) {
*ss='\0';
log_stats.today_count = atol(s);
t = time(NULL);
tm1 = localtime((time_t *)(&log_stats.last_access));
day1 = tm1->tm_yday;
tm1 = localtime(&t);
day2 = tm1->tm_yday;
if (day1 != day2) log_stats.today_count = 0;
s = ss+1;
ss = (char *)strchr(s,27);
if (ss) {
*ss='\0';
log_stats.last_host = (char *)estrdup(s);
s = ss+1;
ss = (char *)strchr(s,27);
if (ss) {
*ss='\0';
if (*s) log_stats.last_email = (char *)estrdup(s);
s = ss+1;
ss = (char *)strchr(s,27);
if (ss) {
*ss='\0';
if (*s) log_stats.last_ref = (char *)estrdup(s);
s = ss+1;
log_stats.last_browser = (char *)estrdup(s);
}
}
}
}
}
}
if (value) efree(value);
value = s = _php3_dbmfetch(dbm, "first");
if (!s) {
/* If there is no first entry in the dbm file, add one now.
This obviously won't be the correct first entry date, but
it is better than not having one at all. */
t = time(NULL);
sprintf(key,"%ld",(long)t);
ret = 1;
while (ret) {
ret = _php3_dbminsert(dbm,"first",key);
retries++;
if (retries>20) {
retries = 0;
break;
}
}
log_stats.start_logging = t;
} else {
log_stats.start_logging = atol(s);
}
if (value) efree(value);
log_stats.filled = 1;
_php3_dbmclose(dbm);
#endif
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|