0) {
\WPDM_Messages::error("Please, no funny business, however, good try though!", 1);
}
if (wpdm_is_url($filepath)) {
header("location: " . $filepath);
die();
}
if (WPDM()->fileSystem->isBlocked($filepath)) \WPDM_Messages::error("Invalid File Type ({$filename})!", 1);
$content_type = function_exists('mime_content_type') ? mime_content_type($filepath) : self::mime_type($filepath);
$buffer = $speed ? $speed : 1024; // bytes
$buffer *= 1024; // to bits
$bandwidth = 0;
if (function_exists('ini_set'))
@ini_set('display_errors', 0);
@session_write_close();
if (function_exists('apache_setenv'))
@apache_setenv('no-gzip', 1);
if (function_exists('ini_set'))
@ini_set('zlib.output_compression', 'Off');
@set_time_limit(0);
@session_cache_limiter('none');
if (get_option('__wpdm_support_output_buffer', 1) == 1) {
$pcl = ob_get_level();
do {
@ob_end_clean();
if (ob_get_level() == $pcl) break;
$pcl = ob_get_level();
} while (ob_get_level() > 0);
}
if (strpos($filepath, '://')) {
header("location: {$filepath}");
die();
}
if (file_exists($filepath))
$fsize = filesize($filepath);
else
$fsize = 0;
$org_size = $fsize;
//wpdmdd($fsize);
nocache_headers();
header("X-Robots-Tag: noindex, nofollow", true);
header("Robots: none");
header('Content-Description: File Transfer');
if (strpos($_SERVER['HTTP_USER_AGENT'], "Safari") && !isset($extras['play']) && !get_option('__wpdm_open_in_browser', 0))
$content_type = "application/octet-stream";
header("Content-type: $content_type");
$filename = rawurlencode($filename);
if (!isset($extras['play'])) {
if (get_option('__wpdm_open_in_browser', 0) || wpdm_query_var('open') == 1)
header("Content-disposition: inline;filename=\"{$filename}\"");
else
header("Content-disposition: attachment;filename=\"{$filename}\"");
header("Content-Transfer-Encoding: binary");
}
if ((isset($extras['play']) && strpos($_SERVER['HTTP_USER_AGENT'], "Safari")) || get_option('__wpdm_download_resume', 1) == 2) {
header("Content-Length: " . $fsize);
header("Content-disposition: attachment;filename=\"{$filename}\"");
\WPDM\TempStorage::set("download." . wpdm_get_client_ip(), 1, 60);
readfile($filepath);
return;
}
$file = @fopen($filepath, "rb");
//check if http_range is sent by browser (or download manager)
if (isset($_SERVER['HTTP_RANGE']) && $fsize > 0) {
list($bytes, $http_range) = explode("=", $_SERVER['HTTP_RANGE']);
$tmp = explode('-', $http_range);
$tmp = array_shift($tmp);
$set_pointer = intval($tmp);
$new_length = $fsize - $set_pointer;
header("Accept-Ranges: bytes");
header("Accept-Ranges: 0-$fsize");
$proto = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
header("{$proto} 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: bytes $http_range-$fsize/$org_size");
fseek($file, $set_pointer);
} else {
header("Content-Length: " . $fsize);
}
$packet = 1;
if ($file) {
while (!(connection_aborted() || connection_status() == 1) && $fsize > 0) {
$parallel_download = (int)get_option('__wpdm_parallel_download', 1);
if ($parallel_download === 0)
\WPDM\TempStorage::set("download." . wpdm_get_client_ip(), 1, 15);
if ($fsize > $buffer)
echo fread($file, $buffer);
else
echo fread($file, $fsize);
if (function_exists('ob_get_level') && ob_get_level() > 0) @ob_flush();
@flush();
$fsize -= $buffer;
$bandwidth += $buffer;
if ($speed > 0 && ($bandwidth > $speed * $packet * 1024)) {
sleep(1);
$packet++;
}
}
$package['downloaded_file_size'] = $fsize;
//add_action('wpdm_download_completed', $package);
@fclose($file);
}
return;
}
/**
* @usage Download any content as a file
* @param $filename
* @param $content
*/
public static function downloadData($filename, $content)
{
@ob_end_clean();
nocache_headers();
$filetype = wp_check_filetype($filename);
header("X-Robots-Tag: noindex, nofollow", true);
header("Robots: none");
header("Content-Description: File Transfer");
header("Content-Type: {$filetype['type']}");
header("Content-disposition: attachment;filename=\"$filename\"");
header("Content-Transfer-Encoding: Binary");
header("Content-Length: " . strlen($content));
echo $content;
}
/**
* @usage Sends download headers only
* @param $filename
* @param int $size
*/
public static function downloadHeaders($filename, $size = null)
{
@ob_end_clean();
$filetype = wp_check_filetype($filename);
header("Content-Description: File Transfer");
header("Content-Type: {$filetype['type']}");
header("Content-disposition: attachment;filename=\"$filename\"");
header("Content-Transfer-Encoding: Binary");
if ($size)
header("Content-Length: " . $size);
}
/**
* @usage Download any content as a file
* @param $filename
* @param $content
*/
public static function mkDir($path, $mode = 0777, $recur = false)
{
$success = true;
if (!file_exists($path))
$success = mkdir($path, $mode, $recur);
return $success;
}
/**
* @usage Create ZIP from given file list
* @param $files
* @param $zipname
* @return bool|string
*/
public static function zipFiles($files, $zipname)
{
$zipped = (basename($zipname) === $zipname) ? WPDM_CACHE_DIR . sanitize_file_name($zipname) : $zipname;
if (substr_count($zipname, '.zip') > 0) $zipped .= '.zip';
if (file_exists($zipped))
unlink($zipped);
if (count($files) < 1) return false;
$zip = new \ZipArchive();
if ($zip->open($zipped, \ZIPARCHIVE::CREATE) !== TRUE) {
return false;
}
foreach ($files as $file) {
$file = trim($file);
if (file_exists(UPLOAD_DIR . $file)) {
$fnm = preg_replace("/^[0-9]+?wpdm_/", "", $file);
$zip->addFile(UPLOAD_DIR . $file, $fnm);
} else if (file_exists($file)) {
$fname = basename($file);
$zip->addFile($file, $fname);
}
//else if (file_exists(WP_CONTENT_DIR . end($tmp = explode("wp-content", $file)))) //path fix on site move
// $zip->addFile(WP_CONTENT_DIR . end($tmp = explode("wp-content", $file)), wpdm_basename($file));
}
$zip->close();
return $zipped;
}
/**
* @usage Create ZIP from given dir path
* @param $files
* @param $zipname
* @return bool|string
*/
public static function zipDir($dir, $zipname = '')
{
if ($zipname === '') $zipname = basename($dir);
$zipped = WPDM_CACHE_DIR . sanitize_file_name($zipname) . '.zip';
$rootPath = realpath($dir);
$zip = new \ZipArchive();
$zip->open($zipped, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($rootPath),
\RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
return $zipped;
}
/**
* @param $dir
* @param bool|true $recur
* @return array
*/
public static function scanDir($dir, $recur = true, $abspath = true, $filter = null)
{
$dir = realpath($dir) . "/";
if ($dir === '/' || $dir === '') return array();
$tmpfiles = file_exists($dir) ? array_diff(scandir($dir), array(".", "..", ".DS_Store")) : array();
$files = array();
foreach ($tmpfiles as $file) {
if (is_dir($dir . $file) && $recur == true)
$files = array_merge($files, self::scanDir($dir . $file, true));
else {
if (!$filter || substr_count($file, $filter) > 0)
$files[] = $abspath ? $dir . $file : $file;
}
}
return $files;
}
/**
* @param $dir
* @param bool|true $recur
* @return array
*/
public static function listFiles($dir, $recur = true, $abspath = true)
{
$dir = realpath($dir) . "/";
if ($dir == '/' || $dir == '') return array();
$tmpfiles = file_exists($dir) ? array_diff(scandir($dir), array(".", "..")) : array();
$files = array();
foreach ($tmpfiles as $file) {
if (is_dir($dir . $file) && $recur == true) $files = array_merge($files, self::scanDir($dir . $file, true));
else if (!is_dir($dir . $file))
$files[] = $abspath ? $dir . $file : $file;
}
return $files;
}
/**
* @param $dir
* @param bool|true $recur
* @return array
*/
public static function subDirs($dir, $abspath = true)
{
$dir = realpath($dir) . "/";
if ($dir == '/' || $dir == '') return array();
$tmpfiles = file_exists($dir) ? array_diff(scandir($dir), array(".", "..")) : array();
$subdirs = array();
foreach ($tmpfiles as $file) {
if (is_dir($dir . $file)) $subdirs[] = $abspath ? $dir . $file : $file;
}
return $subdirs;
}
/**
* @param $dir
* @param bool|true $recur
* @return array|bool
*/
public static function deleteFiles($dir, $recur = true, $filter = '*')
{
$dir = realpath($dir) . "/";
if ($dir == '/' || $dir == '') return array();
$tmpfiles = file_exists($dir) ? array_diff(scandir($dir), array(".", "..")) : array();
$files = array();
foreach ($tmpfiles as $file) {
if (is_dir($dir . $file) && $recur == true) $files = array_merge($files, self::scanDir($dir . $file, true));
else {
if(is_array($filter)){
$ext = isset($filter['ext']) ? $filter['ext'] : '*';
$expiretime = isset($filter['filetime']) ? $filter['filetime'] : null;
$delete = true;
$filetime = filectime($dir.$file);
if(!$filetime || !$expiretime || $filetime < $expiretime) {
if ($ext === '*' || substr_count($file, $ext) > 0) {
@unlink($dir . $file);
}
}
} else {
if ($filter === '*' || substr_count($file, $filter) > 0)
@unlink($dir . $file);
}
}
}
return true;
}
/**
* @param $src
* @param $dst
*/
public static function copyDir($src, $dst)
{
$src = realpath($src);
$dir = opendir($src);
$dst = realpath($dst) . '/' . basename($src);
@mkdir($dst);
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
if (is_dir($src . '/' . $file)) {
self::copyDir($src . '/' . $file, $dst . '/' . $file);
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
closedir($dir);
}
public static function imageThumbnail($path, $width, $height, $crop = WPDM_USE_GLOBAL, $usecache = true)
{
$opath = $path;
$abspath = str_replace("\\", "/", ABSPATH);
$cachedir = str_replace("\\", "/", WPDM_CACHE_DIR);
$path = str_replace("\\", "/", $path);
if (is_ssl()) $path = str_replace("http://", "https://", $path);
else $path = str_replace("https://", "http://", $path);
$path = str_replace(site_url('/'), $abspath, $path);
$crop = $crop === WPDM_USE_GLOBAL ? get_option('__wpdm_crop_thumbs', false) : $crop;
if (strpos($path, '.wp.com')) {
$path = explode("?", $path);
$path = $path[0] . "?resize={$width},{$height}";
return $path;
}
if (strpos($path, '://')) return $path;
if (!file_exists($path)) return WPDM_BASE_URL . 'assets/images/404.jpg';
//if (!file_exists($path) && strstr($opath, "://")) return $opath;
$name_p = explode(".", $path);
$ext = "." . end($name_p);
$filename = basename($path);
$thumbpath = $cachedir . str_replace($ext, "-{$width}x{$height}" . $ext, $filename);
if (file_exists($thumbpath) && $usecache) {
$thumbpath = str_replace($abspath, site_url('/'), $thumbpath);
return $thumbpath;
}
$image = wp_get_image_editor($path);
$fullurl = str_replace(ABSPATH, home_url('/'), $path);
if (!is_wp_error($image)) {
//if ( is_wp_error( $image->resize( $width, $height, true ) ) ) return $fullurl;
$image->resize($width, $height, $crop);
$image->save($thumbpath);
} else
return $path;
$thumb_size = $image->get_size();
if ($thumb_size['width'] < $width || $thumb_size['height'] < $height) {
if ($height == 0) $height = $thumb_size['height'];
$_image_back = imagecreatetruecolor($width, $height);
$color = imagecolorallocatealpha($_image_back, 255, 255, 255, 127);
imagefill($_image_back, 0, 0, $color);
if (strstr($thumbpath, ".png"))
$_image_top = imagecreatefrompng($thumbpath);
if (strstr($thumbpath, ".gif"))
$_image_top = imagecreatefromgif($thumbpath);
if (strstr($thumbpath, ".jpg") || strstr($thumbpath, ".jpeg"))
$_image_top = imagecreatefromjpeg($thumbpath);
if (!isset($_image_top) || !$_image_top) return $thumbpath;
$imgw = imagesx($_image_top);
$imgh = imagesy($_image_top);
$posx = (int)(($width - $imgw) / 2);
$posy = (int)(($height - $imgh) / 2);
imagecopy($_image_back, $_image_top, $posx, $posy, 0, 0, $imgw, $imgh);
imagepng($_image_back, $thumbpath);
imagedestroy($_image_back);
}
$thumbpath = str_replace("\\", "/", $thumbpath);
$thumbpath = str_replace($abspath, site_url('/'), $thumbpath);
return $thumbpath;
}
/**
* @param $pdf
* @param $id
* @return string
* @usage Generates thumbnail from PDF file. [ From v4.1.3 ]
*/
public static function pdfThumbnail($pdf, $id)
{
$pdfurl = '';
if (strpos($pdf, "://")) {
$pdfurl = $pdf;
$pdf = str_replace(home_url('/'), ABSPATH, $pdf);
}
if ($pdf == $pdfurl) return '';
if (file_exists($pdf)) $source = $pdf;
else $source = UPLOAD_DIR . $pdf;
if (!file_exists(WPDM_CACHE_DIR . "pdfthumbs/")) {
@mkdir(WPDM_CACHE_DIR . "pdfthumbs/", 0755);
@chmod(WPDM_CACHE_DIR . "pdfthumbs/", 0755);
}
$dest = WPDM_CACHE_DIR . "pdfthumbs/{$id}.png";
$durl = WPDM_CACHE_URL . "pdfthumbs/{$id}.png";
$ext = explode(".", $source);
$ext = end($ext);
$colors = WPDM()->setting->ui_colors;
$color = is_array($colors['secondary']) && isset($colors['secondary']) ? str_replace("#", "", $colors['secondary']) : '6c757d';
if ($ext != 'pdf') return '';
if (file_exists($dest)) return $durl;
if (!file_exists($source))
$source = utf8_encode($source);
$source = $source . '[0]';
if (!class_exists('Imagick')) return "https://via.placeholder.com/600x900/{$color}/FFFFFF?text=[+Imagick+Missing+]"; // "Error: Imagick is not installed properly";
try {
$image = new \imagick($source);
$image->setResolution(800, 0);
$image->setImageFormat("png");
$image->writeImage($dest);
} catch (\Exception $e) {
return "https://via.placeholder.com/600x900/{$color}/FFFFFF?text=+";
//return '';
}
return $durl;
}
/**
* @usgae Block http access to a dir
* @param $dir
*/
public static function blockHTTPAccess($dir, $fileType = '*')
{
$cont = "RewriteEngine On\r\n
Returns absolute path if file is found, returns false if file is not found
* @param $rel_path * @param null $pid * @return bool|string */ function absPath($rel_path, $pid = null){ $abs_path = false; if(wpdm_is_url($rel_path)) return $rel_path; if(substr_count($rel_path, './')) return false; $fixed_abs_path = false; if(substr_count($rel_path, 'wp-content') > 0 && substr_count($rel_path, WP_CONTENT_DIR) === 0) { $rel_rel_path = explode("wp-content", $rel_path); $rel_rel_path = end($rel_rel_path); $fixed_abs_path = WP_CONTENT_DIR . $rel_rel_path; } $file_browser_root = get_option('_wpdm_file_browser_root', ''); if(file_exists($rel_path)) $abs_path = $rel_path; else if(file_exists(UPLOAD_DIR.$rel_path)) $abs_path = UPLOAD_DIR.$rel_path; else if(file_exists(ABSPATH.$rel_path)) $abs_path = ABSPATH.$rel_path; else if(file_exists($file_browser_root.$rel_path)) $abs_path = $file_browser_root.$rel_path; else if($fixed_abs_path && file_exists($fixed_abs_path)) $abs_path = $fixed_abs_path; else if($pid){ $user_upload_dir = null; $package = get_post($pid); if(is_object($package)){ $author = get_user_by('id', $package->post_author); if($author) $user_upload_dir = UPLOAD_DIR . $author->user_login . '/'; } if($user_upload_dir && file_exists($user_upload_dir.$rel_path)) $abs_path = $user_upload_dir.$rel_path; } $abs_path = str_replace('\\','/', $abs_path ); return realpath($abs_path); } }