Ejemplo n.º 1
0
    /**
     * @dataProvider user_helpers_update_users_data
     */
    public function test_user_helpers_update_users($user_ids, $data, $expected)
    {
        global $db;
        $db = $this->new_dbal();
        $this->assertEquals(true, phpbb_gallery_user_helpers::update_users($user_ids, $data));
        $result = $db->sql_query('SELECT user_id, user_images
			FROM ' . phpbb_gallery_user_base::sql_table());
        $this->assertEquals($expected, $db->sql_fetchrowset($result));
    }
Ejemplo n.º 2
0
    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));
    }