MyBB Community Forums

Full Version: How to solve this?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I got this error in this plugin
Signup Question v1.1


Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting T_VARIABLE in /home/a3876647/public_html/forum/inc/plugins/signupquestions.php on line 802

<?php

if(!defined('IN_MYBB'))
{
	die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}

$plugins->add_hook('member_register_start', 'signupquestions_signup');
$plugins->add_hook('member_register_end', 'signupquestions_validator');
$plugins->add_hook('datahandler_user_validate', 'signupquestions_validate');
$plugins->add_hook('admin_user_menu', 'signupquestions_load_menu');
$plugins->add_hook('admin_user_action_handler', 'signupquestions_handler');
$plugins->add_hook('admin_load', 'signupquestions_load_acp');
$plugins->add_hook('xmlhttp', 'signupquestions_ajax');

/**
 * Plugin info
 *
 * @return array
 */
function signupquestions_info()
{
	return array(
		'name'            => 'Signup Questions',
		'description'     => 'Add a security question in the signup page, stopping spammers from being able to sign up.',
		'website'         => 'http://mods.mybb.com/view/signup-question',
		'author'          => 'Santiago Dimattía',
		'authorsite'      => 'http://teleportz.com.ar',
		'version'         => '1.1',
		'guid'            => '660fa4c815376e5666401df5b8b0854f',
		'compatibility'   => '16*'
	);
}

/**
 * Install
 *
 */
