function add_image($album, $filename, $caption)
{
    $filedat = $_FILES['userfile'];
    $albuminfo = get_album_by_name($album);
    $src = $filedat['tmp_name'];
    $result = add_picture($albuminfo['id'], $_FILES['userfile']['tmp_name'], $_FILES['userfile']['name'], $caption);
    global $debug_msgs;
    $debug_msgs .= print_r($result, true);
    // And this is the place where I need the image data
    global $response;
    if ($result['picture_id'] === false) {
        $response->set_key('status', GR_STAT_UPLOAD_PHOTO_FAIL);
        $response->set_key('status_text', 'Add photo failed.');
    } else {
        $response->set_key('status', GR_STAT_SUCCESS);
        // galleryadd.pl looks for this exact status text and fails if it doesn't find it
        $response->set_key('status_text', 'Add photo successful.');
    }
}
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;
}