list($previewMaxWidth, $previewMaxHeight) = preg_split('/x/', $_config["extrasPreviewRes"]); $previewMaxSize = $_config["extrasPreviewSize"]; if (($previewInfo[2] < 1 || $previewInfo[2] > 4) && $previewInfo[2] != 13) { notice("Your normal quality image is not a valid JPG, GIF, PNG or SWF image file."); break; } if ($previewInfo[0] > $previewMaxWidth || $previewInfo[1] > $previewMaxHeight) { notice(sprintf("Your normal quality image is %dx%d pixels. Maximum is %dx%d pixels.", $previewInfo[0], $previewInfo[1], $previewMaxWidth, $previewMaxHeight)); break; } if ($previewSize > $previewMaxSize) { notice(sprintf("Your normal quality image is %d bytes. Maximum is %d bytes.", $previewSize, $previewMaxSize)); break; } // Validate Full quality image $fullSize = intval(remote_filesize($_POST["fullURL"])); if ($fullSize < 512 || !preg_match('/^http\\:\\/\\//', $_POST["thumbURL"])) { notice("Unable to access the full quality image file at your server. " . "Please ensure that the given URL is correct and try again. " . "Also make sure that your server supports direct linking to the images."); break; } $fullInfo = getimagesize($_POST["fullURL"]); if (($fullInfo[2] < 1 || $fullInfo[2] > 4) && $fullInfo[2] != 13) { notice("Your full quality image is not a valid JPG, GIF, PNG or SWF image file."); break; } // Everything's ok! $filters = ""; $first = true; foreach (getEnabledFilters() as $filter) { if (isset($_POST["filter" . $filter])) { $filters .= ($first ? "" : ",") . $filter;
function url_handler() { list($uploads_dir, $thumbs_dir) = setup_dir(); $url = $_POST['url']; $name = escape_special_char(basename($url)); $result = array('qid' => $_POST['qid']); $host = get_cdn(); $purl = parse_url($url); if ($purl['host'] == $_SERVER['SERVER_NAME'] || CDN_ENABLED && in_array($purl['host'], explode(',', CDN_LIST))) { $result['status'] = 'success'; $result['name'] = 'duplicate'; $result['path'] = $url; $result['thumb'] = $url; return $result; } if (remote_filesize($url) > get_size_limit()) { $result['status'] = 'failed'; $result['err'] = 'size_limit'; } else { if ($content = @file_get_contents($url)) { $temp = ABSPATH . '/' . UPLOAD_DIR . '/working/' . $name . time(); if (!file_put_contents($temp, $content)) { $result['status'] = 'failed'; $result['err'] = 'write_prohibited'; return $result; } else { if ($duplicate = is_duplicate($temp)) { $result['status'] = 'success'; $result['thumb'] = ($duplicate['thumb'] == 'none' ? '' : $host) . $duplicate['thumb']; $result['path'] = $host . $duplicate['path']; $result['name'] = $duplicate['name']; $result['width'] = $duplicate['width']; $result['height'] = $duplicate['height']; $result['exlong'] = $duplicate['exlong']; $result['extiny'] = $duplicate['extiny']; unlink($temp); } else { if (filesize($temp) > get_size_limit()) { $result['status'] = 'failed'; $result['err'] = 'size_limit'; unlink($temp); } else { $mime = file_mime_type($temp); switch ($mime) { case 'image/jpeg': if (!preg_match('/\\.(jpg|jpeg|jpe|jfif|jfi|jif)$/i', $name)) { $name .= '.jpg'; } break; case 'image/png': if (!preg_match('/\\.(png)$/i', $name)) { $name .= '.png'; } break; case 'image/gif': if (!preg_match('/\\.(gif)$/i', $name)) { $name .= '.gif'; } break; case 'image/svg+xml': if (!preg_match('/\\.(svg)$/i', $name)) { $name .= '.svg'; } break; default: $result['status'] = 'failed'; $result['err'] = 'wrong_type'; } if (!isset($result['status']) || !$result['status'] == 'failed') { $name = rename_if_exists($name, $uploads_dir); $path = "{$uploads_dir}/{$name}"; if (!copy($temp, ABSPATH . '/' . $path)) { $result['status'] = 'failed'; $result['err'] = 'write_prohibited'; } else { watermark($path); $thumb = make_thumb($name, $path, $thumbs_dir); if (duplicate_hash($name, $path, $thumb)) { $result['status'] = 'success'; } else { $result['status'] = 'error'; $result['err'] = 'fail_duplicate'; } $result['path'] = $host . $path; $result['name'] = $name; $result['thumb'] = $thumb['generated'] ? $host . $thumb['path'] : 'none'; if (isset($thumb['width'])) { $result['width'] = $thumb['width']; $result['height'] = $thumb['height']; $result['exlong'] = $thumb['exlong']; $result['extiny'] = $thumb['extiny']; } } } unlink($temp); } } } } else { $result['status'] = 'failed'; $result['err'] = 'no_file'; } } return $result; }
// Get filesize $filesize = 0; $header_filename = ''; if (function_exists('get_headers')) { // php5 method $ary_header = @get_headers($thefile, 1); if (is_array($ary_header) && array_key_exists("Content-Length", $ary_header)) { $filesize = $ary_header["Content-Length"]; } if (is_array($ary_header) && array_key_exists("Content-Disposition", $ary_header)) { $header_filename = $ary_header["Content-Disposition"]; } } else { if (function_exists('curl_init')) { // Curl Method $filesize = remote_filesize($thefile); } else { $filesize = @filesize($thefile); } } if (isset($filesize) && $filesize > 0) { header("Content-Length: " . $filesize); } header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Robots: none"); header("Content-Type: " . $ctype . ""); header("Content-Description: File Transfer"); header("Content-Transfer-Encoding: binary");
function save_custom_meta($post_id) { global $custom_meta_fields, $post; // verify nonce if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__))) { return $post_id; } // check autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } // check permissions if ('page' == $_POST['post_type']) { if (!current_user_can('edit_page', $post_id)) { return $post_id; } } elseif (!current_user_can('edit_post', $post_id)) { return $post_id; } $files = getFileByPostID($post->ID); // loop through fields and save the data foreach ($custom_meta_fields as $field) { if ($field['type'] == 'tax_select') { continue; } $old = get_post_meta($post_id, $field['id'], true); $new = $_POST[$field['id']]; if ($new && $new != $old) { update_post_meta($post_id, $field['id'], $new); } elseif ('' == $new && $old) { delete_post_meta($post_id, $field['id'], $old); } if ($field['type'] == 'download') { $gID = $_POST['_infomation_download_file_id_']; $files = (array) $files; foreach ($files as $file) { $file = (array) $file; if (!in_array($file['file_id'], $gID)) { deleteFile(array('file_id' => $file['file_id'])); } } $gFileName = $_POST['_infomation_download_file_name_']; $gFileLink = $_POST['_infomation_download_file_']; $gFileDes = $_POST['_infomation_download_des_']; for ($i = 0; $i < count($gID); $i++) { if (!empty($gFileName[$i]) && !empty($gFileLink[$i])) { if (empty($gID[$i])) { $data = array('post_id' => $post_id, 'file' => $gFileLink[$i], 'jad_file' => " ", 'file_name' => $gFileName[$i], 'file_des' => $gFileDes[$i], 'file_size' => remote_filesize($gFileLink[$i]), 'file_date' => date("d-m-Y H:i:s"), 'file_updated_date' => date("d-m-Y H:i:s"), 'file_last_downloaded_date' => date("d-m-Y H:i:s")); insertFile($data); } else { $data = array('post_id' => $post_id, 'file' => $gFileLink[$i], 'jad_file' => " ", 'file_name' => $gFileName[$i], 'file_des' => $gFileDes[$i], 'file_size' => remote_filesize($gFileLink[$i]), 'file_updated_date' => date("d-m-Y H:i:s")); $where = array('file_id' => $gID[$i]); updateFile($data, $where); } } } } } // enf foreach }
function testRemoteFilesize() { $this->equals(remote_filesize("http://s3.amazonaws.com/amz.clickontyler.com/blog/105home.png"), '132729'); }
static $regex = '/^Content-Length: *+\\K\\d++$/im'; if (!($fp = @fopen($url, 'rb'))) { return false; } if (isset($http_response_header) && preg_match($regex, implode("\n", $http_response_header), $matches)) { return (int) $matches[0]; } return strlen(stream_get_contents($fp)); } // http://php.net/manual/fr/function.filesize.php#106569 function human_filesize($bytes, $decimals = 2, $base = 1000) { $sz = 'BKMGTP'; $factor = floor((strlen($bytes) - 1) / 3); return sprintf("%.{$decimals}f", $bytes / pow($base, $factor)) . @$sz[$factor]; } if (false !== ($size = remote_filesize($file))) { if ($human) { $size = human_filesize($size, $decimals, $base); } $server->success($size); } else { $server->error("Could not guess the file size"); } } else { $server->error("Invalid protocol"); } } else { $server->error("Invalid data, missing file"); } })->output();
function download_file() { global $wpdb, $user_ID; $dl_id = intval(get_query_var('dl_id')); $dl_name = addslashes(get_query_var('dl_name')); $download_options = get_option('download_options'); if ($dl_name == 'rss') { load_template(WP_PLUGIN_DIR . '/wp-downloadmanager/download-rss.php'); exit; } if ($dl_id > 0 || !empty($dl_name)) { if ($dl_id > 0 && $download_options['use_filename'] == 0) { $file = $wpdb->get_row("SELECT file_id, file, file_permission FROM {$wpdb->downloads} WHERE file_id = {$dl_id} AND file_permission != -2"); } elseif (!empty($dl_name) && $download_options['use_filename'] == 1) { if (!is_remote_file($dl_name)) { $dl_name = '/' . $dl_name; } $file = $wpdb->get_row("SELECT file_id, file, file_permission FROM {$wpdb->downloads} WHERE file = \"{$dl_name}\" AND file_permission != -2"); } if (!$file) { header('HTTP/1.0 404 Not Found'); die(__('Invalid File ID or File Name.', 'wp-downloadmanager')); } $file_path = stripslashes(get_option('download_path')); $file_url = stripslashes(get_option('download_path_url')); $download_method = intval(get_option('download_method')); $file_id = intval($file->file_id); $file_name = stripslashes($file->file); $file_permission = intval($file->file_permission); $current_user = wp_get_current_user(); if ($file_permission > 0 && intval($current_user->wp_user_level) >= $file_permission && intval($user_ID) > 0 || $file_permission == 0 && intval($user_ID) > 0 || $file_permission == -1) { $update_hits = $wpdb->query("UPDATE {$wpdb->downloads} SET file_hits = (file_hits + 1), file_last_downloaded_date = '" . current_time('timestamp') . "' WHERE file_id = {$file_id} AND file_permission != -2"); if (!is_remote_file($file_name)) { if (!is_file($file_path . $file_name)) { header('HTTP/1.0 404 Not Found'); die(__('File does not exist.', 'wp-downloadmanager')); } if ($download_method == 0) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Disposition: attachment; filename=" . basename($file_name) . ";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . filesize($file_path . $file_name)); @readfile($file_path . $file_name); } else { header('Location: ' . $file_url . $file_name); } exit; } else { if (ini_get('allow_url_fopen') && $download_method == 0) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Disposition: attachment; filename=" . basename($file_name) . ";"); header("Content-Transfer-Encoding: binary"); $file_size = remote_filesize($file_name); if ($file_size != __('unknown', 'wp-downloadmanager')) { header("Content-Length: " . $file_size); } @readfile($file_name); } else { header('Location: ' . $file_name); } exit; } } else { _e('You do not have permission to download this file.', 'wp-downloadmanager'); exit; } } }
// Close handle curl_close($ch); } function remote_filesize($url) { $url = str_replace(" ", "%20", $url); static $regex = '/^Content-Length: *+\\K\\d++$/im'; if (!($fp = @fopen($url, 'rb'))) { return false; } if (isset($http_response_header) && preg_match($regex, implode("\n", $http_response_header), $matches)) { return (int) $matches[0]; } return strlen(stream_get_contents($fp)); } $result = "<table><tr><th>Set</th><th>Name</th><th>Url</th><th>File size</th><tr>"; foreach ($ckan->datasets as $set) { foreach ($set->res_url as $resource) { $check_against1 = strtolower("http://www.amsterdamopendata.nl/files/"); $check_against2 = strtolower("http://www.amsterdamopendata.nl/documents/"); if (substr(strtolower($resource), 0, strlen($check_against1)) == $check_against1 || substr(strtolower($resource), 0, strlen($check_against2)) == $check_against2) { //print_r(stat($resource)); //$fsize = filesize($resource); //getFileSize($resource); $fsize = remote_filesize($resource); $result .= "<tr><td>" . $set->title . "</td><td>" . $set->name . "</td><td>" . $resource . "</td><td>" . $fsize . "<td></tr>\n\r"; } } } $result .= "</table>"; print $result;
$file = $file_upload_to . basename($_FILES['file_upload']['name']); $file = download_rename_file($file_path, $file); $file_size = filesize($file_path . $file); } else { $text = '<p style="color: red;">' . __('Error In Uploading File', 'wp-downloadmanager') . '</p>'; break; } } else { $text = '<p style="color: red;">' . __('Error In Uploading File', 'wp-downloadmanager') . '</p>'; break; } } break; case 2: $file = !empty($_POST['file_remote']) ? addslashes(trim($_POST['file_remote'])) : ''; $file_size = remote_filesize($file); break; } if ($file_type > -1) { $file_sql = "file = '{$file}',"; if (empty($file_name)) { $file_name = basename($file); } } $file_des = !empty($_POST['file_des']) ? addslashes(trim($_POST['file_des'])) : ''; $file_category = !empty($_POST['file_cat']) ? intval($_POST['file_cat']) : 0; $file_hits = !empty($_POST['file_hits']) ? intval($_POST['file_hits']) : 0; $edit_filetimestamp = !empty($_POST['edit_filetimestamp']) ? intval($_POST['edit_filetimestamp']) : 0; if (intval($_POST['auto_filesize']) == 0) { $file_size = !empty($_POST['file_size']) ? intval($_POST['file_size']) : 0; }
function get_size() { global $wpdb, $wp_dlm_db_meta; $thefile = $this->filename; $urlparsed = parse_url($thefile); $isURI = array_key_exists('scheme', $urlparsed); $localURI = (bool) strstr($thefile, get_bloginfo('wpurl')); /* Local TO WORDPRESS!! */ $filesize = ''; if ($isURI && $localURI || !$isURI && !$localURI) { if ($localURI) { // the URI is local, replace the WordPress url OR blog url with WordPress's absolute path. //$patterns = array( '|^'. get_bloginfo('wpurl') . '/' . '|', '|^'. get_bloginfo('url') . '/' . '|'); $patterns = array('|^' . get_bloginfo('wpurl') . '/' . '|'); $path = preg_replace($patterns, '', $thefile); // this is joining the ABSPATH constant, changing any slashes to local filesystem slashes, and then finally getting the real path. $thefile = str_replace('/', DIRECTORY_SEPARATOR, path_join(ABSPATH, $path)); if (@file_exists($thefile)) { $filesize = filesize($thefile); } // Local File System path } elseif (!path_is_absolute($thefile)) { //$thefile = path_join( ABSPATH, $thefile ); // Get the absolute path if (!isset($_SERVER['DOCUMENT_ROOT'])) { $_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0 - strlen($_SERVER['PHP_SELF']))); } $dir_path = $_SERVER['DOCUMENT_ROOT']; // Now substitute the domain for the absolute path in the file url $thefile = str_replace('/', DIRECTORY_SEPARATOR, path_join($dir_path, $thefile)); if (@file_exists($thefile)) { $filesize = filesize($thefile); } } else { if (@file_exists($thefile)) { $filesize = filesize($thefile); } } } elseif ($isURI && ini_get('allow_url_fopen')) { // Absolute path outside of wordpress if (!function_exists('remote_filesize')) { function remote_filesize($url) { ob_start(); $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); $ok = curl_exec($ch); curl_close($ch); $head = ob_get_contents(); ob_end_clean(); $regex = '/Content-Length:\\s([0-9].+?)\\s/'; $count = preg_match($regex, $head, $matches); return isset($matches[1]) ? $matches[1] : ""; } } $isHTTP = (bool) ($urlparsed['scheme'] == 'http' || $urlparsed['scheme'] == 'https'); if (function_exists('get_headers') && $isHTTP) { $ary_header = @get_headers($thefile, 1); if (is_array($ary_header) && array_key_exists("Content-Length", $ary_header)) { $filesize = $ary_header["Content-Length"]; } } else { if (function_exists('curl_init')) { $filesize = remote_filesize($thefile); // I wonder, is this returning something non-numeric? } else { $filesize = @filesize($thefile); } } } if ($filesize && is_numeric($filesize)) { $bytes = array('bytes', 'kB', 'MB', 'GB', 'TB'); foreach ($bytes as $val) { if ($filesize > 1024) { $filesize = $filesize / 1024; } else { break; } } $this->size = round($filesize, 2) . " " . $val; // Add to DB for quick loading in future $wpdb->query("INSERT INTO {$wp_dlm_db_meta} (meta_name, meta_value, download_id) VALUES ('filesize', '" . $wpdb->escape($this->size) . "', '" . $this->id . "')"); } else { // Could not get size, but insert anyway to prevent slow page loads $wpdb->query("INSERT INTO {$wp_dlm_db_meta} (meta_name, meta_value, download_id) VALUES ('filesize', '', '" . $this->id . "')"); } }