/**
 *	Do note that while this function takes $post_ok as a parameter, it actually doesn't care much
 *  about the previous value, and will instead overwrite it. If you want to make sure your value
 *  is preserved, either disable this default function, or run your function on a later priority
 *  (this defaults to 10).
 */
function relevanssi_default_post_ok($post_ok, $doc)
{
    $status = relevanssi_get_post_status($doc);
    // if it's not public, don't show
    if ('publish' != $status) {
        $post_ok = false;
    }
    // ...unless
    if ('private' == $status) {
        $post_ok = false;
        if (function_exists('awp_user_can')) {
            // Role-Scoper, though Role-Scoper actually uses a different function to do this
            // So whatever is in here doesn't actually run.
            $current_user = wp_get_current_user();
            $post_ok = awp_user_can('read_post', $doc, $current_user->ID);
        } else {
            // Basic WordPress version
            $type = relevanssi_get_post_type($doc);
            $cap = 'read_private_' . $type . 's';
            if (current_user_can($cap)) {
                $post_ok = true;
            }
        }
    }
    // only show drafts, pending and future posts in admin search
    if (in_array($status, array('draft', 'pending', 'future')) && is_admin()) {
        $post_ok = true;
    }
    if (relevanssi_s2member_level($doc) == 0) {
        $post_ok = false;
    }
    // not ok with s2member
    return $post_ok;
}
Example #2
0
function relevanssi_search($q, $cat = NULL, $excat = NULL, $expost = NULL, $post_type = NULL, $taxonomy = NULL, $taxonomy_term = NULL, $operator = "AND") {
	global $relevanssi_table, $wpdb;

	$hits = array();
	
	$custom_cat = NULL;
	
	$o_cat = $cat;
	$o_excat = $excat;
	$o_expost = $expost;
	$o_post_type = $post_type;
	$o_taxonomy = $taxonomy;
	$o_taxonomy_term = $taxonomy_term;
	
	if ("custom" == $cat) {
		$custom_field = "custom";
		$post_ids = array();
		$results = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='$custom_field'");
		foreach ($results as $row) {
			$post_ids[] = $row->post_id;
		}
		$custom_cat = implode(",", $post_ids);
		$cat = "";
	}
	else if ($cat) {
		$cats = explode(",", $cat);
		$inc_term_tax_ids = array();
		$ex_term_tax_ids = array();
		foreach ($cats as $t_cat) {
			$exclude = false;
			if ($t_cat < 0) {
				// Negative category, ie. exclusion
				$exclude = true;
				$t_cat = substr($t_cat, 1); // strip the - sign.
			}
			$t_cat = $wpdb->escape($t_cat);
			$term_tax_id = $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy
				WHERE term_id=$t_cat");
			if ($term_tax_id) {
				$exclude ? $ex_term_tax_ids[] = $term_tax_id : $inc_term_tax_ids[] = $term_tax_id;
			}
		}
		
		$cat = implode(",", $inc_term_tax_ids);
		$excat_temp = implode(",", $ex_term_tax_ids);
	}

	if ($excat) {
		$excats = explode(",", $excat);
		$term_tax_ids = array();
		foreach ($excats as $t_cat) {
			$t_cat = $wpdb->escape(trim($t_cat, ' -'));
			$term_tax_id = $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy
				WHERE term_id=$t_cat");
			if ($term_tax_id) {
				$term_tax_ids[] = $term_tax_id;
			}
		}
		
		$excat = implode(",", $term_tax_ids);
	}

	if (isset($excat_temp)) {
		$excat .= $excat_temp;
	}

	if (isset($taxonomy)) {
		$term_tax_id = null;
		$term_tax_id = $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->terms
			JOIN $wpdb->term_taxonomy USING(`term_id`)
			WHERE `slug` LIKE '$taxonomy_term' AND `taxonomy` LIKE '$taxonomy'");
		if ($term_tax_id) {
			$taxonomy = $term_tax_id;
		} else {
			$taxonomy = null;
		}
	}

	if ($post_type) {
		if (!is_array($post_type)) {
			$post_types = explode(',', $post_type);
		}
		else {
			$post_types = $post_type;
		}
		$pt_array = array();
		foreach ($post_types as $pt) {
			$pt = "'" . trim(mysql_real_escape_string($pt)) . "'";
			array_push($pt_array, $pt);
		}
		$post_type = implode(",", $pt_array);
	}

	//Added by OdditY:
	//Exclude Post_IDs (Pages) for non-admin search ->
	if ($expost) {
		if ($expost != "") {
			$aexpids = explode(",",$expost);
			foreach ($aexpids as $exid){
				$exid = $wpdb->escape(trim($exid, ' -'));
				$postex .= " AND doc !='$exid'";
			}
		}	
	}
	// <- OdditY End

	$remove_stopwords = false;
	$phrases = relevanssi_recognize_phrases($q);

	$terms = relevanssi_tokenize($q, $remove_stopwords);
	if (count($terms) < 1) {
		// Tokenizer killed all the search terms.
		return $hits;
	}
	$terms = array_keys($terms); // don't care about tf in query

	$D = $wpdb->get_var("SELECT COUNT(DISTINCT(doc)) FROM $relevanssi_table");
	
	$total_hits = 0;
		
	$title_matches = array();
	$tag_matches = array();
	$comment_matches = array();
	$body_matches = array();
	$scores = array();
	$term_hits = array();

	$fuzzy = get_option('relevanssi_fuzzy');

	$query_restrictions = "";
	if ($expost) { //added by OdditY
		$query_restrictions .= $postex;
	}
	if ($cat) {
		$query_restrictions .= " AND doc IN (SELECT DISTINCT(object_id) FROM $wpdb->term_relationships
		    WHERE term_taxonomy_id IN ($cat))";
	}
	if ($excat) {
		$query_restrictions .= " AND doc NOT IN (SELECT DISTINCT(object_id) FROM $wpdb->term_relationships
		    WHERE term_taxonomy_id IN ($excat))";
	}
	if ($post_type) {
		$query_restrictions .= " AND doc IN (SELECT DISTINCT(ID) FROM $wpdb->posts
			WHERE post_type IN ($post_type))";
	}
	if ($phrases) {
		$query_restrictions .= " AND doc IN ($phrases)";
	}
	if ($custom_cat) {
		$query_restrictions .= " AND doc IN ($custom_cat)";
	}
	if ($taxonomy) {
		$query_restrictions .= " AND doc IN (SELECT DISTINCT(object_id) FROM $wpdb->term_relationships
			WHERE term_taxonomy_id IN ($taxonomy))";
	}

	if (isset($_REQUEST['by_date'])) {
		$n = $_REQUEST['by_date'];

		$u = substr($n, -1, 1);
		switch ($u) {
			case 'h':
				$unit = "HOUR";
				break;
			case 'd':
				$unit = "DAY";
				break;
			case 'm':
				$unit = "MONTH";
				break;
			case 'y':
				$unit = "YEAR";
				break;
			case 'w':
				$unit = "WEEK";
				break;
			default:
				$unit = "DAY";
		}

		$n = preg_replace('/[hdmyw]/', '', $n);

		if (is_numeric($n)) {
			$query_restrictions .= " AND doc IN (SELECT DISTINCT(ID) FROM $wpdb->posts
				WHERE post_date > DATE_SUB(NOW(), INTERVAL $n $unit))";
		}
	}

	$query_restrictions = apply_filters('relevanssi_where', $query_restrictions); // Charles St-Pierre

	foreach ($terms as $term) {
		$term = $wpdb->escape(like_escape($term));
		
		if ("always" == $fuzzy) {
			$term_cond = "(term LIKE '%$term' OR term LIKE '$term%') ";
		}
		else {
			$term_cond = " term = '$term' ";
		}
		
		$query = "SELECT doc, term, tf, title FROM $relevanssi_table WHERE $term_cond $query_restrictions";

		$matches = $wpdb->get_results($query);
		if (count($matches) < 1 && "sometimes" == $fuzzy) {
			$query = "SELECT doc, term, tf, title FROM $relevanssi_table
			WHERE (term LIKE '$term%' OR term LIKE '%$term') $query_restrictions";
			
			$matches = $wpdb->get_results($query);
		}
		
		$total_hits += count($matches);

		$query = "SELECT COUNT(DISTINCT(doc)) FROM $relevanssi_table WHERE $term_cond $query_restrictions";

		$df = $wpdb->get_var($query);

		if ($df < 1 && "sometimes" == $fuzzy) {
			$query = "SELECT COUNT(DISTINCT(doc)) FROM $relevanssi_table
				WHERE (term LIKE '%$term' OR term LIKE '$term%') $query_restrictions";
		
			$df = $wpdb->get_var($query);
		}
		
		$title_boost = floatval(get_option('relevanssi_title_boost'));
		$tag_boost = floatval(get_option('relevanssi_tag_boost'));
		$comment_boost = floatval(get_option('relevanssi_comment_boost'));
		
		$idf = log($D / (1 + $df));
//		$doc_terms_temp = array();
		foreach ($matches as $match) {
			$weight = $match->tf * $idf;

			if (!isset($term_hits[$match->doc][$term])) {
				$term_hits[$match->doc][$term] = 0;
			}
			
			switch ($match->title) {
				case "1":
					$weight = $weight * $title_boost;
					isset($title_matches[$match->doc]) ? $title_matches[$match->doc] += $match->tf : $title_matches[$match->doc] = $match->tf;
					$term_hits[$match->doc][$term] += $match->tf;
					break;
				case "2":
					$weight = $weight * $tag_boost;
					isset($tag_matches[$match->doc]) ? $tag_matches[$match->doc] += $match->tf : $tag_matches[$match->doc] = $match->tf;
					$term_hits[$match->doc][$term] += $match->tf;
					break;
				case "3":
					$weight = $weight * $comment_boost;
					isset($comment_matches[$match->doc]) ? $comment_matches[$match->doc] += $match->tf : $comment_matches[$match->doc] = $match->tf;
					$term_hits[$match->doc][$term] += $match->tf;
					break;
				default:
					isset($body_matches[$match->doc]) ? $body_matches[$match->doc] += $match->tf : $body_matches[$match->doc] = $match->tf;
					$term_hits[$match->doc][$term] += $match->tf;
			}

			$doc_terms[$match->doc][$term] = true; // count how many terms are matched to a doc
			isset($doc_weight[$match->doc]) ? $doc_weight[$match->doc] += $weight : $doc_weight[$match->doc] = $weight;
			isset($scores[$match->doc]) ? $scores[$match->doc] += $weight : $scores[$match->doc] = $weight;
		}
	}

	$total_terms = count($terms);
	
	if (isset($doc_weight) && count($doc_weight) > 0) {
		arsort($doc_weight);
		$i = 0;
		foreach ($doc_weight as $doc => $weight) {
			if (count($doc_terms[$doc]) < $total_terms && $operator == "AND") {
				// AND operator in action:
				// doc didn't match all terms, so it's discarded
				continue;
			}
			$status = get_post_status($doc);
			$post_ok = true;
			if ('private' == $status) {
				$post_ok = false;

				if (function_exists('awp_user_can')) {
					// Role-Scoper
					$current_user = wp_get_current_user();
					$post_ok = awp_user_can('read_post', $doc, $current_user->ID);
				}
				else {
					// Basic WordPress version
					$type = get_post_type($doc);
					$cap = 'read_private_' . $type . 's';
					if (current_user_can($cap)) {
						$post_ok = true;
					}
				}
			}
			if ($post_ok) $hits[intval($i++)] = get_post($doc);
		}
	}

	if (count($hits) < 1) {
		if ($operator == "AND" AND get_option('relevanssi_disable_or_fallback') != 'on') {
			$return = relevanssi_search($q, $o_cat, $o_excat, $o_expost, $o_post_type, $o_taxonomy, $o_taxonomy_term, "OR");
			extract($return);
		}
	}

	global $wp;	
	isset($wp->query_vars["orderby"]) ? $orderby = $wp->query_vars["orderby"] : $orderby = 'relevance';
	isset($wp->query_vars["order"]) ? $order = $wp->query_vars["order"] : $order = 'desc';
	if ($orderby != 'relevance')
		objectSort($hits, $orderby, $order);

	$return = array('hits' => $hits, 'body_matches' => $body_matches, 'title_matches' => $title_matches,
		'tag_matches' => $tag_matches, 'comment_matches' => $comment_matches, 'scores' => $scores,
		'term_hits' => $term_hits);

	return $return;
}
 Tiago Pocinho, Siemens Networks, S.A.
 */
