Blame Extras/phpBB/3.0.4/includes/acp/acp_bots.php

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package acp
4c79b5
* @version $Id: acp_bots.php 8479 2008-03-29 00:22:48Z naderman $
4c79b5
* @copyright (c) 2005 phpBB Group
4c79b5
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
4c79b5
*
4c79b5
*/
4c79b5
4c79b5
/**
4c79b5
* @ignore
4c79b5
*/
4c79b5
if (!defined('IN_PHPBB'))
4c79b5
{
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* @package acp
4c79b5
*/
4c79b5
class acp_bots
4c79b5
{
4c79b5
	var $u_action;
4c79b5
4c79b5
	function main($id, $mode)
4c79b5
	{
4c79b5
		global $config, $db, $user, $auth, $template, $cache;
4c79b5
		global $phpbb_root_path, $phpbb_admin_path, $phpEx, $table_prefix;
4c79b5
4c79b5
		$action = request_var('action', '');
4c79b5
		$submit = (isset($_POST['submit'])) ? true : false;
4c79b5
		$mark	= request_var('mark', array(0));
4c79b5
		$bot_id	= request_var('id', 0);
4c79b5
4c79b5
		if (isset($_POST['add']))
4c79b5
		{
4c79b5
			$action = 'add';
4c79b5
		}
4c79b5
4c79b5
		$error = array();
4c79b5
4c79b5
		$user->add_lang('acp/bots');
4c79b5
		$this->tpl_name = 'acp_bots';
4c79b5
		$this->page_title = 'ACP_BOTS';
4c79b5
		$form_key = 'acp_bots';
4c79b5
		add_form_key($form_key);
4c79b5
4c79b5
		if ($submit && !check_form_key($form_key))
4c79b5
		{
4c79b5
			$error[] = $user->lang['FORM_INVALID'];
4c79b5
		}
4c79b5
4c79b5
		// User wants to do something, how inconsiderate of them!
4c79b5
		switch ($action)
4c79b5
		{
4c79b5
			case 'activate':
4c79b5
				if ($bot_id || sizeof($mark))
4c79b5
				{
4c79b5
					$sql_id = ($bot_id) ? " = $bot_id" : ' IN (' . implode(', ', $mark) . ')';
4c79b5
4c79b5
					$sql = 'UPDATE ' . BOTS_TABLE . "
4c79b5
						SET bot_active = 1
4c79b5
						WHERE bot_id $sql_id";
4c79b5
					$db->sql_query($sql);
4c79b5
				}
4c79b5
4c79b5
				$cache->destroy('_bots');
4c79b5
			break;
4c79b5
4c79b5
			case 'deactivate':
4c79b5
				if ($bot_id || sizeof($mark))
4c79b5
				{
4c79b5
					$sql_id = ($bot_id) ? " = $bot_id" : ' IN (' . implode(', ', $mark) . ')';
4c79b5
4c79b5
					$sql = 'UPDATE ' . BOTS_TABLE . "
4c79b5
						SET bot_active = 0
4c79b5
						WHERE bot_id $sql_id";
4c79b5
					$db->sql_query($sql);
4c79b5
				}
4c79b5
4c79b5
				$cache->destroy('_bots');
4c79b5
			break;
4c79b5
4c79b5
			case 'delete':
4c79b5
				if ($bot_id || sizeof($mark))
4c79b5
				{
4c79b5
					if (confirm_box(true))
4c79b5
					{
4c79b5
						// We need to delete the relevant user, usergroup and bot entries ...
4c79b5
						$sql_id = ($bot_id) ? " = $bot_id" : ' IN (' . implode(', ', $mark) . ')';
4c79b5
4c79b5
						$sql = 'SELECT bot_name, user_id
4c79b5
							FROM ' . BOTS_TABLE . "
4c79b5
							WHERE bot_id $sql_id";
4c79b5
						$result = $db->sql_query($sql);
4c79b5
4c79b5
						$user_id_ary = $bot_name_ary = array();
4c79b5
						while ($row = $db->sql_fetchrow($result))
4c79b5
						{
4c79b5
							$user_id_ary[] = (int) $row['user_id'];
4c79b5
							$bot_name_ary[] = $row['bot_name'];
4c79b5
						}
4c79b5
						$db->sql_freeresult($result);
4c79b5
4c79b5
						$db->sql_transaction('begin');
4c79b5
4c79b5
						$sql = 'DELETE FROM ' . BOTS_TABLE . "
4c79b5
							WHERE bot_id $sql_id";
4c79b5
						$db->sql_query($sql);
4c79b5
4c79b5
						if (sizeof($user_id_ary))
4c79b5
						{
4c79b5
							$_tables = array(USERS_TABLE, USER_GROUP_TABLE);
4c79b5
							foreach ($_tables as $table)
4c79b5
							{
4c79b5
								$sql = "DELETE FROM $table
4c79b5
									WHERE " . $db->sql_in_set('user_id', $user_id_ary);
4c79b5
								$db->sql_query($sql);
4c79b5
							}
4c79b5
						}
4c79b5
4c79b5
						$db->sql_transaction('commit');
4c79b5
4c79b5
						$cache->destroy('_bots');
4c79b5
4c79b5
						add_log('admin', 'LOG_BOT_DELETE', implode(', ', $bot_name_ary));
4c79b5
						trigger_error($user->lang['BOT_DELETED'] . adm_back_link($this->u_action));
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
4c79b5
							'mark'		=> $mark,
4c79b5
							'id'		=> $bot_id,
4c79b5
							'mode'		=> $mode,
4c79b5
							'action'	=> $action))
4c79b5
						);
4c79b5
					}
4c79b5
				}
4c79b5
			break;
4c79b5
4c79b5
			case 'edit':
4c79b5
			case 'add':
4c79b5
				include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
4c79b5
4c79b5
				$bot_row = array(
4c79b5
					'bot_name'		=> utf8_normalize_nfc(request_var('bot_name', '', true)),
4c79b5
					'bot_agent'		=> request_var('bot_agent', ''),
4c79b5
					'bot_ip'		=> request_var('bot_ip', ''),
4c79b5
					'bot_active'	=> request_var('bot_active', true),
4c79b5
					'bot_lang'		=> request_var('bot_lang', $config['default_lang']),
4c79b5
					'bot_style'		=> request_var('bot_style' , $config['default_style']),
4c79b5
				);
4c79b5
4c79b5
				if ($submit)
4c79b5
				{
4c79b5
					if (!$bot_row['bot_agent'] && !$bot_row['bot_ip'])
4c79b5
					{
4c79b5
						$error[] = $user->lang['ERR_BOT_NO_MATCHES'];
4c79b5
					}
4c79b5
			
4c79b5
					if ($bot_row['bot_ip'] && !preg_match('#^[\d\.,:]+$#', $bot_row['bot_ip']))
4c79b5
					{
4c79b5
						if (!$ip_list = gethostbynamel($bot_row['bot_ip']))
4c79b5
						{
4c79b5
							$error[] = $user->lang['ERR_BOT_NO_IP'];
4c79b5
						}
4c79b5
						else
4c79b5
						{
4c79b5
							$bot_row['bot_ip'] = implode(',', $ip_list);
4c79b5
						}
4c79b5
					}
4c79b5
					$bot_row['bot_ip'] = str_replace(' ', '', $bot_row['bot_ip']);
4c79b5
4c79b5
					// Make sure the admin is not adding a bot with an user agent similar to his one
4c79b5
					if ($bot_row['bot_agent'] && substr($user->data['session_browser'], 0, 149) === substr($bot_row['bot_agent'], 0, 149))
4c79b5
					{
4c79b5
						$error[] = $user->lang['ERR_BOT_AGENT_MATCHES_UA'];
4c79b5
					}
4c79b5
					
4c79b5
					$bot_name = false;
4c79b5
					if ($bot_id)
4c79b5
					{
4c79b5
						$sql = 'SELECT u.username_clean
4c79b5
							FROM ' . BOTS_TABLE . ' b, ' . USERS_TABLE . " u
4c79b5
							WHERE b.bot_id = $bot_id
4c79b5
								AND u.user_id = b.user_id";
4c79b5
						$result = $db->sql_query($sql);
4c79b5
						$row = $db->sql_fetchrow($result);
4c79b5
						$db->sql_freeresult($result);
4c79b5
4c79b5
						if (!$bot_row)
4c79b5
						{
4c79b5
							$error[] = $user->lang['NO_BOT'];
4c79b5
						}
4c79b5
						else
4c79b5
						{
4c79b5
							$bot_name = $row['username_clean'];
4c79b5
						}
4c79b5
					}
4c79b5
					if (!$this->validate_botname($bot_row['bot_name'], $bot_name))
4c79b5
					{
4c79b5
						$error[] = $user->lang['BOT_NAME_TAKEN'];
4c79b5
					}
4c79b5
					
4c79b5
					if (!sizeof($error))
4c79b5
					{
4c79b5
						// New bot? Create a new user and group entry
4c79b5
						if ($action == 'add')
4c79b5
						{
4c79b5
							$sql = 'SELECT group_id, group_colour
4c79b5
								FROM ' . GROUPS_TABLE . "
4c79b5
								WHERE group_name = 'BOTS'
4c79b5
									AND group_type = " . GROUP_SPECIAL;
4c79b5
							$result = $db->sql_query($sql);
4c79b5
							$group_row = $db->sql_fetchrow($result);
4c79b5
							$db->sql_freeresult($result);
4c79b5
4c79b5
							if (!$group_row)
4c79b5
							{
4c79b5
								trigger_error($user->lang['NO_BOT_GROUP'] . adm_back_link($this->u_action . "&id=$bot_id&action=$action"), E_USER_WARNING);
4c79b5
							}
4c79b5
						
4c79b5
4c79b5
							$user_id = user_add(array(
4c79b5
								'user_type'				=> (int) USER_IGNORE,
4c79b5
								'group_id'				=> (int) $group_row['group_id'],
4c79b5
								'username'				=> (string) $bot_row['bot_name'],
4c79b5
								'user_regdate'			=> time(),
4c79b5
								'user_password'			=> '',
4c79b5
								'user_colour'			=> (string) $group_row['group_colour'],
4c79b5
								'user_email'			=> '',
4c79b5
								'user_lang'				=> (string) $bot_row['bot_lang'],
4c79b5
								'user_style'			=> (int) $bot_row['bot_style'],
4c79b5
								'user_allow_massemail'	=> 0,
4c79b5
							));
4c79b5
	
4c79b5
							$sql = 'INSERT INTO ' . BOTS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
4c79b5
								'user_id'		=> (int) $user_id,
4c79b5
								'bot_name'		=> (string) $bot_row['bot_name'],
4c79b5
								'bot_active'	=> (int) $bot_row['bot_active'],
4c79b5
								'bot_agent'		=> (string) $bot_row['bot_agent'],
4c79b5
								'bot_ip'		=> (string) $bot_row['bot_ip'])
4c79b5
							);
4c79b5
							$db->sql_query($sql);
4c79b5
	
4c79b5
							$log = 'ADDED';
4c79b5
						}
4c79b5
						else if ($bot_id)
4c79b5
						{
4c79b5
							$sql = 'SELECT user_id, bot_name
4c79b5
								FROM ' . BOTS_TABLE . "
4c79b5
								WHERE bot_id = $bot_id";
4c79b5
							$result = $db->sql_query($sql);
4c79b5
							$row = $db->sql_fetchrow($result);
4c79b5
							$db->sql_freeresult($result);
4c79b5
4c79b5
							if (!$row)
4c79b5
							{
4c79b5
								trigger_error($user->lang['NO_BOT'] . adm_back_link($this->u_action . "&id=$bot_id&action=$action"), E_USER_WARNING);
4c79b5
							}
4c79b5
4c79b5
							$sql_ary = array(
4c79b5
								'user_style'	=> (int) $bot_row['bot_style'],
4c79b5
								'user_lang'		=> (string) $bot_row['bot_lang'],
4c79b5
							);
4c79b5
4c79b5
							if ($bot_row['bot_name'] !== $row['bot_name'])
4c79b5
							{
4c79b5
								$sql_ary['username'] = (string) $bot_row['bot_name'];
4c79b5
								$sql_ary['username_clean'] = (string) utf8_clean_string($bot_row['bot_name']);
4c79b5
							}
4c79b5
4c79b5
							$sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . " WHERE user_id = {$row['user_id']}";
4c79b5
							$db->sql_query($sql);
4c79b5
4c79b5
							$sql = 'UPDATE ' . BOTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
4c79b5
								'bot_name'		=> (string) $bot_row['bot_name'],
4c79b5
								'bot_active'	=> (int) $bot_row['bot_active'],
4c79b5
								'bot_agent'		=> (string) $bot_row['bot_agent'],
4c79b5
								'bot_ip'		=> (string) $bot_row['bot_ip'])
4c79b5
							) . " WHERE bot_id = $bot_id";
