includes/acp/acp_bots.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/acp/acp_bots.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,418 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package acp
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* @package acp
    1.24 +*/
    1.25 +class acp_bots
    1.26 +{
    1.27 +	var $u_action;
    1.28 +
    1.29 +	function main($id, $mode)
    1.30 +	{
    1.31 +		global $config, $db, $user, $auth, $template, $cache;
    1.32 +		global $phpbb_root_path, $phpbb_admin_path, $phpEx, $table_prefix;
    1.33 +
    1.34 +		$action = request_var('action', '');
    1.35 +		$submit = (isset($_POST['submit'])) ? true : false;
    1.36 +		$mark	= request_var('mark', array(0));
    1.37 +		$bot_id	= request_var('id', 0);
    1.38 +
    1.39 +		if (isset($_POST['add']))
    1.40 +		{
    1.41 +			$action = 'add';
    1.42 +		}
    1.43 +
    1.44 +		$error = array();
    1.45 +
    1.46 +		$user->add_lang('acp/bots');
    1.47 +		$this->tpl_name = 'acp_bots';
    1.48 +		$this->page_title = 'ACP_BOTS';
    1.49 +		$form_key = 'acp_bots';
    1.50 +		add_form_key($form_key);
    1.51 +
    1.52 +		if ($submit && !check_form_key($form_key))
    1.53 +		{
    1.54 +			$error[] = $user->lang['FORM_INVALID'];
    1.55 +		}
    1.56 +
    1.57 +		// User wants to do something, how inconsiderate of them!
    1.58 +		switch ($action)
    1.59 +		{
    1.60 +			case 'activate':
    1.61 +				if ($bot_id || sizeof($mark))
    1.62 +				{
    1.63 +					$sql_id = ($bot_id) ? " = $bot_id" : ' IN (' . implode(', ', $mark) . ')';
    1.64 +
    1.65 +					$sql = 'UPDATE ' . BOTS_TABLE . "
    1.66 +						SET bot_active = 1
    1.67 +						WHERE bot_id $sql_id";
    1.68 +					$db->sql_query($sql);
    1.69 +				}
    1.70 +
    1.71 +				$cache->destroy('_bots');
    1.72 +			break;
    1.73 +
    1.74 +			case 'deactivate':
    1.75 +				if ($bot_id || sizeof($mark))
    1.76 +				{
    1.77 +					$sql_id = ($bot_id) ? " = $bot_id" : ' IN (' . implode(', ', $mark) . ')';
    1.78 +
    1.79 +					$sql = 'UPDATE ' . BOTS_TABLE . "
    1.80 +						SET bot_active = 0
    1.81 +						WHERE bot_id $sql_id";
    1.82 +					$db->sql_query($sql);
    1.83 +				}
    1.84 +
    1.85 +				$cache->destroy('_bots');
    1.86 +			break;
    1.87 +
    1.88 +			case 'delete':
    1.89 +				if ($bot_id || sizeof($mark))
    1.90 +				{
    1.91 +					if (confirm_box(true))
    1.92 +					{
    1.93 +						// We need to delete the relevant user, usergroup and bot entries ...
    1.94 +						$sql_id = ($bot_id) ? " = $bot_id" : ' IN (' . implode(', ', $mark) . ')';
    1.95 +
    1.96 +						$sql = 'SELECT bot_name, user_id
    1.97 +							FROM ' . BOTS_TABLE . "
    1.98 +							WHERE bot_id $sql_id";
    1.99 +						$result = $db->sql_query($sql);
   1.100 +
   1.101 +						$user_id_ary = $bot_name_ary = array();
   1.102 +						while ($row = $db->sql_fetchrow($result))
   1.103 +						{
   1.104 +							$user_id_ary[] = (int) $row['user_id'];
   1.105 +							$bot_name_ary[] = $row['bot_name'];
   1.106 +						}
   1.107 +						$db->sql_freeresult($result);
   1.108 +
   1.109 +						$db->sql_transaction('begin');
   1.110 +
   1.111 +						$sql = 'DELETE FROM ' . BOTS_TABLE . "
   1.112 +							WHERE bot_id $sql_id";
   1.113 +						$db->sql_query($sql);
   1.114 +
   1.115 +						if (sizeof($user_id_ary))
   1.116 +						{
   1.117 +							$_tables = array(USERS_TABLE, USER_GROUP_TABLE);
   1.118 +							foreach ($_tables as $table)
   1.119 +							{
   1.120 +								$sql = "DELETE FROM $table
   1.121 +									WHERE " . $db->sql_in_set('user_id', $user_id_ary);
   1.122 +								$db->sql_query($sql);
   1.123 +							}
   1.124 +						}
   1.125 +
   1.126 +						$db->sql_transaction('commit');
   1.127 +
   1.128 +						$cache->destroy('_bots');
   1.129 +
   1.130 +						add_log('admin', 'LOG_BOT_DELETE', implode(', ', $bot_name_ary));
   1.131 +						trigger_error($user->lang['BOT_DELETED'] . adm_back_link($this->u_action));
   1.132 +					}
   1.133 +					else
   1.134 +					{
   1.135 +						confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
   1.136 +							'mark'		=> $mark,
   1.137 +							'id'		=> $bot_id,
   1.138 +							'mode'		=> $mode,
   1.139 +							'action'	=> $action))
   1.140 +						);
   1.141 +					}
   1.142 +				}
   1.143 +			break;
   1.144 +
   1.145 +			case 'edit':
   1.146 +			case 'add':
   1.147 +				include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
   1.148 +
   1.149 +				$bot_row = array(
   1.150 +					'bot_name'		=> utf8_normalize_nfc(request_var('bot_name', '', true)),
   1.151 +					'bot_agent'		=> request_var('bot_agent', ''),
   1.152 +					'bot_ip'		=> request_var('bot_ip', ''),
   1.153 +					'bot_active'	=> request_var('bot_active', true),
   1.154 +					'bot_lang'		=> request_var('bot_lang', $config['default_lang']),
   1.155 +					'bot_style'		=> request_var('bot_style' , $config['default_style']),
   1.156 +				);
   1.157 +
   1.158 +				if ($submit)
   1.159 +				{
   1.160 +					if (!$bot_row['bot_agent'] && !$bot_row['bot_ip'])
   1.161 +					{
   1.162 +						$error[] = $user->lang['ERR_BOT_NO_MATCHES'];
   1.163 +					}
   1.164 +			
   1.165 +					if ($bot_row['bot_ip'] && !preg_match('#^[\d\.,:]+$#', $bot_row['bot_ip']))
   1.166 +					{
   1.167 +						if (!$ip_list = gethostbynamel($bot_row['bot_ip']))
   1.168 +						{
   1.169 +							$error[] = $user->lang['ERR_BOT_NO_IP'];
   1.170 +						}
   1.171 +						else
   1.172 +						{
   1.173 +							$bot_row['bot_ip'] = implode(',', $ip_list);
   1.174 +						}
   1.175 +					}
   1.176 +					$bot_row['bot_ip'] = str_replace(' ', '', $bot_row['bot_ip']);
   1.177 +
   1.178 +					// Make sure the admin is not adding a bot with an user agent similar to his one
   1.179 +					if ($bot_row['bot_agent'] && substr($user->data['session_browser'], 0, 149) === substr($bot_row['bot_agent'], 0, 149))
   1.180 +					{
   1.181 +						$error[] = $user->lang['ERR_BOT_AGENT_MATCHES_UA'];
   1.182 +					}
   1.183 +					
   1.184 +					$bot_name = false;
   1.185 +					if ($bot_id)
   1.186 +					{
   1.187 +						$sql = 'SELECT u.username_clean
   1.188 +							FROM ' . BOTS_TABLE . ' b, ' . USERS_TABLE . " u
   1.189 +							WHERE b.bot_id = $bot_id
   1.190 +								AND u.user_id = b.user_id";
   1.191 +						$result = $db->sql_query($sql);
   1.192 +						$row = $db->sql_fetchrow($result);
   1.193 +						$db->sql_freeresult($result);
   1.194 +
   1.195 +						if (!$bot_row)
   1.196 +						{
   1.197 +							$error[] = $user->lang['NO_BOT'];
   1.198 +						}
   1.199 +						else
   1.200 +						{
   1.201 +							$bot_name = $row['username_clean'];
   1.202 +						}
   1.203 +					}
   1.204 +					if (!$this->validate_botname($bot_row['bot_name'], $bot_name))
   1.205 +					{
   1.206 +						$error[] = $user->lang['BOT_NAME_TAKEN'];
   1.207 +					}
   1.208 +					
   1.209 +					if (!sizeof($error))
   1.210 +					{
   1.211 +						// New bot? Create a new user and group entry
   1.212 +						if ($action == 'add')
   1.213 +						{
   1.214 +							$sql = 'SELECT group_id, group_colour
   1.215 +								FROM ' . GROUPS_TABLE . "
   1.216 +								WHERE group_name = 'BOTS'
   1.217 +									AND group_type = " . GROUP_SPECIAL;
   1.218 +							$result = $db->sql_query($sql);
   1.219 +							$group_row = $db->sql_fetchrow($result);
   1.220 +							$db->sql_freeresult($result);
   1.221 +
   1.222 +							if (!$group_row)
   1.223 +							{
   1.224 +								trigger_error($user->lang['NO_BOT_GROUP'] . adm_back_link($this->u_action . "&amp;id=$bot_id&amp;action=$action"), E_USER_WARNING);
   1.225 +							}
   1.226 +						
   1.227 +
   1.228 +							$user_id = user_add(array(
   1.229 +								'user_type'				=> (int) USER_IGNORE,
   1.230 +								'group_id'				=> (int) $group_row['group_id'],
   1.231 +								'username'				=> (string) $bot_row['bot_name'],
   1.232 +								'user_regdate'			=> time(),
   1.233 +								'user_password'			=> '',
   1.234 +								'user_colour'			=> (string) $group_row['group_colour'],
   1.235 +								'user_email'			=> '',
   1.236 +								'user_lang'				=> (string) $bot_row['bot_lang'],
   1.237 +								'user_style'			=> (int) $bot_row['bot_style'],
   1.238 +								'user_allow_massemail'	=> 0,
   1.239 +							));
   1.240 +	
   1.241 +							$sql = 'INSERT INTO ' . BOTS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
   1.242 +								'user_id'		=> (int) $user_id,
   1.243 +								'bot_name'		=> (string) $bot_row['bot_name'],
   1.244 +								'bot_active'	=> (int) $bot_row['bot_active'],
   1.245 +								'bot_agent'		=> (string) $bot_row['bot_agent'],
   1.246 +								'bot_ip'		=> (string) $bot_row['bot_ip'])
   1.247 +							);
   1.248 +							$db->sql_query($sql);
   1.249 +	
   1.250 +							$log = 'ADDED';
   1.251 +						}
   1.252 +						else if ($bot_id)
   1.253 +						{
   1.254 +							$sql = 'SELECT user_id, bot_name
   1.255 +								FROM ' . BOTS_TABLE . "
   1.256 +								WHERE bot_id = $bot_id";
   1.257 +							$result = $db->sql_query($sql);
   1.258 +							$row = $db->sql_fetchrow($result);
   1.259 +							$db->sql_freeresult($result);
   1.260 +
   1.261 +							if (!$row)
   1.262 +							{
   1.263 +								trigger_error($user->lang['NO_BOT'] . adm_back_link($this->u_action . "&amp;id=$bot_id&amp;action=$action"), E_USER_WARNING);
   1.264 +							}
   1.265 +
   1.266 +							$sql_ary = array(
   1.267 +								'user_style'	=> (int) $bot_row['bot_style'],
   1.268 +								'user_lang'		=> (string) $bot_row['bot_lang'],
   1.269 +							);
   1.270 +
   1.271 +							if ($bot_row['bot_name'] !== $row['bot_name'])
   1.272 +							{
   1.273 +								$sql_ary['username'] = (string) $bot_row['bot_name'];
   1.274 +								$sql_ary['username_clean'] = (string) utf8_clean_string($bot_row['bot_name']);
   1.275 +							}
   1.276 +
   1.277 +							$sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . " WHERE user_id = {$row['user_id']}";
   1.278 +							$db->sql_query($sql);
   1.279 +
   1.280 +							$sql = 'UPDATE ' . BOTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
   1.281 +								'bot_name'		=> (string) $bot_row['bot_name'],
   1.282 +								'bot_active'	=> (int) $bot_row['bot_active'],
   1.283 +								'bot_agent'		=> (string) $bot_row['bot_agent'],
   1.284 +								'bot_ip'		=> (string) $bot_row['bot_ip'])
   1.285 +							) . " WHERE bot_id = $bot_id";
   1.286 +							$db->sql_query($sql);
   1.287 +
   1.288 +							// Updated username?
   1.289 +							if ($bot_row['bot_name'] !== $row['bot_name'])
   1.290 +							{
   1.291 +								user_update_name($row['bot_name'], $bot_row['bot_name']);
   1.292 +							}
   1.293 +
   1.294 +							$log = 'UPDATED';
   1.295 +						}
   1.296 +						
   1.297 +						$cache->destroy('_bots');
   1.298 +						
   1.299 +						add_log('admin', 'LOG_BOT_' . $log, $bot_row['bot_name']);
   1.300 +						trigger_error($user->lang['BOT_' . $log] . adm_back_link($this->u_action));
   1.301 +					
   1.302 +					}
   1.303 +				}
   1.304 +				else if ($bot_id)
   1.305 +				{
   1.306 +					$sql = 'SELECT b.*, u.user_lang, u.user_style
   1.307 +						FROM ' . BOTS_TABLE . ' b, ' . USERS_TABLE . " u
   1.308 +						WHERE b.bot_id = $bot_id
   1.309 +							AND u.user_id = b.user_id";
   1.310 +					$result = $db->sql_query($sql);
   1.311 +					$bot_row = $db->sql_fetchrow($result);
   1.312 +					$db->sql_freeresult($result);
   1.313 +
   1.314 +					if (!$bot_row)
   1.315 +					{
   1.316 +						trigger_error($user->lang['NO_BOT'] . adm_back_link($this->u_action . "&amp;id=$bot_id&amp;action=$action"), E_USER_WARNING);
   1.317 +					}
   1.318 +
   1.319 +					$bot_row['bot_lang'] = $bot_row['user_lang'];
   1.320 +					$bot_row['bot_style'] = $bot_row['user_style'];
   1.321 +					unset($bot_row['user_lang'], $bot_row['user_style']);
   1.322 +				}
   1.323 +
   1.324 +				$s_active_options = '';
   1.325 +				$_options = array('0' => 'NO', '1' => 'YES');
   1.326 +				foreach ($_options as $value => $lang)
   1.327 +				{
   1.328 +					$selected = ($bot_row['bot_active'] == $value) ? ' selected="selected"' : '';
   1.329 +					$s_active_options .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$lang] . '</option>';
   1.330 +				}
   1.331 +
   1.332 +				$style_select = style_select($bot_row['bot_style'], true);
   1.333 +				$lang_select = language_select($bot_row['bot_lang']);
   1.334 +
   1.335 +				$l_title = ($action == 'edit') ? 'EDIT' : 'ADD';
   1.336 +
   1.337 +				$template->assign_vars(array(
   1.338 +					'L_TITLE'		=> $user->lang['BOT_' . $l_title],
   1.339 +					'U_ACTION'		=> $this->u_action . "&amp;id=$bot_id&amp;action=$action",
   1.340 +					'U_BACK'		=> $this->u_action,
   1.341 +					'ERROR_MSG'		=> (sizeof($error)) ? implode('<br />', $error) : '',
   1.342 +					
   1.343 +					'BOT_NAME'		=> $bot_row['bot_name'],
   1.344 +					'BOT_IP'		=> $bot_row['bot_ip'],
   1.345 +					'BOT_AGENT'		=> $bot_row['bot_agent'],
   1.346 +					
   1.347 +					'S_EDIT_BOT'		=> true,
   1.348 +					'S_ACTIVE_OPTIONS'	=> $s_active_options,
   1.349 +					'S_STYLE_OPTIONS'	=> $style_select,
   1.350 +					'S_LANG_OPTIONS'	=> $lang_select,
   1.351 +					'S_ERROR'			=> (sizeof($error)) ? true : false,
   1.352 +					)
   1.353 +				);
   1.354 +
   1.355 +				return;
   1.356 +
   1.357 +			break;
   1.358 +		}
   1.359 +
   1.360 +		$s_options = '';
   1.361 +		$_options = array('activate' => 'BOT_ACTIVATE', 'deactivate' => 'BOT_DEACTIVATE', 'delete' => 'DELETE');
   1.362 +		foreach ($_options as $value => $lang)
   1.363 +		{
   1.364 +			$s_options .= '<option value="' . $value . '">' . $user->lang[$lang] . '</option>';
   1.365 +		}
   1.366 +
   1.367 +		$template->assign_vars(array(
   1.368 +			'U_ACTION'		=> $this->u_action,
   1.369 +			'S_BOT_OPTIONS'	=> $s_options)
   1.370 +		);
   1.371 +
   1.372 +		$sql = 'SELECT b.bot_id, b.bot_name, b.bot_active, u.user_lastvisit
   1.373 +			FROM ' . BOTS_TABLE . ' b, ' . USERS_TABLE . ' u
   1.374 +			WHERE u.user_id = b.user_id
   1.375 +			ORDER BY u.user_lastvisit DESC, b.bot_name ASC';
   1.376 +		$result = $db->sql_query($sql);
   1.377 +
   1.378 +		while ($row = $db->sql_fetchrow($result))
   1.379 +		{
   1.380 +			$active_lang = (!$row['bot_active']) ? 'BOT_ACTIVATE' : 'BOT_DEACTIVATE';
   1.381 +			$active_value = (!$row['bot_active']) ? 'activate' : 'deactivate';
   1.382 +
   1.383 +			$template->assign_block_vars('bots', array(
   1.384 +				'BOT_NAME'		=> $row['bot_name'],
   1.385 +				'BOT_ID'		=> $row['bot_id'],
   1.386 +				'LAST_VISIT'	=> ($row['user_lastvisit']) ? $user->format_date($row['user_lastvisit']) : $user->lang['BOT_NEVER'],
   1.387 +
   1.388 +				'U_ACTIVATE_DEACTIVATE'	=> $this->u_action . "&amp;id={$row['bot_id']}&amp;action=$active_value",
   1.389 +				'L_ACTIVATE_DEACTIVATE'	=> $user->lang[$active_lang],
   1.390 +				'U_EDIT'				=> $this->u_action . "&amp;id={$row['bot_id']}&amp;action=edit",
   1.391 +				'U_DELETE'				=> $this->u_action . "&amp;id={$row['bot_id']}&amp;action=delete")
   1.392 +			);
   1.393 +		}
   1.394 +		$db->sql_freeresult($result);
   1.395 +	}
   1.396 +	
   1.397 +	/**
   1.398 +	* Validate bot name against username table
   1.399 +	*/
   1.400 +	function validate_botname($newname, $oldname = false)
   1.401 +	{
   1.402 +		global $db;
   1.403 +
   1.404 +		if ($oldname && utf8_clean_string($newname) === $oldname)
   1.405 +		{
   1.406 +			return true;
   1.407 +		}
   1.408 +
   1.409 +		// Admins might want to use names otherwise forbidden, thus we only check for duplicates.
   1.410 +		$sql = 'SELECT username
   1.411 +			FROM ' . USERS_TABLE . "
   1.412 +			WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($newname)) . "'";
   1.413 +		$result = $db->sql_query($sql);
   1.414 +		$row = $db->sql_fetchrow($result);
   1.415 +		$db->sql_freeresult($result);
   1.416 +		
   1.417 +		return ($row) ? false : true;
   1.418 +	}
   1.419 +}
   1.420 +
   1.421 +?>
   1.422 \ No newline at end of file