/*************************************************************
 * This File loads the "Groups -> Groups" Tab
 * It allows to manage Groups by editing, adding or deleting
 ************************************************************/
require_once dirname(__FILE__) . '/groups-support.php';
global $wpdb;
global $scoper;
$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';
$cancel = isset($_REQUEST['cancel']) ? $_REQUEST['cancel'] : '';
$success_msg = '';
$errorMessage = "";
$all_users = array();
$suppress_groups_list = false;
$can_manage_all_groups = is_user_administrator_rs() || awp_user_can('manage_groups', BLOG_SCOPE_RS);
switch ($mode) {
    case "add":
        if (!$can_manage_all_groups) {
            wp_die(__awp('Cheatin&#8217; uh?'));
        }
        check_admin_referer('scoper-edit-groups');
        if (!empty($_POST['groupName'])) {
            $_POST['groupName'] = str_replace('[', '', $_POST['groupName']);
            $_POST['groupName'] = str_replace(']', '', $_POST['groupName']);
        }
        if (!UserGroups_tp::isValidName($_POST['groupName'])) {
            if ($_POST['groupName'] == "") {
                $errorMessage = __("Please specify a name for the group.", 'scoper');
            } else {
                $errorMessage = sprintf(__("A group with the name <strong>%s</strong> already exists.", 'scoper'), $_POST['groupName']);
Example #4
0
function relevanssi_default_post_ok($doc)
{
    $post_ok = true;
    $status = relevanssi_get_post_status($doc);
    if ('publish' != $status) {
        $post_ok = false;
    }
    if ('private' == $status) {
        $post_ok = false;
        if (function_exists('awp_user_can')) {
            // Role-Scoper
            $current_user = wp_get_current_user();
            $post_ok = awp_user_can('read_post', $doc, $current_user->ID);
        } else {
            // Basic WordPress version
            $type = relevanssi_get_post_type($doc);
            $cap = 'read_private_' . $type . 's';
            if (current_user_can($cap)) {
                $post_ok = true;
            }
        }
    }
    // only show drafts in admin search
    if (in_array($status, array('draft', 'pending', 'future')) && is_admin()) {
        $post_ok = true;
    }
    if (relevanssi_s2member_level($doc) == 0) {
        $post_ok = false;
    }
    // not ok with s2member
    return $post_ok;
}