/** * Tracks the user and log what he did * * @param string $action Action string * @param integer $id Current ID * * @return void */ public function userTracking($action, $id = 0) { global $sid, $user, $botBlacklist; if ($this->_config->get('main.enableUserTracking')) { $bots = 0; $banned = false; $agent = $_SERVER['HTTP_USER_AGENT']; $sid = PMF_Filter::filterInput(INPUT_GET, PMF_GET_KEY_NAME_SESSIONID, FILTER_VALIDATE_INT); $sidc = PMF_Filter::filterInput(INPUT_COOKIE, self::PMF_COOKIE_NAME_SESSIONID, FILTER_VALIDATE_INT); if (!is_null($sidc)) { $sid = $sidc; } if ($action == 'old_session') { $sid = null; } foreach ($botBlacklist as $bot) { if ((bool) PMF_String::strstr($agent, $bot)) { $bots++; } } $network = new PMF_Network($this->_config); // if we're running behind a reverse proxy like nginx/varnish, fix the client IP $remoteAddr = $_SERVER['REMOTE_ADDR']; $localAddresses = array('127.0.0.1', '::1'); if (in_array($remoteAddr, $localAddresses) && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $remoteAddr = $_SERVER['HTTP_X_FORWARDED_FOR']; } // clean up as well $remoteAddr = preg_replace('([^0-9a-z:\\.]+)i', '', $remoteAddr); if (!$network->checkIp($remoteAddr)) { $banned = true; } if (0 == $bots && false == $banned) { if (!isset($sid)) { $sid = $this->_config->getDb()->nextId(PMF_Db::getTablePrefix() . 'faqsessions', 'sid'); // Sanity check: force the session cookie to contains the current $sid if (!is_null($sidc) && !$sidc != $sid) { self::setCookie(self::PMF_COOKIE_NAME_SESSIONID, $sid); } $query = sprintf("\n INSERT INTO \n %sfaqsessions\n (sid, user_id, ip, time)\n VALUES\n (%d, %d, '%s', %d)", PMF_Db::getTablePrefix(), $sid, $user ? $user->getUserId() : -1, $remoteAddr, $_SERVER['REQUEST_TIME']); $this->_config->getDb()->query($query); } $data = $sid . ';' . str_replace(';', ',', $action) . ';' . $id . ';' . $remoteAddr . ';' . str_replace(';', ',', isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '') . ';' . str_replace(';', ',', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '') . ';' . str_replace(';', ',', urldecode($_SERVER['HTTP_USER_AGENT'])) . ';' . $_SERVER['REQUEST_TIME'] . ";\n"; $file = './data/tracking' . date('dmY'); if (is_writeable($file)) { file_put_contents($file, $data, FILE_APPEND); } else { throw new PMF_Exception('Cannot write to ' . $file); } } } }
/** * Get occurence of a string within another * * @param string $haystack Haystack * @param string $needle Needle * @param boolean $part Part * * @return string|false */ public static function strstr($haystack, $needle, $part = false) { return self::$instance->strstr($haystack, $needle, $part); }
/** * Tracks the user and log what he did * * @param string $action Action string * @param integer $id Current ID * * @return void */ public function userTracking($action, $id = 0) { global $sid, $user, $botBlacklist; if (PMF_Configuration::getInstance()->get('main.enableUserTracking')) { $bots = 0; $banned = false; $agent = $_SERVER['HTTP_USER_AGENT']; $sid = PMF_Filter::filterInput(INPUT_GET, PMF_GET_KEY_NAME_SESSIONID, FILTER_VALIDATE_INT); $sidc = PMF_Filter::filterInput(INPUT_COOKIE, PMF_COOKIE_NAME_SESSIONID, FILTER_VALIDATE_INT); if (!is_null($sidc)) { $sid = $sidc; } if ($action == 'old_session') { $sid = null; } foreach ($botBlacklist as $bot) { if ((bool) PMF_String::strstr($agent, $bot)) { $bots++; } } $network = new PMF_Network(); if (!$network->checkIp($_SERVER['REMOTE_ADDR'])) { $banned = true; } if (0 == $bots && false == $banned) { if (!isset($sid)) { $sid = $this->db->nextID(SQLPREFIX . 'faqsessions', 'sid'); // Sanity check: force the session cookie to contains the current $sid if (!is_null($sidc) && !$sidc != $sid) { self::setCookie($sid); } $query = sprintf("\n INSERT INTO \n %sfaqsessions\n (sid, user_id, ip, time)\n VALUES\n (%d, %d, '%s', %d)", SQLPREFIX, $sid, $user ? $user->getUserId() : -1, $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_TIME']); $this->db->query($query); } $data = $sid . ';' . str_replace(';', ',', $action) . ';' . $id . ';' . $_SERVER['REMOTE_ADDR'] . ';' . str_replace(';', ',', $_SERVER['QUERY_STRING']) . ';' . str_replace(';', ',', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '') . ';' . str_replace(';', ',', urldecode($_SERVER['HTTP_USER_AGENT'])) . ';' . $_SERVER['REQUEST_TIME'] . ";\n"; $file = './data/tracking' . date('dmY'); file_put_contents($file, $data, FILE_APPEND); } } }