4c79b5
							$db->sql_query($sql);
4c79b5
4c79b5
							// Updated username?
4c79b5
							if ($bot_row['bot_name'] !== $row['bot_name'])
4c79b5
							{
4c79b5
								user_update_name($row['bot_name'], $bot_row['bot_name']);
4c79b5
							}
4c79b5
4c79b5
							$log = 'UPDATED';
4c79b5
						}
4c79b5
						
4c79b5
						$cache->destroy('_bots');
4c79b5
						
4c79b5
						add_log('admin', 'LOG_BOT_' . $log, $bot_row['bot_name']);
4c79b5
						trigger_error($user->lang['BOT_' . $log] . adm_back_link($this->u_action));
4c79b5
					
4c79b5
					}
4c79b5
				}
4c79b5
				else if ($bot_id)
4c79b5
				{
4c79b5
					$sql = 'SELECT b.*, u.user_lang, u.user_style
4c79b5
						FROM ' . BOTS_TABLE . ' b, ' . USERS_TABLE . " u
4c79b5
						WHERE b.bot_id = $bot_id
4c79b5
							AND u.user_id = b.user_id";
4c79b5
					$result = $db->sql_query($sql);
4c79b5
					$bot_row = $db->sql_fetchrow($result);
4c79b5
					$db->sql_freeresult($result);
4c79b5
4c79b5
					if (!$bot_row)
4c79b5
					{
4c79b5
						trigger_error($user->lang['NO_BOT'] . adm_back_link($this->u_action . "&id=$bot_id&action=$action"), E_USER_WARNING);
4c79b5
					}
4c79b5
4c79b5
					$bot_row['bot_lang'] = $bot_row['user_lang'];
4c79b5
					$bot_row['bot_style'] = $bot_row['user_style'];
4c79b5
					unset($bot_row['user_lang'], $bot_row['user_style']);
4c79b5
				}
