6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
26 function main($id, $mode)
28 global $config, $db, $user, $auth, $template;
29 global $phpbb_root_path, $phpbb_admin_path, $phpEx;
31 // Show restore permissions notice
32 if ($user->data['user_perm_from'] && $auth->acl_get('a_switchperm'))
34 $this->tpl_name = 'acp_main';
35 $this->page_title = 'ACP_MAIN';
37 $sql = 'SELECT user_id, username, user_colour
38 FROM ' . USERS_TABLE . '
39 WHERE user_id = ' . $user->data['user_perm_from'];
40 $result = $db->sql_query($sql);
41 $user_row = $db->sql_fetchrow($result);
42 $db->sql_freeresult($result);
44 $perm_from = '<strong' . (($user_row['user_colour']) ? ' style="color: #' . $user_row['user_colour'] . '">' : '>');
45 $perm_from .= ($user_row['user_id'] != ANONYMOUS) ? '<a href="' . append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&u=' . $user_row['user_id']) . '">' : '';
46 $perm_from .= $user_row['username'];
47 $perm_from .= ($user_row['user_id'] != ANONYMOUS) ? '</a>' : '';
48 $perm_from .= '</strong>';
50 $template->assign_vars(array(
51 'S_RESTORE_PERMISSIONS' => true,
52 'U_RESTORE_PERMISSIONS' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=restore_perm'),
53 'PERM_FROM' => $perm_from,
54 'L_PERMISSIONS_TRANSFERRED_EXPLAIN' => sprintf($user->lang['PERMISSIONS_TRANSFERRED_EXPLAIN'], $perm_from, append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=restore_perm')),
60 $action = request_var('action', '');
64 if ($action === 'admlogout')
67 $redirect_url = append_sid("{$phpbb_root_path}index.$phpEx");
68 meta_refresh(3, $redirect_url);
69 trigger_error($user->lang['ADM_LOGGED_OUT'] . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' . $redirect_url . '">', '</a>'));
72 if (!confirm_box(true))
78 $confirm_lang = 'RESET_ONLINE_CONFIRM';
82 $confirm_lang = 'RESYNC_STATS_CONFIRM';
86 $confirm_lang = 'RESYNC_POSTCOUNTS_CONFIRM';
90 $confirm_lang = 'RESET_DATE_CONFIRM';
94 $confirm_lang = 'RESYNC_POST_MARKING_CONFIRM';
98 $confirm_lang = 'PURGE_CACHE_CONFIRM';
100 case 'purge_sessions':
102 $confirm_lang = 'PURGE_SESSIONS_CONFIRM';
107 $confirm_lang = 'CONFIRM_OPERATION';
112 confirm_box(false, $user->lang[$confirm_lang], build_hidden_fields(array(
125 if (!$auth->acl_get('a_board'))
127 trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
130 set_config('record_online_users', 1, true);
131 set_config('record_online_date', time(), true);
132 add_log('admin', 'LOG_RESET_ONLINE');
136 if (!$auth->acl_get('a_board'))
138 trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
141 $sql = 'SELECT COUNT(post_id) AS stat
142 FROM ' . POSTS_TABLE . '
143 WHERE post_approved = 1';
144 $result = $db->sql_query($sql);
145 set_config('num_posts', (int) $db->sql_fetchfield('stat'), true);
146 $db->sql_freeresult($result);
148 $sql = 'SELECT COUNT(topic_id) AS stat
149 FROM ' . TOPICS_TABLE . '
150 WHERE topic_approved = 1';
151 $result = $db->sql_query($sql);
152 set_config('num_topics', (int) $db->sql_fetchfield('stat'), true);
153 $db->sql_freeresult($result);
155 $sql = 'SELECT COUNT(user_id) AS stat
156 FROM ' . USERS_TABLE . '
157 WHERE user_type IN (' . USER_NORMAL . ',' . USER_FOUNDER . ')';
158 $result = $db->sql_query($sql);
159 set_config('num_users', (int) $db->sql_fetchfield('stat'), true);
160 $db->sql_freeresult($result);
162 $sql = 'SELECT COUNT(attach_id) as stat
163 FROM ' . ATTACHMENTS_TABLE . '
164 WHERE is_orphan = 0';
165 $result = $db->sql_query($sql);
166 set_config('num_files', (int) $db->sql_fetchfield('stat'), true);
167 $db->sql_freeresult($result);
169 $sql = 'SELECT SUM(filesize) as stat
170 FROM ' . ATTACHMENTS_TABLE . '
171 WHERE is_orphan = 0';
172 $result = $db->sql_query($sql);
173 set_config('upload_dir_size', (float) $db->sql_fetchfield('stat'), true);
174 $db->sql_freeresult($result);
176 if (!function_exists('update_last_username'))
178 include($phpbb_root_path . "includes/functions_user.$phpEx");
180 update_last_username();
182 add_log('admin', 'LOG_RESYNC_STATS');
186 if (!$auth->acl_get('a_board'))
188 trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
191 // Resync post counts
192 $start = $max_post_id = 0;
194 // Find the maximum post ID, we can only stop the cycle when we've reached it
195 $sql = 'SELECT MAX(forum_last_post_id) as max_post_id
196 FROM ' . FORUMS_TABLE;
197 $result = $db->sql_query($sql);
198 $max_post_id = (int) $db->sql_fetchfield('max_post_id');
199 $db->sql_freeresult($result);
201 // No maximum post id? :o
204 $sql = 'SELECT MAX(post_id)
205 FROM ' . POSTS_TABLE;
206 $result = $db->sql_query($sql);
207 $max_post_id = (int) $db->sql_fetchfield('max_post_id');
208 $db->sql_freeresult($result);
211 // Still no maximum post id? Then we are finished
214 add_log('admin', 'LOG_RESYNC_POSTCOUNTS');
218 $step = ($config['num_posts']) ? (max((int) ($config['num_posts'] / 5), 20000)) : 20000;
219 $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_posts = 0');
221 while ($start < $max_post_id)
223 $sql = 'SELECT COUNT(post_id) AS num_posts, poster_id
224 FROM ' . POSTS_TABLE . '
225 WHERE post_id BETWEEN ' . ($start + 1) . ' AND ' . ($start + $step) . '
226 AND post_postcount = 1 AND post_approved = 1
228 $result = $db->sql_query($sql);
230 if ($row = $db->sql_fetchrow($result))
234 $sql = 'UPDATE ' . USERS_TABLE . " SET user_posts = user_posts + {$row['num_posts']} WHERE user_id = {$row['poster_id']}";
235 $db->sql_query($sql);
237 while ($row = $db->sql_fetchrow($result));
239 $db->sql_freeresult($result);
244 add_log('admin', 'LOG_RESYNC_POSTCOUNTS');
249 if (!$auth->acl_get('a_board'))
251 trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
254 set_config('board_startdate', time() - 1);
255 add_log('admin', 'LOG_RESET_DATE');
259 switch ($db->sql_layer)
263 $db->sql_query('DELETE FROM ' . TOPICS_POSTED_TABLE);
267 $db->sql_query('TRUNCATE TABLE ' . TOPICS_POSTED_TABLE);
271 // This can get really nasty... therefore we only do the last six months
272 $get_from_time = time() - (6 * 4 * 7 * 24 * 60 * 60);
274 // Select forum ids, do not include categories
275 $sql = 'SELECT forum_id
276 FROM ' . FORUMS_TABLE . '
277 WHERE forum_type <> ' . FORUM_CAT;
278 $result = $db->sql_query($sql);
280 $forum_ids = array();
281 while ($row = $db->sql_fetchrow($result))
283 $forum_ids[] = $row['forum_id'];
285 $db->sql_freeresult($result);
287 // Any global announcements? ;)
290 // Now go through the forums and get us some topics...
291 foreach ($forum_ids as $forum_id)
293 $sql = 'SELECT p.poster_id, p.topic_id
294 FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . ' t
295 WHERE t.forum_id = ' . $forum_id . '
296 AND t.topic_moved_id = 0
297 AND t.topic_last_post_time > ' . $get_from_time . '
298 AND t.topic_id = p.topic_id
299 AND p.poster_id <> ' . ANONYMOUS . '
300 GROUP BY p.poster_id, p.topic_id';
301 $result = $db->sql_query($sql);
304 while ($row = $db->sql_fetchrow($result))
306 $posted[$row['poster_id']][] = $row['topic_id'];
308 $db->sql_freeresult($result);
311 foreach ($posted as $user_id => $topic_row)
313 foreach ($topic_row as $topic_id)
316 'user_id' => (int) $user_id,
317 'topic_id' => (int) $topic_id,
324 if (sizeof($sql_ary))
326 $db->sql_multi_insert(TOPICS_POSTED_TABLE, $sql_ary);
330 add_log('admin', 'LOG_RESYNC_POST_MARKING');
334 if ((int) $user->data['user_type'] !== USER_FOUNDER)
336 trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
343 $auth->acl_clear_prefetch();
346 add_log('admin', 'LOG_PURGE_CACHE');
349 case 'purge_sessions':
350 if ((int) $user->data['user_type'] !== USER_FOUNDER)
352 trigger_error($user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
355 $tables = array(CONFIRM_TABLE, SESSIONS_TABLE);
357 foreach ($tables as $table)
359 switch ($db->sql_layer)
363 $db->sql_query("DELETE FROM $table");
367 $db->sql_query("TRUNCATE TABLE $table");
372 // let's restore the admin session
373 $reinsert_ary = array(
374 'session_id' => (string) $user->session_id,
375 'session_page' => (string) substr($user->page['page'], 0, 199),
376 'session_forum_id' => $user->page['forum'],
377 'session_user_id' => (int) $user->data['user_id'],
378 'session_start' => (int) $user->data['session_start'],
379 'session_last_visit' => (int) $user->data['session_last_visit'],
380 'session_time' => (int) $user->time_now,
381 'session_browser' => (string) trim(substr($user->browser, 0, 149)),
382 'session_forwarded_for' => (string) $user->forwarded_for,
383 'session_ip' => (string) $user->ip,
384 'session_autologin' => (int) $user->data['session_autologin'],
385 'session_admin' => 1,
386 'session_viewonline' => (int) $user->data['session_viewonline'],
389 $sql = 'INSERT INTO ' . SESSIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $reinsert_ary);
390 $db->sql_query($sql);
392 add_log('admin', 'LOG_PURGE_SESSIONS');
399 $user->add_lang('install');
401 if ($auth->acl_get('a_server') && version_compare(PHP_VERSION, '5.2.0', '<'))
403 $template->assign_vars(array(
404 'S_PHP_VERSION_OLD' => true,
405 'L_PHP_VERSION_OLD' => sprintf($user->lang['PHP_VERSION_OLD'], '<a href="http://www.phpbb.com/community/viewtopic.php?f=14&t=1958605">', '</a>'),
409 $latest_version_info = false;
410 if (($latest_version_info = obtain_latest_version_info(request_var('versioncheck_force', false))) === false)
412 $template->assign_var('S_VERSIONCHECK_FAIL', true);
416 $latest_version_info = explode("\n", $latest_version_info);
418 $template->assign_vars(array(
419 'S_VERSION_UP_TO_DATE' => phpbb_version_compare(trim($latest_version_info[0]), $config['version'], '<='),
423 // Get forum statistics
424 $total_posts = $config['num_posts'];
425 $total_topics = $config['num_topics'];
426 $total_users = $config['num_users'];
427 $total_files = $config['num_files'];
429 $start_date = $user->format_date($config['board_startdate']);
431 $boarddays = (time() - $config['board_startdate']) / 86400;
433 $posts_per_day = sprintf('%.2f', $total_posts / $boarddays);
434 $topics_per_day = sprintf('%.2f', $total_topics / $boarddays);
435 $users_per_day = sprintf('%.2f', $total_users / $boarddays);
436 $files_per_day = sprintf('%.2f', $total_files / $boarddays);
438 $upload_dir_size = get_formatted_filesize($config['upload_dir_size']);
440 $avatar_dir_size = 0;
442 if ($avatar_dir = @opendir($phpbb_root_path . $config['avatar_path']))
444 while (($file = readdir($avatar_dir)) !== false)
446 if ($file[0] != '.' && $file != 'CVS' && strpos($file, 'index.') === false)
448 $avatar_dir_size += filesize($phpbb_root_path . $config['avatar_path'] . '/' . $file);
451 closedir($avatar_dir);
453 $avatar_dir_size = get_formatted_filesize($avatar_dir_size);
457 // Couldn't open Avatar dir.
458 $avatar_dir_size = $user->lang['NOT_AVAILABLE'];
461 if ($posts_per_day > $total_posts)
463 $posts_per_day = $total_posts;
466 if ($topics_per_day > $total_topics)
468 $topics_per_day = $total_topics;
471 if ($users_per_day > $total_users)
473 $users_per_day = $total_users;
476 if ($files_per_day > $total_files)
478 $files_per_day = $total_files;
481 if ($config['allow_attachments'] || $config['allow_pm_attach'])
483 $sql = 'SELECT COUNT(attach_id) AS total_orphan
484 FROM ' . ATTACHMENTS_TABLE . '
486 AND filetime < ' . (time() - 3*60*60);
487 $result = $db->sql_query($sql);
488 $total_orphan = (int) $db->sql_fetchfield('total_orphan');
489 $db->sql_freeresult($result);
493 $total_orphan = false;
496 $dbsize = get_database_size();
498 $template->assign_vars(array(
499 'TOTAL_POSTS' => $total_posts,
500 'POSTS_PER_DAY' => $posts_per_day,
501 'TOTAL_TOPICS' => $total_topics,
502 'TOPICS_PER_DAY' => $topics_per_day,
503 'TOTAL_USERS' => $total_users,
504 'USERS_PER_DAY' => $users_per_day,
505 'TOTAL_FILES' => $total_files,
506 'FILES_PER_DAY' => $files_per_day,
507 'START_DATE' => $start_date,
508 'AVATAR_DIR_SIZE' => $avatar_dir_size,
510 'UPLOAD_DIR_SIZE' => $upload_dir_size,
511 'TOTAL_ORPHAN' => $total_orphan,
512 'S_TOTAL_ORPHAN' => ($total_orphan === false) ? false : true,
513 'GZIP_COMPRESSION' => ($config['gzip_compress'] && @extension_loaded('zlib')) ? $user->lang['ON'] : $user->lang['OFF'],
514 'DATABASE_INFO' => $db->sql_server_info(),
515 'BOARD_VERSION' => $config['version'],
517 'U_ACTION' => $this->u_action,
518 'U_ADMIN_LOG' => append_sid("{$phpbb_admin_path}index.$phpEx", 'i=logs&mode=admin'),
519 'U_INACTIVE_USERS' => append_sid("{$phpbb_admin_path}index.$phpEx", 'i=inactive&mode=list'),
520 'U_VERSIONCHECK' => append_sid("{$phpbb_admin_path}index.$phpEx", 'i=update&mode=version_check'),
521 'U_VERSIONCHECK_FORCE' => append_sid("{$phpbb_admin_path}index.$phpEx", 'versioncheck_force=1'),
523 'S_ACTION_OPTIONS' => ($auth->acl_get('a_board')) ? true : false,
524 'S_FOUNDER' => ($user->data['user_type'] == USER_FOUNDER) ? true : false,
531 if ($auth->acl_get('a_viewlogs'))
533 view_log('admin', $log_data, $log_count, 5);
535 foreach ($log_data as $row)
537 $template->assign_block_vars('log', array(
538 'USERNAME' => $row['username_full'],
540 'DATE' => $user->format_date($row['time']),
541 'ACTION' => $row['action'])
546 if ($auth->acl_get('a_user'))
548 $user->add_lang('memberlist');
553 view_inactive_users($inactive, $inactive_count, 10);
555 foreach ($inactive as $row)
557 $template->assign_block_vars('inactive', array(
558 'INACTIVE_DATE' => $user->format_date($row['user_inactive_time']),
559 'REMINDED_DATE' => $user->format_date($row['user_reminded_time']),
560 'JOINED' => $user->format_date($row['user_regdate']),
561 'LAST_VISIT' => (!$row['user_lastvisit']) ? ' - ' : $user->format_date($row['user_lastvisit']),
563 'REASON' => $row['inactive_reason'],
564 'USER_ID' => $row['user_id'],
565 'POSTS' => ($row['user_posts']) ? $row['user_posts'] : 0,
566 'REMINDED' => $row['user_reminded'],
568 'REMINDED_EXPLAIN' => $user->lang('USER_LAST_REMINDED', (int) $row['user_reminded'], $user->format_date($row['user_reminded_time'])),
570 'USERNAME_FULL' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, append_sid("{$phpbb_admin_path}index.$phpEx", 'i=users&mode=overview')),
571 'USERNAME' => get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']),
572 'USER_COLOR' => get_username_string('colour', $row['user_id'], $row['username'], $row['user_colour']),
574 'U_USER_ADMIN' => append_sid("{$phpbb_admin_path}index.$phpEx", "i=users&mode=overview&u={$row['user_id']}"),
575 'U_SEARCH_USER' => ($auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", "author_id={$row['user_id']}&sr=posts") : '',
579 $option_ary = array('activate' => 'ACTIVATE', 'delete' => 'DELETE');
580 if ($config['email_enable'])
582 $option_ary += array('remind' => 'REMIND');
585 $template->assign_vars(array(
586 'S_INACTIVE_USERS' => true,
587 'S_INACTIVE_OPTIONS' => build_select($option_ary))
591 // Warn if install is still present
592 if (file_exists($phpbb_root_path . 'install') && !is_file($phpbb_root_path . 'install'))
594 $template->assign_var('S_REMOVE_INSTALL', true);
597 if (!defined('PHPBB_DISABLE_CONFIG_CHECK') && file_exists($phpbb_root_path . 'config.' . $phpEx) && phpbb_is_writable($phpbb_root_path . 'config.' . $phpEx))
599 // World-Writable? (000x)
600 $template->assign_var('S_WRITABLE_CONFIG', (bool) (@fileperms($phpbb_root_path . 'config.' . $phpEx) & 0x0002));
603 if (extension_loaded('mbstring'))
605 $template->assign_vars(array(
606 'S_MBSTRING_LOADED' => true,
607 'S_MBSTRING_FUNC_OVERLOAD_FAIL' => (intval(@ini_get('mbstring.func_overload')) & (MB_OVERLOAD_MAIL | MB_OVERLOAD_STRING)),
608 'S_MBSTRING_ENCODING_TRANSLATION_FAIL' => (@ini_get('mbstring.encoding_translation') != 0),
609 'S_MBSTRING_HTTP_INPUT_FAIL' => (@ini_get('mbstring.http_input') != 'pass'),
610 'S_MBSTRING_HTTP_OUTPUT_FAIL' => (@ini_get('mbstring.http_output') != 'pass'),
614 // Fill dbms version if not yet filled
615 if (empty($config['dbms_version']))
617 set_config('dbms_version', $db->sql_server_info(true));
620 $this->tpl_name = 'acp_main';
621 $this->page_title = 'ACP_MAIN';