function signupquestions_install()
{
	global $db, $lang;

	$db->query("CREATE TABLE IF NOT EXISTS `".TABLE_PREFIX."signup_questions` (
  `qid` int(10) NOT NULL AUTO_INCREMENT,
  `question` varchar(255) NOT NULL,
  `answer` varchar(255) NOT NULL,
  `success_count` int(10) NOT NULL,
  `fail_count` int(10) NOT NULL,
  PRIMARY KEY (`qid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");

	$lang->load('signupquestions');

	$questions = array();

	// Default questions
	$questions[] = array(
		'question' => $lang->sq_default_one,
		'answer' => $lang->sq_default_one_answer,
	);

	$questions[] = array(
		'question' => $lang->sq_default_two,
		'answer' => $lang->sq_default_two_answer,
	);

	$questions[] = array(
		'question' => $lang->sq_default_three,
		'answer' => $lang->sq_default_three_answer,
	);

	foreach($questions as $key => $question)
	{
		$db->insert_query('signup_questions', $question);
	}

	return true;
}

/**
 * Uninstall
 *
 */
function signupquestions_uninstall()
{
	global $db;

	$db->query('DROP TABLE '.TABLE_PREFIX.'signup_questions');

	return true;
}

/**
 * Check if the plugin is installed
 *
 */
function signupquestions_is_installed()
{
	global $db;
	
	if($db->table_exists('signup_questions'))
	{
		return true;
	}

	return false;
}

/**
 * Activate
 *
 */
function signupquestions_activate()
{
	global $db;

	$template_content = '<br />
<fieldset class="trow2">
	<legend>
		<strong>{$lang->signup_question}</strong>
	</legend>
	
	<table cellspacing="0" cellpadding="{$theme[\'tablespace\']}" style="width: 100%;">
		<tr>
			<td>
				<span class="smalltext"><label for="answer" id="question">{$question[\'question\']}</label></span>
			</td>
			<td style="text-align: right;">
				<input type="button" id="change_question" value="{$lang->sq_change_question}" />
			</td>
		</tr>
		
		<tr>
			<td colspan="2">
				<input type="hidden" name="qid" id="qid" value="{$question[\'qid\']}"  />
				<input type="text" class="textbox" name="answer" id="answer" value="{$mybb->input[\'answer\']}" style="width: 100%;" />
			</td>
		</tr>
	</table>
</fieldset>
<script type="text/javascript" src="{$mybb->settings[\'bburl\']}/jscripts/signupquestions.js"></script>
';

	$template = array(
		'title' => 'signupquestions_signup',
		'template' => $db->escape_string($template_content),
		'sid' => '-1',
		'version' => '140',
		'dateline' => TIME_NOW
	);
	$db->insert_query('templates', $template);


	require_once MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('member_register', '#{\$referrer}#', '{\$referrer}<!-- SignupQuestions -->{$question}<!-- /SignupQuestions -->');
}

/**
 * Deactivate
 *
 */
function signupquestions_deactivate()
{
	global $db;

	$db->delete_query('templates', 'title = "signupquestions_signup"');

	require_once MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('member_register', '#\<!--\sSignupQuestions\s--\>\{\$([a-zA-Z_]+)?\}<!--\s/SignupQuestions\s--\>#is', '', 0);
}

/**
 * Get another question with AJAX
 *
 */
function signupquestions_ajax()
{
	global $mybb, $lang, $db;
	
	$lang->load('signupquestions');
	
	// Load another question
	if($mybb->input['action'] === 'newquestion')
	{
		// Get the new question	
		$qid = (int) $mybb->input['qid'];
		$question = Question::get_random_question($qid);
		
		// Set the array
		$data = array(
			'qid'        => $question['qid'],
			'question'   => $question['question']
		);
		
		echo json_encode($data);
		exit;
	}
	
	// Check if an answer is correct
	if($mybb->input['action'] === 'validatequestion')
	{
		// Set header for validator
		if($lang->settings['charset'])
		{
			$charset = $lang->settings['charset'];
		}
		else
		{
			$charset = "UTF-8";
		}
		
		header('Content-type: text/xml; charset='.$charset);
		
		if(Question::validate_answer((int) $mybb->input['qid'], $mybb->input['value']))
		{
			echo '<success>'.$lang->sq_ajax_success.'</success>';
			exit;
		}
		
		echo '<fail>'.$lang->signup_question_wrong.'</fail>';
		exit;
	}
}

/**
 * Add the signup validator to the page
 *
 */
function signupquestions_validator()
{
	global $validator_extra, $lang;
	$validator_extra .= "\tregValidator.register('answer', 'notEmpty', {failure_message:'{$lang->sq_answer_required}'});\n\tregValidator.register('answer', 'ajax', {url:'xmlhttp.php?action=validatequestion', extra_body: 'qid', loading_message:'{$lang->sq_ajax_checking}', failure_message:'{$lang->signup_question_wrong}'});\n";
}

/**
 * Show the question on the signup page
 *
 */
function signupquestions_signup()
{
	global $mybb, $lang, $db, $templates, $question, $theme;

	$lang->load('signupquestions');
	
	$qid = (int) $mybb->input['qid'];
	
	if($qid !== 0)
	{
		$question = Question::get_question($qid);
	}
	else
	{
		$question = Question::get_random_question();
	}
	
	if(!$question)
	{
		return false;
	}

	eval("\$question = \"".$templates->get("signupquestions_signup")."\";");
}

/**
 * Validate the answer when the user sends the registration form
 *
 * @return bool
 */
function signupquestions_validate($handler)
{
	global $mybb, $lang, $db;

	$errors = array();

	// Only check if the request is from the members page
	if(!strpos($_SERVER['REQUEST_URI'], 'member.php'))
	{
		return false;
	}

	// If there is no question, don't do nothing
	$query = $db->simple_select('signup_questions', 'COUNT(qid) AS quantity');
	if($db->fetch_field($query, 'quantity') == 0)
	{
		return false;
	}

	$lang->load('signupquestions');

	// If the answer was not sent in the post data
	if(!$mybb->input['qid'] OR !$mybb->input['answer'])
	{
		$handler->set_error($lang->signup_question_missing);
		return false;
	}
	
	// If the answer was sent, but it was incorrect... show error
	if(!Question::validate_answer((int) $mybb->input['qid'], $mybb->input['answer']))
	{
		$handler->set_error($lang->signup_question_wrong);
		return false;
	}
}

/**
 * Add the tabs to the user module in the ACP
 *
 * @return array
 */
function signupquestions_load_menu($sub_menu)
{
	global $lang;
	
	$lang->load('signupquestions');
	$sub_menu['80'] = array('id' => 'questions', 'title' => $lang->signup_questions, 'link' => "index.php?module=user-questions");

	return $sub_menu;
}

/**
 * Action habdler
 *
 * @return array
 */
function signupquestions_handler($actions)
{
	global $action;

	// Add the function handler
	$actions['questions'] = array('active' => 'questions', 'file' => '');
	
	return $actions;
}

/**
 * Load the ACP
 *
 * @return void
 */
function signupquestions_load_acp()
{
	global $mybb, $page;

	if($page->active_module != 'users' && $page->active_action != 'questions')
	{
		return false;
	}

	// Add navigation
	$page->add_breadcrumb_item($lang->signup_questions);

	if($mybb->input['action'] == 'new')
	{
		signupquestions_acp_new();
	}
	elseif($mybb->input['action'] == 'edit')
	{
		signupquestions_acp_edit();
	}
	elseif($mybb->input['action'] == 'reset_stats')
	{
		signupquestions_acp_reset_stats();
	}
	elseif($mybb->input['action'] == 'delete')
	{
		signupquestions_acp_delete();
	}
	else
	{
		signupquestions_acp_index();
	}

	exit;
}

/**
 * ACP: Index
 *
 * @return void
 */
function signupquestions_acp_index()
{
	global $db, $page, $lang, $mybb;

	// Print header
	$page->output_header($lang->signup_questions);

	// Print tabs
	$page->output_nav_tabs(sq_get_tabs(), 'manage');

	// Create the table
	$table = new Table;
	$table->construct_header($lang->sq_id, array('width' => '30', 'class' => 'align_center'));
	$table->construct_header($lang->sq_question, array('class' => 'align_center'));
	$table->construct_header($lang->sq_answer, array('class' => 'align_center'));
	$table->construct_header($lang->sq_stats, array('width' => '150', 'class' => 'align_center'));
	$table->construct_header($lang->sq_manage, array('width' => '210', 'colspan' => '3', 'class' => 'align_center'));

	// Get all the questions
	$query = $db->simple_select('signup_questions', '*');

	if($db->num_rows($query) === 0)
	{
		$table->construct_cell($lang->sq_no_questions_found, array('colspan' => 4, 'class' => 'align_center'));
		$table->construct_row();
	}

	while($row = $db->fetch_array($query))
	{
		if($row['fail_count'] == 0 && $row['success_count'] == 0)
		{
			$stats = 'Not available yet';
		}
		else
		{
			$stats = '
				<small>
					<span style="color: green;">' . $lang->ua_success . ' ' . $row['success_count'] . ' time(s)</span><br />
					<span style="color: red;">' . $lang->ua_error . ' ' . $row['fail_count'] . ' time(s)</span>
				</small>
			';
		}
		
		$answers = '<ul>';
		foreach(explode("\n", $row['answer']) as $answer)
		{
			$answers .= '<li>'.$answer.'</li>';
		}
		$answers .= '</ul>';

		$table->construct_cell($row['qid'], array('width' => '30', 'class' => 'align_center'));
		$table->construct_cell($row['question']);
		$table->construct_cell($answers);
		$table->construct_cell($stats);
		$table->construct_cell('<a href="index.php?module=user-questions&action=reset_stats&qid=' . $row['qid'] . '&my_post_key=' . $mybb->post_code . '">'.$lang->sq_reset_stats.'</a>', array('width' => '90', 'class' => 'align_center'));
		$table->construct_cell('<a href="index.php?module=user-questions&action=edit&qid=' . $row['qid'] . '">Edit</a>', array('width' => '60', 'class' => 'align_center'));
		$table->construct_cell('<a href="index.php?module=user-questions&action=delete&qid=' . $row['qid'] . '&my_post_key=' . $mybb->post_code . '">Remove</a>', array('width' => '60', 'class' => 'align_center'));

		$table->construct_row();
	}

	$table->output($lang->questions);

	// Print footer
	$page->output_footer();
}

/**
 * ACP: New
 *
 * @return void
 */
function signupquestions_acp_new()
{
	global $db, $lang, $page, $mybb;

	// Print header
	$page->output_header($lang->signup_questions);

	// Print tabs
	$page->output_nav_tabs(sq_get_tabs(), 'new');

	// Default vars
	$question = null;

	// If post is received, try to create the new question
	if($mybb->request_method === 'post')
	{
		$valid = Question::validate();
		$question = Question::$result;

		// We don't need the QID as this is form creating a new question
		unset($question['qid']);

		if($valid)
		{
			// Save in the databse
			$query = $db->insert_query('signup_questions', $question);

			if($query)
			{
				flash_message($lang->sq_new_success, 'success');
				admin_redirect('index.php?module=user-questions');
			}

			$errors[] = $lang->sq_error_adding;
		}
	}

	// Show the form
	Question::$button = $lang->sq_new_button;
	Question::display_form($question, 'index.php?module=user-questions&action=new');

	// Print footer
	$page->output_footer();
}

/**
 * ACP: Reset stats
 *
 */
function signupquestions_acp_reset_stats()
{
	global $db, $mybb, $lang;

	// Check security code
	if(!verify_post_check($mybb->input['my_post_key']))
	{
		flash_message($lang->sq_invalid_post_key, 'error');
		admin_redirect("index.php?module=user-questions");
	}

	// Check if the question exists
	$query = $db->simple_select('signup_questions', '*', 'qid = ' . (int) $mybb->input['qid'], array('limit' => 1));

	if($db->num_rows($query) === 0)
	{
		flash_message($lang->sq_invalid_id, 'error');
		admin_redirect("index.php?module=user-questions");
	}

	// Delete the question
	$data = array(
		'success_count'   => 0,
		'fail_count'      => 0
	);
	$query = $db->update_query('signup_questions', $data, 'qid = ' . (int) $mybb->input['qid']);

	flash_message($lang->sq_reset_stats_success, 'success');
	admin_redirect("index.php?module=user-questions");
}

/**
 * ACP: Edit
 *
 * @return void
 */
function signupquestions_acp_edit()
{
	global $page, $mybb, $db, $lang;

	// Print header
	$page->output_header($lang->signup_questions);

	// Print tabs
	$page->output_nav_tabs(sq_get_tabs(), 'manage');

	// First check if the ID exists
	if(empty($mybb->input['qid']))
	{
		flash_message($lang->sq_invalid_id, 'error');
		admin_redirect('index.php?module=user-questions');
	}

	// Then check if the ID is invalid
	$query = $db->simple_select('signup_questions', '*', 'qid = ' . (int) $mybb->input['qid'], array('limit' => 1));
	if($db->num_rows($query) === 0)
	{
		flash_message($lang->sq_edit_invalid_id, 'error');
		admin_redirect('index.php?module=user-questions');
	}

	// Set var
	$question = $db->fetch_array($query);

	// If post is received, try to create the new question
	if($mybb->request_method === 'post')
	{
		$valid = Question::validate(true);
		$question = Question::$result;

		if($valid)
		{
			// Don't update the id
			$qid = $question['qid'];
			unset($question['qid']);

			// Save in the databse
			$query = $db->update_query('signup_questions', $question, 'qid = ' . $qid);

			if($query)
			{
				flash_message($lang->sq_edit_success, 'success');
				admin_redirect('index.php?module=user-questions');
			}

			$errors[] = $lang->sq_error_adding;
		}
	}

	// Show the form
	Question::$button = $lang->sq_edit_button;
	Question::display_form($question, 'index.php?module=user-questions&action=edit&qid=' . (int) $mybb->input['qid']);

	// Print footer
	$page->output_footer();
}

/**
 * ACP: Delete
 *
 * @return void
 */
function signupquestions_acp_delete()
{
	global $db, $mybb, $lang;

	// Check security code
	if(!verify_post_check($mybb->input['my_post_key']))
	{
		flash_message($lang->sq_invalid_post_key, 'error');
		admin_redirect("index.php?module=user-questions");
	}

	// Check if the question exists
	$query = $db->simple_select('signup_questions', '*', 'qid = ' . (int) $mybb->input['qid'], array('limit' => 1));

	if($db->num_rows($query) === 0)
	{
		flash_message($lang->sq_invalid_id, 'error');
		admin_redirect("index.php?module=user-questions");
	}

	// Delete the question
	$query = $db->delete_query('signup_questions', 'qid = ' . (int) $mybb->input['qid']);

	flash_message($lang->sq_delete_success, 'success');
	admin_redirect("index.php?module=user-questions");


}

/**
 * Get the ACP tabs
 *
 * @return array
 */
function sq_get_tabs()
{
	global $lang;

	$tabs = array();

	$tabs['manage'] = array(
		'title' => $lang->sq_manage_questions,
		'link' => "index.php?module=user-questions",
		'description' => $lang->sq_manage_questions_description
	);

	$tabs['new'] = array(
		'title' => $lang->sq_new_question,
		'link' => "index.php?module=user-questions&action=new",
		'description' => $lang->sq_new_question_description
	);

	return $tabs;
}

/**
 * Question Class
 *
 */
class Question {

	/**
	 * Button text
	 *
	 */
	public static $button = '';

	/**
	 * Generated by self::validate()
	 * Contains the question data extracted from the POST (Already escaped)
	 *
	 */
	public static $result = null;

	/**
	 * Generated by self::validation()
	 * Contains all the validation errors
	 *
	 */
	public static $errors = array();

	/**
	 * Display the form for the ACP. Used in the following pages: new, edit
	 *
	 * @param arrray $data Question data
	 * @param string $url Form action
	 */
	public static function display_form($data = array(), $url)
	{
		global $page, $lang;

		$default = array(
			'question' => '',
			'answer' => '',
			'type' => '',
			'options' => ''
		);

		if(!is_array($data))
		{
			$data = array();
		}

		$data = array_merge($default, $data);

		$form = new Form($url, 'post');

		if(count(self::$errors) > 0)
		{
			$page->output_inline_error(self::$errors);
		}

		$form_container = new FormContainer($lang->sq_new_question);

		$form_container->output_row($lang->sq_question, $lang->sq_question_description, $form->generate_text_box('question', $data['question'], array('id' => 'question')));
		$form_container->output_row($lang->sq_answer, $lang->sq_answer_description, $form->generate_text_area('answer', $data['answer'], array('id' => 'answer')));

		$form_container->end();

		// Generate buttons, print container, end form
		$buttons[] = $form->generate_submit_button(self::$button);
		$form->output_submit_wrapper($buttons);
		$form->end();
	}

	/**
	 * Validate a new/edited question from the input data
	 * Access self::$result to get an array containing all the data
	 *
	 * @param bool $idrequired Send true if the ID is required on the input (Ex: when editing a question)
	 * @return bool
	 */
	public static function validate($idrequired = false)
	{
		global $mybb, $lang, $db;

		$data = array(
			'qid' => (int) $mybb->input['qid'],
			'question' => $db->escape_string($mybb->input['question']),
			'answer' => $db->escape_string(trim(strtolower($mybb->input['answer'])))
		);

		self::$result = $data;

		if($idrequired && empty($mybb->input['qid']))
		{
			self::$errors[] = $lang->sq_error_missing_id;
		}

		if(empty($mybb->input['question']))
		{
			self::$errors[] = $lang->sq_error_missing_question;
		}

		if(empty($mybb->input['answer']))
		{
			self::$errors[] = $lang->sq_error_missing_answer;
		}

		if(count(self::$errors) > 0)
		{
			return false;
		}

		return true;
	}

	/**
	 * Pass the question ID and the answer provided by the user to check if it's correct or not
	 *
	 * @param string $qid The question ID
	 * @param string $answer The answer provided by the user
	 * @return bool
	 */
	public static function validate_answer($qid, $answer)
	{
		global $mybb, $db;
		
		// Filter answer
		$answer = trim(strtolower($answer));
		
		// Get the question from the db
		$query = $db->query('SELECT * FROM '.TABLE_PREFIX.'signup_questions WHERE qid = "'.$qid.'" ORDER BY rand() LIMIT 1');
			
		// If the question dosn't exists, return false
		if($db->num_rows($query) == 0)
		{
			return false;
		}
		
		// Get correct answers
		$question = $db->fetch_array($query);
		$answers = explode("\n", $question['answer']);
		$answers = array_map('strtolower', $answers);
		$answers = array_map('trim', $answers);

		if(!in_array($answer, $answers))
		{
			static::stats_plus_one($qid, 'fail');			
			return false;
		}

		static::stats_plus_one($qid, 'success');
		return true;
	}
	
	/**
	 * Add 1 to the question statistics
	 *
	 * @param int $qid The question ID
	 * @param string $type "success" or "fail"
	 * @return bool
	 */
	public static function stats_plus_one($qid, $type)
	{
		global $db;
		
		if($type !== 'success' AND $type !== 'fail')
		{
			return false;
		}
		
		$db->query('UPDATE '.TABLE_PREFIX.'signup_questions SET '.$type.'_count = '.$type.'_count+1 WHERE qid = '.$qid);
		return true;
	}
	
	/**
	 * Get a question from the db
	 *
	 * @param string $qid The question id.
	 * @param string $random Send true if you want a random question
	 * @return mixed
	 */
	public static function get_question($qid)
	{
		global $db;
		
		$query = $db->query('SELECT * FROM '.TABLE_PREFIX.'signup_questions WHERE qid = '.$qid.' LIMIT 1');
		return $db->fetch_array($query);
	}
	
	/**
	 * Get random question
	 *
	 * @param int $qid Send an uid to exclude from the query
	 * @return mixed
	 */
	public static function get_random_question($qid = null)
	{
		global $db;
		
		$sql = 'SELECT * FROM '.TABLE_PREFIX.'signup_questions';
		
		if($qid !== null)
		{
			$sql .= ' WHERE qid != '.$qid;
		}
		
		$sql .= ' ORDER BY rand() LIMIT 1';
		
		$query = $db->query($sql);
		return $db->fetch_array($query);
	}	

}

/* End of file signupquestions.php */
http://mods.mybb.com/reviews/signup-questions

See if what Masakari said solves it.
(2013-01-21, 07:39 PM)Irreligious Wrote: [ -> ]http://mods.mybb.com/reviews/signup-questions

See if what Masakari said solves it.

thanks Smile