Advertisement
Guest User

Add Linked Images To Gallery error line 57

a guest
Jan 19th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.73 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Add Linked Images To Gallery
  4. Plugin URI: http://www.bbqiguana.com/wordpress-plugins/add-linked-images-to-gallery/
  5. Version: 1.4
  6. Description: Examines the text of a post and makes local copies of all the images linked though IMG tags, adding them as gallery attachments on the post itself.
  7. Author: Randy Hunt
  8. Author URI: http://www.bbqiguana.com/
  9. */
  10.  
  11. require_once(ABSPATH . 'wp-admin/includes/file.php');
  12. require_once(ABSPATH . 'wp-admin/includes/media.php');
  13. require_once(ABSPATH . 'wp-admin/includes/image.php');
  14.  
  15. $externimg_count = 0;
  16.  
  17. function externimg_find_imgs ($post_id) {
  18.  
  19. function externimg_find_imgs ($post_id) { //die('called on delete');
  20. $post = get_post($post_id);
  21.  
  22. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
  23.  
  24. if( $post->post_status == 'trash' ) return;
  25.  
  26. if (wp_is_post_revision($post_id)) return;
  27.  
  28.  
  29. }
  30. if (!$catfound) return;
  31. }
  32. $post = get_post($post_id);
  33. //$post = get_post($post_id);
  34. $a = get_option('externimg_authlist');
  35. if($a && !in_array($post->post_author, explode(',', $a))) return;
  36.  
  37.  
  38. $pathname = $parseurl['path'];
  39. $filename = substr(strrchr($pathname, '/'), 1);
  40. if (preg_match ('/(\.php|\.aspx?)$/', $filename) ) $filename .= '.jpg';
  41.  
  42. $imgid = externimg_sideload($imgs[$i], $filename, $post_id);
  43.  
  44. $imgpath = wp_get_attachment_url($imgid);
  45.  
  46. if (!is_wp_error($imgpath)) {
  47. if ($l=='custtag') {
  48. add_post_meta($post_id, $k, $imgs[$i], false);
  49.  
  50. $content = preg_replace('/(<img[^>]* src=[\'"]?)('.$trans.')/', '$1'.$imgpath, $content);
  51. $replaced = true;
  52. }
  53. $processed[] = $imgs[i];
  54. $processed[] = $imgs[$i];
  55. $externimg_count++;
  56. }
  57. }
  58. }
  59. if ($replaced) {
  60. $upd = array();
  61.  
  62. }
  63.  
  64. function externimg_sideload ($file, $url, $post_id) {
  65.  
  66. if(!empty($file)){
  67.  
  68. $file_array['tmp_name'] = download_url($file);
  69. if(is_wp_error($file_array['tmp_name'])) return;
  70. $file_array['name'] = basename($file);
  71. //$desc = @$desc;
  72. $desc = externimg_getext($file);
  73.  
  74. //$desc = externimg_getext($file_array['tmp_name']);
  75.  
  76. $pathparts = pathinfo($file_array['tmp_name']);
  77.  
  78.  
  79. if (''==$pathparts['extension']) {
  80. $ext = externimg_getext($file_array['tmp_name']);
  81. rename($file_array['tmp_name'], $file_array['tmp_name'] . $ext);
  82.  
  83. $file_array['tmp_name'] .= $ext;
  84. }
  85.  
  86. $id = media_handle_sideload($file_array, $post_id, $desc);
  87. $id = media_handle_sideload($file_array, $post_id, $file_array['name']);
  88.  
  89. $src = $id;
  90.  
  91. if(is_wp_error($id)) {
  92.  
  93. return $id;
  94. }
  95. }
  96.  
  97. if (!empty($src)) return $src;
  98. else return false;
  99. }
  100.  
  101. function externimg_get_img_tags ($post_id) {
  102. $post = get_post($post_id);
  103. $w = get_option('externimg_whichimgs');
  104. $s = get_option('siteurl');
  105.  
  106. $result = array();
  107. //preg_match_all('/<img[^>]+src=\\\\?[\'"]?([^>\\\"\' ]+)/', $content, $matches);
  108. preg_match_all('/<img[^>]* src=[\'"]?([^>\'" ]+)/', $post->post_content, $matches);
  109. for ($i=0; $i<count($matches[0]); $i++) {
  110. $uri = $matches[1][$i];
  111.  
  112. //only check FQDNs
  113. if (preg_match('/^http:\/\//', $uri)) {
  114. //make sure it's not external
  115. if ($s != substr($uri, 0, strlen($s)) ) {
  116. //only match Flickr images?
  117. if($w == 'All' ||
  118. ($w == 'Flickr' && preg_match('/^http:\/\/[a-z0-9]+\.static\.flickr\.com\//', $uri)) ) {
  119. $result[] = $matches[1][$i];
  120. }
  121. }
  122. }
  123. }
  124. return $result;
  125. }
  126.  
  127. function externimg_savefile ($file, $url, $post_id) {
  128. $time = null;
  129.  
  130. $uploads = wp_upload_dir($time);
  131. $filename = wp_unique_filename( $uploads['path'], $url, $unique_filename_callback );
  132. $savepath = $uploads['path'] . "/$filename";
  133.  
  134. if($fp = fopen($savepath, 'w')) {
  135. fwrite($fp, $file);
  136. fclose($fp);
  137. }
  138.  
  139. $wp_filetype = wp_check_filetype( $savepath, $mimes );
  140. $type = $wp_filetype['type'];
  141. $title = $filename;
  142. $content = '';
  143.  
  144. // Construct the attachment array
  145. $attachment = array(
  146. 'post_mime_type' => $type,
  147. 'guid' => $uploads['url'] . "/$filename",
  148. 'post_parent' => $post_id,
  149. 'post_title' => $title,
  150. 'post_content' => $content
  151. );
  152.  
  153. // Save the data
  154. $id = wp_insert_attachment($attachment, $savepath, $post_id);
  155. if ( !is_wp_error($id) ) {
  156. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  157. } else return '';
  158. return $uploads['url'] . "/$filename";
  159. }
  160.  
  161.  
  162. //modified from code found at http://www.bin-co.com/php/scripts/load/
  163. function externimg_loadimage ($url) {
  164.  
  165. $url_parts = parse_url($url);
  166. $ch = false;
  167. $info = array(//Currently only supported by curl.
  168. 'http_code' => 200
  169. );
  170. $response = '';
  171.  
  172. $send_header = array(
  173. 'Accept' => 'text/*',
  174. 'User-Agent' => 'Attach-Linked-Images WordPress Plugin (http://www.bbqiguana.com/)'
  175. );
  176.  
  177.  
  178.  
  179. ///////////////////////////// Curl /////////////////////////////////////
  180. //If curl is available, use curl to get the data.
  181. if(function_exists("curl_init")) { //$options['use'] == 'fsocketopen'))) { //Don't use curl if it is specifically stated to use fsocketopen in the options
  182.  
  183. $page = $url;
  184.  
  185. $ch = curl_init($url_parts['host']);
  186.  
  187. curl_setopt($ch, CURLOPT_URL, $page) or die("Invalid cURL Handle Resouce");
  188. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Just return the data - not print the whole thing.
  189. curl_setopt($ch, CURLOPT_HEADER, true); //We need the headers
  190. curl_setopt($ch, CURLOPT_NOBODY, false); //Yes, get the body.
  191.  
  192. //Set the headers our spiders sends
  193. curl_setopt($ch, CURLOPT_USERAGENT, $send_header['User-Agent']); //The Name of the UserAgent we will be using ;)
  194. $custom_headers = array("Accept: " . $send_header['Accept'] );
  195. curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);
  196.  
  197. @curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/binget-cookie.txt"); //If ever needed...
  198. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  199. @curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
  200. @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  201.  
  202. if(isset($url_parts['user']) and isset($url_parts['pass'])) {
  203. $custom_headers = array("Authorization: Basic ".base64_encode($url_parts['user'].':'.$url_parts['pass']));
  204. curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);
  205. }
  206.  
  207. $response = curl_exec($ch);
  208. $info = curl_getinfo($ch); //Some information on the fetch
  209. if('http://l.yimg.com/g/images/photo_unavailable.gif'==$info['url']) $body = '';
  210. curl_close($ch); //If the session option is not set, close the session.
  211.  
  212. //////////////////////////////////////////// FSockOpen //////////////////////////////
  213. } else { //If there is no curl, use fsocketopen - but keep in mind that most advanced features will be lost with this approch.
  214. if(isset($url_parts['query'])) {
  215. $page = $url_parts['path'] . '?' . $url_parts['query'];
  216. } else {
  217. $page = $url_parts['path'];
  218. }
  219.  
  220. if(!isset($url_parts['port'])) $url_parts['port'] = 80;
  221. $fp = fsockopen($url_parts['host'], $url_parts['port'], $errno, $errstr, 30);
  222. if ($fp) {
  223. $out = "GET $page HTTP/1.0\r\n"; //HTTP/1.0 is much easier to handle than HTTP/1.1
  224. $out .= "Host: $url_parts[host]\r\n";
  225. $out .= "Accept: $send_header[Accept]\r\n";
  226. $out .= "User-Agent: {$send_header['User-Agent']}\r\n";
  227. $out .= "Connection: Close\r\n";
  228.  
  229. //HTTP Basic Authorization support
  230. if(isset($url_parts['user']) and isset($url_parts['pass'])) {
  231. $out .= "Authorization: Basic ".base64_encode($url_parts['user'].':'.$url_parts['pass']) . "\r\n";
  232. }
  233. $out .= "\r\n";
  234.  
  235. fwrite($fp, $out);
  236. while (!feof($fp)) {
  237. $response .= fgets($fp, 128);
  238. }
  239. fclose($fp);
  240. }
  241. }
  242.  
  243. //Get the headers in an associative array
  244. $headers = array();
  245.  
  246. if($info['http_code'] == 404 || $info['http_code'] == 400) {
  247. $body = '';
  248. $headers['Status'] = 404;
  249. } else {
  250. //Seperate header and content
  251. $header_text = substr($response, 0, $info['header_size']);
  252. $body = substr($response, $info['header_size']);
  253.  
  254. foreach(explode("\n",$header_text) as $line) {
  255. $parts = explode(": ",$line);
  256. if(count($parts) == 2) $headers[$parts[0]] = chop($parts[1]);
  257. }
  258. }
  259.  
  260. //if($options['return_info']) return array('body' => $body, 'info' => $info, 'curl_handle'=>$ch);
  261. return $body;
  262. }
  263.  
  264. function externimg_backcatalog () {
  265. global $externimg_count;
  266. $count = 0;
  267. $pp = get_posts( array( 'numberposts'=>-1 ) );
  268. foreach ($pp as $p) {
  269. try {
  270. //echo '<p>' . $p->ID . ': ' . $p->title . '</p>';
  271. echo '<p>[' . $p->ID . '] ' . $p->post_title . ': ';
  272. externimg_find_imgs($p->ID);
  273. echo '<em>' . $externimg_count . ' images processed.</em></p>';
  274. } catch (Exception $e) {
  275. echo '<em>an error occurred</em>.</p>';
  276. }
  277. $count += $externimg_count;
  278. }
  279. }
  280.  
  281. function externimg_getauthors() {
  282. global $wpdb;
  283. $query = "SELECT $wpdb->users.* FROM $wpdb->users ORDER BY display_name;";
  284. $authors = $wpdb->get_results($query);
  285. return $authors;
  286. }
  287.  
  288. function externimg_menu () {
  289. if ( function_exists('add_options_page') ) {
  290. add_options_page('Linked IMGs to Gallery', 'Linked IMGs', 8, 'externimg', 'externimg_options');
  291. }
  292. }
  293.  
  294. function externimg_init () {
  295. //$plugin_dir = basename(dirname(__FILE__));
  296. //load_plugin_textdomain('externimg', 'wp-content/plugins/'.$plugin_dir, 'externimg');
  297.  
  298. register_setting('externimg', 'externimg_whichimgs');
  299. register_setting('externimg', 'externimg_replacesrc');
  300. register_setting('externimg', 'externimg_custtagname');
  301. register_setting('externimg', 'externimg_cats');
  302. register_setting('externimg', 'externimg_auths');
  303. register_setting('externimg', 'externimg_catlist');
  304. register_setting('externimg', 'externimg_authlist');
  305. }
  306.  
  307. function externimg_install () {
  308. //add default options
  309. $whichimgs = get_option('externimg_whichimgs');
  310. $replacesrc = get_option('externimg_replacesrc');
  311. $custtagname = get_option('externimg_custtagname');
  312. $catlist = get_option('externimg_catlist');
  313. $authlist = get_option('externimg_authlist');
  314.  
  315. if(!$whichimgs) update_option('externimg_whichimgs', 'All');
  316. if(!$replacesrc) update_option('externimg_replacesrc', 'replace');
  317. if(!$custtagname) update_option('externimg_custtagname', 'externimg');
  318. if(!$catlist) update_option('externimg_catlist', '');
  319. if(!$authlist) update_option('externimg_authlist', '');
  320. }
  321.  
  322. function externimg_options () {
  323. $_cats = '';
  324. $_auths = '';
  325. echo '<div class="wrap">';
  326. echo '<h2>Linked IMGs to Gallery Attachments</h2>';
  327.  
  328. if ($_POST['action']=='backcatalog') {
  329. externimg_backcatalog();
  330. echo '<div id="message" class="updated fade" style="background-color:rgb(255,251,204);"><p>Finished processing past posts!</p></div>';
  331. }
  332.  
  333. if ($_POST['action']=='update') {
  334. //check_admin_referer('externimg_update-action');
  335. // $_cats = implode(',', $_POST['externimg_cats']);
  336. // $_auths = implode(',', $_POST['externimg_auths']);
  337. update_option('externimg_whichimgs', $_POST['externimg_whichimgs'] );
  338. update_option('externimg_replacesrc', $_POST['externimg_replacesrc'] );
  339. update_option('externimg_custtagname', $_POST['externimg_custtagname'] );
  340. if($_POST['externimg_catlist']) update_option('externimg_catlist', $_cats );
  341. update_option('externimg_catlist', ($_POST['externimg_catlist'] ) ? implode(',', $_POST['externimg_cats'] ) : '');
  342. update_option('externimg_authlist', ($_POST['externimg_authlist']) ? implode(',', $_POST['externimg_auths']) : '');
  343. // if($_POST['externimg_authlist']) update_option('externimg_authlist', $_auths);
  344. // update_option('externimg_catlist', $_POST['externimg_catlist'] );
  345. // update_option('externimg_authlist', $_POST['externimg_authlist'] );
  346. echo '<div id="message" class="updated fade" style="background-color:rgb(255,251,204);"><p>Settings updated.</p></div>';
  347. }
  348. echo '<big>Options</big>';
  349. echo '<form name="externimg-options" method="post" action="">';
  350. settings_fields('externimg');
  351. echo '<table class="form-table"><tbody>';
  352. echo '<tr valign="top"><th scope="row"><strong>Which external IMG links to process:</strong></th>';
  353. echo '<td><label for="myradio1"><input id="myradio1" type="radio" name="externimg_whichimgs" value="All" ' . (get_option('externimg_whichimgs')!='Flickr'?'checked="checked"':'') . '/> All images</label><br/>';
  354. echo '<label for="myradio2"><input id="myradio2" type="radio" name="externimg_whichimgs" value="Flickr" ' . (get_option('externimg_whichimgs')=='Flickr'?'checked="checked"':'') . ' /> Only Flickr images</label><br/>';
  355. echo '<p>By default, all external images are processed. This can be set to only apply to Flickr.</p>';
  356. echo '</td></tr>';
  357. echo '<tr valign="top"><th scope="row"><strong>What to do with the images:</th>';
  358. echo '<td><label for="myradio3"><input id="myradio3" type="radio" name="externimg_replacesrc" value="replace" ' . (get_option('externimg_replacesrc')!='custtag'?'checked="checked"':'') . ' /> Replace SRC attribute with local copy</label><br/>';
  359. echo '<label for="myradio4"><input id="myradio4" type="radio" name="externimg_replacesrc" value="custtag" ' . (get_option('externimg_replacesrc')=='custtag'?'checked="checked"':'') . ' /> Use custom tag:</label> ';
  360. echo '<input type="text" size="20" name="externimg_custtagname" value="' . get_option('externimg_custtagname') . '" /><br/>';
  361. echo '<p>Replacing the SRC attribute will convert the external IMG link to a local link, pointed at the local copy downloaded by this plugin. If the SRC attribute is not replaced, the plugin needs to mark the IMG as having been processed somehow, so this is done by tracking processed images in custom_tag values. You can change the name of the custom tag.</p></td></tr>';
  362. // echo '<tr valign="top"><th scope="row">This site name:</th><td>' . get_option('siteurl') . '</td></tr>';
  363.  
  364. echo '<tr align="top"><th scope="row"><strong>Apply to these categories:</strong></th>';
  365. echo '<td><label for="myradio5"><input type="radio" id="myradio5" name="externimg_catlist" value="" ' . (get_option('externimg_catlist')==''?'checked="checked"':'') . ' /> All categories</label><br/>';
  366. echo '<label for="myradio6"><input type="radio" id="myradio6" name="externimg_catlist" value="Y" ' . (get_option('externimg_catlist')!=''?'checked="checked"':'') . ' /> Selected categories</label><br/>';
  367.  
  368. $_cats = explode(',', get_option('externimg_catlist'));
  369. $chcount = 0;
  370. $cats = get_categories();
  371. foreach ($cats as $cat) {
  372. $chcount++;
  373. echo '<label for="mycheck'.$chcount.'"><input type="checkbox" id="mycheck'.$chcount.'" name="externimg_cats[]" value="' . $cat->cat_ID . '" '.(in_array($cat->cat_ID, $_cats)?'checked="checked"':'').' /> ' . $cat->cat_name . '</label><br/>';
  374. }
  375. echo '</td></tr>';
  376. echo '<tr align="top"><th scope="row"><strong>Apply to these authors:</strong></th>';
  377. echo '<td><label for="myradio7"><input type="radio" id="myradio7" name="externimg_authlist" value="" ' . (get_option('externimg_authlist')==''?'checked="checked"':'') . ' /> All authors</label><br/>';
  378. echo '<label for="myradio8"><input type="radio" id="myradio8" name="externimg_authlist" value="Y" ' . (get_option('externimg_authlist')!=''?'checked="checked"':'') . ' /> Selected authors</label><br/>';
  379.  
  380. $_auths = explode(',', get_option('externimg_authlist'));
  381. $auths = externimg_getauthors();
  382. foreach ($auths as $auth) {
  383. $chcount++;
  384. echo '<label for="mycheck'.$chcount.'"><input type="checkbox" id="mycheck'.$chcount.'" name="externimg_auths[]" value="' . $auth->ID . '" '.(in_array($auth->ID, $_auths)?'checked="checked"':'').'/> ' . $auth->display_name . '</label><br/>';
  385. }
  386. echo '</td></tr>';
  387.  
  388. echo '</tbody></table>';
  389. echo '<div class="submit">';
  390. //echo '<input type="hidden" name="externimg_update" value="action" />';
  391. echo '<input type="submit" name="submit" class="button-primary" value="' . __('Save Changes') . '" />';
  392. echo '</div>';
  393. echo '</form>';
  394.  
  395. echo '<form name="externimg-backcatalog" method="post" action="">';
  396. echo '<div class="wrap">';
  397. echo '<big>Process all posts</big>';
  398. echo '<p>Use this function to apply the plugin to all previous posts. The settings specified above will still be respected.</p>';
  399. echo '<p><em>Please note that this can take a long time for sites with a lot of posts.</em></p>';
  400. echo '<div class="submit">';
  401. echo '<input type="hidden" name="action" value="backcatalog">';
  402. echo '<input type="submit" class="button-primary" value="' . __('Process') . '" />';
  403. echo '</div>';
  404. echo '<p>&nbsp;</p>';
  405. echo '</div>';
  406. echo '</form>';
  407.  
  408. echo '<div class="wrap">';
  409. echo '<big>Donate</big>';
  410. echo '<p>If you like this plugin consider donating a small amount to the author using PayPal to support further plugin development.</p>';
  411. echo '<div align="center"><form name="_xclick" action="https://www.paypal.com/us/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_xclick"><input type="hidden" name="business" value="bbqiguana@gmail.com"><input type="hidden" name="item_name" value="Donations for WP-Externimage Plugin"><input type="hidden" name="currency_code" value="USD"><input type="image" src="http://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it\'s fast, free and secure!"></form></div>';
  412. echo '<p>If you think donating money is somehow impersonal you could also choose items from my <a href="http://www.amazon.com/registry/wishlist/18LMHOMRM49P8/ref=cm_wl_act_vv">Amazon.com wishlist</a>.</p>';
  413. echo '</div>';
  414. echo '';
  415. echo '</div>';
  416. }
  417.  
  418. if ( is_admin() ) { // admin actions
  419. add_action('admin_menu', 'externimg_menu');
  420. add_action('admin_init', 'externimg_init');
  421. }
  422. register_activation_hook(__FILE__, 'externimg_install');
  423.  
  424. add_action('save_post', 'externimg_find_imgs');
  425.  
  426. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement