PageRenderTime 67ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/simple-forum/install/sf-upgrade-support.php

https://bitbucket.org/openfarmtech/weblog-content
PHP | 1594 lines | 1428 code | 94 blank | 72 comment | 134 complexity | 261072bd71a9f68f614061758a7ee163 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.0, LGPL-3.0, BSD-3-Clause, GPL-3.0, LGPL-2.1, AGPL-3.0, CC-BY-SA-3.0
  1. <?php
  2. /*
  3. Simple:Press
  4. Install & Upgrade Support Routines
  5. $LastChangedDate: 2010-05-13 19:54:56 -0700 (Thu, 13 May 2010) $
  6. $Rev: 4018 $
  7. */
  8. if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
  9. die('Access Denied');
  10. }
  11. include_once(SF_PLUGIN_DIR.'/library/sf-common-functions.php');
  12. # ==========================================
  13. #
  14. # GLOBAL UPDATE/INSTALL ROUTINES
  15. #
  16. # ==========================================
  17. # Called from V4.1 onwards to log uodates.
  18. function sf_log_event($release, $version, $build)
  19. {
  20. global $wpdb, $current_user;
  21. $now = current_time('mysql');
  22. $sql = "
  23. INSERT INTO ".SFLOG." (user_id, install_date, release_type, version, build)
  24. VALUES (
  25. ".$current_user->ID.",
  26. '".$now."',
  27. '".$release."',
  28. '".$version."',
  29. ".$build.");";
  30. $wpdb->query($sql);
  31. sf_update_option('sfversion', $version);
  32. sf_update_option('sfbuild', $build);
  33. return;
  34. }
  35. # Upgrade Database wrapper
  36. function sf_upgrade_database($table_name, $column_name, $create_ddl)
  37. {
  38. global $wpdb;
  39. foreach ($wpdb->get_col("DESC $table_name", 0) as $column )
  40. {
  41. if ($column == $column_name)
  42. {
  43. return true;
  44. }
  45. }
  46. # didn't find it try to create it.
  47. $q = $wpdb->query($create_ddl);
  48. # we cannot directly tell that whether this succeeded!
  49. foreach ($wpdb->get_col("DESC $table_name", 0) as $column )
  50. {
  51. if ($column == $column_name)
  52. {
  53. return true;
  54. }
  55. }
  56. die(sprintf(__("DATABASE ERROR: Unable to ALTER the %s to create new column %s", "sforum"), $table_name, $column));
  57. }
  58. function sf_charset()
  59. {
  60. global $wpdb;
  61. $charset='';
  62. if ( ! empty($wpdb->charset) )
  63. {
  64. $charset = "DEFAULT CHARSET $wpdb->charset";
  65. } else {
  66. $charset = "DEFAULT CHARSET utf8";
  67. }
  68. return $charset;
  69. }
  70. # ==========================================
  71. #
  72. # VERSION SPECIFIC UPDATE/INSTALL ROUTINES
  73. #
  74. # ==========================================
  75. # Called by 1.6 to clear up previous deletion orphans
  76. function sf_check_data_integrity()
  77. {
  78. global $wpdb;
  79. $topiclist = array();
  80. $postlist = array();
  81. # to be run against a 1.5 install to clean up orphaned posts
  82. # Step 1: Loop through topics in case forum is gone and remove
  83. $topics = $wpdb->get_results("SELECT topic_id, forum_id FROM ".SFTOPICS);
  84. if($topics)
  85. {
  86. foreach($topics as $topic)
  87. {
  88. $test=$wpdb->get_col("SELECT forum_id FROM ".SFFORUMS." WHERE forum_id=".$topic->forum_id);
  89. if(!$test)
  90. {
  91. $topiclist[]=$topic->topic_id;
  92. }
  93. }
  94. if($topiclist)
  95. {
  96. foreach($topiclist as $topic)
  97. {
  98. $wpdb->query("DELETE FROM ".SFTOPICS." WHERE topic_id=".$topic);
  99. }
  100. }
  101. }
  102. # Step 2: Loop through posts in case topic is gone and remove
  103. $posts = $wpdb->get_results("SELECT post_id, topic_id FROM ".SFPOSTS);
  104. if($posts)
  105. {
  106. foreach($posts as $post)
  107. {
  108. $test=$wpdb->get_col("SELECT topic_id FROM ".SFTOPICS." WHERE topic_id=".$post->topic_id);
  109. if(!$test)
  110. {
  111. $postlist[]=$post->post_id;
  112. }
  113. }
  114. if($postlist)
  115. {
  116. foreach($postlist as $post)
  117. {
  118. $wpdb->query("DELETE FROM ".SFPOSTS." WHERE post_id=".$post);
  119. }
  120. }
  121. }
  122. return;
  123. }
  124. # Called by 1.7 to re-route subscriptions from usermeta to topics
  125. function sf_rebuild_subscriptions()
  126. {
  127. global $wpdb;
  128. # Build a list of users with subscribe set
  129. $users = $wpdb->get_col("SELECT user_id FROM ".SFUSERMETA." WHERE meta_key='".$wpdb->prefix."sfsubscribe'");
  130. if($users)
  131. {
  132. # clear out the old sfsubcribe values ready for the new
  133. $wpdb->query("DELETE FROM ".SFUSERMETA." WHERE meta_key='".$wpdb->prefix."sfsubscribe'");
  134. foreach($users as $user)
  135. {
  136. # now build the list of topics into which each user has posted
  137. $topics = $wpdb->get_col("SELECT DISTINCT topic_id FROM ".SFPOSTS." WHERE user_id=".$user);
  138. if($topics)
  139. {
  140. foreach($topics as $topic)
  141. {
  142. sf_save_subscription($topic, $user, false);
  143. }
  144. }
  145. }
  146. }
  147. return;
  148. }
  149. # Called by 2.0 to clean up the topic subs lists where duplicates have crept in
  150. function sf_clean_topic_subs()
  151. {
  152. global $wpdb;
  153. # build list of topics with subscriptions
  154. $topics = $wpdb->get_results("SELECT topic_id, topic_subs FROM ".SFTOPICS." WHERE topic_subs IS NOT NULL;");
  155. if(!$topics) return;
  156. foreach($topics as $topic)
  157. {
  158. $nvalues = array();
  159. $cvalues = explode('@', $topic->topic_subs);
  160. $nvalues[0] = $cvalues[0];
  161. foreach($cvalues as $cvalue)
  162. {
  163. $notfound = true;
  164. foreach($nvalues as $nvalue)
  165. {
  166. if($nvalue == $cvalue) $notfound = false;
  167. }
  168. if($notfound) $nvalues[]=$cvalue;
  169. }
  170. $nvaluelist = implode('@', $nvalues);
  171. $wpdb->query("UPDATE ".SFTOPICS." SET topic_subs='".$nvaluelist."' WHERE topic_id=".$topic->topic_id);
  172. }
  173. return;
  174. }
  175. function sf_relocate_avatars()
  176. {
  177. global $wpdb;
  178. $basepath='/';
  179. if (is_multisite()) $basepath = '/blogs.dir/' . $wpdb->blogid .'/files/';
  180. $success = 0;
  181. $newpath = SF_STORE_DIR . $basepath . 'forum-avatars';
  182. $oldpath = SF_PLUGIN_DIR . '/styles/avatars';
  183. # check if new folder does not exist - which it shouldn't!
  184. if(!is_dir($newpath))
  185. {
  186. if(!is_writable(SF_STORE_DIR) || !($dir = @mkdir($newpath, 0777)))
  187. {
  188. $success = 1;
  189. return $success;
  190. }
  191. if (!is_writable($newpath))
  192. {
  193. $success = 2;
  194. return $success;
  195. }
  196. if(is_dir($newpath))
  197. {
  198. $avlist = opendir($oldpath);
  199. while (false !== ($file = readdir($avlist)))
  200. {
  201. if (is_file($oldpath.'/'.$file) && $file != "." && $file != "..")
  202. {
  203. if(!file_exists($newpath.'/'.$file))
  204. {
  205. if(@copy($oldpath.'/'.$file, $newpath.'/'.$file) == false)
  206. {
  207. $success = 3;
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. closedir($avlist);
  214. }
  215. }
  216. return $success;
  217. }
  218. # Called by 2.1 to correct old timestamp in usermeta (sflast)
  219. function sf_correct_sflast()
  220. {
  221. global $wpdb;
  222. $sql = "UPDATE ".SFUSERMETA." SET meta_value=now() WHERE meta_key = '".$wpdb->prefix."sflast' AND meta_value < DATE_SUB(CURDATE(), INTERVAL 1 YEAR);";
  223. $wpdb->query($sql);
  224. return;
  225. }
  226. # Called by 2.1 Patch 2 to pre-create last visited date for all existing users who don't have one - Corrects the zero problem
  227. function sf_precreate_sflast()
  228. {
  229. global $wpdb;
  230. $users = $wpdb->get_results("SELECT ID FROM ".SFUSERS);
  231. if($users)
  232. {
  233. foreach($users as $user)
  234. {
  235. $check = $wpdb->get_var("SELECT umeta_id FROM ".SFUSERMETA." WHERE meta_key='".$wpdb->prefix."sflast' AND user_id=".$user->ID);
  236. if(!$check)
  237. {
  238. sf_set_last_visited($user->ID);
  239. }
  240. }
  241. }
  242. return;
  243. }
  244. # Called by 3.0 to create forum and topic slugs
  245. function sf_create_slugs()
  246. {
  247. global $wpdb;
  248. # forums
  249. $records=$wpdb->get_results("SELECT forum_id, forum_name, forum_slug FROM ".SFFORUMS);
  250. if($records)
  251. {
  252. foreach($records as $record)
  253. {
  254. $title = sf_create_slug($record->forum_name, 'forum');
  255. if(empty($title))
  256. {
  257. $title = 'forum-'.$record->forum_id;
  258. }
  259. $wpdb->query("UPDATE ".SFFORUMS." SET forum_slug='".$title."' WHERE forum_id=".$record->forum_id);
  260. }
  261. }
  262. # topics
  263. $records=$wpdb->get_results("SELECT topic_id, topic_name, topic_slug FROM ".SFTOPICS);
  264. if($records)
  265. {
  266. foreach($records as $record)
  267. {
  268. $title = sf_create_slug($record->topic_name, 'topic');
  269. if(empty($title))
  270. {
  271. $title = 'topic-'.$record->topic_id;
  272. }
  273. $wpdb->query("UPDATE ".SFTOPICS." SET topic_slug='".$title."' WHERE topic_id=".$record->topic_id);
  274. }
  275. }
  276. return;
  277. }
  278. # Called by 3 to ensure all users have a display name set
  279. function sf_check_all_display_names()
  280. {
  281. global $wpdb;
  282. $users = $wpdb->get_results("SELECT ID, user_login, display_name FROM ".SFUSERS." WHERE display_name=''");
  283. if($users)
  284. {
  285. foreach($users as $user)
  286. {
  287. $wpdb->query("UPDATE ".SFUSERS." SET display_name='".$user->login_name."' WHERE ID=".$user->ID);
  288. }
  289. }
  290. return;
  291. }
  292. # Called by 3.0 to set up all users into default usergroups
  293. # And then set all 3 usergroups to all forums by default
  294. function sf_setup_usergroup_data($membergroup, $moderatorgroup, $upgrade, $keys)
  295. {
  296. global $wpdb, $current_user;
  297. # if upgrade check if any moderators
  298. $modusers = '';
  299. if ($upgrade) $modusers = get_option('sfmodusers');
  300. if (!empty($modusers))
  301. {
  302. $modusers = explode(';', get_option('sfmodusers'));
  303. }
  304. # get the list of users and do the stuff
  305. $userlist = $wpdb->get_results("SELECT ID FROM ".SFUSERS." ORDER BY display_name ASC;");
  306. if($userlist)
  307. {
  308. foreach($userlist as $user)
  309. {
  310. # check it's not the admin
  311. if($user->ID != $current_user->ID)
  312. {
  313. $target = $membergroup;
  314. # is user a moderator?
  315. if(!empty($modusers))
  316. {
  317. if(in_array($user->ID, $modusers)) $target = $moderatorgroup;
  318. }
  319. $memberships = get_user_meta($user->ID, 'sfusergroup', true);
  320. $memberships[] = $target;
  321. update_user_meta($user->ID, 'sfusergroup', $memberships);
  322. }
  323. }
  324. }
  325. # Now to assign all 3 default usergroups to all forums
  326. if(($keys) && ($upgrade))
  327. {
  328. $forums = $wpdb->get_results("SELECT forum_id FROM ".SFFORUMS.";");
  329. if($forums)
  330. {
  331. foreach($forums as $forum)
  332. {
  333. for($x=0; $x<count($keys); $x++)
  334. {
  335. $group = $keys[$x]['usergroup'];
  336. $perm = $keys[$x]['permission'];
  337. $sql ="INSERT INTO ".SFPERMISSIONS." (forum_id, usergroup_id, permission_role) ";
  338. $sql.="VALUES (".$forum->forum_id.", ".$group.", ".$perm.");";
  339. $wpdb->query($sql);
  340. }
  341. }
  342. }
  343. }
  344. return;
  345. }
  346. # called by 3.1 (?) to build new last post columns, topic post count and post index
  347. function sf_build_lastposts()
  348. {
  349. global $wpdb;
  350. $forums = sf_get_forums_all(true);
  351. if($forums)
  352. {
  353. foreach($forums as $forum)
  354. {
  355. sf_build_forum_index($forum->forum_id);
  356. }
  357. }
  358. $topics = sf_get_topics_all(true);
  359. if($topics)
  360. {
  361. foreach($topics as $topic)
  362. {
  363. sf_build_post_index($topic->topic_id, $topic->topic_slug);
  364. }
  365. }
  366. return;
  367. }
  368. # called by 3.1 upgrade to build members table
  369. # should also now work correctly for individual network sites
  370. # sends $editor_column as install and upgrade have different column names (don't ask!)
  371. function sf_build_members_table($editor_column, $type, $subphase=0)
  372. {
  373. global $wpdb, $current_user;
  374. # get limits for installs
  375. if ($subphase != 0)
  376. {
  377. $limit = " LIMIT 250 OFFSET ".(($subphase - 1) * 250);
  378. }
  379. # select all users
  380. $sql =
  381. "SELECT ID, display_name, user_login FROM ".SFUSERMETA."
  382. RIGHT JOIN ".SFUSERMETA." ON ".SFUSERMETA.".ID = ".SFUSERMETA.".user_id
  383. WHERE meta_key = '".$wpdb->prefix."capabilities'
  384. ORDER BY ID".$limit;
  385. $members = $wpdb->get_results($sql);
  386. if($members)
  387. {
  388. # grab the user groups so we can ensure the users settings are coprrect and groups exist
  389. $ugs = $wpdb->get_col("SELECT usergroup_id FROM ".SFUSERGROUPS);
  390. foreach($members as $member)
  391. {
  392. # Check ID exists and is not zero
  393. if(is_numeric($member->ID) && $member->ID > 0)
  394. {
  395. $usergroups = array();
  396. $usergroups = get_user_meta($member->ID, 'sfusergroup', true);
  397. # user group handling - check groups exist
  398. $newgrouplist=array();
  399. if($usergroups)
  400. {
  401. foreach($usergroups as $group)
  402. {
  403. if(in_array($group, $ugs))
  404. {
  405. $newgrouplist[] = (string) $group;
  406. }
  407. }
  408. } else {
  409. $newgrouplist[] = (string) get_option('sfdefgroup');
  410. }
  411. $usergroups = serialize($newgrouplist);
  412. # admins dont get user groups
  413. # forum admin not set up yet for installs
  414. if ($type == 'upgrade')
  415. {
  416. if (sf_is_forum_admin($member->ID)) $usergroups = '';
  417. } else {
  418. if ($current_user->ID == $member->ID) $usergroups = '';
  419. }
  420. # remaining data items
  421. $display_name = sf_filter_name_save($member->display_name);
  422. if(empty($display_name))
  423. {
  424. $display_name = sf_filter_name_save($member->user_login);
  425. }
  426. $display_name = sf_filter_name_save($display_name);
  427. $buddies = array();
  428. $avatar = esc_sql(get_user_meta($member->ID, 'sfavatar', true));
  429. $signature = esc_sql(get_user_meta($member->ID, 'signature', true));
  430. $sigimage = esc_sql(get_user_meta($member->ID, 'sigimage', true));
  431. $posts = get_user_meta($member->ID, 'sfposts', true);
  432. $lastvisit = get_user_meta($member->ID, 'sflast', true);
  433. $subscribe = get_user_meta($member->ID, 'sfsubscribe', true);
  434. $buddies = get_user_meta($member->ID, 'sfbuddies', true);
  435. $pm = sf_get_user_pm_status($member->ID, $newgrouplist);
  436. $moderator = sf_get_user_mod_status($member->ID, $newgrouplist);
  437. $signature = sf_filter_text_save(trim($signature));
  438. $sigimage = sf_filter_title_save(trim($sigimage));
  439. $buddies = serialize($buddies);
  440. if(!$posts) $posts = '0';
  441. $editor_setting = get_user_meta($member->ID, 'sfuse_quicktags', true);
  442. if(empty($editor_setting))
  443. {
  444. if($editor_column == 'quicktags') $editor_setting = 0;
  445. if($editor_column == 'editor') $editor_setting = 1;
  446. }
  447. if ($type == 'upgrade')
  448. {
  449. $sql ="INSERT INTO ".SFMEMBERS." (user_id, display_name, pm, moderator, {$editor_column}, usergroups, avatar, signature, sigimage, posts, lastvisit, subscribe, buddies) ";
  450. $sql.="VALUES ({$member->ID}, '{$display_name}', {$pm}, {$moderator}, {$editor_setting}, '{$usergroups}', '{$avatar}', '{$signature}', '{$sigimage}', {$posts}, '{$lastvisit}', '{$subscribe}', '{$buddies}');";
  451. } else {
  452. $sql ="INSERT INTO ".SFMEMBERS." (user_id, display_name, pm, moderator, avatar, signature, sigimage, posts, lastvisit, subscribe, buddies, newposts, checktime, admin, watches, posts_rated, admin_options) ";
  453. $sql.="VALUES ({$member->ID}, '{$display_name}', {$pm}, {$moderator}, '{$avatar}', '{$signature}', '{$sigimage}', {$posts}, now(), '{$subscribe}', '{$buddies}', '', now(), 0, '', '', '');";
  454. $memberships = unserialize($usergroups);
  455. if ($memberships) # will be empty for admin
  456. {
  457. foreach ($memberships as $membership)
  458. {
  459. sfc_add_membership($membership, $member->ID);
  460. }
  461. }
  462. $useroptions = array();
  463. $useroptions['pmemail'] = true;
  464. $useroptions['editor'] = $editor_setting;
  465. sf_update_member_item($member->ID, 'user_options', $useroptions);
  466. }
  467. $wpdb->query($sql);
  468. # now remove the old userfmeta entries for the current member
  469. $optionlist = array("sfavatar", "sfposts", "sfsubscribe", "sflast", "sfnewposts", "sfchecktime", "sfbuddies", "sfusergroup", "signature", "sigimage", "sfuse_quicktags");
  470. foreach($optionlist as $option)
  471. {
  472. $wpdb->query("DELETE FROM ".$wpdb->prefix."usermeta WHERE meta_key='".$option."' AND user_id=".$member->ID.";");
  473. }
  474. }
  475. }
  476. }
  477. return;
  478. }
  479. # support function
  480. function sf_get_user_pm_status($user_id, $usergroups)
  481. {
  482. global $wpdb, $current_user;
  483. if ($current_user->ID == $user_id) return '1'; # if user id is current user then its admin
  484. if (empty($usergroups)) return '0';
  485. foreach ($usergroups as $usergroup)
  486. {
  487. $rids = $wpdb->get_results("SELECT permission_role FROM ".SFPERMISSIONS." WHERE usergroup_id='".$usergroup."'");
  488. foreach ($rids as $rid) {
  489. $role_actions = $wpdb->get_var("SELECT role_actions FROM ".SFROLES." WHERE role_id='".$rid->permission_role."'");
  490. $actions = maybe_unserialize($role_actions);
  491. if ($actions['Can use private messaging'] == 1)
  492. {
  493. return '1';
  494. }
  495. }
  496. }
  497. return '0';
  498. }
  499. # support function
  500. function sf_get_user_mod_status($user_id, $usergroups)
  501. {
  502. global $wpdb, $current_user;
  503. if (empty($usergroups)) return '0';
  504. foreach ($usergroups as $usergroup)
  505. {
  506. $mod = $wpdb->get_var("SELECT usergroup_is_moderator FROM ".SFUSERGROUPS." WHERE usergroup_id = ".$usergroup);
  507. if($mod) return '1';
  508. }
  509. return '0';
  510. }
  511. # support function for adding new role. use first two params for fixed value. third param will override second
  512. function sf_upgrade_add_new_role($newaction, $perm, $limit_access=false, $mods_only=false, $no_access=false)
  513. {
  514. global $wpdb;
  515. $roles = $wpdb->get_results("SELECT * FROM ".SFROLES." ORDER BY role_id");
  516. if ($roles)
  517. {
  518. foreach ($roles as $role)
  519. {
  520. if ($no_access)
  521. {
  522. $perm = 1;
  523. if ($role->role_name == 'No Access')
  524. {
  525. $perm = 0;
  526. }
  527. }
  528. if ($limit_access)
  529. {
  530. $perm = 1;
  531. if ($role->role_name == 'No Access' || $role->role_name == 'Read Only Access')
  532. {
  533. $perm = 0;
  534. }
  535. }
  536. if ($mods_only)
  537. {
  538. $perm = 0;
  539. if ($role->role_name == 'Moderators')
  540. {
  541. $perm = 1;
  542. }
  543. }
  544. $actions = unserialize($role->role_actions);
  545. $actions[$newaction] = $perm;
  546. $actions = maybe_serialize($actions);
  547. $sql = "UPDATE ".SFROLES." SET ";
  548. $sql.= 'role_name="'.$role->role_name.'", ';
  549. $sql.= 'role_desc="'.$role->role_desc.'", ';
  550. $sql.= 'role_actions="'.esc_sql($actions).'" ';
  551. $sql.= "WHERE role_id=".$role->role_id.";";
  552. $wpdb->query($sql);
  553. }
  554. }
  555. }
  556. # support function for modifying an existing role name
  557. function array_change_key_name($orig, $new, &$array)
  558. {
  559. if ( isset( $array[$orig] ) )
  560. {
  561. $array[$new] = $array[$orig];
  562. unset( $array[$orig] );
  563. }
  564. return $array;
  565. }
  566. # support function for modifying an existing role name
  567. function sf_modify_rolename($oldrole, $newrole)
  568. {
  569. global $wpdb;
  570. $roles = $wpdb->get_results("SELECT * FROM ".SFROLES." ORDER BY role_id");
  571. if ($roles)
  572. {
  573. foreach ($roles as $role)
  574. {
  575. $actions = unserialize($role->role_actions);
  576. $new_actions = array_change_key_name($oldrole, $newrole, $actions);
  577. $actions = maybe_serialize($new_actions);
  578. $sql = "UPDATE ".SFROLES." SET ";
  579. $sql.= 'role_name="'.$role->role_name.'", ';
  580. $sql.= 'role_desc="'.$role->role_desc.'", ';
  581. $sql.= 'role_actions="'.esc_sql($actions).'" ';
  582. $sql.= "WHERE role_id=".$role->role_id.";";
  583. $wpdb->query($sql);
  584. }
  585. }
  586. }
  587. # function to set up default group permissions
  588. function sf_group_def_perms()
  589. {
  590. global $wpdb;
  591. # grab the "default" permissions if they exist
  592. $noaccess = $wpdb->get_var("SELECT role_id FROM ".SFROLES." WHERE role_name='No Access'");
  593. if (!$noaccess) $noaccess = -1;
  594. $readonly = $wpdb->get_var("SELECT role_id FROM ".SFROLES." WHERE role_name='Read Only Access'");
  595. if (!$readonly) $readonly = -1;
  596. $standard = $wpdb->get_var("SELECT role_id FROM ".SFROLES." WHERE role_name='Standard Access'");
  597. if (!$standard) $standard = -1;
  598. $moderator = $wpdb->get_var("SELECT role_id FROM ".SFROLES." WHERE role_name='Moderator Access'");
  599. if (!$moderator) $moderator = -1;
  600. $usergroups = $wpdb->get_results("SELECT * FROM ".SFUSERGROUPS);
  601. $groups = $wpdb->get_results("SELECT group_id FROM ".SFGROUPS);
  602. if ($groups && $usergroups)
  603. {
  604. foreach ($groups as $group)
  605. {
  606. foreach ($usergroups as $usergroup)
  607. {
  608. if ($usergroup->usergroup_name == 'Guests')
  609. {
  610. $rid = $readonly;
  611. } else if ($usergroup->usergroup_name == 'Members')
  612. {
  613. $rid = $standard;
  614. } else if ($usergroup->usergroup_name == 'Moderators')
  615. {
  616. $rid = $moderator;
  617. } else {
  618. $rid = $noaccess;
  619. }
  620. $wpdb->query("
  621. INSERT INTO ".SFDEFPERMISSIONS."
  622. (group_id, usergroup_id, permission_role)
  623. VALUES
  624. ($group->group_id, $usergroup->usergroup_id, $rid)");
  625. }
  626. }
  627. }
  628. }
  629. # ====== 4.0.0 ==================================================================================================
  630. # Called by 4.0 to create new forum-smileys folder and content
  631. function sf_relocate_smileys()
  632. {
  633. global $wpdb;
  634. $basepath='/';
  635. if (is_multisite()) $basepath = '/blogs.dir/' . $wpdb->blogid .'/files/';
  636. $success = 0;
  637. $newpath = SF_STORE_DIR . $basepath . 'forum-smileys';
  638. $oldpath = SF_PLUGIN_DIR . '/styles/smileys';
  639. # check if new folder does not exist - which it shouldn't!
  640. if(!is_dir($newpath))
  641. {
  642. if(!is_writable(SF_STORE_DIR) || !($dir = @mkdir($newpath, 0777)))
  643. {
  644. $success = 1;
  645. return $success;
  646. }
  647. if (!is_writable($newpath))
  648. {
  649. $success = 2;
  650. return $success;
  651. }
  652. if(is_dir($newpath))
  653. {
  654. $avlist = opendir($oldpath);
  655. while (false !== ($file = readdir($avlist)))
  656. {
  657. if (is_file($oldpath.'/'.$file) && $file != "." && $file != "..")
  658. {
  659. if(!file_exists($newpath.'/'.$file))
  660. {
  661. if(@copy($oldpath.'/'.$file, $newpath.'/'.$file) == false)
  662. {
  663. $success = 3;
  664. break;
  665. }
  666. }
  667. }
  668. }
  669. closedir($avlist);
  670. }
  671. }
  672. return $success;
  673. }
  674. # Called by 4.0 to build smiley array
  675. function sf_build_base_smileys()
  676. {
  677. $smileys = array(
  678. "Confused" => array ( 0 => "sf-confused.gif", 1 => ":???:",),
  679. "Cool" => array ( 0 => "sf-cool.gif", 1 => ":cool:"),
  680. "Cry" => array ( 0 => "sf-cry.gif", 1 => ":cry:",),
  681. "Embarassed" => array ( 0 => "sf-embarassed.gif", 1 => ":oops:",),
  682. "Frown" => array ( 0 => "sf-frown.gif", 1 => ":frown:",),
  683. "Kiss" => array ( 0 => "sf-kiss.gif", 1 => ":kiss:",),
  684. "Laugh" => array ( 0 => "sf-laugh.gif", 1 => ":lol:",),
  685. "Smile" => array ( 0 => "sf-smile.gif", 1 => ":smile:",),
  686. "Surprised" => array ( 0 => "sf-surprised.gif", 1 => ":eek:",),
  687. "Wink" => array ( 0 => "sf-wink.gif", 1 => ":wink:",),
  688. "Yell" => array ( 0 => "sf-yell.gif", 1 => ":yell:",)
  689. );
  690. sf_add_sfmeta('smileys', 'smileys', serialize($smileys));
  691. return;
  692. }
  693. # Called by 4.0 to add tinymce editor toolbar/plugin arrays
  694. function sf_build_tinymce_toolbar_arrays()
  695. {
  696. $tbar_buttons=array('bold','italic','underline','|','bullist','numlist','|','blockquote','outdent','indent','|','link','unlink','|','undo','redo','forecolor','charmap','|','image');
  697. $tbar_buttons_add=array('media','|','spoiler','ddcode','|','emotions','|','pastetext','pasteword','|','selectall','preview','code','|','spellchecker');
  698. $tbar_plugins=array('inlinepopups','safari','media','preview','emotions','spoiler','ddcode','spellchecker','paste');
  699. $tinymce_toolbar = array();
  700. $tinymce_toolbar['tbar_buttons'] = $tbar_buttons;
  701. $tinymce_toolbar['tbar_buttons_add'] = $tbar_buttons_add;
  702. $tinymce_toolbar['tbar_plugins'] = $tbar_plugins;
  703. sf_add_sfmeta('tinymce_toolbar', 'default', serialize($tinymce_toolbar));
  704. sf_add_sfmeta('tinymce_toolbar', 'user', serialize($tinymce_toolbar));
  705. return;
  706. }
  707. # Called by 4.0 and install to build memberships table
  708. function sf_build_memberships_table()
  709. {
  710. global $wpdb;
  711. $users = $wpdb->get_results("SELECT user_id, usergroups FROM ".SFMEMBERS);
  712. if ($users)
  713. {
  714. foreach ($users as $user)
  715. {
  716. $memberships = maybe_unserialize($user->usergroups);
  717. if ($memberships)
  718. {
  719. for ($x=0; $x<count($memberships); $x++)
  720. {
  721. $sql ="INSERT INTO ".SFMEMBERSHIPS." (user_id, usergroup_id) ";
  722. $sql.="VALUES ('".$user->user_id."', '".$memberships[$x]."');";
  723. $wpdb->query($sql);
  724. }
  725. }
  726. }
  727. }
  728. }
  729. # Called by 4.0 to create pm message slugs
  730. function sf_create_message_slugs()
  731. {
  732. global $wpdb;
  733. # remove all single quotes
  734. $messages = $wpdb->get_results("SELECT message_id, title FROM ".SFMESSAGES);
  735. if($messages)
  736. {
  737. foreach($messages as $message)
  738. {
  739. $title = $message->title;
  740. $title = str_replace ("'", "", $title);
  741. $wpdb->query("UPDATE ".SFMESSAGES." SET title = '".$title."' WHERE message_id=".$message->message_id);
  742. }
  743. }
  744. # perform slug creation
  745. $found = true;
  746. while($found)
  747. {
  748. $message = sf_grab_slugless_messages();
  749. if($message)
  750. {
  751. $slug = sf_create_slug($message->title, 'pm');
  752. # if not created force change of title
  753. if($slug)
  754. {
  755. $wpdb->query("UPDATE ".SFMESSAGES." SET message_slug = '".$slug."' WHERE title='".$message->title."';");
  756. } else {
  757. $wpdb->query("UPDATE ".SFMESSAGES." SET title = 'Untitled' WHERE message_id = ".$message->message_id);
  758. }
  759. } else {
  760. $found = false;
  761. }
  762. }
  763. return;
  764. }
  765. function sf_grab_slugless_messages()
  766. {
  767. global $wpdb;
  768. return $wpdb->get_row("SELECT * FROM ".SFMESSAGES." WHERE message_slug='' LIMIT 1;");
  769. }
  770. # ====== 4.0.1 ==================================================================================================
  771. # Called by 4.0.1 to add blockquote to tinymce toolbar
  772. function sf_update_tmtoolbar_blockquote()
  773. {
  774. $tbrow = array();
  775. $tbrow[0]='default';
  776. $tbrow[1]='user';
  777. foreach($tbrow as $tb)
  778. {
  779. $tbmeta = sf_get_sfmeta('tinymce_toolbar', $tb);
  780. $buttons = unserialize($tbmeta[0]['meta_value']);
  781. $newbuttons = array();
  782. $found = false;
  783. # double check not already there...
  784. foreach($buttons['tbar_buttons'] as $button)
  785. {
  786. if($button == 'blockquote') $found=true;
  787. }
  788. if(!$found)
  789. {
  790. foreach($buttons['tbar_buttons'] as $button)
  791. {
  792. if($button == 'outdent')
  793. {
  794. $newbuttons[]='blockquote';
  795. }
  796. $newbuttons[]=$button;
  797. }
  798. $buttons['tbar_buttons']=$newbuttons;
  799. sf_update_sfmeta('tinymce_toolbar', $tb, serialize($buttons), $tbmeta[0]['meta_id']);
  800. }
  801. }
  802. return;
  803. }
  804. # ====== 4.0.2 ==================================================================================================
  805. function sf_update_membership_cleanup()
  806. {
  807. global $wpdb;
  808. #remove any duplicate memberships
  809. $memberships = $wpdb->get_results("SELECT * FROM ".SFMEMBERSHIPS);
  810. if ($memberships)
  811. {
  812. $test = array();
  813. foreach ($memberships as $membership)
  814. {
  815. if ($test[$membership->usergroup_id][$membership->user_id] == 1)
  816. {
  817. $wpdb->query("DELETE FROM ".SFMEMBERSHIPS." WHERE membership_id=".$membership->membership_id);
  818. } else {
  819. $test[$membership->usergroup_id][$membership->user_id] = 1;
  820. }
  821. }
  822. }
  823. }
  824. # ====== 4.1 ==================================================================================================
  825. # Called by 4.1 to create and populate the user options table with the old editor column
  826. function sf_create_user_options()
  827. {
  828. global $wpdb;
  829. $users = $wpdb->get_results("SELECT user_id, editor FROM ".SFMEMBERS." ORDER BY user_id");
  830. if ($users)
  831. {
  832. foreach ($users as $user)
  833. {
  834. # get user options if they exist
  835. $useroptions = sf_get_member_item($user->user_id, 'user_options');
  836. # move editor column to new users options column
  837. if(empty($user->editor) ? $editor=1 : $editor=$user->editor);
  838. $useroptions['editor'] = $editor;
  839. sf_update_member_item($user->user_id, 'user_options', $useroptions);
  840. }
  841. }
  842. return;
  843. }
  844. # Called by 4.1 to populate sfmeta for default usergroups
  845. function sf_create_usergroup_meta($members)
  846. {
  847. global $wp_roles;
  848. $roles = array_keys($wp_roles->role_names);
  849. if ($roles)
  850. {
  851. foreach ($roles as $role)
  852. {
  853. sf_add_sfmeta('default usergroup', $role, $members); # initally set each role to members usergroup
  854. }
  855. }
  856. }
  857. # Called by 4.1 to create new count and info fields
  858. function sf_build_41_counts()
  859. {
  860. global $wpdb;
  861. # start with post count in forums
  862. $forums = $wpdb->get_results("SELECT forum_id FROM ".SFFORUMS);
  863. if($forums)
  864. {
  865. foreach($forums as $forum)
  866. {
  867. $posts = $wpdb->get_var("SELECT COUNT(post_id) FROM ".SFPOSTS." WHERE forum_id=".$forum->forum_id);
  868. $wpdb->query("UPDATE ".SFFORUMS." SET post_count = ".$posts." WHERE forum_id=".$forum->forum_id);
  869. }
  870. }
  871. }
  872. # Called by 4.1 to change topic subscriptions and watches, and post ratings to be stored in serialized arrays
  873. function sf_serialize_subs_watches_ratings()
  874. {
  875. global $wpdb;
  876. # do sftopics table watches and subscriptions
  877. $topics = $wpdb->get_results("SELECT topic_id, topic_subs, topic_watches FROM ".SFTOPICS." WHERE topic_subs is not null OR topic_watches is not null");
  878. if ($topics)
  879. {
  880. foreach ($topics as $topic)
  881. {
  882. # make sure they arent already serialized, ie maybe ugprade already run once?
  883. if ($topic->topic_subs && !is_serialized($topic->topic_subs))
  884. {
  885. $subs = explode('@', $topic->topic_subs);
  886. $subs = serialize($subs);
  887. $wpdb->query("UPDATE ".SFTOPICS." SET topic_subs = '".$subs."' WHERE topic_id=".$topic->topic_id);
  888. }
  889. if ($topic->topic_watches && !is_serialized($topic->topic_watches))
  890. {
  891. $watches = explode('@', $topic->topic_watches);
  892. $watches = serialize($watches);
  893. $wpdb->query("UPDATE ".SFTOPICS." SET topic_watches = '".$watches."' WHERE topic_id=".$topic->topic_id);
  894. }
  895. }
  896. }
  897. # do members table watches and subscriptions
  898. $members = $wpdb->get_results("SELECT user_id, subscribe, watches FROM ".SFMEMBERS." WHERE subscribe is not null OR watches is not null");
  899. if ($members)
  900. {
  901. foreach ($members as $member)
  902. {
  903. if ($member->subscribe && !is_serialized($member->subscribe))
  904. {
  905. $subs = explode('@', $member->subscribe);
  906. $subs = serialize($subs);
  907. $wpdb->query("UPDATE ".SFMEMBERS." SET subscribe = '".$subs."' WHERE user_id=".$member->user_id);
  908. }
  909. if ($member->watches && !is_serialized($member->watches))
  910. {
  911. $watches = explode('@', $member->watches);
  912. $watches = serialize($watches);
  913. $wpdb->query("UPDATE ".SFMEMBERS." SET watches = '".$watches."' WHERE user_id=".$member->user_id);
  914. }
  915. }
  916. }
  917. # do members table post ratings
  918. $members = $wpdb->get_results("SELECT user_id, posts_rated FROM ".SFMEMBERS." WHERE posts_rated is not null");
  919. if ($members)
  920. {
  921. foreach ($members as $member)
  922. {
  923. if ($member->posts_rated && !is_serialized($member->posts_rated))
  924. {
  925. $ratings = explode('@', $member->posts_rated);
  926. $ratings = serialize($ratings);
  927. $wpdb->query("UPDATE ".SFMEMBERS." SET posts_rated = '".$ratings."' WHERE user_id=".$member->user_id);
  928. }
  929. }
  930. }
  931. }
  932. # Called by 4.1.0 to add spoiler to tinymce toolbar
  933. function sf_update_tmtoolbar_spoiler()
  934. {
  935. $tbrow = array();
  936. $tbrow[0]='default';
  937. $tbrow[1]='user';
  938. foreach($tbrow as $tb)
  939. {
  940. $tbmeta = sf_get_sfmeta('tinymce_toolbar', $tb);
  941. $buttons = unserialize($tbmeta[0]['meta_value']);
  942. $newbuttons = array();
  943. $found = false;
  944. # double check not already there...
  945. foreach($buttons['tbar_buttons_add'] as $button)
  946. {
  947. if($button == 'spoiler') $notfound=true;
  948. }
  949. if(!$found)
  950. {
  951. foreach($buttons['tbar_buttons_add'] as $button)
  952. {
  953. if($button == 'ddcode')
  954. {
  955. $newbuttons[]='spoiler';
  956. }
  957. $newbuttons[]=$button;
  958. }
  959. $buttons['tbar_buttons_add']=$newbuttons;
  960. $buttons['tbar_plugins'][]='spoiler';
  961. sf_update_sfmeta('tinymce_toolbar', $tb, serialize($buttons), $tbmeta[0]['meta_id']);
  962. }
  963. }
  964. return;
  965. }
  966. # Called by 4.1.0 to add SPF Manage Toolbox & Configuration caps to all admins with Options caps
  967. function sf_update_admin_toolbox()
  968. {
  969. global $wpdb;
  970. $admins = $wpdb->get_col("SELECT user_id FROM ".SFMEMBERS." WHERE admin = 1");
  971. $metakey = $wpdb->prefix.'capabilities';
  972. if($admins)
  973. {
  974. foreach($admins as $admin)
  975. {
  976. $caps = get_user_meta($admin, $metakey, true);
  977. if($caps['SPF Manage Options'] == 1)
  978. {
  979. $caps['SPF Manage Toolbox'] = 1;
  980. $caps['SPF Manage Configuration'] = 1;
  981. } else {
  982. $caps['SPF Manage Toolbox'] = 0;
  983. $caps['SPF Manage Configuration'] = 0;
  984. }
  985. update_user_meta($admin, $metakey, $caps);
  986. }
  987. }
  988. return;
  989. }
  990. # Called by 4.1.0 to clean up and serialise icon text
  991. function sf_upgrade_icontext()
  992. {
  993. $icons = get_option('sfshowicon');
  994. $list = explode('@', $icons);
  995. $newicons = array();
  996. foreach($list as $i)
  997. {
  998. $temp=explode(';', $i);
  999. if($temp[0] != 'Moderation Queue' && $temp[0] != 'Close New Post List')
  1000. {
  1001. $newicons[$temp[0]] = $temp[1];
  1002. }
  1003. }
  1004. update_option('sfshowicon', $newicons);
  1005. return;
  1006. }
  1007. # Called by 4.1.0 to clean up and serialise avatar options
  1008. function sf_upgrade_avatar_options()
  1009. {
  1010. # if avatar options already serialized, bail since must have already been done
  1011. if (is_serialized(get_option('sfavatars'))) return;
  1012. # convert avatar options
  1013. $sfoptions = array();
  1014. $sfoptions['sfshowavatars'] = get_option('sfshowavatars');
  1015. $sfoptions['sfavataruploads'] = get_option('sfavataruploads');
  1016. $sfoptions['sfgmaxrating'] = get_option('sfgmaxrating');
  1017. $sfoptions['sfavatarsize'] = get_option('sfavatarsize');
  1018. $wpavatar = get_option('sfwpavatar');
  1019. $gravatar = get_option('sfgravatar');
  1020. if ($wpavatar)
  1021. {
  1022. $sfoptions['sfavatarpriority'] = array(1, 0, 2, 3); # wp, gravatar, upload, spf
  1023. } else if ($gravatar) {
  1024. $sfoptions['sfavatarpriority'] = array(0, 2, 3, 1); # gravatar, upload, spf, wp
  1025. } else {
  1026. $sfoptions['sfavatarpriority'] = array(2, 3, 0, 1); # upload, spf, gravatar, wp
  1027. }
  1028. # add the new serialized options
  1029. add_option('sfavatars', $sfoptions);
  1030. return;
  1031. }
  1032. # Called by 4.1.0 to serialise custom field data
  1033. function sf_update_custom_fields()
  1034. {
  1035. $cfields = sf_get_sfmeta('custom_field');
  1036. if ($cfields && !is_serialized($cfields))
  1037. {
  1038. foreach ($cfields as $info)
  1039. {
  1040. $field = array();
  1041. $fields['type'] = $info['meta_value'];
  1042. $cfname = sf_create_slug($info['meta_key'], 'custom');
  1043. $cfname = preg_replace('|[^a-z0-9_]|i', '', $cfname);
  1044. sf_update_sfmeta('custom_field', $cfname, serialize($fields), $info['meta_id']);
  1045. }
  1046. }
  1047. return;
  1048. }
  1049. # Called by 4.1.0 to convert exiting user custom field data
  1050. function sf_convert_custom_profile_fields()
  1051. {
  1052. global $wpdb;
  1053. # see if any custom fields that need converting
  1054. $cfields = sf_get_sfmeta('custom_field');
  1055. if ($cfields && !is_serialized($cfields))
  1056. {
  1057. foreach ($cfields as $x => $cfield)
  1058. {
  1059. $sql = "SELECT user_id, meta_value FROM ".SFUSERMETA." WHERE meta_key='sfcustomfield".$x."'";
  1060. $usermetas = $wpdb->get_results($sql);
  1061. if ($usermetas)
  1062. {
  1063. foreach ($usermetas as $usermeta)
  1064. {
  1065. # delete old usermeta
  1066. delete_user_meta($usermeta->user_id, 'sfcustomfield'.$x);
  1067. # replace the usermeta
  1068. update_user_meta($usermeta->user_id, $cfield['meta_key'], $usermeta->meta_value);
  1069. }
  1070. }
  1071. }
  1072. }
  1073. return;
  1074. }
  1075. function sf_upgrade_members_pm()
  1076. {
  1077. global $wpdb;
  1078. $users = $wpdb->get_results("SELECT user_id, user_options FROM ".SFMEMBERS);
  1079. foreach ($users as $user)
  1080. {
  1081. $useroptions = unserialize($user->user_options);
  1082. $useroptions['pmemail'] = 1;
  1083. $useroptions = serialize($useroptions);
  1084. $wpdb->query("UPDATE ".SFMEMBERS." SET user_options='".$useroptions."' WHERE user_id=".$user->user_id);
  1085. }
  1086. }
  1087. function sf_modify_admin_cap($curcap, $newcap)
  1088. {
  1089. global $wpdb;
  1090. $admins = $wpdb->get_results("SELECT user_id FROM ".SFMEMBERS." WHERE admin=1");
  1091. foreach ($admins as $admin)
  1092. {
  1093. $user = new WP_User($admin->user_id);
  1094. if ($user->has_cap($curcap))
  1095. {
  1096. $user->remove_cap($curcap);
  1097. $user->add_cap($newcap);
  1098. }
  1099. }
  1100. }
  1101. function sf_move_options()
  1102. {
  1103. global $wpdb;
  1104. $sql = "
  1105. CREATE TABLE IF NOT EXISTS ".SFOPTIONS." (
  1106. option_id bigint(20) unsigned NOT NULL auto_increment,
  1107. option_name varchar(64) NOT NULL default '',
  1108. option_value longtext NOT NULL,
  1109. PRIMARY KEY (option_name),
  1110. KEY option_id (option_id)
  1111. ) ENGINE=MyISAM ".sf_charset().";";
  1112. $wpdb->query($sql);
  1113. $optionlist = array(
  1114. 'sfversion',
  1115. 'sfbuild',
  1116. 'sfpage',
  1117. 'sfslug',
  1118. 'sfsmilies',
  1119. 'sfuninstall',
  1120. 'sfdates',
  1121. 'sftimes',
  1122. 'sfshowavatars',
  1123. 'sfshowicon',
  1124. 'sfavatarsize',
  1125. 'sfavatars',
  1126. 'sfpermalink',
  1127. 'sfextprofile',
  1128. 'sfrsscount',
  1129. 'sfrsswords',
  1130. 'sfgravatar',
  1131. 'sfuseannounce',
  1132. 'sfannouncecount',
  1133. 'sfannouncehead',
  1134. 'sfannounceauto',
  1135. 'sfannouncetime',
  1136. 'sfannouncetext',
  1137. 'sfannouncelist',
  1138. 'sflockdown',
  1139. 'sfimgenlarge',
  1140. 'sfthumbsize',
  1141. 'sfmail',
  1142. 'sfnewusermail',
  1143. 'sfbadwords',
  1144. 'sfreplacementwords',
  1145. 'sfpm',
  1146. 'sfcustom',
  1147. 'sfeditormsg',
  1148. 'sfpostmsg',
  1149. 'sfcheck',
  1150. 'sfstyle',
  1151. 'sflogin',
  1152. 'sfadminsettings',
  1153. 'sfauto',
  1154. 'sfpmemail',
  1155. 'sfpmmax',
  1156. 'sfpostpaging',
  1157. 'sfeditor',
  1158. 'sfsmileys',
  1159. 'sfpostratings',
  1160. 'sfavataruploads',
  1161. 'sfprivatemessaging',
  1162. 'sfseo',
  1163. 'sfsigimagesize',
  1164. 'sfmemberlistperms',
  1165. 'sfgmaxrating',
  1166. 'sfcbexclusions',
  1167. 'sfshowmemberlist',
  1168. 'sfpostlinking',
  1169. 'sfwpavatar',
  1170. 'sfcheckformember',
  1171. 'sfsupport',
  1172. 'sfcontrols',
  1173. 'sfmetatags',
  1174. 'sfacolours',
  1175. 'sfallRSSurl',
  1176. 'sfautoupdate',
  1177. 'sfblockadmin',
  1178. 'sfconfig',
  1179. 'sfdisplay',
  1180. 'sffilters',
  1181. 'sfsinglemembership',
  1182. 'sfguests',
  1183. 'sfprofile',
  1184. 'sfuploads',
  1185. 'sfStartUpgrade',
  1186. 'sfforceupgrade',
  1187. 'sfzone'
  1188. );
  1189. foreach( $optionlist as $option)
  1190. {
  1191. $sfopt = sf_opcheck(get_option($option));
  1192. sf_add_option($option, $sfopt);
  1193. delete_option($option);
  1194. }
  1195. return;
  1196. }
  1197. function sf_generate_member_feedkeys()
  1198. {
  1199. global $wpdb;
  1200. $members = $wpdb->get_results("SELECT user_id FROM ".SFMEMBERS);
  1201. foreach ($members as $member)
  1202. {
  1203. # generate a pseudo-random UUID according to RFC 4122
  1204. $key = sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  1205. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
  1206. mt_rand( 0, 0x0fff ) | 0x4000,
  1207. mt_rand( 0, 0x3fff ) | 0x8000,
  1208. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
  1209. $wpdb->query("UPDATE ".SFMEMBERS." SET feedkey = '".$key."' WHERE user_id=".$member->user_id);
  1210. }
  1211. return;
  1212. }
  1213. # 4.2 - Upgrade postmeta records to new blog linking table
  1214. function sf_upgrade_bloglinks_table()
  1215. {
  1216. global $wpdb;
  1217. $links = $wpdb->get_results("SELECT post_id, meta_value FROM ".$wpdb->prefix."postmeta WHERE meta_key = 'forumlink'");
  1218. if($links)
  1219. {
  1220. foreach($links as $link)
  1221. {
  1222. $key=explode('@', $link->meta_value);
  1223. if((isset($key[0]) && !empty($key[0])) && (isset($key[1]) && !empty($key[1])))
  1224. {
  1225. $postid=$link->post_id;
  1226. $forumid=$key[0];
  1227. $topicid=$key[1];
  1228. $sql="INSERT INTO ".SFLINKS." (post_id, forum_id, topic_id) VALUES (".$postid.", ".$forumid.", '".$topicid."');";
  1229. $wpdb->query($sql);
  1230. }
  1231. }
  1232. $wpdb->query("DELETE FROM ".$wpdb->prefix."postmeta WHERE meta_key = 'forumlink'");
  1233. }
  1234. return;
  1235. }
  1236. # 4.2 - new members table building for installs
  1237. function sf_install_members_table($subphase)
  1238. {
  1239. global $wpdb, $current_user;
  1240. # get limits for installs
  1241. if ($subphase != 0)
  1242. {
  1243. $limit = " LIMIT 250 OFFSET ".(($subphase - 1) * 250);
  1244. }
  1245. # select all users
  1246. $sql = "SELECT ID FROM ".$wpdb->prefix."users".$limit;
  1247. $members = $wpdb->get_results($sql);
  1248. if ($members)
  1249. {
  1250. foreach($members as $member)
  1251. {
  1252. # Check ID exists and is not zero
  1253. if(is_numeric($member->ID) && $member->ID > 0)
  1254. {
  1255. sf_create_member_data($member->ID);
  1256. # for the admin installer, remove any usergroup membership added by create member function
  1257. if ($current_user->ID == $member->ID)
  1258. {
  1259. $wpdb->query("DELETE FROM ".$wpdb->prefix."sfmemberships WHERE user_id=".$member->ID);
  1260. sf_update_member_item($member->ID, 'pm', 1);
  1261. }
  1262. }
  1263. }
  1264. }
  1265. return;
  1266. }
  1267. # 4.2 - add additional admin colors
  1268. function sf_update_admin_colors()
  1269. {
  1270. global $wpdb;
  1271. # get admins that have custom colors
  1272. $sql = "SELECT user_id FROM ".SFUSERMETA." WHERE meta_key='sfadmincolours'";
  1273. $admins = $wpdb->get_results($sql);
  1274. if ($admins)
  1275. {
  1276. foreach ($admins as $admin)
  1277. {
  1278. $sfacolors = sf_opcheck(get_user_meta($admin->user_id, 'sfadmincolours', true));
  1279. $sfacolors['submitbg'] = '27537A';
  1280. $sfacolors['submitbgt'] = 'FFFFFF';
  1281. update_user_meta($admin->user_id, 'sfadmincolours', $sfacolors);
  1282. }
  1283. }
  1284. }
  1285. # 4.2 - move admin color options to admin options
  1286. function sf_move_admin_colors()
  1287. {
  1288. global $wpdb;
  1289. # get admins that have custom colors
  1290. $sql = "SELECT user_id FROM ".SFUSERMETA." WHERE meta_key='sfadmincolours'";
  1291. $admins = $wpdb->get_results($sql);
  1292. if ($admins)
  1293. {
  1294. foreach ($admins as $admin)
  1295. {
  1296. # get current admin options
  1297. $curdminoptions = sf_get_member_list($admin->user_id, 'admin_options');
  1298. $newadminoptions = $curdminoptions['admin_options'];
  1299. # add in custom colors
  1300. $newadminoptions['colors'] = sf_opcheck(get_user_meta($admin->user_id, 'sfadmincolours', true));
  1301. if (!$newadminoptions['colors']) $newadminoptions['colors'] = sf_get_option('sfacolours');
  1302. # save new admin options with colors
  1303. sf_update_member_item($admin->user_id, 'admin_options', $newadminoptions);
  1304. # delete old usermeta colors
  1305. delete_user_meta($admin->user_id, 'sfadmincolours');
  1306. }
  1307. }
  1308. }
  1309. # 4.3 - move sig images into sig field and clean up
  1310. function sf_convert_sig_images()
  1311. {
  1312. global $wpdb;
  1313. # remove the permission
  1314. $roles = $wpdb->get_results("SELECT * FROM ".SFROLES." ORDER BY role_id");
  1315. if ($roles)
  1316. {
  1317. foreach ($roles as $role)
  1318. {
  1319. $actions = unserialize($role->role_actions);
  1320. unset($actions['Can use images in signatures']);
  1321. $actions = maybe_serialize($actions);
  1322. $sql = "UPDATE ".SFROLES." SET ";
  1323. $sql.= 'role_name="'.$role->role_name.'", ';
  1324. $sql.= 'role_desc="'.$role->role_desc.'", ';
  1325. $sql.= 'role_actions="'.esc_sql($actions).'" ';
  1326. $sql.= "WHERE role_id=".$role->role_id.";";
  1327. $wpdb->query($sql);
  1328. }
  1329. }
  1330. # convert sig images to beginning of sig
  1331. $sigs = $wpdb->get_results("SELECT user_id, signature, sigimage FROM ".SFMEMBERS." WHERE sigimage != '' AND sigimage != 'NULL'");
  1332. if ($sigs)
  1333. {
  1334. foreach ($sigs as $sig)
  1335. {
  1336. $newsig = '<img src="'.$sig->sigimage.'" />'.addslashes($sig->signature);
  1337. $wpdb->query("UPDATE ".SFMEMBERS." SET signature = '".$newsig."' WHERE user_id=".$sig->user_id);
  1338. }
  1339. }
  1340. # now remove sigimage column and clean up any usermeta stuff left over
  1341. $wpdb->query("ALTER TABLE ".SFMEMBERS." DROP sigimage;");
  1342. $wpdb->query("DELETE FROM ".SFUSERMETA." WHERE meta_key = 'signature' OR meta_key = 'sigimage'");
  1343. }
  1344. # 4.3 - conver block admin levels to accessed roles
  1345. function sf_convert_block_admin()
  1346. {
  1347. global $wp_roles;
  1348. # init allowed roles
  1349. $sfblock['blockroles'] = array();
  1350. # find the min level for admin access
  1351. $sfblock = sf_get_option('sfblockadmin');
  1352. if ($sfblock && !empty($sfblock['blockrole']))
  1353. {
  1354. $minrole = get_role($sfblock['blockrole']);
  1355. $minlevel = 0;
  1356. foreach(array_keys($minrole->capabilities) as $cap)
  1357. {
  1358. preg_match('/^level_(10|[0-9])$/i', $cap, $matches);
  1359. if ($matches[1] > $minlevel) $minlevel = $matches[1];
  1360. }
  1361. # need to find level of each wp role
  1362. $roles = array_keys($wp_roles->role_names);
  1363. if ($roles)
  1364. {
  1365. foreach ($roles as $role)
  1366. {
  1367. $roleobj = $wp_roles->get_role($role);
  1368. $rolelevel = 0;
  1369. foreach(array_keys($roleobj->capabilities) as $cap)
  1370. {
  1371. preg_match('/^level_(10|[0-9])$/i', $cap, $matches);
  1372. if ($matches[1] > $rolelevel) $rolelevel = $matches[1];
  1373. }
  1374. # does the role current have access?
  1375. if ($rolelevel >= $minlevel)
  1376. {
  1377. $sfblock['blockroles'][$role] = 1;
  1378. } else {
  1379. $sfblock['blockroles'][$role] = 0;
  1380. }
  1381. }
  1382. }
  1383. # remove the old blockrole setting
  1384. unset($sfblock['blockrole']);
  1385. }
  1386. # always allow admin
  1387. $sfblock['blockroles']['administrator'] = true;
  1388. # save new blockrole access list
  1389. sf_update_option('sfblockadmin', $sfblock);
  1390. }
  1391. # 4.3 - add user id to new user list
  1392. function sf_upgrade_new_user_list()
  1393. {
  1394. global $wpdb;
  1395. $sfcontrols = sf_get_option('sfcontrols');
  1396. if ($sfcontrols['newuserlist'])
  1397. {
  1398. $newlist = array();
  1399. foreach ($sfcontrols['newuserlist'] as $index => $user)
  1400. {
  1401. $newlist[$index]['id'] = sf_get_user_id($user);
  1402. $newlist[$index]['name'] = $user;
  1403. }
  1404. $sfcontrols['newuserlist'] = $newlist;
  1405. # save update new user list
  1406. sf_update_option('sfcontrols', $sfcontrols);
  1407. }
  1408. }
  1409. ?>