[ Index ] |
PHP Cross Reference of phpBB 3.0 Beta 3 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * 4 * @package acp 5 * @version $Id: acp_bots.php,v 1.15 2006/10/02 15:09:43 acydburn Exp $ 6 * @copyright (c) 2005 phpBB Group 7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License 8 * 9 */ 10 11 /** 12 * @package acp 13 */ 14 class acp_bots 15 { 16 var $u_action; 17 18 function main($id, $mode) 19 { 20 global $config, $db, $user, $auth, $template, $cache; 21 global $phpbb_root_path, $phpbb_admin_path, $phpEx, $table_prefix; 22 23 $action = request_var('action', ''); 24 $submit = (isset($_POST['submit'])) ? true : false; 25 $mark = request_var('mark', array(0)); 26 $bot_id = request_var('id', 0); 27 28 if (isset($_POST['add'])) 29 { 30 $action = 'add'; 31 } 32 33 $error = array(); 34 35 $user->add_lang('acp/bots'); 36 $this->tpl_name = 'acp_bots'; 37 $this->page_title = 'ACP_BOTS'; 38 39 // User wants to do something, how inconsiderate of them! 40 switch ($action) 41 { 42 case 'activate': 43 if ($bot_id || sizeof($mark)) 44 { 45 $sql_id = ($bot_id) ? " = $bot_id" : ' IN (' . implode(', ', $mark) . ')'; 46 47 $sql = 'UPDATE ' . BOTS_TABLE . " 48 SET bot_active = 1 49 WHERE bot_id $sql_id"; 50 $db->sql_query($sql); 51 } 52 53 $cache->destroy('bots'); 54 break; 55 56 case 'deactivate': 57 if ($bot_id || sizeof($mark)) 58 { 59 $sql_id = ($bot_id) ? " = $bot_id" : ' IN (' . implode(', ', $mark) . ')'; 60 61 $sql = 'UPDATE ' . BOTS_TABLE . " 62 SET bot_active = 0 63 WHERE bot_id $sql_id"; 64 $db->sql_query($sql); 65 } 66 67 $cache->destroy('bots'); 68 break; 69 70 case 'delete': 71 if ($bot_id || sizeof($mark)) 72 { 73 // We need to delete the relevant user, usergroup and bot entries ... 74 $sql_id = ($bot_id) ? " = $bot_id" : ' IN (' . implode(', ', $mark) . ')'; 75 76 $sql = 'SELECT bot_name, user_id 77 FROM ' . BOTS_TABLE . " 78 WHERE bot_id $sql_id"; 79 $result = $db->sql_query($sql); 80 81 $user_id_ary = $bot_name_ary = array(); 82 while ($row = $db->sql_fetchrow($result)) 83 { 84 $user_id_ary[] = (int) $row['user_id']; 85 $bot_name_ary[] = $row['bot_name']; 86 } 87 $db->sql_freeresult($result); 88 89 $db->sql_transaction('begin'); 90 91 $sql = 'DELETE FROM ' . BOTS_TABLE . " 92 WHERE bot_id $sql_id"; 93 $db->sql_query($sql); 94 95 $_tables = array(USERS_TABLE, USER_GROUP_TABLE); 96 foreach ($_tables as $table) 97 { 98 $sql = "DELETE FROM $table 99 WHERE " . $db->sql_in_set('user_id', $user_id_ary); 100 $db->sql_query($sql); 101 } 102 103 $db->sql_transaction('commit'); 104 105 $cache->destroy('bots'); 106 107 add_log('admin', 'LOG_BOT_DELETE', implode(', ', $bot_name_ary)); 108 trigger_error($user->lang['BOT_DELETED'] . adm_back_link($this->u_action)); 109 } 110 break; 111 112 case 'edit': 113 case 'add': 114 include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx); 115 116 $bot_row = array( 117 'bot_name' => request_var('bot_name', '', true), 118 'bot_agent' => request_var('bot_agent', ''), 119 'bot_ip' => request_var('bot_ip', ''), 120 'bot_active' => request_var('bot_active', true), 121 'bot_lang' => request_var('bot_lang', $config['default_lang']), 122 'bot_style' => request_var('bot_style' , $config['default_style']), 123 ); 124 125 if ($submit) 126 { 127 if (!$bot_row['bot_agent'] && !$bot_row['bot_ip']) 128 { 129 $error[] = $user->lang['ERR_BOT_NO_MATCHES']; 130 } 131 132 if ($bot_row['bot_ip'] && !preg_match('#^[\d\.,:]+$#', $bot_row['bot_ip'])) 133 { 134 if (!$ip_list = gethostbynamel($bot_row['bot_ip'])) 135 { 136 $error[] = $user->lang['ERR_BOT_NO_IP']; 137 } 138 else 139 { 140 $bot_row['bot_ip'] = implode(',', $ip_list); 141 } 142 } 143 $bot_row['bot_ip'] = str_replace(' ', '', $bot_row['bot_ip']); 144 145 // Make sure the admin is not adding a bot with an user agent similar to his one 146 if ($bot_row['bot_agent'] && substr($user->data['session_browser'], 0, 149) === substr($bot_row['bot_agent'], 0, 149)) 147 { 148 $error[] = $user->lang['ERR_BOT_AGENT_MATCHES_UA']; 149 } 150 151 if (!sizeof($error)) 152 { 153 $db->sql_transaction('begin'); 154 155 // New bot? Create a new user and group entry 156 if ($action == 'add') 157 { 158 $sql = 'SELECT group_id, group_colour 159 FROM ' . GROUPS_TABLE . " 160 WHERE group_name = 'BOTS' 161 AND group_type = " . GROUP_SPECIAL; 162 $result = $db->sql_query($sql); 163 $group_row = $db->sql_fetchrow($result); 164 $db->sql_freeresult($result); 165 166 if (!$group_row) 167 { 168 trigger_error($user->lang['NO_BOT_GROUP'] . adm_back_link($this->u_action . "&id=$bot_id&action=$action"), E_USER_WARNING); 169 } 170 171 $user_id = user_add(array( 172 'user_type' => (int) USER_IGNORE, 173 'group_id' => (int) $group_row['group_id'], 174 'username' => (string) $bot_row['bot_name'], 175 'user_regdate' => time(), 176 'user_password' => '', 177 'user_colour' => (string) $group_row['group_colour'], 178 'user_email' => '', 179 'user_lang' => (string) $bot_row['bot_lang'], 180 'user_style' => (int) $bot_row['bot_style'], 181 'user_options' => 0) 182 ); 183 184 $sql = 'INSERT INTO ' . BOTS_TABLE . ' ' . $db->sql_build_array('INSERT', array( 185 'user_id' => (int) $user_id, 186 'bot_name' => (string) $bot_row['bot_name'], 187 'bot_active' => (int) $bot_row['bot_active'], 188 'bot_agent' => (string) $bot_row['bot_agent'], 189 'bot_ip' => (string) $bot_row['bot_ip']) 190 ); 191 $db->sql_query($sql); 192 193 $log = 'ADDED'; 194 } 195 else if ($bot_id) 196 { 197 $sql = 'SELECT user_id 198 FROM ' . BOTS_TABLE . " 199 WHERE bot_id = $bot_id"; 200 $result = $db->sql_query($sql); 201 $row = $db->sql_fetchrow($result); 202 $db->sql_freeresult($result); 203 204 if (!$row) 205 { 206 trigger_error($user->lang['NO_BOT'] . adm_back_link($this->u_action . "&id=$bot_id&action=$action"), E_USER_WARNING); 207 } 208 209 $sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array( 210 'user_style' => (int) $bot_row['bot_style'], 211 'user_lang' => (string) $bot_row['bot_lang']) 212 ) . " WHERE user_id = {$row['user_id']}"; 213 $db->sql_query($sql); 214 215 $sql = 'UPDATE ' . BOTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array( 216 'bot_name' => (string) $bot_row['bot_name'], 217 'bot_active' => (int) $bot_row['bot_active'], 218 'bot_agent' => (string) $bot_row['bot_agent'], 219 'bot_ip' => (string) $bot_row['bot_ip']) 220 ) . " WHERE bot_id = $bot_id"; 221 $db->sql_query($sql); 222 223 $log = 'UPDATED'; 224 } 225 226 $db->sql_transaction('commit'); 227 228 $cache->destroy('bots'); 229 230 add_log('admin', 'LOG_BOT_' . $log, $bot_row['bot_name']); 231 trigger_error($user->lang['BOT_' . $log] . adm_back_link($this->u_action . "&id=$bot_id&action=$action")); 232 } 233 } 234 else if ($bot_id) 235 { 236 $sql = 'SELECT b.*, u.user_lang, u.user_style 237 FROM ' . BOTS_TABLE . ' b, ' . USERS_TABLE . " u 238 WHERE b.bot_id = $bot_id 239 AND u.user_id = b.user_id"; 240 $result = $db->sql_query($sql); 241 $bot_row = $db->sql_fetchrow($result); 242 $db->sql_freeresult($result); 243 244 if (!$bot_row) 245 { 246 trigger_error($user->lang['NO_BOT'] . adm_back_link($this->u_action . "&id=$bot_id&action=$action"), E_USER_WARNING); 247 } 248 249 $bot_row['bot_lang'] = $bot_row['user_lang']; 250 $bot_row['bot_style'] = $bot_row['user_style']; 251 unset($bot_row['user_lang'], $bot_row['user_style']); 252 } 253 254 $s_active_options = ''; 255 $_options = array('0' => 'NO', '1' => 'YES'); 256 foreach ($_options as $value => $lang) 257 { 258 $selected = ($bot_row['bot_active'] == $value) ? ' selected="selected"' : ''; 259 $s_active_options .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$lang] . '</option>'; 260 } 261 262 $style_select = style_select($bot_row['bot_style'], true); 263 $lang_select = language_select($bot_row['bot_lang']); 264 265 $l_title = ($action == 'edit') ? 'EDIT' : 'ADD'; 266 267 $template->assign_vars(array( 268 'L_TITLE' => $user->lang['BOT_' . $l_title], 269 'U_ACTION' => $this->u_action . "&id=$bot_id&action=$action", 270 'U_BACK' => $this->u_action, 271 'ERROR_MSG' => (sizeof($error)) ? implode('<br />', $error) : '', 272 273 'BOT_NAME' => $bot_row['bot_name'], 274 'BOT_IP' => $bot_row['bot_ip'], 275 'BOT_AGENT' => $bot_row['bot_agent'], 276 277 'S_EDIT_BOT' => true, 278 'S_ACTIVE_OPTIONS' => $s_active_options, 279 'S_STYLE_OPTIONS' => $style_select, 280 'S_LANG_OPTIONS' => $lang_select, 281 'S_ERROR' => (sizeof($error)) ? true : false, 282 ) 283 ); 284 285 return; 286 287 break; 288 } 289 290 $s_options = ''; 291 $_options = array('activate' => 'BOT_ACTIVATE', 'deactivate' => 'BOT_DEACTIVATE', 'delete' => 'DELETE'); 292 foreach ($_options as $value => $lang) 293 { 294 $s_options .= '<option value="' . $value . '">' . $user->lang[$lang] . '</option>'; 295 } 296 297 $template->assign_vars(array( 298 'U_ACTION' => $this->u_action, 299 'S_BOT_OPTIONS' => $s_options) 300 ); 301 302 $sql = 'SELECT b.bot_id, b.bot_name, b.bot_active, u.user_lastvisit 303 FROM ' . BOTS_TABLE . ' b, ' . USERS_TABLE . ' u 304 WHERE u.user_id = b.user_id 305 ORDER BY u.user_lastvisit DESC'; 306 $result = $db->sql_query($sql); 307 308 while ($row = $db->sql_fetchrow($result)) 309 { 310 $active_lang = (!$row['bot_active']) ? 'BOT_ACTIVATE' : 'BOT_DEACTIVATE'; 311 $active_value = (!$row['bot_active']) ? 'activate' : 'deactivate'; 312 313 $template->assign_block_vars('bots', array( 314 'BOT_NAME' => $row['bot_name'], 315 'BOT_ID' => $row['bot_id'], 316 'LAST_VISIT' => ($row['user_lastvisit']) ? $user->format_date($row['user_lastvisit']) : $user->lang['BOT_NEVER'], 317 318 'U_ACTIVATE_DEACTIVATE' => $this->u_action . "&id={$row['bot_id']}&action=$active_value", 319 'L_ACTIVATE_DEACTIVATE' => $user->lang[$active_lang], 320 'U_EDIT' => $this->u_action . "&id={$row['bot_id']}&action=edit", 321 'U_DELETE' => $this->u_action . "&id={$row['bot_id']}&action=delete") 322 ); 323 } 324 $db->sql_freeresult($result); 325 } 326 } 327 328 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 22 00:35:05 2006 | Cross-referenced by PHPXref 0.6 |