| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2Pdf Pdf Helper |
| 5 |
* @copyright Copyright 2017 https://e2pdf.com |
| 6 |
* @license GPLv3 |
| 7 |
* @version 1 |
| 8 |
* @link https://e2pdf.com |
| 9 |
* @since 0.00.01 |
| 10 |
*/ |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
die('Access denied.'); |
| 13 |
} |
| 14 |
|
| 15 |
class Helper_E2pdf_Pdf { |
| 16 |
|
| 17 |
private $helper; |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Get Base64 Encoded Pdf |
| 25 |
* @param string $value - Pdf path |
| 26 |
* @return mixed - Base64 encoded Pdf OR FALSE |
| 27 |
*/ |
| 28 |
public function get_pdf($value, $extension = false) { |
| 29 |
$source = false; |
| 30 |
$protected = false; |
| 31 |
if ($value) { |
| 32 |
$value = trim($value); |
| 33 |
$site_url = site_url('/'); |
| 34 |
$https = str_replace('http://', 'https://', $site_url); |
| 35 |
$http = str_replace('https://', 'http://', $site_url); |
| 36 |
if (!get_option('e2pdf_images_remote_request', '0')) { |
| 37 |
if (0 === strpos($value, $https)) { |
| 38 |
$tmp_value = ABSPATH . substr($value, strlen($https)); |
| 39 |
if (@file_exists($tmp_value)) { |
| 40 |
$value = $tmp_value; |
| 41 |
} |
| 42 |
} elseif (0 === strpos($value, $http)) { |
| 43 |
$tmp_value = ABSPATH . substr($value, strlen($http)); |
| 44 |
if (@file_exists($tmp_value)) { |
| 45 |
$value = $tmp_value; |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
if (!$source) { |
| 50 |
if ((0 === strpos($value, ABSPATH) || 0 === strpos($value, '/')) && @file_exists($value) && $this->get_extension($value)) { |
| 51 |
if ($extension == 'formidable' && class_exists('FrmProFileField') && !@is_readable($value)) { |
| 52 |
FrmProFileField::chmod($value, apply_filters('frm_protected_file_readonly_permission', 0400)); |
| 53 |
if (@!is_readable($value)) { |
| 54 |
@chmod($value, apply_filters('frm_protected_file_readonly_permission', 0400)); |
| 55 |
} |
| 56 |
if (@is_readable($value)) { |
| 57 |
$protected = true; |
| 58 |
} |
| 59 |
} |
| 60 |
$contents = @file_get_contents($value); |
| 61 |
if ($contents) { |
| 62 |
$source = base64_encode($contents); |
| 63 |
} |
| 64 |
if ($protected) { |
| 65 |
FrmProFileField::chmod($value, 0200); |
| 66 |
} |
| 67 |
} elseif (@file_exists(ABSPATH . $value) && $this->get_extension(ABSPATH . $value)) { |
| 68 |
if ($extension == 'formidable' && class_exists('FrmProFileField') && !@is_readable(ABSPATH . $value)) { |
| 69 |
FrmProFileField::chmod(ABSPATH . $value, apply_filters('frm_protected_file_readonly_permission', 0400)); |
| 70 |
if (@!is_readable(ABSPATH . $value)) { |
| 71 |
@chmod(ABSPATH . $value, apply_filters('frm_protected_file_readonly_permission', 0400)); |
| 72 |
} |
| 73 |
if (@is_readable(ABSPATH . $value)) { |
| 74 |
$protected = true; |
| 75 |
} |
| 76 |
} |
| 77 |
$contents = @file_get_contents(ABSPATH . $value); |
| 78 |
if ($contents) { |
| 79 |
$source = base64_encode($contents); |
| 80 |
} |
| 81 |
if ($protected) { |
| 82 |
FrmProFileField::chmod(ABSPATH . $value, 0200); |
| 83 |
} |
| 84 |
} elseif ($tmp_file = base64_decode($value, true)) { |
| 85 |
if ($this->get_extension($tmp_file)) { |
| 86 |
$source = $value; |
| 87 |
} |
| 88 |
} elseif ($body = $this->get_by_url($value)) { |
| 89 |
$source = base64_encode($body); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
return $source; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get pdf by Url |
| 98 |
* @param string $url - Url to pdf |
| 99 |
* @return array(); |
| 100 |
*/ |
| 101 |
public function get_by_url($url) { |
| 102 |
$response = wp_remote_get( |
| 103 |
$url, |
| 104 |
array( |
| 105 |
'timeout' => get_option('e2pdf_images_timeout', '30'), |
| 106 |
'sslverify' => false, |
| 107 |
) |
| 108 |
); |
| 109 |
if (wp_remote_retrieve_response_code($response) === 200) { |
| 110 |
return wp_remote_retrieve_body($response); |
| 111 |
} else { |
| 112 |
return ''; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
public function get_allowed_extensions() { |
| 117 |
return array( |
| 118 |
'application/pdf' => 'pdf' |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
public function get_extension($value = false) { |
| 123 |
|
| 124 |
$value = trim($value); |
| 125 |
if (!$value) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
$extensions = $this->get_allowed_extensions(); |
| 130 |
$extension = false; |
| 131 |
$mime = false; |
| 132 |
|
| 133 |
if (@file_exists($value)) { |
| 134 |
$file_extension = strtolower(pathinfo($value, PATHINFO_EXTENSION)); |
| 135 |
if (in_array($file_extension, $extensions)) { |
| 136 |
return $file_extension; |
| 137 |
} elseif (function_exists('finfo_open') && function_exists('finfo_file')) { |
| 138 |
$f = finfo_open(); |
| 139 |
$mime = @finfo_file($f, $value, FILEINFO_MIME_TYPE); |
| 140 |
} |
| 141 |
} else { |
| 142 |
$file_extension = strtolower(pathinfo($value, PATHINFO_EXTENSION)); |
| 143 |
if (in_array($file_extension, $extensions)) { |
| 144 |
return $file_extension; |
| 145 |
} elseif (function_exists('finfo_open') && function_exists('finfo_buffer')) { |
| 146 |
$f = finfo_open(); |
| 147 |
$mime = finfo_buffer($f, $value, FILEINFO_MIME_TYPE); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if ($mime && isset($extensions[$mime])) { |
| 152 |
$extension = $extensions[$mime]; |
| 153 |
} |
| 154 |
return $extension; |
| 155 |
} |
| 156 |
} |
| 157 |
|