function upgrade_images($num, $list)
{
    $output = array();
    $errors = array();
    $count = 0;
    $list = array_slice($list, 0, $num);
    foreach ($list as $image) {
        if (!empty($image['id'])) {
            // Work on the images - move physical file, create directory if necessary and update path in database
            if (!makeDirs(PLOGGER_DIR . 'plog-content/images/' . dirname($image['new_path'] . '/'))) {
                $errors[] = plog_tr('Could not create directory') . ': ' . PLOGGER_DIR . 'plog-content/images/' . $image['new_path'];
            } else {
                if (!move_this(PLOGGER_DIR . $image['old_path'], PLOGGER_DIR . 'plog-content/images/' . $image['new_path'])) {
                    $errors[] = plog_tr('Could not move file') . ': ' . PLOGGER_DIR . $image['old_path'];
                } else {
                    @chmod(PLOGGER_DIR . $new_path, PLOGGER_CHMOD_DIR);
                    $output[] = sprintf(plog_tr('Moved file %s -> %s'), '<strong>' . $image['old_path'] . '</strong>', '<strong>' . 'plog-content/images/' . $image['new_path'] . '</strong>');
                    // Update database
                    $sql = "UPDATE " . PLOGGER_TABLE_PREFIX . "pictures SET path = '" . mysql_real_escape_string($image['new_path']) . "' WHERE id = '" . $image['id'] . "'";
                    run_query($sql);
                    // Generate a new small thumbnail after database has been updated in case script times out
                    $thumbpath = generate_thumb($image['new_path'], $image['id'], THUMB_SMALL);
                    $count++;
                }
            }
        } else {
            if (!empty($image['uploads'])) {
                // Work on the uploads - move physical file and create directory in the uploads folder if necessary and update path in database
                if (!makeDirs(PLOGGER_DIR . dirname($image['new_path'] . '/'))) {
                    $errors[] = plog_tr('Could not create directory') . ': ' . PLOGGER_DIR . $image['new_path'];
                } else {
                    if (!move_this(PLOGGER_DIR . $image['old_path'], PLOGGER_DIR . $image['new_path'])) {
                        $errors[] = plog_tr('Could not move file') . ': ' . PLOGGER_DIR . $image['old_path'];
                    } else {
                        @chmod(PLOGGER_DIR . $new_path, PLOGGER_CHMOD_DIR);
                        $output[] = sprintf(plog_tr('Moved file %s -> %s'), '<strong>' . $image['old_path'] . '</strong>', '<strong>' . $image['new_path'] . '</strong>');
                        $count++;
                    }
                }
            } else {
                if (!empty($image['container'])) {
                    // Create the collection and album directory structure
                    if (!makeDirs(PLOGGER_DIR . $image['new_path'] . '/')) {
                        $errors[] = plog_tr('Could not create directory') . ': ' . PLOGGER_DIR . $image['new_path'];
                    } else {
                        $output[] = sprintf(plog_tr('Created directory %s'), '<strong>' . $image['new_path'] . '</strong>');
                        $count++;
                    }
                }
            }
        }
    }
    return array('errors' => $errors, 'output' => $output, 'count' => $count);
}
function move_album($album_id, $to_collection)
{
    global $config, $PLOGGER_DBH;
    $res = array('errors' => '', 'output' => '');
    $album_id = intval($album_id);
    $to_collection = intval($to_collection);
    $sql = "SELECT\n\t\t\t\tc.path as collection_path,\n\t\t\t\tc.thumbnail_id as collection_thumb,\n\t\t\t\tc.id as collection_id,\n\t\t\t\ta.path as album_path\n\t\t\tFROM " . PLOGGER_TABLE_PREFIX . "albums a, " . PLOGGER_TABLE_PREFIX . "collections c\n\t\t\tWHERE c.id = a.parent_id AND a.id = '{$album_id}'";
    $result = run_query($sql);
    $row = $result->fetch();
    $source_album_name = SmartStripSlashes($row['album_path']);
    $source_collection_name = SmartStripSlashes($row['collection_path']);
    $source_collection_thumb = $row['collection_thumb'];
    $source_collection_id = $row['collection_id'];
    // If moving to same collection, abort
    if ($to_collection == $source_collection_id) {
        return;
    }
    // Next, get the collection name of our destination collection
    $sql = "SELECT c.path as collection_path FROM " . PLOGGER_TABLE_PREFIX . "collections c WHERE c.id = '{$to_collection}'";
    $result = run_query($sql);
    $row = $result->fetch();
    $target_collection_name = SmartStripSlashes($row['collection_path']);
    $source_path = $config['basedir'] . 'plog-content/images/' . $source_collection_name . '/' . $source_album_name . '/';
    $target_path = $config['basedir'] . 'plog-content/images/' . $target_collection_name . '/' . $source_album_name . '/';
    $thumb_path = $config['basedir'] . 'plog-content/thumbs/' . $source_collection_name . '/' . $source_album_name . '/';
    // Check path so we are not creating duplicate albums within the same collection
    if (is_dir($target_path)) {
        // If there is already a directory, check to see if it's in the database
        $album_data = get_album_by_name($source_album_name, $to_collection);
        if ($album_data) {
            // It's in the database, so throw duplicate album error
            return array('errors' => sprintf(plog_tr('New album could not be created, because there is already one named %s in the collection %s'), '<strong>' . $source_album_name . '</strong>', '<strong>' . $target_collection_name . '</strong>'));
        } else {
            // It's not in the database so attempt to delete the directory
            if (!kill_dir($target_path)) {
                // Could not delete the directory, so prompt the user to delete it manually
                return array('errors' => sprintf(plog_tr('Album directory %s exists, but no album exists in the database. Attempt to delete automatically failed. Please delete folder via FTP manually and try again.'), '<strong>' . $target_path . '</strong>'));
            }
        }
    }
    // Attempt to make new album directory in target collection
    if (!makeDirs($target_path)) {
        return array('errors' => sprintf(plog_tr('Could not create directory %s!'), '<strong>' . $target_path . '</strong>'));
    }
    // Now we need to update the database paths of all pictures within source album
    $sql = "SELECT p.path as path, p.id as picture_id, c.name as collection_name, a.name as album_name\n\t\tFROM " . PLOGGER_TABLE_PREFIX . "albums a, " . PLOGGER_TABLE_PREFIX . "pictures p, " . PLOGGER_TABLE_PREFIX . "collections c\n\t\tWHERE p.parent_album = a.id AND p.parent_collection = c.id AND p.parent_album = '{$album_id}'";
    $result = run_query($sql);
    $pic_ids = array();
    while ($row = $result->fetch()) {
        $filename = SmartStripSlashes(basename($row['path']));
        $pic_ids[] = $row['picture_id'];
        $old_path = $source_path . $filename;
        $new_path = $target_path . $filename;
        if (!move_this($old_path, $new_path)) {
            $res['errors'] .= sprintf(plog_tr('Could not move file: %s to %s'), '<strong>' . $old_path . '</strong>', '<strong>' . $new_path . '</strong>');
        } else {
            @chmod($new_path, PLOGGER_CHMOD_FILE);
        }
        $path_insert = $PLOGGER_DBH->quote($target_collection_name . '/' . $source_album_name . '/' . $filename);
        $sql = "UPDATE " . PLOGGER_TABLE_PREFIX . "pictures SET\n\t\t\t\tparent_collection = '{$to_collection}',\n\t\t\t\tpath = '{$path_insert}'\n\t\t\tWHERE id = '{$row['picture_id']}'";
        run_query($sql, false);
    }
    // Check if collection thumbnail = picture moved to different collection and set to default if so
    if (in_array($source_collection_thumb, $pic_ids)) {
        $query = "UPDATE " . PLOGGER_TABLE_PREFIX . "collections SET \"thumbnail_id\"='0' WHERE id='" . $source_collection_id . "'";
        run_query($query);
    }
    // Update the parent id of the moved album
    $query = "UPDATE " . PLOGGER_TABLE_PREFIX . "albums SET \"parent_id\" = '{$to_collection}' WHERE \"id\"='{$album_id}'";
    $result = run_query($query);
    // Attempt to delete the old folder and thumbnails if there were no errors moving the files
    if ($res['errors'] == '') {
        kill_dir($thumb_path);
        $remove = kill_dir($source_path);
        if (!$remove) {
            $res['errors'] .= sprintf(plog_tr('Could not remove album from collection %s. Album still contains files after all pictures have been moved.'), '<strong>' . $source_collection_name . '</strong>');
        }
    }
    return $res;
}