ValekS, <?php
/**
* @package JohnCMS
* @link
http://johncms.com
* @copyright Copyright (C) 2008-2011 JohnCMS Community
* @license LICENSE.txt (see attached file)
* @version VERSION.txt (see attached file)
* @author
http://johncms.com/about
*/
defined('_IN_JOHNCMS') or die('Restricted access');
class functions extends core
{
/**
* ��������
* ������ ������:
* 1 - ����������
* 2 - ���� / ����
* 3 - ����
* 4 - ����
*
* @return int|bool
*/
public static function antiflood()
{
$default = array(
'mode' => 2,
'day' => 10,
'night' => 30,
'dayfrom' => 10,
'dayto' => 22
);
$af = isset(self::$system_set['antiflood']) ? unserialize(self::$system_set['antiflood']) : $default;
switch ($af['mode']) {
case 1:
// ���������� �����
$adm = mysql_result(mysql_query("SELECT COUNT(*) FROM `users` WHERE `rights` > 0 AND `lastdate` > " . (time() - 300)), 0);
$limit = $adm > 0 ? $af['day'] : $af['night'];
break;
case 3:
// ����
$limit = $af['day'];
break;
case 4:
// ����
$limit = $af['night'];
break;
default:
// �� ��������� ���� / ����
$c_time = date('G', time());
$limit = $c_time > $af['day'] && $c_time < $af['night'] ? $af['day'] : $af['night'];
}
if (self::$user_rights > 0)
$limit = 4; // ��� ������������� ������ ����� � 4 �������
$flood = self::$user_data['lastpost'] + $limit - time();
if ($flood > 0)
return $flood;
else
return FALSE;
}
/**
* ���������� ������ � ������
*
* @param $var
*
* @return string
*/
public static function antilink($var)
{
$var = preg_replace('~\\[url=(https?://.+?)\\](.+?)\\[/url\\]|(https?://(www.)?[0-9a-z\.-]+\.[0-9a-z]{2,6}[0-9a-zA-Z/\?\.\~&_=/%-:#]*)~', '###', $var);
$replace = array(
'.ru' => '***',
'.com' => '***',
'.biz' => '***',
'.cn' => '***',
'.in' => '***',
'.net' => '***',
'.org' => '***',
'.info' => '***',
'.mobi' => '***',
'.wen' => '***',
'.kmx' => '***',
'.h2m' => '***'
);
return strtr($var, $replace);
}
/**
* ���������� �����
*
* @param string $str
*
* @return string
*/
public static function checkin($str)
{
if (function_exists('iconv')) {
$str = iconv("UTF-8", "UTF-8", $str);
}
// ��������� ��������� �������
$str = preg_replace('/[^\P{C}\n]+/u', '', $str);
return trim($str);
}
/**
* ��������� ������� ����� ������� �� �����
*
* @param string $str
* @param int $br �������� ��������� ��������� �����
* 0 - �� ������������ (�� ���������)
* 1 - ������������
* 2 - ������ ��������� ������ ����������� �������
* @param int $tags �������� ��������� �����
* 0 - �� ������������ (�� ���������)
* 1 - ������������
* 2 - �������� ����
*
* @return string
*/
public static function checkout($str, $br = 0, $tags = 0)
{
$str = htmlentities(trim($str), ENT_QUOTES, 'UTF-8');
if ($br == 1) {
// ��������� �������� �����
$str = nl2br($str);
} elseif ($br == 2) {
$str = str_replace("\r\n", ' ', $str);
}
if ($tags == 1) {
$str = bbcode::tags($str);
} elseif ($tags == 2) {
$str = bbcode::notags($str);
}
return trim($str);
}
/**
* ����� ��������� ��������� ����� ��������
*/
public static function display_counters()
{
global $headmod;
$req = mysql_query("SELECT * FROM `cms_counters` WHERE `switch` = '1' ORDER BY `sort` ASC");
if (mysql_num_rows($req) > 0) {
while (($res = mysql_fetch_array($req)) !== FALSE) {
$link1 = ($res['mode'] == 1 || $res['mode'] == 2) ? $res['link1'] : $res['link2'];
$link2 = $res['mode'] == 2 ? $res['link1'] : $res['link2'];
$count = ($headmod == 'mainpage') ? $link1 : $link2;
if (!empty($count))
echo $count;
}
}
}
/**
* ���������� ���� � ������ ������ �������
*
* @param int $var ����� � Unix �������
*
* @return string ����������������� �����
*/
public static function display_date($var)
{
$shift = (self::$system_set['timeshift'] + self::$user_set['timeshift']) * 3600;
if (date('Y', $var) == date('Y', time())) {
if (date('z', $var + $shift) == date('z', time() + $shift))
return self::$lng['today'] . ', ' . date("H:i", $var + $shift);
if (date('z', $var + $shift) == date('z', time() + $shift) - 1)
return self::$lng['yesterday'] . ', ' . date("H:i", $var + $shift);
}
return date("d.m.Y / H:i", $var + $shift);
}
/**
* ��������� �� �������
*
* @param string|array $error ��������� �� ������ (��� ������ � �����������)
* @param string $link �������������� ������ ��������
*
* @return bool|string
*/
public static function display_error($error = '', $link = '')
{
if (!empty($error)) {
return '<div class="rmenu"><p><b>' . self::$lng['error'] . '!</b><br />' .
(is_array($error) ? implode('<br />', $error) : $error) . '</p>' .
(!empty($link) ? '<p>' . $link . '</p>' : '') . '</div>';
} else {
return FALSE;
}
}
/**
* ����������� ��������� ����
*
* @param array $val
* @param string $delimiter ����������� ����� ��������
* @param string $end_space ��������� � �����
*
* @return string
*/
public static function display_menu($val = array(), $delimiter = ' | ', $end_space = '')
{
return implode($delimiter, array_diff($val, array(''))) . $end_space;
}
/**
* ������������ ���������
* �� ������ ����� ������������ ������� �� ������ SMF 2.x.x
*
* @param string $url
* @param int $start
* @param int $total
* @param int $kmess
*
* @return string
*/
public static function display_pagination($url, $start, $total, $kmess)
{
$neighbors = 2;
if ($start >= $total)
$start = max(0, $total - (($total % $kmess) == 0 ? $kmess : ($total % $kmess)));
else
$start = max(0, (int)$start - ((int)$start % (int)$kmess));
$base_link = '<a class="pagenav" href="' . strtr($url, array('%' => '%%')) . 'page=%d' . '">%s</a>';
$out[] = $start == 0 ? '' : sprintf($base_link, $start / $kmess, '<<');
if ($start > $kmess * $neighbors)
$out[] = sprintf($base_link, 1, '1');
if ($start > $kmess * ($neighbors + 1))
$out[] = '<span style="font-weight: bold;">...</span>';
for ($nCont = $neighbors; $nCont >= 1; $nCont--)
if ($start >= $kmess * $nCont) {
$tmpStart = $start - $kmess * $nCont;
$out[] = sprintf($base_link, $tmpStart / $kmess + 1, $tmpStart / $kmess + 1);
}
$out[] = '<span class="currentpage"><b>' . ($start / $kmess + 1) . '</b></span>';
$tmpMaxPages = (int)(($total - 1) / $kmess) * $kmess;
for ($nCont = 1; $nCont <= $neighbors; $nCont++)
if ($start + $kmess * $nCont <= $tmpMaxPages) {
$tmpStart = $start + $kmess * $nCont;
$out[] = sprintf($base_link, $tmpStart / $kmess + 1, $tmpStart / $kmess + 1);
}
if ($start + $kmess * ($neighbors + 1) < $tmpMaxPages)
$out[] = '<span style="font-weight: bold;">...</span>';
if ($start + $kmess * $neighbors < $tmpMaxPages)
$out[] = sprintf($base_link, $tmpMaxPages / $kmess + 1, $tmpMaxPages / $kmess + 1);
if ($start + $kmess < $total) {
$display_page = ($start + $kmess) > $total ? $total : ($start / $kmess + 2);
$out[] = sprintf($base_link, $display_page, '>>');
}
return implode(' ', $out);
}
/**
* ���������� �������������� ������������
*
* @param int $user_id
* @param string $place
*
* @return mixed|string
*/
public static function display_place($user_id = 0, $place = '')
{
global $headmod;
$place = explode(",", $place);
$placelist = parent::load_lng('places');
if (array_key_exists($place[0], $placelist)) {
if ($place[0] == 'profile') {
if ($place[1] == $user_id) {
return '<a href="' . self::$system_set['homeurl'] . '/users/profile.php?user=' . $place[1] . '">' . $placelist['profile_personal'] . '</a>';
} else {
$user = self::get_user($place[1]);
return $placelist['profile'] . ': <a href="' . self::$system_set['homeurl'] . '/users/profile.php?user=' . $user['id'] . '">' . $user['name'] . '</a>';
}
} elseif ($place[0] == 'online' && isset($headmod) && $headmod == 'online') {
return $placelist['here'];
} else {
return str_replace('#home#', self::$system_set['homeurl'], $placelist[$place[0]]);
}
}
return '<a href="' . self::$system_set['homeurl'] . '/index.php">' . $placelist['homepage'] . '</a>';
}
/**
* ����������� ������ ������ ������������
*
* @param int $user ������ ������� � ������� `users`
* @param array $arg ������ ���������� �����������
* [lastvisit] (boolean) ���� � ����� ���������� ������
* [stshide] (boolean) ������ ������ (���� ����)
* [iphide] (boolean) ������ (�� ����������) IP � UserAgent
* [iphist] (boolean) ���������� ������ �� ������� IP
*
* [header] (string) ����� � ������ ����� ���� ������������
* [body] (string) �������� �����, ��� ����� ������������
* [sub] (string) ������ ��������� ������ ������� "sub"
* [footer] (string) ������ ��������� ����� ������� "sub"
*
* @return string
*/
public static function display_user($user = 0, $arg = array())
{
global $mod;
$out = FALSE;
// ������� ��� �������� �����
$clr = mysql_fetch_assoc(mysql_query("SELECT `cnick`,`cstat` FROM `users` WHERE `id`='".$user['id']."'"));
$cnick = $clr['cnick'];
$cstat = $clr['cstat'];
if (!$user['id']) {
$out = '<b>' . self::$lng['guest'] . '</b>';
if (!empty($user['name']))
$out .= ': ' . $user['name'];
if (!empty($arg['header']))
$out .= ' ' . $arg['header'];
} else {
if (self::$user_set['avatar']) {
$out .= '<table cellpadding="0" cellspacing="0"><tr><td>';
if (file_exists(($rootpath . 'files/users/avatar/' . $user['id'] . '.png')))
$out .= '<img src="' . self::$system_set['homeurl'] . '/files/users/avatar/' . $user['id'] . '.png" width="32" height="32" alt="" /> ';
else
$out .= '<img src="' . self::$system_set['homeurl'] . '/images/empty.png" width="32" height="32" alt="" /> ';
$out .= '</td><td>';
}
if ($user['sex'])
$out .= '<img src="' . self::$system_set['homeurl'] . '/theme/' . self::$user_set['skin'] . '/images/' . ($user['sex'] == 'm' ? 'm' : 'w') . ($user['datereg'] > time() - 86400 ? '_new' : '')
. '.png" width="16" height="16" align="middle" alt="' . ($user['sex'] == 'm' ? '�' : '�') . '" /> ';
else
$out .= '<img src="' . self::$system_set['homeurl'] . '/images/del.png" width="12" height="12" align="middle" /> ';
$out .= !self::$user_id || self::$user_id == $user['id'] ? ''.(strlen($cnick) == 1 ? '<b>' . $user['name'] . '</b>' : '<font color="'.$cnick.'"><b>' . $user['name'] . '</b></font>').'' : ''.(strlen($cnick) == 1 ? '<a href="' . self::$system_set['homeurl'] . '/users/profile.php?user=' . $user['id'] . '"><b>' . $user['name'] . '</b></a>' : '<a href="' . self::$system_set['homeurl'] . '/users/profile.php?user=' . $user['id'] . '"><font color="'.$cnick.'"><b>' . $user['name'] . '</b></font></a>').'';
$rank = array(
0 => '(����������)',
1 => '(GMod)',
2 => '(CMod)',
3 => '(FMod)',
4 => '(DMod)',
5 => '(LMod)',
6 => '(Smd)',
7 => '(�������������)',
9 => '(���������)'
);
$out .= ' ' . $rank[$user['rights']];
$out .= (time() > $user['lastdate'] + 300 ? '<span class="red"> [OFFLINE]</span>' : '<span class="green"> [ONLINE]</span>');
if (!empty($arg['header']))
$out .= ' ' . $arg['header'];
if (!isset($arg['stshide']) && !empty($user['status']))
$out .= '<div class="status"><img src="' . self::$system_set['homeurl'] . '/theme/' . self::$user_set['skin'] . '/img/icons/add.gif" alt="" align="middle" /> '.(strlen($cstat) == 1 ? '' . $user['status'] . '' : '<font color="'.$cstat.'">' . $user['status'] . '</font>').'</div>';
if (self::$user_set['avatar'])
$out .= '</td></tr></table>';
}
if (isset($arg['body']))
$out .= '<div>' . $arg['body'] . '</div>';
$ipinf = !isset($arg['iphide']) && self::$user_rights ? 1 : 0;
$lastvisit = time() > $user['lastdate'] + 300 && isset($arg['lastvisit']) ? self::display_date($user['lastdate']) : FALSE;
if ($ipinf || $lastvisit || isset($arg['sub']) && !empty($arg['sub']) || isset($arg['footer'])) {
$out .= '<div class="sub">';
if (isset($arg['sub'])) {
$out .= '<div>' . $arg['sub'] . '</div>';
}
if ($lastvisit) {
$out .= '<div><span class="gray">' . self::$lng['last_visit'] . ':</span> ' . $lastvisit . '</div>';
}
$iphist = '';
if ($ipinf) {
$out .= '<div><span class="gray">' . self::$lng['browser'] . ':</span> ' . htmlspecialchars($user['browser']) . '</div>' .
'<div><span class="gray">' . self::$lng['ip_address'] . ':</span> ';
$hist = $mod == 'history' ? '&mod=history' : '';
$ip = long2ip($user['ip']);
if (self::$user_rights && isset($user['ip_via_proxy']) && $user['ip_via_proxy']) {
$out .= '<b class="red"><a href="' . self::$system_set['homeurl'] . '/' . self::$system_set['admp'] . '/index.php?act=search_ip&ip=' . $ip . $hist . '">' . $ip . '</a></b>';
$out .= ' [<a href="' . self::$system_set['homeurl'] . '/' . self::$system_set['admp'] . '/index.php?act=ip_whois&ip=' . $ip . '">?</a>]';
$out .= ' / ';
$out .= '<a href="' . self::$system_set['homeurl'] . '/' . self::$system_set['admp'] . '/index.php?act=search_ip&ip=' . long2ip($user['ip_via_proxy']) . $hist . '">' . long2ip($user['ip_via_proxy']) . '</a>';
$out .= ' [<a href="' . self::$system_set['homeurl'] . '/' . self::$system_set['admp'] . '/index.php?act=ip_whois&ip=' . long2ip($user['ip_via_proxy']) . '">?</a>]';
} elseif (self::$user_rights) {
$out .= '<a href="' . self::$system_set['homeurl'] . '/' . self::$system_set['admp'] . '/index.php?act=search_ip&ip=' . $ip . $hist . '">' . $ip . '</a>';
$out .= ' [<a href="' . self::$system_set['homeurl'] . '/' . self::$system_set['admp'] . '/index.php?act=ip_whois&ip=' . $ip . '">?</a>]';
} else {
$out .= $ip . $iphist;
}
if (isset($arg['iphist'])) {
$iptotal = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_users_iphistory` WHERE `user_id` = '" . $user['id'] . "'"), 0);
$out .= '<div><span class="gray">' . self::$lng['ip_history'] . ':</span> <a href="' . self::$system_set['homeurl'] . '/users/profile.php?act=ip&user=' . $user['id'] . '">[' . $iptotal . ']</a></div>';
}
$out .= '</div>';
}
if (isset($arg['footer']))
$out .= $arg['footer'];
$out .= '</div>';
}
return $out;
}
/**
* �������������� ����� �����
*
* @param string $name
*
* @return string
*/
public static function format($name)
{
$f1 = strrpos($name, ".");
$f2 = substr($name, $f1 + 1, 999);
$fname = strtolower($f2);
return $fname;
}
/**
* �������� ������ ������������
*
* @param int $id ������������� ������������
*
* @return array|bool
*/
public static function get_user($id = 0)
{
if ($id && $id != self::$user_id) {
$req = mysql_query("SELECT * FROM `users` WHERE `id` = '$id'");
if (mysql_num_rows($req)) {
return mysql_fetch_assoc($req);
} else {
return FALSE;
}
} else {
return self::$user_data;
}
}
public static function image($name, $args = array())
{
if (is_file(ROOTPATH . 'theme' . DIRECTORY_SEPARATOR . core::$user_set['skin'] . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $name)) {
$src = core::$system_set['homeurl'] . '/theme/' . core::$user_set['skin'] . '/images/' . $name;
} elseif (is_file(ROOTPATH . 'images' . DIRECTORY_SEPARATOR . $name)) {
$src = core::$system_set['homeurl'] . '/images/' . $name;
} else {
return false;
}
return '<img src="' . $src . '" alt="' . (isset($args['alt']) ? $args['alt'] : '') . '"' .
(isset($args['width']) ? ' width="' . $args['width'] . '"' : '') .
(isset($args['height']) ? ' height="' . $args['height'] . '"' : '') .
' class="' . (isset($args['class']) ? $args['class'] : 'icon') . '"/>';
}
/**
* �������� �� ��������� ���� ������?
*
* @param int $id ������������� ������������, �������� ���������
*
* @return bool
*/
public static function is_friend($id = 0)
{
static $user_id = NULL;
static $return = FALSE;
if (!self::$user_id && !$id) {
return FALSE;
}
if (is_null($user_id) || $id != $user_id) {
$query = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_contact` WHERE `type` = '2' AND ((`from_id` = '$id' AND `user_id` = '" . self::$user_id . "') OR (`from_id` = '" . self::$user_id . "' AND `user_id` = '$id'))"), 0);
$return = $query == 2 ? TRUE : FALSE;
}
return $return;
}
/**
* ��������� �� ��������� ������������ � ��������� � ������?
*
* @param int $id ������������� ������������, �������� ���������
*
* @return int ��������� �������:
* 0 - �� � ���������
* 1 - � ���������
* 2 - � ������ � ����
*/
public static function is_contact($id = 0)
{
static $user_id = NULL;
static $return = 0;
if (!self::$user_id && !$id) {
return 0;
}
if (is_null($user_id) || $id != $user_id) {
$user_id = $id;
$req_1 = mysql_query("SELECT * FROM `cms_contact` WHERE `user_id` = '" . self::$user_id . "' AND `from_id` = '$id'");
if (mysql_num_rows($req_1)) {
$res_1 = mysql_fetch_assoc($req_1);
if ($res_1['ban'] == 1) {
$return = 2;
} else {
$return = 1;
}
} else {
$return = 0;
}
}
return $return;
}
/**
* �������� �� ����� � ����������
*
* @param $id
*
* @return bool
*/
public static function is_ignor($id)
{
static $user_id = NULL;
static $return = FALSE;
if (!self::$user_id && !$id) {
return FALSE;
}
if (is_null($user_id) || $id != $user_id) {
$user_id = $id;
$req_2 = mysql_query("SELECT * FROM `cms_contact` WHERE `user_id` = '$id' AND `from_id` = '" . self::$user_id . "'");
if (mysql_num_rows($req_2)) {
$res_2 = mysql_fetch_assoc($req_2);
if ($res_2['ban'] == 1) {
$return = TRUE;
}
}
}
return $return;
}
/*
-----------------------------------------------------------------
�������������� � �������� � ��������
-----------------------------------------------------------------
*/
public static function rus_lat($str)
{
$replace = array(
'�' => 'a',
'�' => 'b',
'�' => 'v',
'�' => 'g',
'�' => 'd',
'�' => 'e',
'�' => 'e',
'�' => 'j',
'�' => 'z',
'�' => 'i',
'�' => 'i',
'�' => 'k',
'�' => 'l',
'�' => 'm',
'�' => 'n',
'�' => 'o',
'�' => 'p',
'�' => 'r',
'�' => 's',
'�' => 't',
'�' => 'u',
'�' => 'f',
'�' => 'h',
'�' => 'c',
'�' => 'ch',
'�' => 'sh',
'�' => 'sch',
'�' => "",
'�' => 'y',
'�' => "",
'�' => 'ye',
'�' => 'yu',
'�' => 'ya'
);
return strtr($str, $replace);
}
/*
-----------------------------------------------------------------
��������� �������
-----------------------------------------------------------------
*/
public static function smileys($str, $adm = FALSE)
{
static $smileys_cache = array();
if (empty($smileys_cache)) {
$file = ROOTPATH . 'files/cache/smileys.dat';
if (file_exists($file) && ($smileys = file_get_contents($file)) !== FALSE) {
$smileys_cache = unserialize($smileys);
return strtr($str, ($adm ? array_merge($smileys_cache['usr'], $smileys_cache['adm']) : $smileys_cache['usr']));
} else {
return $str;
}
} else {
return strtr($str, ($adm ? array_merge($smileys_cache['usr'], $smileys_cache['adm']) : $smileys_cache['usr']));
}
}
/*
-----------------------------------------------------------------
������� ��������� �� ���, ��� ����
-----------------------------------------------------------------
*/
public static function timecount($var)
{
global $lng;
if ($var < 0) $var = 0;
$day = ceil($var / 86400);
if ($var > 345600) return $day . ' ' . $lng['timecount_days'];
if ($var >= 172800) return $day . ' ' . $lng['timecount_days_r'];
if ($var >= 86400) return '1 ' . $lng['timecount_day'];
return date("G:i:s", mktime(0, 0, $var));
}
/*
-----------------------------------------------------------------
�������������� ������
-----------------------------------------------------------------
*/
public static function trans($str)
{
$replace = array(
'a' => '�',
'b' => '�',
'v' => '�',
'g' => '�',
'd' => '�',
'e' => '�',
'yo' => '�',
'zh' => '�',
'z' => '�',
'i' => '�',
'j' => '�',
'k' => '�',
'l' => '�',
'm' => '�',
'n' => '�',
'o' => '�',
'p' => '�',
'r' => '�',
's' => '�',
't' => '�',
'u' => '�',
'f' => '�',
'h' => '�',
'c' => '�',
'ch' => '�',
'w' => '�',
'sh' => '�',
'q' => '�',
'y' => '�',
'x' => '�',
'yu' => '�',
'ya' => '�',
'A' => '�',
'B' => '�',
'V' => '�',
'G' => '�',
'D' => '�',
'E' => '�',
'YO' => '�',
'ZH' => '�',
'Z' => '�',
'I' => '�',
'J' => '�',
'K' => '�',
'L' => '�',
'M' => '�',
'N' => '�',
'O' => '�',
'P' => '�',
'R' => '�',
'S' => '�',
'T' => '�',
'U' => '�',
'F' => '�',
'H' => '�',
'C' => '�',
'CH' => '�',
'W' => '�',
'SH' => '�',
'Q' => '�',
'Y' => '�',
'X' => '�',
'YU' => '�',
'YA' => '�'
);
return strtr($str, $replace);
}
/*
-----------------------------------------------------------------
������ ������� �������� ����������.
� ����� ����������� �� ���������!
������ ������ ������� ������������ checkin()
-----------------------------------------------------------------
*/
public static function check($str)
{
$str = htmlentities(trim($str), ENT_QUOTES, 'UTF-8');
$str = self::checkin($str);
$str = nl2br($str);
$str = mysql_real_escape_string($str);
return $str;
}
}