4c79b5
4c79b5
				$s_active_options = '';
4c79b5
				$_options = array('0' => 'NO', '1' => 'YES');
4c79b5
				foreach ($_options as $value => $lang)
4c79b5
				{
4c79b5
					$selected = ($bot_row['bot_active'] == $value) ? ' selected="selected"' : '';
4c79b5
					$s_active_options .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$lang] . '</option>';
4c79b5
				}
4c79b5
4c79b5
				$style_select = style_select($bot_row['bot_style'], true);
4c79b5
				$lang_select = language_select($bot_row['bot_lang']);
4c79b5
4c79b5
				$l_title = ($action == 'edit') ? 'EDIT' : 'ADD';
4c79b5
4c79b5
				$template->assign_vars(array(
4c79b5
					'L_TITLE'		=> $user->lang['BOT_' . $l_title],
4c79b5
					'U_ACTION'		=> $this->u_action . "&id=$bot_id&action=$action",
4c79b5
					'U_BACK'		=> $this->u_action,
4c79b5
					'ERROR_MSG'		=> (sizeof($error)) ? implode('
', $error) : '',
4c79b5
					
4c79b5
					'BOT_NAME'		=> $bot_row['bot_name'],
4c79b5
					'BOT_IP'		=> $bot_row['bot_ip'],
4c79b5
					'BOT_AGENT'		=> $bot_row['bot_agent'],
4c79b5
					
4c79b5
					'S_EDIT_BOT'		=> true,
4c79b5
					'S_ACTIVE_OPTIONS'	=> $s_active_options,
4c79b5
					'S_STYLE_OPTIONS'	=> $style_select,
4c79b5
					'S_LANG_OPTIONS'	=> $lang_select,
4c79b5
					'S_ERROR'			=> (sizeof($error)) ? true : false,
4c79b5
					)
4c79b5
				);
4c79b5
4c79b5
				return;
4c79b5
4c79b5
			break;
4c79b5
		}
