PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/explay/functions.php

http://explay-cms.googlecode.com/
PHP | 447 lines | 285 code | 56 blank | 106 comment | 68 complexity | 9de731b4e8d0139086589f4bad9b7d13 MD5 | raw file
  1. <?php
  2. function __autoload ($className) {
  3. $classFile = ENGINE_ROOT . '/classes/' . $className . '/' . $className . '.php';
  4. $classInterface = ENGINE_ROOT . '/classes/' . $className . '/i' . $className . '.php';
  5. if (!file_exists ($classFile) || !file_exists ($classInterface)) {
  6. trigger_error ('Class ' . $className . ' or his interface not found!', E_USER_ERROR);
  7. }
  8. $langFile = ENGINE_ROOT . '/classes/' . $className . '/i18n.php';
  9. if (file_exists ($langFile)) {
  10. loadLang ($langFile, $className);
  11. }
  12. include $classInterface;
  13. include $classFile;
  14. }
  15. /**
  16. * ???? mod_rewrite
  17. */
  18. function rewriteURI () {
  19. include ENGINE_ROOT . '/URIs.php';
  20. if (empty ($REWRITE)) {
  21. throw new CoreException ('Rewrite rules not found!');
  22. }
  23. if (empty ($_REQUEST['u'])) {
  24. return;
  25. }
  26. $base = $_REQUEST['u'];
  27. foreach ($REWRITE as $find => $set) {
  28. $findLen = mb_strlen ($find);
  29. if ($find == mb_substr ($base, 0, $findLen) && (mb_strlen ($base) == $findLen || mb_substr ($base, $findLen, 1) == '/')) {
  30. $base = $set . mb_substr ($base, mb_strlen ($find));
  31. break;
  32. }
  33. }
  34. $_REQUEST['u'] = $base;
  35. }
  36. /**
  37. * ?????????? ????????? ???????? ?????? ????????????
  38. * @param integer $num ????? ??????
  39. * @return string
  40. */
  41. function userGroup ($num) {
  42. $a = array (
  43. 0 => 'admin',
  44. 1 => 'moderator',
  45. 2 => 'user',
  46. 3 => 'guest'
  47. );
  48. $num = (int) $num;
  49. return isset ($a[$num]) ? $a[$num] : 'guest';
  50. }
  51. /**
  52. * ?????????? ???????? ???????? ?????????? ? ?????? $label ? ?????????? $key
  53. * @param string $label ???? ??????????
  54. * @param string $key ???? ?????????? (???????? ?????? ? ?.?.)
  55. * @return string ???????; $label, ???? ???????? ?????????? ?? ??????????
  56. */
  57. function lang ($label, $key) {
  58. global $g_langCollection;
  59. return isset ($g_langCollection[$key][$label]) ? $g_langCollection[$key][$label] : $label;
  60. }
  61. /**
  62. * ?????????? ?????? ? ????????? ??????????? ?? ?????????? ????? ? ???????? ?????????
  63. * @param string $langFile ?????? ???? ?? ?????
  64. * @param string $key ???? ?????????? (???????? ?????? ? ?.?.)
  65. * @return void
  66. */
  67. function loadLang ($langFile, $key) {
  68. global $g_langCollection;
  69. if (file_exists ($langFile)) {
  70. include $langFile;
  71. if (isset($LANG) && is_array ($LANG)) {
  72. if (isset($g_langCollection[$key])) {
  73. $g_langCollection[$key] = array_merge($g_langCollection[$key], $LANG);
  74. } else {
  75. $g_langCollection[$key] = $LANG;
  76. }
  77. unset ($LANG);
  78. }
  79. }
  80. }
  81. function getLangVars($key) {
  82. global $g_langCollection;
  83. return isset($g_langCollection[$key]) ? $g_langCollection[$key] : array();
  84. }
  85. /**
  86. * ?????????? ?????????? ???????? ?????????? ?? ??????? $_REQUEST
  87. * @param string $key ???? ??????????
  88. * @param string $type ??? ?????????? ????????: s - ??????, i - ????? ?????, f - ????? ? ????????? ??????, b - ??????? ?????????
  89. * @return mixed
  90. */
  91. function getRequest ($key, $type = 's') {
  92. switch ($type) {
  93. case 's' :
  94. case 'str' :
  95. case 'string' : {
  96. return isset ($_REQUEST[$key]) ? $_REQUEST[$key] : '';
  97. }
  98. case 'i' :
  99. case 'int' :
  100. case 'integer' :
  101. case 'number' : {
  102. return isset ($_REQUEST[$key]) ? (int) $_REQUEST[$key] : 0;
  103. }
  104. case 'b' :
  105. case 'bool' :
  106. case 'boolean' : {
  107. return isset ($_REQUEST[$key]) ? true : false;
  108. }
  109. case 'f' :
  110. case 'float' : {
  111. return (float) isset ($_REQUEST[$key]) ? $_REQUEST[$key] : 0;
  112. }
  113. default : {
  114. return isset ($_REQUEST[$key]) ? $_REQUEST[$key] : '';
  115. }
  116. }
  117. }
  118. /**
  119. * ?????????? ?????????? ???????? ?????????? ?? ??????? $_POST
  120. * @param string $key ???? ??????????
  121. * @param string $type ??? ?????????? ????????: s - ??????, i - ????? ?????, f - ????? ? ????????? ??????, b - ??????? ?????????
  122. * @return mixed
  123. */
  124. function getPost ($key, $type = 's') {
  125. switch ($type) {
  126. case 's' :
  127. case 'str' :
  128. case 'string' : {
  129. return isset ($_POST[$key]) ? $_POST[$key] : '';
  130. }
  131. case 'i' :
  132. case 'int' :
  133. case 'integer' :
  134. case 'number' : {
  135. return isset ($_POST[$key]) ? (int) $_POST[$key] : 0;
  136. }
  137. case 'b' :
  138. case 'bool' :
  139. case 'boolean' : {
  140. return isset ($_POST[$key]) ? true : false;
  141. }
  142. case 'f' :
  143. case 'float' : {
  144. return (float) isset ($_POST[$key]) ? $_POST[$key] : 0;
  145. }
  146. default : {
  147. return isset ($_POST[$key]) ? $_POST[$key] : '';
  148. }
  149. }
  150. }
  151. /**
  152. * ?????????? ???????? ?????????? ?? ??????? $_SERVER
  153. * @param string $key ???? ??????????
  154. * @return mixed
  155. */
  156. function getServer ($key) {
  157. return isset($_SERVER[$key]) ? $_SERVER[$key] : false;
  158. }
  159. /**
  160. * ?????????? ?????????? ???????? cookie ????????????
  161. * @param string $key ???????? ??????????
  162. * @return string
  163. */
  164. function getCookie ($key) {
  165. return isset($_COOKIE[$key]) ? safeSql ($_COOKIE[$key]) : '';
  166. }
  167. /**
  168. * ????????????? ???????? cookie ????????????
  169. * @param string $name ???????? ??????????
  170. * @param mixed $value ????????
  171. * @param integer $lifetime ????? ????? ? ???????? (??-????????? - ??????? ???????? ?? ??????? "//settings/site/cookie_lifetime")
  172. * @return void
  173. */
  174. function cmsSetCookie ($name, $value, $lifetime = false) {
  175. if ($lifetime === false) {
  176. $lifetime = Regedit::getInstance()->get ('//settings/site/cookie_lifetime');
  177. }
  178. setcookie ((string) $name, (string) $value, time() + $lifetime, '/');
  179. }
  180. /**
  181. * ?????????? ???????? ?????????? ?????? ????????????
  182. * @param string $key ??? ??????????
  183. * @return string
  184. */
  185. function getSession ($key) {
  186. return isset($_SESSION[$key]) ? safeSql ($_SESSION[$key]) : false;
  187. }
  188. /**
  189. * ????????????? ???????? ?????????? ?????? ????????????
  190. * @param string $name ??? ??????????
  191. * @param mixed $value ????????
  192. * @return
  193. */
  194. function setSession ($name, $value) {
  195. $_SESSION[$name] = $value;
  196. }
  197. /**
  198. * ?????????? ?????????? ?? mysql-???????? ??????
  199. * @param string $s
  200. * @return string
  201. */
  202. function safeSql ($s, $force = false) {
  203. if ($force || !get_magic_quotes_gpc()) {
  204. if (is_string ($s)) {
  205. $s = mysql_real_escape_string ((string) $s);
  206. return $s;
  207. }
  208. elseif (is_array ($s)) {
  209. foreach ($s as $key => $v) {
  210. $s[$key] = mysql_real_escape_string ((string) $v);
  211. }
  212. return $s;
  213. } else {
  214. return $s;
  215. }
  216. }
  217. return $s;
  218. }
  219. /**
  220. * ?????????? ?????????? ?? ??????, ??????? ????? ?????????? ? URI
  221. * @param string $s
  222. * @return string
  223. */
  224. function safeURI ($s) {
  225. $s = str_replace (';', '', $s);
  226. $s = str_replace ("'", '', $s);
  227. $s = str_replace ('"', '', $s);
  228. $s = str_replace ('\\', '', $s);
  229. $s = mb_substr ($s, 0, 255);
  230. return $s;
  231. }
  232. /**
  233. * ?????????????? ???????????? ?? ???????? URI
  234. * @param string $uri
  235. * @return void
  236. */
  237. function redirect ($uri) {
  238. header ('Location: ' . $uri);
  239. exit;
  240. }
  241. /**
  242. * ?????????????? ???????????? ?? ????? ?????????? ? ?????????? $_REQUEST['referer'] ??? $_SESSION['referer']
  243. * @param string $url URI, ???? ????? ????????????? ????????????, ???? ?? ????? ???????? ???? ????????????? ??????????
  244. * @return void
  245. */
  246. function autoRedirect ($uri = '/') {
  247. if ($referer = getRequest('referer')) {
  248. header ('Location: ' . $referer);
  249. } elseif ($referer = getSession ('referer')) {
  250. header ('Location: ' . $referer);
  251. } else {
  252. header ('Location: ' . $uri);
  253. }
  254. exit;
  255. }
  256. /**
  257. * ??????? ?????? ? ??????????? ?????????? ????? ?????
  258. * @param string $string
  259. * @return string
  260. */
  261. function explayHash ($string) {
  262. return md5 (hash ('sha256', (string) $string));
  263. }
  264. /**
  265. * ??????????? ??????? ?????? ? ?????? ???????, ???????? ??????? ????? ?? ????????? ????????? ? ??????????/???????? ????????? ????? ??????????
  266. * @param string $string
  267. * @return string
  268. */
  269. function translit ($string) {
  270. $aFrom = array ('?','?','?','?','?','?','?','?', '?', '?','?','?','?','?','?','?','?','?','?','?','?','?','?','?', '?', '?','?','?','?','?','?','?', '?');
  271. $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');
  272. $rel = array ();
  273. $cnt = count ($aFrom);
  274. for ($i = 0; $i < $cnt; ++$i) {
  275. $aFrom[$i] = '#' . $aFrom[$i] . '#iu';
  276. }
  277. $string = mb_strtolower ($string);
  278. $string = preg_replace ($aFrom, $aTo, $string);
  279. $string = preg_replace("/[\/\\\',\t`\^\[\]]*/", "", $string);
  280. $string = str_replace(chr(8470), "", $string);
  281. $string = preg_replace("/[ \.]+/", "_", $string);
  282. $string = preg_replace("/([_]+)/", "_", $string);
  283. //$string = trim(trim($string), "_");
  284. return $string;
  285. }
  286. /**
  287. * ??????? ?????????????? ??????? ? ????????????? ??????
  288. * @param string $string ????? ? ??????? UNIX
  289. * @return string
  290. */
  291. function formatDate ($string, $isBirthdate = false) {
  292. $today = intval (date('H')) * 3600 + intval(date('i')) * 60 + intval(date('s'));
  293. if (date ('Y', $string) != date ('Y')) {
  294. $date = date ($isBirthdate ? 'd F Y' : 'd F Y, H:i', $string);
  295. $date = str_replace ("January", "??????", $date);
  296. $date = str_replace ("February", "???????", $date);
  297. $date = str_replace ("March", "?????", $date);
  298. $date = str_replace ("April", "??????", $date);
  299. $date = str_replace ("May", "???", $date);
  300. $date = str_replace ("June", "????", $date);
  301. $date = str_replace ("July", "????", $date);
  302. $date = str_replace ("August", "???????", $date);
  303. $date = str_replace ("September", "????????", $date);
  304. $date = str_replace ("October", "???????", $date);
  305. $date = str_replace ("November", "??????", $date);
  306. $date = str_replace ("December", "???????", $date);
  307. return $date;
  308. }
  309. if ((time() - $string) < 120) {
  310. $date = '?????? ???';
  311. }
  312. elseif ((time() - $string) < 3600) {
  313. $date = getNumWithCase ((int) (date ('i', (time() - $string))), array ('??????', '??????', '?????'),false).' ?????';
  314. }
  315. elseif ((time() - $string) < $today) {
  316. $date = '??????? ? '.date('H:i', $string);
  317. }
  318. else {
  319. $date = date ($isBirthdate ? 'j F' : 'j F, H:i', $string);
  320. $date = str_replace ("January", "??????", $date);
  321. $date = str_replace ("February", "???????", $date);
  322. $date = str_replace ("March", "?????", $date);
  323. $date = str_replace ("April", "??????", $date);
  324. $date = str_replace ("May", "???", $date);
  325. $date = str_replace ("June", "????", $date);
  326. $date = str_replace ("July", "????", $date);
  327. $date = str_replace ("August", "???????", $date);
  328. $date = str_replace ("September", "????????", $date);
  329. $date = str_replace ("October", "???????", $date);
  330. $date = str_replace ("November", "??????", $date);
  331. $date = str_replace ("December", "???????", $date);
  332. }
  333. return $date;
  334. }
  335. /**
  336. * ???????, ???????????? ?????? ????? ????? ? ??????????? ?? ???????? ????? $cnt (??????????? ??????)
  337. * @param integer $cnt ?????
  338. * @param $case ?????? ?? 3-? ????????? - ???? ?????????? ?????
  339. * @param boolean $word ??? ????????? ????? ????????? ? true ????? ?????? ???? ????? ?????????? ?? ????? "???"
  340. * @return string
  341. */
  342. function getNumWithCase ($cnt, $case, $word = true) {
  343. $cnt1 = $cnt;
  344. $cnt = str_replace ('-', '', $cnt);
  345. (mb_strlen($cnt) == 1) ? $minus = 1 : $minus = 2;
  346. $temp1 = mb_substr ($cnt, mb_strlen($cnt)-$minus, mb_strlen($cnt));
  347. $temp2 = mb_substr ($cnt, mb_strlen($cnt)-2, mb_strlen($cnt));
  348. if ($cnt == 0) $count = (($word) ? '??? ' : '0 ').$case[2];
  349. elseif ($temp2 == 11 || $temp2 == 12 || $temp2 == 13 || $temp2 == 14) $count = "$cnt1 ".$case[2];
  350. elseif ($cnt == 1) $count = "$cnt1 ".$case[0];
  351. elseif ($temp1 == 2 || $temp1 == 3 || $temp1 == 4) $count = "$cnt1 ".$case[1];
  352. else $count = "$cnt1 ".$case[2];
  353. return $count;
  354. }
  355. /**
  356. * ??????? ???????? ??????????, ?????? ?? ????????? ???? ????? $cnt
  357. */
  358. function getCaseByNum ($cnt, $case, $word = true) {
  359. $cnt = str_replace ('-', '', $cnt);
  360. (mb_strlen($cnt) == 1) ? $minus = 1 : $minus = 2;
  361. $temp1 = mb_substr($cnt, mb_strlen($cnt)-$minus, mb_strlen($cnt));
  362. $temp2 = mb_substr($cnt, mb_strlen($cnt)-2, mb_strlen($cnt));
  363. if ($cnt == 0) $count = (($word) ? '??? ' : '0 ').$case[2];
  364. elseif ($temp2 == 11 || $temp2 == 12 || $temp2 == 13 || $temp2 == 14) $count = $case[2];
  365. elseif ($cnt == 1) $count = $case[0];
  366. elseif ($temp1 == 2 || $temp1 == 3 || $temp1 == 4) $count = $case[1];
  367. else $count = $case[2];
  368. return $count;
  369. }
  370. function GET2String (array $get) {
  371. $a = array ();
  372. unset ($get['u'], $get['page'], $get['force_format']);
  373. foreach ($get as $key => $value) {
  374. if (is_array ($value)) {
  375. foreach ($value as $key2 => $value2) {
  376. $a[] .= $key . '[' . $key2 . ']=' . $value2;
  377. }
  378. } else {
  379. $a[] .= $key . ($value != '' ? '=' . $value : '');
  380. }
  381. }
  382. return implode ('&', $a);
  383. }
  384. function createHash ($numChars) {
  385. $s = '0123456789qwertyuiopasdfghjklzxcvbnm';
  386. $res = '';
  387. for ($i = 0; $i < $numChars; ++$i) {
  388. $res .= $s[mt_rand(0,mb_strlen($s)-1)];
  389. }
  390. return $res;
  391. }