<?php

if(!defined('IN_MYBB'))
	die('This file cannot be accessed directly.');

$plugins->add_hook('postbit', 'hidenewbsigs_postbit');
$plugins->add_hook('member_profile_start', 'hidenewbsigs_profile_start');
$plugins->add_hook('member_profile_end', 'hidenewbsigs_profile');
$plugins->add_hook('usercp_editsig_start', 'hidenewbsigs_notify');

function hidenewbsigs_info() {
	return array(
		'name'			=> 'Hide Sigs and Website of New Users',
		'description'	=> 'Hides the signatures and website links of users with low post counts, in an attempt to lessen impact of sig-link spammers.',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.3',
		'compatibility'	=> '14*,15*,16*,17*,18*',
		'guid'			=> ''
	);
}


function hidenewbsigs_activate() {
	global $db;
	$gid = $db->fetch_field($db->simple_select('settinggroups', 'gid', 'name="member"'), 'gid');
	$db->insert_query('settings', array(
		'gid' => $gid,
		'name' => 'hidenewbsigs_postcount',
		'title' => 'Minimum Post Count for Signature/Website Link Display',
		'description' => 'The minium number of posts a user must have before their signature and website link is displayed on posts and their profile.',
		'optionscode' => 'text',
		'value' => 5,
		'disporder' => 100,
		'isdefault' => 0
	));
	rebuild_settings();
}

function hidenewbsigs_deactivate() {
	global $db;
	$db->delete_query('settings', 'name="hidenewbsigs_postcount"');
	rebuild_settings();
}

function hidenewbsigs_hidesigs($num) {
	// attempt to get postcount by removing all non-numerical characters from it
	if(!is_numeric($num)) // even though ctype_digit is apparently faster...
		$num = preg_replace('~[^0-9]~','',$num);
	
	return (intval($num) < intval($GLOBALS['mybb']->settings['hidenewbsigs_postcount']));
}

function hidenewbsigs_postbit(&$p) {
	if(hidenewbsigs_hidesigs($p['postnum']))
		$p['signature'] = $p['button_www'] = '';
}

function hidenewbsigs_profile_start() {
	if(hidenewbsigs_hidesigs($GLOBALS['memprofile']['postnum']))
		$GLOBALS['templates']->cache['member_profile_website'] = '';
}
function hidenewbsigs_profile() {
	if(hidenewbsigs_hidesigs($GLOBALS['memprofile']['postnum'])) {
		$GLOBALS['signature'] = $GLOBALS['website'] = '';
		if(isset($GLOBALS['contact_details'])) {
			// re-evaluate conditional
			if(
				(
					(isset($GLOBALS['any_contact_field']) && !$GLOBALS['any_contact_field'])
					|| (isset($GLOBALS['contact_fields']) && empty($GLOBALS['contact_fields']))
				) && !$GLOBALS['sendemail'] && !$GLOBALS['sendpm']
			)
				unset($GLOBALS['contact_details']);
		}
	}
}

function hidenewbsigs_notify() {
	$user =& $GLOBALS['mybb']->user;
	
	if(!$user['suspendsignature'] || $user['suspendsigtime'] <= TIME_NOW) {
		if(hidenewbsigs_hidesigs($user['postnum'])) {
			global $lang;
			isset($lang->hidenewbsigs_sig_hidden_notice) or $lang->hidenewbsigs_sig_hidden_notice = 'Note that your signature will be hidden on the forums until you have enough posts.';
			
			$tpl =& $GLOBALS['templates']->cache['usercp_editsig'];
			if(!$tpl) $GLOBALS['templates']->cache('usercp_editsig');
			$tpl = str_replace('{$error}', '{$error}<div class="error"><p>{$lang->hidenewbsigs_sig_hidden_notice}</p></div><br />', $tpl);
		}
	}
}

?>