PageRenderTime 243ms CodeModel.GetById 60ms app.highlight 89ms RepoModel.GetById 42ms app.codeStats 1ms

/wp-content/plugins/simple-forum/admin/panel-forums/support/sfa-forums-save.php

https://bitbucket.org/openfarmtech/weblog-content
PHP | 1163 lines | 909 code | 175 blank | 79 comment | 204 complexity | f05563b91b37b98130b35eddcba26971 MD5 | raw file
   1<?php
   2/*
   3Simple:Press
   4Admin Forums Data Sae Support Functions
   5$LastChangedDate: 2010-05-18 02:54:49 -0700 (Tue, 18 May 2010) $
   6$Rev: 4032 $
   7*/
   8
   9if (preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF']))
  10{
  11	die('Access Denied');
  12}
  13
  14function sfa_save_forums_create_group()
  15{
  16	global $wpdb;
  17
  18    check_admin_referer('forum-adminform_groupnew', 'forum-adminform_groupnew');
  19
  20    $ug_list = array_unique($_POST['usergroup_id']);
  21    $perm_list = $_POST['role'];
  22
  23    # fail if any user groups arent assigned a permission
  24	for ($x=0; $x<count($perm_list); $x++)
  25	{
  26		if ($perm_list[$x] == -1)
  27		{
  28	        $mess = __("All User Groups Must Be Assigned A Default Permission", "sforum");
  29    	    return $mess;
  30		}
  31	}
  32
  33    $seq = ($wpdb->get_var("SELECT MAX(group_seq) FROM ".SFGROUPS) + 1);
  34    $groupdata = array();
  35
  36    if (empty($_POST['group_name']))
  37    {
  38        $groupdata['group_name'] = __("New Forum Group", "sforum");
  39    } else
  40    {
  41        $groupdata['group_name'] = sf_filter_title_save(trim($_POST['group_name']));
  42    }
  43    if (empty($_POST['group_seq']))
  44    {
  45        $groupdata['group_seq'] = $seq;
  46    } else
  47    {
  48    	if (is_numeric($_POST['group_seq']))
  49    	{
  50 	       $groupdata['group_seq'] = sf_esc_int($_POST['group_seq']);
  51    	} else {
  52	        $mess = __("New Group Creation Failed - Sequence Must Be An Integer!", "sforum");
  53    		return $mess;
  54    	}
  55    }
  56
  57	if (!empty($_POST['group_icon']))
  58	{
  59		# Check new icon exists
  60		$groupdata['group_icon'] = sf_filter_title_save(trim($_POST['group_icon']));
  61		$path = SFCUSTOM.$groupdata['group_icon'];
  62		if (!file_exists($path))
  63		{
  64			$mess = sprintf(__("Custom Icon '%s' does not exist", "sforum"), $groupdata['group_icon']);
  65			return $mess;
  66		}
  67	} else {
  68		$groupdata['group_icon'] = NULL;
  69	}
  70
  71    $groupdata['group_desc'] = sf_filter_text_save(trim($_POST['group_desc']));
  72    $groupdata['group_message'] = sf_filter_text_save(trim($_POST['group_message']));
  73
  74    # check if we need to shuffle sequence numbers
  75    if ($groupdata['group_seq'] < $seq)
  76    {
  77        $groups = sf_get_groups_all();
  78        foreach ($groups as $group)
  79        {
  80            if ($group->group_seq >= $groupdata['group_seq'])
  81            {
  82                sfa_bump_group_seq($group->group_id, ($group->group_seq + 1));
  83            }
  84        }
  85    }
  86
  87    # create the group
  88	$sql ="INSERT INTO ".SFGROUPS." (group_name, group_desc, group_seq, group_icon, group_message) ";
  89	$sql.="VALUES ('".$groupdata['group_name']."', '".$groupdata['group_desc']."', ".$groupdata['group_seq'].", '".$groupdata['group_icon']."', '".$groupdata['group_message']."');";
  90    $success = $wpdb->query($sql);
  91    $group_id = $wpdb->insert_id;
  92
  93 	# save the default permissions for the group
  94	for( $x=0; $x<count($ug_list); $x++)
  95	{
  96		sfa_add_defpermission_row($group_id, $ug_list[$x], $perm_list[$x]);
  97	}
  98
  99    if ($success == false)
 100    {
 101        $mess = __("New Group Creation Failed!", "sforum");
 102    } else {
 103        $mess = __("New Forum Group Created", "sforum");
 104    }
 105
 106    return $mess;
 107}
 108
 109function sfa_save_forums_create_forum()
 110{
 111	global $wpdb;
 112
 113    check_admin_referer('forum-adminform_forumnew', 'forum-adminform_forumnew');
 114
 115    $forumdata = array();
 116
 117	if($_POST['forumtype'] == 1)
 118	{
 119		# Standard forum
 120	    $forumdata['group_id'] = sf_esc_int($_POST['group_id']);
 121	} else {
 122		# Sub forum
 123		$parentforum = $wpdb->get_row("SELECT * FROM ".SFFORUMS." WHERE forum_id=".sf_esc_int($_POST['forum_id']));
 124		$forumdata['group_id'] = $parentforum->group_id;
 125	}
 126
 127    $seq = $wpdb->get_var("SELECT MAX(forum_seq) FROM ".SFFORUMS." WHERE group_id=".$forumdata['group_id']) + 1;
 128    if(!isset($_POST['forum_seq']) || sf_esc_int($_POST['forum_seq'] == 0))
 129    {
 130    	$forumdata['forum_seq'] = $seq;
 131    } else {
 132		$forumdata['forum_seq'] = sf_esc_int($_POST['forum_seq']);
 133	}
 134
 135    $forumdata['forum_desc'] = sf_filter_text_save(trim($_POST['forum_desc']));
 136
 137    $forumdata['forum_status'] = 0;
 138    if (isset($_POST['forum_status']))
 139    {
 140        $forumdata['forum_status'] = 1;
 141	}
 142
 143    $forumdata['post_ratings'] = 0;
 144    if (isset($_POST['sfforum_ratings']))
 145    {
 146        $forumdata['post_ratings'] = 1;
 147	}
 148
 149    $forumdata['use_tags'] = 0;
 150    if (isset($_POST['forum_tags']))
 151    {
 152        $forumdata['use_tags'] = 1;
 153	}
 154
 155    $forumdata['forum_rss_private'] = 0;
 156    if (isset($_POST['forum_private']))
 157    {
 158        $forumdata['forum_rss_private'] = 1;
 159    }
 160
 161    $forumdata['forum_sitemap'] = 0;
 162    if (isset($_POST['forum_sitemap']))
 163    {
 164        $forumdata['forum_sitemap'] = 1;
 165    }
 166
 167    if (empty($_POST['forum_name']))
 168    {
 169        $forumdata['forum_name'] = __("New Forum", "sforum");
 170    } else {
 171        $forumdata['forum_name'] = sf_filter_title_save(trim($_POST['forum_name']));
 172    }
 173
 174    $forumdata['forum_message'] = sf_filter_text_save(trim($_POST['forum_message']));
 175
 176	if (!empty($_POST['forum_icon']))
 177	{
 178		# Check new icon exists
 179		$forumdata['forum_icon'] = sf_filter_title_save(trim($_POST['forum_icon']));
 180		$path = SFCUSTOM.$forumdata['forum_icon'];
 181		if (!file_exists($path))
 182		{
 183			$mess = sprintf(__("Custom Icon '%s' does not exist", "sforum"), $forumdata['forum_icon']);
 184			return $mess;
 185		}
 186	} else {
 187		$forumdata['forum_icon'] = NULL;
 188	}
 189
 190	if ($_POST['forum_topic_status'] == '' || $_POST['forum_topic_status'] == __('None', 'sforum'))
 191	{
 192		$forumdata['topic_status_set'] = 0;
 193	} else {
 194		$forumdata['topic_status_set'] = sf_esc_int($_POST['forum_topic_status']);
 195	}
 196    # check if we need to shuffle sequence numbers
 197    if ($forumdata['forum_seq'] < $seq)
 198    {
 199        $forums = sfa_get_forums_in_group($forumdata['group_id']);
 200        foreach ($forums as $forum)
 201        {
 202            if ($forum->forum_seq >= $forumdata['forum_seq'])
 203            {
 204                sfa_bump_forum_seq($forum->forum_id, ($forum->forum_seq + 1));
 205            }
 206        }
 207    }
 208
 209    # create the forum
 210	if($_POST['forumtype'] == 2)
 211	{
 212		$parentdata = $parentforum->forum_id;
 213	} else {
 214		$parentdata = '0';
 215	}
 216
 217	# do slug
 218	if(!isset($_POST['thisforumslug']) || empty($_POST['thisforumslug']))
 219	{
 220		$forumslug = sf_create_slug($forumdata['forum_name'], 'forum');
 221	} else {
 222		$forumslug = $_POST['thisforumslug'];
 223	}
 224
 225	$sql = "INSERT INTO ".SFFORUMS." (forum_name, forum_slug, forum_desc, group_id, forum_status, forum_seq, forum_rss_private, forum_icon, topic_status_set, post_ratings, use_tags, parent, forum_message, in_sitemap) ";
 226	$sql.= "VALUES ('".$forumdata['forum_name']."', '".$forumslug."', '".$forumdata['forum_desc']."', ".$forumdata['group_id'].", ".$forumdata['forum_status'].", ".$forumdata['forum_seq'].", ".$forumdata['forum_rss_private'].", '".$forumdata['forum_icon']."', ".$forumdata['topic_status_set'].", ".$forumdata['post_ratings'].", ".$forumdata['use_tags'].", ".$parentdata.", '".$forumdata['forum_message']."', ".$forumdata['forum_sitemap'].");";
 227	$thisforum = $wpdb->query($sql);
 228
 229	# now check the slug was populated and if not replace with forum id
 230	if (empty($forumslug))
 231	{
 232		$forumid = $wpdb->insert_id;
 233		$forumslug = 'forum-'.$forumid;
 234		$thisforum = $wpdb->query("UPDATE ".SFFORUMS." SET forum_slug='".$forumslug."' WHERE forum_id=".$forumid);
 235	}
 236    $success = $thisforum;
 237    $forum_id = $wpdb->insert_id;
 238
 239	# Id subforum add it as child to the parent
 240	if($_POST['forumtype'] == 2)
 241	{
 242		$children = array();
 243		$list = $wpdb->get_results("SELECT forum_id FROM ".SFFORUMS." WHERE parent=".$parentforum->forum_id." ORDER BY forum_seq");
 244		foreach($list as $id)
 245		{
 246			$children[]=$id->forum_id;
 247		}
 248		$wpdb->query("UPDATE ".SFFORUMS." SET children='".serialize($children)."' WHERE forum_id=".$parentforum->forum_id);
 249	}
 250
 251    # add the user group permission sets
 252    $usergroup_id_list = array_unique($_POST['usergroup_id']);
 253    $role_list = $_POST['role'];
 254	$perm_prob = false;
 255	for ($x=0; $x<count($usergroup_id_list); $x++)
 256	{
 257		$usergroup_id = sf_esc_int($usergroup_id_list[$x]);
 258		$role = $role_list[$x];
 259   		if ($role == -1)
 260	    {
 261			$defrole = sfa_get_defpermissions_role($forumdata['group_id'], $usergroup_id);
 262			if ($defrole == '')
 263			{
 264		    	$perm_prob = true;
 265		    } else {
 266				sfa_add_permission_data($forum_id, $usergroup_id, $defrole);
 267		    }
 268    	} else {
 269			sfa_add_permission_data($forum_id, $usergroup_id, $role);
 270		}
 271    }
 272
 273	# get affected members
 274	$members = $wpdb->get_results("
 275		SELECT DISTINCT user_id
 276		FROM ".SFMEMBERSHIPS."
 277		WHERE usergroup_id IN (
 278			SELECT usergroup_id
 279			FROM ".SFPERMISSIONS."
 280			WHERE forum_id = ".$forum_id."
 281		)
 282	");
 283	sfc_rebuild_members_pm($members);
 284
 285	# if the forum was created, signal success - doesnt check user group permission set though
 286    if ($success == false)
 287    {
 288        $mess = __("New Forum Creation Failed!", "sforum");
 289    } else {
 290		if ($perm_prob)
 291		{
 292        	$mess = __("New Forum Created - Permission Sets Not Properly Set For All User Groups!", "sforum");
 293	    } else {
 294        	$mess = __("New Forum Created!", "sforum");
 295		}
 296    }
 297
 298	sfa_clean_forum_children();
 299	sfa_resequence_forums($forumdata['group_id'], 0);
 300
 301    return $mess;
 302}
 303
 304# function to add a permission set globally to all forum
 305function sfa_save_forums_global_perm()
 306{
 307	global $wpdb;
 308
 309    check_admin_referer('forum-adminform_globalpermissionnew', 'forum-adminform_globalpermissionnew');
 310
 311	if ($_POST['usergroup_id'] != -1 && $_POST['role'] != -1)
 312	{
 313	    $usergroup_id = sf_esc_int($_POST['usergroup_id']);
 314    	$permission = sf_esc_int($_POST['role']);
 315
 316		# loop through all the groups
 317		$groups = sf_get_groups_all();
 318  	  	if ($groups)
 319  	  	{
 320  	  		$mess = '';
 321        	foreach ($groups as $group)
 322        	{
 323            	# use group permission set helper function to actually set the permission set
 324            	$mess.= sfa_set_group_permission($group->group_id, $usergroup_id, $permission);
 325            }
 326
 327			#update pm flag
 328			$members = $wpdb->get_results("SELECT user_id FROM ".SFMEMBERSHIPS.' WHERE usergroup_id='.$usergroup_id);
 329			sfc_rebuild_members_pm($members);
 330        } else {
 331        	$mess = __("There are no Groups or Forum!  No Permission Set Added!", "sforum");
 332    	}
 333	} else {
 334		$mess = __("Adding User Group Permission Set Failed!", "sforum");
 335	}
 336
 337    return $mess;
 338}
 339
 340# function to add a permission set to every forum within a group
 341function sfa_save_forums_group_perm()
 342{
 343	global $wpdb;
 344
 345    check_admin_referer('forum-adminform_grouppermissionnew', 'forum-adminform_grouppermissionnew');
 346
 347	if(isset($_POST['group_id']) && $_POST['usergroup_id'] != -1 && $_POST['role'] != -1)
 348	{
 349	    $group_id = sf_esc_int($_POST['group_id']);
 350	    $usergroup_id = sf_esc_int($_POST['usergroup_id']);
 351	    $permission = sf_esc_int($_POST['role']);
 352
 353		#update pm flag
 354	    $mess = sfa_set_group_permission($group_id, $usergroup_id, $permission);
 355		$members = $wpdb->get_results("SELECT user_id FROM ".SFMEMBERSHIPS.' WHERE usergroup_id='.$usergroup_id);
 356		sfc_rebuild_members_pm($members);
 357	} else {
 358		$mess = __("Adding User Group Permission Set Failed!", "sforum");
 359	}
 360
 361	return $mess;
 362}
 363
 364# helper function to loop through all forum in a group and add a permission set
 365function sfa_set_group_permission($group_id, $usergroup_id, $permission)
 366{
 367	global $wpdb;
 368
 369    $forums = sfa_get_forums_in_group($group_id);
 370
 371    if ($forums)
 372    {
 373    	$mess = '';
 374        foreach ($forums as $forum)
 375        {
 376            # If user group has a current permission set for this forum, remove the old one before adding the new one
 377            $current = $wpdb->get_row("SELECT * FROM ".SFPERMISSIONS." WHERE forum_id=".$forum->forum_id." AND usergroup_id=".$usergroup_id);
 378
 379            if ($current)
 380            {
 381                sfa_remove_permission_data($current->permission_id);
 382            }
 383
 384            # add the new permission set
 385            $success = sfa_add_permission_data($forum->forum_id, $usergroup_id, $permission);
 386
 387            if ($success == false)
 388            {
 389                $mess.= sf_filter_title_display($forum->forum_name) . ": ". __("Adding User Group Permission Set Failed!", "sforum") . '<br />';
 390            } else {
 391                $mess.= sf_filter_title_display($forum->forum_name) . ": ". __("User Group Permission Set Added to Forum!", "sforum") . '<br />';
 392            }
 393        }
 394    } else {
 395        $mess = __("Group has no Members!  No Permission Sets Added!", "sforum");
 396    }
 397
 398    return $mess;
 399}
 400
 401# function to remove all permission set from all forum
 402function sfa_save_forums_remove_perms()
 403{
 404	global $wpdb;
 405
 406    check_admin_referer('forum-adminform_allpermissionsdelete', 'forum-adminform_allpermissionsdelete');
 407
 408	# remove all permission set
 409    $wpdb->query("TRUNCATE TABLE ".SFPERMISSIONS);
 410
 411	# only admins can PM now
 412	$wpdb->query("UPDATE ".SFMEMBERS." SET pm = 0 WHERE admin = 0");
 413
 414    $mess = __("All Permission Sets Removed.", "sforum");
 415
 416    return $mess;
 417}
 418
 419# function to add a new permission set to a forum
 420function sfa_save_forums_forum_perm()
 421{
 422	global $wpdb;
 423
 424    check_admin_referer('forum-adminform_permissionnew', 'forum-adminform_permissionnew');
 425
 426	if (isset($_POST['forum_id']) && $_POST['usergroup_id'] != -1 && $_POST['role'] != -1)
 427	{
 428		$usergroup_id = sf_esc_int($_POST['usergroup_id']);
 429		$forum_id = sf_esc_int($_POST['forum_id']);
 430		$permission = sf_esc_int($_POST['role']);
 431
 432		# If user group has a current permission set for this forum, remove the old one before adding the new one
 433		$current = $wpdb->get_row("SELECT * FROM ".SFPERMISSIONS." WHERE forum_id=".$forum_id." AND usergroup_id=".$usergroup_id);
 434
 435		if ($current)
 436		{
 437			sfa_remove_permission_data($current->permission_id);
 438		}
 439
 440		# add the new permission set
 441		$success = sfa_add_permission_data($forum_id, $usergroup_id, $permission);
 442		if ($success == false)
 443		{
 444			$mess = __("Adding User Group Permission Set Failed!", "sforum");
 445		} else {
 446			$mess = __("User Group Permission Set Added to Forum", "sforum");
 447			$members = $wpdb->get_results("SELECT user_id FROM ".SFMEMBERSHIPS." WHERE usergroup_id = ".$usergroup_id." AND forum_id = ".$forum_id);
 448			sfc_rebuild_members_pm($members);
 449		}
 450	} else {
 451		$mess = __("Adding User Group Permission Set Failed!", "sforum");
 452	}
 453
 454    return $mess;
 455}
 456
 457function sfa_save_forums_delete_forum()
 458{
 459	global $wpdb;
 460
 461    check_admin_referer('forum-adminform_forumdelete', 'forum-adminform_forumdelete');
 462
 463    $group_id = sf_esc_int($_POST['group_id']);
 464    $forum_id = sf_esc_int($_POST['forum_id']);
 465    $cseq = sf_esc_int($_POST['cforum_seq']);
 466
 467	# get affected members
 468	$members = $wpdb->get_results("
 469		SELECT DISTINCT user_id
 470		FROM ".SFMEMBERSHIPS."
 471		WHERE usergroup_id IN (
 472			SELECT usergroup_id
 473			FROM ".SFPERMISSIONS."
 474			WHERE forum_id = ".$forum_id."
 475		)
 476	");
 477
 478	# If subforum or parent remove the relationship first.
 479	if($_POST['children'])
 480	{
 481		$children = unserialize($_POST['children']);
 482		foreach($children as $child)
 483		{
 484			$wpdb->query("UPDATE ".SFFORUMS." SET parent = null WHERE forum_id=".sf_esc_int($child));
 485		}
 486	}
 487	if($_POST['parent'])
 488	{
 489		$parentforum=$wpdb->get_var("SELECT children FROM ".SFFORUMS." WHERE forum_id=".sf_esc_int($_POST['parent']));
 490		$children = unserialize($parentforum);
 491		if(count($children) == 1)
 492		{
 493			$wpdb->query("UPDATE ".SFFORUMS." SET children = null WHERE forum_id=".sf_esc_int($_POST['parent']));
 494		} else {
 495			$newlist=array();
 496			foreach($children as $child)
 497			{
 498				if($child != $forum_id) $newlist[]=$child;
 499			}
 500			$wpdb->query("UPDATE ".SFFORUMS." SET children = '".serialize($newlist)."' WHERE forum_id=".sf_esc_int($_POST['parent']));
 501		}
 502	}
 503
 504	# need to delete all topics in the forum using standard routine to clean up behind it
 505	$topics = $wpdb->get_results("SELECT topic_id FROM ".SFTOPICS." WHERE forum_id=".$forum_id);
 506	if ($topics)
 507	{
 508		foreach ($topics as $topic)
 509		{
 510			sf_delete_topic($topic->topic_id, false);
 511		}
 512	}
 513
 514	# now delete the forum itself
 515	$wpdb->query("DELETE FROM ".SFFORUMS." WHERE forum_id=".$forum_id);
 516
 517	# remove permissions for this forum
 518	$perms = sfa_get_forum_permissions($forum_id);
 519	if ($perms)
 520	{
 521		foreach ($perms as $perm)
 522		{
 523			sfa_remove_permission_data($perm->permission_id);
 524		}
 525	}
 526
 527	# update pm flag for affected members
 528	sfc_rebuild_members_pm($members);
 529
 530    # need to iterate through the groups
 531    $forums = sfa_get_forums_in_group($group_id);
 532    foreach ($forums as $forum)
 533    {
 534        if ($forum->forum_seq > $cseq)
 535        {
 536            sfa_bump_forum_seq($forum->forum_id, ($forum->forum_seq - 1));
 537        }
 538    }
 539
 540	$mess = "Forum Deleted!";
 541
 542	sfa_clean_forum_children();
 543	sfa_resequence_forums($group_id, 0);
 544
 545    return $mess;
 546}
 547
 548function sfa_save_forums_delete_group()
 549{
 550	global $wpdb;
 551
 552    check_admin_referer('forum-adminform_groupdelete', 'forum-adminform_groupdelete');
 553
 554    $group_id = sf_esc_int($_POST['group_id']);
 555    $cseq = sf_esc_int($_POST['cgroup_seq']);
 556
 557	# get affected members
 558	$members = $wpdb->get_results("
 559		SELECT DISTINCT user_id
 560		FROM ".SFMEMBERSHIPS."
 561		WHERE usergroup_id IN (
 562			SELECT usergroup_id
 563			FROM ".SFPERMISSIONS."
 564			WHERE forum_id IN (
 565				SELECT forum_id
 566				FROM ".SFFORUMS."
 567				WHERE group_id = ".$group_id."
 568			)
 569		)
 570	");
 571
 572	# remove permissions for each forum in group
 573	$forums = sfa_get_forums_in_group($group_id);
 574	if ($forums)
 575	{
 576		foreach ($forums as $forum)
 577		{
 578			# remove permissions for this forum
 579			$perms = sfa_get_forum_permissions($forum->forum_id);
 580			if ($perms)
 581			{
 582				foreach ($perms as $perm)
 583				{
 584					sfa_remove_permission_data($perm->permission_id);
 585				}
 586			}
 587		}
 588	}
 589
 590	# rebuild pm column for affected members
 591	sfc_rebuild_members_pm($members);
 592
 593	# select all the forums in the group
 594	$forums = sfa_get_forums_in_group($group_id);
 595
 596	# remove the topics and posts in each forum
 597	foreach ($forums as $forum)
 598	{
 599		# need to delete all topics in the forum using standard routine to clean up behind it
 600		$topics = $wpdb->get_results("SELECT topic_id FROM ".SFTOPICS." WHERE forum_id=".$forum->forum_id);
 601		if ($topics)
 602		{
 603			foreach ($topics as $topic)
 604			{
 605				sf_delete_topic($topic->topic_id, false);
 606			}
 607		}
 608	}
 609
 610	#now remove the forums themselves
 611	$wpdb->query("DELETE FROM ".SFFORUMS." WHERE group_id=".$group_id);
 612	# and finaly remove the group
 613	$wpdb->query("DELETE FROM ".SFGROUPS." WHERE group_id=".$group_id);
 614
 615    # need to iterate through the groups
 616    $groups = sf_get_groups_all();
 617    foreach ($groups as $group)
 618    {
 619        if ($group->group_seq > $cseq)
 620        {
 621            sfa_bump_group_seq($group->group_id, ($group->group_seq - 1));
 622        }
 623    }
 624
 625	# remove the default permissions for the group being deleted
 626	$wpdb->query("DELETE FROM ".SFDEFPERMISSIONS." WHERE group_id=".$group_id);
 627
 628    $mess = __("Group Deleted", "sforum");
 629
 630    return $mess;
 631}
 632
 633# function to delete an existing permission set for a forum
 634function sfa_save_forums_delete_perm()
 635{
 636	global $wpdb;
 637
 638    check_admin_referer('forum-adminform_permissiondelete', 'forum-adminform_permissiondelete');
 639
 640    $permission_id = sf_esc_int($_POST['permission_id']);
 641
 642	# get affected members
 643	$members = $wpdb->get_results("
 644		SELECT DISTINCT user_id
 645		FROM ".SFMEMBERSHIPS."
 646		WHERE usergroup_id = (
 647			SELECT usergroup_id
 648			FROM ".SFPERMISSIONS."
 649			WHERE permission_id = ".$permission_id."
 650		)
 651	");
 652
 653	# remove the permission set from the forum
 654    $success = sfa_remove_permission_data($permission_id);
 655    if ($success == false)
 656    {
 657        $mess = __("Permission Set Delete Failed!", "sforum");
 658    } else {
 659        $mess = __("Permission Set Deleted", "sforum");
 660
 661        # rebuild pm flag for affected members
 662		sfc_rebuild_members_pm($members);
 663    }
 664
 665    return $mess;
 666}
 667
 668function sfa_save_forums_edit_forum()
 669{
 670	global $wpdb;
 671
 672    check_admin_referer('forum-adminform_forumedit', 'forum-adminform_forumedit');
 673
 674    $forumdata = array();
 675    $forum_id = sf_esc_int($_POST['forum_id']);
 676    $forumdata['forum_name'] = sf_filter_title_save(trim($_POST['forum_name']));
 677	if(!empty($_POST['thisforumslug']))
 678	{
 679		$forumdata['forum_slug'] = sf_create_slug($_POST['thisforumslug'], 'forum', false);
 680	} else {
 681		$forumdata['forum_slug'] = sf_create_slug($forumdata['forum_name'], 'forum');
 682	}
 683    $forumdata['forum_desc'] = sf_filter_text_save(trim($_POST['forum_desc']));
 684
 685	if(!isset($_POST['forum_seq']) || sf_esc_int($_POST['forum_seq'] == 0))
 686	{
 687		$mess = __("Unable to Save until Display Position is set", "sforum");
 688		return $mess;
 689	} else {
 690	    $forumdata['forum_seq'] = sf_esc_int($_POST['forum_seq']);
 691	}
 692
 693    $forumdata['group_id'] = sf_esc_int($_POST['group_id']);
 694
 695    $forumdata['forum_status'] = 0;
 696    if (isset($_POST['forum_status']))
 697    {
 698        $forumdata['forum_status'] = 1;
 699	}
 700
 701    $forumdata['use_tags'] = 0;
 702    if (isset($_POST['forum_tags']))
 703    {
 704        $forumdata['use_tags'] = 1;
 705	}
 706
 707    $forumdata['post_ratings'] = 0;
 708    if (isset($_POST['forum_ratings']))
 709    {
 710        $forumdata['post_ratings'] = 1;
 711    }
 712
 713    $forumdata['forum_rss_private'] = 0;
 714    if (isset($_POST['forum_private']))
 715    {
 716        $forumdata['forum_rss_private'] = 1;
 717    }
 718
 719    $forumdata['forum_sitemap'] = 0;
 720    if (isset($_POST['forum_sitemap']))
 721    {
 722        $forumdata['forum_sitemap'] = 1;
 723    }
 724
 725	if (!empty($_POST['forum_icon']))
 726	{
 727		# Check new icon exists
 728		$forumdata['forum_icon'] = sf_filter_title_save(trim($_POST['forum_icon']));
 729		$path = SFCUSTOM.$forumdata['forum_icon'];
 730		if (!file_exists($path))
 731		{
 732			$mess = sprintf(__("Custom Icon '%s' does not exist", "sforum"), $forumdata['forum_icon']);
 733			return $mess;
 734		}
 735	} else {
 736		$forumdata['forum_icon'] = NULL;
 737	}
 738
 739	if (isset($_POST['forum_topic_status']))
 740	{
 741		if ($_POST['forum_topic_status'] == '')
 742		{
 743			$forumdata['topic_status_set'] = 0;
 744
 745			# remove from all topics in the forum
 746			$wpdb->query("UPDATE ".SFTOPICS." SET topic_status_flag=0 WHERE forum_id=".$forum_id);
 747		} else {
 748            $forumdata['topic_status_set'] = $_POST['forum_topic_status'];
 749		}
 750	} else {
 751		$forumdata['topic_status_set'] = sf_esc_int($_POST['cforum_topic_status']);
 752	}
 753
 754	if (isset($_POST['forum_rss']))
 755	{
 756		$forumdata['forum_rss'] = sf_filter_save_cleanurl($_POST['forum_rss']);
 757	} else {
 758		$forumdata['forum_rss'] = sf_filter_save_cleanurl($_POST['cforum_rss']);
 759	}
 760
 761    $forumdata['forum_message'] = sf_filter_text_save(trim($_POST['forum_message']));
 762
 763    if (($forumdata['forum_name'] == sf_filter_title_display($_POST['cforum_name'])) &&
 764		($forumdata['forum_slug'] == $_POST['cforum_slug']) &&
 765		($forumdata['forum_seq'] == $_POST['cforum_seq']) &&
 766		($forumdata['group_id'] == $_POST['cgroup_id']) &&
 767		($forumdata['forum_status'] == $_POST['cforum_status']) &&
 768		($forumdata['use_tags'] == $_POST['cforum_tags']) &&
 769		($forumdata['post_ratings'] == $_POST['cforum_ratings']) &&
 770		($forumdata['forum_rss_private'] == $_POST['cforum_rss_private'])  &&
 771		($forumdata['forum_desc'] == $_POST['cforum_desc']) &&
 772		($forumdata['forum_icon'] == $_POST['cforum_icon']) &&
 773		($forumdata['forum_topic_status'] == $_POST['cforum_topic_status']) &&
 774		($forumdata['forum_message'] == $_POST['cforum_message']) &&
 775		($forumdata['forum_sitemap'] == $_POST['cforum_sitemap']) &&
 776		($forumdata['forum_rss'] == $_POST['cforum_rss']))
 777    {
 778        $mess = __("No Data Changed", "sforum");
 779        return $mess;
 780    }
 781
 782    # has the forum changed to a new group
 783    if ($forumdata['group_id'] != $_POST['cgroup_id'])
 784    {
 785        # let's resequence old group list first
 786        $forums = $wpdb->get_results("SELECT forum_id, forum_seq FROM ".SFFORUMS." WHERE group_id=".sf_esc_int($_POST['cgroup_id'])." AND forum_id <> ".$forum_id." ORDER BY forum_seq;");
 787        $cnt = count($forums);
 788        for ($i = 0; $i < $cnt; $i++)
 789        {
 790            sfa_bump_forum_seq($forums[$i]->forum_id, ($i + 1));
 791        }
 792
 793        # now we can make room in new group
 794        $seq = $wpdb->get_var("SELECT MAX(forum_seq) FROM ".SFFORUMS." WHERE group_id=". $forumdata['group_id']) + 1;
 795        if ($forumdata['forum_seq'] < $seq)
 796        {
 797            $forums = sfa_get_forums_in_group($forumdata['group_id']);
 798            foreach ($forums as $forum)
 799            {
 800                if ($forum->forum_seq >= $forumdata['forum_seq'])
 801                {
 802                    sfa_bump_forum_seq($forum->forum_id, ($forum->forum_seq + 1));
 803                }
 804            }
 805        }
 806    } else {
 807        # same group but has the seq changed?
 808        if ($forumdata['forum_seq'] != $_POST['cforum_seq'])
 809        {
 810            $forums = $wpdb->get_results("SELECT forum_id, forum_seq FROM ".SFFORUMS." WHERE group_id=".sf_esc_int($_POST['cgroup_id'])." AND forum_id <> ".$forum_id." ORDER BY forum_seq;");
 811            $cnt = count($forums);
 812            for ($i = 0; $i < $cnt; $i++)
 813            {
 814                if (($i + 1) < $forumdata['forum_seq'])
 815                {
 816                    sfa_bump_forum_seq($forums[$i]->forum_id, ($i + 1));
 817                } else {
 818                    sfa_bump_forum_seq($forums[$i]->forum_id, ($i + 2));
 819                }
 820            }
 821        }
 822    }
 823
 824    # Finally - we can save the updated forum record!
 825	if(empty($forumdata['forum_slug']))
 826	{
 827		$forumslug = sf_create_slug($forumdata['forum_name'], 'forum');
 828		if(empty($forumslug)) $forumslug = 'forum-'.$forum_id;
 829	} else {
 830		$forumslug = $forumdata['forum_slug'];
 831	}
 832
 833	# Let's make sure parent is set
 834	if($_POST['forumtype'] == 1)
 835	{
 836		$parent = 0;
 837	} else {
 838		$parent = sf_esc_int($_POST['forum_parent']);
 839	}
 840
 841	$sql = "UPDATE ".SFFORUMS." SET ";
 842	$sql.= 'forum_name="'.$forumdata['forum_name'].'", ';
 843	$sql.= 'forum_slug="'.$forumslug.'", ';
 844	$sql.= 'forum_desc="'.$forumdata['forum_desc'].'", ';
 845	$sql.= 'group_id='.$forumdata['group_id'].', ';
 846	$sql.= 'forum_status='.$forumdata['forum_status'].', ';
 847	$sql.= 'use_tags='.$forumdata['use_tags'].', ';
 848	$sql.= 'post_ratings='.$forumdata['post_ratings'].', ';
 849	$sql.= 'forum_rss_private='.$forumdata['forum_rss_private'].', ';
 850	$sql.= 'forum_icon="'.$forumdata['forum_icon'].'", ';
 851	$sql.= 'topic_status_set='.$forumdata['topic_status_set'].', ';
 852	$sql.= 'forum_rss="'.$forumdata['forum_rss'].'", ';
 853	$sql.= 'in_sitemap="'.$forumdata['forum_sitemap'].'", ';
 854	$sql.= 'parent='.$parent.', ';
 855	$sql.= 'forum_message="'.$forumdata['forum_message'].'", ';
 856	$sql.= 'forum_seq='.$forumdata['forum_seq']." ";
 857	$sql.= "WHERE forum_id=".$forum_id.";";
 858    $success = $wpdb->query($sql);
 859    if ($success == false)
 860    {
 861        $mess = __("Forum Record Update Failed!", "sforum");
 862    } else {
 863        $mess = __("Forum Record Updated", "sforum");
 864    }
 865
 866	sfa_clean_forum_children();
 867	sfa_resequence_forums($forumdata['group_id'], 0);
 868
 869    return $mess;
 870}
 871
 872function sfa_save_forums_edit_group()
 873{
 874	global $wpdb;
 875
 876    check_admin_referer('forum-adminform_groupedit', 'forum-adminform_groupedit');
 877
 878    $groupdata = array();
 879    $group_id = sf_esc_int($_POST['group_id']);
 880    $groupdata['group_name'] = sf_filter_title_save(trim($_POST['group_name']));
 881    $groupdata['group_seq'] = sf_filter_title_save(trim($_POST['group_seq']));
 882    $groupdata['group_desc'] = sf_filter_text_save(trim($_POST['group_desc']));
 883    $groupdata['group_message'] = sf_filter_text_save(trim($_POST['group_message']));
 884
 885    $ug_list = array_unique($_POST['usergroup_id']);
 886    $perm_list = $_POST['role'];
 887
 888	if (!empty($_POST['group_icon']))
 889	{
 890		# Check new icon exists
 891		$groupdata['group_icon'] = sf_filter_title_save(trim($_POST['group_icon']));
 892		$path = SFCUSTOM.$groupdata['group_icon'];
 893		if (!file_exists($path))
 894		{
 895			$mess = sprintf(__("Custom Icon '%s' does not exist", "sforum"), $groupdata['group_icon']);
 896			return $mess;
 897		}
 898	} else {
 899		$groupdata['group_icon'] = NULL;
 900	}
 901
 902	if (isset($_POST['group_rss']))
 903	{
 904		$groupdata['group_rss'] = sf_filter_save_cleanurl($_POST['group_rss']);
 905	} else {
 906		$groupdata['group_rss'] = sf_filter_save_cleanurl($_POST['cgroup_rss']);
 907	}
 908
 909    # fail if any user groups arent assigned a permission
 910	for ($x=0; $x<count($perm_list); $x++)
 911	{
 912		if ($perm_list[$x] == -1)
 913		{
 914	        $mess = __("All User Groups Must Be Assigned A Default Permission", "sforum");
 915    	    return $mess;
 916		}
 917	}
 918
 919	# save the default permissions for the group
 920	for ($x=0; $x<count($ug_list); $x++)
 921	{
 922		if (sfa_get_defpermissions_role($group_id, $ug_list[$x]))
 923		{
 924			$sql = "
 925				UPDATE ".SFDEFPERMISSIONS."
 926				SET permission_role=$perm_list[$x]
 927				WHERE group_id=$group_id AND usergroup_id=$ug_list[$x]";
 928			$wpdb->query($sql);
 929		} else {
 930			sfa_add_defpermission_row($group_id, $ug_list[$x], $perm_list[$x]);
 931		}
 932	}
 933
 934    if ($groupdata['group_name'] == $_POST['cgroup_name'] &&
 935		$groupdata['group_seq'] == $_POST['cgroup_seq'] &&
 936		$groupdata['group_desc'] == $_POST['cgroup_desc'] &&
 937		$groupdata['group_rss'] == $_POST['cgroup_rss'] &&
 938		$groupdata['group_message'] == $_POST['cgroup_message'] &&
 939		$groupdata['group_icon'] == $_POST['cgroup_icon'])
 940    {
 941        $mess = __("No Data Changed", "sforum");
 942    } else {
 943	    # has the sequence changed?
 944	    if ($groupdata['group_seq'] != $_POST['cgroup_seq'])
 945	    {
 946	        # need to iterate through the groups to change sequence number
 947	        $groups = $wpdb->get_results("SELECT group_id, group_seq FROM ".SFGROUPS." WHERE group_id <> ".$group_id." ORDER BY group_seq;");
 948	        $cnt = count($groups);
 949	        for ($i = 0; $i < $cnt; $i++)
 950	        {
 951	            if (($i + 1) < $groupdata['group_seq'])
 952	            {
 953	                sfa_bump_group_seq($groups[$i]->group_id, ($i + 1));
 954	            } else {
 955	                sfa_bump_group_seq($groups[$i]->group_id, ($i + 2));
 956	            }
 957	        }
 958	    }
 959
 960		$sql = "UPDATE ".SFGROUPS." SET ";
 961		$sql.= 'group_name="'.$groupdata['group_name'].'", ';
 962		$sql.= 'group_desc="'.$groupdata['group_desc'].'", ';
 963		$sql.= 'group_icon="'.$groupdata['group_icon'].'", ';
 964		$sql.= 'group_rss="'.$groupdata['group_rss'].'", ';
 965		$sql.= 'group_message="'.$groupdata['group_message'].'", ';
 966		$sql.= 'group_seq='.$groupdata['group_seq']." ";
 967		$sql.= "WHERE group_id=".$group_id.";";
 968	    $success = $wpdb->query($sql);
 969	    if ($success == false)
 970	    {
 971	        $mess = __("Group Record Update Failed!", "sforum");
 972	    } else {
 973	        $mess = __("Forum Group Record Updated", "sforum");
 974	    }
 975    }
 976
 977    return $mess;
 978}
 979
 980# function to update an existing permission set for a forum
 981function sfa_save_forums_edit_perm()
 982{
 983	global $wpdb;
 984
 985    check_admin_referer('forum-adminform_permissionedit', 'forum-adminform_permissionedit');
 986
 987    $permissiondata = array();
 988    $permission_id = sf_esc_int($_POST['permission_id']);
 989    $permissiondata['permission_role'] = sf_esc_int($_POST['role']);
 990
 991    # dont do anything if the permission set wasnt actually updated
 992    if ($permissiondata['permission_role'] == $_POST['ugroup_perm'])
 993    {
 994        $mess = __("No Data Changed", "sforum");
 995        return;
 996    }
 997
 998	# save the updated permission set info
 999	$sql = "UPDATE ".SFPERMISSIONS." SET ";
1000	$sql.= 'permission_role="'.$permissiondata['permission_role'].'" ';
1001	$sql.= "WHERE permission_id=".$permission_id.";";
1002    $success = $wpdb->query($sql);
1003    if ($success == false)
1004    {
1005        $mess = __("Permission Set Update Failed!", "sforum");
1006    } else {
1007        $mess = __("Permission Set Updated", "sforum");
1008
1009		# get affected members
1010		$members = $wpdb->get_results("
1011			SELECT DISTINCT user_id
1012			FROM ".SFMEMBERSHIPS."
1013			WHERE usergroup_id = (
1014				SELECT usergroup_id
1015				FROM ".SFPERMISSIONS."
1016				WHERE permission_id = ".$permission_id."
1017			)
1018		");
1019		sfc_rebuild_members_pm($members);
1020    }
1021
1022    return $mess;
1023}
1024
1025function sfa_bump_group_seq($id, $seq)
1026{
1027	global $wpdb;
1028
1029	$sql = "UPDATE ".SFGROUPS." SET ";
1030	$sql.= 'group_seq='.$seq." ";
1031	$sql.= "WHERE group_id=".$id.";";
1032
1033	$wpdb->query($sql);
1034	return;
1035}
1036
1037function sfa_bump_forum_seq($id, $seq)
1038{
1039	global $wpdb;
1040
1041	$sql = "UPDATE ".SFFORUMS." SET ";
1042	$sql.= 'forum_seq='.$seq." ";
1043	$sql.= "WHERE forum_id=".$id.";";
1044
1045	$wpdb->query($sql);
1046	return;
1047}
1048
1049function sfa_add_permission_data($forum_id, $usergroup_id, $permission)
1050{
1051	global $wpdb;
1052
1053	$forumid = esc_sql($forum_id);
1054	$usergroupid = esc_sql($usergroup_id);
1055	$perm = esc_sql($permission);
1056
1057	$sql ="INSERT INTO ".SFPERMISSIONS." (forum_id, usergroup_id, permission_role) ";
1058	$sql.="VALUES ('".$forumid."', '".$usergroupid."', '".$perm."');";
1059
1060	return $wpdb->query($sql);
1061}
1062
1063function sfa_add_defpermission_row($group_id, $usergroup_id, $role)
1064{
1065	global $wpdb;
1066
1067	$sql = "
1068		INSERT INTO ".SFDEFPERMISSIONS."
1069		(group_id, usergroup_id, permission_role)
1070		VALUES
1071		($group_id, $usergroup_id, $role)";
1072
1073	return $wpdb->query($sql);
1074}
1075
1076function sfa_resequence_forums($groupid, $parent)
1077{
1078	global $sequence;
1079
1080	$forums = sfa_get_group_forums_by_parent($groupid, $parent);
1081
1082	if($forums)
1083	{
1084		foreach ($forums as $forum)
1085		{
1086			$sequence++;
1087			sfa_bump_forum_seq($forum->forum_id, $sequence);
1088
1089			if($forum->children)
1090			{
1091				$childlist = array(unserialize($forum->children));
1092				if(count($childlist) > 0)
1093				{
1094					sfa_resequence_forums($groupid, $forum->forum_id);
1095				}
1096			}
1097		}
1098	}
1099	return;
1100}
1101
1102function sfa_clean_forum_children()
1103{
1104	global $wpdb;
1105
1106	# Remove all chil records from forums
1107	$wpdb->query("UPDATE ".SFFORUMS." set children=''");
1108
1109	# Now get ALL forums
1110	$forums = $wpdb->get_results("SELECT forum_id, parent FROM ".SFFORUMS);
1111	if($forums)
1112	{
1113		foreach($forums as $forum)
1114		{
1115			if($forum->parent != 0)
1116			{
1117				$childlist = $wpdb->get_row("SELECT group_id, children FROM ".SFFORUMS." WHERE forum_id=".$forum->parent);
1118				if($childlist->children)
1119				{
1120					$children=unserialize($childlist->children);
1121				} else {
1122					$children = array();
1123				}
1124				$children[]=$forum->forum_id;
1125				$wpdb->query("UPDATE ".SFFORUMS." set children='".serialize($children)."' WHERE forum_id=".$forum->parent);
1126				$wpdb->query("UPDATE ".SFFORUMS." set group_id=".$childlist->group_id." WHERE forum_id=".$forum->forum_id);
1127			}
1128		}
1129	}
1130	return;
1131}
1132
1133function sfa_save_forums_global_rss()
1134{
1135	global $wpdb;
1136
1137    check_admin_referer('forum-adminform_globalrss', 'forum-adminform_globalrss');
1138
1139	# update the globla rss replacement url
1140	sf_update_option('sfallRSSurl', sf_filter_save_cleanurl($_POST['sfallrssurl']));
1141    $mess = __("Global RSS Settings Updated!", "sforum");
1142
1143    return $mess;
1144}
1145
1146function sfa_save_forums_global_rssset()
1147{
1148	global $wpdb;
1149
1150    check_admin_referer('forum-adminform_globalrssset', 'forum-adminform_globalrssset');
1151
1152    $private = sf_esc_int($_POST['sfglobalrssset']);
1153
1154	$sql = "UPDATE ".SFFORUMS." SET ";
1155	$sql.= 'forum_rss_private='.$private;
1156	$success = $wpdb->query($sql);
1157
1158    $mess = __("Global RSS Settings Updated!", "sforum");
1159
1160    return $mess;
1161}
1162
1163?>