blogs ORDER BY blog_id" ); $orig_blog_id = $blog_id; } else $blog_ids = array( '1' ); foreach ( $blog_ids as $id ) { if ( count($blog_ids) > 1 ) { switch_to_blog( $id ); _e( "
site $id :
" ); } $uploads = scoper_get_upload_info(); $site_url = untrailingslashit( get_option('siteurl') ); if ( false === strpos( $uploads['baseurl'], $site_url ) ) { if ( $echo ) { _e('Note: Direct access to uploaded file attachments cannot be filtered because your WP_CONTENT_DIR is not in the WordPress branch.', 'scoper'); echo '

'; _e('The operation was terminated due to an invalid configuration.', 'scoper'); } return false; } $post_types = array_diff( get_post_types( array( 'public' => true ) ), array( 'attachment' ) ); $post_type_in = "'" . implode( "','", $post_types ) . "'"; if ( $post_ids = scoper_get_col( "SELECT ID FROM $wpdb->posts WHERE post_type IN ($post_type_in) ORDER BY post_type, post_title" ) ) { $stored_attachments = array(); if ( $results = scoper_get_results( "SELECT post_parent, guid FROM $wpdb->posts WHERE post_type = 'attachment'" ) ) { foreach ( $results as $row ) { if ( ! isset($stored_attachments[$row->post_parent]) ) $stored_attachments[$row->post_parent] = array(); $stored_attachments[$row->post_parent][$row->guid] = true; } } // for reasonable memory usage, only hold 10 posts in memory at a time $found_links = 0; $num_inserted = 0; $num_posts = count($post_ids); $bite_size = 10; $num_bites = $num_posts / $bite_size; if ( $num_posts % $bite_size ) $num_bites++; $upload_path = $uploads['baseurl']; $upload_dir = $uploads['basedir']; if ( $echo ) { printf(__( "checking %s posts / pages...", 'scoper' ), $num_posts); echo '

