Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3.  
  4. Plugin Thanks 3.9.1
  5. (c) 2008-2011 by Huji Lee, SaeedGh (SaeehGhMail@Gmail.com)
  6. Last edit: 11-26-2011
  7.  
  8. This program is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation, either version 3 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>.
  20.  
  21. */
  22.  
  23. if(!defined("IN_MYBB"))
  24. {
  25. die("Direct initialization of this file is not allowed.");
  26. }
  27.  
  28. if(isset($GLOBALS['templatelist']))
  29. {
  30. $GLOBALS['templatelist'] .= ", thanks_postbit_count";
  31. }
  32.  
  33. $plugins->add_hook("postbit", "thx");
  34. $plugins->add_hook("xmlhttp", "do_action");
  35. $plugins->add_hook("showthread_start", "direct_action");
  36. $plugins->add_hook("class_moderation_delete_post", "deletepost_edit");
  37. $plugins->add_hook('admin_tools_action_handler', 'thx_admin_action');
  38. $plugins->add_hook('admin_tools_menu', 'thx_admin_menu');
  39. $plugins->add_hook('admin_tools_permissions', 'thx_admin_permissions');
  40. $plugins->add_hook('admin_load', 'thx_admin');
  41.  
  42. function thx_info()
  43. {
  44. return array(
  45. 'name' => '<img border="0" src="../images/Thanks.gif" align="absbottom" alt="" /> Thanks',
  46. 'description' => 'Add a Thanks button to user posts.',
  47. 'website' => 'http://www.mybb.com',
  48. 'author' => 'Huji Lee, SaeedGh',
  49. 'authorsite' => 'mailto:SaeedGhMail@Gmail.com',
  50. 'version' => '3.9.1',
  51. 'guid' => 'd82cb3ceedd7eafa8954449cd02a449f',
  52. 'compatibility' => '14*,16*'
  53. );
  54. }
  55.  
  56.  
  57. function thx_install()
  58. {
  59. global $db;
  60.  
  61. $db->query("CREATE TABLE IF NOT EXISTS ".TABLE_PREFIX."thx (
  62. txid INT UNSIGNED NOT NULL AUTO_INCREMENT,
  63. uid int(10) UNSIGNED NOT NULL,
  64. adduid int(10) UNSIGNED NOT NULL,
  65. pid int(10) UNSIGNED NOT NULL,
  66. time bigint(30) NOT NULL DEFAULT '0',
  67. PRIMARY KEY (`txid`),
  68. INDEX (`adduid`, `pid`, `time`)
  69. );"
  70. );
  71.  
  72. if(!$db->field_exists("thx", "users"))
  73. {
  74. $sq[] = "ALTER TABLE ".TABLE_PREFIX."users ADD `thx` INT NOT NULL, ADD `thxcount` INT NOT NULL, ADD `thxpost` INT NOT NULL";
  75. }
  76. elseif (!$db->field_exists("thxpost", "users"))
  77. {
  78. $sq[] = "ALTER TABLE ".TABLE_PREFIX."users ADD `thxpost` INT NOT NULL";
  79. }
  80.  
  81. if($db->field_exists("thx", "posts"))
  82. {
  83. $sq[] = "ALTER TABLE ".TABLE_PREFIX."posts DROP thx";
  84. }
  85.  
  86. if(!$db->field_exists("pthx", "posts"))
  87. {
  88. $sq[] = "ALTER TABLE ".TABLE_PREFIX."posts ADD `pthx` INT(10) NOT NULL DEFAULT '0'";
  89. }
  90.  
  91. if(is_array($sq))
  92. {
  93. foreach($sq as $q)
  94. {
  95. $db->query($q);
  96. }
  97. }
  98. }
  99.  
  100.  
  101. function thx_is_installed()
  102. {
  103. global $db;
  104. if($db->field_exists('thxpost', "users"))
  105. {
  106. return true;
  107. }
  108. return false;
  109. }
  110.  
  111.  
  112. function thx_activate()
  113. {
  114. global $db;
  115.  
  116. //Update from v3.8
  117. $thx_tbl_keys = $db->query("SHOW KEYS FROM ".TABLE_PREFIX."thx WHERE Key_name='adduid'");
  118.  
  119. if(!$db->fetch_field($thx_tbl_keys, "Key_name"))
  120. {
  121. $db->query("ALTER TABLE ".TABLE_PREFIX."thx ADD INDEX (`adduid`, `pid`, `time`)");
  122. }
  123.  
  124. //Adding templates
  125. require MYBB_ROOT."inc/adminfunctions_templates.php";
  126.  
  127. if(!find_replace_templatesets("postbit", '#'.preg_quote('{$seperator}').'#', '{$post[\'thxdsp_inline\']}{$seperator}{$post[\'thxdsp_outline\']}'))
  128. {
  129. find_replace_templatesets("postbit", '#button_delete_pm(.*)<\/tr>(.*)<\/table>#is', 'button_delete_pm$1</tr>{\$post[\'thxdsp_inline\']}$2</table>{$post[\'thxdsp_outline\']}');
  130. }
  131. find_replace_templatesets("postbit", '#'.preg_quote('{$post[\'button_quote\']}').'#', '{$post[\'button_quote\']}{$post[\'thanks\']}');
  132. find_replace_templatesets("postbit_classic", '#button_delete_pm(.*)<\/tr>(.*)<\/table>#is', 'button_delete_pm$1</tr>{\$post[\'thxdsp_inline\']}$2</table>{$post[\'thxdsp_outline\']}');
  133. find_replace_templatesets("postbit_classic", '#'.preg_quote('{$post[\'button_quote\']}').'#', '{$post[\'button_quote\']}{$post[\'thanks\']}');
  134.  
  135. find_replace_templatesets("headerinclude", "#".preg_quote('{$newpmmsg}').'#',
  136. '<script type="text/javascript" src="jscripts/thx.js"></script>{$newpmmsg}');
  137.  
  138. $templatearray = array(
  139. 'title' => 'thanks_postbit_count',
  140. 'template' => "<div><span class=\"smalltext\">{\$lang->thx_thank} {\$post[\'thank_count\']}<br />
  141. {\$post[\'thanked_count\']}<br /></span></div>",
  142. 'sid' => '-1',
  143. );
  144. $db->insert_query("templates", $templatearray);
  145.  
  146. $templatearray = array(
  147. 'title' => 'thanks_postbit_inline',
  148. 'template' => "<tr id=\"thx{\$post[\'pid\']}\" style=\"{\$display_style}\" class=\"trow2 tnx_style tnx_newstl\"><td><span class=\"smalltext\">{\$lang->thx_givenby}</span>&nbsp;<span id=\"thx_list{\$post[\'pid\']}\">\$entries</span></td></tr>",
  149. 'sid' => '-1',
  150. );
  151. $db->insert_query("templates", $templatearray);
  152.  
  153. $templatearray = array(
  154. 'title' => 'thanks_postbit_inline_classic',
  155. 'template' => "<tr id=\"thx{\$post[\'pid\']}\" style=\"{\$display_style}\" class=\"trow2 tnx_style tnx_classic\"><td><span class=\"smalltext\">{\$lang->thx_givenby}</span></td><td class=\"trow2 tnx_style\" id=\"thx_list{\$post[\'pid\']}\">\$entries</td></tr>",
  156. 'sid' => '-1',
  157. );
  158. $db->insert_query("templates", $templatearray);
  159.  
  160. $templatearray = array(
  161. 'title' => 'thanks_postbit_outline',
  162. 'template' => "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" id=\"thx{\$post[\'pid\']}\" style=\"{\$display_style};margin-top:5px;\"><tr><td>
  163. <table border=\"0\" cellspacing=\"{\$theme[\'borderwidth\']}\" cellpadding=\"{\$theme[\'tablespace\']}\" class=\"tborder thxdsp_outline\"><tr class=\"trow1 tnx_style\"><td valign=\"top\" width=\"1%\" nowrap=\"nowrap\"><img src=\"{\$mybb->settings[\'bburl\']}/images/rose.gif\" align=\"absmiddle\" /> &nbsp;<span class=\"smalltext\">{\$lang->thx_givenby}</span></td><td class=\"trow2 tnx_style\" id=\"thx_list{\$post[\'pid\']}\">\$entries</td></tr></table>
  164. </td></tr></table>",
  165. 'sid' => '-1',
  166. );
  167. $db->insert_query("templates", $templatearray);
  168.  
  169. $thx_group = array(
  170. "name" => "Thanks",
  171. "title" => "Thanks",
  172. "description" => "Displays thank you note below each post.",
  173. "disporder" => "3",
  174. "isdefault" => "1"
  175. );
  176. $db->insert_query("settinggroups", $thx_group);
  177. $gid = $db->insert_id();
  178.  
  179. $thx[]= array(
  180. "name" => "thx_active",
  181. "title" => "Activate/Deactivate this plugin",
  182. "description" => "Activate or deactivate plugin but no delete table",
  183. "optionscode" => "onoff",
  184. "value" => '1',
  185. "disporder" => '1',
  186. "gid" => intval($gid),
  187. );
  188.  
  189. $thx[] = array(
  190. "name" => "thx_count",
  191. "title" => "Show count thanks",
  192. "description" => "Show count thanks in any post",
  193. "optionscode" => "onoff",
  194. "value" => '1',
  195. "disporder" => '2',
  196. "gid" => intval($gid),
  197. );
  198.  
  199. $thx[] = array(
  200. "name" => "thx_del",
  201. "title" => "Users can remove their thanks",
  202. "description" => "Every one can delete his thanks",
  203. "optionscode" => "onoff",
  204. "value" => '1',
  205. "disporder" => '3',
  206. "gid" => intval($gid),
  207. );
  208.  
  209. $thx[] = array(
  210. "name" => "thx_hidemode",
  211. "title" => "Show date on mouse over",
  212. "description" => "Show date of thanks just when mouse is over it",
  213. "optionscode" => "onoff",
  214. "value" => '1',
  215. "disporder" => '4',
  216. "gid" => intval($gid),
  217. );
  218.  
  219. $thx[] = array(
  220. "name" => "thx_autolayout",
  221. "title" => "Auto detect layout",
  222. "description" => "Detect postbit layout and try to correct related HTML code! (just works if \"Separate table\" is ON)",
  223. "optionscode" => "onoff",
  224. "value" => '1',
  225. "disporder" => '5',
  226. "gid" => intval($gid),
  227. );
  228.  
  229. $thx[] = array(
  230. "name" => "thx_outline",
  231. "title" => "Separate table",
  232. "description" => "If you want to show thanks between of tow post (not in end of a post), switch this option on.",
  233. "optionscode" => "onoff",
  234. "value" => '1',
  235. "disporder" => '6',
  236. "gid" => intval($gid),
  237. );
  238.  
  239. foreach($thx as $t)
  240. {
  241. $db->insert_query("settings", $t);
  242. }
  243.  
  244. rebuild_settings();
  245. }
  246.  
  247.  
  248. function thx_deactivate()
  249. {
  250. global $db;
  251. require '../inc/adminfunctions_templates.php';
  252.  
  253. find_replace_templatesets("postbit", '#'.preg_quote('{$post[\'thxdsp_inline\']}').'#', '', 0);
  254. find_replace_templatesets("postbit", '#'.preg_quote('{$post[\'thxdsp_outline\']}').'#', '', 0);
  255. find_replace_templatesets("postbit", '#'.preg_quote('{$post[\'thanks\']}').'#', '', 0);
  256. find_replace_templatesets("postbit_classic", '#'.preg_quote('{$post[\'thxdsp_inline\']}').'#', '', 0);
  257. find_replace_templatesets("postbit_classic", '#'.preg_quote('{$post[\'thxdsp_outline\']}').'#', '', 0);
  258. find_replace_templatesets("postbit_classic", '#'.preg_quote('{$post[\'thanks\']}').'#', '', 0);
  259. find_replace_templatesets("headerinclude", "#".preg_quote('<script type="text/javascript" src="jscripts/thx.js"></script>').'#', '', 0);
  260.  
  261. $db->delete_query("settings", "name IN ('thx_active', 'thx_count', 'thx_del', 'thx_hidemode', 'thx_autolayout', 'thx_outline')");
  262. $db->delete_query("settinggroups", "name='Thanks'");
  263. $db->delete_query("templates", "title='thanks_postbit_count'");
  264. $db->delete_query("templates", "title='thanks_postbit_inline'");
  265. $db->delete_query("templates", "title='thanks_postbit_inline_classic'");
  266. $db->delete_query("templates", "title='thanks_postbit_outline'");
  267.  
  268. rebuild_settings();
  269. }
  270.  
  271.  
  272. function thx_uninstall()
  273. {
  274. global $db;
  275.  
  276. /*$db->query("drop TABLE ".TABLE_PREFIX."thx");*/
  277.  
  278. if($db->field_exists("thx", "users"))
  279. {
  280. $db->query("ALTER TABLE ".TABLE_PREFIX."users DROP thx, DROP thxcount, DROP thxpost");
  281. }
  282.  
  283. if($db->field_exists("pthx", "posts"))
  284. {
  285. $db->query("ALTER TABLE ".TABLE_PREFIX."posts DROP pthx");
  286. }
  287. }
  288.  
  289.  
  290. function thx(&$post)
  291. {
  292. global $db, $mybb, $lang ,$session, $theme, $altbg, $templates, $thx_cache;
  293.  
  294. if(!$mybb->settings['thx_active'] || !empty($session->is_spider))
  295. {
  296. return false;
  297. }
  298.  
  299. $lang->load("thx");
  300.  
  301. if($b = $post['pthx'])
  302. {
  303. $entries = build_thank($post['pid'], $b);
  304. }
  305. else
  306. {
  307. $entries = "";
  308. }
  309.  
  310. if($mybb->user['uid'] != 0 && $mybb->user['uid'] != $post['uid'])
  311. {
  312. if(!$b)
  313. {
  314. $post['thanks'] = "<a id=\"a{$post['pid']}\" class=\"postbit_btn\" onclick=\"javascript:return thx({$post['pid']});\" href=\"showthread.php?action=thank&tid={$post['tid']}&pid={$post['pid']}\">
  315. <img id=\"i{$post['pid']}\" />Like</a>";
  316. }
  317. else if($mybb->settings['thx_del'] == "1")
  318. {
  319. $post['thanks'] = "<a id=\"a{$post['pid']}\" class=\"postbit_btn\" onclick=\"javascript:return rthx({$post['pid']});\" href=\"showthread.php?action=remove_thank&tid={$post['tid']}&pid={$post['pid']}\">
  320. <img id=\"i{$post['pid']}\" />Unlike</a>";
  321. }
  322. else
  323. {
  324. $post['thanks'] = "<!-- remove thanks disabled by administrator -->";
  325. }
  326. }
  327.  
  328. $display_style = $entries ? "" : "display:none; border:0;";
  329. $playout = $mybb->settings['postlayout'];
  330.  
  331. if(!$mybb->settings['thx_outline'])
  332. {
  333. eval("\$post['thxdsp_inline'] .= \"".$templates->get("thanks_postbit_inline")."\";");
  334.  
  335. if($mybb->settings['thx_autolayout'] && $playout == "classic")
  336. {
  337. eval("\$post['thxdsp_inline'] .= \"".$templates->get("thanks_postbit_inline_classic")."\";");
  338. }
  339. }
  340. else
  341. {
  342. eval("\$post['thxdsp_outline'] .= \"".$templates->get("thanks_postbit_outline")."\";");
  343. }
  344.  
  345. if($mybb->settings['thx_count'] == "1")
  346. {
  347. if(!isset($thx_cache['postbit'][$post['uid']]))
  348. {
  349. $post['thank_count'] = $post['thx'];
  350. $post['thanked_count'] = $lang->sprintf($lang->thx_thanked_count, $post['thxcount'], $post['thxpost']);
  351. eval("\$x = \"".$templates->get("thanks_postbit_count")."\";");
  352. $thx_cache['postbit'][$post['uid']] = $x;
  353. }
  354.  
  355. $post['user_details'] .= $thx_cache['postbit'][$post['uid']];
  356. }
  357. }
  358.  
  359. function do_action()
  360. {
  361. global $mybb, $lang, $theme;
  362.  
  363. if(($mybb->input['action'] != "thankyou" && $mybb->input['action'] != "remove_thankyou") || $mybb->request_method != "post")
  364. {
  365. return false;
  366. }
  367.  
  368. if(file_exists($lang->path."/".$lang->language."/thx.lang.php"))
  369. {
  370. $lang->load("thx");
  371. }
  372. else
  373. {
  374. $l = $lang->language;
  375. $lang->set_language();
  376. $lang->load("thx");
  377. $lang->set_language($l);
  378. }
  379.  
  380. $pid = intval($mybb->input['pid']);
  381.  
  382. if ($mybb->input['action'] == "thankyou" )
  383. {
  384. do_thank($pid);
  385. }
  386. else if($mybb->settings['thx_del'] == "1")
  387. {
  388. del_thank($pid);
  389. }
  390.  
  391. $nonead = 0;
  392. $list = build_thank($pid, $nonead);
  393. header('Content-Type: text/xml');
  394. $output = "<thankyou>
  395. <list><![CDATA[$list]]></list>
  396. <display>".($list ? "1" : "0")."</display>
  397. <image>{$mybb->settings['bburl']}/{$theme['imgdir']}/";
  398.  
  399. if($mybb->input['action'] == "thankyou")
  400. {
  401. $output .= "postbit_rthx.gif";
  402. }
  403. else
  404. {
  405. $output .= "postbit_thx.gif";
  406. }
  407.  
  408. $output .= "</image>
  409. <del>{$mybb->settings['thx_del']}</del>
  410. </thankyou>";
  411. echo $output;
  412. }
  413.  
  414. function direct_action()
  415. {
  416. global $mybb, $lang;
  417.  
  418. if($mybb->input['action'] != "thank" && $mybb->input['action'] != "remove_thank")
  419. {
  420. return false;
  421. }
  422.  
  423. if(file_exists($lang->path."/".$lang->language."/thx.lang.php"))
  424. {
  425. $lang->load("thx");
  426. }
  427. else
  428. {
  429. $l = $lang->language;
  430. $lang->set_language();
  431. $lang->load("thx");
  432. $lang->set_language($l);
  433. }
  434. $pid=intval($mybb->input['pid']);
  435.  
  436. if($mybb->input['action'] == "thank" )
  437. {
  438. do_thank($pid);
  439. }
  440. else if($mybb->settings['thx_del'] == "1")
  441. {
  442. del_thank($pid);
  443. }
  444. redirect($_SERVER['HTTP_REFERER']);
  445. }
  446.  
  447. function build_thank($pid, &$is_thx)
  448. {
  449. global $db, $mybb, $lang, $thx_cache;
  450. $is_thx = 0;
  451.  
  452. $pid = intval($pid);
  453.  
  454. if(file_exists($lang->path."/".$lang->language."/thx.lang.php"))
  455. {
  456. $lang->load("thx");
  457. }
  458. else
  459. {
  460. $l=$lang->language;
  461. $lang->set_language();
  462. $lang->load("thx");
  463. $lang->set_language($l);
  464. }
  465. $dir = $lang->thx_dir;
  466.  
  467. $query=$db->query("SELECT th.txid, th.uid, th.adduid, th.pid, th.time, u.username, u.usergroup, u.displaygroup
  468. FROM ".TABLE_PREFIX."thx th
  469. JOIN ".TABLE_PREFIX."users u
  470. ON th.adduid=u.uid
  471. WHERE th.pid='$pid'
  472. ORDER BY th.time ASC"
  473. );
  474.  
  475. while($record = $db->fetch_array($query))
  476. {
  477. if($record['adduid'] == $mybb->user['uid'])
  478. {
  479. $is_thx++;
  480. }
  481. $date = my_date($mybb->settings['dateformat'].' '.$mybb->settings['timeformat'], $record['time']);
  482. if(!isset($thx_cache['showname'][$record['username']]))
  483. {
  484. $url = get_profile_link($record['adduid']);
  485. $name = format_name($record['username'], $record['usergroup'], $record['displaygroup']);
  486. $thx_cache['showname'][$record['username']] = "<a href=\"$url\" dir=\"$dir\">$name</a>";
  487. }
  488.  
  489. if($mybb->settings['thx_hidemode'])
  490. {
  491. $entries .= $r1comma." <span title=\"".$date."\">".$thx_cache['showname'][$record['username']]."</span>";
  492. }
  493. else
  494. {
  495. $entries .= $r1comma.$thx_cache['showname'][$record['username']]." <span class=\"smalltext\">(".$date.")</span>";
  496. }
  497.  
  498. $r1comma = $lang->thx_comma;
  499. }
  500.  
  501. return $entries;
  502. }
  503.  
  504. function do_thank($pid)
  505. {
  506. global $db, $mybb;
  507.  
  508. $pid = intval($pid);
  509.  
  510. $check_query = $db->simple_select("thx", "count(*) as c" ,"adduid='{$mybb->user['uid']}' AND pid='$pid'", array("limit"=>"1"));
  511.  
  512. $tmp=$db->fetch_array($check_query);
  513. if($tmp['c'] != 0)
  514. {
  515. return false;
  516. }
  517.  
  518. $check_query = $db->simple_select("posts", "uid", "pid='$pid'", array("limit"=>1));
  519. if($db->num_rows($check_query) == 1)
  520. {
  521.  
  522. $tmp=$db->fetch_array($check_query);
  523.  
  524. if($tmp['uid'] == $mybb->user['uid'])
  525. {
  526. return false;
  527. }
  528.  
  529. $database = array (
  530. "uid" =>$tmp['uid'],
  531. "adduid" => $mybb->user['uid'],
  532. "pid" => $pid,
  533. "time" => time()
  534. );
  535.  
  536. unset($tmp);
  537.  
  538. $sq = array (
  539. "UPDATE ".TABLE_PREFIX."users SET thx=thx+1 WHERE uid='{$mybb->user['uid']}' LIMIT 1",
  540. "UPDATE ".TABLE_PREFIX."users SET thxcount=thxcount+1, thxpost=CASE( SELECT COUNT(*) FROM ".TABLE_PREFIX."thx WHERE pid='{$pid}' LIMIT 1) WHEN 0 THEN thxpost+1 ELSE thxpost END WHERE uid='{$database['uid']}' LIMIT 1",
  541. "UPDATE ".TABLE_PREFIX."posts SET pthx=pthx+1 WHERE pid='{$pid}' LIMIT 1"
  542. );
  543.  
  544. foreach($sq as $q)
  545. {
  546. $db->query($q);
  547. }
  548. $db->insert_query("thx", $database);
  549. }
  550. }
  551.  
  552. function del_thank($pid)
  553. {
  554. global $mybb, $db;
  555.  
  556. $pid = intval($pid);
  557. if($mybb->settings['thx_del'] != "1")
  558. {
  559. return false;
  560. }
  561.  
  562. $check_query = $db->simple_select("thx", "`uid`, `txid`" ,"adduid='{$mybb->user['uid']}' AND pid='$pid'", array("limit"=>"1"));
  563.  
  564. if($db->num_rows($check_query))
  565. {
  566. $data = $db->fetch_array($check_query);
  567. $uid = intval($data['uid']);
  568. $thxid = intval($data['txid']);
  569. unset($data);
  570.  
  571. $sq = array (
  572. "UPDATE ".TABLE_PREFIX."users SET thx=thx-1 WHERE uid='{$mybb->user['uid']}' LIMIT 1",
  573. "UPDATE ".TABLE_PREFIX."users SET thxcount=thxcount-1, thxpost=CASE(SELECT COUNT(*) FROM ".TABLE_PREFIX."thx WHERE pid='{$pid}' LIMIT 1) WHEN 0 THEN thxpost-1 ELSE thxpost END WHERE uid='{$uid}' LIMIT 1",
  574. "UPDATE ".TABLE_PREFIX."posts SET pthx=pthx-1 WHERE pid='{$pid}' LIMIT 1"
  575. );
  576.  
  577. $db->delete_query("thx", "txid='{$thxid}'", "1");
  578.  
  579. foreach($sq as $q)
  580. {
  581. $db->query($q);
  582. }
  583. }
  584. }
  585.  
  586. function deletepost_edit($pid)
  587. {
  588. global $db;
  589.  
  590. $pid = intval($pid);
  591. $q = $db->simple_select("thx", "uid, adduid", "pid='{$pid}'");
  592.  
  593. $postnum = $db->num_rows($q);
  594. if($postnum <= 0)
  595. {
  596. return false;
  597. }
  598.  
  599. $adduids = array();
  600.  
  601. while($r = $db->fetch_array($q))
  602. {
  603. $uid = intval($r['uid']);
  604. $adduids[] = $r['adduid'];
  605. }
  606.  
  607. $adduids = implode(", ", $adduids);
  608.  
  609. $sq = array();
  610. $sq[] = "UPDATE ".TABLE_PREFIX."users SET thxcount=thxcount-1, thxpost=thxpost-1 WHERE uid='{$uid}'";
  611. $sq[] = "UPDATE ".TABLE_PREFIX."users SET thx=thx-1 WHERE uid IN ({$adduids})";
  612.  
  613. foreach($sq as $q)
  614. {
  615. $db->query($q);
  616. }
  617.  
  618. $db->delete_query("thx", "pid={$pid}", $postnum);
  619.  
  620. }
  621.  
  622. function thx_admin_action(&$action)
  623. {
  624. $action['recount_thanks'] = array ('active'=>'recount_thanks');
  625. }
  626.  
  627. function thx_admin_menu(&$sub_menu)
  628. {
  629. $sub_menu['45'] = array (
  630. 'id' => 'recount_thanks',
  631. 'title' => 'Recount thanks',
  632. 'link' => 'index.php?module=tools/recount_thanks'
  633. );
  634. }
  635.  
  636. function thx_admin_permissions(&$admin_permissions)
  637. {
  638. $admin_permissions['recount_thanks'] = 'Can recount thanks';
  639. }
  640.  
  641. function thx_admin()
  642. {
  643. global $mybb, $page, $db;
  644. require_once MYBB_ROOT.'inc/functions_rebuild.php';
  645. if($page->active_action != 'recount_thanks')
  646. {
  647. return false;
  648. }
  649.  
  650. if($mybb->request_method == "post")
  651. {
  652. if(!isset($mybb->input['page']) || intval($mybb->input['page']) < 1)
  653. {
  654. $mybb->input['page'] = 1;
  655. }
  656. if(isset($mybb->input['do_recountthanks']))
  657. {
  658. if(!intval($mybb->input['thx_chunk_size']))
  659. {
  660. $mybb->input['thx_chunk_size'] = 500;
  661. }
  662.  
  663. do_recount();
  664. }
  665. else if(isset($mybb->input['do_recountposts']))
  666. {
  667. if(!intval($mybb->input['post_chunk_size']))
  668. {
  669. $mybb->input['post_chunk_size'] = 500;
  670. }
  671.  
  672. do_recount_post();
  673. }
  674. }
  675.  
  676. $page->add_breadcrumb_item('Recount thanks', "index.php?module=tools/recount_thanks");
  677. $page->output_header('Recount thanks');
  678.  
  679. $sub_tabs['thankyoulike_recount'] = array(
  680. 'title' => 'Recount thanks',
  681. 'link' => "index.php?module=tools/recount_thanks",
  682. 'description' => 'Update the thanks counters'
  683. );
  684.  
  685. $page->output_nav_tabs($sub_tabs, 'thankyoulike_recount');
  686.  
  687. $form = new Form("index.php?module=tools/recount_thanks", "post");
  688.  
  689. $form_container = new FormContainer('Recount thanks');
  690. $form_container->output_row_header('Name');
  691. $form_container->output_row_header('Chunk size', array('width' => 50));
  692. $form_container->output_row_header("&nbsp;");
  693.  
  694. $form_container->output_cell("<label>Update thanks counters</label>
  695. <div class=\"description\">Updates the counters for the number of thanks given/received by users and the number of thanks given to each post.</div>");
  696. $form_container->output_cell($form->generate_text_box("thx_chunk_size", 100, array('style' => 'width: 150px;')));
  697. $form_container->output_cell($form->generate_submit_button('Go', array("name" => "do_recountthanks")));
  698. $form_container->construct_row();
  699.  
  700. $form_container->output_cell("<label>Update post counters</label>
  701. <div class=\"description\">Updates the numer of posts in which a user has received thanks.</div>");
  702. $form_container->output_cell($form->generate_text_box("post_chunk_size", 500, array('style' => 'width: 150px;')));
  703. $form_container->output_cell($form->generate_submit_button('Go', array("name" => "do_recountposts")));
  704. $form_container->construct_row();
  705.  
  706. $form_container->end();
  707.  
  708. $form->end();
  709.  
  710. $page->output_footer();
  711.  
  712. exit;
  713. }
  714.  
  715. function do_recount()
  716. {
  717. global $db, $mybb;
  718.  
  719. $cur_page = intval($mybb->input['page']);
  720. $per_page = intval($mybb->input['thx_chunk_size']);
  721. $start = ($cur_page-1) * $per_page;
  722. $end = $start + $per_page;
  723.  
  724. if ($cur_page == 1)
  725. {
  726. $db->write_query("UPDATE ".TABLE_PREFIX."users SET thx='0', thxcount='0'");
  727. $db->write_query("UPDATE ".TABLE_PREFIX."posts SET pthx='0'");
  728. }
  729.  
  730. $query = $db->simple_select("thx", "COUNT(txid) AS thx_count");
  731. $thx_count = $db->fetch_field($query, 'thx_count');
  732.  
  733. $query = $db->query("
  734. SELECT uid, adduid, pid
  735. FROM ".TABLE_PREFIX."thx
  736. ORDER BY time ASC
  737. LIMIT $start, $per_page
  738. ");
  739.  
  740. $post_thx = array();
  741. $user_thx = array();
  742. $user_thx_to = array();
  743.  
  744. while($thx = $db->fetch_array($query))
  745. {
  746. if($post_thx[$thx['pid']])
  747. {
  748. $post_thx[$thx['pid']]++;
  749. }
  750. else
  751. {
  752. $post_thx[$thx['pid']] = 1;
  753. }
  754. if($user_thx[$thx['adduid']])
  755. {
  756. $user_thx[$thx['adduid']]++;
  757. }
  758. else
  759. {
  760. $user_thx[$thx['adduid']] = 1;
  761. }
  762. if($user_thx_to[$thx['uid']])
  763. {
  764. $user_thx_to[$thx['uid']]++;
  765. }
  766. else
  767. {
  768. $user_thx_to[$thx['uid']] = 1;
  769. }
  770. }
  771.  
  772. if(is_array($post_thx))
  773. {
  774. foreach($post_thx as $pid => $change)
  775. {
  776. $db->write_query("UPDATE ".TABLE_PREFIX."posts SET pthx=pthx+$change WHERE pid='$pid'");
  777. }
  778. }
  779. if(is_array($user_thx))
  780. {
  781. foreach($user_thx as $adduid => $change)
  782. {
  783. $db->write_query("UPDATE ".TABLE_PREFIX."users SET thx=thx+$change WHERE uid='$adduid'");
  784. }
  785. }
  786. if(is_array($user_thx_to))
  787. {
  788. foreach($user_thx_to as $uid => $change)
  789. {
  790. $db->write_query("UPDATE ".TABLE_PREFIX."users SET thxcount=thxcount+$change WHERE uid='$uid'");
  791. }
  792. }
  793. my_check_proceed($thx_count, $end, $cur_page+1, $per_page, "thx_chunk_size", "do_recountthanks", "Successfully updated the thanks counters");
  794. }
  795.  
  796. function do_recount_post()
  797. {
  798. global $db, $mybb;
  799.  
  800. $cur_page = intval($mybb->input['page']);
  801. $per_page = intval($mybb->input['post_chunk_size']);
  802. $start = ($cur_page-1) * $per_page;
  803. $end = $start + $per_page;
  804.  
  805. if ($cur_page == 1)
  806. {
  807. $db->write_query("UPDATE ".TABLE_PREFIX."users SET thxpost='0'");
  808. }
  809.  
  810. $query = $db->simple_select("thx", "COUNT(distinct pid) AS post_count");
  811. $post_count = $db->fetch_field($query, 'post_count');
  812.  
  813. $query = $db->query("
  814. SELECT uid, pid
  815. FROM ".TABLE_PREFIX."thx
  816. GROUP BY pid
  817. ORDER BY pid ASC
  818. LIMIT $start, $per_page
  819. ");
  820.  
  821. while($thx = $db->fetch_array($query))
  822. {
  823. $db->write_query("UPDATE ".TABLE_PREFIX."users SET thxpost=thxpost+1 WHERE uid='{$thx['uid']}'");
  824. }
  825.  
  826. my_check_proceed($post_count, $end, $cur_page+1, $per_page, "post_chunk_size", "do_recountposts", "Successfully updated the post counters");
  827. }
  828.  
  829. function my_check_proceed($current, $finish, $next_page, $per_page, $name_chunk, $name_submit, $message)
  830. {
  831. global $page;
  832.  
  833. if($finish >= $current)
  834. {
  835. flash_message($message, 'success');
  836. admin_redirect("index.php?module=tools/recount_thanks");
  837. }
  838. else
  839. {
  840. $page->output_header();
  841.  
  842. $form = new Form("index.php?module=tools/recount_thanks", 'post');
  843.  
  844. echo $form->generate_hidden_field("page", $next_page);
  845. echo $form->generate_hidden_field($name_chunk, $per_page);
  846. echo $form->generate_hidden_field($name_submit, "Go");
  847. echo "<div class=\"confirm_action\">\n";
  848. echo "<p>Click \"Proceed\" to continue the recount and rebuild process.</p>\n";
  849. echo "<br />\n";
  850. echo "<p class=\"buttons\">\n";
  851. echo $form->generate_submit_button("Proceed", array('class' => 'button_yes'));
  852. echo "</p>\n";
  853. echo "</div>\n";
  854.  
  855. $form->end();
  856.  
  857. $page->output_footer();
  858. exit;
  859. }
  860. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement