| 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_Attachments { |
| 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_file($value, $extension = null) { |
| 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->info('key') == '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->info('key') == '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 |
public function get_attachments($list, $extension = null) { |
| 97 |
$attachments = array(); |
| 98 |
if ($extension) { |
| 99 |
$value = array_filter(array_map('trim', preg_split('/[\n\,]+/', $list, 0, PREG_SPLIT_NO_EMPTY))); |
| 100 |
foreach ($value as $shortcode) { |
| 101 |
$files = array_filter(array_map('trim', preg_split('/[\n\,]+/', $extension->render($shortcode, array()), 0, PREG_SPLIT_NO_EMPTY))); |
| 102 |
foreach ($files as $file) { |
| 103 |
$source = $this->get_file(trim($file), $extension); |
| 104 |
if ($source) { |
| 105 |
$attachments[] = array( |
| 106 |
'value' => $source, |
| 107 |
'name' => $this->get_attachment_name($file, $extension, $shortcode), |
| 108 |
'description' => $this->get_attachment_description($file, $extension, $shortcode), |
| 109 |
); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
return $attachments; |
| 115 |
} |
| 116 |
|
| 117 |
public function get_attachment_name($file, $extension = null, $shortcode = '') { |
| 118 |
$name = basename(trim($file)); |
| 119 |
if (false !== strpos($name, 'index.php?gf-download')) { |
| 120 |
$parse_url = wp_parse_args(wp_parse_url($name, PHP_URL_QUERY)); |
| 121 |
if (!empty($parse_url['gf-download'])) { |
| 122 |
$name = basename($parse_url['gf-download']); |
| 123 |
} |
| 124 |
} |
| 125 |
return apply_filters('e2pdf_attachments_attachment_name', $name, $extension, $file, trim($shortcode)); |
| 126 |
} |
| 127 |
|
| 128 |
public function get_attachment_description($file, $extension = null, $shortcode = '') { |
| 129 |
return apply_filters('e2pdf_attachments_attachment_description', '', $extension, $file, trim($shortcode)); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get pdf by Url |
| 134 |
* @param string $url - Url to pdf |
| 135 |
* @return array(); |
| 136 |
*/ |
| 137 |
public function get_by_url($url) { |
| 138 |
$response = wp_remote_get( |
| 139 |
$url, |
| 140 |
array( |
| 141 |
'timeout' => get_option('e2pdf_images_timeout', '30'), |
| 142 |
'sslverify' => false, |
| 143 |
) |
| 144 |
); |
| 145 |
if (wp_remote_retrieve_response_code($response) === 200) { |
| 146 |
return wp_remote_retrieve_body($response); |
| 147 |
} else { |
| 148 |
return ''; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
public function get_allowed_extensions() { |
| 153 |
return apply_filters( |
| 154 |
'e2pdf_pdf_allowed_attachments_extensions', |
| 155 |
array( |
| 156 |
'image/jpeg' => 'jpg', |
| 157 |
'image/jpeg2' => 'jpeg', |
| 158 |
'image/png' => 'png', |
| 159 |
'image/gif' => 'gif', |
| 160 |
'image/svg' => 'svg', |
| 161 |
'image/svg+xml' => 'svg', |
| 162 |
'image/webp' => 'webp', |
| 163 |
'image/x-ms-bmp' => 'bmp', |
| 164 |
'image/bmp' => 'bmp', |
| 165 |
'image/tif' => 'tiff', |
| 166 |
'image/tiff' => 'tiff', |
| 167 |
'application/pdf' => 'pdf', |
| 168 |
'text/csv' => 'csv', |
| 169 |
'application/msword' => 'doc', |
| 170 |
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx', |
| 171 |
'application/vnd.ms-excel' => '.xls', |
| 172 |
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => '.xlsx', |
| 173 |
'application/vnd.oasis.opendocument.text' => 'odt', |
| 174 |
'application/vnd.oasis.opendocument.spreadsheet' => 'ods', |
| 175 |
'application/vnd.oasis.opendocument.presentation' => '.odp', |
| 176 |
) |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
public function get_extension($value = false) { |
| 181 |
|
| 182 |
$value = trim($value); |
| 183 |
if (!$value) { |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
$extensions = $this->get_allowed_extensions(); |
| 188 |
$extension = false; |
| 189 |
$mime = false; |
| 190 |
|
| 191 |
if (@file_exists($value)) { |
| 192 |
$file_extension = strtolower(pathinfo($value, PATHINFO_EXTENSION)); |
| 193 |
if (in_array($file_extension, $extensions)) { |
| 194 |
return $file_extension; |
| 195 |
} elseif (function_exists('finfo_open') && function_exists('finfo_file')) { |
| 196 |
$f = finfo_open(); |
| 197 |
$mime = @finfo_file($f, $value, FILEINFO_MIME_TYPE); |
| 198 |
} |
| 199 |
} else { |
| 200 |
$file_extension = strtolower(pathinfo($value, PATHINFO_EXTENSION)); |
| 201 |
if (in_array($file_extension, $extensions)) { |
| 202 |
return $file_extension; |
| 203 |
} elseif (function_exists('finfo_open') && function_exists('finfo_buffer')) { |
| 204 |
$f = finfo_open(); |
| 205 |
$mime = finfo_buffer($f, $value, FILEINFO_MIME_TYPE); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
if ($mime && isset($extensions[$mime])) { |
| 210 |
$extension = $extensions[$mime]; |
| 211 |
} |
| 212 |
return $extension; |
| 213 |
} |
| 214 |
} |
| 215 |
|