function cleanup()
    {
        global $auth, $cache, $db, $template, $user;
        $delete = isset($_POST['delete']) ? true : false;
        $submit = isset($_POST['submit']) ? true : false;
        $missing_sources = request_var('source', array(0));
        $missing_entries = request_var('entry', array(''), true);
        $missing_authors = request_var('author', array(0), true);
        $missing_comments = request_var('comment', array(0), true);
        $missing_personals = request_var('personal', array(0), true);
        $personals_bad = request_var('personal_bad', array(0), true);
        $s_hidden_fields = build_hidden_fields(array('source' => $missing_sources, 'entry' => $missing_entries, 'author' => $missing_authors, 'comment' => $missing_comments, 'personal' => $missing_personals, 'personal_bad' => $personals_bad));
        if ($submit) {
            if ($missing_authors) {
                $sql = 'UPDATE ' . GALLERY_IMAGES_TABLE . ' 
					SET image_user_id = ' . ANONYMOUS . ",\n\t\t\t\t\t\timage_user_colour = ''\n\t\t\t\t\tWHERE " . $db->sql_in_set('image_id', $missing_authors);
                $db->sql_query($sql);
            }
            if ($missing_comments) {
                $sql = 'UPDATE ' . GALLERY_COMMENTS_TABLE . ' 
					SET comment_user_id = ' . ANONYMOUS . ",\n\t\t\t\t\t\tcomment_user_colour = ''\n\t\t\t\t\tWHERE " . $db->sql_in_set('comment_id', $missing_comments);
                $db->sql_query($sql);
            }
            trigger_error($user->lang['CLEAN_CHANGED'] . adm_back_link($this->u_action));
        }
        if (confirm_box(true)) {
            $message = array();
            if ($missing_sources) {
                $sql = 'DELETE FROM ' . GALLERY_COMMENTS_TABLE . ' WHERE ' . $db->sql_in_set('comment_image_id', $missing_sources);
                $db->sql_query($sql);
                $sql = 'DELETE FROM ' . GALLERY_REPORTS_TABLE . ' WHERE ' . $db->sql_in_set('report_image_id', $missing_sources);
                $db->sql_query($sql);
                $sql = 'DELETE FROM ' . GALLERY_FAVORITES_TABLE . ' WHERE ' . $db->sql_in_set('image_id', $missing_sources);
                $db->sql_query($sql);
                $sql = 'DELETE FROM ' . GALLERY_WATCH_TABLE . ' WHERE ' . $db->sql_in_set('image_id', $missing_sources);
                $db->sql_query($sql);
                phpbb_gallery_image_base::delete_images($missing_sources);
                $message[] = $user->lang['CLEAN_SOURCES_DONE'];
            }
            if ($missing_entries) {
                foreach ($missing_entries as $missing_image) {
                    @unlink(phpbb_gallery_url::path('upload') . utf8_decode($missing_image));
                }
                $message[] = $user->lang['CLEAN_ENTRIES_DONE'];
            }
            if ($missing_authors) {
                $deleted_images = $filenames = array();
                $sql = 'SELECT image_id, image_filename
					FROM ' . GALLERY_IMAGES_TABLE . '
					WHERE ' . $db->sql_in_set('image_id', $missing_authors);
                $result = $db->sql_query($sql);
                while ($row = $db->sql_fetchrow($result)) {
                    $filenames[(int) $row['image_id']] = $row['image_filename'];
                    $deleted_images[] = $row['image_id'];
                }
                $db->sql_freeresult($result);
                // we have all image_ids in $deleted_images which are deleted
                // aswell as the album_ids in $deleted_albums
                // so now drop the comments, ratings, images and albums
                if ($deleted_images) {
                    $sql = 'DELETE FROM ' . GALLERY_COMMENTS_TABLE . ' WHERE ' . $db->sql_in_set('comment_image_id', $deleted_images);
                    $db->sql_query($sql);
                    $sql = 'DELETE FROM ' . GALLERY_FAVORITES_TABLE . ' WHERE ' . $db->sql_in_set('image_id', $deleted_images);
                    $db->sql_query($sql);
                    $sql = 'DELETE FROM ' . GALLERY_REPORTS_TABLE . ' WHERE ' . $db->sql_in_set('report_image_id', $deleted_images);
                    $db->sql_query($sql);
                    $sql = 'DELETE FROM ' . GALLERY_WATCH_TABLE . ' WHERE ' . $db->sql_in_set('image_id', $deleted_images);
                    $db->sql_query($sql);
                    phpbb_gallery_image_base::delete_images($deleted_images, $filenames);
                }
                $message[] = $user->lang['CLEAN_AUTHORS_DONE'];
            }
            if ($missing_comments) {
                $sql = 'DELETE FROM ' . GALLERY_COMMENTS_TABLE . ' WHERE ' . $db->sql_in_set('comment_id', $missing_comments);
                $db->sql_query($sql);
                $message[] = $user->lang['CLEAN_COMMENTS_DONE'];
            }
            if ($missing_personals || $personals_bad) {
                $delete_albums = array_merge($missing_personals, $personals_bad);
                $deleted_images = $deleted_albums = array(0);
                $user_image_count = array();
                $sql = 'SELECT COUNT(album_user_id) personal_counter
					FROM ' . GALLERY_ALBUMS_TABLE . '
					WHERE parent_id = 0
						AND ' . $db->sql_in_set('album_user_id', $delete_albums);
                $result = $db->sql_query($sql);
                $remove_personal_counter = $db->sql_fetchfield('personal_counter');
                $db->sql_freeresult($result);
                $sql = 'SELECT album_id
					FROM ' . GALLERY_ALBUMS_TABLE . '
					WHERE ' . $db->sql_in_set('album_user_id', $delete_albums);
                $result = $db->sql_query($sql);
                while ($row = $db->sql_fetchrow($result)) {
                    $deleted_albums[] = $row['album_id'];
                }
                $db->sql_freeresult($result);
                $sql = 'SELECT image_id, image_filename, image_user_id
					FROM ' . GALLERY_IMAGES_TABLE . '
					WHERE ' . $db->sql_in_set('image_album_id', $deleted_albums);
                $result = $db->sql_query($sql);
                $filenames = array();
                while ($row = $db->sql_fetchrow($result)) {
                    $deleted_images[] = $row['image_id'];
                    $filenames[(int) $row['image_id']] = $row['image_filename'];
                    if (isset($user_image_count[$row['image_user_id']])) {
                        $user_image_count[$row['image_user_id']]++;
                    } else {
                        $user_image_count[(int) $row['image_user_id']] = 1;
                    }
                }
                $db->sql_freeresult($result);
                if ($deleted_images) {
                    $sql = 'DELETE FROM ' . GALLERY_COMMENTS_TABLE . ' WHERE ' . $db->sql_in_set('comment_image_id', $deleted_images);
                    $db->sql_query($sql);
                    $sql = 'DELETE FROM ' . GALLERY_FAVORITES_TABLE . ' WHERE ' . $db->sql_in_set('image_id', $deleted_images);
                    $db->sql_query($sql);
                    $sql = 'DELETE FROM ' . GALLERY_REPORTS_TABLE . ' WHERE ' . $db->sql_in_set('report_image_id', $deleted_images);
                    $db->sql_query($sql);
                    $sql = 'DELETE FROM ' . GALLERY_WATCH_TABLE . ' WHERE ' . $db->sql_in_set('image_id', $deleted_images);
                    $db->sql_query($sql);
                    phpbb_gallery_image_base::delete_images($deleted_images, $filenames);
                }
                $sql = 'DELETE FROM ' . GALLERY_ALBUMS_TABLE . ' WHERE ' . $db->sql_in_set('album_id', $deleted_albums);
                $db->sql_query($sql);
                phpbb_gallery_config::get('num_pegas', $remove_personal_counter);
                if (in_array(phpbb_gallery_config::get('newest_pega_album_id'), $deleted_albums)) {
                    // Update the config for the statistic on the index
                    if (phpbb_gallery_config::get('num_pegas') > 0) {
                        $sql_array = array('SELECT' => 'a.album_id, u.user_id, u.username, u.user_colour', 'FROM' => array(GALLERY_ALBUMS_TABLE => 'a'), 'LEFT_JOIN' => array(array('FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = a.album_user_id')), 'WHERE' => 'a.album_user_id <> ' . phpbb_gallery_album::PUBLIC_ALBUM . ' AND a.parent_id = 0', 'ORDER_BY' => 'a.album_id DESC');
                        $sql = $db->sql_build_query('SELECT', $sql_array);
                        $result = $db->sql_query_limit($sql, 1);
                        $newest_pgallery = $db->sql_fetchrow($result);
                        $db->sql_freeresult($result);
                        phpbb_gallery_config::set('newest_pega_user_id', $newest_pgallery['user_id']);
                        phpbb_gallery_config::set('newest_pega_username', $newest_pgallery['username']);
                        phpbb_gallery_config::set('newest_pega_user_colour', $newest_pgallery['user_colour']);
                        phpbb_gallery_config::set('newest_pega_album_id', $newest_pgallery['album_id']);
                    } else {
                        phpbb_gallery_config::set('newest_pega_user_id', 0);
                        phpbb_gallery_config::set('newest_pega_username', '');
                        phpbb_gallery_config::set('newest_pega_user_colour', '');
                        phpbb_gallery_config::set('newest_pega_album_id', 0);
                    }
                }
                $user_ids = array();
                foreach ($user_image_count as $user_id => $images) {
                    $user_ids[] = (int) $user_id;
                    phpbb_gallery_hookup::add_image($user_id, 0 - $images);
                    $uploader = new phpbb_gallery_user($db, $user_id, false);
                    $uploader->update_images(0 - $images);
                }
                phpbb_gallery_user_helpers::update_users($user_ids, array('personal_album_id' => 0));
                if ($missing_personals) {
                    $message[] = $user->lang['CLEAN_PERSONALS_DONE'];
                }
                if ($personals_bad) {
                    $message[] = $user->lang['CLEAN_PERSONALS_BAD_DONE'];
                }
            }
            // Make sure the overall image & comment count is correct...
            $sql = 'SELECT COUNT(image_id) AS num_images, SUM(image_comments) AS num_comments
				FROM ' . GALLERY_IMAGES_TABLE . '
				WHERE image_status <> ' . phpbb_gallery_image::STATUS_UNAPPROVED;
            $result = $db->sql_query($sql);
            $row = $db->sql_fetchrow($result);
            $db->sql_freeresult($result);
            phpbb_gallery_config::set('num_images', $row['num_images']);
            phpbb_gallery_config::set('num_comments', $row['num_comments']);
            $cache->destroy('sql', GALLERY_ALBUMS_TABLE);
            $cache->destroy('sql', GALLERY_COMMENTS_TABLE);
            $cache->destroy('sql', GALLERY_FAVORITES_TABLE);
            $cache->destroy('sql', GALLERY_IMAGES_TABLE);
            $cache->destroy('sql', GALLERY_RATES_TABLE);
            $cache->destroy('sql', GALLERY_REPORTS_TABLE);
            $cache->destroy('sql', GALLERY_WATCH_TABLE);
            $cache->destroy('_albums');
            phpbb_gallery_auth::set_user_permissions('all', '');
            trigger_error(implode('<br />', $message) . adm_back_link($this->u_action));
        } else {
            if ($delete || isset($_POST['cancel'])) {
                if (isset($_POST['cancel'])) {
                    trigger_error($user->lang['CLEAN_GALLERY_ABORT'] . adm_back_link($this->u_action), E_USER_WARNING);
                } else {
                    $user->lang['CLEAN_GALLERY_CONFIRM'] = $user->lang['CONFIRM_CLEAN'];
                    if ($missing_sources) {
                        $user->lang['CLEAN_GALLERY_CONFIRM'] = $user->lang['CONFIRM_CLEAN_SOURCES'] . '<br />' . $user->lang['CLEAN_GALLERY_CONFIRM'];
                    }
                    if ($missing_entries) {
                        $user->lang['CLEAN_GALLERY_CONFIRM'] = $user->lang['CONFIRM_CLEAN_ENTRIES'] . '<br />' . $user->lang['CLEAN_GALLERY_CONFIRM'];
                    }
                    if ($missing_authors) {
                        $user->lang['CLEAN_GALLERY_CONFIRM'] = $user->lang['CONFIRM_CLEAN_AUTHORS'] . '<br />' . $user->lang['CLEAN_GALLERY_CONFIRM'];
                    }
                    if ($missing_comments) {
                        $user->lang['CLEAN_GALLERY_CONFIRM'] = $user->lang['CONFIRM_CLEAN_COMMENTS'] . '<br />' . $user->lang['CLEAN_GALLERY_CONFIRM'];
                    }
                    if ($personals_bad || $missing_personals) {
                        $sql = 'SELECT album_name, album_user_id
						FROM ' . GALLERY_ALBUMS_TABLE . '
						WHERE ' . $db->sql_in_set('album_user_id', array_merge($missing_personals, $personals_bad));
                        $result = $db->sql_query($sql);
                        while ($row = $db->sql_fetchrow($result)) {
                            if (in_array($row['album_user_id'], $personals_bad)) {
                                $personals_bad_names[] = $row['album_name'];
                            } else {
                                $missing_personals_names[] = $row['album_name'];
                            }
                        }
                        $db->sql_freeresult($result);
                    }
                    if ($missing_personals) {
                        $user->lang['CLEAN_GALLERY_CONFIRM'] = sprintf($user->lang['CONFIRM_CLEAN_PERSONALS'], implode(', ', $missing_personals_names)) . '<br />' . $user->lang['CLEAN_GALLERY_CONFIRM'];
                    }
                    if ($personals_bad) {
                        $user->lang['CLEAN_GALLERY_CONFIRM'] = sprintf($user->lang['CONFIRM_CLEAN_PERSONALS_BAD'], implode(', ', $personals_bad_names)) . '<br />' . $user->lang['CLEAN_GALLERY_CONFIRM'];
                    }
                    confirm_box(false, 'CLEAN_GALLERY', $s_hidden_fields);
                }
            }
        }
        $requested_source = array();
        $sql_array = array('SELECT' => 'i.image_id, i.image_name, i.image_filemissing, i.image_filename, i.image_username, u.user_id', 'FROM' => array(GALLERY_IMAGES_TABLE => 'i'), 'LEFT_JOIN' => array(array('FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = i.image_user_id')));
        $sql = $db->sql_build_query('SELECT', $sql_array);
        $result = $db->sql_query($sql);
        while ($row = $db->sql_fetchrow($result)) {
            if ($row['image_filemissing']) {
                $template->assign_block_vars('sourcerow', array('IMAGE_ID' => $row['image_id'], 'IMAGE_NAME' => $row['image_name']));
            }
            if (!$row['user_id']) {
                $template->assign_block_vars('authorrow', array('IMAGE_ID' => $row['image_id'], 'AUTHOR_NAME' => $row['image_username']));
            }
            $requested_source[] = $row['image_filename'];
        }
        $db->sql_freeresult($result);
        $check_mode = request_var('check_mode', '');
        if ($check_mode == 'source') {
            $source_missing = array();
            // Reset the status: a image might have been viewed without file but the file is back
            $sql = 'UPDATE ' . GALLERY_IMAGES_TABLE . '
				SET image_filemissing = 0';
            $db->sql_query($sql);
            $sql = 'SELECT image_id, image_filename, image_filemissing
				FROM ' . GALLERY_IMAGES_TABLE;
            $result = $db->sql_query($sql);
            while ($row = $db->sql_fetchrow($result)) {
                if (!file_exists(phpbb_gallery_url::path('upload') . $row['image_filename'])) {
                    $source_missing[] = $row['image_id'];
                }
            }
            $db->sql_freeresult($result);
            if ($source_missing) {
                $sql = 'UPDATE ' . GALLERY_IMAGES_TABLE . "\n\t\t\t\t\tSET image_filemissing = 1\n\t\t\t\t\tWHERE " . $db->sql_in_set('image_id', $source_missing);
                $db->sql_query($sql);
            }
        }
        if ($check_mode == 'entry') {
            $directory = phpbb_gallery_url::path('upload');
            $handle = opendir($directory);
            while ($file = readdir($handle)) {
                if (!is_dir($directory . $file) && (substr(strtolower($file), '-4') == '.png' || substr(strtolower($file), '-4') == '.gif' || substr(strtolower($file), '-4') == '.jpg') && !in_array($file, $requested_source)) {
                    if (strpos($file, 'image_not_exist') !== false || strpos($file, 'not_authorised') !== false || strpos($file, 'no_hotlinking') !== false) {
                        continue;
                    }
                    $template->assign_block_vars('entryrow', array('FILE_NAME' => utf8_encode($file)));
                }
            }
            closedir($handle);
        }
        $sql_array = array('SELECT' => 'c.comment_id, c.comment_image_id, c.comment_username, u.user_id', 'FROM' => array(GALLERY_COMMENTS_TABLE => 'c'), 'LEFT_JOIN' => array(array('FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = c.comment_user_id')));
        $sql = $db->sql_build_query('SELECT', $sql_array);
        $result = $db->sql_query($sql);
        while ($row = $db->sql_fetchrow($result)) {
            if (!$row['user_id']) {
                $template->assign_block_vars('commentrow', array('COMMENT_ID' => $row['comment_id'], 'IMAGE_ID' => $row['comment_image_id'], 'AUTHOR_NAME' => $row['comment_username']));
            }
        }
        $db->sql_freeresult($result);
        $sql_array = array('SELECT' => 'a.album_id, a.album_user_id, a.album_name, u.user_id, a.album_images_real', 'FROM' => array(GALLERY_ALBUMS_TABLE => 'a'), 'LEFT_JOIN' => array(array('FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = a.album_user_id')), 'WHERE' => 'a.album_user_id <> ' . phpbb_gallery_album::PUBLIC_ALBUM . ' AND a.parent_id = 0');
        $sql = $db->sql_build_query('SELECT', $sql_array);
        $result = $db->sql_query($sql);
        $personalrow = $personal_bad_row = array();
        while ($row = $db->sql_fetchrow($result)) {
            $album = array('user_id' => $row['album_user_id'], 'album_id' => $row['album_id'], 'album_name' => $row['album_name'], 'images' => $row['album_images_real']);
            if (!$row['user_id']) {
                $personalrow[$row['album_user_id']] = $album;
            }
            $personal_bad_row[$row['album_user_id']] = $album;
        }
        $db->sql_freeresult($result);
        $sql = 'SELECT ga.album_user_id, ga.album_images_real
			FROM ' . GALLERY_ALBUMS_TABLE . ' ga
			WHERE ga.album_user_id <> ' . phpbb_gallery_album::PUBLIC_ALBUM . '
				AND ga.parent_id <> 0';
        $result = $db->sql_query($sql);
        while ($row = $db->sql_fetchrow($result)) {
            if (isset($personalrow[$row['album_user_id']])) {
                $personalrow[$row['album_user_id']]['images'] = $personalrow[$row['album_user_id']]['images'] + $row['album_images_real'];
            }
            $personal_bad_row[$row['album_user_id']]['images'] = $personal_bad_row[$row['album_user_id']]['images'] + $row['album_images_real'];
        }
        $db->sql_freeresult($result);
        foreach ($personalrow as $key => $row) {
            $template->assign_block_vars('personalrow', array('USER_ID' => $row['user_id'], 'ALBUM_ID' => $row['album_id'], 'AUTHOR_NAME' => $row['album_name']));
        }
        foreach ($personal_bad_row as $key => $row) {
            $template->assign_block_vars('personal_bad_row', array('USER_ID' => $row['user_id'], 'ALBUM_ID' => $row['album_id'], 'AUTHOR_NAME' => $row['album_name'], 'IMAGES' => $row['images']));
        }
        $template->assign_vars(array('S_GALLERY_MANAGE_RESTS' => true, 'ACP_GALLERY_TITLE' => $user->lang['ACP_GALLERY_CLEANUP'], 'ACP_GALLERY_TITLE_EXPLAIN' => $user->lang['ACP_GALLERY_CLEANUP_EXPLAIN'], 'CHECK_SOURCE' => $this->u_action . '&amp;check_mode=source', 'CHECK_ENTRY' => $this->u_action . '&amp;check_mode=entry', 'S_FOUNDER' => $user->data['user_type'] == USER_FOUNDER ? true : false));
    }
    function delete_album()
    {
        global $cache, $db, $template, $user;
        $s_hidden_fields = build_hidden_fields(array('album_id' => request_var('album_id', 0)));
        if (confirm_box(true)) {
            $album_id = request_var('album_id', 0);
            $left_id = $right_id = 0;
            $deleted_images_na = '';
            $deleted_albums = array();
            // Check for owner
            $sql = 'SELECT album_id, left_id, right_id, parent_id
				FROM ' . GALLERY_ALBUMS_TABLE . '
				WHERE album_user_id = ' . $user->data['user_id'] . '
				ORDER BY left_id ASC';
            $result = $db->sql_query($sql);
            while ($row = $db->sql_fetchrow($result)) {
                $album[] = $row;
                if ($row['album_id'] == $album_id) {
                    $left_id = $row['left_id'];
                    $right_id = $row['right_id'];
                    $parent_id = $row['parent_id'];
                }
            }
            $db->sql_freeresult($result);
            for ($i = 0, $end = count($album); $i < $end; $i++) {
                if ($left_id <= $album[$i]['left_id'] && $album[$i]['left_id'] <= $right_id) {
                    $deleted_albums[] = $album[$i]['album_id'];
                }
            }
            // $deleted_albums is the array of albums we are going to delete.
            // Now get the images in $deleted_images
            $sql = 'SELECT image_id, image_filename
				FROM ' . GALLERY_IMAGES_TABLE . '
				WHERE ' . $db->sql_in_set('image_album_id', $deleted_albums) . '
				ORDER BY image_id ASC';
            $result = $db->sql_query($sql);
            $deleted_images = $filenames = array();
            while ($row = $db->sql_fetchrow($result)) {
                $deleted_images[] = $row['image_id'];
                $filenames[(int) $row['image_id']] = $row['image_filename'];
            }
            // We have all image_ids in $deleted_images which are deleted.
            // Aswell as the album_ids in $deleted_albums.
            // So now drop the comments, ratings, images and albums.
            if (!empty($deleted_images)) {
                $sql = 'DELETE FROM ' . GALLERY_COMMENTS_TABLE . '
					WHERE ' . $db->sql_in_set('comment_image_id', $deleted_images);
                $db->sql_query($sql);
                $sql = 'DELETE FROM ' . GALLERY_REPORTS_TABLE . '
					WHERE ' . $db->sql_in_set('report_image_id', $deleted_images);
                $db->sql_query($sql);
                $sql = 'DELETE FROM ' . GALLERY_FAVORITES_TABLE . '
					WHERE ' . $db->sql_in_set('image_id', $deleted_images);
                $db->sql_query($sql);
                $sql = 'DELETE FROM ' . GALLERY_WATCH_TABLE . '
					WHERE ' . $db->sql_in_set('image_id', $deleted_images);
                $db->sql_query($sql);
                phpbb_gallery_image_base::delete_images($deleted_images, $filenames);
            }
            $sql = 'DELETE FROM ' . GALLERY_ALBUMS_TABLE . '
				WHERE ' . $db->sql_in_set('album_id', $deleted_albums);
            $db->sql_query($sql);
            // Make sure the overall image & comment count is correct...
            $sql = 'SELECT COUNT(image_id) AS num_images, SUM(image_comments) AS num_comments
				FROM ' . GALLERY_IMAGES_TABLE . '
				WHERE image_status <> ' . phpbb_gallery_image::STATUS_UNAPPROVED;
            $result = $db->sql_query($sql);
            $row = $db->sql_fetchrow($result);
            $db->sql_freeresult($result);
            phpbb_gallery_config::set('num_images', $row['num_images']);
            phpbb_gallery_config::set('num_comments', $row['num_comments']);
            $num_images = sizeof($deleted_images);
            if ($num_images) {
                phpbb_gallery_hookup::add_image($user->data['user_id'], 0 - $num_images);
                phpbb_gallery::$user->update_images(0 - $num_images);
            }
            // Maybe we deleted all, so we have to empty phpbb_gallery::$user->get_data('personal_album_id')
            if (in_array(phpbb_gallery::$user->get_data('personal_album_id'), $deleted_albums)) {
                phpbb_gallery::$user->update_data(array('personal_album_id' => 0));
                phpbb_gallery_config::dec('num_pegas', 1);
                if (phpbb_gallery_config::get('newest_pega_album_id') == phpbb_gallery::$user->get_data('personal_album_id')) {
                    // Update the config for the statistic on the index
                    if (phpbb_gallery_config::get('num_pegas') > 0) {
                        $sql_array = array('SELECT' => 'a.album_id, u.user_id, u.username, u.user_colour', 'FROM' => array(GALLERY_ALBUMS_TABLE => 'a'), 'LEFT_JOIN' => array(array('FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = a.album_user_id')), 'WHERE' => 'a.album_user_id <> ' . phpbb_gallery_album::PUBLIC_ALBUM . ' AND a.parent_id = 0', 'ORDER_BY' => 'a.album_id DESC');
                        $sql = $db->sql_build_query('SELECT', $sql_array);
                        $result = $db->sql_query_limit($sql, 1);
                        $newest_pgallery = $db->sql_fetchrow($result);
                        $db->sql_freeresult($result);
                        phpbb_gallery_config::set('newest_pega_user_id', $newest_pgallery['user_id']);
                        phpbb_gallery_config::set('newest_pega_username', $newest_pgallery['username']);
                        phpbb_gallery_config::set('newest_pega_user_colour', $newest_pgallery['user_colour']);
                        phpbb_gallery_config::set('newest_pega_album_id', $newest_pgallery['album_id']);
                    } else {
                        phpbb_gallery_config::set('newest_pega_user_id', 0);
                        phpbb_gallery_config::set('newest_pega_username', '');
                        phpbb_gallery_config::set('newest_pega_user_colour', '');
                        phpbb_gallery_config::set('newest_pega_album_id', 0);
                    }
                }
            } else {
                // Solve the left_id right_id problem
                $delete_id = $right_id - ($left_id - 1);
                $sql = 'UPDATE ' . GALLERY_ALBUMS_TABLE . "\n\t\t\t\t\tSET left_id = left_id - {$delete_id}\n\t\t\t\t\tWHERE left_id > {$left_id}\n\t\t\t\t\t\tAND album_user_id = " . $user->data['user_id'];
                $db->sql_query($sql);
                $sql = 'UPDATE ' . GALLERY_ALBUMS_TABLE . "\n\t\t\t\t\tSET right_id = right_id - {$delete_id}\n\t\t\t\t\tWHERE right_id > {$right_id}\n\t\t\t\t\t\tAND album_user_id = " . $user->data['user_id'];
                $db->sql_query($sql);
            }
            $cache->destroy('sql', GALLERY_ALBUMS_TABLE);
            $cache->destroy('sql', GALLERY_COMMENTS_TABLE);
            $cache->destroy('sql', GALLERY_FAVORITES_TABLE);
            $cache->destroy('sql', GALLERY_IMAGES_TABLE);
            $cache->destroy('sql', GALLERY_RATES_TABLE);
            $cache->destroy('sql', GALLERY_REPORTS_TABLE);
            $cache->destroy('sql', GALLERY_WATCH_TABLE);
            $cache->destroy('_albums');
            phpbb_gallery_auth::set_user_permissions('all', '');
            trigger_error($user->lang['DELETED_ALBUMS'] . '<br /><br />
				<a href="' . ($parent_id ? phpbb_gallery_url::append_sid('phpbb', 'ucp', 'i=gallery&amp;mode=manage_albums&amp;action=manage&amp;parent_id=' . $parent_id) : append_sid('phpbb', 'ucp', 'i=gallery&amp;mode=manage_albums')) . '">' . $user->lang['BACK_TO_PREV'] . '</a>');
        } else {
            $album_id = request_var('album_id', 0);
            phpbb_gallery_album::check_user($album_id);
            confirm_box(false, 'DELETE_ALBUM', $s_hidden_fields);
        }
    }
Example #3
0
                    add_log('gallery', $album_id, $row['image_id'], 'LOG_GALLERY_DELETED', $row['image_name']);
                }
                $db->sql_freeresult($result);
                $sql = 'DELETE FROM ' . GALLERY_COMMENTS_TABLE . '
					WHERE ' . $db->sql_in_set('comment_image_id', $image_id_ary);
                $db->sql_query($sql);
                $sql = 'DELETE FROM ' . GALLERY_REPORTS_TABLE . '
					WHERE ' . $db->sql_in_set('report_image_id', $image_id_ary);
                $db->sql_query($sql);
                $sql = 'DELETE FROM ' . GALLERY_FAVORITES_TABLE . '
					WHERE ' . $db->sql_in_set('image_id', $image_id_ary);
                $db->sql_query($sql);
                $sql = 'DELETE FROM ' . GALLERY_WATCH_TABLE . '
					WHERE ' . $db->sql_in_set('image_id', $image_id_ary);
                $db->sql_query($sql);
                phpbb_gallery_image_base::delete_images($image_id_ary, $filenames);
                $success = true;
            } else {
                confirm_box(false, 'QUEUE' . $multiple . '_A_DELETE2', $s_hidden_fields);
            }
            break;
        case 'reports_close':
            if (confirm_box(true)) {
                $sql_ary = array('report_manager' => $user->data['user_id'], 'report_status' => phpbb_gallery_constants::REPORT_LOCKED);
                $sql = 'UPDATE ' . GALLERY_REPORTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
					WHERE ' . $db->sql_in_set('report_id', $image_id_ary);
                $db->sql_query($sql);
                $sql = 'SELECT image_id, image_name
					FROM ' . GALLERY_IMAGES_TABLE . '
					WHERE ' . $db->sql_in_set('image_reported', $image_id_ary);
                $result = $db->sql_query($sql);
    /**
     * Delete album content:
     * Deletes all images, comments, rates, image-files, etc.
     */
    public function delete_album_content($album_id)
    {
        global $cache, $db;
        $album_id = (int) $album_id;
        // Before we remove anything we make sure we are able to adjust the image counts later. ;)
        $sql = 'SELECT image_user_id
			FROM ' . GALLERY_IMAGES_TABLE . '
			WHERE image_album_id = ' . $album_id . '
				AND image_status <> ' . phpbb_gallery_image::STATUS_UNAPPROVED;
        $result = $db->sql_query($sql);
        $image_counts = array();
        while ($row = $db->sql_fetchrow($result)) {
            $image_counts[$row['image_user_id']] = !empty($image_counts[$row['image_user_id']]) ? $image_counts[$row['image_user_id']] + 1 : 1;
        }
        $db->sql_freeresult($result);
        $sql = 'SELECT image_id, image_filename, image_album_id
			FROM ' . GALLERY_IMAGES_TABLE . '
			WHERE image_album_id = ' . $album_id;
        $result = $db->sql_query($sql);
        $filenames = $deleted_images = array();
        while ($row = $db->sql_fetchrow($result)) {
            $deleted_images[] = $row['image_id'];
            $filenames[(int) $row['image_id']] = $row['image_filename'];
        }
        $db->sql_freeresult($result);
        if (!empty($deleted_images)) {
            $sql = 'DELETE FROM ' . GALLERY_COMMENTS_TABLE . '
				WHERE ' . $db->sql_in_set('comment_image_id', $deleted_images);
            $db->sql_query($sql);
            $sql = 'DELETE FROM ' . GALLERY_FAVORITES_TABLE . '
				WHERE ' . $db->sql_in_set('image_id', $deleted_images);
            $db->sql_query($sql);
            $sql = 'DELETE FROM ' . GALLERY_REPORTS_TABLE . '
				WHERE ' . $db->sql_in_set('report_image_id', $deleted_images);
            $db->sql_query($sql);
            $sql = 'DELETE FROM ' . GALLERY_WATCH_TABLE . '
				WHERE ' . $db->sql_in_set('image_id', $deleted_images);
            $db->sql_query($sql);
            phpbb_gallery_image_base::delete_images($deleted_images, $filenames);
        }
        $sql = 'DELETE FROM ' . LOG_TABLE . "\n\t\t\tWHERE album_id = {$album_id}\n\t\t\t\tAND log_type = " . LOG_GALLERY;
        $db->sql_query($sql);
        //@todo: merge queries into loop
        $sql = 'DELETE FROM ' . GALLERY_PERMISSIONS_TABLE . '
			WHERE perm_album_id = ' . $album_id;
        $db->sql_query($sql);
        $sql = 'DELETE FROM ' . GALLERY_CONTESTS_TABLE . '
			WHERE contest_album_id = ' . $album_id;
        $db->sql_query($sql);
        $table_ary = array(GALLERY_WATCH_TABLE, GALLERY_MODSCACHE_TABLE);
        foreach ($table_ary as $table) {
            $db->sql_query("DELETE FROM {$table} WHERE album_id = {$album_id}");
        }
        // Adjust users image counts
        if (!empty($image_counts)) {
            foreach ($image_counts as $image_user_id => $substract) {
                $sql = 'UPDATE ' . GALLERY_USERS_TABLE . '
					SET user_images = 0
					WHERE user_id = ' . $image_user_id . '
						AND user_images < ' . $substract;
                $db->sql_query($sql);
                $sql = 'UPDATE ' . GALLERY_USERS_TABLE . '
					SET user_images = user_images - ' . $substract . '
					WHERE user_id = ' . $image_user_id . '
						AND user_images >= ' . $substract;
                $db->sql_query($sql);
            }
        }
        // Make sure the overall image & comment count is correct...
        $sql = 'SELECT COUNT(image_id) AS num_images, SUM(image_comments) AS num_comments
			FROM ' . GALLERY_IMAGES_TABLE . '
			WHERE image_status <> ' . phpbb_gallery_image::STATUS_UNAPPROVED;
        $result = $db->sql_query($sql);
        $row = $db->sql_fetchrow($result);
        $db->sql_freeresult($result);
        phpbb_gallery_config::set('num_images', $row['num_images']);
        phpbb_gallery_config::set('num_comments', $row['num_comments']);
        $cache->destroy('sql', GALLERY_ALBUMS_TABLE);
        $cache->destroy('sql', GALLERY_COMMENTS_TABLE);
        $cache->destroy('sql', GALLERY_FAVORITES_TABLE);
        $cache->destroy('sql', GALLERY_IMAGES_TABLE);
        $cache->destroy('sql', GALLERY_RATES_TABLE);
        $cache->destroy('sql', GALLERY_REPORTS_TABLE);
        $cache->destroy('sql', GALLERY_WATCH_TABLE);
        $cache->destroy('_albums');
        return array();
    }
Example #5
0
     }
     break;
 case 'delete':
     if ($submode == 'delete') {
         $s_hidden_fields = build_hidden_fields(array('album_id' => $album_id, 'image_id' => $image_id, 'mode' => 'image', 'submode' => 'delete'));
         if (confirm_box(true)) {
             handle_image_counter($image_id, false);
             $sql = 'DELETE FROM ' . GALLERY_COMMENTS_TABLE . "\n\t\t\t\t\t\tWHERE comment_image_id = {$image_id}";
             $db->sql_query($sql);
             $sql = 'DELETE FROM ' . GALLERY_FAVORITES_TABLE . "\n\t\t\t\t\t\tWHERE image_id = {$image_id}";
             $db->sql_query($sql);
             $sql = 'DELETE FROM ' . GALLERY_REPORTS_TABLE . "\n\t\t\t\t\t\tWHERE report_image_id = {$image_id}";
             $db->sql_query($sql);
             $sql = 'DELETE FROM ' . GALLERY_WATCH_TABLE . "\n\t\t\t\t\t\tWHERE image_id = {$image_id}";
             $db->sql_query($sql);
             phpbb_gallery_image_base::delete_images(array($image_id), array($image_id => $image_data['image_filename']));
             update_album_info($album_id);
             $submit = true;
             $message = $user->lang['DELETED_IMAGE'] . '<br />';
             $image_id = false;
             if ($user->data['user_id'] != $image_data['image_user_id']) {
                 add_log('gallery', $image_data['image_album_id'], $image_id, 'LOG_GALLERY_DELETED', $image_data['image_name']);
             }
         } else {
             if (isset($_POST['cancel'])) {
                 $message = $user->lang['DELETED_IMAGE_NOT'] . '<br />';
                 $submit = true;
             } else {
                 confirm_box(false, 'DELETE_IMAGE2', $s_hidden_fields);
             }
         }