PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.53
E2Pdf – Export Pdf Tool for WordPress v1.32.53
1.32.53 1.32.51 1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 All 74 releases
← All changes | classes/helper/e2pdf-view.php +240 -180 1.07.11 → 1.32.53 View file →
@@ -1,14 +1,12 @@
1 1 <?php
2 2
3 3 /**
4 - * E2pdf View Helper
5 - *
6 - * @copyright Copyright 2017 https://e2pdf.com
7 - * @license GPL v2
8 - * @version 1
9 - * @link https://e2pdf.com
10 - * @since 0.00.01
4 + * File: /helper/e2pdf-view.php
5 + *
6 + * @package E2Pdf
7 + * @license GPLv3
8 + * @link https://e2pdf.com
11 9 */
12 10 if (!defined('ABSPATH')) {
13 11 die('Access denied.');
14 12 }
@@ -38,137 +36,122 @@
38 36 }
39 37
40 38 public function __construct() {
41 39 $this->view = new stdClass();
42 - $this->uri = home_url(add_query_arg(null, null));
40 + $this->uri = home_url(add_query_arg([], false));
43 41 $this->get = new Helper_E2pdf_Get($this->uri);
44 42 $this->post = new Helper_E2pdf_Post();
45 43 $this->files = new Helper_E2pdf_Files();
46 44 $this->page = $this->get->get_page();
47 45 $this->helper = Helper_E2pdf_Helper::instance();
48 - $this->notification = new Model_E2pdf_Notification();
46 + $this->notification = Model_E2pdf_Notification::instance();
49 47 $this->view_dir = $this->helper->get('plugin_dir') . 'classes/view/';
50 48 }
51 49
52 - /**
53 - * Get Tpl Value
54 - *
55 - * @param string $key - Tpl key
56 - *
57 - * @return mixed - Tpl value
58 - */
50 + // render
59 51 public function render($type, $element, $args = array()) {
60 52 $this->tpl_args = new Helper_E2pdf_Tplargs($args);
61 53 $this->tpl = $this->view_dir . $type . '/' . $element . '.php';
62 54 if (file_exists($this->tpl)) {
63 - include($this->tpl);
55 + include $this->tpl;
64 56 }
65 57 }
66 58
67 - /**
68 - * Get Tpl Value
69 - *
70 - * @param string $key - Tpl key
71 - *
72 - * @return mixed - Tpl value
73 - */
59 + // render metabox
74 60 public function render_metabox($post, $metabox) {
75 61 $this->render('metaboxes', $metabox['args']['tpl']);
76 62 }
77 63
78 - /**
79 - * Get Tpl Value
80 - *
81 - * @param string $key - Tpl key
82 - *
83 - * @return mixed - Tpl value
84 - */
64 + // render page
85 65 public function render_page() {
86 -
87 - if (get_option('e2pdf_developer')) {
88 - $model_e2pdf_options = new Model_E2pdf_Options();
89 - if (in_array($this->helper->get_ip(), $model_e2pdf_options->get_option('e2pdf_developer_ips'))) {
90 - error_reporting(E_ALL);
91 - ini_set('display_startup_errors', 1);
92 - ini_set('display_errors', 1);
93 - }
94 - }
95 -
96 -
97 66 $this->tpl_page = $this->view_dir . 'page-' . $this->page . '.php';
98 67 $controller = $this->page_to_controller($this->page);
99 68 if (class_exists($controller)) {
100 69 $this->controller = new $controller();
70 + $method = 'index_action';
101 71 if ($this->get->get('action')) {
102 72 $method = $this->get->get('action') . '_action';
103 - } else {
104 - $method = 'index_action';
105 73 }
106 74 if (method_exists($this->controller, $method)) {
107 75 $this->controller->$method();
108 76 $this->view = $this->controller->view;
77 + if (file_exists($this->tpl_page)) {
78 + include $this->tpl_page;
79 + }
109 80 } else {
110 - $this->error('404');
81 + $this->handle_404();
111 82 }
112 83 }
113 -
114 - if (file_exists($this->tpl_page)) {
115 - include($this->tpl_page);
116 - }
117 84 }
118 85
86 + // render frontend page
119 87 public function render_frontend_page() {
120 - if ($this->page == 'e2pdf-download') {
121 - $this->tpl_page = $this->view_dir . '/frontend/page-' . $this->page . '.php';
122 - $controller = $this->page_to_controller($this->page, true);
88 + if ($this->page == 'e2pdf-download' || get_query_var('e2pdf')) {
89 + $this->tpl_page = $this->view_dir . '/frontend/page-e2pdf-download.php';
90 + $controller = $this->page_to_controller('e2pdf-download', true);
123 91 if (class_exists($controller)) {
124 92 $this->controller = new $controller();
93 + $method = 'index_action';
125 94 if ($this->get->get('action')) {
126 95 $method = $this->get->get('action') . '_action';
127 - } else {
128 - $method = 'index_action';
129 96 }
130 97 if (method_exists($this->controller, $method)) {
131 98 $this->controller->$method();
99 + if (file_exists($this->tpl_page)) {
100 + include $this->tpl_page;
101 + }
132 102 } else {
133 - $this->error('404');
103 + $this->handle_404();
134 104 }
135 105 }
106 + } elseif ($this->page == 'e2pdf-activation') {
107 + if (get_transient('e2pdf_activation_key')) {
108 + $activation_key = get_transient('e2pdf_activation_key');
109 + if ($this->get->get('key') == $activation_key) {
110 + if (ob_get_length() > 0) {
111 + while (@ob_end_clean());
112 + }
113 + header('Content-Type: text/plain');
114 + echo $activation_key;
115 + die();
116 + }
117 + }
118 + }
119 + }
136 120
137 - if (file_exists($this->tpl_page)) {
138 - include($this->tpl_page);
121 + // rpc
122 + public function rpc() {
123 + if (ob_get_length() > 0) {
124 + while (@ob_end_clean());
125 + }
126 + $rpc = new Model_E2pdf_Rpc($this->helper->load('server')->get('REQUEST_URI'));
127 + $controller = $this->page_to_controller('e2pdf-rpc-' . $rpc->get('version'), true);
128 + if (class_exists($controller)) {
129 + $this->controller = new $controller();
130 + $method = 'index_action';
131 + if ($rpc->get('method')) {
132 + $method = $rpc->get('method') . '_action';
139 133 }
134 + if (method_exists($this->controller, $method)) {
135 + $this->controller->$method($rpc);
136 + }
140 137 }
141 138 }
142 139
143 - /**
144 - * Get Tpl Value
145 - *
146 - * @param string $key - Tpl key
147 - *
148 - * @return mixed - Tpl value
149 - */
140 + // page to controller
150 141 public function page_to_controller($page, $frontend = false) {
151 -
152 142 $controller = array(
153 - 'Controller'
143 + 'Controller',
154 144 );
155 -
156 145 if ($frontend) {
157 146 $controller[] = 'Frontend';
158 147 }
159 -
160 148 $controller = array_merge($controller, explode('-', $page));
161 149 $controller = array_map('ucfirst', $controller);
162 -
163 150 return implode("_", $controller);
164 151 }
165 152
166 - /**
167 - * Redirect to page
168 - *
169 - * @param string $location - Full url where to redirect
170 - */
153 + // redirect
171 154 public function redirect($location) {
172 155 if (headers_sent()) {
173 156 die("<script>window.location='{$location}';</script>");
174 157 } else {
@@ -176,35 +159,19 @@
176 159 exit();
177 160 }
178 161 }
179 162
180 - /**
181 - * Add notification
182 - *
183 - * @param string $type - Type of notification error|update
184 - * @param string $text - Text of notification
185 - */
163 + // add notification
186 164 public function add_notification($type, $text) {
187 - $this->notification->add_notification($type, $text);
165 + return $this->notification->add_notification($type, $text);
188 166 }
189 167
190 - /**
191 - * Get notifications
192 - *
193 - * @return array - List of notifications
194 - */
195 - public function get_notifications() {
196 - $notifications = $this->notification->get_notifications();
197 - return $notifications;
168 + // get notifications
169 + public function get_notifications($notification_id = '') {
170 + return $this->notification->get_notifications($notification_id);
198 171 }
199 172
200 - /**
201 - * Force Json response
202 - *
203 - * @param array $data - Array of data
204 - *
205 - * @return json
206 - */
173 + // json response
207 174 public function json_response($data = array()) {
208 175 @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
209 176 if (function_exists('wp_json_encode')) {
210 177 echo wp_json_encode($data, JSON_FORCE_OBJECT);
@@ -213,11 +180,25 @@
213 180 }
214 181 wp_die();
215 182 }
216 183
217 - /**
218 - * Force close browser tab from PHP
219 - */
184 + // json response ajax
185 + public function json_response_ajax($data = array(), $status = 200) {
186 + if (ob_get_length() > 0) {
187 + while (@ob_end_clean());
188 + }
189 + status_header($status);
190 + header('X-Robots-Tag: noindex, nofollow');
191 + @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
192 + if (function_exists('wp_json_encode')) {
193 + echo wp_json_encode($data, JSON_FORCE_OBJECT);
194 + } else {
195 + echo json_encode($data, JSON_FORCE_OBJECT);
196 + }
197 + exit;
198 + }
199 +
200 + // close tab response
220 201 public function close_tab_response() {
221 202 echo "
222 203 <script>
223 204 self.close();
@@ -224,85 +205,187 @@
224 205 </script>
225 206 ";
226 207 }
227 208
228 - /**
229 - * Force Download response
230 - *
231 - * @param string $format - Format of file
232 - * @param string $file - Base64 Encoded File
233 - * @param string $name - Name of file when download
234 - * @param string $disposition - Disposition of file inline|attachment
235 - *
236 - * @return string
237 - */
238 - public function download_response($format = 'pdf', $file, $name = false, $disposition = false) {
239 - ob_end_clean();
209 + // download response
210 + public function download_response($format, $file, $name = '', $disposition = '', $fpassthru = false, $preview = false) {
240 211
212 + $content_length = true;
213 + /**
214 + * External Links – nofollow, noopener & new window compatibility fix filter
215 + * https://wordpress.org/plugins/wp-external-links/
216 + */
217 + add_filter('wpel_apply_settings', '__return_false');
218 +
219 + /**
220 + * Compatibility fix with TranslatePress
221 + * https://wordpress.org/plugins/translatepress-multilingual/
222 + */
223 + add_filter('trp_stop_translating_page', '__return_true');
224 +
225 + if (ob_get_length() > 0) {
226 + while (@ob_end_clean());
227 + }
228 +
229 + /**
230 + * PHP 8.0.17 & PHP 8.1.4 compatibility with zlib enabled
231 + */
232 + header_remove("Content-Encoding");
233 +
234 + /**
235 + * W3 Total Cache + OVH compatibility fix
236 + * https://wordpress.org/plugins/w3-total-cache/
237 + */
238 + if (function_exists('w3tc_config')) {
239 + $w3tc_config = w3tc_config();
240 + if ($w3tc_config->get_boolean('browsercache.enabled') && $w3tc_config->get_boolean('browsercache.html.compression') && function_exists('php_uname') && false !== stripos(@php_uname(), '.ovh.net')) {
241 + $content_length = false;
242 + }
243 + }
244 +
245 + /**
246 + * WP Rocket + OVH compatibility fix
247 + * https://wp-rocket.me/
248 + */
249 + if (function_exists('rocket_generate_config_file') && !function_exists('WP_Rocket\Helpers\htaccess\remove_gzip\flush_wp_rocket') && function_exists('php_uname') && false !== stripos(@php_uname(), '.ovh.net')) {
250 + $content_length = false;
251 + }
252 +
241 253 if (!$name) {
242 254 $name = time();
243 255 }
256 + $name = rawurlencode($this->helper->load('convert')->to_file_name($name));
244 257
245 - $name = $this->helper->load('convert')->to_file_name($name);
246 -
258 + status_header(200);
247 259 header('Content-Transfer-Encoding: binary');
248 - header('Content-Length: ' . strlen(base64_decode($file)));
260 + header('X-Robots-Tag: noindex, nofollow');
249 261 switch ($format) {
250 262 case 'pdf':
263 + if ($content_length) {
264 + header('Content-Length: ' . strlen($file));
265 + }
251 266 if (!$disposition) {
252 267 $disposition = 'inline';
253 268 }
254 - header("Content-Disposition: {$disposition}; filename=\"{$name}.pdf\"");
255 - header("Content-type: application/pdf");
269 + if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) {
270 + header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.pdf");
271 + } else {
272 + header("Content-Disposition: {$disposition}; filename=\"{$name}.pdf\"");
273 + }
274 + header("Content-Type: application/pdf");
275 + if ($preview && $disposition == 'inline' && isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false) {
276 + header('Cache-Control: private');
277 + }
256 278 break;
279 + case 'jpg':
280 + if ($content_length) {
281 + header('Content-Length: ' . strlen($file));
282 + }
283 + if (!$disposition) {
284 + $disposition = 'attachment';
285 + }
286 + if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) {
287 + header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.jpg");
288 + } else {
289 + header("Content-Disposition: {$disposition}; filename=\"{$name}.jpg\"");
290 + }
291 + header('Content-Type: image/jpeg');
292 + if ($preview && $disposition == 'inline' && isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false) {
293 + header('Cache-Control: private');
294 + }
295 + break;
257 296 case 'zip':
297 + if ($content_length) {
298 + if ($fpassthru) {
299 + if (ini_get('zlib.output_compression')) {
300 + ini_set('zlib.output_compression', 'Off');
301 + }
302 + header('Content-Length: ' . filesize(trim($file)));
303 + } else {
304 + header('Content-Length: ' . strlen($file));
305 + }
306 + }
258 307 if (!$disposition) {
259 308 $disposition = 'attachment';
260 309 }
261 - header("Content-Disposition: {$disposition}; filename=\"{$name}.zip\"");
262 - header('Content-type: application/zip');
310 + if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) {
311 + header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.zip");
312 + } else {
313 + header("Content-Disposition: {$disposition}; filename=\"{$name}.zip\"");
314 + }
315 + header('Content-Type: application/zip');
263 316 break;
264 -
265 317 case 'xml':
266 318 if (!$disposition) {
267 319 $disposition = 'attachment';
268 320 }
269 - header("Content-Disposition: {$disposition}; filename=\"{$name}.xml\"");
270 - header('Content-type: text/xml');
321 + if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) {
322 + header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.xml");
323 + } else {
324 + header("Content-Disposition: {$disposition}; filename=\"{$name}.xml\"");
325 + }
326 + header('Content-Type: text/xml');
271 327 break;
272 -
273 328 case 'txt':
274 329 if (!$disposition) {
275 330 $disposition = 'attachment';
276 331 }
277 - header("Content-Disposition: {$disposition}; filename=\"{$name}.txt\"");
278 - header('Content-type: text/html');
332 + if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) {
333 + header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.txt");
334 + } else {
335 + header("Content-Disposition: {$disposition}; filename=\"{$name}.txt\"");
336 + }
337 + header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
279 338 break;
280 -
339 + case 'doc':
340 + if (!$disposition) {
341 + $disposition = 'attachment';
342 + }
343 + if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) {
344 + header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.txt");
345 + } else {
346 + header("Content-Disposition: {$disposition}; filename=\"{$name}.txt\"");
347 + }
348 + header('Content-Type: application/msword');
349 + break;
350 + case 'docx':
351 + if (!$disposition) {
352 + $disposition = 'attachment';
353 + }
354 + if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) {
355 + header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.txt");
356 + } else {
357 + header("Content-Disposition: {$disposition}; filename=\"{$name}.txt\"");
358 + }
359 + header('Content-Type: text/html');
360 + break;
281 361 case 'php':
282 362 if (!$disposition) {
283 363 $disposition = 'attachment';
284 364 }
285 - header("Content-Disposition: {$disposition}; filename=\"{$name}.php\"");
286 - header('Content-type: text/html');
365 + if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) {
366 + header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.php");
367 + } else {
368 + header("Content-Disposition: {$disposition}; filename=\"{$name}.php\"");
369 + }
370 + header('Content-Type: text/html');
287 371 break;
288 -
289 372 default:
290 373 break;
291 374 }
292 - echo base64_decode($file);
293 - die();
375 +
376 + if ($fpassthru) {
377 + $handle = @fopen($file, 'rb');
378 + while (!feof($handle)) {
379 + echo fread($handle, 8192);
380 + }
381 + fclose($handle);
382 + } else {
383 + echo $file;
384 + }
294 385 }
295 386
296 - /**
297 - * Check if exists in array
298 - *
299 - * @param string $value - Array Key
300 - * @param mixed $compare
301 - * @param array $data - Array of data
302 - *
303 - * @return bool
304 - */
387 + // exist
305 388 public function exist($value, $compare = false, $data = array()) {
306 389 if (isset($data[$value]) && $value === $compare) {
307 390 return true;
308 391 } else {
@@ -309,40 +392,25 @@
309 392 return false;
310 393 }
311 394 }
312 395
313 - /**
314 - * Force Error
315 - *
316 - * @param string $code - Error Code
317 - */
318 - public function error($code = '404') {
396 + // handle 404
397 + public function handle_404() {
319 398 global $wp_query;
320 - if ($code === '404') {
321 - $wp_query->set_404();
322 - status_header(404);
323 - $this->redirect(site_url('wp-admin'));
399 + $wp_query->set_404();
400 + status_header(404);
401 + nocache_headers();
402 + if (is_admin()) {
324 403 wp_die('404');
325 404 }
326 405 }
327 406
328 - /**
329 - * Set var available to view template
330 - *
331 - * @param string $key - Key of value
332 - * @param mixed $value - Value
333 - */
407 + // view
334 408 public function view($key, $value) {
335 409 $this->view->$key = $value;
336 410 }
337 411
338 - /**
339 - * Check if Array/Object Empty
340 - *
341 - * @param mixed $data - Object/Array to check
342 - *
343 - * @return bool
344 - */
412 + // is empty
345 413 public function is_empty($data) {
346 414 if (is_object($data) || is_array($data)) {
347 415
348 416 if (is_object($data)) {
@@ -355,25 +423,17 @@
355 423
356 424 return true;
357 425 }
358 426
359 - /**
360 - * Verify nonce
361 - *
362 - * @param string $nonce - Nonce value
363 - * @param string $key - Nonce Key
364 - */
365 - public function check_nonce($nonce, $key) {
366 - $message = __('Security Issue: Nonce Incorrect', 'e2pdf');
367 - if ($key === 'e2pdf_ajax') {
368 - if (!wp_verify_nonce($nonce, $key)) {
369 - $data['error'] = $message;
370 - $this->json_response($data);
371 - }
372 - } else {
373 - if (!wp_verify_nonce($nonce, $key)) {
374 - die('<div id="message" class="error e2pdf-notice">' . $message . '</div>');
375 - }
427 + // message
428 + public function message($key) {
429 + $message = '';
430 + switch ($key) {
431 + case 'wp_verify_nonce_error':
432 + $message = 'E2Pdf Bad Request';
433 + break;
434 + default:
435 + break;
376 436 }
437 + return $message;
377 438 }
378 -
379 439 }