public static function putInDB($uid, $url, $now, $flags)
    {
        $url = ProfileUserTracker_sharedstatic::mysql_escape_mimic_fromPhpDoc($url);
        $flags = ProfileUserTracker_sharedstatic::mysql_escape_mimic_fromPhpDoc(serialize($flags));
        $q = 'INSERT INTO kiror_user_browsing (uid,href,uts,flag) VALUES
			(\'' . $uid . '\', \'' . $url . '\', \'' . $now . '\', \'' . $flags . '\');';
        //die($q);
        $dbc = XenForo_Application::get('db');
        $dbc->query($q);
    }
    public function prepareParams()
    {
        $resp = parent::prepareParams();
        $vis = XenForo_Visitor::getInstance();
        if ($vis['user_id']) {
            $rq = $this->_renderer->getRequest();
            $dh = $this->_renderer->getDependencyHandler();
            $rfl = new ReflectionObject($dh);
            $prp = $rfl->getProperty('_defaultTemplateParams');
            $prp->setAccessible(true);
            $out = $prp->getValue($dh);
            unset($prp);
            unset($rfl);
            $defaultTemplateParams = $out;
            unset($out);
            $uid = $vis['user_id'];
            $url = 'data:text/html;base64,';
            if (!is_null($defaultTemplateParams) && is_array($defaultTemplateParams) && array_key_exists('requestPaths', $defaultTemplateParams) && is_array($defaultTemplateParams['requestPaths']) && array_key_exists('fullUri', $defaultTemplateParams['requestPaths'])) {
                $url = $defaultTemplateParams['requestPaths']['fullUri'];
            } else {
                try {
                    $url = $rq->getRequestUri();
                } catch (Exception $e) {
                    $url .= base64_encode('<!DOCTYPE html>
<html>
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Error: URL parsing</title>
	</head>
	<body>
		<h1>Error</h1>
		<p>An unexpected error happened while trying to get the URL from the page.</p>
		<p>This message was stored as the URL instead.</p>
	</body>
</html>');
                }
            }
            $now = time();
            $flags = array();
            if ($rq->isPost()) {
                $flags[] = 'POST';
            }
            if ($rq->isGet()) {
                $flags[] = 'GET';
            }
            if ($rq->isPut()) {
                $flags[] = 'PUT';
            }
            if ($rq->isDelete()) {
                $flags[] = 'DELETE';
            }
            if ($rq->isHead()) {
                $flags[] = 'HEAD';
            }
            if ($rq->isOptions()) {
                $flags[] = 'OPTIONS';
            }
            if ($rq->isXmlHttpRequest()) {
                $flags[] = 'XmlHttpRequest';
            }
            if ($rq->isFlashRequest()) {
                $flags[] = 'FlashRequest';
            }
            if ($rq->isSecure()) {
                $flags[] = 'SSL';
            }
            //die(print_r(array($dh),true));
            //die(print_r(array($rq),true));
            //die(print_r(array($flags),true));
            //**TESTS
            //die(print_r(array(get_class_methods($this),get_object_vars($this)),true));
            //die(print_r(array(get_class_methods($this->_renderer),get_object_vars($this->_renderer)),true));
            //die(print_r(array($rq,get_class_methods($rq),get_object_vars($rq)),true));
            //die(print_r($defaultTemplateParams,true));
            //die(print_r(array($dh,get_class_methods($dh),get_object_vars($dh)),true));
            //die(print_r(array($rq),true));
            //**/
            //hiding "_xfToken"
            $urlprot = preg_replace('/((_xfToken=).+&|(_xfToken=).+$)/', '_xfToken=<b><i>censored</i></b>&', $url);
            ///Throwing everything in DB!
            if (strpos($url, '/admin.php') == false && strpos($url, '?chat/refresh') == false) {
                ProfileUserTracker_sharedstatic::putInDB($uid, $urlprot, $now, $flags);
            }
        }
        return $resp;
    }
    public function actionIndex()
    {
        ProfileUserTracker_sharedstatic::pruneOlders();
        $unm = $this->_input->filterSingle('unm', XenForo_Input::STRING);
        $uid = $this->_input->filterSingle('uid', XenForo_Input::STRING);
        $ftr = $this->_input->filterSingle('ftr', XenForo_Input::STRING);
        $page = $this->_input->filterSingle('pag', XenForo_Input::STRING);
        if ($page == 'downloadjson') {
            $downloadmode = 'json';
        } else {
            $downloadmode = false;
        }
        $page = $this->_input->filterSingle('pag', XenForo_Input::INT);
        //
        $viewParams = array('page' => $page);
        //
        $userModel = XenForo_Model::create('XenForo_Model_User');
        $user = array('user_id' => intval($uid));
        if (intval($uid) > 0) {
            $user = $userModel->getUserById(intval($uid));
        } else {
            $user = $userModel->getUserByName($unm);
        }
        $SearchUid = $user['user_id'];
        if ($SearchUid == null) {
            $SearchUid = 0;
        }
        if ($page == null || $page < 0) {
            $page = 0;
        }
        if ($downloadmode) {
            $visitor = XenForo_Visitor::getInstance();
            if ($downloadmode == 'json') {
                $ips = array();
                try {
                    $ips = $this->getModelFromCache('XenForo_Model_Ip')->getIpsByUserId($user['user_id']);
                } catch (Exception $e) {
                }
                ksort($ips);
                $nips = array();
                foreach ($ips as $time => $ip) {
                    $nips[] = array('timestamp' => $time, 'timestring' => date('r', $time), 'ipaddress' => $ip);
                }
                $results = ProfileUserTracker_sharedstatic::getAllFromDB($SearchUid, $ftr);
                $nresults = array();
                foreach ($results as $result) {
                    $result['timestring'] = date('r', $result['uts']);
                    $nresults[] = $result;
                }
                $t = time();
                $return = array('now' => array('unixTimeStamp' => $t, 'textTimeStamp' => date('r', $t), 'requestUserID' => $visitor['user_id'], 'requestUserName' => $visitor['username']), 'search' => array('user_id' => $user['user_id'], 'username' => $user['username'], 'URLfilter' => $ftr, 'IPs' => $nips), 'results' => $nresults);
                $downloadable = json_encode($return);
                $fsize = strlen($downloadable);
                $identif = $user == null ? 'allUsers' : 'ID_' . $user['user_id'];
                $fname = '' . $identif . '_-_' . (strlen($ftr) <= 0 ? 'unfiltered' : 'filtered') . '_-_' . date('Y-m-d--G-i-s--e', $t) . '.json';
                $mime = 'application/json';
                header('Content-Type: ' . $mime);
                header('Content-Disposition: attachment; filename="' . $fname . '"');
                header('Content-Length: ' . $fsize);
                header('Connection: close');
                die($downloadable);
            }
        }
        $resultsPerPage = 50;
        $res = ProfileUserTracker_sharedstatic::getFromDB($SearchUid, $page, $ftr, $resultsPerPage);
        $rescount = ProfileUserTracker_sharedstatic::getFromDBLimitless($SearchUid, $page, $ftr);
        $lastpage = ceil($rescount / $resultsPerPage);
        $html = '';
        foreach ($res as $k => $v) {
            $html .= ProfileUserTracker_sharedstatic::resultToHtml($v);
        }
        $html = '<style type="text/css">
td
{
    padding:0 5px 0 5px;
}
</style>
<div class="section sectionMain searchResults InlineModForm">
<ol class="searchResultsList">' . $html . "\n" . '</ol></div>' . "\n\n\n";
        $viewParams['htmlpage'] = $html;
        //
        $viewParams['prevpg'] = $page - 1 < 0 ? 0 : $page - 1;
        $viewParams['nextpg'] = $page + 2 > $lastpage ? $lastpage - 1 : $page + 1;
        $viewParams['page'] = $page;
        $viewParams['pageinc'] = $page + 1;
        $viewParams['uid'] = $user['user_id'];
        $viewParams['unm'] = $user['username'];
        $viewParams['ftr'] = $ftr;
        $viewParams['totalpages'] = $lastpage;
        $viewParams['totalres'] = $rescount;
        $viewParams['startres'] = $page * $resultsPerPage + 1;
        $viewParams['endres'] = ($page + 1) * $resultsPerPage;
        $viewParams['downloadjsonlink'] = XenForo_Link::buildAdminLink('usertracking', '', array('unm' => $user['username'], 'uid' => $user['user_id'], 'ftr' => $ftr, 'pag' => 'downloadjson'));
        return $this->responseView('XenForo_ViewAdmin_Base', 'kiror_user_tracking_page', $viewParams);
    }
 public static function uninstall()
 {
     ProfileUserTracker_sharedstatic::dropDB();
 }