diff options
Diffstat (limited to 'phpBB/includes/acp/acp_attachments.php')
-rw-r--r-- | phpBB/includes/acp/acp_attachments.php | 216 |
1 files changed, 139 insertions, 77 deletions
diff --git a/phpBB/includes/acp/acp_attachments.php b/phpBB/includes/acp/acp_attachments.php index 55459739ca..1aaf1f9c09 100644 --- a/phpBB/includes/acp/acp_attachments.php +++ b/phpBB/includes/acp/acp_attachments.php @@ -20,14 +20,37 @@ if (!defined('IN_PHPBB')) */ class acp_attachments { - var $u_action; - var $new_config; + /** @var \phpbb\db\driver\driver_interface */ + protected $db; + + /** @var \phpbb\config\config */ + protected $config; + + /** @var ContainerBuilder */ + protected $phpbb_container; + + /** @var \phpbb\template\template */ + protected $template; + + /** @var \phpbb\user */ + protected $user; + + public $id; + public $u_action; + protected $new_config; function main($id, $mode) { - global $db, $user, $auth, $template, $cache; + global $db, $user, $auth, $template, $cache, $phpbb_container; global $config, $phpbb_admin_path, $phpbb_root_path, $phpEx; + $this->id = $id; + $this->db = $db; + $this->config = $config; + $this->template = $template; + $this->user = $user; + $this->phpbb_container = $phpbb_container; + $user->add_lang(array('posting', 'viewtopic', 'acp/attachments')); $error = $notify = array(); @@ -124,7 +147,6 @@ class acp_attachments 'secure_allow_empty_referer' => array('lang' => 'SECURE_EMPTY_REFERRER', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), 'check_attachment_content' => array('lang' => 'CHECK_CONTENT', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), - 'legend2' => $l_legend_cat_images, 'img_display_inlined' => array('lang' => 'DISPLAY_INLINED', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), 'img_create_thumbnail' => array('lang' => 'CREATE_THUMBNAIL', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), @@ -748,7 +770,6 @@ class acp_attachments } $template->assign_vars(array( - 'PHPBB_ROOT_PATH' => $phpbb_root_path, 'IMG_PATH' => $img_path, 'ACTION' => $action, 'GROUP_ID' => $group_id, @@ -1083,9 +1104,21 @@ class acp_attachments } } + if ($action == 'stats') + { + $this->handle_stats_resync(); + } + + $stats_error = $this->check_stats_accuracy(); + + if ($stats_error) + { + $error[] = $stats_error; + } + $template->assign_vars(array( - 'S_MANAGE' => true) - ); + 'S_MANAGE' => true, + )); $start = request_var('start', 0); @@ -1108,69 +1141,13 @@ class acp_attachments $attachments_per_page = (int) $config['topics_per_page']; - // Handle files stats resync - $action = request_var('action', ''); - $resync_files_stats = false; - if ($action && $action = 'stats') - { - if (!confirm_box(true)) - { - confirm_box(false, $user->lang['RESYNC_FILES_STATS_CONFIRM'], build_hidden_fields(array( - 'i' => $id, - 'mode' => $mode, - 'action' => $action, - ))); - } - else - { - $resync_files_stats = true; - add_log('admin', 'LOG_RESYNC_FILES_STATS'); - } - } - - // Check if files stats are accurate - $sql = 'SELECT COUNT(attach_id) as num_files - FROM ' . ATTACHMENTS_TABLE . ' - WHERE is_orphan = 0'; - $result = $db->sql_query($sql, 600); - $num_files_real = (int) $db->sql_fetchfield('num_files'); - if ($resync_files_stats === true) - { - set_config('num_files', $num_files_real, true); - } - $db->sql_freeresult($result); - - $sql = 'SELECT SUM(filesize) as upload_dir_size - FROM ' . ATTACHMENTS_TABLE . ' - WHERE is_orphan = 0'; - $result = $db->sql_query($sql, 600); - $total_size_real = (float) $db->sql_fetchfield('upload_dir_size'); - if ($resync_files_stats === true) - { - set_config('upload_dir_size', $total_size_real, true); - } - $db->sql_freeresult($result); - - // Get current files stats - $num_files = (int) $config['num_files']; - $total_size = (float) $config['upload_dir_size']; - - // Issue warning message if files stats are inaccurate - if (($num_files != $num_files_real) || ($total_size != $total_size_real)) - { - $error[] = $user->lang('FILES_STATS_WRONG', (int) $num_files_real, get_formatted_filesize($total_size_real)); - - $template->assign_vars(array( - 'S_ACTION_OPTIONS' => ($auth->acl_get('a_board')) ? true : false, - 'U_ACTION' => $this->u_action,) - ); - } + $stats = $this->get_attachment_stats($limit_filetime); + $num_files = $stats['num_files']; + $total_size = $stats['upload_dir_size']; // Make sure $start is set to the last page if it exceeds the amount - if ($start < 0 || $start > $num_files) - { - $start = ($start < 0) ? 0 : floor(($num_files - 1) / $attachments_per_page) * $attachments_per_page; - } + $pagination = $phpbb_container->get('pagination'); + $start = $pagination->validate_start($start, $attachments_per_page, $num_files); // If the user is trying to reach the second half of the attachments list, fetch it starting from the end $store_reverse = false; @@ -1180,15 +1157,11 @@ class acp_attachments { $store_reverse = true; - if ($start + $attachments_per_page > $num_files) - { - $sql_limit = min($attachments_per_page, max(1, $num_files - $start)); - } - // Select the sort order. Add time sort anchor for non-time sorting cases $sql_sort_anchor = ($sort_key != 't') ? ', a.filetime ' . (($sort_dir == 'd') ? 'ASC' : 'DESC') : ''; $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'ASC' : 'DESC') . $sql_sort_anchor; - $sql_start = max(0, $num_files - $sql_limit - $start); + $sql_limit = $pagination->reverse_limit($start, $sql_limit, $num_files); + $sql_start = $pagination->reverse_start($start, $sql_limit, $num_files); } else { @@ -1196,7 +1169,6 @@ class acp_attachments $sql_sort_anchor = ($sort_key != 't') ? ', a.filetime ' . (($sort_dir == 'd') ? 'DESC' : 'ASC') : ''; $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC') . $sql_sort_anchor; $sql_start = $start; - } $attachments_list = array(); @@ -1223,13 +1195,12 @@ class acp_attachments $db->sql_freeresult($result); $base_url = $this->u_action . "&$u_sort_param"; - phpbb_generate_template_pagination($template, $base_url, 'pagination', 'start', $num_files, $attachments_per_page, $start); + $pagination->generate_template_pagination($base_url, 'pagination', 'start', $num_files, $attachments_per_page, $start); $template->assign_vars(array( 'TOTAL_FILES' => $num_files, 'TOTAL_SIZE' => get_formatted_filesize($total_size), - 'S_ON_PAGE' => phpbb_on_page($template, $user, $base_url, $num_files, $attachments_per_page, $start), 'S_LIMIT_DAYS' => $s_limit_days, 'S_SORT_KEY' => $s_sort_key, 'S_SORT_DIR' => $s_sort_dir) @@ -1291,6 +1262,97 @@ class acp_attachments } /** + * Get attachment file count and size of upload directory + * + * @param $limit string Additional limit for WHERE clause to filter stats by. + * @return array Returns array with stats: num_files and upload_dir_size + */ + public function get_attachment_stats($limit = '') + { + $sql = 'SELECT COUNT(a.attach_id) AS num_files, SUM(a.filesize) AS upload_dir_size + FROM ' . ATTACHMENTS_TABLE . " a + WHERE a.is_orphan = 0 + $limit"; + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + return array( + 'num_files' => (int) $row['num_files'], + 'upload_dir_size' => (float) $row['upload_dir_size'], + ); + } + + /** + * Set config attachment stat values + * + * @param $stats array Array of config key => value pairs to set. + * @return null + */ + public function set_attachment_stats($stats) + { + foreach ($stats as $key => $value) + { + $this->config->set($key, $value, true); + } + } + + /** + * Check accuracy of attachment statistics. + * + * @param $resync bool Resync stats if they're incorrect. + * @return bool|string Returns false if stats are correct or error message + * otherwise. + */ + public function check_stats_accuracy() + { + // Get fresh stats. + $stats = $this->get_attachment_stats(); + + // Get current files stats + $num_files = (int) $this->config['num_files']; + $total_size = (float) $this->config['upload_dir_size']; + + if (($num_files != $stats['num_files']) || ($total_size != $stats['upload_dir_size'])) + { + $u_resync = $this->u_action . '&action=stats'; + + return $this->user->lang( + 'FILES_STATS_WRONG', + (int) $stats['num_files'], + get_formatted_filesize($stats['upload_dir_size']), + '<a href="' . $u_resync . '">', + '</a>' + ); + } + return false; + } + + /** + * Handle stats resync. + * + * @return null + */ + public function handle_stats_resync() + { + if (!confirm_box(true)) + { + confirm_box(false, $this->user->lang['RESYNC_FILES_STATS_CONFIRM'], build_hidden_fields(array( + 'i' => $this->id, + 'mode' => 'manage', + 'action' => 'stats', + ))); + } + else + { + $this->set_attachment_stats($this->get_attachment_stats()); + $log = $this->phpbb_container->get('log'); + $log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_RESYNC_FILES_STATS'); + } + + } + + /** * Build Select for category items */ function category_select($select_name, $group_id = false, $key = '') |