4c79b5
4c79b5
		$s_options = '';
4c79b5
		$_options = array('activate' => 'BOT_ACTIVATE', 'deactivate' => 'BOT_DEACTIVATE', 'delete' => 'DELETE');
4c79b5
		foreach ($_options as $value => $lang)
4c79b5
		{
4c79b5
			$s_options .= '<option value="' . $value . '">' . $user->lang[$lang] . '</option>';
4c79b5
		}
4c79b5
4c79b5
		$template->assign_vars(array(
4c79b5
			'U_ACTION'		=> $this->u_action,
4c79b5
			'S_BOT_OPTIONS'	=> $s_options)
4c79b5
		);
4c79b5
4c79b5
		$sql = 'SELECT b.bot_id, b.bot_name, b.bot_active, u.user_lastvisit
4c79b5
			FROM ' . BOTS_TABLE . ' b, ' . USERS_TABLE . ' u
4c79b5
			WHERE u.user_id = b.user_id
4c79b5
			ORDER BY u.user_lastvisit DESC, b.bot_name ASC';
4c79b5
		$result = $db->sql_query($sql);
4c79b5
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$active_lang = (!$row['bot_active']) ? 'BOT_ACTIVATE' : 'BOT_DEACTIVATE';
4c79b5
			$active_value = (!$row['bot_active']) ? 'activate' : 'deactivate';
