. */ function hi_mkdirr($pathname, $mode = 0777) { // Recursive, Hat tip: PHP.net // Check if directory already exists if ( is_dir($pathname) || empty($pathname) ) return true; // Ensure a file does not already exist with the same name if ( is_file($pathname) ) return false; // Crawl up the directory tree $next_pathname = substr( $pathname, 0, strrpos($pathname, DIRECTORY_SEPARATOR) ); if ( hi_mkdirr($next_pathname, $mode) ) { if (!file_exists($pathname)) return mkdir($pathname, $mode); } return false; } function hi_mm_ci_add_pages() { add_management_page('Hot Linked Image Cacher Plugin', 'Hotlinked Image Cacher', 8, __FILE__, 'hi_mm_ci_manage_page'); } function hi_mm_ci_manage_page() { global $wpdb; $debug = 0; $httppath = get_option('siteurl') . "/wp-content/plugins/hot-linked-image-cacher/upload"; $tophttp = get_option('siteurl'); if($debug==1){ echo $httppath." is the url for this site
"; } $absoupload = ABSPATH . "/wp-content/plugins/hot-linked-image-cacher/upload"; if($debug==1) { echo $absoupload . " is the absolute path
"; } ?>

Hotlinked Image Caching

Here's how this plugin works:

  1. Enter the post id for the post you need to perform image cache on.
  2. If you need to perform image cache on all post, then put all in the post id field.
  3. Then you'll be presented with a list of domains, check the domains you want to grab the image from.
  4. The images will be copied to your upload directory (this directory is under your hot-linked-image-cacher plugin directory and must be writable).
  5. The img links in your posts will be updated to their new local url location automatically.

Choose from one of the following methods to grab remote image, curl is more secure. If your server support both method, choose curl.
The default will be using curl, you must choose allow_url_fopen if your server do not support curl but enabled allow_url_fopen in php.ini.

curl (choose this method if your server support curl)
allow_url_fopen (choose this method if your server allow remote fopen AND does not support curl)

Post ID:

get_results("SELECT post_content FROM $wpdb->posts WHERE post_content LIKE ('%get_results("SELECT post_content FROM $wpdb->posts WHERE ID LIKE $postidnum"); if ( !$posts ) die('No posts with this Post ID were found.'); } if($debug==1){ echo $postidnum." was the post ID chosen
"; } foreach ($posts as $post) : preg_match_all('||i', $post->post_content, $matches); foreach ($matches[1] as $url) : if($debug==1){ echo $url; echo $httppath; } $op2 = stristr( $url, $tophttp ); if ( $op2 === false ){ $msg = 'NOT LOCAL'; if($debug==1){ echo $msg; } } else { continue; // Already local } $url = parse_url($url); $url['host'] = str_replace('www.', '', $url['host']); $domains[$url['host']]++; endforeach; endforeach; ?>

Check the domains that you want to grab images from:

    $num) : ?>

"; echo $postidnum." is the current post ID
"; } if ( !isset($_POST['domains']) ) die("You didn't check any domains, did you change your mind?"); if ( !is_writable($absoupload) ) die('Your upload folder is not writable, chmod 777 on the folder /wp-content/plugins/hot-linked-image-cacher/upload/'); foreach ( $_POST['domains'] as $domain ) : if($debug==1){ echo $postidnum." is the post ID chosen, now going to rewrite urls
"; } if ($postidnum == 'all' || $postidnum == 'All' || $postidnum == 'ALL') { $posts = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE post_content LIKE ('%get_results("SELECT post_content FROM $wpdb->posts WHERE ID LIKE $postidnum"); } ?>

    |i', $post->post_content, $matches); foreach ( $matches[1] as $url ) : $dummy5 = $url; $dummy2 = str_replace('http://', '', $url); $dummy3 = str_replace('//', '/', $dummy2); $dummy4 = 'http://'.$dummy3; $url = $dummy4; $op1 = stristr( $url, $tophttp); if ( $op1 === false ){ $msg = 'NOT LOCAL'; } else { continue; // Already local } $filename = str_replace('%20', 'spa', basename ( $url )); $b = parse_url( $url ); $dir = $absoupload . '/' . $domain . dirname ( $b['path'] ); hi_mkdirr( $dir ); $f = fopen( $dir . '/' . $filename , 'w' ); if($urlmethod=="curl" || is_null($urlmethod)){ $url = $b['scheme'] . '://' . $b['host'] . str_replace(' ', '%20', $b['path']) . $b['query']; $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $img = curl_exec($ch); curl_close($ch); } else{ $img = file_get_contents( $b['scheme'] . '://' . $b['host'] . str_replace(' ', '%20', $b['path']) . $b['query'] ); } if ( $img ) { fwrite( $f, $img ); fclose( $f ); $local = $httppath . '/' . $domain . dirname ( $b['path'] ) . "/$filename"; $wpdb->query("UPDATE $wpdb->posts SET post_content = REPLACE(post_content, '$dummy5', '$local');"); echo "
  • Cached $url
  • "; flush(); } endforeach; endforeach; ?>

All done!