Database::simpleQuery("SELECT dateline, logtypeid AS typeid, clientip, description FROM clientlog WHERE description REGEXP :exp ORDER BY dateline ASC", ['exp' => $exp])]; if (Module::get('statistics') !== false) { $srcs[] = ['res' => Database::simpleQuery("SELECT dateline, typeid, clientip, data AS description FROM statistic WHERE username = :user ORDER BY dateline ASC", ['user' => $user])]; } echo "# Begin log\n"; for (;;) { unset($best); foreach ($srcs as &$src) { if (!isset($src['row'])) { $src['row'] = $src['res']->fetch(); } if ($src['row'] !== false && (!isset($best) || $src['row']['dateline'] < $best['dateline'])) { $best =& $src['row']; } } if (!isset($best)) break; echo date('Y-m-d H:i:s', $best['dateline']), "\t", $best['typeid'], "\t", $best['clientip'], "\t", $best['description'], "\n"; $best = null; // so we repopulate on next iteration } die("# End log\n"); } if (empty($_POST['type'])) die('Missing options.'); $type = mb_strtolower($_POST['type']); $ip = $_SERVER['REMOTE_ADDR']; if (substr($ip, 0, 7) === '::ffff:') $ip = substr($ip, 7); // TODO: Handle UUID in appropriate modules (optional) $uuid = Request::post('uuid', '', 'string'); if (strlen($uuid) !== 36) { // Probably invalid UUID. What to do? Set NULL for now so the insert will succeed $uuid = null; error_log("Client log event $type without UUID"); } /* * Normal logging */ if (!isset($_POST['description'])) die('Missing options..'); $description = $_POST['description']; $longdesc = ''; if (isset($_POST['longdesc'])) $longdesc = $_POST['longdesc']; $longdesc = Request::post('longdesc', '', 'string'); if (preg_match('/^[a-z0-9\-]+$/', $type)) { // Spam from IP? $row = Database::queryFirst('SELECT Count(*) AS cnt FROM clientlog WHERE clientip = :client AND dateline + 1800 > UNIX_TIMESTAMP()', [':client' => $ip]); if ($row !== false && $row['cnt'] > 250) { exit(0); } ClientLog::write(['machineuuid' => $uuid, 'clientip' => $ip], $type, $description, $longdesc); } echo "OK.\n";