| 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 = Model_E2pdf_Notification::instance(); |
| 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 |
return $this->notification->add_notification($type, $text); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Get notifications |
| 194 |
* @return array - List of notifications |
| 195 |
*/ |
| 196 |
public function get_notifications($notification_id = '') { |
| 197 |
return $this->notification->get_notifications($notification_id); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Force Json response |
| 202 |
* @param array $data - Array of data |
| 203 |
* @return json |
| 204 |
*/ |
| 205 |
public function json_response($data = array()) { |
| 206 |
@header('Content-Type: application/json; charset=' . get_option('blog_charset')); |
| 207 |
if (function_exists('wp_json_encode')) { |
| 208 |
echo wp_json_encode($data, JSON_FORCE_OBJECT); |
| 209 |
} else { |
| 210 |
echo json_encode($data, JSON_FORCE_OBJECT); |
| 211 |
} |
| 212 |
wp_die(); |
| 213 |
} |
| 214 |
|
| 215 |
public function json_response_ajax($data = array(), $status = 200) { |
| 216 |
if (ob_get_length() > 0) { |
| 217 |
while (@ob_end_clean()); |
| 218 |
} |
| 219 |
status_header($status); |
| 220 |
header('X-Robots-Tag: noindex, nofollow'); |
| 221 |
@header('Content-Type: application/json; charset=' . get_option('blog_charset')); |
| 222 |
if (function_exists('wp_json_encode')) { |
| 223 |
echo wp_json_encode($data, JSON_FORCE_OBJECT); |
| 224 |
} else { |
| 225 |
echo json_encode($data, JSON_FORCE_OBJECT); |
| 226 |
} |
| 227 |
exit; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Force close browser tab from PHP |
| 232 |
*/ |
| 233 |
public function close_tab_response() { |
| 234 |
echo " |
| 235 |
<script> |
| 236 |
self.close(); |
| 237 |
</script> |
| 238 |
"; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Force Download response |
| 243 |
* @param string $format - Format of file |
| 244 |
* @param string $file - Base64 Encoded File |
| 245 |
* @param string $name - Name of file when download |
| 246 |
* @param string $disposition - Disposition of file inline|attachment |
| 247 |
* @return string |
| 248 |
*/ |
| 249 |
public function download_response($format, $file, $name = '', $disposition = '', $fpassthru = false, $preview = false) { |
| 250 |
|
| 251 |
$content_length = true; |
| 252 |
/** |
| 253 |
* External Links – nofollow, noopener & new window compatibility fix filter |
| 254 |
* https://wordpress.org/plugins/wp-external-links/ |
| 255 |
*/ |
| 256 |
add_filter('wpel_apply_settings', '__return_false'); |
| 257 |
|
| 258 |
/** |
| 259 |
* Compatibility fix with TranslatePress |
| 260 |
* https://wordpress.org/plugins/translatepress-multilingual/ |
| 261 |
*/ |
| 262 |
add_filter('trp_stop_translating_page', '__return_true'); |
| 263 |
|
| 264 |
if (ob_get_length() > 0) { |
| 265 |
while (@ob_end_clean()); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* PHP 8.0.17 & PHP 8.1.4 compatibility with zlib enabled |
| 270 |
*/ |
| 271 |
header_remove("Content-Encoding"); |
| 272 |
|
| 273 |
/** |
| 274 |
* W3 Total Cache + OVH compatibility fix |
| 275 |
* https://wordpress.org/plugins/w3-total-cache/ |
| 276 |
*/ |
| 277 |
if (function_exists('w3tc_config')) { |
| 278 |
$w3tc_config = w3tc_config(); |
| 279 |
if ($w3tc_config->get_boolean('browsercache.enabled') && $w3tc_config->get_boolean('browsercache.html.compression') && function_exists('php_uname') && false !== stripos(@php_uname(), '.ovh.net')) { |
| 280 |
$content_length = false; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* WP Rocket + OVH compatibility fix |
| 286 |
* https://wp-rocket.me/ |
| 287 |
*/ |
| 288 |
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')) { |
| 289 |
$content_length = false; |
| 290 |
} |
| 291 |
|
| 292 |
if (!$name) { |
| 293 |
$name = time(); |
| 294 |
} |
| 295 |
$name = rawurlencode($this->helper->load('convert')->to_file_name($name)); |
| 296 |
|
| 297 |
status_header(200); |
| 298 |
header('Content-Transfer-Encoding: binary'); |
| 299 |
header('X-Robots-Tag: noindex, nofollow'); |
| 300 |
switch ($format) { |
| 301 |
case 'pdf': |
| 302 |
if ($content_length) { |
| 303 |
header('Content-Length: ' . strlen($file)); |
| 304 |
} |
| 305 |
if (!$disposition) { |
| 306 |
$disposition = 'inline'; |
| 307 |
} |
| 308 |
if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) { |
| 309 |
header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.pdf"); |
| 310 |
} else { |
| 311 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.pdf\""); |
| 312 |
} |
| 313 |
header("Content-Type: application/pdf"); |
| 314 |
if ($preview && $disposition == 'inline' && isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false) { |
| 315 |
header('Cache-Control: private'); |
| 316 |
} |
| 317 |
break; |
| 318 |
case 'jpg': |
| 319 |
if ($content_length) { |
| 320 |
header('Content-Length: ' . strlen($file)); |
| 321 |
} |
| 322 |
if (!$disposition) { |
| 323 |
$disposition = 'attachment'; |
| 324 |
} |
| 325 |
if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) { |
| 326 |
header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.jpg"); |
| 327 |
} else { |
| 328 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.jpg\""); |
| 329 |
} |
| 330 |
header('Content-Type: image/jpeg'); |
| 331 |
if ($preview && $disposition == 'inline' && isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false) { |
| 332 |
header('Cache-Control: private'); |
| 333 |
} |
| 334 |
break; |
| 335 |
case 'zip': |
| 336 |
if ($content_length) { |
| 337 |
if ($fpassthru) { |
| 338 |
if (ini_get('zlib.output_compression')) { |
| 339 |
ini_set('zlib.output_compression', 'Off'); |
| 340 |
} |
| 341 |
header('Content-Length: ' . filesize(trim($file))); |
| 342 |
} else { |
| 343 |
header('Content-Length: ' . strlen($file)); |
| 344 |
} |
| 345 |
} |
| 346 |
if (!$disposition) { |
| 347 |
$disposition = 'attachment'; |
| 348 |
} |
| 349 |
if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) { |
| 350 |
header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.zip"); |
| 351 |
} else { |
| 352 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.zip\""); |
| 353 |
} |
| 354 |
header('Content-Type: application/zip'); |
| 355 |
break; |
| 356 |
case 'xml': |
| 357 |
if (!$disposition) { |
| 358 |
$disposition = 'attachment'; |
| 359 |
} |
| 360 |
if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) { |
| 361 |
header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.xml"); |
| 362 |
} else { |
| 363 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.xml\""); |
| 364 |
} |
| 365 |
header('Content-Type: text/xml'); |
| 366 |
break; |
| 367 |
case 'txt': |
| 368 |
if (!$disposition) { |
| 369 |
$disposition = 'attachment'; |
| 370 |
} |
| 371 |
if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) { |
| 372 |
header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.txt"); |
| 373 |
} else { |
| 374 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.txt\""); |
| 375 |
} |
| 376 |
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); |
| 377 |
break; |
| 378 |
case 'doc': |
| 379 |
if (!$disposition) { |
| 380 |
$disposition = 'attachment'; |
| 381 |
} |
| 382 |
if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) { |
| 383 |
header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.txt"); |
| 384 |
} else { |
| 385 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.txt\""); |
| 386 |
} |
| 387 |
header('Content-Type: application/msword'); |
| 388 |
break; |
| 389 |
case 'docx': |
| 390 |
if (!$disposition) { |
| 391 |
$disposition = 'attachment'; |
| 392 |
} |
| 393 |
if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) { |
| 394 |
header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.txt"); |
| 395 |
} else { |
| 396 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.txt\""); |
| 397 |
} |
| 398 |
header('Content-Type: text/html'); |
| 399 |
break; |
| 400 |
case 'php': |
| 401 |
if (!$disposition) { |
| 402 |
$disposition = 'attachment'; |
| 403 |
} |
| 404 |
if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) { |
| 405 |
header("Content-Disposition: {$disposition}; filename*=UTF-8''{$name}.php"); |
| 406 |
} else { |
| 407 |
header("Content-Disposition: {$disposition}; filename=\"{$name}.php\""); |
| 408 |
} |
| 409 |
header('Content-Type: text/html'); |
| 410 |
break; |
| 411 |
default: |
| 412 |
break; |
| 413 |
} |
| 414 |
|
| 415 |
if ($fpassthru) { |
| 416 |
$handle = @fopen($file, 'rb'); |
| 417 |
while (!feof($handle)) { |
| 418 |
echo fread($handle, 8192); |
| 419 |
} |
| 420 |
fclose($handle); |
| 421 |
} else { |
| 422 |
echo $file; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Check if exists in array |
| 428 |
* @param string $value - Array Key |
| 429 |
* @param mixed $compare |
| 430 |
* @param array $data - Array of data |
| 431 |
* @return bool |
| 432 |
*/ |
| 433 |
public function exist($value, $compare = false, $data = array()) { |
| 434 |
if (isset($data[$value]) && $value === $compare) { |
| 435 |
return true; |
| 436 |
} else { |
| 437 |
return false; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* Force Error |
| 443 |
* @param string $code - Error Code |
| 444 |
*/ |
| 445 |
public function handle_404() { |
| 446 |
global $wp_query; |
| 447 |
$wp_query->set_404(); |
| 448 |
status_header(404); |
| 449 |
nocache_headers(); |
| 450 |
if (is_admin()) { |
| 451 |
wp_die('404'); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* Set var available to view template |
| 457 |
* @param string $key - Key of value |
| 458 |
* @param mixed $value - Value |
| 459 |
*/ |
| 460 |
public function view($key, $value) { |
| 461 |
$this->view->$key = $value; |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* Check if Array/Object Empty |
| 466 |
* @param mixed $data - Object/Array to check |
| 467 |
* @return bool |
| 468 |
*/ |
| 469 |
public function is_empty($data) { |
| 470 |
if (is_object($data) || is_array($data)) { |
| 471 |
|
| 472 |
if (is_object($data)) { |
| 473 |
$data = (array) $data; |
| 474 |
} |
| 475 |
if (count($data) > 0) { |
| 476 |
return false; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
return true; |
| 481 |
} |
| 482 |
|
| 483 |
public function message($key) { |
| 484 |
$message = ''; |
| 485 |
switch ($key) { |
| 486 |
case 'wp_verify_nonce_error': |
| 487 |
$message = 'E2Pdf Bad Request'; |
| 488 |
break; |
| 489 |
default: |
| 490 |
break; |
| 491 |
} |
| 492 |
return $message; |
| 493 |
} |
| 494 |
} |
| 495 |
|