addBehavior('publicBeforeDocument',array('publicLiveCounter','adjustCache')); $core->addBehavior('templateBeforeValue',array('publicLiveCounter','templateBeforeValue')); $core->tpl->addValue('ConnectedUsers',array('publicLiveCounter','tplConnectedUsers')); $core->tpl->addBlock('ConnectedUsersIf',array('publicLiveCounter','tplConnectedUsersIf')); class publicLiveCounter { private static $connectedCounter = false; public static function initCounters() { $timeout = (integer) $GLOBALS['core']->blog->settings->get('lc_timeout'); $dir = $GLOBALS['core']->blog->settings->get('lc_cache_dir'); if (!is_dir($dir)) { try {files::makeDir($dir,true);} catch (Exception $e) {return;} } $file = $dir.DIRECTORY_SEPARATOR.md5(DC_MASTER_KEY.$GLOBALS['core']->blog->uid); self::$connectedCounter = new connectedCounter($file,$timeout); self::$connectedCounter->count(); self::$connectedCounter->writeNewData(); } public static function showInWidget($w) { global $core; if ($w->homeonly && $core->url->type != 'default') { return; } $sets = &$core->blog->settings; if ($timeout = (int) $w->timeout and $timeout != $sets->lc_timeout) { $sets->setNamespace('livecounter'); $sets->put('lc_timeout',$timeout); } if (self::$connectedCounter !== false || self::initCounters() || true and !$c = self::$connectedCounter->count() or !$content = $c == 1 ? $w->content_one : $w->content) return; $show_users = ''; if (strpos($content,'%2$s')) { $res = array(); $pattern = $w->show_links ? '%1$s' : '%1$s'; $nofollow = $sets->comments_nofollow ? ' rel="nofollow"' : ''; foreach (self::$connectedCounter->result as $v) { $res[] = sprintf($v['site'] ? $pattern : '%1$s', html::escapeHTML($v['name']), html::escapeURL($v['site']), $nofollow); } $res = array(implode(', ',$res)); if (($d = $c-count(self::$connectedCounter->result)) > 0 and $pattern = $d < 2 ? $w->one_unknown : $w->more_unknown) { $res[] = sprintf($pattern,$d); } $show_users = implode(' '.__('and').' ',$res); } $content = sprintf($content,(string) $c,$show_users); $res = '
'. ($w->title ? '

'.html::escapeHTML($w->title).'

' : ''). '

'.$content.'

'; return $res; } public static function tplConnectedUsers($attr) { return 'count();} ?>'; } public static function tplConnectedUsersIf($attr,$content) { $if = array(); $operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&'; if (isset($attr['number'])) { $if[] = '$lcc === '.(int) $attr['number']; } if (isset($attr['min'])) { $if[] = '$lcc >= '.(int) $attr['min']; } if (isset($attr['max'])) { $if[] = '$lcc <= '.(int) $attr['max']; } $res = 'count())'; if (!empty($if)) { $res .= ' && ('.implode(' '.$operator.' ',$if).')'; } $res .= ') : ?>'.$content.''; return $res; } public static function templateBeforeValue(&$core,$id,$attr) { if ($id == 'include' && isset($attr['src']) && $attr['src'] == '_head.html') { return ''; } } public static function adjustCache(&$core) { if ($core->blog->settings->get('lc_no_browser_cache')) { $GLOBALS['mod_ts'] = array(time()); } } public static function getOperator($op) { switch (strtolower($op)) { case 'or': case '||': return '||'; case 'and': case '&&': default: return '&&'; } } } ?>