PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/phpBB3/includes/points/points_robbery.php

https://gitlab.com/perioner/mfhs
PHP | 406 lines | 296 code | 66 blank | 44 comment | 36 complexity | 7eafd421fad7edcaf51821475810fee4 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package Ultimate Points
  5. * @version $Id: points_robbery.php 779 2011-02-05 10:06:37Z Wuerzi $
  6. * @copyright (c) 2009 wuerzi & femu
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. if (!defined('IN_PHPBB'))
  11. {
  12. exit;
  13. }
  14. /**
  15. * @package Ultimate Points
  16. */
  17. class points_robbery
  18. {
  19. var $u_action;
  20. function main($id, $mode)
  21. {
  22. global $template, $user, $db, $config, $phpEx, $phpbb_root_path, $ultimate_points, $points_config, $points_values, $auth, $checked_user, $check_auth;
  23. // Check, if user is allowed to use the robbery
  24. if (!$auth->acl_get('u_use_robbery'))
  25. {
  26. $message = $user->lang['NOT_AUTHORISED'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.$phpEx") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
  27. trigger_error($message);
  28. }
  29. // Check, if robbery is enabled
  30. if (!$points_config['robbery_enable'])
  31. {
  32. $message = $user->lang['ROBBERY_DISABLED'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.$phpEx") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
  33. trigger_error($message);
  34. }
  35. // Add part to bar
  36. $template->assign_block_vars('navlinks', array(
  37. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery"),
  38. 'FORUM_NAME' => sprintf($user->lang['POINTS_ROBBERY'], $config['points_name']),
  39. ));
  40. // Read out cash of current user
  41. $pointsa = $user->data['user_points'];
  42. // Check key
  43. add_form_key('robbery_attack');
  44. if(isset($_POST['submit']))
  45. {
  46. if (!check_form_key('robbery_attack'))
  47. {
  48. trigger_error('FORM_INVALID');
  49. }
  50. // Add all required informations
  51. $username = utf8_normalize_nfc(request_var('username', '', true));
  52. $attacked_amount = round(request_var('attacked_amount', 0.00),2);
  53. if ($attacked_amount <= 0)
  54. {
  55. $message = $user->lang['ROBBERY_TOO_SMALL_AMOUNT'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
  56. trigger_error($message);
  57. }
  58. // Check, if user has entered the name of the user to be robbed
  59. if (empty($username))
  60. {
  61. $message = $user->lang['ROBBERY_NO_ID_SPECIFIED'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
  62. trigger_error($message);
  63. }
  64. // Check, if user tries to rob himself
  65. if ($user->data['username_clean'] == utf8_clean_string($username))
  66. {
  67. $message = $user->lang['ROBBERY_SELF'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
  68. trigger_error($message);
  69. }
  70. // Check, if user is trying to rob to much cash
  71. if ($points_values['robbery_loose'] != 0)
  72. {
  73. if ($user->data['user_points'] < ($attacked_amount/100*$points_values['robbery_loose']))
  74. {
  75. $message = $user->lang['ROBBERY_TO_MUCH'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
  76. trigger_error($message);
  77. }
  78. }
  79. // Select the user_id and language of user to be robbed
  80. $sql_array = array(
  81. 'SELECT' => 'user_id',
  82. 'FROM' => array(
  83. USERS_TABLE => 'u',
  84. ),
  85. 'WHERE' => 'username_clean = "' . $db->sql_escape(utf8_clean_string($username)) . '"',
  86. );
  87. $sql = $db->sql_build_query('SELECT', $sql_array);
  88. $result = $db->sql_query($sql);
  89. $user_id = (int) $db->sql_fetchfield('user_id');
  90. $db->sql_freeresult($result);
  91. $sql_array = array(
  92. 'SELECT' => '*',
  93. 'FROM' => array(
  94. USERS_TABLE => 'u',
  95. ),
  96. 'WHERE' => 'user_id = "' . (int) $user_id . '"',
  97. );
  98. $sql = $db->sql_build_query('SELECT', $sql_array);
  99. $result = $db->sql_query($sql);
  100. $user_info = $db->sql_fetchrow($result);;
  101. $db->sql_freeresult($result);
  102. // If no matching user id is found
  103. if (!$user_id)
  104. {
  105. $message = $user->lang['POINTS_NO_USER'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
  106. trigger_error($message);
  107. }
  108. // If the robbed user doesn't have enough cash
  109. $sql_array = array(
  110. 'SELECT' => 'user_points, user_robbery_pm',
  111. 'FROM' => array(
  112. USERS_TABLE => 'u',
  113. ),
  114. 'WHERE' => 'user_id = ' . (int) $user_id,
  115. );
  116. $sql = $db->sql_build_query('SELECT', $sql_array);
  117. $result = $db->sql_query($sql);
  118. $user1_row = $db->sql_fetchrow($result);
  119. $pointsa = $user1_row['user_points'];
  120. $user_robbery_pm = $user1_row['user_robbery_pm'];
  121. $db->sql_freeresult($result);
  122. if ($attacked_amount > $pointsa)
  123. {
  124. $message = $user->lang['ROBBERY_TO_MUCH_FROM_USER'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
  125. trigger_error($message);
  126. }
  127. // Check, if user tries to rob more than x % of users cash
  128. if ($points_values['robbery_max_rob'] != 0)
  129. {
  130. if ($attacked_amount > ($pointsa/100*$points_values['robbery_max_rob']))
  131. {
  132. $message = sprintf($user->lang['ROBBERY_MAX_ROB'], $points_values['robbery_max_rob']) . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
  133. trigger_error($message);
  134. }
  135. }
  136. // Get some info about the robbed user
  137. $user_namepoints = get_username_string('full', $checked_user['user_id'], $checked_user['username'], $checked_user['user_colour']);
  138. // Genarate a random number
  139. $rand_base = $points_values['robbery_chance'];
  140. $rand_value = rand(0, 100);
  141. // If robbery was successful and PM is enabled, send PM
  142. if ($rand_value <= $rand_base)
  143. {
  144. add_points($user->data['user_id'], $attacked_amount);
  145. substract_points($user_id, $attacked_amount);
  146. if ($points_config['robbery_sendpm'] && $user_info['user_allow_pm'] == 1 && $user_robbery_pm)
  147. {
  148. // Prepare user lang
  149. $sql_array = array(
  150. 'SELECT' => '*',
  151. 'FROM' => array(
  152. USERS_TABLE => 'u',
  153. ),
  154. 'WHERE' => 'user_id = ' . (int) $user_id,
  155. );
  156. $sql = $db->sql_build_query('SELECT', $sql_array);
  157. $result = $db->sql_query($sql);
  158. $user_row = $db->sql_fetchrow($result);
  159. $db->sql_freeresult($result);
  160. // first check if language file exists, if not, use the default language
  161. $user_row['user_lang'] = (file_exists($phpbb_root_path . 'language/' . $user_row['user_lang'] . "/mods/points.$phpEx")) ? $user_row['user_lang'] : $config['default_lang'];
  162. // load receivers language
  163. include($phpbb_root_path . 'language/' . basename($user_row['user_lang']) . "/mods/points.$phpEx");
  164. // Send PM
  165. $pm_subject = utf8_normalize_nfc(sprintf($lang['ROBBERY_PM_SUCCESFUL_SUBJECT'], $config['points_name']));
  166. $pm_text = utf8_normalize_nfc(sprintf($lang['ROBBERY_PM_SUCCESFUL_BODY'], $user_namepoints, $attacked_amount, $config['points_name']));
  167. $poll = $uid = $bitfield = $options = '';
  168. generate_text_for_storage($pm_subject, $uid, $bitfield, $options, false, false, false);
  169. generate_text_for_storage($pm_text, $uid, $bitfield, $options, true, true, true);
  170. $pm_data = array(
  171. 'address_list' => array ('u' => array($user_id => 'to')),
  172. 'from_user_id' => $user->data['user_id'],
  173. 'icon_id' => 0,
  174. 'from_username' => $user->lang['ROBBERY_PM_SENDER'],
  175. 'from_user_ip' => '',
  176. 'enable_bbcode' => true,
  177. 'enable_smilies' => true,
  178. 'enable_urls' => true,
  179. 'enable_sig' => true,
  180. 'message' => $pm_text,
  181. 'bbcode_bitfield' => $bitfield,
  182. 'bbcode_uid' => $uid,
  183. );
  184. submit_pm('post', $pm_subject, $pm_data, false);
  185. }
  186. $message = $user->lang['ROBBERY_SUCCESFUL'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
  187. trigger_error($message);
  188. }
  189. // If robbery failed and PM is enabled, send PM
  190. else
  191. {
  192. if ($points_values['robbery_loose'] != 0)
  193. {
  194. $robbery_pm_info = '';
  195. $lose = $attacked_amount/100*$points_values['robbery_loose'];
  196. substract_points($user->data['user_id'], $lose);
  197. // Prepare user lang
  198. $sql_array = array(
  199. 'SELECT' => '*',
  200. 'FROM' => array(
  201. USERS_TABLE => 'u',
  202. ),
  203. 'WHERE' => 'user_id = ' . (int) $user_id,
  204. );
  205. $sql = $db->sql_build_query('SELECT', $sql_array);
  206. $result = $db->sql_query($sql);
  207. $user_row = $db->sql_fetchrow($result);
  208. $db->sql_freeresult($result);
  209. // What shall we do with the points we substract from the failed robbery?
  210. // Send it to the lottery jackpot
  211. if ($points_config['robbery_usage'])
  212. {
  213. $robbery_usage_info = $user->lang['ROBBERY_USAGE_INFO_LOTTERY'];
  214. $sql = 'UPDATE ' . POINTS_VALUES_TABLE . '
  215. SET lottery_jackpot = lottery_jackpot + ' . $lose;
  216. $result = $db->sql_query($sql);
  217. }
  218. else
  219. {
  220. // Select randomly, if robbed user will get the points or if it's added to the lottery jackpot
  221. $random_usage = mt_rand(0, 1);
  222. // Add to Lottery Jackpot
  223. if ($random_usage == 0)
  224. {
  225. $robbery_usage_info = $user->lang['ROBBERY_USAGE_INFO_LOTTERY'];
  226. $sql = 'UPDATE ' . POINTS_VALUES_TABLE . '
  227. SET lottery_jackpot = lottery_jackpot + ' . $lose;
  228. $result = $db->sql_query($sql);
  229. }
  230. // Else give robbed user
  231. else
  232. {
  233. $robbery_usage_info = sprintf($user->lang['ROBBERY_USAGE_INFO_USER'], $username);
  234. $robbery_pm_info = 1;
  235. $sql = 'UPDATE ' . USERS_TABLE . '
  236. SET user_points = user_points + ' . $lose . '
  237. WHERE user_id = ' . (int) $user_id;
  238. $result = $db->sql_query($sql);
  239. }
  240. }
  241. if ($points_config['robbery_sendpm'] && $user_info['user_allow_pm'] == 1 && $user_robbery_pm)
  242. {
  243. // Select the receiver language
  244. $user_row['user_lang'] = (file_exists($phpbb_root_path . 'language/' . $user_row['user_lang'] . "/mods/points.$phpEx")) ? $user_row['user_lang'] : $config['default_lang'];
  245. // load receivers language
  246. include($phpbb_root_path . 'language/' . basename($user_row['user_lang']) . "/mods/points.$phpEx");
  247. // Send PM
  248. $pm_subject = utf8_normalize_nfc($lang['ROBBERY_PM_BAD_SUBJECT']);
  249. if ($robbery_pm_info == 1)
  250. {
  251. $pm_text = utf8_normalize_nfc(sprintf($lang['ROBBERY_PM_BAD_BODY_1'], $user_namepoints, $attacked_amount, $config['points_name'], sprintf(number_format_points($lose))));
  252. }
  253. else
  254. {
  255. $pm_text = utf8_normalize_nfc(sprintf($lang['ROBBERY_PM_BAD_BODY'], $user_namepoints, $attacked_amount, $config['points_name']));
  256. }
  257. $poll = $uid = $bitfield = $options = '';
  258. generate_text_for_storage($pm_subject, $uid, $bitfield, $options, false, false, false);
  259. generate_text_for_storage($pm_text, $uid, $bitfield, $options, true, true, true);
  260. $pm_data = array(
  261. 'address_list' => array ('u' => array($user_id => 'to')),
  262. 'from_user_id' => $user->data['user_id'],
  263. 'from_username' => $user->lang['ROBBERY_PM_SENDER'],
  264. 'icon_id' => 0,
  265. 'from_user_ip' => '',
  266. 'enable_bbcode' => true,
  267. 'enable_smilies' => true,
  268. 'enable_urls' => true,
  269. 'enable_sig' => true,
  270. 'message' => $pm_text,
  271. 'bbcode_bitfield' => $bitfield,
  272. 'bbcode_uid' => $uid,
  273. );
  274. submit_pm('post', $pm_subject, $pm_data, false);
  275. }
  276. $message = $user->lang['ROBBERY_BAD'] . '<br /><br />' . $robbery_usage_info . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
  277. trigger_error($message);
  278. }
  279. }
  280. $template->assign_vars(array(
  281. 'USER_NAME' => get_username_string('full', $checked_user['user_id'], $points_config['username'], $points_config['user_colour']),
  282. 'U_ACTION' => $this->u_action,
  283. 'S_HIDDEN_FIELDS' => $hidden_fields,
  284. ));
  285. }
  286. // If Robbery PN is enabled, show option to disable for the users
  287. if ($points_config['robbery_sendpm'])
  288. {
  289. if(isset($_POST['robbery_pm']))
  290. {
  291. if (!check_form_key('robbery_attack'))
  292. {
  293. trigger_error('FORM_INVALID');
  294. }
  295. $user_robbery_pm = request_var('user_robbery_pm', 0);
  296. $sql = 'UPDATE ' . USERS_TABLE . '
  297. SET user_robbery_pm = ' . $user_robbery_pm . '
  298. WHERE user_id = ' . (int) $user->data['user_id'];
  299. $result = $db->sql_query($sql);
  300. $redirect_url = append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery");
  301. meta_refresh(5, $redirect_url);
  302. $message = $user->lang['ROBBERY_PM_CHANGE'] . '<br /><br /><a href="' . append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery") . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
  303. trigger_error($message);
  304. $template->assign_vars(array(
  305. 'USER_ROBBERY_PM' => $user->data['user_robbery_pm'],
  306. 'U_ACTION' => $this->u_action,
  307. ));
  308. }
  309. }
  310. $template->assign_vars(array(
  311. 'S_ROBBERY_PM' => $points_config['robbery_sendpm'],
  312. 'USER_POINTS' => sprintf(number_format_points($pointsa)),
  313. 'POINTS_NAME' => $config['points_name'],
  314. 'LOTTERY_NAME' => $points_values['lottery_name'],
  315. 'BANK_NAME' => $points_values['bank_name'],
  316. 'L_ROBBERY_CHANCE' => sprintf($user->lang['ROBBERY_CHANCE'], (number_format_points($points_values['robbery_max_rob'])), (number_format_points($points_values['robbery_chance']))),
  317. 'L_ROBBERY_AMOUNTLOSE' => sprintf($user->lang['ROBBERY_AMOUNTLOSE'], (number_format_points($points_values['robbery_loose']))),
  318. 'U_FIND_USERNAME' => append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=searchuser&amp;form=post&amp;field=username&amp;select_single=true"),
  319. 'U_TRANSFER_USER' => append_sid("{$phpbb_root_path}points.$phpEx", "mode=transfer_user"),
  320. 'U_LOGS' => append_sid("{$phpbb_root_path}points.$phpEx", "mode=logs"),
  321. 'U_LOTTERY' => append_sid("{$phpbb_root_path}points.$phpEx", "mode=lottery"),
  322. 'U_BANK' => append_sid("{$phpbb_root_path}points.$phpEx", "mode=bank"),
  323. 'U_ROBBERY' => append_sid("{$phpbb_root_path}points.$phpEx", "mode=robbery"),
  324. 'U_INFO' => append_sid("{$phpbb_root_path}points.$phpEx", "mode=info"),
  325. 'U_USE_TRANSFER' => $auth->acl_get('u_use_transfer'),
  326. 'U_USE_LOGS' => $auth->acl_get('u_use_logs'),
  327. 'U_USE_LOTTERY' => $auth->acl_get('u_use_lottery'),
  328. 'U_USE_BANK' => $auth->acl_get('u_use_bank'),
  329. 'U_USE_ROBBERY' => $auth->acl_get('u_use_robbery'),
  330. ));
  331. // Generate the page
  332. page_header($user->lang['POINTS_ROBBERY']);
  333. // Generate the page template
  334. $template->set_filenames(array(
  335. 'body' => 'points/points_robbery.html'
  336. ));
  337. page_footer();
  338. }
  339. }
  340. ?>