4c79b5
4c79b5
			$template->assign_block_vars('bots', array(
4c79b5
				'BOT_NAME'		=> $row['bot_name'],
4c79b5
				'BOT_ID'		=> $row['bot_id'],
4c79b5
				'LAST_VISIT'	=> ($row['user_lastvisit']) ? $user->format_date($row['user_lastvisit']) : $user->lang['BOT_NEVER'],
4c79b5
4c79b5
				'U_ACTIVATE_DEACTIVATE'	=> $this->u_action . "&id={$row['bot_id']}&action=$active_value",
4c79b5
				'L_ACTIVATE_DEACTIVATE'	=> $user->lang[$active_lang],
4c79b5
				'U_EDIT'				=> $this->u_action . "&id={$row['bot_id']}&action=edit",
4c79b5
				'U_DELETE'				=> $this->u_action . "&id={$row['bot_id']}&action=delete")
4c79b5
			);
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
	
4c79b5
	/**
4c79b5
	* Validate bot name against username table
4c79b5
	*/
4c79b5
	function validate_botname($newname, $oldname = false)
4c79b5
	{
4c79b5
		global $db;
4c79b5
4c79b5
		if ($oldname && utf8_clean_string($newname) === $oldname)
4c79b5
		{
4c79b5
			return true;
4c79b5
		}
4c79b5
4c79b5
		// Admins might want to use names otherwise forbidden, thus we only check for duplicates.
4c79b5
		$sql = 'SELECT username
4c79b5
			FROM ' . USERS_TABLE . "
4c79b5
			WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($newname)) . "'";
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		$row = $db->sql_fetchrow($result);
4c79b5
		$db->sql_freeresult($result);
4c79b5
		
4c79b5
		return ($row) ? false : true;
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
?>