'; } for ( $i = 0; $i < $num_bites; $i++ ) { $id_in = "'" . implode( "','", array_slice( $post_ids, $i * $bite_size, $bite_size ) ) . "'"; if ( ! $results = scoper_get_results( "SELECT ID, post_content, post_author, post_title, post_type FROM $wpdb->posts WHERE ID IN ($id_in)" ) ) continue; foreach ( $results as $row ) { $linked_uploads = array(); // preg_match technique learned from http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php $tags = array( 'img' => array(), 'a' => array() ); $content = $row->post_content; preg_match_all('/]+>/i', $row->post_content, $tags['img']); preg_match_all('/]+>/i', $row->post_content, $tags['a']); // don't care that this will terminate with any enclosed tags (i.e. img) foreach ( array_keys($tags) as $tag_type ) { foreach ( $tags[$tag_type]['0'] as $found_tag ) { $found_attribs = array( 'src' => '', 'href' => '', 'title' => '', 'alt' => '' ); if ( ! preg_match_all('/(alt|title|src|href)=("[^"]*")/i', $found_tag, $tag_attributes) ) continue; foreach ( $tag_attributes[1] as $key => $attrib_name ) $found_attribs[$attrib_name] = trim($tag_attributes[2][$key], "'" . '"'); if ( ! $found_attribs['href'] && ! $found_attribs['src'] ) continue; $file_url = ( $found_attribs['src'] ) ? $found_attribs['src'] : $found_attribs['href']; if ( ! strpos( $file_url, '.' ) ) continue; if ( MULTISITE && strpos( $uploads['url'], 'blogs.dir' ) ) $file_url = str_replace( '/files/', "/wp-content/blogs.dir/$blog_id/files/", $file_url ); // links can't be registered as attachments unless they're in the WP uploads path if ( false === strpos($file_url, $upload_path) ) { if ( $echo ) { //printf( _ x( 'skipping unfilterable file in %1$s "%2$s": %3$s', 'post_type, post_title, file_url', 'scoper' ), __(ucwords($row->post_type)), $row->post_title, $file_url); printf( __( 'skipping unfilterable file in %1$s "%2$s": %3$s', 'scoper' ), __(ucwords($row->post_type)), $row->post_title, $file_url); echo '

'; } continue; } // make sure the linked file actually exists if ( ! file_exists( str_replace( $upload_path, $upload_dir, $file_url ) ) ) { if ( $echo ) { //printf( _ x( 'skipping unfilterable file in %1$s "%2$s": %3$s', 'post_type, post_title, file_url', 'scoper' ), __(ucwords($row->post_type)), $row->post_title, $file_url); printf( __( 'skipping missing file in %1$s "%2$s": %3$s', 'scoper' ), __(ucwords($row->post_type)), $row->post_title, $file_url); echo '

'; } continue; } $caption = ( $found_attribs['title'] ) ? $found_attribs['title'] : $found_attribs['alt']; // we might find the same file sourced in both link and img tags if ( ! isset($linked_uploads[$file_url]) || ! $linked_uploads[$file_url] ) { $found_links++; $linked_uploads[$file_url] = $caption; } } // end foreach found tag } // end foreach loop on 'img' and 'a' foreach ( $linked_uploads as $file_url => $caption ) { $unsuffixed_file_url = preg_replace( "/-[0-9]{2,4}x[0-9]{2,4}./", '.', $file_url ); $file_info = wp_check_filetype( $unsuffixed_file_url ); if ( ! isset($stored_attachments[$row->ID][$unsuffixed_file_url]) ) { $att = array(); $att['guid'] = $unsuffixed_file_url; $info = pathinfo($unsuffixed_file_url); if ( isset($info['filename']) ) { $att['post_name'] = $info['filename']; $att['post_title'] = $info['filename']; } $att['post_excerpt'] = $caption; $att['post_author'] = $row->post_author; $att['post_parent'] = $row->ID; $att['post_category'] = wp_get_post_categories( $row->ID ); if ( isset($file_info['type']) ) $att['post_mime_type'] = $file_info['type']; $num_inserted++; if ( $echo ) printf(__( 'new attachment in %1$s "%2$s": %3$s', 'scoper' ), __(ucwords($row->post_type)), $row->post_title, $file_url); //printf(_ x( 'new attachment in %1$s "%2$s": %3$s', 'post_type, post_title, file_url', 'scoper' ), __(ucwords($row->post_type)), $row->post_title, $file_url); wp_insert_attachment( $att ); } else { if ( $echo ) printf(__( 'attachment OK in %1$s "%2$s": %3$s', 'scoper' ), __(ucwords($row->post_type)), $row->post_title, $file_url); //printf(_ x( 'attachment OK in %1$s "%2$s": %3$s', 'post_type, post_title, file_url', 'scoper' ), __(ucwords($row->post_type)), $row->post_title, $file_url); } if ( $echo ) echo '

'; } // end foreach linked_uploads } // end foreach post in this bite } // endif for each 10-post bite if ( $echo ) { echo ''; printf( __( "Operation complete: %s linked uploads were found in your post / page content.", 'scoper' ), $found_links ); echo '

'; if ( $num_inserted ) { printf(__( '%s attachment records were added to the database.', 'scoper' ), $num_inserted); echo '

'; } elseif ( $found_links ) { _e('All linked uploads are already registered as attachments.', 'scoper'); echo '

'; } } } if ( count($blog_ids) > 1 ) echo '
'; } // end foreach site if ( count($blog_ids) > 1 ) { switch_to_blog( $orig_blog_id ); } return true; } ?>

", ''); printf( __('Back to %1$sRole Scoper Options%2$s', 'scoper'), "", ''); ?>
'; _e("Role Scoper can limit direct URL access to files linked from your posts and pages, but only if the following requirements are met:", 'scoper'); echo '
  1. '; _e( 'Your WP content directory must be a branch of the WordPress directory tree (i.e. wp-config.php must not be customized to separate WP_CONTENT_DIR and WP_CONTENT_URL from the main WordPress folder).', 'scoper'); echo '
  2. '; $search_replace_url = awp_plugin_search_url('replace'); if ( false !== strpos( $upload_path, 'http://www.' ) ) $www_msg = __('Note that to be detected as attachments, your file references must include www.'); else $www_msg = __('Note that to be detected as attachments, your file references must NOT include www.'); require_once( SCOPER_ABSPATH . '/uploads_rs.php' ); $uploads = scoper_get_upload_info(); printf(__('Files linked from WP Posts and Pages must be in %1$s (or a subdirectory of it) to be filtered. After moving files, you may use %2$s a search and replace plugin%3$s to conveniently update the URLs stored in your Post / Page content. %4$s', 'scoper'), '' . $uploads['baseurl'] . '', "", '', $www_msg); echo '
  3. '; _e( 'Files which are already appropriately located and linked must also have their post-file attachment relationship stored to the WP database. This is normally accomplished by clicking the "Insert into Post" button in the WP file uploader / Media Library. Files which were instead uploaded manually via FTP or CPanel can receive their attachment record via this utility.', 'scoper'); echo '
'; if ( ! empty($_POST['rs_run_utility']) ) { scoper_attach_linked_uploads( true ); } ?>