Warning: session_start(): open(/var/lib/php/sessions/sess_vdvrncfl384crei072cg1vv6cj, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
!7
static S cookieName = "cookie";
static int cookieDays = 32, cookieLength = 20;
static int cookieCounter = 1;
static long countingForUnixDay;
static new L ips;
static new L cookies;
static long cookieLess;
static new L uaips;
static new MultiSet referers;
static Lock lock = lock();
static new ThreadLocal cookieFromUser;
static new ThreadLocal cookieSent;
static class Uaip {
S ua, ip;
*() {}
*(S *ua, S *ip) {}
}
static class Data {
long day, ips, cookies, cookieLess;
new L uaips;
*(long *day, long *ips, long *cookies, long *cookieLess) {}
*() {}
}
static PersistentLog stats;
p {
stats = new PersistentLog("stats.log");
load("cookieCounter");
load("countingForUnixDay");
load("ips");
load("cookies");
load("cookieLess");
load("uaips");
load("referers");
}
static S html(S uri) {
lock lock;
if (eq(uri, "/referers"))
ret hmobilefix() + h3("Referers!") + htable(
renderMapForTable(orderMapByDescendingValue(referers.asMap()), "Referer", "Count", true));
if (eq(uri, "/stats")) {
ret
htag("p", "IPs in the day: " + l(ips) + ", cookies in the day: " + l(cookies) + ", cookieless today: " + cookieLess + ". Total cookies given out since all time: " + cookieCounter) +
htag("pre", fromLines(reversedList(toLines(stats.fileContents()))));
}
if (eq(uri, "/uaips"))
ret sfu(cloneList(uaips));
O session = call(getMainBot(), "getSession");
O cookieHandler = call(getMainBot(), "getCookies");
S cookie = cast call(cookieHandler, "read", cookieName);
cookieFromUser.set(cookie);
// rotate if day changed
long day = unixDay();
if (day != countingForUnixDay) {
if (countingForUnixDay != 0) {
// archive
print("Archiving stats for unix day " + countingForUnixDay + "!");
stats.add(today());
}
// rotate
countingForUnixDay = day;
save("countingForUnixDay");
ips.clear();
save("ips");
cookies.clear();
save("cookies");
cookieLess = 0;
save("cookieLess");
uaips = new L;
save("uaips");
}
// save only cookies returned by user
if (!cookies.contains(cookie)) {
cookies.add(cookie);
save("cookies");
} else {
++cookieLess;
save("cookieLess");
}
// record IP
Map headers = cast call(session, "getHeaders");
S remoteAddr = cast headers.get("remote-addr");
S client = cast headers.get("x-forwarded-for");
if (nempty(client)) remoteAddr += "," + client;
if (!empty(remoteAddr) && !ips.contains(remoteAddr)) {
ips.add(remoteAddr);
save("ips");
}
// add uaip
S ua = cast headers.get("user-agent");
addUAIP(ua, remoteAddr);
// referer
S referer = cast headers.get("referer");
if (nempty(referer)) {
referers.add(referer);
save("referers"); // TODO: rotate etc.
}
boolean isNew = false;
if (cookie == null) {
isNew = true;
//cookie = "Cookie " + (cookieCounter++);
if (empty((S) headers.get("x-no-cookies")))
cookie = randomID(cookieLength);
//save("cookieCounter");
}
if (cookie != null) {
cookieSent.set(cookie);
call(cookieHandler, "set", cookieName, cookie, cookieDays); // hopefully this refreshes the expiration?
}
S s = cookie == null ? "" : "Your " + (isNew ? "new " : "recurring ") + "cookie is: " + cookie;
s += "
";
Data y = yesterday();
S yesterday = y == null ? "" : " (" + y.cookies + "/" + y.ips + " yesterday)";
s += l(cookies) + " cookies, " + l(ips) + " IPs" + yesterday + " seen today. " + cookieLess + " requests.";
ret s;
}
synchronized answer {
if "visitors today" {
ret structure(today());
}
if "visitors yesterday" {
Data data = yesterday();
if (data == null)
ret "There was no yesterday.";
ret structure(data);
}
if "visitors last * days" {
int i = max(0, l(stats)-parseInt(m.unq(0))+1);
new L