예제 #1
0
function wppa_get_source_pl($id)
{
    // Init
    $result = '';
    // If feature is enabled
    if (wppa_opt('pl_dirname')) {
        $source_path = wppa_fix_poster_ext(wppa_get_source_path($id), $id);
        if (is_file($source_path)) {
            $result = content_url() . '/' . wppa_opt('pl_dirname') . '/' . wppa_get_album_name_for_pl(wppa_get_photo_item($id, 'album')) . '/' . basename($source_path);
            // My-Photo.jpg
        }
    }
    return $result;
}
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;
}