/explay/functions.php
PHP | 447 lines | 285 code | 56 blank | 106 comment | 68 complexity | 9de731b4e8d0139086589f4bad9b7d13 MD5 | raw file
- <?php
- function __autoload ($className) {
- $classFile = ENGINE_ROOT . '/classes/' . $className . '/' . $className . '.php';
- $classInterface = ENGINE_ROOT . '/classes/' . $className . '/i' . $className . '.php';
-
- if (!file_exists ($classFile) || !file_exists ($classInterface)) {
- trigger_error ('Class ' . $className . ' or his interface not found!', E_USER_ERROR);
- }
-
- $langFile = ENGINE_ROOT . '/classes/' . $className . '/i18n.php';
-
- if (file_exists ($langFile)) {
- loadLang ($langFile, $className);
- }
-
- include $classInterface;
- include $classFile;
- }
-
- /**
- * ???? mod_rewrite
- */
- function rewriteURI () {
- include ENGINE_ROOT . '/URIs.php';
-
- if (empty ($REWRITE)) {
- throw new CoreException ('Rewrite rules not found!');
- }
-
- if (empty ($_REQUEST['u'])) {
- return;
- }
-
- $base = $_REQUEST['u'];
-
- foreach ($REWRITE as $find => $set) {
- $findLen = mb_strlen ($find);
- if ($find == mb_substr ($base, 0, $findLen) && (mb_strlen ($base) == $findLen || mb_substr ($base, $findLen, 1) == '/')) {
- $base = $set . mb_substr ($base, mb_strlen ($find));
- break;
- }
- }
-
- $_REQUEST['u'] = $base;
- }
-
- /**
- * ?????????? ????????? ???????? ?????? ????????????
- * @param integer $num ????? ??????
- * @return string
- */
- function userGroup ($num) {
- $a = array (
- 0 => 'admin',
- 1 => 'moderator',
- 2 => 'user',
- 3 => 'guest'
- );
-
- $num = (int) $num;
-
- return isset ($a[$num]) ? $a[$num] : 'guest';
- }
- /**
- * ?????????? ???????? ???????? ?????????? ? ?????? $label ? ?????????? $key
- * @param string $label ???? ??????????
- * @param string $key ???? ?????????? (???????? ?????? ? ?.?.)
- * @return string ???????; $label, ???? ???????? ?????????? ?? ??????????
- */
- function lang ($label, $key) {
- global $g_langCollection;
- return isset ($g_langCollection[$key][$label]) ? $g_langCollection[$key][$label] : $label;
- }
- /**
- * ?????????? ?????? ? ????????? ??????????? ?? ?????????? ????? ? ???????? ?????????
- * @param string $langFile ?????? ???? ?? ?????
- * @param string $key ???? ?????????? (???????? ?????? ? ?.?.)
- * @return void
- */
- function loadLang ($langFile, $key) {
- global $g_langCollection;
-
- if (file_exists ($langFile)) {
- include $langFile;
- if (isset($LANG) && is_array ($LANG)) {
- if (isset($g_langCollection[$key])) {
- $g_langCollection[$key] = array_merge($g_langCollection[$key], $LANG);
- } else {
- $g_langCollection[$key] = $LANG;
- }
- unset ($LANG);
- }
- }
- }
-
- function getLangVars($key) {
- global $g_langCollection;
-
- return isset($g_langCollection[$key]) ? $g_langCollection[$key] : array();
- }
- /**
- * ?????????? ?????????? ???????? ?????????? ?? ??????? $_REQUEST
- * @param string $key ???? ??????????
- * @param string $type ??? ?????????? ????????: s - ??????, i - ????? ?????, f - ????? ? ????????? ??????, b - ??????? ?????????
- * @return mixed
- */
- function getRequest ($key, $type = 's') {
- switch ($type) {
- case 's' :
- case 'str' :
- case 'string' : {
- return isset ($_REQUEST[$key]) ? $_REQUEST[$key] : '';
- }
- case 'i' :
- case 'int' :
- case 'integer' :
- case 'number' : {
- return isset ($_REQUEST[$key]) ? (int) $_REQUEST[$key] : 0;
- }
- case 'b' :
- case 'bool' :
- case 'boolean' : {
- return isset ($_REQUEST[$key]) ? true : false;
- }
- case 'f' :
- case 'float' : {
- return (float) isset ($_REQUEST[$key]) ? $_REQUEST[$key] : 0;
- }
- default : {
- return isset ($_REQUEST[$key]) ? $_REQUEST[$key] : '';
- }
- }
- }
-
- /**
- * ?????????? ?????????? ???????? ?????????? ?? ??????? $_POST
- * @param string $key ???? ??????????
- * @param string $type ??? ?????????? ????????: s - ??????, i - ????? ?????, f - ????? ? ????????? ??????, b - ??????? ?????????
- * @return mixed
- */
- function getPost ($key, $type = 's') {
- switch ($type) {
- case 's' :
- case 'str' :
- case 'string' : {
- return isset ($_POST[$key]) ? $_POST[$key] : '';
- }
- case 'i' :
- case 'int' :
- case 'integer' :
- case 'number' : {
- return isset ($_POST[$key]) ? (int) $_POST[$key] : 0;
- }
- case 'b' :
- case 'bool' :
- case 'boolean' : {
- return isset ($_POST[$key]) ? true : false;
- }
- case 'f' :
- case 'float' : {
- return (float) isset ($_POST[$key]) ? $_POST[$key] : 0;
- }
- default : {
- return isset ($_POST[$key]) ? $_POST[$key] : '';
- }
- }
- }
- /**
- * ?????????? ???????? ?????????? ?? ??????? $_SERVER
- * @param string $key ???? ??????????
- * @return mixed
- */
- function getServer ($key) {
- return isset($_SERVER[$key]) ? $_SERVER[$key] : false;
- }
- /**
- * ?????????? ?????????? ???????? cookie ????????????
- * @param string $key ???????? ??????????
- * @return string
- */
- function getCookie ($key) {
- return isset($_COOKIE[$key]) ? safeSql ($_COOKIE[$key]) : '';
- }
- /**
- * ????????????? ???????? cookie ????????????
- * @param string $name ???????? ??????????
- * @param mixed $value ????????
- * @param integer $lifetime ????? ????? ? ???????? (??-????????? - ??????? ???????? ?? ??????? "//settings/site/cookie_lifetime")
- * @return void
- */
- function cmsSetCookie ($name, $value, $lifetime = false) {
- if ($lifetime === false) {
- $lifetime = Regedit::getInstance()->get ('//settings/site/cookie_lifetime');
- }
-
- setcookie ((string) $name, (string) $value, time() + $lifetime, '/');
- }
- /**
- * ?????????? ???????? ?????????? ?????? ????????????
- * @param string $key ??? ??????????
- * @return string
- */
- function getSession ($key) {
- return isset($_SESSION[$key]) ? safeSql ($_SESSION[$key]) : false;
- }
- /**
- * ????????????? ???????? ?????????? ?????? ????????????
- * @param string $name ??? ??????????
- * @param mixed $value ????????
- * @return
- */
- function setSession ($name, $value) {
- $_SESSION[$name] = $value;
- }
- /**
- * ?????????? ?????????? ?? mysql-???????? ??????
- * @param string $s
- * @return string
- */
- function safeSql ($s, $force = false) {
- if ($force || !get_magic_quotes_gpc()) {
- if (is_string ($s)) {
- $s = mysql_real_escape_string ((string) $s);
- return $s;
- }
- elseif (is_array ($s)) {
- foreach ($s as $key => $v) {
- $s[$key] = mysql_real_escape_string ((string) $v);
- }
- return $s;
- } else {
- return $s;
- }
- }
-
- return $s;
- }
-
- /**
- * ?????????? ?????????? ?? ??????, ??????? ????? ?????????? ? URI
- * @param string $s
- * @return string
- */
- function safeURI ($s) {
- $s = str_replace (';', '', $s);
- $s = str_replace ("'", '', $s);
- $s = str_replace ('"', '', $s);
- $s = str_replace ('\\', '', $s);
- $s = mb_substr ($s, 0, 255);
- return $s;
- }
- /**
- * ?????????????? ???????????? ?? ???????? URI
- * @param string $uri
- * @return void
- */
- function redirect ($uri) {
- header ('Location: ' . $uri);
- exit;
- }
- /**
- * ?????????????? ???????????? ?? ????? ?????????? ? ?????????? $_REQUEST['referer'] ??? $_SESSION['referer']
- * @param string $url URI, ???? ????? ????????????? ????????????, ???? ?? ????? ???????? ???? ????????????? ??????????
- * @return void
- */
- function autoRedirect ($uri = '/') {
- if ($referer = getRequest('referer')) {
- header ('Location: ' . $referer);
- } elseif ($referer = getSession ('referer')) {
- header ('Location: ' . $referer);
- } else {
- header ('Location: ' . $uri);
- }
- exit;
- }
- /**
- * ??????? ?????? ? ??????????? ?????????? ????? ?????
- * @param string $string
- * @return string
- */
- function explayHash ($string) {
- return md5 (hash ('sha256', (string) $string));
- }
- /**
- * ??????????? ??????? ?????? ? ?????? ???????, ???????? ??????? ????? ?? ????????? ????????? ? ??????????/???????? ????????? ????? ??????????
- * @param string $string
- * @return string
- */
- function translit ($string) {
- $aFrom = array ('?','?','?','?','?','?','?','?', '?', '?','?','?','?','?','?','?','?','?','?','?','?','?','?','?', '?', '?','?','?','?','?','?','?', '?');
- $aTo = array ('y','c','u','k','e','n','g','sh','sh','z','h','', 'f','i','v','a','p','r','o','l','d','j','e','ya','ch','s','m','i','t','', 'b','yu','e');
- $rel = array ();
-
- $cnt = count ($aFrom);
-
- for ($i = 0; $i < $cnt; ++$i) {
- $aFrom[$i] = '#' . $aFrom[$i] . '#iu';
- }
-
- $string = mb_strtolower ($string);
- $string = preg_replace ($aFrom, $aTo, $string);
-
- $string = preg_replace("/[\/\\\',\t`\^\[\]]*/", "", $string);
- $string = str_replace(chr(8470), "", $string);
- $string = preg_replace("/[ \.]+/", "_", $string);
- $string = preg_replace("/([_]+)/", "_", $string);
- //$string = trim(trim($string), "_");
-
- return $string;
- }
-
- /**
- * ??????? ?????????????? ??????? ? ????????????? ??????
- * @param string $string ????? ? ??????? UNIX
- * @return string
- */
- function formatDate ($string, $isBirthdate = false) {
- $today = intval (date('H')) * 3600 + intval(date('i')) * 60 + intval(date('s'));
-
- if (date ('Y', $string) != date ('Y')) {
- $date = date ($isBirthdate ? 'd F Y' : 'd F Y, H:i', $string);
- $date = str_replace ("January", "??????", $date);
- $date = str_replace ("February", "???????", $date);
- $date = str_replace ("March", "?????", $date);
- $date = str_replace ("April", "??????", $date);
- $date = str_replace ("May", "???", $date);
- $date = str_replace ("June", "????", $date);
- $date = str_replace ("July", "????", $date);
- $date = str_replace ("August", "???????", $date);
- $date = str_replace ("September", "????????", $date);
- $date = str_replace ("October", "???????", $date);
- $date = str_replace ("November", "??????", $date);
- $date = str_replace ("December", "???????", $date);
- return $date;
- }
-
- if ((time() - $string) < 120) {
- $date = '?????? ???';
- }
- elseif ((time() - $string) < 3600) {
- $date = getNumWithCase ((int) (date ('i', (time() - $string))), array ('??????', '??????', '?????'),false).' ?????';
- }
- elseif ((time() - $string) < $today) {
- $date = '??????? ? '.date('H:i', $string);
- }
- else {
- $date = date ($isBirthdate ? 'j F' : 'j F, H:i', $string);
- $date = str_replace ("January", "??????", $date);
- $date = str_replace ("February", "???????", $date);
- $date = str_replace ("March", "?????", $date);
- $date = str_replace ("April", "??????", $date);
- $date = str_replace ("May", "???", $date);
- $date = str_replace ("June", "????", $date);
- $date = str_replace ("July", "????", $date);
- $date = str_replace ("August", "???????", $date);
- $date = str_replace ("September", "????????", $date);
- $date = str_replace ("October", "???????", $date);
- $date = str_replace ("November", "??????", $date);
- $date = str_replace ("December", "???????", $date);
- }
- return $date;
- }
-
- /**
- * ???????, ???????????? ?????? ????? ????? ? ??????????? ?? ???????? ????? $cnt (??????????? ??????)
- * @param integer $cnt ?????
- * @param $case ?????? ?? 3-? ????????? - ???? ?????????? ?????
- * @param boolean $word ??? ????????? ????? ????????? ? true ????? ?????? ???? ????? ?????????? ?? ????? "???"
- * @return string
- */
- function getNumWithCase ($cnt, $case, $word = true) {
- $cnt1 = $cnt;
- $cnt = str_replace ('-', '', $cnt);
-
- (mb_strlen($cnt) == 1) ? $minus = 1 : $minus = 2;
- $temp1 = mb_substr ($cnt, mb_strlen($cnt)-$minus, mb_strlen($cnt));
- $temp2 = mb_substr ($cnt, mb_strlen($cnt)-2, mb_strlen($cnt));
- if ($cnt == 0) $count = (($word) ? '??? ' : '0 ').$case[2];
- elseif ($temp2 == 11 || $temp2 == 12 || $temp2 == 13 || $temp2 == 14) $count = "$cnt1 ".$case[2];
- elseif ($cnt == 1) $count = "$cnt1 ".$case[0];
- elseif ($temp1 == 2 || $temp1 == 3 || $temp1 == 4) $count = "$cnt1 ".$case[1];
- else $count = "$cnt1 ".$case[2];
-
- return $count;
- }
-
- /**
- * ??????? ???????? ??????????, ?????? ?? ????????? ???? ????? $cnt
- */
- function getCaseByNum ($cnt, $case, $word = true) {
- $cnt = str_replace ('-', '', $cnt);
-
- (mb_strlen($cnt) == 1) ? $minus = 1 : $minus = 2;
- $temp1 = mb_substr($cnt, mb_strlen($cnt)-$minus, mb_strlen($cnt));
- $temp2 = mb_substr($cnt, mb_strlen($cnt)-2, mb_strlen($cnt));
- if ($cnt == 0) $count = (($word) ? '??? ' : '0 ').$case[2];
- elseif ($temp2 == 11 || $temp2 == 12 || $temp2 == 13 || $temp2 == 14) $count = $case[2];
- elseif ($cnt == 1) $count = $case[0];
- elseif ($temp1 == 2 || $temp1 == 3 || $temp1 == 4) $count = $case[1];
- else $count = $case[2];
-
- return $count;
-
- }
-
- function GET2String (array $get) {
- $a = array ();
-
- unset ($get['u'], $get['page'], $get['force_format']);
-
- foreach ($get as $key => $value) {
- if (is_array ($value)) {
- foreach ($value as $key2 => $value2) {
- $a[] .= $key . '[' . $key2 . ']=' . $value2;
- }
- } else {
- $a[] .= $key . ($value != '' ? '=' . $value : '');
- }
- }
-
- return implode ('&', $a);
- }
-
- function createHash ($numChars) {
- $s = '0123456789qwertyuiopasdfghjklzxcvbnm';
-
- $res = '';
- for ($i = 0; $i < $numChars; ++$i) {
- $res .= $s[mt_rand(0,mb_strlen($s)-1)];
- }
-
- return $res;
- }