private function __construct() { $this->dbh = new PDO('pgsql:host=' . Config\POSTGRESQL_HOST . ';dbname=' . Config\POSTGRESQL_DATA_NAME . ';port=' . Config\POSTGRESQL_PORT, Config\POSTGRESQL_USER, Config\POSTGRESQL_PASS); $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Fetch the IDs for special profiles/projects $cache = Config\SITE_HOST . 'special-ids'; if (!($specialIds = Utils::apc_get($cache))) { $me = $this; $specialIds = Utils::apc_set($cache, function () use($me) { try { $stmt = $this->dbh->query('SELECT * FROM special_users'); $userIds = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); $stmt = $this->dbh->query('SELECT * FROM special_groups'); $projectsIds = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); return ['USER' => $userIds, 'PROJECT' => $projectsIds]; } catch (PDOException $e) { static::dumpException($e); die($e->getTraceAsString()); } }, 86400); } Config::add('USERS_NEWS', $specialIds['USER']['GLOBAL_NEWS']); Config::add('DELETED_USERS', $specialIds['USER']['DELETED']); Config::add('ISSUE_BOARD', $specialIds['PROJECT']['ISSUE']); Config::add('PROJECTS_NEWS', $specialIds['PROJECT']['GLOBAL_NEWS']); }
public function lang($index) { // we don't worrie about language file modifications, since this ones shouldn't occur often $cache = "language-file-{$this->lang}-{$this->tpl_no}" . Config\SITE_HOST; if (!($_LANG = Utils::apc_get($cache))) { $_LANG = Utils::apc_set($cache, function () { // first load default language file $defaultLang = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/data/langs/{$this->lang}/default.json"), true); // then we add eventually merge template additions $tplFile = $_SERVER['DOCUMENT_ROOT'] . "/tpl/{$this->tpl_no}/langs/{$this->lang}/json/default.json"; if (is_readable($tplFile)) { $defaultLang = array_merge($defaultLang, json_decode(file_get_contents($tplFile), true)); } return $defaultLang; }, 3600); } return nl2br(htmlspecialchars($_LANG[$index], ENT_QUOTES, 'UTF-8')); }
public static function getVersion() { $cache = 'NERDZVersion' . Config\SITE_HOST; if ($version = Utils::apc_get($cache)) { return $version; } return Utils::apc_set($cache, function () { if (!is_dir($_SERVER['DOCUMENT_ROOT'] . '/.git') || !file_exists($_SERVER['DOCUMENT_ROOT'] . '/.git/refs/heads/master')) { return 'null'; } $revision = substr(file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/.git/refs/heads/master'), 0, 7); if (strlen($revision) != 7) { return 'null'; } return $revision; }, 5400); }
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/class/autoload.php'; use NERDZ\Core\Trend; use NERDZ\Core\Utils; use NERDZ\Core\Config; if (!isset($user)) { die('$user required'); } $func = function () use($user) { $vals = []; $cache = 'trends' . Config\SITE_HOST; if (!($trends = Utils::apc_get($cache))) { $trends = Utils::apc_set($cache, function () { $trend = new Trend(); $ret = []; $ret['popular'] = $trend->getPopular(); $ret['newest'] = $trend->getNewest(); return $ret; }, 300); } $vals['popular_a'] = $trends['popular']; $vals['newest_a'] = $trends['newest']; $user->getTPL()->assign($vals); }; $func();
if ($pair['number'] == $userTpl) { $vals['template_n'] = $pair['name']; break; } } $vals['registrationtime_n'] = $user->getDateTime($o->registration_time); $vals['username_n'] = $info->username; $vals['username4link_n'] = Utils::userLink($info->username); $vals['lang_n'] = $user->getLanguage($info->counter); $vals['online_b'] = $user->isOnline($info->counter); $vals['name_n'] = ucfirst($info->name); $vals['surname_n'] = ucfirst($info->surname); list($year, $month, $day) = explode('-', $info->birth_date); $vals['birthdate_n'] = $day . '/' . $month . '/' . $year; $apc_name = 'userstuff' . $info->counter . Config\SITE_HOST; if (!($stuff = Utils::apc_get($apc_name))) { $stuff = Utils::apc_set($apc_name, function () use($user, $ida) { if (!($o = Db::query(['SELECT COUNT("hcid") AS cc FROM "comments" WHERE "from" = :id', $ida], Db::FETCH_OBJ))) { die($user->lang('ERROR')); } $n = $o->cc; if (!($o = Db::query(['SELECT COUNT("hcid") AS cc FROM "groups_comments" WHERE "from" = :id', $ida], Db::FETCH_OBJ))) { die($user->lang('ERROR')); } $n += $o->cc; $a = Stuff::stupid($n); $a['n'] = $n; return $a; }, 300); } $vals['stupidstuffnow_n'] = $stuff['now'];
MathJax.Hub.Config({ extensions: ["tex2jax.js"], jax: ["input/TeX", "output/HTML-CSS"], tex2jax: { inlineMath: [ ['[m]','[/m]'] ], displayMath: [ ['[math]','[/math]'] ], processEscapes: false }, "HTML-CSS": { availableFonts: ["TeX"], linebreaks: { automatic: true, width: "container" } } }); </script> <script src="//cdn.mathjax.org/mathjax/latest/MathJax.js" async></script> <script> <?php $trackingCacheKey = 'tracking_js' . NERDZ\Core\Config\SITE_HOST; if (!($tracking = Utils::apc_get($trackingCacheKey))) { $tracking = Utils::apc_set($trackingCacheKey, function () { $trjs = $_SERVER['DOCUMENT_ROOT'] . '/data/tracking.js'; return is_readable($trjs) ? file_get_contents($trjs) : ''; }, 3600); } echo $tracking; /* BEGIN SSL_VARIABLES (used by the JS API) */ ?> var Nssl = { login: <?php echo Config\LOGIN_SSL_ONLY ? 'true' : 'false'; ?> , domain: "<?php echo Config\HTTPS_DOMAIN;
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/class/autoload.php'; use NERDZ\Core\Config; use NERDZ\Core\Utils; use NERDZ\Core\Stuff; use NERDZ\Core\Db; use NERDZ\Core\User; $mo = empty($_GET['top']); $path = Config\SITE_HOST . ($mo ? 'r_month.json' : 'rank.json'); if (!($ret = Utils::apc_get($path))) { $ret = Utils::apc_set($path, function () use($mo) { $un_ti = ' AND ("time" + INTERVAL \'28 days\') > NOW()'; $res = Db::query('SELECT COUNT("hcid") AS cc,"from" FROM "comments" WHERE "from" <> (SELECT counter FROM special_users WHERE role = \'DELETED\')' . (!$mo ? $un_ti : '') . ' GROUP BY "from" ORDER BY cc DESC LIMIT 100', Db::FETCH_STMT); $rank = []; while ($o = $res->fetch(PDO::FETCH_OBJ)) { $gc = Db::query(['SELECT COUNT("hcid") AS cc FROM "groups_comments" WHERE "from" = :from ' . (!$mo ? $un_ti : ''), [':from' => $o->from]], Db::FETCH_OBJ); $us = User::getUsername($o->from); $n = $o->cc + $gc->cc; $rank[$us] = $n; $stupid = Stuff::stupid($n); $ss[$us] = $stupid['now']; } asort($rank); $rank = array_reverse($rank, true); $i = 0; $ret = []; foreach ($rank as $username => $val) {
$cache = 'nerdz_stats' . Config\SITE_HOST; if (!($ret = Utils::apc_get($cache))) { $ret = Utils::apc_set($cache, function () use($cache) { function createArray(&$ret, $query, $position) { if (!($o = Db::query($query, Db::FETCH_OBJ))) { $ret[$position] = -1; } else { $ret[$position] = $o->cc; } } $queries = [0 => 'SELECT COUNT(counter) AS cc FROM users', 1 => 'SELECT COUNT(hpid) AS cc FROM posts', 2 => 'SELECT COUNT(hcid) AS cc FROM comments', 3 => 'SELECT COUNT(counter) AS cc FROM groups', 4 => 'SELECT COUNT(hpid) AS cc FROM groups_posts', 5 => 'SELECT COUNT(hcid) AS cc FROM groups_comments', 6 => 'SELECT COUNT(counter) AS cc FROM users WHERE last > (NOW() - INTERVAL \'4 MINUTES\') AND viewonline IS TRUE', 7 => 'SELECT COUNT(counter) AS cc FROM users WHERE last > (NOW() - INTERVAL \'4 MINUTES\') AND viewonline IS FALSE', 8 => 'SELECT COUNT(remote_addr) AS cc FROM guests WHERE last > (NOW() - INTERVAL \'4 MINUTES\')']; foreach ($queries as $position => $query) { createArray($ret, $query, $position); } if (!($bots = Utils::apc_get($cache . 'bots'))) { $bots = Utils::apc_set($cache . 'bots', function () { $txt = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/data/bots.json'); return json_decode(preg_replace('#(/\\*([^*]|[\\r\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+/)|([\\s\\t](//).*)#', '', $txt), true); }, 86400); } $ret[9] = 0; $ret[10] = []; if ($uas = Db::query('SELECT http_user_agent FROM guests WHERE last > (NOW() - INTERVAL \'4 MINUTES\')', DB::FETCH_OBJ, true)) { foreach ($uas as $ua) { foreach ($bots as $bot) { if (preg_match('#' . $bot['regex'] . '#', $ua->http_user_agent)) { $ret[10][$ret[9]]['name_n'] = $bot['name']; ++$ret[9]; break; }
public function story() { if (!($ret = Utils::apc_get($this->cachekey))) { return Utils::apc_set($this->cachekey, function () { if (!($o = Db::query(['SELECT "notify_story" FROM "users" WHERE "counter" = :id', [':id' => $_SESSION['id']]], Db::FETCH_OBJ))) { return []; } return json_decode($o->notify_story, true); }, 300); } return $ret; }