cat_ID, explode(',', $c)))
$catfound = true;
}
if (!$catfound) return;
}
$post = get_post($post_id);
$a = get_option('externimg_authlist');
if($a && !in_array($post->post_author, explode(',', $a))) return;
$l = get_option('externimg_replacesrc');
$k = get_option('externimg_custtagname');
$processed = get_post_custom_values($k, $post_id);
$replaced = false;
$content = $post->post_content;
$imgs = externimg_get_img_tags($post_id);
global $externimg_count;
for($i=0; $i]* src=[\'"]?)('.$trans.')/', '$1'.$imgpath, $content);
$replaced = true;
}
$processed[] = $imgs[i];
$externimg_count++;
}
}
}
if ($replaced) {
$upd = array();
$upd['ID'] = $post_id;
$upd['post_content'] = $content;
wp_update_post($upd);
}
}
function externimg_getext ($file) {
if (function_exists('mime_content_type'))
$mime = mime_content_type($file);
else return '';
switch($mime) {
case 'image/jpg':
case 'image/jpeg':
return '.jpg';
break;
case 'image/gif':
return '.gif';
break;
case 'image/png':
return '.png';
break;
}
return '';
}
function externimg_sideload ($file, $url, $post_id) {
if(!empty($file)){
$file_array['tmp_name'] = download_url($file);
if(is_wp_error($file_array['tmp_name'])) return;
$file_array['name'] = basename($file);
//$desc = @$desc;
$desc = externimg_getext($file);
$pathparts = pathinfo($file_array['tmp_name']);
if (''==$pathparts['extension']) {
$ext = externimg_getext($file_array['tmp_name']);
rename($file_array['tmp_name'], $file_array['tmp_name'] . $ext);
$file_array['name'] = basename($file_array['tmp_name']) . $ext;
$file_array['tmp_name'] .= $ext;
}
$id = media_handle_sideload($file_array, $post_id, $desc);
$src = $id;
if(is_wp_error($id)) {
@unlink($file_array['tmp_name']);
return $id;
}
}
if (!empty($src)) return $src;
else return false;
}
function externimg_get_img_tags ($post_id) {
$post = get_post($post_id);
$w = get_option('externimg_whichimgs');
$s = get_option('siteurl');
$result = array();
//preg_match_all('/
]+src=\\\\?[\'"]?([^>\\\"\' ]+)/', $content, $matches);
preg_match_all('/
]* src=[\'"]?([^>\'" ]+)/', $post->post_content, $matches);
for ($i=0; $i $type,
'guid' => $uploads['url'] . "/$filename",
'post_parent' => $post_id,
'post_title' => $title,
'post_content' => $content
);
// Save the data
$id = wp_insert_attachment($attachment, $savepath, $post_id);
if ( !is_wp_error($id) ) {
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
} else return '';
return $uploads['url'] . "/$filename";
}
//modified from code found at http://www.bin-co.com/php/scripts/load/
function externimg_loadimage ($url) {
$url_parts = parse_url($url);
$ch = false;
$info = array(//Currently only supported by curl.
'http_code' => 200
);
$response = '';
$send_header = array(
'Accept' => 'text/*',
'User-Agent' => 'Attach-Linked-Images WordPress Plugin (http://www.bbqiguana.com/)'
);
///////////////////////////// Curl /////////////////////////////////////
//If curl is available, use curl to get the data.
if(function_exists("curl_init")) { //$options['use'] == 'fsocketopen'))) { //Don't use curl if it is specifically stated to use fsocketopen in the options
$page = $url;
$ch = curl_init($url_parts['host']);
curl_setopt($ch, CURLOPT_URL, $page) or die("Invalid cURL Handle Resouce");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Just return the data - not print the whole thing.
curl_setopt($ch, CURLOPT_HEADER, true); //We need the headers
curl_setopt($ch, CURLOPT_NOBODY, false); //Yes, get the body.
//Set the headers our spiders sends
curl_setopt($ch, CURLOPT_USERAGENT, $send_header['User-Agent']); //The Name of the UserAgent we will be using ;)
$custom_headers = array("Accept: " . $send_header['Accept'] );
curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);
@curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/binget-cookie.txt"); //If ever needed...
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
@curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
@curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if(isset($url_parts['user']) and isset($url_parts['pass'])) {
$custom_headers = array("Authorization: Basic ".base64_encode($url_parts['user'].':'.$url_parts['pass']));
curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);
}
$response = curl_exec($ch);
$info = curl_getinfo($ch); //Some information on the fetch
if('http://l.yimg.com/g/images/photo_unavailable.gif'==$info['url']) $body = '';
curl_close($ch); //If the session option is not set, close the session.
//////////////////////////////////////////// FSockOpen //////////////////////////////
} else { //If there is no curl, use fsocketopen - but keep in mind that most advanced features will be lost with this approch.
if(isset($url_parts['query'])) {
$page = $url_parts['path'] . '?' . $url_parts['query'];
} else {
$page = $url_parts['path'];
}
if(!isset($url_parts['port'])) $url_parts['port'] = 80;
$fp = fsockopen($url_parts['host'], $url_parts['port'], $errno, $errstr, 30);
if ($fp) {
$out = "GET $page HTTP/1.0\r\n"; //HTTP/1.0 is much easier to handle than HTTP/1.1
$out .= "Host: $url_parts[host]\r\n";
$out .= "Accept: $send_header[Accept]\r\n";
$out .= "User-Agent: {$send_header['User-Agent']}\r\n";
$out .= "Connection: Close\r\n";
//HTTP Basic Authorization support
if(isset($url_parts['user']) and isset($url_parts['pass'])) {
$out .= "Authorization: Basic ".base64_encode($url_parts['user'].':'.$url_parts['pass']) . "\r\n";
}
$out .= "\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$response .= fgets($fp, 128);
}
fclose($fp);
}
}
//Get the headers in an associative array
$headers = array();
if($info['http_code'] == 404 || $info['http_code'] == 400) {
$body = '';
$headers['Status'] = 404;
} else {
//Seperate header and content
$header_text = substr($response, 0, $info['header_size']);
$body = substr($response, $info['header_size']);
foreach(explode("\n",$header_text) as $line) {
$parts = explode(": ",$line);
if(count($parts) == 2) $headers[$parts[0]] = chop($parts[1]);
}
}
//if($options['return_info']) return array('body' => $body, 'info' => $info, 'curl_handle'=>$ch);
return $body;
}
function externimg_backcatalog () {
global $externimg_count;
$count = 0;
$pp = get_posts( array( 'numberposts'=>-1 ) );
foreach ($pp as $p) {
try {
//echo '' . $p->ID . ': ' . $p->title . '
';
echo '[' . $p->ID . '] ' . $p->post_title . ': ';
externimg_find_imgs($p->ID);
echo '' . $externimg_count . ' images processed.
';
} catch (Exception $e) {
echo 'an error occurred.
';
}
$count += $externimg_count;
}
}
function externimg_getauthors() {
global $wpdb;
$query = "SELECT $wpdb->users.* FROM $wpdb->users ORDER BY display_name;";
$authors = $wpdb->get_results($query);
return $authors;
}
function externimg_menu () {
if ( function_exists('add_options_page') ) {
add_options_page('Linked IMGs to Gallery', 'Linked IMGs', 8, 'externimg', 'externimg_options');
}
}
function externimg_init () {
//$plugin_dir = basename(dirname(__FILE__));
//load_plugin_textdomain('externimg', 'wp-content/plugins/'.$plugin_dir, 'externimg');
register_setting('externimg', 'externimg_whichimgs');
register_setting('externimg', 'externimg_replacesrc');
register_setting('externimg', 'externimg_custtagname');
register_setting('externimg', 'externimg_cats');
register_setting('externimg', 'externimg_auths');
register_setting('externimg', 'externimg_catlist');
register_setting('externimg', 'externimg_authlist');
}
function externimg_install () {
//add default options
$whichimgs = get_option('externimg_whichimgs');
$replacesrc = get_option('externimg_replacesrc');
$custtagname = get_option('externimg_custtagname');
$catlist = get_option('externimg_catlist');
$authlist = get_option('externimg_authlist');
if(!$whichimgs) update_option('externimg_whichimgs', 'All');
if(!$replacesrc) update_option('externimg_replacesrc', 'replace');
if(!$custtagname) update_option('externimg_custtagname', 'externimg');
if(!$catlist) update_option('externimg_catlist', '');
if(!$authlist) update_option('externimg_authlist', '');
}
function externimg_options () {
$_cats = '';
$_auths = '';
echo '';
echo '
Linked IMGs to Gallery Attachments
';
if ($_POST['action']=='backcatalog') {
externimg_backcatalog();
echo '
Finished processing past posts!
';
}
if ($_POST['action']=='update') {
//check_admin_referer('externimg_update-action');
// $_cats = implode(',', $_POST['externimg_cats']);
// $_auths = implode(',', $_POST['externimg_auths']);
update_option('externimg_whichimgs', $_POST['externimg_whichimgs'] );
update_option('externimg_replacesrc', $_POST['externimg_replacesrc'] );
update_option('externimg_custtagname', $_POST['externimg_custtagname'] );
if($_POST['externimg_catlist']) update_option('externimg_catlist', $_cats );
update_option('externimg_catlist', ($_POST['externimg_catlist'] ) ? implode(',', $_POST['externimg_cats'] ) : '');
update_option('externimg_authlist', ($_POST['externimg_authlist']) ? implode(',', $_POST['externimg_auths']) : '');
// if($_POST['externimg_authlist']) update_option('externimg_authlist', $_auths);
// update_option('externimg_catlist', $_POST['externimg_catlist'] );
// update_option('externimg_authlist', $_POST['externimg_authlist'] );
echo '
';
}
echo '
Options';
echo '
';
echo '
';
echo '
';
echo '
Donate';
echo '
If you like this plugin consider donating a small amount to the author using PayPal to support further plugin development.
';
echo '
';
echo '
If you think donating money is somehow impersonal you could also choose items from my Amazon.com wishlist.
';
echo '
';
echo '';
echo '
';
}
if ( is_admin() ) { // admin actions
add_action('admin_menu', 'externimg_menu');
add_action('admin_init', 'externimg_init');
}
register_activation_hook(__FILE__, 'externimg_install');
add_action('save_post', 'externimg_find_imgs');
?>