exec($sqldel) === false) {
echo "
DATABASE ERROR(del): ".print_r($db->errorInfo(),true)."
\n";
die();
}
$sqlins = "INSERT INTO whoswhere (sessionid, authid, author, ip, action, pn, date) VALUES ('".session_id()."', '$authid_fetched', '$Author', '$_SERVER[REMOTE_ADDR]', '$action', '$pagename', $Now)";
if ($db->exec($sqlins) === false) {
echo "DATABASE ERROR(ins): ".print_r($db->errorInfo(),true)."
\n";
die();
}
}
# $wwHeaderFmt and $wwDetailFmt can be overridden
# $wwNoAuthHeaderFmt and $wwNoAuthDetailFmt use $wwHeaderFmt and
# $wwDetailFmt if they are blank (default)
# arguments to (:whoswhere ARGS:) can override all of the above:
# headerfmt=
# detailfmt=
# noauthheaderfmt=
# noauthdetailfmt=
# $wwAuthFields lists allowed fields for authorized users to see
# $wwNoAuthFields lists allowed fields for NONauthorized users to see, but it
# uses the value from $wwAuthFields if $wwNoAuthFields is blank (default)
Markup('whoswhere', '>{$var}', '/\(:whoswhere([^:]*):\)/ie', 'wwDisplay(\$pagename, "$1")');
function wwDisplay($pagename, $args)
{
global $Now, $AuthId, $wwSeconds, $wwHeaderFmt, $wwDetailFmt,
$wwNoAuthHeaderFmt, $wwNoAuthDetailFmt, $wwAuthFields, $wwNoAuthFields;
# Handle all the defaults & overrides to get $DetailFmt, $HeaderFmt, $Fields
$opt = ParseArgs($args);
SDV($wwSeconds, 60*20); // report activity within the last 20 minutes
if (@$opt['headerfmt'])
$wwHeaderFmt = str_replace('\n', "\n", $opt['headerfmt']);
else
SDV($wwHeaderFmt, "|| border=1\n||! Who? ||! Where? ||! Doing What? ||! When?||");
if (@$opt['detailfmt'])
$wwDetailFmt = $opt['detailfmt'];
else
SDV($wwDetailFmt, '|| {anyid} || {pn} || {action} || {date} ago||');
if (@$opt['noauthdetailfmt'])
$wwNoAuthDetailFmt = $opt['noauthdetailfmt'];
else
SDV($wwNoAuthDetailFmt, '');
if (@$opt['noauthheaderfmt'])
$wwNoAuthHeaderFmt = str_replace('\n', "\n", $opt['noauthheaderfmt']);
else
SDV($wwNoAuthHeaderFmt, '');
$DetailFmt = $wwDetailFmt;
$HeaderFmt = $wwHeaderFmt;
if (!$AuthId && $wwNoAuthDetailFmt) $DetailFmt = $wwNoAuthDetailFmt;
else $DetailFmt = $wwDetailFmt;
if (!$AuthId && $wwNoAuthHeaderFmt) $HeaderFmt = $wwNoAuthHeaderFmt;
else $HeaderFmt = $wwHeaderFmt;
SDV($wwAuthFields, 'anyid|ip|authid|author|pn|action|date');
SDV($wwNoAuthFields, '');
$Fields = ((!$AuthId && $wwNoAuthFields) ? $wwNoAuthFields : $wwAuthFields);
$db = wwWhosWhereInit();
$sql = "SELECT * FROM whoswhere WHERE date>$Now-$wwSeconds";
$rtn = '';
foreach ($db->query($sql) as $row) {
$secs = $Now - $row['date'];
$mins = floor($secs/60);
$secs = $secs - ($mins*60);
$row['date'] = sprintf("%d:%02d", $mins, $secs);
$row['anyid'] = ($row['authid']?$row['authid']:"(not logged in)") .
($row['author']?' ('.$row['author'].')':'');
$rtn .= "\n" . preg_replace("/\\{($Fields)\\}/e", '$row["$1"]', $DetailFmt);
}
if ($rtn)
$rtn = $HeaderFmt . $rtn;
else
$rtn = '(Nobody is Anywhere)';
return($rtn);
}
# Return the $db resource (create if necessary)
function wwWhosWhereInit()
{
global $WorkDir, $wwDatabase;
SDV($wwDatabase, "sqlite:$WorkDir/whoswhere.sqlite.db");
try {
$db = new PDO($wwDatabase);
} catch (PDOException $e) {
echo "Error!: " . $e->getMessage() . " (database=$wwDatabase)
";
die(); // possibly no sqlite support?
}
# Now check if the whoswhere table exists and create if necessary.
$statement = $db->query('SELECT name FROM sqlite_master WHERE type = \'table\' AND name = \'whoswhere\'');
$result = $statement->fetchAll();
if( sizeof($result) == 0 ){
$sqlcreate = 'CREATE TABLE whoswhere (sessionid CHAR(40) PRIMARY KEY, authid CHAR(30), author CHAR(50), ip CHAR(20), action CHAR(15), pn CHAR(50), date INT)';
if ($db->exec($sqlcreate) === false) {
echo "DATABASE ERROR(create): ".print_r($db->errorInfo(),true)."
\n";
die();
}
}
return($db);
}