Guest User

tagging plugins for mybb 1.6.5

a guest
Nov 30th, 2011
328
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?PHP
  2.  
  3. function tagging_info()
  4. {
  5. return array(
  6. "name" => "Tagging Plugin!",
  7. "description" => "Allows you to tag other users in your posts",
  8. "author" => "flash.tato",
  9. "authorsite" => "http://www.tatodev.host22.com",
  10. "version" => "1.3.2",
  11. "compatibility" => "14*,16*",
  12. "guid" => "04eeda1eecb9cffea06f152f204be13f"
  13. );
  14. }
  15.  
  16. function tagging_is_installed()
  17. {
  18. global $db;
  19. return $db->field_exists("tags", "posts") && $db->field_exists("allowtags", "users");
  20. }
  21.  
  22. function tagging_install()
  23. {
  24. global $db;
  25. $db->query("ALTER TABLE `" . $db->table_prefix . "posts` ADD `tags` TEXT NOT NULL DEFAULT ''");
  26. $db->query("ALTER TABLE `" . $db->table_prefix . "users` ADD `allowtags` INT( 1 ) NOT NULL DEFAULT '" . TAG_ALL . "'");
  27. $db->insert_query("templates", array('sid' => '-1', 'dateline' => TIME_NOW, 'title' => 'usercp_tags', 'template' => $db->escape_string('<html>
  28. <head>
  29. <title>{$mybb->settings[\'bbname\']} - {$lang->tagging_usercp_settings}</title>
  30. {$headerinclude}
  31. </head>
  32. <body>
  33. {$header}
  34. <table width="100%" border="0" align="center">
  35. <tr>
  36. <form action="usercp.php?action=tags" method="POST">
  37. {$usercpnav}
  38. <td valign="top">
  39. <table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
  40. <tr>
  41. <td class="thead" align="center"><strong>{$lang->tagging_usercp_settings}</strong></td>
  42. </tr>
  43. <tr>
  44. <td class="trow1">
  45. {$error}
  46. <fieldset>
  47. <legend><strong>{$lang->tagging_who_can}</strong></legend>
  48. <input type="radio" name="allowtags" value="0" {$notagcheck}/> {$lang->tagging_usercp_noone}<br />
  49. <input type="radio" name="allowtags" value="1" {$allcheck}/> {$lang->tagging_usercp_all}<br />
  50. <input type="radio" name="allowtags" value="2" {$buddycheck}/> {$lang->tagging_usercp_buddy}
  51. </fieldset>
  52. <input type="submit" class="button" name="submit" value="{$lang->tagging_usercp_update_settings}" />
  53. </td>
  54. </tr>
  55. </table>
  56. </td>
  57. </tr>
  58. </table>
  59. </form>
  60. </body>
  61. </html>')));
  62. $setting_group = array(
  63. "gid" => "NULL",
  64. "title" => "Tagging Plugin! Settings",
  65. "name" => "tagging",
  66. "description" => "Manage every single aspect of the Tagging Plugin!",
  67. "disporder" => '4',
  68. "isdefault" => "0"
  69. );
  70. $db->insert_query("settinggroups", $setting_group);
  71. $gid = $db->insert_id();
  72. $setting_1 = array(
  73. 'name' => 'tagging_tag_show',
  74. 'title' => 'Tag appareance',
  75. 'description' => 'Here you can set how a tag appears (Variables you can use {profileurl}, {username})',
  76. 'optionscode' => 'text',
  77. 'value' => '<a href="{profileurl}">{username}</a>',
  78. 'disporder' => 1,
  79. 'gid' => $gid
  80. );
  81. $db->insert_query("settings", $setting_1);
  82. $setting_2 = array(
  83. 'name' => 'tagging_tag_pm_title',
  84. 'title' => 'PM Subject',
  85. 'description' => 'Write the subject of the PM that it is sent to the user',
  86. 'optionscode' => 'text',
  87. 'value' => $db->escape_string('You\'ve been tagged'),
  88. 'disporder' => 2,
  89. 'gid' => $gid
  90. );
  91. $db->insert_query("settings", $setting_2);
  92. $setting_3 = array(
  93. 'name' => 'tagging_tag_pm_message',
  94. 'title' => 'PM Message',
  95. 'description' => 'Write the PM Message which will be sent to the tagged user',
  96. 'optionscode' => 'textarea',
  97. 'value' => $db->escape_string('You\'ve been tagged in this [URL={bburl}{posturl}]post[/URL]'),
  98. 'disporder' => 3,
  99. 'gid' => $gid
  100. );
  101. $db->insert_query("settings", $setting_3);
  102. rebuild_settings();
  103. }
  104.  
  105. function tagging_uninstall()
  106. {
  107. global $db, $plugins;
  108. $db->query("ALTER TABLE `" . $db->table_prefix . "posts` DROP `tags`");
  109. $db->query("ALTER TABLE `" . $db->table_prefix . "users` DROP `allowtags`");
  110. $db->delete_query("templates", "title='usercp_tags'");
  111. $db->delete_query("settings", "name IN ('tagging_tag_show', 'tagging_tag_pm_title', 'tagging_tag_pm_message')");
  112. $db->delete_query("settinggroups", "name='tagging'");
  113. rebuild_settings();
  114. }
  115.  
  116. function tagging_activate()
  117. {
  118. require_once MYBB_ROOT . "inc/adminfunctions_templates.php";
  119. find_replace_templatesets('usercp_nav_misc', '#' . preg_quote('<tr><td class="trow1 smalltext"><a href="usercp.php?action=editlists" class="usercp_nav_item usercp_nav_editlists">{$lang->ucp_nav_editlists}</a></td></tr>') . '#si', '<tr><td class="trow1 smalltext"><a href="usercp.php?action=editlists" class="usercp_nav_item usercp_nav_editlists">{$lang->ucp_nav_editlists}</a></td></tr><tr><td class="trow1 smalltext"><a href="usercp.php?action=tags" class="usercp_nav_item usercp_nav_editlists">Tags</a></td></tr>');
  120.  
  121. }
  122.  
  123. function tagging_deactivate()
  124. {
  125. require_once MYBB_ROOT . "inc/adminfunctions_templates.php";
  126. find_replace_templatesets('usercp_nav_misc', '#' . preg_quote('<tr><td class="trow1 smalltext"><a href="usercp.php?action=tags" class="usercp_nav_item usercp_nav_editlists">Tags</a></td></tr>') . '#si', '', 0);
  127. }
  128.  
  129. //TAGGING API: an external plugin could include this file and then using these functions
  130.  
  131. function get_tags_from_message(&$message)
  132. {
  133. if(!preg_match_all("#@\[(.*?)\]#", $message, $matches))
  134. return array();
  135. return array_unique($matches[1]);
  136. }
  137.  
  138. function tagging_get_user(&$p)
  139. {
  140. global $db;
  141. $p = $db->escape_string($p);
  142. $query = $db->simple_select("users", "uid,username", "username='{$p}' OR uid='{$p}'");
  143. if($db->num_rows($query))
  144. return $db->fetch_array($query);
  145. }
  146.  
  147. define('NO_TAG', 0);
  148. define('TAG_ALL', 1);
  149. define('TAG_BUDDY', 2);
  150. function tagging_can_tag_user(&$fromid, &$uid)
  151. {
  152. global $db;
  153. $user = get_user($uid);
  154. switch($user['allowtags'])
  155. {
  156. case NO_TAG:
  157. return false;
  158. case TAG_ALL:
  159. return true;
  160. case TAG_BUDDY:
  161. $buddies = explode(",", $user['buddylist']);
  162. return in_array($fromid, $buddies);
  163. }
  164. }
  165.  
  166. function tagging_send_pm(&$to, &$from, &$subject, &$message)
  167. {
  168. require_once MYBB_ROOT."inc/datahandlers/pm.php";
  169.  
  170. $pmhandler = new PMDataHandler();
  171. $pm = array(
  172. "subject" => $subject,
  173. "message" => $message,
  174. "toid" => $to,
  175. "fromid" => $from
  176. );
  177. $pmhandler->set_data($pm);
  178. if(!$pmhandler->validate_pm())
  179. return false;
  180. else
  181. {
  182. $pmhandler->insert_pm();
  183. return true;
  184. }
  185. }
  186.  
  187. //TAGGING HELPER FUNCTIONS
  188.  
  189. function tagging_strip_post_quotes(&$message)
  190. {
  191. //Need to think a better approach for recognizing posts quotes
  192. return preg_replace("#\[quote(.*?)pid='([0-9]+)'(.*?)\](.*?)\[/quote\]#si", "", $message);
  193. }
  194.  
  195. function tagging_get_tag_text(&$uid, &$username)
  196. {
  197. global $mybb, $parser;
  198. $str_find = array('{profileurl}', '{username}', '{uid}');
  199. $str_replace = array(get_profile_link($uid), $username, $uid);
  200. return str_replace($str_find, $str_replace, $mybb->settings['tagging_tag_show']);
  201. }
  202.  
  203. function tagging_load_lang()
  204. {
  205. global $lang;
  206. if(file_exists(MYBB_ROOT . "inc/languages/" . $lang->language . "/tagging.lang.php") && $lang->language != "english")
  207. {
  208. $lang->load("tagging");
  209. }
  210. elseif(!isset($lang->tagging_usercp_noone))
  211. {
  212. $lang->tagging_usercp_noone = "No one";
  213. $lang->tagging_usercp_all = "Everyone";
  214. $lang->tagging_usercp_buddy = "Only your buddies";
  215. $lang->tagging_usercp_settings = "Tagging settings";
  216. $lang->tagging_usercp_update_settings = "Update your tagging settings";
  217. $lang->tagging_usercp_setting_invalid = "We're sorry but the setting is invalid, retry!";
  218. $lang->tagging_usercp_setting_updated = "You updated successfully your tagging settings";
  219. $lang->tagging_who_can = "Who can tag you?";
  220. }
  221. }
  222.  
  223. //TAGGING CORE
  224. $plugins->add_hook("datahandler_post_insert_thread_post", "tagging_on_insert_post");
  225. $plugins->add_hook("datahandler_post_insert_post", "tagging_on_insert_post");
  226. $plugins->add_hook("datahandler_post_update", "tagging_on_update_post");
  227. //Ok we can't send PMs via datahandler insert as we don't know the id of the post
  228. $plugins->add_hook("newthread_do_newthread_end", "tagging_on_insert_post_send_pm");
  229. $plugins->add_hook("newreply_do_newreply_end", "tagging_on_insert_post_send_pm");
  230.  
  231. function tagging_on_insert_post(&$datahandler)
  232. {
  233. global $mybb, $db, $plugins;
  234. $message = tagging_strip_post_quotes($datahandler->data['message']);
  235. $found_tags = get_tags_from_message($message);
  236. $invalid_keys = array($datahandler->data['uid'], $datahandler->data['username']);
  237. if(!count($found_tags))
  238. return;
  239. $found_tags = array_diff($found_tags, $invalid_keys);
  240. if(count($found_tags))
  241. {
  242. $tags = array();
  243. $uids = array();
  244. foreach($found_tags as $tag)
  245. {
  246. $user = tagging_get_user($tag);
  247. if(!in_array(intval($user['uid']), $uids))
  248. {
  249. if(tagging_can_tag_user($datahandler->data['uid'], $user['uid']))
  250. {
  251. $tags[] = array('uid' => $user['uid'], 'username' => $user['username']);
  252. $uids[] = $user['uid'];
  253. }
  254. }
  255. }
  256. if(count($uids))
  257. {
  258. $datahandler->post_insert_data['tags'] = $db->escape_string(serialize($tags));
  259. }
  260. }
  261. }
  262.  
  263. function tagging_on_insert_post_send_pm()
  264. {
  265. global $posthandler, $datahandler, $mybb, $plugins;
  266. $pid = $posthandler->pid;
  267. $tid = $posthandler->tid;
  268. if(!$tid)
  269. $tid = intval($mybb->input['tid']);
  270. $tags = unserialize(stripslashes($posthandler->post_insert_data['tags']));
  271. if(is_array($tags) && count($tags))
  272. {
  273. $uids = array();
  274. foreach($tags as $tag)
  275. {
  276. $uids[] = $tag['uid'];
  277. }
  278. tagging_send_pm($uids, $mybb->user['uid'], $mybb->settings['tagging_tag_pm_title'], str_replace(array("{bburl}", "{posturl}"), array($mybb->settings['bburl'] . "/", get_post_link($pid) . "#pid" . $pid), $mybb->settings['tagging_tag_pm_message']));
  279. $plugins->run_hooks("tagging_tag_added", array('pid' => $pid, 'tid' => $tid, 'users' => $uids));
  280. }
  281. }
  282.  
  283. //Updating post could mean less tags, more tags or none of both, basically the function checks:
  284. //1) If new tags are added
  285. //2) If some tags are removed
  286. function tagging_on_update_post(&$datahandler)
  287. {
  288. global $mybb, $post, $plugins, $db;
  289. if($post['uid'] != $mybb->user['uid'])
  290. return;
  291. $pid = $datahandler->data['pid'];
  292. $tid = $datahandler->data['tid'];
  293. $old_tags = unserialize($post['tags']);
  294. if(!is_array($old_tags))
  295. $old_tags = array();
  296. $old_tags_tag = array();
  297. $old_tags_uid = array();
  298. foreach($old_tags as $key => $t)
  299. {
  300. if(preg_match("#".preg_quote("@[{$t['username']}")."#", $post['message']))
  301. {
  302. $old_tags_tag[$key] = $t['username'];
  303. $old_tags_uid[$key] = $t['uid'];
  304. }
  305. if(preg_match("#".preg_quote("@[{$t['uid']}")."#", $post['message']))
  306. {
  307. $old_tags_tag[$key] = $t['uid'];
  308. $old_tags_uid[$key] = $t['uid'];
  309. }
  310. }
  311. $tags = get_tags_from_message(tagging_strip_post_quotes($datahandler->data['message']));
  312. $exclude = array($post['uid'], $post['username']);
  313. $u_tags = array_diff($tags, $exclude, $old_tags_tag);
  314. $n_tags = array();
  315. if(count($u_tags))
  316. {
  317. $uids = array();
  318. foreach($u_tags as $tag)
  319. {
  320. $user = tagging_get_user($tag);
  321. if(!in_array(intval($user['uid']), $uids))
  322. {
  323. if(tagging_can_tag_user($post['uid'], $user['uid']))
  324. {
  325. $uids[] = $user['uid'];
  326. $n_tags[] = array('uid' => $user['uid'], 'username' => $user['username']);
  327. }
  328. }
  329. }
  330. if(count($uids))
  331. {
  332. $plugins->run_hooks("tagging_tag_added", array('pid' => $pid, 'tid' => $tid, 'users' => $uids));
  333. tagging_send_pm($uids, $post['uid'], $mybb->settings['tagging_tag_pm_title'], str_replace(array("{bburl}", "{posturl}"), array($mybb->settings['bburl'] . "/", get_post_link($pid) . "#pid" . $pid), $mybb->settings['tagging_tag_pm_message']));
  334. }
  335. }
  336. $r_tags = array_diff($old_tags_tag, $tags);
  337. $r_uids = array();
  338. foreach($r_tags as $key => $tag)
  339. {
  340. $r_uids[] = $old_tags_uid[$key];
  341. unset($old_tags[$key]);
  342. }
  343. if(count($r_uids))
  344. {
  345. $plugins->run_hooks("tagging_tag_removed", array('pid' => $pid, 'tid' => $tid, 'users' => $r_uids));
  346. }
  347. $data = array_merge($old_tags, $n_tags);
  348. if(count($data))
  349. $datahandler->post_update_data['tags'] = $db->escape_string(serialize($data));
  350. else
  351. $datahandler->post_update_data['tags'] = "";
  352. }
  353.  
  354. //What if one or more posts are merged? We've to merge tags too!
  355. //For threads itsn't needed as the tags are a post's field
  356. $plugins->add_hook("moderation_start", "tagging_on_merge_posts");
  357.  
  358. function tagging_on_merge_posts()
  359. {
  360. global $db, $mybb, $plist, $plugins;
  361. if(THIS_SCRIPT == "moderation.php" && $mybb->input['action'] == "do_multimergeposts")
  362. {
  363. verify_post_check($mybb->input['my_post_key']);
  364. $mergepost = $mybb->input['mergepost'];
  365. $pids = array();
  366. foreach($mergepost as $pid => $yes)
  367. {
  368. $pids[] = intval($pid);
  369. }
  370. if(count($pids) <= 1)
  371. return;
  372. if(is_moderator_by_pids($pids, "canmanagethreads"))
  373. {
  374. $master_pid = $pids[0];
  375. $pidsin = "'" . implode("','", $pids) . "'";
  376. $query = $db->simple_select("posts", "tags", "pid IN ({$pidsin})");
  377. $uids = array();
  378. $tags = array();
  379. while($post = $db->fetch_array($query))
  380. {
  381. $t = unserialize($post['tags']);
  382. if(is_array($t) && count($t) > 0)
  383. {
  384. foreach($t as $tag)
  385. {
  386. if(!in_array($t['uid'], $uids))
  387. {
  388. $uids[] = $tag['uid'];
  389. $tags[] = $tag;
  390. }
  391. }
  392. }
  393. }
  394. if(count($tags))
  395. {
  396. $db->update_query("posts", array('tags' => $db->escape_string(serialize($tags))), "pid='{$master_pid}'");
  397. $post = get_post($master_pid);
  398. $plugins->run_hooks("tagging_tag_merged", array('pid' => $master_pid, 'tid' => $post['tid'], 'users' => $uids));
  399. }
  400. }
  401. }
  402. }
  403.  
  404. //Hook threads deletion and post deletion so i can launch tagging_tag_removed hook!
  405. $plugins->add_hook("class_moderation_delete_thread_start", "tagging_on_delete_thread");
  406. $plugins->add_hook("class_moderation_delete_post_start", "tagging_on_delete_post");
  407.  
  408. function tagging_on_delete_thread(&$tid)
  409. {
  410. global $db, $plugins;
  411. $tid = intval($tid);
  412. $query = $db->simple_select("posts", "pid,tags", "tid='{$tid}'");
  413. if($db->num_rows($query))
  414. {
  415. while($post = $db->fetch_array($query))
  416. {
  417. $tags = unserialize($post['tags']);
  418. if(is_array($tags) && count($tags))
  419. {
  420. $uids = array();
  421. foreach($tags as $tag)
  422. $uids[] = intval($tag['uid']);
  423. $plugins->run_hooks("tagging_tag_removed", array('pid' => $post['pid'], 'tid' => $tid, 'users' => $uids));
  424. }
  425. }
  426. }
  427. }
  428.  
  429. function tagging_on_delete_post(&$pid)
  430. {
  431. global $db, $plugins;
  432. $pid = intval($pid);
  433. $query = $db->simple_select("posts", "tid,tags", "pid='{$pid}'");
  434. if($db->num_rows($query))
  435. {
  436. $post = $db->fetch_array($query);
  437. $tags = unserialize($post['tags']);
  438. if(is_array($tags) && count($tags))
  439. {
  440. $uids = array();
  441. foreach($tags as $tag)
  442. $uids[] = intval($tag['uid']);
  443. $plugins->run_hooks("tagging_tag_removed", array('pid' => $pid, 'tid' => $post['tid'], 'users' => $uids));
  444. }
  445. }
  446. }
  447.  
  448. //TAGGING PARSING POSTS
  449. $plugins->add_hook("postbit", "tagging_show_post"); //If you wonder why i don't use parse_message hook is because i want to be sure that the plugin works only on posts!
  450. $plugins->add_hook("archive_thread_post", "tagging_show_post_archive");
  451.  
  452. function tagging_parse(&$message, &$tags)
  453. {
  454. if(!$tags) return $message;
  455. if(is_string($tags))
  456. $tags = unserialize($tags);
  457. if(is_array($tags) && count($tags) > 0)
  458. {
  459. $replacement = array();
  460. foreach($tags as $tag)
  461. {
  462. $replacements["@[{$tag['uid']}]"] = tagging_get_tag_text($tag['uid'], $tag['username']);
  463. $replacements["@[{$tag['username']}]"] = tagging_get_tag_text($tag['uid'], $tag['username']);
  464. }
  465. return strtr($message, $replacements);
  466. }
  467. return $message;
  468. }
  469.  
  470. function tagging_show_post(&$post)
  471. {
  472. $post['message'] = tagging_parse($post['message'], $post['tags']);
  473. }
  474.  
  475. function tagging_show_post_archive()
  476. {
  477. global $post;
  478. $post['message'] = tagging_parse($post['message'], $post['tags']);
  479. }
  480.  
  481. /*
  482. * Little trick for supporting AJAX Editing, the problem is that postbit hook is not called as it hasn't to return the postbit itself so what do we do?
  483. * We intercept parse_message event, to be sure that it works after post insertion we add the hook only after that post is inserted
  484. */
  485. global $mybb;
  486. if(THIS_SCRIPT == "xmlhttp.php" && $mybb->input['do'] == "update_post")
  487. {
  488. $plugins->add_hook("datahandler_post_update", "tagging_trick_first");
  489.  
  490. function tagging_trick_first(&$datahandler)
  491. {
  492. global $plugins;
  493. $plugins->add_hook("parse_message", "tagging_trick_second");
  494. }
  495.  
  496. function tagging_trick_second(&$message)
  497. {
  498. global $post, $mybb, $db, $post, $plugins;
  499. $query = $db->simple_select("posts", "tags", "pid='{$post['pid']}'");
  500. $result = $db->fetch_array($query);
  501. $message = tagging_parse($message, $result['tags']);
  502. return $message;
  503. }
  504.  
  505. }
  506.  
  507. //We want to display tags also in search results
  508. $plugins->add_hook("search_results_post", "tagging_search_result_post");
  509.  
  510. function tagging_search_result_post()
  511. {
  512. global $prev, $post;
  513. $prev = tagging_parse($prev, $post['tags']);
  514. }
  515.  
  516. //UserCP Settings for tagging
  517.  
  518. $plugins->add_hook("usercp_start", "tagging_usercp");
  519.  
  520. function tagging_usercp()
  521. {
  522. extract($GLOBALS); //Little trick for getting all variables in global scope, useful for templates
  523. if($mybb->input['action'] == "tags")
  524. {
  525. tagging_load_lang();
  526. if(isset($mybb->input['submit']))
  527. {
  528. $val = intval($mybb->input['allowtags']);
  529. if(is_numeric($mybb->input['allowtags']) && ($val > -1 && $val < 3))
  530. {
  531. $db->update_query("users", array('allowtags' => $db->escape_string($mybb->input['allowtags'])), "uid='" . $mybb->user['uid'] . "'");
  532. $mybb->user['allowtags'] = $mybb->input['allowtags'];
  533. redirect("usercp.php", $lang->tagging_usercp_setting_updated);
  534. }
  535. else
  536. {
  537. $error = inline_error($lang->tagging_usercp_setting_invalid);
  538. }
  539. }
  540. $c = 'checked="checked" ';
  541. switch($mybb->user['allowtags'])
  542. {
  543. case 0:
  544. $notagcheck = $c;
  545. break;
  546. case 1:
  547. $allcheck = $c;
  548. break;
  549. case 2:
  550. $buddycheck = $c;
  551. break;
  552. }
  553. eval("\$page = \"".$templates->get("usercp_tags")."\";");
  554. output_page($page);
  555. }
  556. }
  557.  
  558. ?>
  559.  
RAW Paste Data