PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.15
E2Pdf – Export Pdf Tool for WordPress v1.32.15
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 1.08.00 1.08.06 All 72 releases
e2pdf / classes / helper / e2pdf-view.php

e2pdf-view.php in E2Pdf – Export Pdf Tool for WordPress 1.32.15, at classes/helper/e2pdf-view.php

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