| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf Frontend Download Controller |
| 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 |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
die('Access denied.'); |
| 14 |
} |
| 15 |
|
| 16 |
class Controller_Frontend_E2pdf_Download extends Helper_E2pdf_View { |
| 17 |
|
| 18 |
/** |
| 19 |
* Frontend download action |
| 20 |
* |
| 21 |
* @url page=e2pdf-download&uid=$uid |
| 22 |
*/ |
| 23 |
public function index_action() { |
| 24 |
global $wp_query; |
| 25 |
|
| 26 |
$download = false; |
| 27 |
$uid = $this->get->get('uid'); |
| 28 |
|
| 29 |
$entry = new Model_E2pdf_Entry(); |
| 30 |
|
| 31 |
if ($entry->load_by_uid($uid)) { |
| 32 |
|
| 33 |
$uid_params = $entry->get('entry'); |
| 34 |
|
| 35 |
$template = new Model_E2pdf_Template(); |
| 36 |
|
| 37 |
if (isset($uid_params['pdf'])) { |
| 38 |
if ($uid_params['pdf'] && file_exists($uid_params['pdf']) && isset($uid_params['template_id']) && isset($uid_params['dataset']) && $template->load($uid_params['template_id'])) { |
| 39 |
$download = true; |
| 40 |
$disposition = 'attachment'; |
| 41 |
if (isset($uid_params['inline'])) { |
| 42 |
if ($uid_params['inline']) { |
| 43 |
$disposition = 'inline'; |
| 44 |
} |
| 45 |
} else { |
| 46 |
if ($template->get('inline')) { |
| 47 |
$disposition = 'inline'; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
$name = basename($uid_params['pdf'], ".pdf"); |
| 52 |
$file = base64_encode(file_get_contents($uid_params['pdf'])); |
| 53 |
$this->download_response('pdf', $file, $name, $disposition); |
| 54 |
} |
| 55 |
} elseif (isset($uid_params['template_id']) && isset($uid_params['dataset']) && $template->load($uid_params['template_id'])) { |
| 56 |
|
| 57 |
$template->extension()->set('dataset', $uid_params['dataset']); |
| 58 |
|
| 59 |
if (isset($uid_params['flatten'])) { |
| 60 |
$template->set('flatten', $uid_params['flatten']); |
| 61 |
} |
| 62 |
|
| 63 |
if (isset($uid_params['format'])) { |
| 64 |
$template->set('format', $uid_params['format']); |
| 65 |
} |
| 66 |
|
| 67 |
if (isset($uid_params['password'])) { |
| 68 |
$template->set('password', $uid_params['password']); |
| 69 |
} else { |
| 70 |
$template->set('password', $template->extension()->render($template->get('password'), 'value')); |
| 71 |
} |
| 72 |
|
| 73 |
if (isset($uid_params['name'])) { |
| 74 |
$template->set('name', $uid_params['name']); |
| 75 |
} else { |
| 76 |
$template->set('name', $template->extension()->render($template->get('name'), 'value')); |
| 77 |
} |
| 78 |
|
| 79 |
$disposition = 'attachment'; |
| 80 |
if (isset($uid_params['inline'])) { |
| 81 |
if ($uid_params['inline']) { |
| 82 |
$disposition = 'inline'; |
| 83 |
} |
| 84 |
} else { |
| 85 |
if ($template->get('inline')) { |
| 86 |
$disposition = 'inline'; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if (isset($uid_params['pdf']) && file_exists($uid_params['pdf'])) { |
| 91 |
|
| 92 |
$entry->set('pdf_num', $entry->get('pdf_num') + 1); |
| 93 |
$entry->save(); |
| 94 |
|
| 95 |
$download = true; |
| 96 |
|
| 97 |
if ($template->get('name')) { |
| 98 |
$name = $template->get('name'); |
| 99 |
} else { |
| 100 |
$name = $template->extension()->render($template->get_filename(), 'value'); |
| 101 |
} |
| 102 |
$file = base64_encode(file_get_contents($uid_params['pdf'])); |
| 103 |
$this->download_response('pdf', $file, $name, $disposition); |
| 104 |
} elseif ($template->extension()->verify()) { |
| 105 |
$template->fill($uid_params['dataset'], $uid); |
| 106 |
$request = $template->render(); |
| 107 |
if (isset($request['error'])) { |
| 108 |
wp_die($request['error']); |
| 109 |
} elseif ($request['file'] === '') { |
| 110 |
wp_die(__('Something went wrong!', 'e2pdf')); |
| 111 |
} else { |
| 112 |
$entry->set('pdf_num', $entry->get('pdf_num') + 1); |
| 113 |
$entry->save(); |
| 114 |
|
| 115 |
$download = true; |
| 116 |
|
| 117 |
if ($template->get('name')) { |
| 118 |
$name = $template->get('name'); |
| 119 |
} else { |
| 120 |
$name = $template->extension()->render($template->get_filename(), 'value'); |
| 121 |
} |
| 122 |
|
| 123 |
$file = $request['file']; |
| 124 |
$this->download_response('pdf', $file, $name, $disposition); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if (!$download) { |
| 131 |
$wp_query->set_404(); |
| 132 |
status_header(404); |
| 133 |
nocache_headers(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|