| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /helper/e2pdf-view.php |
| 5 |
* |
| 6 |
* @package E2Pdf |
| 7 |
* @license GPLv3 |
| 8 |
* @link https://e2pdf.com |
| 9 |
*/ |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
die('Access denied.'); |
| 12 |
} |
| 13 |
|
| 14 |
class Helper_E2pdf_View { |
| 15 |
|
| 16 |
protected static $instance = null; |
| 17 |
private $view_dir; |
| 18 |
public $uri; |
| 19 |
public $page; |
| 20 |
public $controller; |
| 21 |
public $helper; |
| 22 |
public $notification; |
| 23 |
public $tpl; |
| 24 |
public $tpl_page; |
| 25 |
public $view = null; |
| 26 |
public $tpl_args = null; |
| 27 |
public $get = null; |
| 28 |
public $post = null; |
| 29 |
public $files = null; |
| 30 |
|
| 31 |
public static function instance() { |
| 32 |
if (!isset(self::$instance)) { |
| 33 |
self::$instance = new self(); |
| 34 |
} |
| 35 |
return self::$instance; |
| 36 |
} |
| 37 |
|
| 38 |
public function __construct() { |
| 39 |
$this->view = new stdClass(); |
| 40 |
$this->uri = home_url(add_query_arg(null, null)); |
| 41 |
$this->get = new Helper_E2pdf_Get($this->uri); |
| 42 |
$this->post = new Helper_E2pdf_Post(); |
| 43 |
$this->files = new Helper_E2pdf_Files(); |
| 44 |
$this->page = $this->get->get_page(); |
| 45 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 46 |
$this->notification = Model_E2pdf_Notification::instance(); |
| 47 |
$this->view_dir = $this->helper->get('plugin_dir') . 'classes/view/'; |
| 48 |
} |
| 49 |
|
| 50 |
// render |
| 51 |
public function render($type, $element, $args = array()) { |
| 52 |
$this->tpl_args = new Helper_E2pdf_Tplargs($args); |
| 53 |
$this->tpl = $this->view_dir . $type . '/' . $element . '.php'; |
| 54 |
if (file_exists($this->tpl)) { |
| 55 |
include $this->tpl; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// render metabox |
| 60 |
public function render_metabox($post, $metabox) { |
| 61 |
$this->render('metaboxes', $metabox['args']['tpl']); |
| 62 |
} |
| 63 |
|
| 64 |
// render page |
| 65 |
public function render_page() { |
| 66 |
$this->tpl_page = $this->view_dir . 'page-' . $this->page . '.php'; |
| 67 |
$controller = $this->page_to_controller($this->page); |
| 68 |
if (class_exists($controller)) { |
| 69 |
$this->controller = new $controller(); |
| 70 |
$method = 'index_action'; |
| 71 |
if ($this->get->get('action')) { |
| 72 |
$method = $this->get->get('action') . '_action'; |
| 73 |
} |
| 74 |
if (method_exists($this->controller, $method)) { |
| 75 |
$this->controller->$method(); |
| 76 |
$this->view = $this->controller->view; |
| 77 |
if (file_exists($this->tpl_page)) { |
| 78 |
include $this->tpl_page; |
| 79 |
} |
| 80 |
} else { |
| 81 |
$this->handle_404(); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// render frontend page |
| 87 |
public function render_frontend_page() { |
| 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); |
| 91 |
if (class_exists($controller)) { |
| 92 |
$this->controller = new $controller(); |
| 93 |
$method = 'index_action'; |
| 94 |
if ($this->get->get('action')) { |
| 95 |
$method = $this->get->get('action') . '_action'; |
| 96 |
} |
| 97 |
if (method_exists($this->controller, $method)) { |
| 98 |
$this->controller->$method(); |
| 99 |
if (file_exists($this->tpl_page)) { |
| 100 |
include $this->tpl_page; |
| 101 |
} |
| 102 |
} else { |
| 103 |
$this->handle_404(); |
| 104 |
} |
| 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 |
} |
| 120 |
|
| 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'; |
| 133 |
} |
| 134 |
if (method_exists($this->controller, $method)) { |
| 135 |
$this->controller->$method($rpc); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// page to controller |
| 141 |
public function page_to_controller($page, $frontend = false) { |
| 142 |
$controller = array( |
| 143 |
'Controller', |
| 144 |
); |
| 145 |
if ($frontend) { |
| 146 |
$controller[] = 'Frontend'; |
| 147 |
} |
| 148 |
$controller = array_merge($controller, explode('-', $page)); |
| 149 |
$controller = array_map('ucfirst', $controller); |
| 150 |
return implode("_", $controller); |
| 151 |
} |
| 152 |
|
| 153 |
// redirect |
| 154 |
public function redirect($location) { |
| 155 |
if (headers_sent()) { |
| 156 |
die("<script>window.location='{$location}';</script>"); |
| 157 |
} else { |
| 158 |
wp_redirect($location); |
| 159 |
exit(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// add notification |
| 164 |
public function add_notification($type, $text) { |
| 165 |
return $this->notification->add_notification($type, $text); |
| 166 |
} |
| 167 |
|
| 168 |
// get notifications |
| 169 |
public function get_notifications($notification_id = '') { |
| 170 |
return $this->notification->get_notifications($notification_id); |
| 171 |
} |
| 172 |
|
| 173 |
// json response |
| 174 |
public function json_response($data = array()) { |
| 175 |
@header('Content-Type: application/json; charset=' . get_option('blog_charset')); |
| 176 |
if (function_exists('wp_json_encode')) { |
| 177 |
echo wp_json_encode($data, JSON_FORCE_OBJECT); |
| 178 |
} else { |
| 179 |
echo json_encode($data, JSON_FORCE_OBJECT); |
| 180 |
} |
| 181 |
wp_die(); |
| 182 |
} |
| 183 |
|
| 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 |
| 201 |
public function close_tab_response() { |
| 202 |
echo " |
| 203 |
<script> |
| 204 |
self.close(); |
| 205 |
</script> |
| 206 |
"; |
| 207 |
} |
| 208 |
|
| 209 |
// download response |
| 210 |
public function download_response($format, $file, $name = '', $disposition = '', $fpassthru = false, $preview = false) { |
| 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 |
|
| 253 |
if (!$name) { |
| 254 |
$name = time(); |
| 255 |
} |
| 256 |
$name = rawurlencode($this->helper->load('convert')->to_file_name($name)); |
| 257 |
|
| 258 |
status_header(200); |
| 259 |
header('Content-Transfer-Encoding: binary'); |
| 260 |
header('X-Robots-Tag: noindex, nofollow'); |
| 261 |
switch ($format) { |
| 262 |
case 'pdf': |
| 263 |
if ($content_length) { |
| 264 |
header('Content-Length: ' . strlen($file)); |
| 265 |
} |
| 266 |
if (!$disposition) { |
| 267 |
$disposition = 'inline'; |
| 268 |
} |
| 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 |
} |
| 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; |
| 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 |
} |
| 307 |
if (!$disposition) { |
| 308 |
$disposition = 'attachment'; |
| 309 |
} |
| 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'); |
| 316 |
break; |
| 317 |
case 'xml': |
| 318 |
if (!$disposition) { |
| 319 |
$disposition = 'attachment'; |
| 320 |
} |
| 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'); |
| 327 |
break; |
| 328 |
case 'txt': |
| 329 |
if (!$disposition) { |
| 330 |
$disposition = 'attachment'; |
| 331 |
} |
| 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'); |
| 338 |
break; |
| 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; |
| 361 |
case 'php': |
| 362 |
if (!$disposition) { |
| 363 |
$disposition = 'attachment'; |
| 364 |
} |
| 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'); |
| 371 |
break; |
| 372 |
default: |
| 373 |
break; |
| 374 |
} |
| 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 |
} |
| 385 |
} |
| 386 |
|
| 387 |
// exist |
| 388 |
public function exist($value, $compare = false, $data = array()) { |
| 389 |
if (isset($data[$value]) && $value === $compare) { |
| 390 |
return true; |
| 391 |
} else { |
| 392 |
return false; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
// handle 404 |
| 397 |
public function handle_404() { |
| 398 |
global $wp_query; |
| 399 |
$wp_query->set_404(); |
| 400 |
status_header(404); |
| 401 |
nocache_headers(); |
| 402 |
if (is_admin()) { |
| 403 |
wp_die('404'); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
// view |
| 408 |
public function view($key, $value) { |
| 409 |
$this->view->$key = $value; |
| 410 |
} |
| 411 |
|
| 412 |
// is empty |
| 413 |
public function is_empty($data) { |
| 414 |
if (is_object($data) || is_array($data)) { |
| 415 |
|
| 416 |
if (is_object($data)) { |
| 417 |
$data = (array) $data; |
| 418 |
} |
| 419 |
if (count($data) > 0) { |
| 420 |
return false; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
return true; |
| 425 |
} |
| 426 |
|
| 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; |
| 436 |
} |
| 437 |
return $message; |
| 438 |
} |
| 439 |
} |
| 440 |
|