function wppa_create_pl_htaccess($pl_dirname = '')
{
    global $wpdb;
    $tim = time();
    // Only supported on single sites at the moment
    if (is_multisite() && !WPPA_MULTISITE_GLOBAL) {
        return false;
    }
    // If there are more than 200 albums, we do this as a cron job
    $n_albs = $wpdb->get_var("SELECT COUNT(*) FROM `" . WPPA_ALBUMS . "`");
    // Big site?
    if ($n_albs > 200) {
        // Are we a cron job?
        if (wppa_is_cron()) {
            // Remake required?
            if (!get_option('wppa_pl_htaccess_required')) {
                return false;
            }
        } else {
            // Tell cron it must be done
            update_option('wppa_pl_htaccess_required', true);
            return false;
        }
    } else {
        // In cron?
        if (wppa_is_cron()) {
            return false;
        }
    }
    // Either big in cron and required, or small and realtime
    // Where are the photo source files?
    $source_root = str_replace(ABSPATH, '', wppa_opt('source_dir'));
    // Find permalink root name
    if (!$pl_dirname) {
        $pl_dirname = wppa_opt('pl_dirname');
    }
    // If no pl_dirname, feature is disabled
    if (!$pl_dirname) {
        return false;
    }
    // Create pl root directory
    $pl_root = WPPA_CONTENT_PATH . '/' . $pl_dirname;
    if (!wppa_mktree($pl_root)) {
        wppa_log('Error', 'Can not create ' . $pl_root);
        return false;
    }
    // Create .htaccess file
    $file = fopen($pl_root . '/.htaccess', 'wb');
    if (!$file) {
        wppa_log('Error', 'Can not create ' . $pl_root . '/.htaccess');
        return false;
    }
    fwrite($file, '<IfModule mod_rewrite.c>');
    fwrite($file, "\n" . 'RewriteEngine On');
    // RewriteBase /wp-content/wppa-pl
    fwrite($file, "\n" . 'RewriteBase /' . str_replace(ABSPATH, '', $pl_root));
    $albs = $wpdb->get_results("SELECT `id`, `name` FROM `" . WPPA_ALBUMS . "` ORDER BY `name` DESC", ARRAY_A);
    if ($albs) {
        foreach ($albs as $alb) {
            $fm = wppa_get_album_name_for_pl($alb['id'], $alb['name']);
            $to = $source_root . '/album-' . $alb['id'];
            fwrite($file, "\n" . 'RewriteRule ^' . $fm . '/(.*) /' . $to . '/$1 [NC]');
        }
    }
    fwrite($file, "\n" . '</IfModule>');
    fclose($file);
    // Remove required flag
    delete_option('wppa_pl_htaccess_required');
    wppa_log('Dbg', 'Create pl_htaccess took ' . (time() - $tim) . ' seconds.');
    return true;
}
function wppa_recuperate($id)
{
    global $wpdb;
    $thumb = wppa_cache_thumb($id);
    $iptcfix = false;
    $exiffix = false;
    $file = wppa_get_source_path($id);
    if (!is_file($file)) {
        $file = wppa_get_photo_path($id);
    }
    if (is_file($file)) {
        // Not a dir
        $attr = getimagesize($file, $info);
        if (is_array($attr)) {
            // Is a picturefile
            if ($attr[2] == IMAGETYPE_JPEG) {
                // Is a jpg
                // Save iptc is on?
                if (wppa_switch('save_iptc')) {
                    // There is IPTC data
                    if (isset($info["APP13"])) {
                        // If this is a cron prcess, the table is not pre-emptied
                        if (wppa_is_cron()) {
                            // Replace or add data
                            wppa_import_iptc($id, $info);
                        } else {
                            wppa_import_iptc($id, $info, 'nodelete');
                        }
                        $iptcfix = true;
                    }
                }
                // Save exif is on?
                if (wppa_switch('save_exif')) {
                    $image_type = exif_imagetype($file);
                    // EXIF supported by server
                    if ($image_type == IMAGETYPE_JPEG) {
                        // Get exif data
                        $exif = exif_read_data($file, 'EXIF');
                        // Exif data found
                        if ($exif) {
                            // If this is a cron prcess, the table is not pre-emptied
                            if (wppa_is_cron()) {
                                // Replace or add data
                                wppa_import_exif($id, $file);
                            } else {
                                wppa_import_exif($id, $file, 'nodelete');
                            }
                            $exiffix = true;
                        }
                    }
                }
            }
        }
    }
    return array('iptcfix' => $iptcfix, 'exiffix' => $exiffix);
}