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

Размер файла: 12.87Kb
  1. <?php
  2. /***************************************************************************
  3. * mides.ru
  4. * -------------------
  5. ***************************************************************************/
  6. if (!defined('IN_PHPBB'))
  7. {
  8. die('Hacking attempt');
  9. }
  10.  
  11. if (!empty($images))
  12. {
  13. $warned_img = $lang['Warning'];
  14. $banned_img = $lang['reputation_ban'];
  15. $thumb_up_img = $lang['reputation_approve'];
  16. $thumb_dn_img = $lang['reputation_disapprove'];
  17. }
  18. $current_time = time();
  19.  
  20. $reputation_auth_keys = array('auth_view_rep', 'auth_view_warns', 'auth_add_rep', 'auth_add_rep_nonpost', 'auth_edit_rep', 'auth_delete_rep', 'auth_warn', 'auth_warn_nonpost', 'auth_ban', 'auth_ban_nonpost', 'auth_edit_warn', 'auth_delete_warn', 'auth_no_limits');
  21.  
  22. function input_var($name, $default, $required_msg = null)
  23. {
  24. global $HTTP_POST_VARS, $HTTP_GET_VARS;
  25.  
  26. if (isset($HTTP_POST_VARS[$name]))
  27. {
  28. $var = $HTTP_POST_VARS[$name];
  29. }
  30. elseif (isset($HTTP_GET_VARS[$name]))
  31. {
  32. $var = $HTTP_GET_VARS[$name];
  33. }
  34. elseif ($required_msg)
  35. {
  36. message_die(GENERAL_ERROR, $required_msg);
  37. }
  38. else
  39. {
  40. return addslashes(is_array($default) ? $default[0] : $default);
  41. }
  42.  
  43. if (is_array($default))
  44. {
  45. settype($var, gettype($default[0]));
  46.  
  47. if (in_array($var, $default))
  48. {
  49. return $var;
  50. }
  51. return $default[0];
  52. }
  53. return settype($var, gettype($default)) ? $var : $default;
  54. }
  55.  
  56. function censor($message, $html = false)
  57. {
  58. static $orig_word = null, $replacement_word = null;
  59.  
  60. if (is_null($orig_word))
  61. {
  62. if (isset($GLOBALS['orig_word']) && isset($GLOBALS['replacement_word']))
  63. {
  64. $orig_word = $GLOBALS['orig_word'];
  65. $replacement_word = $GLOBALS['replacement_word'];
  66. }
  67. else
  68. {
  69. obtain_word_list($orig_word, $replacement_word);
  70. }
  71. }
  72. if ($orig_word)
  73. {
  74. return str_replace($orig_word, $replacement_word, $message);
  75. }
  76. return $message;
  77. }
  78.  
  79. function prepare_display($message, $bbcode_uid = '', $enable_html = false, $enable_smilies = true, $enable_links = true)
  80. {
  81. global $board_config;
  82.  
  83. if (!$board_config['allow_html'] && $enable_html)
  84. {
  85. $message = preg_replace('#(<)([\/]?.*?)(>)#is', "&lt;\\2&gt;", $message);
  86. }
  87.  
  88. if ($bbcode_uid)
  89. {
  90. if ($board_config['allow_bbcode'])
  91. {
  92. $message = bbencode_second_pass($message, $bbcode_uid);
  93. }
  94. else
  95. {
  96. $message = preg_replace('/\:[0-9a-z\:]+\]/si', ']', $message);
  97. }
  98. }
  99.  
  100. if ($enable_links)
  101. {
  102. $message = make_clickable($message);
  103. }
  104.  
  105. if ($board_config['allow_smilies'] && $enable_smilies)
  106. {
  107. $message = smilies_pass($message);
  108. }
  109.  
  110. $message = censor($message, true);
  111.  
  112. return nl2br($message);
  113. }
  114.  
  115. function html_select($name, $values, $titles, $default = null)
  116. {
  117. global $lang;
  118.  
  119. $select = '<select name="' . $name . '" onchange="this.form.submit();">';
  120.  
  121. foreach ($values as $i => $value)
  122. {
  123. $checked = ($value == $default) ? ' selected="selected"' : '';
  124. $select .= "<option value=\"$value\"$checked>" . htmlspecialchars($lang[$titles[$i]]) . '</option>';
  125. }
  126.  
  127. $select .= '</select>';
  128.  
  129. return $select;
  130. }
  131.  
  132. function reputation_display($userdata, $is_auth, $for_post)
  133. {
  134. global $lang, $thumb_up_img, $thumb_dn_img, $board_config, $phpEx;
  135.  
  136. if ($is_auth['no_rep'])
  137. {
  138. return '';
  139. }
  140. if ($userdata['user_reputation'] || $userdata['user_reputation_plus'])
  141. {
  142. if ($for_post)
  143. {
  144. if ($is_auth['auth_view_rep'])
  145. {
  146. $user_reputation = '<a href="' . append_sid("profile.$phpEx?mode=reputation&amp;" . POST_USERS_URL . '=' . $userdata['user_id']) . '" title="' . $lang['repвutation_search_reputation'] . '">' . $lang['Reputation'] . '</a>: ';
  147. }
  148. else
  149. {
  150. $user_reputation = $lang['Reputation'] . ': ';
  151. }
  152. }
  153. else
  154. {
  155. $user_reputation = '';
  156. }
  157.  
  158. switch ($board_config['reputation_display'])
  159. {
  160. case REPUTATION_SUM:
  161. $user_reputation .= $userdata['user_reputation'] . '' . $lang['reputation_golosov'];
  162. break;
  163.  
  164. case REPUTATION_PLUSMINUS:
  165. $user_reputation .= $userdata['user_reputation'] . ' ';
  166. if ($userdata['user_reputation_plus'])
  167. {
  168. if ($userdata['user_reputation_plus'] - $userdata['user_reputation'] == 0)
  169. {
  170. $user_reputation .= '(+' . $userdata['user_reputation_plus'] . '/-0)';
  171. } else {
  172. $user_reputation .= '(+' . $userdata['user_reputation_plus'];
  173. }
  174. }
  175. if ($reputation_minus = $userdata['user_reputation_plus'] - $userdata['user_reputation'])
  176. {
  177. $user_reputation .= ($userdata['user_reputation_plus'] ? '/' : '(+0/') . '-' . $reputation_minus . ')';
  178. }
  179. break;
  180.  
  181. default:
  182. message_die(GENERAL_ERROR, 'Reputation config is damaged');
  183. }
  184. }
  185. else
  186. {
  187. $user_reputation = $for_post ? ($lang['Reputation'] . ': 0') : '0 голосов';
  188. }
  189.  
  190. if ($is_auth['auth_add_rep'])
  191. {
  192. $url_param = $for_post ? (POST_POST_URL . '=' . $userdata['post_id']) : (POST_USERS_URL . '=' . $userdata['user_id']);
  193.  
  194. $user_reputation .= '<br/>[ <a href="' . append_sid("reputation.$phpEx?mode=inc&amp;$url_param") . '"><img src="../images/img/plus.gif" alt="image" /><span style="color:#0099cc"> Плюс</span></a> / ';
  195.  
  196. if (!$board_config['reputation_positive_only'])
  197. {
  198. $user_reputation .= ' <a href="' . append_sid("reputation.$phpEx?mode=dec&amp;$url_param") . '"><span style="color:#ff0000">Минус</span> <img src="../images/img/minus.gif" alt="image" /></a> ]</a>';
  199. }
  200. }
  201. return $user_reputation;
  202. }
  203.  
  204. function cache_set($id, $data = null)
  205. {
  206. global $phpbb_root_path;
  207.  
  208. $cache_file = $phpbb_root_path . 'cache/' . $id;
  209.  
  210. if ($data)
  211. {
  212. if ($f = fopen($cache_file, 'wb'))
  213. {
  214. fwrite($f, serialize($data));
  215. fclose($f);
  216. return true;
  217. }
  218. return false;
  219. }
  220. elseif (file_exists($cache_file))
  221. {
  222. @unlink($cache_file);
  223. }
  224. }
  225.  
  226. function cache_get($id)
  227. {
  228. global $phpbb_root_path;
  229.  
  230. $cache_file = $phpbb_root_path . 'cache/' . $id;
  231.  
  232. if (is_readable($cache_file) && ($f = fopen($cache_file, 'rb')))
  233. {
  234. if ($data = fread($f, filesize($cache_file)))
  235. {
  236. $data = unserialize($data);
  237.  
  238. }
  239. fclose($f);
  240. return $data;
  241. }
  242.  
  243. return false;
  244. }
  245.  
  246. function reputation_auth($auth_or_forum_id, $userdata, $subject = null, $quick = false)
  247. {
  248. global $board_config, $reputation_auth_keys;
  249.  
  250. if (is_array($auth_or_forum_id))
  251. {
  252. $auth = $auth_or_forum_id;
  253. }
  254. elseif ($auth_or_forum_id == NO_ID)
  255. {
  256. $auth = array('auth_read' => true, 'auth_view' => true, 'auth_mod' => ($userdata['user_level'] == MOD || $userdata['user_level'] == ADMIN));
  257. }
  258. else
  259. {
  260. $auth = auth(AUTH_ALL, $auth_or_forum_id, $userdata);
  261. }
  262.  
  263. if ($auth_or_forum_id == AUTH_LIST_ALL)
  264. {
  265. $auth[NO_ID] = NO_ID;
  266.  
  267. foreach ($auth as $forum_id => $forum_auth)
  268. {
  269. $auth[$forum_id] = reputation_auth($forum_auth, $userdata, $subject, $quick);
  270. }
  271.  
  272. return $auth;
  273. }
  274.  
  275. if (!isset($auth['auth_add_rep']))
  276. {
  277. $admin = ($userdata['user_id'] != ANONYMOUS) && ($userdata['user_level'] == ADMIN);
  278. $moder = ($userdata['user_id'] != ANONYMOUS) && ($userdata['user_level'] == MOD);
  279. $guest = $auth['auth_read'] && $auth['auth_view'];
  280. $reg = ($userdata['user_id'] != ANONYMOUS) && $guest;
  281.  
  282. foreach ($reputation_auth_keys as $i => $key)
  283. {
  284. switch ($board_config['reputation_perms'][$i])
  285. {
  286. case AUTH_ADMIN:
  287. $auth[$key] = ($userdata['user_level'] == ADMIN);
  288. break;
  289. case AUTH_MOD:
  290. $auth[$key] = ($userdata['user_level'] == MOD || $userdata['user_level'] == ADMIN);
  291. break;
  292. case AUTH_REG:
  293. $auth[$key] = $reg;
  294. break;
  295. case AUTH_ALL:
  296. $auth[$key] = $guest;
  297. break;
  298. default:
  299. message_die(GENERAL_ERROR, 'Reputation config is damaged');
  300. }
  301. }
  302. if ($auth_or_forum_id == NO_ID)
  303. {
  304. $auth['auth_add_rep'] = $auth['auth_add_rep_nonpost'];
  305. $auth['auth_warn'] = $auth['auth_warn_nonpost'];
  306. $auth['auth_ban'] = $auth['auth_ban_nonpost'];
  307. }
  308. }
  309.  
  310. if (!empty($subject))
  311. {
  312. reputation_auth_personal($auth, $userdata, $subject, $quick);
  313. }
  314.  
  315. return $auth;
  316. }
  317.  
  318. function reputation_check_limits($actor, $subject, $quick = false)
  319. {
  320. global $db, $board_config, $lang, $current_time;
  321.  
  322. if ($actor['user_posts'] < $board_config['reputation_posts_req'])
  323. {
  324. return $lang['reputation_limits_apply'];
  325. }
  326. if (($current_time - $actor['user_regdate']) < $board_config['reputation_days_req'] * 86400)
  327. {
  328. return $lang['reputation_limits_apply'];
  329. }
  330. if ($actor['user_warnings'] > $board_config['reputation_warnings_req'])
  331. {
  332. return $lang['reputation_limits_apply'];
  333. }
  334. if ($actor['user_reputation'] < $board_config['reputation_points_req'])
  335. {
  336. return $lang['reputation_limits_apply'];
  337. }
  338.  
  339. if (!$quick && ($board_config['reputation_time_limit'] || $board_config['reputation_rotation_limit']))
  340. {
  341. $result = db_query('SELECT id, date FROM {REPUTATION_TABLE}
  342. WHERE voter_id = %d AND user_id = %d
  343. AND (modification = {REPUTATION_INC} OR modification = {REPUTATION_DEC})
  344. ORDER BY id DESC
  345. LIMIT 1', $actor['user_id'], empty($subject['voter_id']) ? $subject['user_id'] : $subject['voter_id']);
  346. if ($row = $db->sql_fetchrow($result))
  347. {
  348. if ($board_config['reputation_time_limit'] && ($current_time < $row['date'] + $board_config['reputation_time_limit'] * 60))
  349. {
  350. return sprintf($lang['reputation_time_limit'], $board_config['reputation_time_limit']);
  351. }
  352. if ($board_config['reputation_rotation_limit'])
  353. {
  354. $result = db_query('SELECT COUNT(*) AS cnt FROM {REPUTATION_TABLE}
  355. WHERE voter_id = %d AND id > %d
  356. AND (modification = {REPUTATION_INC} OR modification = {REPUTATION_DEC})
  357. GROUP BY voter_id',
  358. $actor['user_id'], $row['id']);
  359.  
  360. if (!($row = $db->sql_fetchrow($result)) || $row['cnt'] < $board_config['reputation_rotation_limit'])
  361. {
  362. return sprintf($lang['reputation_rotation_limit'], $board_config['reputation_rotation_limit']);
  363. }
  364. }
  365. }
  366. }
  367.  
  368. return false;
  369. }
  370.  
  371. function reputation_auth_personal(&$auth, $actor, $subject, $quick)
  372. {
  373. global $db, $lang, $board_config;
  374.  
  375. static $cache = array();
  376. $subject_id = empty($subject['voter_id']) ? $subject['user_id'] : $subject['voter_id'];
  377. $pair_id = $actor['user_id'] . ',' . $subject_id;
  378.  
  379. if (isset($cache[$pair_id]))
  380. {
  381. $fix = $cache[$pair_id];
  382. }
  383. else
  384. {
  385. $subject_is_mod = ($subject['user_level'] == ADMIN || $subject['user_level'] == MOD);
  386.  
  387. $fix = array(
  388. 'no_rep' => ($subject['user_id'] == ANONYMOUS || $subject['user_level'] == MOD && $board_config['reputation_mod_norep'] || $subject['user_level'] == ADMIN && $board_config['reputation_admin_norep']),
  389. 'no_warn' => ($subject_id == ANONYMOUS || $subject_is_mod),
  390. );
  391.  
  392. if ($subject_id == ANONYMOUS)
  393. {
  394. $fix['auth_view_rep'] = $fix['auth_add_rep'] = $fix['auth_warn'] = $lang['reputation_anonymous_no_reputation'];
  395. }
  396. elseif ($actor['user_id'] == $subject_id)
  397. {
  398. $fix['auth_add_rep'] = $fix['auth_warn'] = $fix['auth_ban'] = $lang['reputation_self_no_modify'];
  399. }
  400. else
  401. {
  402. if ($fix['no_rep'])
  403. {
  404. $fix['auth_view_rep'] = $fix['auth_add_rep'] = $lang['reputation_not_applicable'];
  405. }
  406. elseif (!$auth['auth_no_limits'])
  407. {
  408. if ($message = reputation_check_limits($actor, $subject, $quick))
  409. {
  410. $fix['auth_add_rep'] = $message;
  411. }
  412. }
  413.  
  414. if ($subject_is_mod)
  415. {
  416. $fix['auth_view_warns'] = $fix['auth_warn'] = $fix['auth_ban'] = $lang['reputation_cant_warn_mods'];
  417. }
  418.  
  419. if ($actor['user_level'] == MOD)
  420. {
  421. /*if ($subject_is_mod)
  422. {
  423. $fix['auth_edit_rep'] = $fix['auth_delete_rep'] = $lang['reputation_other_mods_no_edit'];
  424. }*/
  425. }
  426. elseif ($actor['user_level'] != ADMIN)
  427. {
  428. $fix['auth_edit_rep'] = $fix['auth_delete_rep'] = $lang['reputation_others_no_edit'];
  429. }
  430. }
  431.  
  432. /*if ($subject_is_mod)
  433. {
  434. if ($actor['user_level'] == MOD)
  435. {
  436. $fix['auth_edit_warn'] = $fix['auth_delete_warn'] = false;
  437. }
  438. }*/
  439.  
  440. $cache[$pair_id] = $fix;
  441. }
  442.  
  443. foreach ($fix as $key => $value)
  444. {
  445. if (!empty($auth[$key]))
  446. {
  447. $auth[$key] = false;
  448. if ($value)
  449. {
  450. $auth[$key . '_msg'] = $value;
  451. }
  452. }
  453. }
  454. $auth['no_rep'] = $fix['no_rep']; $auth['no_warn'] = $fix['no_warn'];
  455. }
  456.  
  457. function server_url()
  458. {
  459. global $board_config, $phpEx;
  460.  
  461. if ($script_name = preg_replace('/^\/?(.*?)\/?$/', '\1', trim($board_config['script_path'])))
  462. {
  463. $script_name .= '/';
  464. }
  465.  
  466. $server_port = ($board_config['server_port'] != 80) ? (':' . trim($board_config['server_port']) . '/') : '/';
  467.  
  468. return ($board_config['cookie_secure'] ? 'https://' : 'http://') . trim($board_config['server_name']) . $server_port . $script_name;
  469. }
  470.  
  471. $board_config['reputation_perms'] = explode(',', $board_config['reputation_perms']);
  472. $board_config['reputation_warning_expire'] = $board_config['reputation_warning_expire'] ? explode(',', $board_config['reputation_warning_expire']) : array();
  473. $board_config['reputation_ban_expire'] = $board_config['reputation_ban_expire'] ? explode(',', $board_config['reputation_ban_expire']) : array();
  474.  
  475. ?>