function update_collection($collection_id, $name, $description, $thumbnail_id = 0)
{
    global $config;
    $errors = $output = "";
    $name = trim(SmartStripSlashes($name));
    if (empty($name)) {
        return array("errors" => plog_tr("Please enter a valid name for the collection"));
    }
    $target_name = strtolower(sanitize_filename($name));
    $errors = $output = "";
    $collection_id = intval($collection_id);
    $thumbnail_id = intval($thumbnail_id);
    $name = mysql_real_escape_string($name);
    $description = mysql_real_escape_string($description);
    // rename the directory
    // first, get the collection name of our source collection
    $sql = "SELECT c.path as collection_path,name\r\n\t\t\tFROM " . TABLE_PREFIX . "collections c\r\n\t\t\tWHERE c.id = '{$collection_id}'";
    $result = run_query($sql);
    $row = mysql_fetch_assoc($result);
    // do not allow collections with duplicate names, otherwise mod_rewritten links will start
    // to behave weird.
    $collection_exists = get_collection_by_name($name);
    if ($row["name"] != $name && $collection_exists) {
        return array("errors" => sprintf(plog_tr('Collection `%s could not be renamed to `%s, because there is another collection with that name'), $row['name'], $name));
    }
    $source_collection_name = SmartStripSlashes($row["collection_path"]);
    $source_path = $config["basedir"] . "images/" . $source_collection_name;
    $target_path = $config["basedir"] . "images/" . $target_name;
    // perform the rename on the directory
    if (!rename($source_path, $target_path)) {
        return array("errors" => sprintf(plog_tr("Error renaming directory! (%s to %s)"), $source_path, $target_path));
    }
    $target_name = mysql_real_escape_string($target_name);
    $query = "UPDATE " . TABLE_PREFIX . "collections SET name = '{$name}', path = '{$target_name}', description = '{$description}', thumbnail_id = '{$thumbnail_id}' WHERE id='{$collection_id}'";
    $result = mysql_query($query);
    if (!$result) {
        return array("errors" => mysql_error());
    }
    $output = plog_tr('You have successfully modified the selected collection.');
    // XXX: Update the path only if a collection was actually renamed
    // update the path field for all pictures within that collection
    // now we need to update the database paths of all pictures within source album
    $sql = "SELECT p.id AS id,p.path AS path, c.name AS collection_name, a.path AS album_path\r\n\t\tFROM " . TABLE_PREFIX . "albums a, " . TABLE_PREFIX . "pictures p, " . TABLE_PREFIX . "collections c\r\n\t\tWHERE p.parent_album = a.id AND p.parent_collection = c.id AND p.parent_collection = '{$collection_id}'";
    $result = run_query($sql);
    while ($row = mysql_fetch_assoc($result)) {
        $filename = basename(SmartStripSlashes($row['path']));
        $album_path = $row['album_path'];
        $new_path = mysql_escape_string($target_name . "/" . $album_path . "/" . $filename);
        // update database
        $sql = "UPDATE " . TABLE_PREFIX . "pictures SET path = '{$new_path}' WHERE id = '{$row['id']}'";
        mysql_query($sql) or $output .= mysql_error();
    }
    return array("errors" => $errors, "output" => $output);
}
function update_collection($collection_id, $name, $description, $thumbnail_id = 0)
{
    global $config, $PLOGGER_DBH;
    $errors = $output = '';
    $name = trim(SmartStripSlashes($name));
    if (empty($name)) {
        return array('errors' => plog_tr('Please enter a valid name for the collection.'));
    }
    $target_name = strtolower(sanitize_filename($name));
    $errors = $output = '';
    $collection_id = intval($collection_id);
    $thumbnail_id = intval($thumbnail_id);
    $name = $PLOGGER_DBH->quote($name);
    $description = $PLOGGER_DBH->quote($description);
    // Rename the directory
    // First, get the collection name of our source collection
    $sql = "SELECT c.path as collection_path, name\n\t\t\tFROM " . PLOGGER_TABLE_PREFIX . "collections c\n\t\t\tWHERE c.id = '{$collection_id}'";
    $result = run_query($sql);
    $row = $result->fetch();
    $source_collection_name = SmartStripSlashes($row['collection_path']);
    $source_path = $config['basedir'] . 'plog-content/images/' . $source_collection_name;
    $target_path = $config['basedir'] . 'plog-content/images/' . $target_name;
    // Check for self-renaming collection instance
    if ($source_path != $target_path) {
        // Do not allow collections with duplicate names, otherwise mod_rewritten links will start
        // to behave weird.
        if (is_dir($target_path)) {
            // If there is already a directory, check to see if it's in the database
            $collection_data = get_collection_by_name($name);
            if ($collection_data) {
                // It's in the database, so throw duplicate collection error
                return array('errors' => sprintf(plog_tr('Collection %s could not be renamed to %s, because there is another collection with that name.'), '<strong>' . $row['name'] . '</strong>', '<strong>' . $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('Collection directory %s exists, but no collection exists in the database. Attempt to delete automatically failed. Please delete folder via FTP manually and try again.'), '<strong>' . $target_path . '</strong>'));
                }
            }
        }
        // Perform the rename on the directory
        if (!move_this($source_path, $target_path)) {
            return array('errors' => sprintf(plog_tr('Error renaming directory: %s to %s'), '<strong>' . $source_path . '</strong>', '<strong>' . $target_path . '</strong>'));
        }
    }
    $target_name = $PLOGGER_DBH->quote($target_name);
    $query = "UPDATE " . PLOGGER_TABLE_PREFIX . "collections SET name = {$name}, path = {$target_name}, description = {$description}, thumbnail_id = '{$thumbnail_id}' WHERE id='{$collection_id}'";
    $result = run_query($query, false, "");
    if (!$result) {
        return array('errors' => 'Failed to modify collection - DB Error');
    }
    $output = plog_tr('You have successfully modified the selected collection.');
    // XXX: Update the path only if a collection was actually renamed
    // Update the path field for all pictures within that collection
    // Now we need to update the database paths of all pictures within source album
    $sql = "SELECT p.id AS id, p.path AS path, c.name AS collection_name, a.path AS album_path\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_collection = '{$collection_id}'";
    $result = run_query($sql);
    while ($row = $result->fetch()) {
        $filename = basename($row['path']);
        $album_path = $row['album_path'];
        $new_path = $PLOGGER_DBH->quote(SmartStripSlashes($target_name . '/' . $album_path . '/' . $filename));
        // Update database
        $sql = "UPDATE " . PLOGGER_TABLE_PREFIX . "pictures SET path = {$new_path} WHERE id = '{$row['id']}'";
        run_query($sql, false, "") or $output .= "DB Error - Failed to update table.";
    }
    return array('errors' => $errors, 'output' => $output);
}