本文整理汇总了PHP中get_file_curl函数的典型用法代码示例。如果您正苦于以下问题:PHP get_file_curl函数的具体用法?PHP get_file_curl怎么用?PHP get_file_curl使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_file_curl函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: http_curl
public function http_curl()
{
$url = "http://www.baidu.com";
$data['info'] = get_file_curl($url);
$data['menus'] = $this->get_menu_data();
$this->render('http/view_http_curl', $data);
}
示例2: wp_all_import_get_gz
function wp_all_import_get_gz($filename, $use_include_path = 0, $targetDir = false)
{
$type = 'csv';
$uploads = wp_upload_dir();
$targetDir = !$targetDir ? wp_all_import_secure_file($uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::UPLOADS_DIRECTORY) : $targetDir;
$tmpname = wp_unique_filename($targetDir, strlen(basename($filename)) < 30 ? basename($filename) : time());
$localPath = $targetDir . '/' . urldecode(sanitize_file_name($tmpname));
$fp = @fopen($localPath, 'w');
$file = @gzopen($filename, 'rb', $use_include_path);
if ($file) {
$first_chunk = true;
while (!gzeof($file)) {
$chunk = gzread($file, 1024);
if ($first_chunk and strpos($chunk, "<?") !== false and strpos($chunk, "</") !== false) {
$type = 'xml';
$first_chunk = false;
}
// if it's a 1st chunk, then chunk <? symbols to detect XML file
@fwrite($fp, $chunk);
}
gzclose($file);
} else {
$tmpname = wp_unique_filename($targetDir, strlen(basename($filename)) < 30 ? basename($filename) : time());
$localGZpath = $targetDir . '/' . urldecode(sanitize_file_name($tmpname));
$request = get_file_curl($filename, $localGZpath, false, true);
if (!is_wp_error($request)) {
$file = @gzopen($localGZpath, 'rb', $use_include_path);
if ($file) {
$first_chunk = true;
while (!gzeof($file)) {
$chunk = gzread($file, 1024);
if ($first_chunk and strpos($chunk, "<?") !== false and strpos($chunk, "</") !== false) {
$type = 'xml';
$first_chunk = false;
}
// if it's a 1st chunk, then chunk <? symbols to detect XML file
@fwrite($fp, $chunk);
}
gzclose($file);
}
@unlink($localGZpath);
} else {
return $request;
}
}
@fclose($fp);
if (preg_match('%\\W(gz)$%i', basename($localPath))) {
if (@rename($localPath, str_replace('.gz', '.' . $type, $localPath))) {
$localPath = str_replace('.gz', '.' . $type, $localPath);
}
} else {
if (@rename($localPath, $localPath . '.' . $type)) {
$localPath = $localPath . '.' . $type;
}
}
return array('type' => $type, 'localPath' => $localPath);
}
示例3: get_file_curl
function get_file_curl($url, $fullpath, $to_variable = false, $iteration = false)
{
if (!preg_match('%^(http|ftp)s?://%i', $url)) {
return;
}
$response = wp_remote_get($url);
if (!is_wp_error($response) and (!isset($response['response']['code']) or isset($response['response']['code']) and !in_array($response['response']['code'], array(401, 403, 404)))) {
$rawdata = wp_remote_retrieve_body($response);
if (empty($rawdata)) {
$result = pmxi_curl_download($url, $fullpath, $to_variable);
if (!$result and $iteration === false) {
$new_url = wp_all_import_translate_uri($url);
return $new_url !== $url ? get_file_curl($new_url, $fullpath, $to_variable, true) : $result;
}
return $result;
}
if (!@file_put_contents($fullpath, $rawdata)) {
$fp = fopen($fullpath, 'w');
fwrite($fp, $rawdata);
fclose($fp);
}
if (preg_match('%\\W(svg)$%i', basename($fullpath)) or preg_match('%\\W(jpg|jpeg|gif|png)$%i', basename($fullpath)) and (!($image_info = apply_filters('pmxi_getimagesize', @getimagesize($fullpath), $fullpath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)))) {
$result = pmxi_curl_download($url, $fullpath, $to_variable);
if (!$result and $iteration === false) {
$new_url = wp_all_import_translate_uri($url);
return $new_url !== $url ? get_file_curl($new_url, $fullpath, $to_variable, true) : $result;
}
return $result;
}
return $to_variable ? $rawdata : true;
} else {
$curl = pmxi_curl_download($url, $fullpath, $to_variable);
if ($curl === false and $iteration === false) {
$new_url = wp_all_import_translate_uri($url);
return $new_url !== $url ? get_file_curl($new_url, $fullpath, $to_variable, true) : (is_wp_error($response) ? $response : false);
}
return $curl === false ? is_wp_error($response) ? $response : false : $curl;
}
}
示例4: get_file_curl
function get_file_curl($url, $fullpath, $to_variable = false, $iteration = 0)
{
if (!preg_match('%^(http|ftp)s?://%i', $url)) {
return;
}
$request = wp_remote_get($url);
if (!is_wp_error($request)) {
$rawdata = wp_remote_retrieve_body($request);
if (empty($rawdata)) {
$result = pmxi_curl_download($url, $fullpath, $to_variable);
if (!$result and !$iteration) {
$url = wp_all_import_translate_uri($url);
return get_file_curl($url, $fullpath, $to_variable, 1);
}
return $result;
}
if (!@file_put_contents($fullpath, $rawdata)) {
$fp = fopen($fullpath, 'w');
fwrite($fp, $rawdata);
fclose($fp);
}
if (preg_match('%\\W(jpg|jpeg|gif|png)$%i', basename($fullpath)) and (!($image_info = @getimagesize($fullpath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)))) {
$result = pmxi_curl_download($url, $fullpath, $to_variable);
if (!$result and !$iteration) {
$url = wp_all_import_translate_uri($url);
return get_file_curl($url, $fullpath, $to_variable, 1);
}
return $result;
}
return $to_variable ? $rawdata : true;
} else {
$curl = pmxi_curl_download($url, $fullpath, $to_variable);
if ($curl === false and !$iteration) {
$url = wp_all_import_translate_uri($url);
return get_file_curl($url, $fullpath, $to_variable, 1);
}
return $curl === false ? $request : $curl;
}
}
示例5: pmxi_wp_ajax_test_images
function pmxi_wp_ajax_test_images()
{
if (!check_ajax_referer('wp_all_import_secure', 'security', false)) {
exit(json_encode(array('result' => array(), 'failed_msgs' => array(__('Security check', 'wp_all_import_plugin')))));
}
if (!current_user_can('manage_options')) {
exit(json_encode(array('result' => array(), 'failed_msgs' => array(__('Security check', 'wp_all_import_plugin')))));
}
$input = new PMXI_Input();
$post = $input->post(array('download' => 'yes', 'imgs' => array()));
$result = array();
$wp_uploads = wp_upload_dir();
$imgs_basedir = $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR;
$targetDir = $wp_uploads['path'];
$success_images = 0;
$success_msg = '';
$failed_msgs = array();
if (!@is_writable($targetDir)) {
$failed_msgs[] = sprintf(__('Uploads folder `%s` is not writable.', 'wp_all_import_plugin'), $targetDir);
} else {
if ('no' == $post['download']) {
if (!empty($post['imgs'])) {
foreach ($post['imgs'] as $img) {
if (preg_match('%^(http|https|ftp|ftps)%i', $img)) {
$failed_msgs[] = sprintf(__('Use image name instead of URL `%s`.', 'wp_all_import_plugin'), $img);
continue;
}
if (@file_exists($imgs_basedir . $img)) {
if (@is_readable($imgs_basedir . $img)) {
$success_images++;
} else {
$failed_msgs[] = sprintf(__('File `%s` isn\'t readable'), preg_replace('%.*/wp-content%', '/wp-content', $imgs_basedir . $img));
}
} else {
$failed_msgs[] = sprintf(__('File `%s` doesn\'t exist'), preg_replace('%.*/wp-content%', '/wp-content', $imgs_basedir . $img));
}
}
}
if ((int) $success_images === 1) {
$success_msg = sprintf(__('%d image was successfully retrieved from `%s`', 'wp_all_import_plugin'), $success_images, preg_replace('%.*/wp-content%', '/wp-content', $wp_uploads['basedir']) . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY);
} elseif ((int) $success_images > 1) {
$success_msg = sprintf(__('%d images were successfully retrieved from `%s`', 'wp_all_import_plugin'), $success_images, preg_replace('%.*/wp-content%', '/wp-content', $wp_uploads['basedir']) . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY);
}
} else {
$start = time();
if (!empty($post['imgs'])) {
foreach ($post['imgs'] as $img) {
if (!preg_match('%^(http|https|ftp|ftps)%i', $img)) {
$failed_msgs[] = sprintf(__('URL `%s` is not valid.', 'wp_all_import_plugin'), $img);
continue;
}
$image_name = wp_unique_filename($targetDir, 'test');
$image_filepath = $targetDir . '/' . $image_name;
$url = str_replace(" ", "%20", trim($img));
$request = get_file_curl($url, $image_filepath);
if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($img))) {
$failed_msgs[] = is_wp_error($request) ? $request->get_error_message() : sprintf(__('File `%s` cannot be saved locally', 'wp_all_import_plugin'), $img);
} elseif (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
$failed_msgs[] = sprintf(__('File `%s` is not a valid image.', 'wp_all_import_plugin'), $img);
} else {
$success_images++;
}
@unlink($image_filepath);
}
}
$time = time() - $start;
if ((int) $success_images === 1) {
$success_msg = sprintf(__('%d image was successfully downloaded in %s seconds', 'wp_all_import_plugin'), $success_images, number_format($time, 2));
} elseif ((int) $success_images > 1) {
$success_msg = sprintf(__('%d images were successfully downloaded in %s seconds', 'wp_all_import_plugin'), $success_images, number_format($time, 2));
}
}
}
exit(json_encode(array('success_images' => $success_images, 'success_msg' => $success_msg, 'failed_msgs' => $failed_msgs)));
die;
}
示例6: url
public function url($feed_type = '', $feed_xpath = '')
{
$uploads = wp_upload_dir();
$template = false;
if (empty($this->file)) {
$this->errors->add('form-validation', __('Please specify a file to import.', 'wp_all_import_plugin'));
} elseif (!preg_match('%^https?://%i', $this->file)) {
$this->errors->add('form-validation', __('The URL to your file is not valid.<br/><br/>Please make sure the URL starts with http:// or https://. To import from https://, your server must have OpenSSL installed.'), 'wp_all_import_plugin');
} elseif (!is_writeable($this->uploadsPath)) {
$this->errors->add('form-validation', __('Uploads folder ' . $this->uploadsPath . ' is not writable.'), 'wp_all_import_plugin');
}
$this->file = trim($this->file);
$csv_path = '';
if (empty($this->errors->errors)) {
if ('' == $feed_type and !preg_match('%\\W(xml|csv|zip|gz|xls|xlsx)$%i', trim($this->file))) {
$feed_type = wp_all_import_get_remote_file_name(trim($this->file));
}
if ('zip' == $feed_type or empty($feed_type) and preg_match('%\\W(zip)$%i', trim($this->file))) {
$tmpname = $this->uploadsPath . '/' . wp_unique_filename($this->uploadsPath, basename($this->file));
@copy($this->file, $tmpname);
if (!file_exists($tmpname)) {
$request = get_file_curl($this->file, $tmpname);
if (is_wp_error($request)) {
$this->errors->add('form-validation', $request->get_error_message());
}
if (!file_exists($tmpname)) {
$this->errors->add('form-validation', __('Failed upload ZIP archive', 'wp_all_import_plugin'));
}
}
if (!class_exists('PclZip')) {
include_once PMXI_Plugin::ROOT_DIR . '/libraries/pclzip.lib.php';
}
$archive = new PclZip($tmpname);
if (($v_result_list = $archive->extract(PCLZIP_OPT_PATH, $this->uploadsPath, PCLZIP_OPT_REPLACE_NEWER)) == 0) {
$this->errors->add('form-validation', __('WP All Import couldn\'t find a file to import inside your ZIP.<br/><br/>Either the .ZIP file is broken, or doesn\'t contain a file with an extension of XML, CSV, PSV, DAT, or TXT. <br/>Please attempt to unzip your .ZIP file on your computer to ensure it is a valid .ZIP file which can actually be unzipped, and that it contains a file which WP All Import can import.', 'wp_all_import_plugin'));
} else {
$filePath = '';
if (!empty($v_result_list)) {
foreach ($v_result_list as $unzipped_file) {
if ($unzipped_file['status'] == 'ok' and preg_match('%\\W(xml|csv|txt|dat|psv|json|xls|xlsx)$%i', trim($unzipped_file['stored_filename'])) and strpos($unzipped_file['stored_filename'], 'readme.txt') === false) {
if (strpos(basename($unzipped_file['stored_filename']), 'WP All Import Template') === 0 || strpos(basename($unzipped_file['stored_filename']), 'templates_') === 0) {
$template = file_get_contents($unzipped_file['filename']);
} elseif ($filePath == '') {
$filePath = $unzipped_file['filename'];
}
}
}
}
if ($this->uploadsPath === false) {
$this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'wp_all_import_plugin'));
}
if (empty($filePath)) {
$zip = zip_open(trim($tmpname));
if (is_resource($zip)) {
while ($zip_entry = zip_read($zip)) {
$filePath = zip_entry_name($zip_entry);
$fp = fopen($this->uploadsPath . "/" . $filePath, "w");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp, "{$buf}");
zip_entry_close($zip_entry);
fclose($fp);
}
break;
}
zip_close($zip);
} else {
$this->errors->add('form-validation', __('WP All Import couldn\'t find a file to import inside your ZIP.<br/><br/>Either the .ZIP file is broken, or doesn\'t contain a file with an extension of XML, CSV, PSV, DAT, or TXT. <br/>Please attempt to unzip your .ZIP file on your computer to ensure it is a valid .ZIP file which can actually be unzipped, and that it contains a file which WP All Import can import.', 'wp_all_import_plugin'));
}
}
// Detect if file is very large
$source = array('name' => basename(parse_url($this->file, PHP_URL_PATH)), 'type' => 'url', 'path' => $feed_xpath);
if (preg_match('%\\W(csv|txt|dat|psv)$%i', trim($filePath))) {
include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
$csv = new PMXI_CsvParser(array('filename' => $filePath, 'targetDir' => $this->uploadsPath));
// create chunks
//wp_all_import_remove_source($filePath, false);
$csv_path = $filePath;
$filePath = $csv->xml_path;
$this->is_csv = $csv->is_csv;
$this->root_element = 'node';
} elseif (preg_match('%\\W(json)$%i', trim($filePath))) {
$json_str = file_get_contents($filePath);
$is_json = wp_all_import_is_json($json_str);
if (is_wp_error($is_json)) {
$this->errors->add('form-validation', $is_json->get_error_message(), 'wp_all_import_plugin');
} else {
$xml_data = wp_all_import_json_to_xml(json_decode($json_str, true));
if (empty($xml_data)) {
$this->errors->add('form-validation', __('Can not import this file. JSON to XML convertation failed.', 'wp_all_import_plugin'));
} else {
$jsontmpname = $this->uploadsPath . '/' . wp_all_import_url_title(wp_unique_filename($this->uploadsPath, str_replace("json", "xml", basename($filePath))));
file_put_contents($jsontmpname, $xml_data);
wp_all_import_remove_source($filePath, false);
$filePath = $jsontmpname;
}
}
} elseif (preg_match('%\\W(sql)$%i', trim($filePath))) {
include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportSQLParse.php';
$localSQLPath = $filePath;
//.........这里部分代码省略.........
示例7: pmxi_wp_ajax_test_images
function pmxi_wp_ajax_test_images()
{
if (!check_ajax_referer('wp_all_import_secure', 'security', false)) {
exit(json_encode(array('result' => array(), 'failed_msgs' => array(__('Security check', 'wp_all_import_plugin')))));
}
if (!current_user_can(PMXI_Plugin::$capabilities)) {
exit(json_encode(array('result' => array(), 'failed_msgs' => array(__('Security check', 'wp_all_import_plugin')))));
}
$input = new PMXI_Input();
$post = $input->post(array('download' => 'yes', 'imgs' => array()));
$result = array();
$wp_uploads = wp_upload_dir();
$imgs_basedir = $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR;
$targetDir = $wp_uploads['path'];
$success_images = 0;
$success_msg = '';
$failed_msgs = array();
if (!@is_writable($targetDir)) {
$failed_msgs[] = sprintf(__('Uploads folder `%s` is not writable.', 'wp_all_import_plugin'), $targetDir);
} else {
switch ($post['download']) {
// Use images currently uploaded in wp-content/uploads/wpallimport/files/
case 'no':
if (!empty($post['imgs'])) {
foreach ($post['imgs'] as $img) {
if (preg_match('%^(http|https|ftp|ftps)%i', $img)) {
$failed_msgs[] = sprintf(__('Use image name instead of URL `%s`.', 'wp_all_import_plugin'), $img);
continue;
}
if (@file_exists($imgs_basedir . $img)) {
if (@is_readable($imgs_basedir . $img)) {
$success_images++;
} else {
$failed_msgs[] = sprintf(__('File `%s` isn\'t readable', 'wp_all_import_plugin'), preg_replace('%.*/wp-content%', '/wp-content', $imgs_basedir . $img));
}
} else {
$failed_msgs[] = sprintf(__('File `%s` doesn\'t exist', 'wp_all_import_plugin'), preg_replace('%.*/wp-content%', '/wp-content', $imgs_basedir . $img));
}
}
}
if ((int) $success_images === 1) {
$success_msg = sprintf(__('%d image was successfully retrieved from `%s`', 'wp_all_import_plugin'), $success_images, preg_replace('%.*/wp-content%', '/wp-content', $wp_uploads['basedir']) . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY);
} elseif ((int) $success_images > 1) {
$success_msg = sprintf(__('%d images were successfully retrieved from `%s`', 'wp_all_import_plugin'), $success_images, preg_replace('%.*/wp-content%', '/wp-content', $wp_uploads['basedir']) . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY);
}
break;
// Use images currently in Media Library
// Use images currently in Media Library
case 'gallery':
if (!empty($post['imgs'])) {
global $wpdb;
foreach ($post['imgs'] as $img) {
$bn = wp_all_import_sanitize_filename(basename($img));
$img_ext = pmxi_getExtensionFromStr($img);
$default_extension = pmxi_getExtension($bn);
$image_name = apply_filters("wp_all_import_image_filename", urldecode(sanitize_file_name($img_ext ? str_replace("." . $default_extension, "", $bn) : $bn)) . ("" != $img_ext ? '.' . $img_ext : ''));
$attch = wp_all_import_get_image_from_gallery($image_name, $targetDir);
if (!empty($attch)) {
$success_images++;
} else {
$failed_msgs[] = sprintf(__('Image `%s` not found in media library.', 'wp_all_import_plugin'), $image_name);
}
}
}
if ((int) $success_images === 1) {
$success_msg = sprintf(__('%d image was successfully founded in media gallery', 'wp_all_import_plugin'), $success_images);
} elseif ((int) $success_images > 1) {
$success_msg = sprintf(__('%d images were successfully founded in media gallery', 'wp_all_import_plugin'), $success_images);
}
break;
// Download images hosted elsewhere
// Download images hosted elsewhere
default:
$start = time();
if (!empty($post['imgs'])) {
foreach ($post['imgs'] as $img) {
if (!preg_match('%^(http|https|ftp|ftps)%i', $img)) {
$failed_msgs[] = sprintf(__('URL `%s` is not valid.', 'wp_all_import_plugin'), $img);
continue;
}
$image_name = wp_unique_filename($targetDir, 'test');
$image_filepath = $targetDir . '/' . $image_name;
$url = trim($img);
$request = get_file_curl($url, $image_filepath);
$get_ctx = stream_context_create(array('http' => array('timeout' => 5)));
if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($img, false, $get_ctx))) {
$failed_msgs[] = is_wp_error($request) ? $request->get_error_message() : sprintf(__('File `%s` cannot be saved locally', 'wp_all_import_plugin'), $img);
} elseif (!($image_info = apply_filters('pmxi_getimagesize', @getimagesize($image_filepath), $image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
$failed_msgs[] = sprintf(__('File `%s` is not a valid image.', 'wp_all_import_plugin'), $img);
} else {
$success_images++;
}
@unlink($image_filepath);
}
}
$time = time() - $start;
if ((int) $success_images === 1) {
$success_msg = sprintf(__('%d image was successfully downloaded in %s seconds', 'wp_all_import_plugin'), $success_images, number_format($time, 2));
} elseif ((int) $success_images > 1) {
$success_msg = sprintf(__('%d images were successfully downloaded in %s seconds', 'wp_all_import_plugin'), $success_images, number_format($time, 2));
//.........这里部分代码省略.........
示例8: file_get_contents
$url = $sportscapping_site_path . '/new/export/?' . $params_array[4] . '/export/capper/' . $params_array[2] . '/overall';
}
if ($params_array[0] == 'top_trends') {
$url = $sportscapping_site_path . '/new/export/?' . $your_partner_code . '/export/top_trends/menu/' . $named_param['leg'] . '/10';
}
if ($params_array[0] == 'top_cappers') {
$url = $sportscapping_site_path . '/new/export/?' . $your_partner_code . '/export/top_cappers/full/menu/' . $named_param['leg'];
}
if ($mode == 'standard') {
echo file_get_contents($url);
}
if ($mode == 'fopen') {
echo get_file($url);
}
if ($mode == 'curl') {
echo get_file_curl($url);
}
if ($mode == 'iframe') {
if ($params_array[0] == 'premium_picks') {
$url = $sportscapping_site_path . '/new/export/?' . $your_partner_code . '/export/premium_picks/menu/' . $named_param['leg'] . '/' . $params_array[1];
}
if ($params_array[0] == 'free_picks') {
$url = $sportscapping_site_path . '/new/export/?' . $your_partner_code . '/export/free_picks';
}
if ($params_array[0] == 'leaderboard') {
$url = $sportscapping_site_path . '/new/export/?' . $your_partner_code . '/export/leaderboard/menu/5/' . $named_param['leg'];
}
if ($params_array[0] == 'capper') {
$url = $sportscapping_site_path . '/new/export/?' . $params_array[4] . '/export/capper/' . $params_array[2] . '/overall';
}
if ($params_array[0] == 'top_trends') {
示例9: process
//.........这里部分代码省略.........
}
}
}
}
if ($download_image) {
// do not download images
if (!$this->options['download_images']) {
$image_filename = $image_name;
$image_filepath = $uploads['path'] . '/' . $image_filename;
$existing_attachment = $this->wpdb->get_row($this->wpdb->prepare("SELECT * FROM " . $this->wpdb->prefix . "posts WHERE guid = '%s'", $uploads['url'] . '/' . $image_filename));
if (!empty($existing_attachment->ID)) {
$download_image = false;
$create_image = false;
if (!has_post_thumbnail($pid)) {
set_post_thumbnail($pid, $existing_attachment->ID);
} else {
$gallery_attachment_ids[] = $existing_attachment->ID;
}
do_action('pmxi_gallery_image', $pid, $existing_attachment->ID, $image_filepath);
} else {
if (@file_exists($image_filepath)) {
$download_image = false;
if (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
$logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'pmxi_plugin'), $image_filepath));
$logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings'];
@unlink($image_filepath);
} else {
$create_image = true;
}
}
}
}
if ($download_image) {
$request = get_file_curl($url, $image_filepath);
if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($url))) {
@unlink($image_filepath);
// delete file since failed upload may result in empty file created
} elseif ($image_info = @getimagesize($image_filepath) and in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
$create_image = true;
}
if (!$create_image) {
$url = str_replace(" ", "%20", trim(pmxi_convert_encoding($img_url)));
$request = get_file_curl($url, $image_filepath);
if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($url))) {
$logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: File %s cannot be saved locally as %s', 'pmxi_plugin'), $url, $image_filepath));
$logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings'];
@unlink($image_filepath);
// delete file since failed upload may result in empty file created
} elseif (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
$logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'pmxi_plugin'), $url));
$logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings'];
@unlink($image_filepath);
} else {
$create_image = true;
}
}
}
}
}
if ($create_image) {
$attachment = array('post_mime_type' => image_type_to_mime_type($image_info[2]), 'guid' => $uploads['url'] . '/' . $image_filename, 'post_title' => $image_filename, 'post_content' => '', 'post_author' => $post_author[$i]);
if ($image_meta = wp_read_image_metadata($image_filepath)) {
if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
$attachment['post_title'] = $image_meta['title'];
}
if (trim($image_meta['caption'])) {
示例10: pmxi_wp_ajax_test_images
function pmxi_wp_ajax_test_images()
{
extract($_POST);
$result = array();
$wp_uploads = wp_upload_dir();
$imgs_basedir = $wp_uploads['basedir'] . '/wpallimport/files/';
$targetDir = $wp_uploads['path'];
// . '/wpallimport/uploads';
$success_images = 0;
$success_msg = '';
$failed_msgs = array();
if (!@is_writable($targetDir)) {
$failed_msgs[] = sprintf(__('Uploads folder `%s` is not writable.', 'pmxi_plugin'), $targetDir);
} else {
if ('no' == $download) {
if (!empty($imgs)) {
foreach ($imgs as $img) {
if (preg_match('%^(http|https|ftp|ftps)%i', $img)) {
$failed_msgs[] = sprintf(__('Use image name instead of URL `%s`.', 'pmxi_plugin'), $img);
continue;
}
if (@file_exists($imgs_basedir . $img)) {
if (@is_readable($imgs_basedir . $img)) {
$success_images++;
} else {
$failed_msgs[] = sprintf(__('File `%s` isn\'t readable'), preg_replace('%.*/wp-content%', '/wp-content', $imgs_basedir . $img));
}
} else {
$failed_msgs[] = sprintf(__('File `%s` doesn\'t exist'), preg_replace('%.*/wp-content%', '/wp-content', $imgs_basedir . $img));
}
}
}
if ((int) $success_images === 1) {
$success_msg = sprintf(__('%d image was successfully retrieved from `%s/wpallimport/files`', 'pmxi_plugin'), $success_images, preg_replace('%.*/wp-content%', '/wp-content', $wp_uploads['basedir']));
} elseif ((int) $success_images > 1) {
$success_msg = sprintf(__('%d images were successfully retrieved from `%s/wpallimport/files`', 'pmxi_plugin'), $success_images, preg_replace('%.*/wp-content%', '/wp-content', $wp_uploads['basedir']));
}
} else {
$start = time();
if (!empty($imgs)) {
foreach ($imgs as $img) {
if (!preg_match('%^(http|https|ftp|ftps)%i', $img)) {
$failed_msgs[] = sprintf(__('URL `%s` is not valid.', 'pmxi_plugin'), $img);
continue;
}
$image_name = wp_unique_filename($targetDir, 'test');
$image_filepath = $targetDir . '/' . $image_name;
$request = get_file_curl($img, $image_filepath);
if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($img))) {
$failed_msgs[] = is_wp_error($request) ? $request->get_error_message() : sprintf(__('File `%s` cannot be saved locally', 'pmxi_plugin'), $img);
} elseif (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
$failed_msgs[] = sprintf(__('File `%s` is not a valid image.', 'pmxi_plugin'), $img);
} else {
$success_images++;
}
@unlink($image_filepath);
}
}
$time = time() - $start;
if ((int) $success_images === 1) {
$success_msg = sprintf(__('%d image was successfully downloaded in %s seconds', 'pmxi_plugin'), $success_images, number_format($time, 2));
} elseif ((int) $success_images > 1) {
$success_msg = sprintf(__('%d images were successfully downloaded in %s seconds', 'pmxi_plugin'), $success_images, number_format($time, 2));
}
}
}
exit(json_encode(array('success_images' => $success_images, 'success_msg' => $success_msg, 'failed_msgs' => $failed_msgs)));
die;
}
示例11: import
//.........这里部分代码省略.........
if ("gallery" == $this->import->options['download_images']) {
$download_image = false;
}
if (empty($attch)) {
$logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Image %s not found in media gallery.', 'wp_all_import_plugin'), trim($image_name)));
} else {
$logger and call_user_func($logger, sprintf(__('- Using existing image `%s` for post `%s` ...', 'wp_all_import_plugin'), trim($image_name), $variation_post_title));
$download_image = false;
$create_image = false;
$attid = $attch->ID;
}
}
if ($download_image && "gallery" != $this->import->options['download_images']) {
// do not download images
if ("no" == $this->import->options['download_images']) {
$image_filename = $image_name;
$image_filepath = $targetDir . DIRECTORY_SEPARATOR . $image_filename;
$wpai_uploads = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR;
$wpai_image_path = $wpai_uploads . str_replace('%20', ' ', $url);
$logger and call_user_func($logger, sprintf(__('- Searching for existing image `%s` in `%s` folder', 'wp_all_import_plugin'), $wpai_image_path, $wpai_uploads));
if (@file_exists($wpai_image_path) and @copy($wpai_image_path, $image_filepath)) {
$download_image = false;
// valdate import attachments
if (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
$logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'wp_all_import_plugin'), $image_filepath));
@unlink($image_filepath);
} else {
$create_image = true;
$logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully found', 'wp_all_import_plugin'), $wpai_image_path));
}
}
} else {
$logger and call_user_func($logger, sprintf(__('- Downloading image from `%s`', 'wp_all_import_plugin'), $url));
$request = get_file_curl($url, $image_filepath);
if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($url))) {
@unlink($image_filepath);
// delete file since failed upload may result in empty file created
} else {
if ($image_info = @getimagesize($image_filepath) and in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
$create_image = true;
$logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url));
}
}
if (!$create_image) {
$url = str_replace(" ", "%20", trim(pmxi_convert_encoding($img_url)));
$request = get_file_curl($url, $image_filepath);
if ((is_wp_error($request) or $request === false) and !@file_put_contents($image_filepath, @file_get_contents($url))) {
$logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s cannot be saved locally as %s', 'wp_all_import_plugin'), $url, $image_filepath));
@unlink($image_filepath);
// delete file since failed upload may result in empty file created
} else {
if (!($image_info = @getimagesize($image_filepath)) or !in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
$logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'wp_all_import_plugin'), $url));
@unlink($image_filepath);
} else {
$create_image = true;
$logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url));
}
}
}
}
}
$handle_image = false;
if ($create_image) {
$handle_image = array('file' => $image_filepath, 'url' => $targetUrl . '/' . $image_filename, 'type' => image_type_to_mime_type($image_info[2]));
$logger and call_user_func($logger, sprintf(__('- Creating an attachment for image `%s`', 'wp_all_import_plugin'), $handle_image['url']));
示例12: wp_all_import_get_url
function wp_all_import_get_url($filePath, $targetDir = false, $contentType = false, $contentEncoding = false, $detect = false)
{
$type = $contentType;
$uploads = wp_upload_dir();
$targetDir = !$targetDir ? wp_all_import_secure_file($uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::UPLOADS_DIRECTORY) : $targetDir;
$tmpname = wp_unique_filename($targetDir, ($type and strlen(basename($filePath)) < 30) ? basename($filePath) : time());
$localPath = $targetDir . '/' . urldecode(sanitize_file_name($tmpname)) . (!$type ? '.tmp' : '');
if ($contentEncoding == 'gzip') {
$file = @fopen($filePath);
} else {
$file = @fopen($filePath, "rb");
}
$is_valid = false;
if (is_resource($file)) {
$fp = @fopen($localPath, 'w');
$first_chunk = true;
while (!@feof($file)) {
$chunk = @fread($file, 1024);
if (!$type and $first_chunk and (strpos($chunk, "<?") !== false or strpos($chunk, "<rss") !== false) or strpos($chunk, "xmlns") !== false) {
$type = 'xml';
} elseif (!$type and $first_chunk) {
$type = 'csv';
}
// if it's a 1st chunk, then chunk <? symbols to detect XML file
$first_chunk = false;
@fwrite($fp, $chunk);
}
@fclose($file);
@fclose($fp);
$chunk = new PMXI_Chunk($localPath);
$is_valid = true;
if (!empty($chunk->options['element'])) {
$defaultXpath = "/" . $chunk->options['element'];
} else {
$is_valid = false;
}
if ($is_valid) {
while ($xml = $chunk->read()) {
if (!empty($xml)) {
PMXI_Import_Record::preprocessXml($xml);
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n" . $xml;
$dom = new DOMDocument('1.0', 'UTF-8');
$old = libxml_use_internal_errors(true);
$dom->loadXML($xml);
libxml_use_internal_errors($old);
$xpath = new DOMXPath($dom);
if ($elements = $xpath->query($defaultXpath) and $elements->length) {
break;
}
}
}
if (empty($xml)) {
$is_valid = false;
}
}
unset($chunk);
}
if (!$is_valid) {
$request = get_file_curl($filePath, $localPath);
if (!is_wp_error($request)) {
if (!$type) {
if ($contentEncoding == 'gzip') {
$file = @fopen($localPath);
} else {
$file = @fopen($localPath, "rb");
}
while (!@feof($file)) {
$chunk = @fread($file, 1024);
if (strpos($chunk, "<?") !== false or strpos($chunk, "<rss") !== false or strpos($chunk, "xmlns") !== false) {
$type = 'xml';
} else {
$type = 'csv';
}
// if it's a 1st chunk, then chunk <? symbols to detect XML file
break;
}
@fclose($file);
}
} else {
return $request;
}
}
if (!preg_match('%\\W(' . $type . ')$%i', basename($localPath))) {
if (@rename($localPath, $localPath . '.' . $type)) {
$localPath = $localPath . '.' . $type;
}
}
return $detect ? array('type' => $type, 'localPath' => $localPath) : $localPath;
}
示例13: wp_all_import_get_gz
function wp_all_import_get_gz($filename, $use_include_path = 0, $targetDir = false, $headers = false)
{
$type = 'csv';
$uploads = wp_upload_dir();
$targetDir = !$targetDir ? wp_all_import_secure_file($uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::UPLOADS_DIRECTORY) : $targetDir;
$tmpname = wp_unique_filename($targetDir, strlen(basename($filename)) < 30 ? basename($filename) : time());
$localPath = $targetDir . '/' . urldecode(sanitize_file_name($tmpname));
$fp = @fopen($localPath, 'w');
$file = @gzopen($filename, 'rb', $use_include_path);
if ($file) {
$first_chunk = true;
while (!gzeof($file)) {
$chunk = gzread($file, 1024);
if ($first_chunk and strpos($chunk, "<?") !== false and strpos($chunk, "</") !== false) {
$type = 'xml';
$first_chunk = false;
}
// if it's a 1st chunk, then chunk <? symbols to detect XML file
@fwrite($fp, $chunk);
}
gzclose($file);
} else {
$tmpname = wp_unique_filename($targetDir, strlen(basename($filename)) < 30 ? basename($filename) : time());
$localGZpath = $targetDir . '/' . urldecode(sanitize_file_name($tmpname));
$request = get_file_curl($filename, $localGZpath, false, true);
if (!is_wp_error($request)) {
$file = @gzopen($localGZpath, 'rb', $use_include_path);
if ($file) {
$first_chunk = true;
while (!gzeof($file)) {
$chunk = gzread($file, 1024);
if ($first_chunk and strpos($chunk, "<?") !== false and strpos($chunk, "</") !== false) {
$type = 'xml';
$first_chunk = false;
}
// if it's a 1st chunk, then chunk <? symbols to detect XML file
@fwrite($fp, $chunk);
}
gzclose($file);
}
@unlink($localGZpath);
} else {
return $request;
}
}
@fclose($fp);
if (strpos($headers['Content-Disposition'], 'tar.gz') !== false && class_exists('PharData')) {
rename($localPath, $localPath . '.tar');
$phar = new PharData($localPath . '.tar');
$phar->extractTo($targetDir);
@unlink($localPath . '.tar');
$scanned_files = @scandir($targetDir);
if (!empty($scanned_files) and is_array($scanned_files)) {
$files = array_diff($scanned_files, array('.', '..'));
if (!empty($files)) {
foreach ($files as $file) {
if (preg_match('%\\W(csv|xml|json|sql|txt|xls|xlsx)$%i', basename($file))) {
$localPath = $targetDir . DIRECTORY_SEPARATOR . $file;
break;
}
}
}
}
}
if (preg_match('%\\W(gz)$%i', basename($localPath))) {
if (@rename($localPath, str_replace('.gz', '.' . $type, $localPath))) {
$localPath = str_replace('.gz', '.' . $type, $localPath);
}
} else {
if (@rename($localPath, $localPath . '.' . $type)) {
$localPath = $localPath . '.' . $type;
}
}
return array('type' => $type, 'localPath' => $localPath);
}
示例14: wp_all_import_get_url
function wp_all_import_get_url($filePath, $targetDir = false, $contentType = false, $contentEncoding = false, $detect = false)
{
$type = $contentType;
$uploads = wp_upload_dir();
$targetDir = !$targetDir ? wp_all_import_secure_file($uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::UPLOADS_DIRECTORY) : $targetDir;
$tmpname = wp_unique_filename($targetDir, ($type and strlen(basename($filePath)) < 30) ? basename($filePath) : time());
$localPath = $targetDir . '/' . urldecode(sanitize_file_name($tmpname)) . (!$type ? '.tmp' : '');
if ($contentEncoding == 'gzip') {
$file = @fopen($filePath);
} else {
$file = @fopen($filePath, "rb");
}
if (is_resource($file)) {
$fp = @fopen($localPath, 'w');
$first_chunk = true;
while (!@feof($file)) {
$chunk = @fread($file, 1024);
if (!$type and $first_chunk and (strpos($chunk, "<?") !== false or strpos($chunk, "<rss") !== false) or strpos($chunk, "xmlns") !== false) {
$type = 'xml';
} elseif (!$type and $first_chunk) {
$type = 'csv';
}
// if it's a 1st chunk, then chunk <? symbols to detect XML file
$first_chunk = false;
@fwrite($fp, $chunk);
}
@fclose($file);
@fclose($fp);
}
if (!file_exists($localPath)) {
$request = get_file_curl($filePath, $localPath);
if (!is_wp_error($request)) {
if (!$type) {
if ($contentEncoding == 'gzip') {
$file = @fopen($localPath);
} else {
$file = @fopen($localPath, "rb");
}
while (!@feof($file)) {
$chunk = @fread($file, 1024);
if (strpos($chunk, "<?") !== false or strpos($chunk, "<rss") !== false or strpos($chunk, "xmlns") !== false) {
$type = 'xml';
} else {
$type = 'csv';
}
// if it's a 1st chunk, then chunk <? symbols to detect XML file
break;
}
@fclose($file);
}
} else {
return $request;
}
}
if (!preg_match('%\\W(' . $type . ')$%i', basename($localPath))) {
if (@rename($localPath, $localPath . '.' . $type)) {
$localPath = $localPath . '.' . $type;
}
}
return $detect ? array('type' => $type, 'localPath' => $localPath) : $localPath;
}
示例15: import_file
public function import_file($atch_url, $pid, $logger, $fast = false)
{
global $wpdb;
$uploads = wp_upload_dir();
$file_name = sanitize_file_name(basename(parse_url(trim($atch_url), PHP_URL_PATH)));
$wpai_uploads = PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR;
if (!preg_match('%^https?://%i', $atch_url)) {
$file_path = $wpai_uploads . $atch_url;
} else {
$file_path = $uploads['path'] . '/' . $file_name;
}
$attachment_filename = wp_unique_filename($uploads['path'], $file_name);
$attachment_filepath = $uploads['path'] . '/' . sanitize_file_name($attachment_filename);
$download_file = true;
$create_file = false;
if ($this->parsing_data['import']->options['is_search_existing_attach']) {
// searching for existing attachment
$attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE (post_title = %s OR post_title = %s OR post_name = %s) AND post_type = %s;", $file_name, preg_replace('/\\.[^.\\s]{3,4}$/', '', $file_name), sanitize_title(preg_replace('/\\.[^.\\s]{3,4}$/', '', $file_name)), "attachment"));
if ($attachment_id and !is_wp_error($attachment_id)) {
return $attachment_id;
}
if (@file_exists($file_path)) {
if (!($wp_filetype = wp_check_filetype(basename($file_name), null))) {
$logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s is not a valid image and cannot be set as ACF image', 'pmxi_plugin'), $file_path));
} else {
$download_file = false;
$create_file = true;
}
}
}
if ($download_file) {
if (!get_file_curl(trim($atch_url), $attachment_filepath) and !@file_put_contents($attachment_filepath, @file_get_contents(trim($atch_url)))) {
$logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Attachment file %s cannot be saved locally as %s', 'pmxi_plugin'), trim($atch_url), $attachment_filepath));
unlink($attachment_filepath);
// delete file since failed upload may result in empty file created
} elseif (!($wp_filetype = wp_check_filetype(basename($attachment_filename), null))) {
$logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Can\'t detect attachment file type %s', 'pmxi_plugin'), trim($atch_url)));
} else {
$attachment_data = array('guid' => $uploads['baseurl'] . '/' . _wp_relative_upload_path($attachment_filepath), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($attachment_filepath)), 'post_content' => '', 'post_status' => 'inherit');
$attach_id = wp_insert_attachment($attachment_data, $attachment_filepath, $pid);
if (is_wp_error($attach_id)) {
$logger and call_user_func($logger, __('- <b>WARNING</b>', 'pmxi_plugin') . ': ' . $pid->get_error_message());
} else {
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
require_once ABSPATH . 'wp-admin/includes/image.php';
do_action('pmxi_attachment_uploaded', $pid, $attach_id, $attachment_filepath);
wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attachment_filepath));
return $attach_id;
}
}
}
if ($create_file) {
$attachment_data = array('guid' => $uploads['baseurl'] . '/' . _wp_relative_upload_path($attachment_filepath), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($attachment_filepath)), 'post_content' => '', 'post_status' => 'inherit');
$attach_id = wp_insert_attachment($attachment_data, $attachment_filepath, $pid);
if (is_wp_error($attach_id)) {
$logger and call_user_func($logger, __('- <b>WARNING</b>', 'pmxi_plugin') . ': ' . $pid->get_error_message());
} else {
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
require_once ABSPATH . 'wp-admin/includes/image.php';
do_action('pmxi_attachment_uploaded', $pid, $attach_id, $attachment_filepath);
wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attachment_filepath));
return $attach_id;
}
}
return false;
}