Просмотр файла includes/functions.php

Размер файла: 27.97Kb
  1. <?php
  2. /***************************************************************************
  3. * mides.ru
  4. * -------------------
  5. ***************************************************************************/
  6. function get_db_stat($mode)
  7. {
  8. global $db;
  9.  
  10. switch( $mode )
  11. {
  12. case 'usercount':
  13. $sql = "SELECT COUNT(user_id) AS total
  14. FROM " . USERS_TABLE . "
  15. WHERE user_id <> " . ANONYMOUS;
  16. break;
  17.  
  18. case 'newestuser':
  19. $sql = "SELECT user_id, username
  20. FROM " . USERS_TABLE . "
  21. WHERE user_id <> " . ANONYMOUS . "
  22. ORDER BY user_id DESC
  23. LIMIT 1";
  24. break;
  25.  
  26. case 'postcount':
  27. case 'topiccount':
  28. $sql = "SELECT SUM(forum_topics) AS topic_total, SUM(forum_posts) AS post_total
  29. FROM " . FORUMS_TABLE;
  30. break;
  31. case 'attachcount':
  32. $sql = "SELECT count(*) AS total FROM " . ATTACHMENTS_DESC_TABLE;
  33. break;
  34. }
  35.  
  36. if ( !($result = $db->sql_query($sql)) )
  37. {
  38. return false;
  39. }
  40.  
  41. $row = $db->sql_fetchrow($result);
  42.  
  43. switch ( $mode )
  44. {
  45. case 'usercount':
  46. return $row['total'];
  47. break;
  48. case 'newestuser':
  49. return $row;
  50. break;
  51. case 'postcount':
  52. return $row['post_total'];
  53. break;
  54. case 'topiccount':
  55. return $row['topic_total'];
  56. break;
  57. case 'attachcount':
  58. return $row['total'];
  59. break;
  60. }
  61.  
  62. return false;
  63. }
  64.  
  65. function phpbb_clean_username($username)
  66. {
  67. $username = substr(htmlspecialchars(str_replace("\'", "'", trim($username))), 0, 25);
  68. $username = phpbb_rtrim($username, "\\");
  69. $username = str_replace("'", "\'", $username);
  70.  
  71. return $username;
  72. }
  73.  
  74. function phpbb_ltrim($str, $charlist = false)
  75. {
  76. if ($charlist === false)
  77. {
  78. return ltrim($str);
  79. }
  80. $php_version = explode('.', PHP_VERSION);
  81.  
  82. if ((int) $php_version[0] < 4 || ((int) $php_version[0] == 4 && (int) $php_version[1] < 1))
  83. {
  84. while ($str{0} == $charlist)
  85. {
  86. $str = substr($str, 1);
  87. }
  88. }
  89. else
  90. {
  91. $str = ltrim($str, $charlist);
  92. }
  93.  
  94. return $str;
  95. }
  96.  
  97. function phpbb_rtrim($str, $charlist = false)
  98. {
  99. if ($charlist === false)
  100. {
  101. return rtrim($str);
  102. }
  103. $php_version = explode('.', PHP_VERSION);
  104.  
  105. if ((int) $php_version[0] < 4 || ((int) $php_version[0] == 4 && (int) $php_version[1] < 1))
  106. {
  107. while ($str{strlen($str)-1} == $charlist)
  108. {
  109. $str = substr($str, 0, strlen($str)-1);
  110. }
  111. }
  112. else
  113. {
  114. $str = rtrim($str, $charlist);
  115. }
  116.  
  117. return $str;
  118. }
  119.  
  120. function dss_rand()
  121. {
  122. global $db, $board_config, $dss_seeded;
  123.  
  124. $val = $board_config['rand_seed'] . microtime();
  125. $val = md5($val);
  126. $board_config['rand_seed'] = md5($board_config['rand_seed'] . $val . 'a');
  127. if($dss_seeded !== true)
  128. {
  129. $sql = "UPDATE " . CONFIG_TABLE . " SET
  130. config_value = '" . $board_config['rand_seed'] . "'
  131. WHERE config_name = 'rand_seed'";
  132. if( !$db->sql_query($sql) )
  133. {
  134. message_die(GENERAL_ERROR, "Unable to reseed PRNG", "", __LINE__, __FILE__, $sql);
  135. }
  136.  
  137. $dss_seeded = true;
  138. }
  139.  
  140. return substr($val, 4, 16);
  141. }
  142.  
  143. function get_userdata($user, $force_str = false)
  144. {
  145. global $db;
  146.  
  147. if (!is_numeric($user) || $force_str)
  148. {
  149. $user = phpbb_clean_username($user);
  150. }
  151. else
  152. {
  153. $user = intval($user);
  154. }
  155.  
  156. $sql = "SELECT *
  157. FROM " . USERS_TABLE . "
  158. WHERE ";
  159. $sql .= ( ( is_integer($user) ) ? "user_id = $user" : "username = '" . str_replace("\'", "''", $user) . "'" ) . " AND user_id <> " . ANONYMOUS;
  160. if ( !($result = $db->sql_query($sql)) )
  161. {
  162. message_die(GENERAL_ERROR, 'Tried obtaining data for a non-existent user', '', __LINE__, __FILE__, $sql);
  163. }
  164.  
  165. return ( $row = $db->sql_fetchrow($result) ) ? $row : false;
  166. }
  167.  
  168. function init_userprefs($userdata)
  169. {
  170. global $board_config;
  171. global $template, $lang, $phpEx, $phpbb_root_path, $db;
  172.  
  173. if ( $userdata['user_id'] != ANONYMOUS )
  174. {
  175. if ( !empty($userdata['user_lang']))
  176. {
  177. $default_lang = phpbb_ltrim(basename(phpbb_rtrim($userdata['user_lang'])), "'");
  178. }
  179.  
  180. if ( !empty($userdata['user_dateformat']) )
  181. {
  182. $board_config['default_dateformat'] = $userdata['user_dateformat'];
  183. }
  184.  
  185. if ( isset($userdata['user_timezone']) )
  186. {
  187. $board_config['board_timezone'] = $userdata['user_timezone'];
  188. }
  189. if ( isset($userdata['user_topics_per_page']) )
  190. {
  191. $board_config['topics_per_page'] = $userdata['user_topics_per_page'];
  192. }
  193. if ( isset($userdata['user_posts_per_page']) )
  194. {
  195. $board_config['posts_per_page'] = $userdata['user_posts_per_page'];
  196. }
  197. }
  198. else
  199. {
  200. $default_lang = phpbb_ltrim(basename(phpbb_rtrim($board_config['default_lang'])), "'");
  201. }
  202.  
  203. if ( !file_exists(@phpbb_realpath($phpbb_root_path . 'language/lang_' . $default_lang . '/lang_main.'.$phpEx)) )
  204. {
  205. if ( $userdata['user_id'] != ANONYMOUS )
  206. {
  207. $default_lang = phpbb_ltrim(basename(phpbb_rtrim($board_config['default_lang'])), "'");
  208. }
  209. else
  210. {
  211.  
  212. $default_lang = 'russian';
  213. }
  214.  
  215. if ( !file_exists(@phpbb_realpath($phpbb_root_path . 'language/lang_' . $default_lang . '/lang_main.'.$phpEx)) )
  216. {
  217. message_die(CRITICAL_ERROR, 'Could not locate valid language pack');
  218. }
  219. }
  220.  
  221. if ( $userdata['user_id'] != ANONYMOUS && $userdata['user_lang'] !== $default_lang )
  222. {
  223. $sql = 'UPDATE ' . USERS_TABLE . "
  224. SET user_lang = '" . $default_lang . "'
  225. WHERE user_lang = '" . $userdata['user_lang'] . "'";
  226.  
  227. if ( !($result = $db->sql_query($sql)) )
  228. {
  229. message_die(CRITICAL_ERROR, 'Could not update user language info');
  230. }
  231.  
  232. $userdata['user_lang'] = $default_lang;
  233. }
  234. elseif ( $userdata['user_id'] == ANONYMOUS && $board_config['default_lang'] !== $default_lang )
  235. {
  236. $sql = 'UPDATE ' . CONFIG_TABLE . "
  237. SET config_value = '" . $default_lang . "'
  238. WHERE config_name = 'default_lang'";
  239.  
  240. if ( !($result = $db->sql_query($sql)) )
  241. {
  242. message_die(CRITICAL_ERROR, 'Could not update user language info');
  243. }
  244. }
  245.  
  246. $board_config['default_lang'] = $default_lang;
  247.  
  248. include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_main.' . $phpEx);
  249. if(file_exists($phpbb_root_path . "language/lang_".$board_config['default_lang'] . "/lang_guestbook.php"))
  250. {
  251. @include($phpbb_root_path . "language/lang_".$board_config['default_lang'] . "/lang_guestbook.php");
  252. }
  253. elseif(file_exists($phpbb_root_path . "language/lang_english/lang_guestbook.php"))
  254. {
  255. @include($phpbb_root_path . "language/lang_english/lang_guestbook.php");
  256. }
  257.  
  258. if ( defined('IN_ADMIN') )
  259. {
  260. if( !file_exists(@phpbb_realpath($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_admin.'.$phpEx)) )
  261. {
  262. $board_config['default_lang'] = 'russian';
  263. }
  264.  
  265. include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_admin.' . $phpEx);
  266. }
  267. include_attach_lang();
  268.  
  269. $template = new Template($phpbb_root_path . 'templates');
  270.  
  271. return;
  272. }
  273.  
  274. function style_text()
  275. {
  276. global $db, $board_config, $userdata;
  277.  
  278. if( $userdata['session_logged_in'] )
  279. {
  280. $sql = 'SELECT style_text
  281. FROM ' . USERS_TABLE . '
  282. WHERE user_id = ' . (int) $userdata['user_id'];
  283. if ( !($result = $db->sql_query($sql)) )
  284. {
  285. message_die(CRITICAL_ERROR, 'Could not query database for style text');
  286. }
  287. $row = $db->sql_fetchrow($result);
  288. return $row['style_text'];
  289. } else {
  290. $sql = 'SELECT *
  291. FROM ' . STYLES_CSS . '
  292. WHERE style_id = ' . (int) $board_config['default_style'];
  293. if( !($result = $db->sql_query($sql)) )
  294. {
  295. message_die(CRITICAL_ERROR, "Could not query style information", "", __LINE__, __FILE__, $sql);
  296. }
  297. $row = $db->sql_fetchrow($result);
  298. return $row['style_text'];
  299. }
  300. }
  301.  
  302. function encode_ip($dotquad_ip)
  303. {
  304. $ip_sep = explode('.', $dotquad_ip);
  305. return sprintf('%02x%02x%02x%02x', $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]);
  306. }
  307.  
  308. function decode_ip($int_ip)
  309. {
  310. $hexipbang = explode('.', chunk_split($int_ip, 2, '.'));
  311. return hexdec($hexipbang[0]). '.' . hexdec($hexipbang[1]) . '.' . hexdec($hexipbang[2]) . '.' . hexdec($hexipbang[3]);
  312. }
  313.  
  314. function create_date($format, $gmepoch, $tz)
  315. {
  316. global $board_config, $lang;
  317. static $translate;
  318.  
  319. if ( empty($translate) && $board_config['default_lang'] = 'russian' )
  320. {
  321. @reset($lang['datetime']);
  322. while ( list($match, $replace) = @each($lang['datetime']) )
  323. {
  324. $translate[$match] = $replace;
  325. }
  326. }
  327.  
  328. // friendly date mod\\*******************
  329.  
  330. $thetime = ( !empty($translate) ) ? strtr(@gmdate($format, $gmepoch + (3600 * $tz)), $translate) : @gmdate($format, $gmepoch + (3600 * $tz));
  331. $L_Today = 'Сегодня';
  332. $L_Yesterday = 'Вчера';
  333. $time_format = " в G:i";// - hour:minute am/pm
  334. $date = getdate();
  335. $today = $date['mday'];
  336. $month = $date['mon'];
  337. $year = $date['year'];
  338.  
  339. $forum_date_today = @gmdate ("d", $gmepoch + (3600 * $tz));
  340. $forum_date_month = @gmdate ("m", $gmepoch + (3600 * $tz));
  341. $forum_date_year = @gmdate ("Y", $gmepoch + (3600 * $tz));
  342. if ($forum_date_today == $today && $forum_date_month == $month && $forum_date_year == $year)
  343. $thetime = $L_Today . @gmdate ($time_format, $gmepoch + (3600 * $tz));//today
  344. else
  345. if ($today != 1 && $forum_date_today == ($today-1) && $forum_date_month == $month && $forum_date_year == $year)
  346. $thetime = $L_Yesterday . @gmdate ($time_format, $gmepoch + (3600 * $tz));//yesterday
  347. else
  348. //if today is 1 and the month is not 1, then we have to check how many days in the previews month
  349. //and then set $yesterday to the last day in the previews month
  350. if ($today == 1 && $month != 1)
  351. {
  352. $yesterday = date ("t", mktime(0,0,0,($month-1),1,$year));//returns how many days in the previews month
  353. if ($forum_date_today == $yesterday && $forum_date_month == ($month-1) && $forum_date_year == $year)
  354. $thetime = $L_Yesterday . @gmdate ($time_format, $gmepoch + (3600 * $tz));//yesterday
  355. }
  356. else
  357. //if we are in the first day in the year
  358. if ($today == 1 && $month == 1)
  359. {
  360. $yesterday = date ("t", mktime(0,0,0,12,1,($year -1)));
  361. if ($forum_date_today == $yesterday && $forum_date_month == 12 && $forum_date_year == ($year-1))
  362. $thetime = $L_Yesterday . @gmdate ($time_format, $gmepoch + (3600 * $tz));//yesterday
  363. }
  364.  
  365. return ($thetime);
  366. //end friendly date \\*******************************
  367. }
  368.  
  369. function generate_pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE)
  370. {
  371. global $lang;
  372.  
  373. $total_pages = ceil($num_items/$per_page);
  374.  
  375. if ( $total_pages == 1 )
  376. {
  377. return '';
  378. }
  379.  
  380. $on_page = floor($start_item / $per_page) + 1;
  381.  
  382. $page_string = '';
  383. if ( $on_page == 1 )
  384. {
  385. $page_string = 'страницы: <br/>';
  386. }
  387. if ( $on_page == $total_pages )
  388. {
  389. $page_string = 'страницы: <br/>';
  390. }
  391. if ( $total_pages > 5 )
  392. {
  393. $init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;
  394.  
  395. for($i = 1; $i < $init_page_max + 1; $i++)
  396. {
  397. $page_string .= ( $i == $on_page ) ? '<u>' . $i . '</u>' : '<a href="' . append_sid($base_url . "&amp;start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
  398. if ( $i < $init_page_max )
  399. {
  400. $page_string .= " ";
  401. }
  402. }
  403.  
  404. if ( $total_pages > 3 )
  405. {
  406. if ( $on_page > 1 && $on_page < $total_pages )
  407. {
  408. $page_string .= ( $on_page > 5 ) ? ' ' : ' ';
  409.  
  410. $init_page_min = ( $on_page > 4 ) ? $on_page : 5;
  411. $init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;
  412.  
  413. for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++)
  414. {
  415. $page_string .= ($i == $on_page) ? '<u>' . $i . '</u>' : '<a href="' . append_sid($base_url . "&amp;start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
  416. if ( $i < $init_page_max + 1 )
  417. {
  418. $page_string .= ' ';
  419. }
  420. }
  421.  
  422. $page_string .= ( $on_page < $total_pages - 4 ) ? '' : ' ';
  423. }
  424. else
  425. {
  426. $page_string .= ' ';
  427. }
  428.  
  429. for($i = $total_pages - 2; $i < $total_pages + 1; $i++)
  430. {
  431. $page_string .= ( $i == $on_page ) ? '<u>' . $i . '</u></b>' : '<a href="' . append_sid($base_url . "&amp;start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
  432. if( $i < $total_pages )
  433. {
  434. $page_string .= " ";
  435. }
  436. }
  437. }
  438. }
  439. else
  440. {
  441. for($i = 1; $i < $total_pages + 1; $i++)
  442. {
  443. $page_string .= ( $i == $on_page ) ? '<u>' . $i . '</u>' : '<a href="' . append_sid($base_url . "&amp;start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
  444. if ( $i < $total_pages )
  445. {
  446. $page_string .= ' ';
  447. }
  448. }
  449. }
  450.  
  451. if ( $add_prevnext_text )
  452. {
  453. if ( $on_page > 1 && $on_page < $total_pages )
  454. {
  455. $page_string = 'страницы: ' . $page_string .'<br/>';
  456. }
  457.  
  458. if ( $on_page < $total_pages )
  459. {
  460. $page_string .= '';
  461. }
  462. }
  463.  
  464. $page_string = $page_string . '<br/>';
  465.  
  466. return $page_string;
  467. }
  468.  
  469. function phpbb_preg_quote($str, $delimiter)
  470. {
  471. $text = preg_quote($str);
  472. $text = str_replace($delimiter, '\\' . $delimiter, $text);
  473. return $text;
  474. }
  475.  
  476. function obtain_word_list(&$orig_word, &$replacement_word)
  477. {
  478. global $db;
  479.  
  480. $sql = "SELECT word, replacement
  481. FROM " . WORDS_TABLE;
  482. if( !($result = $db->sql_query($sql)) )
  483. {
  484. message_die(GENERAL_ERROR, 'Could not get censored words from database', '', __LINE__, __FILE__, $sql);
  485. }
  486.  
  487. if ( $row = $db->sql_fetchrow($result) )
  488. {
  489. do
  490. {
  491. $orig_word[] = $row['word'];
  492. $replacement_word[] = $row['replacement'];
  493. }
  494. while ( $row = $db->sql_fetchrow($result) );
  495. }
  496.  
  497. return true;
  498. }
  499.  
  500. function message_die($msg_code, $msg_text = '', $msg_title = '', $err_line = '', $err_file = '', $sql = '')
  501. {
  502. global $db, $template, $board_config, $lang, $phpEx, $phpbb_root_path, $gen_simple_header, $opera_mini, $tmp_name;
  503. global $userdata, $user_ip, $session_length;
  504. global $starttime;
  505.  
  506. $sql_store = $sql;
  507.  
  508. if ( DEBUG && ( $msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR ) )
  509. {
  510. $sql_error = $db->sql_error();
  511.  
  512. $debug_text = '';
  513.  
  514. if ( $sql_error['message'] != '' )
  515. {
  516. $debug_text .= '<br /><br />SQL Error : ' . $sql_error['code'] . ' ' . $sql_error['message'];
  517. }
  518.  
  519. if ( $sql_store != '' )
  520. {
  521. $debug_text .= "<br /><br />$sql_store";
  522. }
  523.  
  524. if ( $err_line != '' && $err_file != '' )
  525. {
  526. $debug_text .= '<br /><br />Line : ' . $err_line . '<br />File : ' . basename($err_file);
  527. }
  528. }
  529.  
  530. if( empty($userdata) && ( $msg_code == GENERAL_MESSAGE || $msg_code == GENERAL_ERROR ) )
  531. {
  532. $userdata = session_pagestart($user_ip, PAGE_INDEX);
  533. init_userprefs($userdata);
  534. }
  535.  
  536. if ( !defined('HEADER_INC') && $msg_code != CRITICAL_ERROR )
  537. {
  538. if ( empty($lang) )
  539. {
  540. if ( !empty($board_config['default_lang']) )
  541. {
  542. include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_main.'.$phpEx);
  543. }
  544. else
  545. {
  546. include($phpbb_root_path . 'language/lang_russian/lang_main.'.$phpEx);
  547. }
  548. }
  549.  
  550. $page_title = 'Информация';
  551.  
  552. if ( !defined('IN_ADMIN') )
  553. {
  554. include($phpbb_root_path . 'includes/page_header.'.$phpEx);
  555. }
  556. else
  557. {
  558. include($phpbb_root_path . 'admin/page_header_admin.'.$phpEx);
  559. }
  560. }
  561.  
  562. switch($msg_code)
  563. {
  564. case GENERAL_MESSAGE:
  565. if ( $msg_title == '' )
  566. {
  567. $msg_title = 'Информация';
  568. }
  569. break;
  570.  
  571. case CRITICAL_MESSAGE:
  572. if ( $msg_title == '' )
  573. {
  574. $msg_title = 'Информация';
  575. }
  576. break;
  577.  
  578. case GENERAL_ERROR:
  579. if ( $msg_text == '' )
  580. {
  581. $msg_text = 'Произошла ошибка';
  582. }
  583.  
  584. if ( $msg_title == '' )
  585. {
  586. $msg_title = 'Общая ошибка';
  587. }
  588. break;
  589.  
  590. case CRITICAL_ERROR:
  591.  
  592. if ( $msg_text == '' )
  593. {
  594. $msg_text = 'Произошла критическая ошибка';
  595. }
  596.  
  597. if ( $msg_title == '' )
  598. {
  599. $msg_title = 'phpBB-CMS : <b>Критическая ошибка</b>';
  600. }
  601. break;
  602. }
  603.  
  604. if ( DEBUG && ( $msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR ) )
  605. {
  606. if ( $debug_text != '' )
  607. {
  608. $msg_text = $msg_text . '<br /><br /><b><u>DEBUG MODE</u></b>' . $debug_text;
  609. }
  610. }
  611.  
  612. if ( $msg_code != CRITICAL_ERROR )
  613. {
  614. if ( !empty($lang[$msg_text]) )
  615. {
  616. $msg_text = $lang[$msg_text];
  617. }
  618.  
  619. if ( !defined('IN_ADMIN') )
  620. {
  621. $template->set_filenames(array(
  622. 'message_body' => 'message_body.tpl')
  623. );
  624. }
  625. else
  626. {
  627. $template->set_filenames(array(
  628. 'message_body' => 'admin/admin_message_body.tpl')
  629. );
  630. }
  631.  
  632. $template->assign_vars(array(
  633. 'MESSAGE_TITLE' => $msg_title,
  634. 'MESSAGE_TEXT' => $msg_text)
  635. );
  636. $template->pparse('message_body');
  637.  
  638. if ( !defined('IN_ADMIN') )
  639. {
  640. include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
  641. }
  642. else
  643. {
  644. include($phpbb_root_path . 'admin/page_footer_admin.'.$phpEx);
  645. }
  646. }
  647. else
  648. {
  649. echo "<html>\n<body>\n" . $msg_title . "\n<br /><br />\n" . $msg_text . "</body>\n</html>";
  650. }
  651. exit;
  652. }
  653.  
  654. function phpbb_realpath($path)
  655. {
  656. global $phpbb_root_path, $phpEx;
  657.  
  658. return (!@function_exists('realpath') || !@realpath($phpbb_root_path . 'includes/functions.'.$phpEx)) ? $path : @realpath($path);
  659. }
  660.  
  661. function redirect($url)
  662. {
  663. global $db, $board_config;
  664.  
  665. if (!empty($db))
  666. {
  667. $db->sql_close();
  668. }
  669.  
  670. if (strstr(urldecode($url), "\n") || strstr(urldecode($url), "\r") || strstr(urldecode($url), ';url'))
  671. {
  672. message_die(GENERAL_ERROR, 'Tried to redirect to potentially insecure url.');
  673. }
  674.  
  675. $server_protocol = ($board_config['cookie_secure']) ? 'https://' : 'http://';
  676. $server_name = preg_replace('#^\/?(.*?)\/?$#', '\1', trim($board_config['server_name']));
  677. $server_port = ($board_config['server_port'] <> 80) ? ':' . trim($board_config['server_port']) : '';
  678. $script_name = preg_replace('#^\/?(.*?)\/?$#', '\1', trim($board_config['script_path']));
  679. $script_name = ($script_name == '') ? $script_name : '/' . $script_name;
  680. $url = preg_replace('#^\/?(.*?)\/?$#', '/\1', trim($url));
  681.  
  682. if (@preg_match('/Microsoft|WebSTAR|Xitami/', getenv('SERVER_SOFTWARE')))
  683. {
  684. header('Refresh: 0; URL=' . $server_protocol . $server_name . $server_port . $script_name . $url);
  685. echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><meta http-equiv="refresh" content="0; url=' . $server_protocol . $server_name . $server_port . $script_name . $url . '"><title>Redirect</title></head><body><div align="center">If your browser does not support meta redirection please click <a href="' . $server_protocol . $server_name . $server_port . $script_name . $url . '">HERE</a> to be redirected</div></body></html>';
  686. exit;
  687. }
  688.  
  689. header('Location: ' . $server_protocol . $server_name . $server_port . $script_name . $url);
  690. exit;
  691. }
  692.  
  693. function db_query($sql)
  694. {
  695. global $db;
  696.  
  697. $sql = preg_replace_callback('#{(\w+)}#', 'const_subst', $sql);
  698.  
  699. if (func_num_args() > 1)
  700. {
  701. $args = func_get_args();
  702. $args = array_map('addslashes', $args);
  703. $args[0] = $sql;
  704. $sql = call_user_func_array('sprintf', $args);
  705. }
  706.  
  707. if ($result = $db->sql_query($sql))
  708. {
  709. return $result;
  710. }
  711. else
  712. {
  713. message_die(GENERAL_ERROR, 'SQL query failed', '', __LINE__, __FILE__, $sql);
  714. }
  715. }
  716.  
  717. function const_subst($match)
  718. {
  719. return constant($match[1]);
  720. }
  721.  
  722. function db_transaction($command)
  723. {
  724. global $db;
  725.  
  726. $db->sql_query('SELECT 0', $command);
  727. }
  728.  
  729.  
  730. function mkrealdate($day,$month,$birth_year)
  731. {
  732. if ($month<1 || $month>12) return "error";
  733. switch ($month)
  734. {
  735. case 1: if ($day>31) return "error";break;
  736. case 2: if ($day>29) return "error";
  737. $epoch=$epoch+31;break;
  738. case 3: if ($day>31) return "error";
  739. $epoch=$epoch+59;break;
  740. case 4: if ($day>30) return "error" ;
  741. $epoch=$epoch+90;break;
  742. case 5: if ($day>31) return "error";
  743. $epoch=$epoch+120;break;
  744. case 6: if ($day>30) return "error";
  745. $epoch=$epoch+151;break;
  746. case 7: if ($day>31) return "error";
  747. $epoch=$epoch+181;break;
  748. case 8: if ($day>31) return "error";
  749. $epoch=$epoch+212;break;
  750. case 9: if ($day>30) return "error";
  751. $epoch=$epoch+243;break;
  752. case 10: if ($day>31) return "error";
  753. $epoch=$epoch+273;break;
  754. case 11: if ($day>30) return "error";
  755. $epoch=$epoch+304;break;
  756. case 12: if ($day>31) return "error";
  757. $epoch=$epoch+334;break;
  758. }
  759. $epoch=$epoch+$day;
  760. $epoch_Y=sqrt(($birth_year-1970)*($birth_year-1970));
  761. $leapyear=round((($epoch_Y+2) / 4)-.5);
  762. if (($epoch_Y+2)%4==0)
  763. {
  764. $leapyear--;
  765. if ($birth_year >1970 && $month>=3) $epoch=$epoch+1;
  766. if ($birth_year <1970 && $month<3) $epoch=$epoch-1;
  767. } else if ($month==2 && $day>28) return "error";
  768. if ($birth_year>1970)
  769. $epoch=$epoch+$epoch_Y*365-1+$leapyear;
  770. else
  771. $epoch=$epoch-$epoch_Y*365-1-$leapyear;
  772. return $epoch;
  773. }
  774. function add_friend($to, $friend)
  775. {
  776. global $db, $lang;
  777.  
  778. $userdata = get_userdata($to);
  779. if ( $userdata['username'] == $friend )
  780. {
  781. message_die(GENERAL_ERROR, $lang['you_cannot_add_self']);
  782. }
  783.  
  784. if(!intval($friend))
  785. {
  786. $sql = 'SELECT user_id FROM '.USERS_TABLE.' WHERE username = \''.str_replace("\'", "''", $friend).'\'';
  787.  
  788. if (!$result = $db->sql_query($sql))
  789. {
  790. message_die(GENERAL_ERROR, 'Could not get friend info', '', __LINE__, __FILE__, $sql);
  791. }
  792.  
  793. $friend_row = $db->sql_fetchrow($result);
  794. $friend_id = $friend_row['user_id'];
  795. } else
  796. {
  797. $friend_id = $friend;
  798. }
  799.  
  800. $sql_friend_id = $friend_id;
  801.  
  802. if ( empty($friend_id) || $friend_id == ANONYMOUS )
  803. {
  804. message_die(GENERAL_ERROR, $lang['select_correct_user']);
  805. }
  806.  
  807. //
  808. // Is user already in friends list?
  809. //
  810.  
  811. $sql = 'SELECT friend_id FROM '.FRIENDS_TABLE.' WHERE friend_id = '.$sql_friend_id.' AND user_id = '.$userdata['user_id'].'';
  812. if (!$result = $db->sql_query($sql))
  813. {
  814. message_die(GENERAL_ERROR, 'Could not get friends info', '', __LINE__, __FILE__, $sql);
  815. }
  816.  
  817. $friend_row = $db->sql_fetchrow($result);
  818. $friend_id = $friend_row['friend_id'];
  819.  
  820. if ( !empty($friend_id) )
  821. {
  822. message_die(GENERAL_ERROR, $lang['This_user_already_added_to_friends']);
  823. }
  824.  
  825. //
  826. // Is user in foes?
  827. //
  828.  
  829. $sql = 'SELECT foe_id FROM '.FOES_TABLE.' WHERE foe_id = '.$sql_friend_id.' AND user_id = '.$userdata['user_id'].'';
  830. if (!$result = $db->sql_query($sql))
  831. {
  832. message_die(GENERAL_ERROR, 'Could not get foes info', '', __LINE__, __FILE__, $sql);
  833. }
  834.  
  835. $foes_row = $db->sql_fetchrow($result);
  836. $friend_id = $foes_row['foe_id'];
  837.  
  838. if ( !empty($friend_id) )
  839. {
  840. message_die(GENERAL_ERROR, $lang['This_user_already_added_to_foes']);
  841. }
  842.  
  843. //
  844. // All tests passes; buil a query to add friend to DB
  845. //
  846.  
  847. $sql = 'INSERT INTO '.FRIENDS_TABLE.' (user_id, friend_id) VALUES ('.$userdata['user_id'].', '.$sql_friend_id.')';
  848. if (!$result = $db->sql_query($sql))
  849. {
  850. message_die(GENERAL_ERROR, 'Could not add friend', '', __LINE__, __FILE__, $sql);
  851. }
  852. }
  853.  
  854. function add_foe($to, $foe)
  855. {
  856. global $db, $lang;
  857.  
  858. $userdata = get_userdata($to);
  859.  
  860. if ( $userdata['username'] == $foe )
  861. {
  862. message_die(GENERAL_ERROR, $lang['you_cannot_add_self']);
  863. }
  864.  
  865. if( !intval($foe) )
  866. {
  867. $sql = 'SELECT user_id FROM '.USERS_TABLE.' WHERE username = \''.str_replace("\'", "''", $foe).'\'';
  868. if (!$result = $db->sql_query($sql))
  869. {
  870. message_die(GENERAL_ERROR, 'Could not get foe info', '', __LINE__, __FILE__, $sql);
  871. }
  872.  
  873. $foe_row = $db->sql_fetchrow($result);
  874. $foe_id = $foe_row['user_id'];
  875. } else
  876. {
  877. $foe_id = $foe;
  878. }
  879.  
  880. $sql_foe_id = $foe_id;
  881.  
  882. if ( empty($foe_id) || $foe_id == ANONYMOUS )
  883. {
  884. message_die(GENERAL_ERROR, $lang['select_correct_user']);
  885. }
  886.  
  887. //
  888. // Is user already in foes list?
  889. //
  890.  
  891. $sql = 'SELECT foe_id FROM '.FOES_TABLE.' WHERE foe_id = '.$sql_foe_id.' AND user_id = '.$userdata['user_id'].'';
  892. if (!$result = $db->sql_query($sql))
  893. {
  894. message_die(GENERAL_ERROR, 'Could not get foes info', '', __LINE__, __FILE__, $sql);
  895. }
  896.  
  897. $foe_row = $db->sql_fetchrow($result);
  898. $foe_id = $foe_row['foe_id'];
  899.  
  900. if ( !empty($foe_id) )
  901. {
  902. message_die(GENERAL_ERROR, $lang['This_user_already_added_to_foes']);
  903. }
  904.  
  905. //
  906. // Is user in friends?
  907. //
  908.  
  909. $sql = 'SELECT friend_id FROM '.FRIENDS_TABLE.' WHERE friend_id = '.$sql_foe_id.' AND user_id = '.$userdata['user_id'].'';
  910. if (!$result = $db->sql_query($sql))
  911. {
  912. message_die(GENERAL_ERROR, 'Could not get friends info', '', __LINE__, __FILE__, $sql);
  913. }
  914.  
  915. $foes_row = $db->sql_fetchrow($result);
  916. $foe_id = $foes_row['friend_id'];
  917.  
  918. if ( !empty($foe_id) )
  919. {
  920. message_die(GENERAL_ERROR, $lang['This_user_already_added_to_friends']);
  921. }
  922.  
  923. //
  924. // All tests passes; buil a query to add friend to DB
  925. //
  926.  
  927. $sql = 'INSERT INTO '.FOES_TABLE.' (user_id, foe_id) VALUES ('.$userdata['user_id'].', '.$sql_foe_id.')';
  928. if (!$result = $db->sql_query($sql))
  929. {
  930. message_die(GENERAL_ERROR, 'Could not add foe', '', __LINE__, __FILE__, $sql);
  931. }
  932. message_die(GENERAL_MESSAGE, $lang['foe_suc_added']);
  933. }
  934.  
  935.  
  936. function get_status($id, $reverse = true, $nofriend=false, $nofoe=false)
  937. {
  938. global $db, $userdata;
  939.  
  940. if ( $reverse )
  941. {
  942. $uid = $userdata['user_id'];
  943. $id = $id;
  944. } else
  945. {
  946. $uid = $id;
  947. $id = $userdata['user_id'];
  948. }
  949.  
  950. if ( !$nofriend )
  951. {
  952. $sql = 'SELECT friend_id FROM '.FRIENDS_TABLE.'
  953. WHERE user_id = '.$id.'
  954. AND friend_id = '.$uid;
  955. if ( !($result = $db->sql_query($sql)) )
  956. {
  957. message_die(GENERAL_ERROR, 'Could not obtain friends information', '', __LINE__, __FILE__, $sql);
  958. }
  959. $friends = $db->sql_fetchrow($result);
  960. }
  961. if ( !$nofoe )
  962. {
  963. $sql = 'SELECT foe_id FROM '.FOES_TABLE.'
  964. WHERE user_id = '.$id.'
  965. AND foe_id = '.$uid;
  966.  
  967. if ( !($result = $db->sql_query($sql)) )
  968. {
  969. message_die(GENERAL_ERROR, 'Could not obtain foes information', '', __LINE__, __FILE__, $sql);
  970. }
  971. $foes = $db->sql_fetchrow($result);
  972. }
  973. if ( !empty($foes['foe_id']) )
  974. {
  975. return 2;
  976. }
  977. elseif ( !empty($friends['friend_id']) )
  978. {
  979. return 1;
  980. } else
  981. {
  982. return 0;
  983. }
  984. }
  985.  
  986.  
  987. function realdate($date_syntax="Ymd",$date=0)
  988. {
  989. global $lang;
  990. $i=2;
  991. if ($date>=0)
  992. {
  993. return create_date($date_syntax,$date*86400+1,0);
  994. } else
  995. {
  996. $year= -(date%1461);
  997. $days = $date + $year*1461;
  998. while ($days<0)
  999. {
  1000. $year--;
  1001. $days+=365;
  1002. if ($i++==3)
  1003. {
  1004. $i=0;
  1005. $days++;
  1006. }
  1007. }
  1008. }
  1009. $leap_year = ($i==0) ? TRUE : FALSE;
  1010. $months_array = ($i==0) ?
  1011. array (0,31,60,91,121,152,182,213,244,274,305,335,366) :
  1012. array (0,31,59,90,120,151,181,212,243,273,304,334,365);
  1013. for ($month=1;$month<12;$month++)
  1014. {
  1015. if ($days<$months_array[$month]) break;
  1016. }
  1017.  
  1018. $day=$days-$months_array[$month-1]+1;
  1019. return strtr ($date_syntax, array(
  1020. 'a' => '',
  1021. 'A' => '',
  1022. '\\d' => 'd',
  1023. 'd' => ($day>9) ? $day : '0'.$day,
  1024. '\\D' => 'D',
  1025. 'D' => $lang['day_short'][($date-3)%7],
  1026. '\\F' => 'F',
  1027. 'F' => $lang['month_long'][$month-1],
  1028. 'g' => '',
  1029. 'G' => '',
  1030. 'H' => '',
  1031. 'h' => '',
  1032. 'i' => '',
  1033. 'I' => '',
  1034. '\\j' => 'j',
  1035. 'j' => $day,
  1036. '\\l' => 'l',
  1037. 'l' => $lang['day_long'][($date-3)%7],
  1038. '\\L' => 'L',
  1039. 'L' => $leap_year,
  1040. '\\m' => 'm',
  1041. 'm' => ($month>9) ? $month : '0'.$month,
  1042. '\\M' => 'M',
  1043. 'M' => $lang['month_short'][$month-1],
  1044. '\\n' => 'n',
  1045. 'n' => $month,
  1046. 'O' => '',
  1047. 's' => '',
  1048. 'S' => '',
  1049. '\\t' => 't',
  1050. 't' => $months_array[$month]-$months_array[$month-1],
  1051. 'w' => '',
  1052. '\\y' => 'y',
  1053. 'y' => ($year>29) ? $year-30 : $year+70,
  1054. '\\Y' => 'Y',
  1055. 'Y' => $year+1970,
  1056. '\\z' => 'z',
  1057. 'z' => $days,
  1058. '\\W' => '',
  1059. 'W' => '') );
  1060. }
  1061.  
  1062. function check_medal_mod($medal_id)
  1063. {
  1064. global $db, $userdata;
  1065. $sql = "SELECT *
  1066. FROM " . MEDAL_MOD_TABLE . "
  1067. WHERE medal_id =" . $medal_id;
  1068. if ( !($result = $db->sql_query($sql)) )
  1069. {
  1070. message_die(GENERAL_ERROR, 'Could not obtain user and medal information', '', __LINE__, __FILE__, $sql);
  1071. }
  1072.  
  1073. $medal_info = array();
  1074. $found = FALSE;
  1075. while ( $medal_info = $db->sql_fetchrow($result) )
  1076. {
  1077.  
  1078. $medal_moderator = $medal_info['user_id'];
  1079.  
  1080. if ( $medal_moderator == $userdata['user_id'] )
  1081. {
  1082. $found = TRUE;
  1083. }
  1084. }
  1085. $db->sql_freeresult($result);
  1086. return $found;
  1087. }
  1088.  
  1089. ?>