/** * do the stuff */ public static function init() { self::$opts = get_option(self::opt, self::$opts); add_action('admin_menu', array(__CLASS__, 'add_setting_menu')); // add editor button add_action('media_buttons', array(__CLASS__, 'add_media_button'), 20); register_activation_hook(HACKLOG_RIA_LOADER, array(__CLASS__, 'my_activation')); register_deactivation_hook(HACKLOG_RIA_LOADER, array(__CLASS__, 'my_deactivation')); }
/** * NOTE: wp curl class default timeoute is 5s,must set it long to avoid the * "Operation timed out after 5008 milliseconds with 122371 out of 315645 bytes received" * error. * On windows server,if we want to fetch data from ssl remote server, * extra things shoudl be do with php5 curl module,set ssl verify to FALSE can simply solve the problem. * but this is not the ideal solution! * @param unknown_type $post_id * @param unknown_type $url * @return boolean|multitype:string unknown |multitype:string Ambigous <string, number> */ public static function down_remote_file($post_id, $url) { global $wp_version; //set up required options $http_options = array('timeout' => 120, 'redirection' => 20, 'user-agent' => 'WordPress/' . $wp_version . '; ' . hacklog_remote_image_autosave::VERSION, 'sslverify' => FALSE); $home_url = home_url('/'); set_time_limit(200); // if is remote image $remote_image_url = $url; $headers = wp_remote_head($remote_image_url, $http_options); // var_dump($headers); $response_code = wp_remote_retrieve_response_code($headers); // var_dump($response_code);exit; // 302 防盗链的,不下载 if (200 != $response_code) { if (is_wp_error($headers)) { echo self::raise_error($headers->get_error_message()); } else { echo self::raise_error('fetch error!'); } return FALSE; } $mime = $headers['headers']['content-type']; $file_ext = self::mime_to_ext($mime); $allowed_filetype = array('jpg', 'gif', 'png', 'bmp'); // var_dump($file_ext);exit; if (in_array($file_ext, $allowed_filetype)) { $http = wp_remote_get($remote_image_url, $http_options); // ignore WP_Error if (is_wp_error($http)) { echo self::raise_error($http->get_error_message()); return FALSE; } if (200 == $http['response']['code']) { $file_content = $http['body']; } else { // time out or 302 redirect (remote site anti-leech) echo self::raise_error('Can not fetch remote image file!'); return FALSE; } if (!self::check_image_size($file_content)) { return self::return_origin($remote_image_url); } $filename = sanitize_file_name(basename($remote_image_url)); $type = $mime; // download remote file and save it into database; $result = self::handle_upload($filename, $file_content, $type, $post_id); if (is_wp_error($result)) { echo self::raise_error($result->get_error_message()); return FALSE; } // var_dump($result);exit; if (!is_wp_error($result['id'])) { // wp_get_attachment_image($attachment_id, $size = 'thumbnail', // $icon = false, $attr = '') // array('thumbnail', 'medium', 'large'); // Standard sizes $size = hacklog_remote_image_autosave::get_conf('thumbnail_size', 'medium'); $img = wp_get_attachment_image($result['id'], $size, $icon = false, $attr = ''); $full_image = wp_get_attachment_image_src($result['id'], 'full'); $html = '<a href="' . $full_image[0] . '">' . $img . '</a>'; return array('src' => $result['url'], 'html' => $html); } else { $result_id = $result['id']; echo self::raise_error($result_id->get_error_message()); return FALSE; } } }
* @package Hacklog Remote Image Autosave * @encoding UTF-8 * @author 荒野无灯 <HuangYeWuDeng> * @link http://ihacklog.com * @copyright Copyright (C) 2012 荒野无灯 * @license http://www.gnu.org/licenses/ */ /* Copyright 2012 荒野无灯 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ defined('ABSPATH') || die('No direct access!'); if (is_admin()) { define('HACKLOG_RIA_LOADER', __FILE__); require plugin_dir_path(__FILE__) . '/hacklog-remote-image-autosave.php'; //ok,let's go,have fun-_- hacklog_remote_image_autosave::init(); }