Advertisement
Guest User

hooks.php

a guest
Aug 5th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 50.89 KB | None | 0 0
  1. <?php
  2. /***************************************************************************
  3. *
  4. * NewPoints plugin (/inc/plugins/newpoints/core/hooks.php)
  5. * Author: Pirata Nervo
  6. * Copyright: © 2014 Pirata Nervo
  7. *
  8. * Website: http://www.mybb-plugins.com
  9. *
  10. * NewPoints plugin for MyBB - A complex but efficient points system for MyBB.
  11. *
  12. ***************************************************************************/
  13.  
  14. /****************************************************************************
  15. This program is free software: you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation, either version 3 of the License, or
  18. (at your option) any later version.
  19.  
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. ****************************************************************************/
  28.  
  29. if(!defined("IN_MYBB") || !defined("NP_HOOKS"))
  30. die("This file cannot be accessed directly.");
  31.  
  32. if (NP_HOOKS == 1)
  33. {
  34. $plugins->add_hook('admin_load', 'newpoints_admin_load_hook');
  35. $plugins->add_hook('admin_newpoints_menu', 'newpoints_admin_menu_hook');
  36. $plugins->add_hook('admin_newpoints_action_handler', 'newpoints_admin_action_handler_hook');
  37. $plugins->add_hook('admin_newpoints_permissions', 'newpoints_admin_permissions_hook');
  38.  
  39. function newpoints_admin_load_hook()
  40. {
  41. global $plugins, $newpoints_plugins, $mybb;
  42.  
  43. if (!$newpoints_plugins || !isset($newpoints_plugins))
  44. {
  45. newpoints_load_plugins();
  46. }
  47.  
  48. // as plugins can't hook to admin_load, we must allow them to hook to newpoints_admin_load
  49. $plugins->run_hooks("newpoints_admin_load");
  50. }
  51.  
  52. function newpoints_admin_menu_hook(&$sub_menu)
  53. {
  54. global $plugins, $newpoints_plugins;
  55.  
  56. if (!$newpoints_plugins || !isset($newpoints_plugins))
  57. {
  58. newpoints_load_plugins();
  59. }
  60.  
  61. // as plugins can't hook to admin_newpoints_menu, we must allow them to hook to newpoints_admin_newpoints_menu
  62. $sub_menu = $plugins->run_hooks("newpoints_admin_newpoints_menu", $sub_menu);
  63. }
  64.  
  65. function newpoints_admin_action_handler_hook(&$actions)
  66. {
  67. global $plugins, $newpoints_plugins;
  68.  
  69. if (!$newpoints_plugins || !isset($newpoints_plugins))
  70. {
  71. newpoints_load_plugins();
  72. }
  73.  
  74. // as plugins can't hook to admin_newpoints_action_handler, we must allow them to hook to newpoints_newpoints_action_handler
  75. $actions = $plugins->run_hooks("newpoints_admin_newpoints_action_handler", $actions);
  76. }
  77.  
  78. function newpoints_admin_permissions_hook(&$admin_permissions)
  79. {
  80. global $plugins, $newpoints_plugins;
  81.  
  82. if (!$newpoints_plugins || !isset($newpoints_plugins))
  83. {
  84. newpoints_load_plugins();
  85. }
  86.  
  87. // as plugins can't hook to admin_newpoints_permissions, we must allow them to hook to newpoints_newpoints_permissions
  88. $admin_permissions = $plugins->run_hooks("newpoints_admin_newpoints_permissions", $admin_permissions);
  89. }
  90. }
  91. // outside ACP hooks
  92. elseif (NP_HOOKS == 2)
  93. {
  94. $plugins->add_hook('global_start', 'newpoints_plugins_start');
  95. $plugins->add_hook('xmlhttp', 'newpoints_load_xmlhttp'); // we want to make sure plugins can use the xmlhttp file
  96. $plugins->add_hook('archive_start', 'newpoints_load_archive'); // we want to make sure plugins can use the archive
  97.  
  98. // postbit
  99. $plugins->add_hook('postbit', 'newpoints_postbit', 50); // set priority to 50
  100. $plugins->add_hook('postbit_prev', 'newpoints_postbit', 50); // set priority to 50
  101. $plugins->add_hook('postbit_pm', 'newpoints_postbit', 50); // set priority to 50
  102. $plugins->add_hook('postbit_announcement', 'newpoints_postbit', 50); // set priority to 50
  103.  
  104. // member profile
  105. $plugins->add_hook("member_profile_end", "newpoints_profile");
  106.  
  107. // per new post
  108. $plugins->add_hook('datahandler_post_insert_post', 'newpoints_newpost');
  109. // edit post
  110. $plugins->add_hook('datahandler_post_update', 'newpoints_editpost');
  111. $plugins->add_hook('xmlhttp', 'newpoints_editpost_xmlhttp');
  112.  
  113. // delete post
  114. $plugins->add_hook('class_moderation_delete_post_start', 'newpoints_deletepost');
  115. // soft delete posts
  116. $plugins->add_hook('class_moderation_soft_delete_posts', 'newpoints_softdeleteposts');
  117. // restore soft deleted posts
  118. $plugins->add_hook('class_moderation_restore_posts', 'newpoints_restoreposts');
  119.  
  120. // approve threads
  121. $plugins->add_hook('class_moderation_approve_threads', 'newpoints_approvethreads');
  122. // approve posts
  123. $plugins->add_hook('class_moderation_approve_posts', 'newpoints_approveposts');
  124.  
  125. // unapprove threads
  126. $plugins->add_hook('class_moderation_unapprove_threads', 'newpoints_unapprovethreads');
  127. // unapprove posts
  128. $plugins->add_hook('class_moderation_unapprove_posts', 'newpoints_unapproveposts');
  129.  
  130. // per new thread
  131. $plugins->add_hook('datahandler_post_insert_thread', 'newpoints_newthread');
  132. // delete thread
  133. $plugins->add_hook('class_moderation_delete_thread', 'newpoints_deletethread');
  134. // soft delete threads
  135. $plugins->add_hook('class_moderation_soft_delete_threads', 'newpoints_softdeletethreads');
  136. // restore soft deleted threads
  137. $plugins->add_hook('class_moderation_restore_threads', 'newpoints_restorethreads');
  138.  
  139. // per new poll
  140. $plugins->add_hook('polls_do_newpoll_process', 'newpoints_newpoll');
  141. $plugins->add_hook('class_moderation_delete_poll', 'newpoints_deletepoll');
  142.  
  143. // per new registration
  144. $plugins->add_hook("member_do_register_end", "newpoints_newreg");
  145.  
  146. // per poll vote
  147. $plugins->add_hook('polls_vote_process', 'newpoints_pervote');
  148.  
  149. // per pm sent
  150. $plugins->add_hook('private_do_send_end', 'newpoints_pmsent');
  151.  
  152. // per thread rate
  153. $plugins->add_hook('ratethread_process', 'newpoints_perrate');
  154.  
  155. // per page viewed and visit
  156. $plugins->add_hook('global_end', 'newpoints_perview');
  157.  
  158. // minimum points to view
  159. $plugins->add_hook('forumdisplay_end', 'newpoints_blockview');
  160. $plugins->add_hook('showthread_start', 'newpoints_blockview');
  161. $plugins->add_hook('editpost_start', 'newpoints_blockview_edit');
  162. $plugins->add_hook('sendthread_do_sendtofriend_start', 'newpoints_blockview_send');
  163. $plugins->add_hook('sendthread_start', 'newpoints_blockview_send');
  164. $plugins->add_hook('archive_forum_start', 'newpoints_blockview_archive');
  165. $plugins->add_hook('archive_thread_start', 'newpoints_blockview_archive');
  166. $plugins->add_hook('printthread_end', 'newpoints_blockview_print');
  167.  
  168. // minimum points to post
  169. $plugins->add_hook('newreply_start', 'newpoints_blockpost');
  170. $plugins->add_hook('newreply_do_newreply_start', 'newpoints_blockpost');
  171. $plugins->add_hook('newthread_start', 'newpoints_blockpost');
  172. $plugins->add_hook('newthread_do_newthread_start', 'newpoints_blockpost');
  173.  
  174. // ****************** //
  175. // GENERAL HOOKS START //
  176. // ****************** //
  177.  
  178. // Loads plugins when in archive and runs a new hook called 'newpoints_archive_start' that can be used by NewPoints plugins (instead of archive_start)
  179. function newpoints_load_archive()
  180. {
  181. global $plugins;
  182.  
  183. newpoints_load_plugins();
  184. //newpoints_load_settings();
  185.  
  186. // as plugins can't hook to archive_start, we must allow them to hook to newpoints_archive_start
  187. $plugins->run_hooks("newpoints_archive_start");
  188. }
  189.  
  190. // Loads plugins from global_start and runs a new hook called 'newpoints_global_start' that can be used by NewPoints plugins (instead of global_start)
  191. // global_start can't be used by NP plugins
  192. function newpoints_plugins_start()
  193. {
  194. global $plugins, $mybb, $mypoints;
  195.  
  196. newpoints_load_plugins();
  197. //newpoints_load_settings();
  198.  
  199. if($mybb->user['uid'] > 0)
  200. {
  201. $mypoints = newpoints_format_points($mybb->user['newpoints']);
  202. }
  203. else
  204. $mypoints = 0;
  205.  
  206. // as plugins can't hook to global_start, we must allow them to hook to global_start
  207. $plugins->run_hooks("newpoints_global_start");
  208. }
  209.  
  210. // Loads plugins from global_start and runs a new hook called 'newpoints_global_start' that can be used by NewPoints plugins (instead of global_start)
  211. // global_start can't be used by NP plugins
  212. function newpoints_load_xmlhttp()
  213. {
  214. global $plugins;
  215.  
  216. newpoints_load_plugins();
  217. //newpoints_load_settings();
  218.  
  219. // as plugins can't hook to xmlhttp, we must allow them to hook to newpoints_xmlhttp
  220. $plugins->run_hooks("newpoints_xmlhttp");
  221. }
  222.  
  223. // postbit
  224. function newpoints_postbit(&$post)
  225. {
  226. global $mybb, $db, $currency, $points, $templates, $donate, $lang, $uid;
  227.  
  228. if($post['uid'] == 0)
  229. {
  230. $post['newpoints_postbit'] = '';
  231. return;
  232. }
  233.  
  234. if ($mybb->settings['newpoints_main_enabled'] != 1)
  235. {
  236. $post['newpoints_postbit'] = '';
  237. return;
  238. }
  239.  
  240. $lang->load("newpoints");
  241.  
  242. $currency = $mybb->settings['newpoints_main_curname'];
  243. $points = newpoints_format_points($post['newpoints']);
  244. $uid = intval($post['uid']);
  245.  
  246. if ($mybb->settings['newpoints_main_donationsenabled'] && $post['uid'] != $mybb->user['uid'] && $mybb->user['uid'] > 0)
  247. eval("\$donate = \"".$templates->get('newpoints_donate_inline')."\";");
  248. else
  249. $donate = '';
  250.  
  251. eval("\$post['newpoints_postbit'] = \"".$templates->get('newpoints_postbit')."\";");
  252. }
  253.  
  254. // member profile
  255. function newpoints_profile()
  256. {
  257. global $mybb, $db, $currency, $points, $templates, $memprofile, $newpoints_profile, $lang, $uid;
  258.  
  259. if ($mybb->settings['newpoints_main_enabled'] != 1)
  260. {
  261. $newpoints_profile = '';
  262. return;
  263. }
  264.  
  265. $lang->load("newpoints");
  266.  
  267. $currency = $mybb->settings['newpoints_main_curname'];
  268. $points = newpoints_format_points($memprofile['newpoints']);
  269. $uid = intval($memprofile['uid']);
  270.  
  271. if ($mybb->settings['newpoints_main_donationsenabled'] && $memprofile['uid'] != $mybb->user['uid'] && $mybb->user['uid'] > 0)
  272. eval("\$donate = \"".$templates->get('newpoints_donate_inline')."\";");
  273. else
  274. $donate = '';
  275.  
  276. eval("\$newpoints_profile = \"".$templates->get('newpoints_profile')."\";");
  277. }
  278.  
  279. // ****************** //
  280. // INCOME HOOKS START //
  281. // ****************** //
  282.  
  283. // new post
  284. function newpoints_newpost(&$data)
  285. {
  286. global $db, $mybb, $post, $thread;
  287.  
  288. if ($mybb->input['action'] != "do_newreply" || $post['savedraft'])
  289. return;
  290.  
  291. if($data->post_insert_data['visible'] != 1)
  292. {
  293. // If it's not visible, then we may have moderation (drafts are already considered above so it doesn't matter here)
  294. return;
  295. }
  296.  
  297. if (!$mybb->user['uid'])
  298. return;
  299.  
  300. if ($mybb->settings['newpoints_main_enabled'] != 1)
  301. return;
  302.  
  303. if ($mybb->settings['newpoints_income_newpost'] == 0)
  304. return;
  305.  
  306. // check forum rules
  307. $forumrules = newpoints_getrules('forum', $data->post_insert_data['fid']);
  308. if (!$forumrules)
  309. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  310.  
  311. // if the forum rate is 0, nothing is going to be added so let's just leave the function
  312. if ($forumrules['rate'] == 0)
  313. return;
  314.  
  315. // check group rules - primary group check
  316. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  317. if (!$grouprules)
  318. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  319.  
  320. // if the group rate is 0, nothing is going to be added so let's just leave the function
  321. if ($grouprules['rate'] == 0)
  322. return;
  323.  
  324. // calculate points per character bonus
  325. // let's see if the number of characters in the post is greater than the minimum characters
  326. if (($charcount = newpoints_count_characters($post['message'])) >= $mybb->settings['newpoints_income_minchar'])
  327. $bonus = $charcount * $mybb->settings['newpoints_income_perchar'];
  328. else
  329. $bonus = 0;
  330.  
  331. // give points to the poster
  332. newpoints_addpoints($mybb->user['uid'], $mybb->settings['newpoints_income_newpost']+$bonus, $forumrules['rate'], $grouprules['rate']);
  333.  
  334. if ($thread['uid'] != $mybb->user['uid'])
  335. {
  336. // we are not the thread started so give points to him/her
  337. if ($mybb->settings['newpoints_income_perreply'] != 0)
  338. newpoints_addpoints($thread['uid'], $mybb->settings['newpoints_income_perreply'], $forumrules['rate'], $grouprules['rate']);
  339. }
  340. }
  341.  
  342. function newpoints_approveposts($pids)
  343. {
  344. global $db, $mybb, $fid;
  345.  
  346. if (!$mybb->user['uid'])
  347. return;
  348.  
  349. if ($mybb->settings['newpoints_main_enabled'] != 1)
  350. return;
  351.  
  352. if ($mybb->settings['newpoints_income_newpost'] == 0)
  353. return;
  354.  
  355. if(!empty($pids))
  356. {
  357. foreach($pids as $pid)
  358. {
  359. $post = get_post((int)$pid);
  360. $thread = get_thread($post['tid']);
  361.  
  362. // check forum rules
  363. $forumrules = newpoints_getrules('forum', $fid);
  364. if (!$forumrules)
  365. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  366.  
  367. // if the forum rate is 0, nothing is going to be removed so let's just leave the function
  368. if ($forumrules['rate'] == 0)
  369. return;
  370.  
  371. // check group rules - primary group check
  372. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  373. if (!$grouprules)
  374. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  375.  
  376. // if the group rate is 0, nothing is going to be removed so let's just leave the function
  377. if ($grouprules['rate'] == 0)
  378. return;
  379.  
  380. // calculate points per character bonus
  381. // let's see if the number of characters in the post is greater than the minimum characters
  382. if (($charcount = newpoints_count_characters($post['message'])) >= $mybb->settings['newpoints_income_minchar'])
  383. $bonus = $charcount * $mybb->settings['newpoints_income_perchar'];
  384. else
  385. $bonus = 0;
  386.  
  387. // the post author != thread author?
  388. if ($thread['uid'] != $post['uid'])
  389. {
  390. // we are not the thread started so give points to them
  391. if ($mybb->settings['newpoints_income_perreply'] != 0)
  392. newpoints_addpoints($thread['uid'], $mybb->settings['newpoints_income_perreply'], $forumrules['rate'], $grouprules['rate']);
  393. }
  394.  
  395. // give points to the author of the post
  396. newpoints_addpoints($post['uid'], $mybb->settings['newpoints_income_newpost']+$bonus, $forumrules['rate'], $grouprules['rate']);
  397. }
  398. }
  399. }
  400.  
  401. function newpoints_unapproveposts($pids)
  402. {
  403. global $db, $mybb, $fid;
  404.  
  405. if (!$mybb->user['uid'])
  406. return;
  407.  
  408. if ($mybb->settings['newpoints_main_enabled'] != 1)
  409. return;
  410.  
  411. if ($mybb->settings['newpoints_income_newpost'] == 0)
  412. return;
  413.  
  414. if(!empty($pids))
  415. {
  416. foreach($pids as $pid)
  417. {
  418. $post = get_post((int)$pid);
  419. $thread = get_thread($post['tid']);
  420.  
  421. // check forum rules
  422. $forumrules = newpoints_getrules('forum', $fid);
  423. if (!$forumrules)
  424. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  425.  
  426. // if the forum rate is 0, nothing is going to be removed so let's just leave the function
  427. if ($forumrules['rate'] == 0)
  428. return;
  429.  
  430. // check group rules - primary group check
  431. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  432. if (!$grouprules)
  433. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  434.  
  435. // if the group rate is 0, nothing is going to be removed so let's just leave the function
  436. if ($grouprules['rate'] == 0)
  437. return;
  438.  
  439. // calculate points per character bonus
  440. // let's see if the number of characters in the post is greater than the minimum characters
  441. if (($charcount = newpoints_count_characters($post['message'])) >= $mybb->settings['newpoints_income_minchar'])
  442. $bonus = $charcount * $mybb->settings['newpoints_income_perchar'];
  443. else
  444. $bonus = 0;
  445.  
  446. // the post author != thread author?
  447. if ($thread['uid'] != $post['uid'])
  448. {
  449. // we are not the thread started so remove points from them
  450. if ($mybb->settings['newpoints_income_perreply'] != 0)
  451. newpoints_addpoints($thread['uid'], -$mybb->settings['newpoints_income_perreply'], $forumrules['rate'], $grouprules['rate']);
  452. }
  453.  
  454. // give points to the author of the post
  455. newpoints_addpoints($post['uid'], -$mybb->settings['newpoints_income_newpost']-$bonus, $forumrules['rate'], $grouprules['rate']);
  456. }
  457. }
  458. }
  459.  
  460. // edit post
  461. function newpoints_editpost(&$newpost)
  462. {
  463. global $db, $mybb, $thread;
  464.  
  465. if (!$mybb->user['uid'])
  466. return;
  467.  
  468. if ($mybb->settings['newpoints_main_enabled'] != 1)
  469. return;
  470.  
  471. if ($mybb->settings['newpoints_income_perchar'] == 0)
  472. return;
  473.  
  474. if ($mybb->input['action'] != "do_editpost" || $mybb->input['editdraft'])
  475. return;
  476.  
  477. $fid = intval($newpost->data['fid']);
  478.  
  479. // check forum rules
  480. $forumrules = newpoints_getrules('forum', $fid);
  481. if (!$forumrules)
  482. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  483.  
  484. // if the forum rate is 0, nothing is going to be added so let's just leave the function
  485. if ($forumrules['rate'] == 0)
  486. return;
  487.  
  488. // check group rules - primary group check
  489. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  490. if (!$grouprules)
  491. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  492.  
  493. // if the group rate is 0, nothing is going to be added so let's just leave the function
  494. if ($grouprules['rate'] == 0)
  495. return;
  496.  
  497. // get old message
  498. $post = get_post(intval($newpost->data['pid']));
  499. $oldcharcount = newpoints_count_characters($post['message']);
  500. $newcharcount = newpoints_count_characters($newpost->data['message']);
  501.  
  502. // calculate points per character bonus
  503. // let's see if the number of characters in the post is greater than the minimum characters
  504. if ($newcharcount >= $mybb->settings['newpoints_income_minchar'])
  505. {
  506. // if we have more characters now
  507. if ($newcharcount > $oldcharcount)
  508. {
  509. // calculate bonus based on difference of characters
  510. // bonus will be positive as the new message is longer than the old one
  511. $bonus = ($newcharcount - $oldcharcount) * $mybb->settings['newpoints_income_perchar'];
  512. }
  513. // otherwise if the message is shorter
  514. elseif ($newcharcount < $oldcharcount)
  515. {
  516. // calculate bonus based on difference of characters
  517. // bonus will be negative as the new message is shorter than the old one
  518. $bonus = ($newcharcount - $oldcharcount) * $mybb->settings['newpoints_income_perchar'];
  519. }
  520. // else if the length is the same, the bonus is 0
  521. elseif ($newcharcount == $oldcharcount)
  522. {
  523. $bonus = 0;
  524. }
  525. }
  526. elseif ($newcharcount >= $mybb->settings['newpoints_income_minchar'] && $oldcharcount >= $mybb->settings['newpoints_income_minchar'])
  527. {
  528. // calculate bonus based on difference of characters
  529. // bonus will be negative as the new message is shorter than the minimum chars
  530. $bonus = ($newcharcount - $oldcharcount) * $mybb->settings['newpoints_income_perchar'];
  531. }
  532.  
  533. // give points to the poster
  534. newpoints_addpoints($mybb->user['uid'], $bonus, $forumrules['rate'], $grouprules['rate'], false, true);
  535. }
  536.  
  537. // edit post - counts less chars on edit because of \n\r being deleted
  538. function newpoints_editpost_xmlhttp()
  539. {
  540. global $db, $mybb, $thread, $lang, $charset;
  541.  
  542. if (!$mybb->user['uid'])
  543. return;
  544.  
  545. if ($mybb->settings['newpoints_main_enabled'] != 1)
  546. return;
  547.  
  548. if ($mybb->settings['newpoints_income_perchar'] == 0)
  549. return;
  550.  
  551. if ($mybb->input['action'] != "edit_post")
  552. return;
  553. elseif ($mybb->input['action'] == "edit_post" && $mybb->input['do'] != 'update_post')
  554. return;
  555.  
  556. if ($mybb->input['editdraft'])
  557. return;
  558.  
  559. // Verify POST request
  560. if(!verify_post_check($mybb->input['my_post_key'], true))
  561. {
  562. xmlhttp_error($lang->invalid_post_code);
  563. }
  564.  
  565. $post = get_post($mybb->input['pid']);
  566.  
  567. $fid = intval($post['fid']);
  568.  
  569. // check forum rules
  570. $forumrules = newpoints_getrules('forum', $fid);
  571. if (!$forumrules)
  572. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  573.  
  574. // if the forum rate is 0, nothing is going to be added so let's just leave the function
  575. if ($forumrules['rate'] == 0)
  576. return;
  577.  
  578. // check group rules - primary group check
  579. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  580. if (!$grouprules)
  581. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  582.  
  583. // if the group rate is 0, nothing is going to be added so let's just leave the function
  584. if ($grouprules['rate'] == 0)
  585. return;
  586.  
  587. // get old message
  588. $oldcharcount = newpoints_count_characters($post['message']);
  589.  
  590. $message = strval($_POST['value']);
  591. if(my_strtolower($charset) != "utf-8")
  592. {
  593. if(function_exists("iconv"))
  594. {
  595. $message = iconv($charset, "UTF-8//IGNORE", $message);
  596. }
  597. else if(function_exists("mb_convert_encoding"))
  598. {
  599. $message = @mb_convert_encoding($message, $charset, "UTF-8");
  600. }
  601. else if(my_strtolower($charset) == "iso-8859-1")
  602. {
  603. $message = utf8_decode($message);
  604. }
  605. }
  606.  
  607. $newcharcount = newpoints_count_characters($message);
  608.  
  609. // calculate points per character bonus
  610. // let's see if the number of characters in the post is greater than the minimum characters
  611. if ($newcharcount >= $mybb->settings['newpoints_income_minchar'])
  612. {
  613. // if we have more characters now
  614. if ($newcharcount > $oldcharcount)
  615. {
  616. // calculate bonus based on difference of characters
  617. // bonus will be positive as the new message is longer than the old one
  618. $bonus = ($newcharcount - $oldcharcount) * $mybb->settings['newpoints_income_perchar'];
  619. }
  620. // otherwise if the message is shorter
  621. elseif ($newcharcount < $oldcharcount)
  622. {
  623. // calculate bonus based on difference of characters
  624. // bonus will be positive as the new message is longer than the old one
  625. $bonus = ($newcharcount - $oldcharcount) * $mybb->settings['newpoints_income_perchar'];
  626. }
  627. // else if the length is the same, the bonus is 0
  628. elseif ($newcharcount == $oldcharcount)
  629. {
  630. $bonus = 0;
  631. }
  632. }
  633. else
  634. {
  635. // calculate bonus based on difference of characters
  636. // bonus will be negative as the new message is shorter than the minimum chars
  637. $bonus = ($newcharcount - $oldcharcount) * $mybb->settings['newpoints_income_perchar'];
  638. }
  639.  
  640. // give points to the poster
  641. newpoints_addpoints($mybb->user['uid'], $bonus, $forumrules['rate'], $grouprules['rate'], false, true);
  642. }
  643.  
  644.  
  645. // delete post
  646. function newpoints_deletepost($pid)
  647. {
  648. global $db, $mybb, $fid;
  649.  
  650. if (!$mybb->user['uid'])
  651. return;
  652.  
  653. if ($mybb->settings['newpoints_main_enabled'] != 1)
  654. return;
  655.  
  656. if ($mybb->settings['newpoints_income_newpost'] == 0)
  657. return;
  658.  
  659. $post = get_post((int)$pid);
  660. // It's currently soft deleted, so we do nothing as we already subtracted points when doing that
  661. // If it's not visible (unapproved) we also don't take out any money
  662. if($post['visible'] == -1 || $post['visible'] == 0)
  663. return;
  664.  
  665. $thread = get_thread($post['tid']);
  666.  
  667. // check forum rules
  668. $forumrules = newpoints_getrules('forum', $fid);
  669. if (!$forumrules)
  670. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  671.  
  672. // if the forum rate is 0, nothing is going to be removed so let's just leave the function
  673. if ($forumrules['rate'] == 0)
  674. return;
  675.  
  676. // check group rules - primary group check
  677. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  678. if (!$grouprules)
  679. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  680.  
  681. // if the group rate is 0, nothing is going to be removed so let's just leave the function
  682. if ($grouprules['rate'] == 0)
  683. return;
  684.  
  685. // calculate points per character bonus
  686. // let's see if the number of characters in the post is greater than the minimum characters
  687. if (($charcount = newpoints_count_characters($post['message'])) >= $mybb->settings['newpoints_income_minchar'])
  688. $bonus = $charcount * $mybb->settings['newpoints_income_perchar'];
  689. else
  690. $bonus = 0;
  691.  
  692. if ($thread['uid'] != $post['uid'])
  693. {
  694. // we are not the thread started so remove points from him/her
  695. if ($mybb->settings['newpoints_income_perreply'] != 0)
  696. newpoints_addpoints($thread['uid'], -$mybb->settings['newpoints_income_perreply'], $forumrules['rate'], $grouprules['rate']);
  697. }
  698.  
  699. // remove points from the poster
  700. newpoints_addpoints($post['uid'], -$mybb->settings['newpoints_income_newpost']-$bonus, $forumrules['rate'], $grouprules['rate']);
  701. }
  702.  
  703. // soft delete posts
  704. function newpoints_softdeleteposts($pids)
  705. {
  706. global $db, $mybb, $fid;
  707.  
  708. if (!$mybb->user['uid'])
  709. return;
  710.  
  711. if ($mybb->settings['newpoints_main_enabled'] != 1)
  712. return;
  713.  
  714. if ($mybb->settings['newpoints_income_newpost'] == 0)
  715. return;
  716.  
  717. if(!empty($pids))
  718. {
  719. foreach($pids as $pid)
  720. {
  721. $post = get_post((int)$pid);
  722. $thread = get_thread($post['tid']);
  723.  
  724. // check forum rules
  725. $forumrules = newpoints_getrules('forum', $fid);
  726. if (!$forumrules)
  727. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  728.  
  729. // if the forum rate is 0, nothing is going to be removed so let's just leave the function
  730. if ($forumrules['rate'] == 0)
  731. return;
  732.  
  733. // check group rules - primary group check
  734. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  735. if (!$grouprules)
  736. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  737.  
  738. // if the group rate is 0, nothing is going to be removed so let's just leave the function
  739. if ($grouprules['rate'] == 0)
  740. return;
  741.  
  742. // calculate points per character bonus
  743. // let's see if the number of characters in the post is greater than the minimum characters
  744. if (($charcount = newpoints_count_characters($post['message'])) >= $mybb->settings['newpoints_income_minchar'])
  745. $bonus = $charcount * $mybb->settings['newpoints_income_perchar'];
  746. else
  747. $bonus = 0;
  748.  
  749. // the post author != thread author?
  750. if ($thread['uid'] != $post['uid'])
  751. {
  752. // we are not the thread started so remove points from him/her
  753. if ($mybb->settings['newpoints_income_perreply'] != 0)
  754. newpoints_addpoints($thread['uid'], -$mybb->settings['newpoints_income_perreply'], $forumrules['rate'], $grouprules['rate']);
  755. }
  756.  
  757. // remove points from the poster
  758. newpoints_addpoints($post['uid'], -$mybb->settings['newpoints_income_newpost']-$bonus, $forumrules['rate'], $grouprules['rate']);
  759. }
  760. }
  761. }
  762.  
  763. // restore posts
  764. function newpoints_restoreposts($pids)
  765. {
  766. global $db, $mybb, $fid;
  767.  
  768. if (!$mybb->user['uid'])
  769. return;
  770.  
  771. if ($mybb->settings['newpoints_main_enabled'] != 1)
  772. return;
  773.  
  774. if ($mybb->settings['newpoints_income_newpost'] == 0)
  775. return;
  776.  
  777. if(!empty($pids))
  778. {
  779. foreach($pids as $pid)
  780. {
  781. $post = get_post((int)$pid);
  782. $thread = get_thread($post['tid']);
  783.  
  784. // check forum rules
  785. $forumrules = newpoints_getrules('forum', $fid);
  786. if (!$forumrules)
  787. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  788.  
  789. // if the forum rate is 0, nothing is going to be removed so let's just leave the function
  790. if ($forumrules['rate'] == 0)
  791. return;
  792.  
  793. // check group rules - primary group check
  794. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  795. if (!$grouprules)
  796. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  797.  
  798. // if the group rate is 0, nothing is going to be removed so let's just leave the function
  799. if ($grouprules['rate'] == 0)
  800. return;
  801.  
  802. // calculate points per character bonus
  803. // let's see if the number of characters in the post is greater than the minimum characters
  804. if (($charcount = newpoints_count_characters($post['message'])) >= $mybb->settings['newpoints_income_minchar'])
  805. $bonus = $charcount * $mybb->settings['newpoints_income_perchar'];
  806. else
  807. $bonus = 0;
  808.  
  809. // the post author != thread author?
  810. if ($thread['uid'] != $post['uid'])
  811. {
  812. // we are not the thread started so give points to them
  813. if ($mybb->settings['newpoints_income_perreply'] != 0)
  814. newpoints_addpoints($thread['uid'], $mybb->settings['newpoints_income_perreply'], $forumrules['rate'], $grouprules['rate']);
  815. }
  816.  
  817. // give points to the author of the post
  818. newpoints_addpoints($post['uid'], $mybb->settings['newpoints_income_newpost']+$bonus, $forumrules['rate'], $grouprules['rate']);
  819. }
  820. }
  821. }
  822.  
  823. // new thread
  824. function newpoints_newthread(&$that)
  825. {
  826. global $db, $mybb, $fid, $thread;
  827.  
  828. if ($mybb->input['action'] != "do_newthread" || $mybb->input['savedraft'])
  829. return;
  830.  
  831. if($that->thread_insert_data['visible'] != 1)
  832. {
  833. // If it's not visible, then we may have moderation (drafts are already considered above so it doesn't matter here)
  834. return;
  835. }
  836.  
  837. if (!$mybb->user['uid'])
  838. return;
  839.  
  840. if ($mybb->settings['newpoints_main_enabled'] != 1)
  841. return;
  842.  
  843. if ($mybb->settings['newpoints_income_newthread'] == 0)
  844. return;
  845.  
  846. // check forum rules
  847. $forumrules = newpoints_getrules('forum', $fid);
  848. if (!$forumrules)
  849. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  850.  
  851. // if the forum rate is 0, nothing is going to be added so let's just leave the function
  852. if ($forumrules['rate'] == 0)
  853. return;
  854.  
  855. // check group rules - primary group check
  856. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  857. if (!$grouprules)
  858. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  859.  
  860. // if the group rate is 0, nothing is going to be added so let's just leave the function
  861. if ($grouprules['rate'] == 0)
  862. return;
  863.  
  864. // calculate points per character bonus
  865. // let's see if the number of characters in the thread is greater than the minimum characters
  866. if (($charcount = newpoints_count_characters($mybb->input['message'])) >= $mybb->settings['newpoints_income_minchar'])
  867. $bonus = $charcount * $mybb->settings['newpoints_income_perchar'];
  868. else
  869. $bonus = 0;
  870.  
  871. // give points to the author of the new thread
  872. newpoints_addpoints($mybb->user['uid'], $mybb->settings['newpoints_income_newthread']+$bonus, $forumrules['rate'], $grouprules['rate']);
  873. }
  874.  
  875. function newpoints_approvethreads($tids)
  876. {
  877. global $db, $mybb, $fid;
  878.  
  879. if (!$mybb->user['uid'])
  880. return;
  881.  
  882. if ($mybb->settings['newpoints_main_enabled'] != 1)
  883. return;
  884.  
  885. if ($mybb->settings['newpoints_income_newthread'] == 0)
  886. return;
  887.  
  888. if(!empty($tids))
  889. {
  890. foreach($tids as $tid)
  891. {
  892. $thread = get_thread($tid);
  893. $post = get_post((int)$thread['firstpost']);
  894.  
  895. // check forum rules
  896. $forumrules = newpoints_getrules('forum', $fid);
  897. if (!$forumrules)
  898. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  899.  
  900. // if the forum rate is 0, nothing is going to be removed so let's just leave the function
  901. if ($forumrules['rate'] == 0)
  902. return;
  903.  
  904. // check group rules - primary group check
  905. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  906. if (!$grouprules)
  907. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  908.  
  909. // if the group rate is 0, nothing is going to be removed so let's just leave the function
  910. if ($grouprules['rate'] == 0)
  911. return;
  912.  
  913. // calculate points per character bonus
  914. // let's see if the number of characters in the post is greater than the minimum characters
  915. if (($charcount = newpoints_count_characters($post['message'])) >= $mybb->settings['newpoints_income_minchar'])
  916. $bonus = $charcount * $mybb->settings['newpoints_income_perchar'];
  917. else
  918. $bonus = 0;
  919.  
  920. // add points to the poster
  921. newpoints_addpoints($post['uid'], $mybb->settings['newpoints_income_newthread']+$bonus, $forumrules['rate'], $grouprules['rate']);
  922. }
  923. }
  924. }
  925.  
  926. function newpoints_unapprovethreads($tids)
  927. {
  928. global $db, $mybb, $fid;
  929.  
  930. if (!$mybb->user['uid'])
  931. return;
  932.  
  933. if ($mybb->settings['newpoints_main_enabled'] != 1)
  934. return;
  935.  
  936. if ($mybb->settings['newpoints_income_newthread'] == 0)
  937. return;
  938.  
  939. if(!empty($tids))
  940. {
  941. foreach($tids as $tid)
  942. {
  943. $thread = get_thread($tid);
  944. $post = get_post((int)$thread['firstpost']);
  945.  
  946. // check forum rules
  947. $forumrules = newpoints_getrules('forum', $fid);
  948. if (!$forumrules)
  949. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  950.  
  951. // if the forum rate is 0, nothing is going to be removed so let's just leave the function
  952. if ($forumrules['rate'] == 0)
  953. return;
  954.  
  955. // check group rules - primary group check
  956. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  957. if (!$grouprules)
  958. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  959.  
  960. // if the group rate is 0, nothing is going to be removed so let's just leave the function
  961. if ($grouprules['rate'] == 0)
  962. return;
  963.  
  964. // calculate points per character bonus
  965. // let's see if the number of characters in the post is greater than the minimum characters
  966. if (($charcount = newpoints_count_characters($post['message'])) >= $mybb->settings['newpoints_income_minchar'])
  967. $bonus = $charcount * $mybb->settings['newpoints_income_perchar'];
  968. else
  969. $bonus = 0;
  970.  
  971. // add points to the poster
  972. newpoints_addpoints($post['uid'], -$mybb->settings['newpoints_income_newthread']-$bonus, $forumrules['rate'], $grouprules['rate']);
  973. }
  974. }
  975. }
  976.  
  977. // delete thread
  978. function newpoints_deletethread($tid)
  979. {
  980. global $db, $mybb;
  981.  
  982. if (!$mybb->user['uid'])
  983. return;
  984.  
  985. if ($mybb->settings['newpoints_main_enabled'] != 1)
  986. return;
  987.  
  988. if ($mybb->settings['newpoints_income_newthread'] == 0)
  989. return;
  990.  
  991. // even though the thread was deleted it was previously cached so we can use get_thread
  992. $thread = get_thread((int)$tid);
  993. $fid = $thread['fid'];
  994.  
  995. // It's currently soft deleted, so we do nothing as we already subtracted points when doing that
  996. // If it's not visible (unapproved) we also don't take out any money
  997. if($thread['visible'] == -1 || $thread['visible'] == 0)
  998. return;
  999.  
  1000. // check forum rules
  1001. $forumrules = newpoints_getrules('forum', $fid);
  1002. if (!$forumrules)
  1003. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  1004.  
  1005. // if the forum rate is 0, nothing is going to be removed so let's just leave the function
  1006. if ($forumrules['rate'] == 0)
  1007. return;
  1008.  
  1009. // check group rules - primary group check
  1010. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  1011. if (!$grouprules)
  1012. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  1013.  
  1014. // if the group rate is 0, nothing is going to be removed so let's just leave the function
  1015. if ($grouprules['rate'] == 0)
  1016. return;
  1017.  
  1018. // get post of the thread
  1019. $post = get_post($thread['firstpost']);
  1020.  
  1021. // calculate points per character bonus
  1022. // let's see if the number of characters in the thread is greater than the minimum characters
  1023. if (($charcount = newpoints_count_characters($post['message'])) >= $mybb->settings['newpoints_income_minchar'])
  1024. $bonus = $charcount * $mybb->settings['newpoints_income_perchar'];
  1025. else
  1026. $bonus = 0;
  1027.  
  1028. if ($thread['poll'] != 0)
  1029. {
  1030. // if this thread has a poll, remove points from the author of the thread
  1031. newpoints_addpoints($thread['uid'], -$mybb->settings['newpoints_income_newpoll'], $forumrules['rate'], $grouprules['rate']);
  1032. }
  1033.  
  1034. $q = $db->simple_select('posts', 'COUNT(*) as total_replies', 'uid!='.(int)$thread['uid'].' AND tid='.(int)$thead['tid']);
  1035. $thread['replies'] = (int)$db->fetch_field($q, 'total_replies');
  1036. newpoints_addpoints($thread['uid'], -($thread['replies']*$mybb->settings['newpoints_income_perreply']), $forumrules['rate'], $grouprules['rate']);
  1037.  
  1038. // take out points from the author of the thread
  1039. newpoints_addpoints($thread['uid'], -$mybb->settings['newpoints_income_newthread']-$bonus, $forumrules['rate'], $grouprules['rate']);
  1040. }
  1041.  
  1042. // soft delete threads
  1043. function newpoints_softdeletethreads($tids)
  1044. {
  1045. global $db, $mybb, $fid;
  1046.  
  1047. if (!$mybb->user['uid'])
  1048. return;
  1049.  
  1050. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1051. return;
  1052.  
  1053. if ($mybb->settings['newpoints_income_newthread'] == 0)
  1054. return;
  1055.  
  1056. if(!empty($tids))
  1057. {
  1058. foreach($tids as $tid)
  1059. {
  1060. $thread = get_thread($tid);
  1061. $post = get_post((int)$thread['firstpost']);
  1062.  
  1063. // check forum rules
  1064. $forumrules = newpoints_getrules('forum', $fid);
  1065. if (!$forumrules)
  1066. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  1067.  
  1068. // if the forum rate is 0, nothing is going to be removed so let's just leave the function
  1069. if ($forumrules['rate'] == 0)
  1070. return;
  1071.  
  1072. // check group rules - primary group check
  1073. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  1074. if (!$grouprules)
  1075. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  1076.  
  1077. // if the group rate is 0, nothing is going to be removed so let's just leave the function
  1078. if ($grouprules['rate'] == 0)
  1079. return;
  1080.  
  1081. // calculate points per character bonus
  1082. // let's see if the number of characters in the post is greater than the minimum characters
  1083. if (($charcount = newpoints_count_characters($post['message'])) >= $mybb->settings['newpoints_income_minchar'])
  1084. $bonus = $charcount * $mybb->settings['newpoints_income_perchar'];
  1085. else
  1086. $bonus = 0;
  1087.  
  1088. // the post author != thread author?
  1089. if ($thread['uid'] != $post['uid'])
  1090. {
  1091. // we are not the thread started so remove points from him/her
  1092. if ($mybb->settings['newpoints_income_perreply'] != 0)
  1093. newpoints_addpoints($thread['uid'], -$mybb->settings['newpoints_income_perreply'], $forumrules['rate'], $grouprules['rate']);
  1094. }
  1095.  
  1096. // remove points from the poster
  1097. newpoints_addpoints($post['uid'], -$mybb->settings['newpoints_income_newthread']-$bonus, $forumrules['rate'], $grouprules['rate']);
  1098. }
  1099. }
  1100. }
  1101.  
  1102. // restore threads
  1103. function newpoints_restorethreads($tids)
  1104. {
  1105. global $db, $mybb, $fid;
  1106.  
  1107. if (!$mybb->user['uid'])
  1108. return;
  1109.  
  1110. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1111. return;
  1112.  
  1113. if ($mybb->settings['newpoints_income_newthread'] == 0)
  1114. return;
  1115.  
  1116. if(!empty($tids))
  1117. {
  1118. foreach($tids as $tid)
  1119. {
  1120. $thread = get_thread($tid);
  1121. $post = get_post((int)$thread['firstpost']);
  1122.  
  1123. // check forum rules
  1124. $forumrules = newpoints_getrules('forum', $fid);
  1125. if (!$forumrules)
  1126. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  1127.  
  1128. // if the forum rate is 0, nothing is going to be removed so let's just leave the function
  1129. if ($forumrules['rate'] == 0)
  1130. return;
  1131.  
  1132. // check group rules - primary group check
  1133. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  1134. if (!$grouprules)
  1135. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  1136.  
  1137. // if the group rate is 0, nothing is going to be removed so let's just leave the function
  1138. if ($grouprules['rate'] == 0)
  1139. return;
  1140.  
  1141. // calculate points per character bonus
  1142. // let's see if the number of characters in the post is greater than the minimum characters
  1143. if (($charcount = newpoints_count_characters($post['message'])) >= $mybb->settings['newpoints_income_minchar'])
  1144. $bonus = $charcount * $mybb->settings['newpoints_income_perchar'];
  1145. else
  1146. $bonus = 0;
  1147.  
  1148. // the post author != thread author?
  1149. if ($thread['uid'] != $post['uid'])
  1150. {
  1151. // we are not the thread started so give points to them
  1152. if ($mybb->settings['newpoints_income_perreply'] != 0)
  1153. newpoints_addpoints($thread['uid'], $mybb->settings['newpoints_income_perreply'], $forumrules['rate'], $grouprules['rate']);
  1154. }
  1155.  
  1156. // give points to the author of the post
  1157. newpoints_addpoints($post['uid'], $mybb->settings['newpoints_income_newthread']+$bonus, $forumrules['rate'], $grouprules['rate']);
  1158. }
  1159. }
  1160. }
  1161.  
  1162. // new poll
  1163. function newpoints_newpoll()
  1164. {
  1165. global $db, $mybb, $fid;
  1166.  
  1167. if (!$mybb->user['uid'])
  1168. return;
  1169.  
  1170. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1171. return;
  1172.  
  1173. if ($mybb->settings['newpoints_income_newpoll'] == 0)
  1174. return;
  1175.  
  1176. // check forum rules
  1177. $forumrules = newpoints_getrules('forum', $fid);
  1178. if (!$forumrules)
  1179. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  1180.  
  1181. // if the forum rate is 0, nothing is going to be added so let's just leave the function
  1182. if ($forumrules['rate'] == 0)
  1183. return;
  1184.  
  1185. // check group rules - primary group check
  1186. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  1187. if (!$grouprules)
  1188. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  1189.  
  1190. // if the group rate is 0, nothing is going to be added so let's just leave the function
  1191. if ($grouprules['rate'] == 0)
  1192. return;
  1193.  
  1194. // give points to the author of the new poll
  1195. newpoints_addpoints($mybb->user['uid'], $mybb->settings['newpoints_income_newpoll'], $forumrules['rate'], $grouprules['rate']);
  1196. }
  1197.  
  1198.  
  1199. // delete poll
  1200. function newpoints_deletepoll($pid)
  1201. {
  1202. global $db, $mybb;
  1203.  
  1204. if (!$mybb->user['uid'])
  1205. return;
  1206.  
  1207. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1208. return;
  1209.  
  1210. if ($mybb->settings['newpoints_income_newpoll'] == 0)
  1211. return;
  1212.  
  1213. $query = $db->simple_select("polls", "*", "pid = '{$pid}'");
  1214. $poll = $db->fetch_array($query);
  1215.  
  1216. $fid = $poll['fid'];
  1217.  
  1218. // check forum rules
  1219. $forumrules = newpoints_getrules('forum', $fid);
  1220. if (!$forumrules)
  1221. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  1222.  
  1223. // if the forum rate is 0, nothing is going to be added so let's just leave the function
  1224. if ($forumrules['rate'] == 0)
  1225. return;
  1226.  
  1227. // check group rules - primary group check
  1228. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  1229. if (!$grouprules)
  1230. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  1231.  
  1232. // if the group rate is 0, nothing is going to be added so let's just leave the function
  1233. if ($grouprules['rate'] == 0)
  1234. return;
  1235.  
  1236. // remove points from the author by deleting the poll
  1237. newpoints_addpoints($poll['uid'], -$mybb->settings['newpoints_income_newpoll'], $forumrules['rate'], $grouprules['rate']);
  1238. }
  1239.  
  1240. // new registration
  1241. function newpoints_newreg()
  1242. {
  1243. global $db, $mybb, $user_info;
  1244.  
  1245. // give points to our new user
  1246. if ($mybb->settings['newpoints_income_newreg'] != 0)
  1247. newpoints_addpoints(trim($mybb->input['username']), $mybb->settings['newpoints_income_newreg'], 1, 1, true);
  1248.  
  1249. if ($mybb->settings['newpoints_income_referral'] != 0)
  1250. {
  1251. // Grab the referred user's points
  1252. $query = $db->simple_select('users', 'uid,newpoints', 'username=\''.my_strtolower($db->escape_string(trim($mybb->input['referrername']))).'\'');
  1253. $user = $db->fetch_array($query);
  1254. if(empty($user))
  1255. return;
  1256.  
  1257. // check group rules - primary group check
  1258. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  1259. if (!$grouprules)
  1260. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  1261.  
  1262. // if the group rate is 0, nothing is going to be added so let's just leave the function
  1263. if ($grouprules['rate'] == 0)
  1264. return;
  1265.  
  1266. newpoints_addpoints($user['uid'], $mybb->settings['newpoints_income_referral'], 1, $grouprules['rate']);
  1267. }
  1268. }
  1269.  
  1270. // new poll vote
  1271. function newpoints_pervote()
  1272. {
  1273. global $db, $mybb, $fid;
  1274.  
  1275. if (!$mybb->user['uid'])
  1276. return;
  1277.  
  1278. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1279. return;
  1280.  
  1281. if ($mybb->settings['newpoints_income_pervote'] == 0)
  1282. return;
  1283.  
  1284. // check forum rules
  1285. $forumrules = newpoints_getrules('forum', $fid);
  1286. if (!$forumrules)
  1287. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  1288.  
  1289. // if the forum rate is 0, nothing is going to be added so let's just leave the function
  1290. if ($forumrules['rate'] == 0)
  1291. return;
  1292.  
  1293. // check group rules - primary group check
  1294. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  1295. if (!$grouprules)
  1296. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  1297.  
  1298. // if the group rate is 0, nothing is going to be added so let's just leave the function
  1299. if ($grouprules['rate'] == 0)
  1300. return;
  1301.  
  1302. // give points to us as we're voting in a poll
  1303. newpoints_addpoints($mybb->user['uid'], $mybb->settings['newpoints_income_pervote'], $forumrules['rate'], $grouprules['rate']);
  1304. }
  1305.  
  1306. // send a pm
  1307. function newpoints_pmsent()
  1308. {
  1309. global $pmhandler, $pminfo, $db, $mybb;
  1310.  
  1311. if (!$mybb->user['uid'])
  1312. return;
  1313.  
  1314. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1315. return;
  1316.  
  1317. if ($mybb->settings['newpoints_income_pmsent'] == 0)
  1318. return;
  1319.  
  1320. if(isset($pminfo['draftsaved']))
  1321. return;
  1322.  
  1323. if($mybb->user['uid'] == $pmhandler->data['toid'])
  1324. return;
  1325.  
  1326. // check group rules - primary group check
  1327. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  1328. if (!$grouprules)
  1329. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  1330.  
  1331. // if the group rate is 0, nothing is going to be added so let's just leave the function
  1332. if ($grouprules['rate'] == 0)
  1333. return;
  1334.  
  1335. // give points to the author of the PM
  1336. newpoints_addpoints($mybb->user['uid'], $mybb->settings['newpoints_income_pmsent'], 1, $grouprules['rate']);
  1337. }
  1338.  
  1339. // per rate
  1340. function newpoints_perrate()
  1341. {
  1342. global $db, $mybb, $fid;
  1343.  
  1344. if (!$mybb->user['uid'])
  1345. return;
  1346.  
  1347. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1348. return;
  1349.  
  1350. if ($mybb->settings['newpoints_income_perrate'] == 0)
  1351. return;
  1352.  
  1353. // check forum rules
  1354. $forumrules = newpoints_getrules('forum', $fid);
  1355. if (!$forumrules)
  1356. $forumrules['rate'] = 1; // no rule set so default income rate is 1
  1357.  
  1358. // if the forum rate is 0, nothing is going to be added so let's just leave the function
  1359. if ($forumrules['rate'] == 0)
  1360. return;
  1361.  
  1362. // check group rules - primary group check
  1363. $grouprules = newpoints_getrules('group', $mybb->user['usergroup']);
  1364. if (!$grouprules)
  1365. $grouprules['rate'] = 1; // no rule set so default income rate is 1
  1366.  
  1367. // if the group rate is 0, nothing is going to be added so let's just leave the function
  1368. if ($grouprules['rate'] == 0)
  1369. return;
  1370.  
  1371. // give points us, as we're rating a thread
  1372. newpoints_addpoints($mybb->user['uid'], $mybb->settings['newpoints_income_perrate'], $forumrules['rate'], $grouprules['rate']);
  1373. }
  1374.  
  1375. // page view / visit
  1376. function newpoints_perview()
  1377. {
  1378. global $db, $mybb, $cache, $groupscache, $userupdates;
  1379.  
  1380. if (!$mybb->user['uid'])
  1381. return;
  1382.  
  1383. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1384. return;
  1385.  
  1386. // check group rules - primary group check
  1387. $grouprules = newpoints_getallrules('group');
  1388. if (empty($grouprules))
  1389. return;
  1390.  
  1391. if ($mybb->settings['newpoints_income_pageview'] != 0)
  1392. {
  1393. newpoints_addpoints($mybb->user['uid'], $mybb->settings['newpoints_income_pageview'], 1, $grouprules[$mybb->user['usergroup']]['rate']);
  1394. }
  1395.  
  1396. if ($mybb->settings['newpoints_income_visit'] != 0)
  1397. {
  1398. if((TIME_NOW - $mybb->user['lastactive']) > 900)
  1399. newpoints_addpoints($mybb->user['uid'], $mybb->settings['newpoints_income_visit'], 1, $grouprules[$mybb->user['usergroup']]['rate']);
  1400. }
  1401.  
  1402. foreach($grouprules as $gid => $rule)
  1403. {
  1404. if ($rule['pointsearn'] == 0 || $rule['period'] == 0 || $rule['lastpay']>(TIME_NOW - $rule['period']))
  1405. return;
  1406.  
  1407. //die("testing".$rule['pointsearn']." | ".$rule['period']." | ".$rule['lastpay']." | ".TIME_NOW);
  1408.  
  1409. $amount = floatval($rule['pointsearn']);
  1410.  
  1411. $userupdates[$gid] = $amount;
  1412. // update rule with last payment
  1413. $db->update_query('newpoints_grouprules', array('lastpay' => TIME_NOW), 'gid=\''.$gid.'\'');
  1414.  
  1415. if($mybb->user['usergroup'] == $gid)
  1416. $mybb->user['newpoints'] += $amount;
  1417.  
  1418. if(!empty($userupdates))
  1419. {
  1420. // run updates to users on shut down
  1421. add_shutdown('newpoints_update_users');
  1422. }
  1423. }
  1424. }
  1425.  
  1426. // ****************** //
  1427. // FORUM RULES HOOKS START //
  1428. // ****************** //
  1429.  
  1430. function newpoints_blockview()
  1431. {
  1432. global $mybb, $lang, $fid;
  1433.  
  1434. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1435. return;
  1436.  
  1437. if (THIS_SCRIPT == 'forumdisplay.php')
  1438. $fid = intval($mybb->input['fid']);
  1439.  
  1440. $forumrules = newpoints_getrules('forum', $fid);
  1441. if ($forumrules['pointsview'] > $mybb->user['newpoints'])
  1442. {
  1443. $lang->load("newpoints");
  1444. error($lang->sprintf($lang->newpoints_not_enough_points, newpoints_format_points($forumrules['pointsview'])));
  1445. }
  1446. }
  1447.  
  1448. function newpoints_blockview_edit()
  1449. {
  1450. global $mybb, $lang;
  1451.  
  1452. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1453. return;
  1454.  
  1455. $pid = intval($mybb->input['pid']);
  1456. $post = get_post($pid);
  1457. if (!$post)
  1458. return;
  1459.  
  1460. $fid = $post['fid'];
  1461.  
  1462. $forumrules = newpoints_getrules('forum', $fid);
  1463. if ($forumrules['pointsview'] > $mybb->user['newpoints'])
  1464. {
  1465. $lang->load("newpoints");
  1466. error($lang->sprintf($lang->newpoints_not_enough_points, newpoints_format_points($forumrules['pointsview'])));
  1467. }
  1468. }
  1469.  
  1470. function newpoints_blockview_send()
  1471. {
  1472. global $mybb, $lang, $fid;
  1473.  
  1474. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1475. return;
  1476.  
  1477. $forumrules = newpoints_getrules('forum', $fid);
  1478. if ($forumrules['pointsview'] > $mybb->user['newpoints'])
  1479. {
  1480. $lang->load("newpoints");
  1481. error($lang->sprintf($lang->newpoints_not_enough_points, newpoints_format_points($forumrules['pointsview'])));
  1482. }
  1483. }
  1484.  
  1485. function newpoints_blockview_archive()
  1486. {
  1487. global $mybb, $lang, $forum;
  1488.  
  1489. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1490. return;
  1491.  
  1492. $fid = intval($forum['fid']);
  1493.  
  1494. $forumrules = newpoints_getrules('forum', $fid);
  1495. if ($forumrules['pointsview'] > $mybb->user['newpoints'])
  1496. {
  1497. $lang->load("newpoints");
  1498. error($lang->sprintf($lang->newpoints_not_enough_points, newpoints_format_points($forumrules['pointsview'])));
  1499. }
  1500. }
  1501.  
  1502. function newpoints_blockview_print()
  1503. {
  1504. global $mybb, $lang, $fid;
  1505.  
  1506. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1507. return;
  1508.  
  1509. $forumrules = newpoints_getrules('forum', $fid);
  1510. if ($forumrules['pointsview'] > $mybb->user['newpoints'])
  1511. {
  1512. $lang->load("newpoints");
  1513. error($lang->sprintf($lang->newpoints_not_enough_points, newpoints_format_points($forumrules['pointsview'])));
  1514. }
  1515. }
  1516.  
  1517. function newpoints_blockpost()
  1518. {
  1519. global $mybb, $lang, $fid;
  1520.  
  1521. if ($mybb->settings['newpoints_main_enabled'] != 1)
  1522. return;
  1523.  
  1524. $forumrules = newpoints_getrules('forum', $fid);
  1525. if ($forumrules['pointspost'] > $mybb->user['newpoints'])
  1526. {
  1527. $lang->load("newpoints");
  1528. error($lang->sprintf($lang->newpoints_not_enough_points, newpoints_format_points($forumrules['pointspost'])));
  1529. }
  1530. }
  1531. }
  1532.  
  1533. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement