replace constants with class constants.
[phpbb.git] / phpBB / modules / acp / acp_bots.php
blobb1a0f3f7cd0a840eb6cb86d1dc74fb27701c2fb7
1 <?php
2 /**
4 * @package acp
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * @package acp
22 class acp_bots
24 var $u_action;
26 function main($id, $mode)
28 global $config, $db, $user, $auth, $template, $cache;
30 $action = request_var('action', '');
31 $submit = phpbb_request::is_set_post('submit');
32 $mark = request_var('mark', array(0));
33 $bot_id = request_var('id', 0);
35 if (phpbb_request::is_set_post('add'))
37 $action = 'add';
40 $error = array();
42 $user->add_lang('acp/bots');
43 $this->tpl_name = 'acp_bots';
44 $this->page_title = 'ACP_BOTS';
45 $form_key = 'acp_bots';
46 add_form_key($form_key);
48 if ($submit && !check_form_key($form_key))
50 $error[] = $user->lang['FORM_INVALID'];
53 // User wants to do something, how inconsiderate of them!
54 switch ($action)
56 case 'activate':
57 if ($bot_id || sizeof($mark))
59 $sql_id = ($bot_id) ? " = $bot_id" : ' IN (' . implode(', ', $mark) . ')';
61 $sql = 'UPDATE ' . BOTS_TABLE . "
62 SET bot_active = 1
63 WHERE bot_id $sql_id";
64 $db->sql_query($sql);
67 $cache->destroy('_bots');
68 break;
70 case 'deactivate':
71 if ($bot_id || sizeof($mark))
73 $sql_id = ($bot_id) ? " = $bot_id" : ' IN (' . implode(', ', $mark) . ')';
75 $sql = 'UPDATE ' . BOTS_TABLE . "
76 SET bot_active = 0
77 WHERE bot_id $sql_id";
78 $db->sql_query($sql);
81 $cache->destroy('_bots');
82 break;
84 case 'delete':
85 if ($bot_id || sizeof($mark))
87 if (confirm_box(true))
89 // We need to delete the relevant user, usergroup and bot entries ...
90 $sql_id = ($bot_id) ? " = $bot_id" : ' IN (' . implode(', ', $mark) . ')';
92 $sql = 'SELECT bot_name, user_id
93 FROM ' . BOTS_TABLE . "
94 WHERE bot_id $sql_id";
95 $result = $db->sql_query($sql);
97 $user_id_ary = $bot_name_ary = array();
98 while ($row = $db->sql_fetchrow($result))
100 $user_id_ary[] = (int) $row['user_id'];
101 $bot_name_ary[] = $row['bot_name'];
103 $db->sql_freeresult($result);
105 $db->sql_transaction('begin');
107 $sql = 'DELETE FROM ' . BOTS_TABLE . "
108 WHERE bot_id $sql_id";
109 $db->sql_query($sql);
111 if (sizeof($user_id_ary))
113 $_tables = array(USERS_TABLE, USER_GROUP_TABLE);
114 foreach ($_tables as $table)
116 $sql = "DELETE FROM $table
117 WHERE " . $db->sql_in_set('user_id', $user_id_ary);
118 $db->sql_query($sql);
122 $db->sql_transaction('commit');
124 $cache->destroy('_bots');
126 add_log('admin', 'LOG_BOT_DELETE', implode(', ', $bot_name_ary));
127 trigger_error($user->lang['BOT_DELETED'] . adm_back_link($this->u_action));
129 else
131 confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
132 'mark' => $mark,
133 'id' => $bot_id,
134 'mode' => $mode,
135 'action' => $action))
139 break;
141 case 'edit':
142 case 'add':
143 include_once(PHPBB_ROOT_PATH . 'includes/functions_user.' . PHP_EXT);
145 $bot_row = array(
146 'bot_name' => utf8_normalize_nfc(request_var('bot_name', '', true)),
147 'bot_agent' => request_var('bot_agent', ''),
148 'bot_ip' => request_var('bot_ip', ''),
149 'bot_active' => request_var('bot_active', true),
150 'bot_lang' => request_var('bot_lang', $config['default_lang']),
151 'bot_style' => request_var('bot_style' , $config['default_style']),
154 if ($submit)
156 if (!$bot_row['bot_agent'] && !$bot_row['bot_ip'])
158 $error[] = $user->lang['ERR_BOT_NO_MATCHES'];
161 if ($bot_row['bot_ip'] && !preg_match('#^[\d\.,:]+$#', $bot_row['bot_ip']))
163 if (!$ip_list = gethostbynamel($bot_row['bot_ip']))
165 $error[] = $user->lang['ERR_BOT_NO_IP'];
167 else
169 $bot_row['bot_ip'] = implode(',', $ip_list);
172 $bot_row['bot_ip'] = str_replace(' ', '', $bot_row['bot_ip']);
174 // Make sure the admin is not adding a bot with an user agent similar to his one
175 if ($bot_row['bot_agent'] && substr($user->data['session_browser'], 0, 149) === substr($bot_row['bot_agent'], 0, 149))
177 $error[] = $user->lang['ERR_BOT_AGENT_MATCHES_UA'];
180 $bot_name = false;
181 if ($bot_id)
183 $sql = 'SELECT u.username_clean
184 FROM ' . BOTS_TABLE . ' b, ' . USERS_TABLE . " u
185 WHERE b.bot_id = $bot_id
186 AND u.user_id = b.user_id";
187 $result = $db->sql_query($sql);
188 $row = $db->sql_fetchrow($result);
189 $db->sql_freeresult($result);
191 if (!$bot_row)
193 $error[] = $user->lang['NO_BOT'];
195 else
197 $bot_name = $row['username_clean'];
200 if (!$this->validate_botname($bot_row['bot_name'], $bot_name))
202 $error[] = $user->lang['BOT_NAME_TAKEN'];
205 if (!sizeof($error))
207 // New bot? Create a new user and group entry
208 if ($action == 'add')
210 $sql = 'SELECT group_id, group_colour
211 FROM ' . GROUPS_TABLE . "
212 WHERE group_name_clean = 'bots'
213 AND group_type = " . GROUP_SPECIAL;
214 $result = $db->sql_query($sql);
215 $group_row = $db->sql_fetchrow($result);
216 $db->sql_freeresult($result);
218 if (!$group_row)
220 trigger_error($user->lang['NO_BOT_GROUP'] . adm_back_link($this->u_action . "&amp;id=$bot_id&amp;action=$action"), E_USER_WARNING);
224 $user_id = user_add(array(
225 'user_type' => (int) phpbb::USER_IGNORE,
226 'group_id' => (int) $group_row['group_id'],
227 'username' => (string) $bot_row['bot_name'],
228 'user_regdate' => time(),
229 'user_password' => '',
230 'user_colour' => (string) $group_row['group_colour'],
231 'user_email' => '',
232 'user_lang' => (string) $bot_row['bot_lang'],
233 'user_style' => (int) $bot_row['bot_style'],
234 'user_allow_massemail' => 0,
237 $sql = 'INSERT INTO ' . BOTS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
238 'user_id' => (int) $user_id,
239 'bot_name' => (string) $bot_row['bot_name'],
240 'bot_active' => (int) $bot_row['bot_active'],
241 'bot_agent' => (string) $bot_row['bot_agent'],
242 'bot_ip' => (string) $bot_row['bot_ip'])
244 $db->sql_query($sql);
246 $log = 'ADDED';
248 else if ($bot_id)
250 $sql = 'SELECT user_id, bot_name
251 FROM ' . BOTS_TABLE . "
252 WHERE bot_id = $bot_id";
253 $result = $db->sql_query($sql);
254 $row = $db->sql_fetchrow($result);
255 $db->sql_freeresult($result);
257 if (!$row)
259 trigger_error($user->lang['NO_BOT'] . adm_back_link($this->u_action . "&amp;id=$bot_id&amp;action=$action"), E_USER_WARNING);
262 $sql_ary = array(
263 'user_style' => (int) $bot_row['bot_style'],
264 'user_lang' => (string) $bot_row['bot_lang'],
267 if ($bot_row['bot_name'] !== $row['bot_name'])
269 $sql_ary['username'] = (string) $bot_row['bot_name'];
270 $sql_ary['username_clean'] = (string) utf8_clean_string($bot_row['bot_name']);
273 $sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . " WHERE user_id = {$row['user_id']}";
274 $db->sql_query($sql);
276 $sql = 'UPDATE ' . BOTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
277 'bot_name' => (string) $bot_row['bot_name'],
278 'bot_active' => (int) $bot_row['bot_active'],
279 'bot_agent' => (string) $bot_row['bot_agent'],
280 'bot_ip' => (string) $bot_row['bot_ip'])
281 ) . " WHERE bot_id = $bot_id";
282 $db->sql_query($sql);
284 // Updated username?
285 if ($bot_row['bot_name'] !== $row['bot_name'])
287 user_update_name($row['bot_name'], $bot_row['bot_name']);
290 $log = 'UPDATED';
293 $cache->destroy('_bots');
295 add_log('admin', 'LOG_BOT_' . $log, $bot_row['bot_name']);
296 trigger_error($user->lang['BOT_' . $log] . adm_back_link($this->u_action));
300 else if ($bot_id)
302 $sql = 'SELECT b.*, u.user_lang, u.user_style
303 FROM ' . BOTS_TABLE . ' b, ' . USERS_TABLE . " u
304 WHERE b.bot_id = $bot_id
305 AND u.user_id = b.user_id";
306 $result = $db->sql_query($sql);
307 $bot_row = $db->sql_fetchrow($result);
308 $db->sql_freeresult($result);
310 if (!$bot_row)
312 trigger_error($user->lang['NO_BOT'] . adm_back_link($this->u_action . "&amp;id=$bot_id&amp;action=$action"), E_USER_WARNING);
315 $bot_row['bot_lang'] = $bot_row['user_lang'];
316 $bot_row['bot_style'] = $bot_row['user_style'];
317 unset($bot_row['user_lang'], $bot_row['user_style']);
320 $s_active_options = '';
321 $_options = array('0' => 'NO', '1' => 'YES');
322 foreach ($_options as $value => $lang)
324 $selected = ($bot_row['bot_active'] == $value) ? ' selected="selected"' : '';
325 $s_active_options .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$lang] . '</option>';
328 $style_select = style_select($bot_row['bot_style'], true);
329 $lang_select = language_select($bot_row['bot_lang']);
331 $l_title = ($action == 'edit') ? 'EDIT' : 'ADD';
333 $template->assign_vars(array(
334 'L_TITLE' => $user->lang['BOT_' . $l_title],
335 'U_ACTION' => $this->u_action . "&amp;id=$bot_id&amp;action=$action",
336 'U_BACK' => $this->u_action,
337 'ERROR_MSG' => (sizeof($error)) ? implode('<br />', $error) : '',
339 'BOT_NAME' => $bot_row['bot_name'],
340 'BOT_IP' => $bot_row['bot_ip'],
341 'BOT_AGENT' => $bot_row['bot_agent'],
343 'S_EDIT_BOT' => true,
344 'S_ACTIVE_OPTIONS' => $s_active_options,
345 'S_STYLE_OPTIONS' => $style_select,
346 'S_LANG_OPTIONS' => $lang_select,
347 'S_ERROR' => (sizeof($error)) ? true : false,
351 return;
353 break;
356 $s_options = '';
357 $_options = array('activate' => 'BOT_ACTIVATE', 'deactivate' => 'BOT_DEACTIVATE', 'delete' => 'DELETE');
358 foreach ($_options as $value => $lang)
360 $s_options .= '<option value="' . $value . '">' . $user->lang[$lang] . '</option>';
363 $template->assign_vars(array(
364 'U_ACTION' => $this->u_action,
365 'S_BOT_OPTIONS' => $s_options)
368 $sql = 'SELECT b.bot_id, b.bot_name, b.bot_active, u.user_lastvisit
369 FROM ' . BOTS_TABLE . ' b, ' . USERS_TABLE . ' u
370 WHERE u.user_id = b.user_id
371 ORDER BY u.user_lastvisit DESC, b.bot_name ASC';
372 $result = $db->sql_query($sql);
374 while ($row = $db->sql_fetchrow($result))
376 $active_lang = (!$row['bot_active']) ? 'BOT_ACTIVATE' : 'BOT_DEACTIVATE';
377 $active_value = (!$row['bot_active']) ? 'activate' : 'deactivate';
379 $template->assign_block_vars('bots', array(
380 'BOT_NAME' => $row['bot_name'],
381 'BOT_ID' => $row['bot_id'],
382 'LAST_VISIT' => ($row['user_lastvisit']) ? $user->format_date($row['user_lastvisit']) : $user->lang['BOT_NEVER'],
384 'U_ACTIVATE_DEACTIVATE' => $this->u_action . "&amp;id={$row['bot_id']}&amp;action=$active_value",
385 'L_ACTIVATE_DEACTIVATE' => $user->lang[$active_lang],
386 'U_EDIT' => $this->u_action . "&amp;id={$row['bot_id']}&amp;action=edit",
387 'U_DELETE' => $this->u_action . "&amp;id={$row['bot_id']}&amp;action=delete")
390 $db->sql_freeresult($result);
394 * Validate bot name against username table
396 function validate_botname($newname, $oldname = false)
398 global $db;
400 if ($oldname && utf8_clean_string($newname) === $oldname)
402 return true;
405 // Admins might want to use names otherwise forbidden, thus we only check for duplicates.
406 $sql = 'SELECT username
407 FROM ' . USERS_TABLE . "
408 WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($newname)) . "'";
409 $result = $db->sql_query($sql);
410 $row = $db->sql_fetchrow($result);
411 $db->sql_freeresult($result);
413 return ($row) ? false : true;