PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.08.06
E2Pdf – Export Pdf Tool for WordPress v1.08.06
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 / controller / e2pdf-templates.php

e2pdf-templates.php in E2Pdf – Export Pdf Tool for WordPress 1.08.06, at classes/controller/e2pdf-templates.php

2,003 lines 83.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * E2pdf Templates 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_E2pdf_Templates extends Helper_E2pdf_View {
17
18 /**
19 * @url admin.php?page=e2pdf-templates
20 */
21 public function index_action() {
22
23 if ($this->post->get()) {
24 if ($this->post->get('screenoptionnonce')) {
25 $this->screen_action();
26 } else {
27 $this->check_nonce($this->post->get('_nonce'), 'e2pdf_post');
28 if (($this->post->get('action') == '-1' && $this->post->get('action2') == '-1')) {
29 if ($this->post->get('s')) {
30 $location = $this->helper->get_url(
31 array(
32 'page' => 'e2pdf-templates',
33 'status' => $this->get->get('status'),
34 'orderby' => $this->get->get('orderby'),
35 'order' => $this->get->get('order'),
36 's' => $this->post->get('s')
37 )
38 );
39 } else {
40 $location = $this->helper->get_url(
41 array(
42 'page' => 'e2pdf-templates',
43 'status' => $this->get->get('status'),
44 'orderby' => $this->get->get('orderby'),
45 'order' => $this->get->get('order'),
46 )
47 );
48 }
49 $this->redirect($location);
50 } else {
51 $action = $this->post->get('action') != '-1' ? $this->post->get('action') : $this->post->get('action2');
52 if ($this->post->get('post')) {
53 if ($action == 'trash') {
54 foreach ($this->post->get('post') as $key => $value) {
55 $this->trash_template($value);
56 }
57 $this->add_notification('update', __('Templates moved to trash', 'e2pdf'));
58 } elseif ($action == 'restore') {
59 foreach ($this->post->get('post') as $key => $value) {
60 $this->restore_template($value);
61 }
62 $this->add_notification('update', __('Templates restored', 'e2pdf'));
63 } elseif ($action == 'delete') {
64 foreach ($this->post->get('post') as $key => $value) {
65 $this->delete_template($value);
66 }
67 $this->add_notification('update', __('Templates deleted', 'e2pdf'));
68 }
69 }
70 }
71 }
72 }
73 }
74
75 /**
76 * @url admin.php?page=e2pdf-templates&action=view&id=$id
77 *
78 * @return file
79 */
80 public function view_action() {
81
82 if ($this->get->get('id')) {
83 $template = new Model_E2pdf_Template();
84
85 if ($template->load($this->get->get('id'))) {
86 $pages = $template->get('pages');
87 foreach ($pages as $page_key => $page_value) {
88 if (isset($page_value['elements'])) {
89 foreach ($page_value['elements'] as $element_key => $element_value) {
90 if (isset($element_value['type']) && $element_value['type'] == 'e2pdf-image') {
91 if (!$this->helper->load('image')->get_image($element_value['value'])) {
92 $pages[$page_key]['elements'][$element_key]['value'] = plugins_url('', $this->helper->get('plugin_file_path')) . "/img/upload.png";
93
94 if (isset($pages[$page_key]['elements'][$element_key]['properties']['dimension'])) {
95 unset($pages[$page_key]['elements'][$element_key]['properties']['dimension']);
96 }
97 }
98 }
99 }
100 }
101 }
102
103 $template->set('pages', $pages);
104 $request = $template->render();
105
106 if (isset($request['error'])) {
107 $this->add_notification('error', $request['error']);
108 $this->render('blocks', 'notifications');
109 } else {
110 $filename = $template->get_filename();
111 $file = $request['file'];
112 $this->download_response('pdf', $file, $filename);
113 }
114 } else {
115 $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
116 $this->render('blocks', 'notifications');
117 }
118 } else {
119 $location = $this->helper->get_url(
120 array(
121 'page' => 'e2pdf',
122 )
123 );
124 $this->redirect($location);
125 }
126 }
127
128 /**
129 * @url admin.php?page=e2pdf-templates&action=preview
130 */
131 public function preview_action() {
132
133 $preview = array();
134
135 if ($this->post->get('preview')) {
136 $preview = json_decode(stripslashes($this->post->get('preview')), true);
137 }
138
139 if (empty($preview)) {
140 $this->close_tab_response();
141 } else {
142 $data = $preview;
143 $pages = $preview['pages'];
144 unset($preview['pages']);
145
146 $this->check_nonce($preview['_nonce'], 'e2pdf_post');
147
148 $template = new Model_E2pdf_Template();
149 foreach ($data as $key => $value) {
150 $template->set($key, $value);
151 }
152
153 $preview_pages = array();
154 foreach ($pages as $page_key => $page_value) {
155 $page = new Model_E2pdf_Page();
156 foreach ($page_value as $page_value_key => $page_value_value) {
157 $page->set($page_value_key, $page_value_value);
158 }
159 $page->set('page_id', $page_key);
160
161 $elements = array();
162
163 $this->helper->load('sort')->uasort($page_value['elements'], "sort_by_elementid");
164 $this->helper->load('sort')->stable_uasort($page_value['elements'], "sort_by_zindex");
165
166 foreach ($page_value['elements'] as $element_key => $element_value) {
167 $element = new Model_E2pdf_Element();
168 $esig = isset($element_value['properties']['esig']) && $element_value['properties']['esig'] ? true : false;
169 if (isset($element_value['type']) && ($element_value['type'] == 'e2pdf-image' || ($element_value['type'] == 'e2pdf-signature' && !$esig))) {
170 if (!$this->helper->load('image')->get_image($element_value['value'])) {
171 $element_value['value'] = plugins_url('', $this->helper->get('plugin_file_path')) . "/img/upload.png";
172 if (isset($element_value['properties']['dimension'])) {
173 unset($element_value['properties']['dimension']);
174 }
175 }
176 }
177
178 foreach ($element_value as $element_value_key => $element_value_value) {
179 $element->set($element_value_key, $element_value_value);
180 }
181
182 if (!$element_value['properties']) {
183 $element_properties = array();
184 } else {
185 $element_properties = $element_value['properties'];
186 }
187
188 $element->set('properties', $element_properties);
189 $elements[] = $element->get_element();
190 }
191
192 $page->set('elements', $elements);
193 $preview_pages[] = $page->get_page();
194 }
195
196 $template->set('pages', $preview_pages);
197 $request = $template->render(true);
198
199 if (isset($request['error'])) {
200 $this->add_notification('error', $request['error']);
201 $this->render('blocks', 'notifications');
202 } else {
203 $filename = $template->get_filename();
204 $file = $request['file'];
205 $this->download_response('pdf', $file, $filename);
206 }
207 }
208 }
209
210 /**
211 * @url admin.php?page=e2pdf-templates&action=convert&type=php
212 */
213 public function convert_action() {
214 $preview = array();
215
216 if ($this->post->get('preview')) {
217 $preview = json_decode(stripslashes($this->post->get('preview')), true);
218 }
219
220 if (empty($preview)) {
221 $this->close_tab_response();
222 } else {
223 $data = $preview;
224 $pages = $preview['pages'];
225 unset($preview['pages']);
226
227 $this->check_nonce($preview['_nonce'], 'e2pdf_post');
228
229 $template = new Model_E2pdf_Template();
230 foreach ($data as $key => $value) {
231 $template->set($key, $value);
232 }
233
234 $preview_pages = array();
235 foreach ($pages as $page_key => $page_value) {
236 $page = new Model_E2pdf_Page();
237 foreach ($page_value as $page_value_key => $page_value_value) {
238 $page->set($page_value_key, $page_value_value);
239 }
240 $page->set('page_id', $page_key);
241
242 $elements = array();
243
244 $this->helper->load('sort')->uasort($page_value['elements'], "sort_by_elementid");
245 $this->helper->load('sort')->stable_uasort($page_value['elements'], "sort_by_zindex");
246
247 foreach ($page_value['elements'] as $element_key => $element_value) {
248 $element = new Model_E2pdf_Element();
249 $esig = isset($element_value['properties']['esig']) && $element_value['properties']['esig'] ? true : false;
250 if (isset($element_value['type']) && ($element_value['type'] == 'e2pdf-image' || ($element_value['type'] == 'e2pdf-signature' && !$esig))) {
251 if (!$this->helper->load('image')->get_image($element_value['value'])) {
252 $element_value['value'] = plugins_url('', $this->helper->get('plugin_file_path')) . "/img/upload.png";
253 if (isset($element_value['properties']['dimension'])) {
254 unset($element_value['properties']['dimension']);
255 }
256 }
257 }
258
259 foreach ($element_value as $element_value_key => $element_value_value) {
260 $element->set($element_value_key, $element_value_value);
261 }
262
263 if (!$element_value['properties']) {
264 $element_properties = array();
265 } else {
266 $element_properties = $element_value['properties'];
267 }
268
269 $element->set('properties', $element_properties);
270 $elements[] = $element->get_element();
271 }
272
273 $page->set('elements', $elements);
274 $preview_pages[] = $page->get_page();
275 }
276
277 $template->set('pages', $preview_pages);
278
279 $allowed_types = array(
280 'php'
281 );
282
283 if (!in_array($this->get->get('type'), $allowed_types)) {
284 $type = 'php';
285 } else {
286 $type = $this->get->get('type');
287 }
288
289 $request = $template->convert(true, $type);
290
291 if (isset($request['error'])) {
292 $this->add_notification('error', $request['error']);
293 $this->render('blocks', 'notifications');
294 } else {
295 $filename = 'template';
296 $file = $request['file'];
297 $this->download_response($type, $file, $filename);
298 }
299 }
300 }
301
302 /**
303 * @url admin.php?page=e2pdf-templates&action=create
304 */
305 public function create_action() {
306
307 $template = new Model_E2pdf_Template();
308 $this->load_metaboxes();
309 $this->load_scripts();
310 $this->load_styles();
311
312 $this->view('license', $this->helper->get('license'));
313 $this->view('template', $template);
314 }
315
316 /**
317 * @url admin.php?page=e2pdf-templates&action=edit&id=$id
318 */
319 public function edit_action() {
320 if ($this->get->get('id')) {
321 $template = new Model_E2pdf_Template();
322 if ($template->load($this->get->get('id'))) {
323 $this->view('license', $this->helper->get('license'));
324 $this->view('template', $template);
325 $this->load_metaboxes();
326 $this->load_scripts();
327 $this->load_styles($template->get('extension'));
328 } else {
329 $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
330 $this->render('blocks', 'notifications');
331 }
332 } else {
333 $location = $this->helper->get_url(
334 array(
335 'page' => 'e2pdf-templates',
336 )
337 );
338 $this->redirect($location);
339 }
340 }
341
342 /**
343 * @url admin.php?page=e2pdf-templates&action=trash&id=$id
344 */
345 public function trash_action() {
346
347 if ($this->get->get('id')) {
348 $this->trash_template($this->get->get('id'));
349 $this->add_notification('update', __('Template moved to trash', 'e2pdf'));
350 }
351 $location = $this->helper->get_url(
352 array(
353 'page' => 'e2pdf-templates',
354 'status' => $this->get->get('status'),
355 'orderby' => $this->get->get('orderby'),
356 'order' => $this->get->get('order'),
357 's' => $this->get->get('s'),
358 )
359 );
360 $this->redirect($location);
361 }
362
363 /**
364 * @url admin.php?page=e2pdf-templates&action=restore&id=$id&status=$status
365 */
366 public function restore_action() {
367
368 if ($this->get->get('id')) {
369 $this->restore_template($this->get->get('id'));
370 $this->add_notification('update', __('Template restored', 'e2pdf'));
371 }
372
373 $location = $this->helper->get_url(
374 array(
375 'page' => 'e2pdf-templates',
376 'status' => $this->get->get('status'),
377 'orderby' => $this->get->get('orderby'),
378 'order' => $this->get->get('order'),
379 's' => $this->get->get('s'),
380 )
381 );
382 $this->redirect($location);
383 }
384
385 /**
386 * @url admin.php?page=e2pdf-templates&action=delete&id=$id&status=$status
387 */
388 public function delete_action() {
389
390 if ($this->get->get('id')) {
391 $this->delete_template($this->get->get('id'));
392 $this->add_notification('update', __('Template deleted', 'e2pdf'));
393 }
394 $location = $this->helper->get_url(
395 array(
396 'page' => 'e2pdf-templates',
397 'status' => $this->get->get('status'),
398 'orderby' => $this->get->get('orderby'),
399 'order' => $this->get->get('order'),
400 's' => $this->get->get('s'),
401 )
402 );
403 $this->redirect($location);
404 }
405
406 /**
407 * @url admin.php?page=e2pdf-templates&action=duplicate&id=$id
408 */
409 public function duplicate_action() {
410
411 if ($this->get->get('id')) {
412 $this->duplicate_template($this->get->get('id'));
413 $this->add_notification('update', __('Template copy created', 'e2pdf'));
414 }
415 $location = $this->helper->get_url(
416 array(
417 'page' => 'e2pdf-templates',
418 'status' => $this->get->get('status'),
419 'orderby' => $this->get->get('orderby'),
420 'order' => $this->get->get('order'),
421 's' => $this->get->get('s'),
422 )
423 );
424 $this->redirect($location);
425 }
426
427 /**
428 * @url admin.php?page=e2pdf-templates&action=import
429 */
430 public function import_action() {
431
432 if ($this->post->get()) {
433
434 $this->check_nonce($this->post->get('_nonce'), 'e2pdf_post');
435 $errors = array();
436 $allowed_ext = array(
437 'xml'
438 );
439
440 $import = $this->files->get('template');
441 $name = $import['name'];
442 $tmp = $import['tmp_name'];
443 $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
444
445 if (!$tmp) {
446 $this->add_notification('error', __('Please choose template to upload', 'e2pdf'));
447 } else if ($import['error']) {
448 $this->add_notification('error', $import['error']);
449 } elseif (!in_array($ext, $allowed_ext)) {
450 $this->add_notification('error', __('Incorrect file extension', 'e2pdf'));
451 } elseif ($import['type'] != 'text/xml') {
452 $this->add_notification('error', __('Incompatible type', 'e2pdf'));
453 } else {
454
455 $options = $this->post->get('options');
456
457 $xml = simplexml_load_file($import['tmp_name'], "SimpleXMLElement", LIBXML_PARSEHUGE);
458
459 $pages = stripslashes_deep(unserialize(base64_decode((String) $xml->template->pages)));
460
461 foreach ($pages as $page_key => $page) {
462 if (isset($page['elements']) && !empty($page['elements'])) {
463 foreach ($page['elements'] as $element_key => $element) {
464 if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) {
465 if ($options['images']) {
466 $image = base64_decode($element['base64']);
467 $file_name = basename($element['value']);
468 $upload_file = wp_upload_bits($file_name, null, $image);
469 if (!$upload_file['error']) {
470 $wp_filetype = wp_check_filetype($file_name, null);
471 $attachment = array(
472 'post_mime_type' => $wp_filetype['type'],
473 'post_parent' => 0,
474 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
475 'post_content' => '',
476 'post_status' => 'inherit'
477 );
478 $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0);
479 if (!is_wp_error($attachment_id)) {
480 require_once(ABSPATH . "wp-admin" . '/includes/image.php');
481 $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
482 wp_update_attachment_metadata($attachment_id, $attachment_data);
483 $pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url'];
484 }
485 }
486 }
487 unset($pages[$page_key]['elements'][$element_key]['base64']);
488 }
489 }
490 }
491 }
492
493 $template = new Model_E2pdf_Template();
494 if ($options['overwrite']) {
495 $template->load((String) $xml->template->ID);
496 }
497
498 $template->set('title', (String) $xml->template->title);
499 $template->set('flatten', (String) $xml->template->flatten);
500 $template->set('format', (String) $xml->template->format);
501 $template->set('compression', (String) $xml->template->compression);
502 $template->set('appearance', (String) $xml->template->appearance);
503 $template->set('width', (String) $xml->template->width);
504 $template->set('height', (String) $xml->template->height);
505 $template->set('extension', (String) $xml->template->extension);
506
507 if (!$options['overwrite']) {
508 $template->set('item', "");
509 }
510
511 $fpro2pdf_backup = false;
512
513 if ($xml->source) {
514 if ((String) $xml->source == 'fpro2pdf') {
515 $template->set('item', (String) $xml->template->item);
516 $fpro2pdf_backup = true;
517 }
518 }
519
520 $template->set('inline', (String) $xml->template->inline);
521 $template->set('auto', (String) $xml->template->auto);
522 $template->set('font', (String) $xml->template->font);
523 $template->set('font_size', (String) $xml->template->font_size);
524 $template->set('font_color', (String) $xml->template->font_color);
525 $template->set('line_height', (String) $xml->template->line_height);
526 $template->set('fonts', stripslashes_deep(unserialize(base64_decode((String) $xml->template->fonts))));
527
528 $extension = new Model_E2pdf_Extension();
529 $extension->load($template->get('extension'));
530
531 if ($options['item'] && $xml->item && !$fpro2pdf_backup) {
532 $template->set('item', (String) $xml->template->item);
533 $extension->set('item', $template->get('item'));
534 if ($updated_item = $extension->import($xml->item, (String) $xml->template->item, $options)) {
535 if (is_array($updated_item) && array_key_exists('errors', $updated_item)) {
536 $errors = array_merge($errors, $updated_item['errors']);
537 } else {
538 $template->set('item', $updated_item);
539 $extension->set('item', $updated_item);
540 }
541 }
542 }
543
544 $pages = apply_filters('e2pdf_controller_templates_import_pages', $pages, $options, $xml, $template, $extension);
545 $dataset_title = apply_filters('e2pdf_controller_templates_import_dataset_title', (String) $xml->template->dataset_title, $options, $xml, $template, $extension);
546 $button_title = apply_filters('e2pdf_controller_templates_import_button_title', (String) $xml->template->button_title, $options, $xml, $template, $extension);
547 $name = apply_filters('e2pdf_controller_templates_import_name', (String) $xml->template->name, $options, $xml, $template, $extension);
548 $password = apply_filters('e2pdf_controller_templates_import_password', (String) $xml->template->password, $options, $xml, $template, $extension);
549 $meta_title = apply_filters('e2pdf_controller_templates_import_meta_title', (String) $xml->template->meta_title, $options, $xml, $template, $extension);
550 $meta_subject = apply_filters('e2pdf_controller_templates_import_meta_subject', (String) $xml->template->meta_subject, $options, $xml, $template, $extension);
551 $meta_author = apply_filters('e2pdf_controller_templates_import_meta_author', (String) $xml->template->meta_author, $options, $xml, $template, $extension);
552 $meta_keywords = apply_filters('e2pdf_controller_templates_import_meta_keywords', (String) $xml->template->meta_keywords, $options, $xml, $template, $extension);
553
554 $template->set('pages', $pages);
555 $template->set('dataset_title', $dataset_title);
556 $template->set('button_title', $button_title);
557 $template->set('name', $name);
558 $template->set('password', $password);
559 $template->set('meta_title', $meta_title);
560 $template->set('meta_subject', $meta_subject);
561 $template->set('meta_author', $meta_author);
562 $template->set('meta_keywords', $meta_keywords);
563
564 if ($options['fonts'] && $xml->fonts) {
565 $model_e2pdf_font = new Model_E2pdf_Font();
566 $fonts = $model_e2pdf_font->get_fonts();
567
568 if ($xml->fonts) {
569 foreach ($xml->fonts->children() as $key => $font) {
570 $title = (String) $font->title;
571 $name = (String) $font->name;
572 $value = (String) $font->value;
573
574 $exist = array_search($title, $fonts);
575 if (!$exist) {
576 if (!file_exists($this->helper->get('fonts_dir') . $name)) {
577 $f_name = $name;
578 } else {
579 $i = 0;
580 do {
581 $f_name = $i . '_' . $name;
582 $i++;
583 } while (file_exists($this->helper->get('fonts_dir') . $f_name));
584 }
585 file_put_contents($this->helper->get('fonts_dir') . $f_name, base64_decode($value));
586 }
587 }
588 }
589 }
590
591 if ($xml->pdf && $xml->pdf->source) {
592
593 $pdf_name = md5(time());
594 $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/";
595 $pdf_images_dir = $pdf_dir . "images/";
596
597 $this->helper->create_dir($pdf_dir);
598 $this->helper->create_dir($pdf_images_dir);
599
600 $pdf_source = (String) $xml->pdf->source;
601 file_put_contents($pdf_dir . $pdf_name . ".pdf", base64_decode($pdf_source));
602
603 if ($xml->pdf->images) {
604 foreach ($xml->pdf->images->children() as $key => $image) {
605 $page_id = (String) $image->page_id;
606 $image_source = (String) $image->source;
607 file_put_contents($pdf_dir . "images/" . $page_id . ".png", base64_decode($image_source));
608 }
609 }
610 $template->set('pdf', $pdf_name);
611 }
612
613 if (empty($errors)) {
614 $template->save(true);
615
616 if ($options['item'] && (String) $xml->template->item != $template->get('item') && $template->get('ID') != (String) $xml->template->ID) {
617 $extension->after_import((String) $xml->template->ID, $template->get('ID'));
618 }
619
620 $this->add_notification('update', __('Template imported successfully', 'e2pdf'));
621 } else {
622 foreach ($errors as $key => $error) {
623 $this->add_notification('error', __($error, 'e2pdf'));
624 }
625 }
626
627 unlink($import['tmp_name']);
628 }
629 }
630
631 $this->view('import_disabled', false);
632 if (!$this->helper->load('xml')->check()) {
633 $this->add_notification('error', sprintf(__("PHP Extension <strong>%s</strong> not found", 'e2pdf'), 'SimpleXml'));
634 $this->view('import_disabled', true);
635 }
636
637 $options = array(
638 'common' => array(
639 'name' => __('Template Options', 'e2pdf'),
640 'options' => array(
641 array(
642 'name' => __('Overwrite template by ID', 'e2pdf'),
643 'key' => 'options[overwrite]',
644 'value' => '0',
645 'default_value' => '0',
646 'type' => 'checkbox',
647 'placeholder' => '',
648 ),
649 array(
650 'name' => __('Import Images', 'e2pdf'),
651 'key' => 'options[images]',
652 'value' => '1',
653 'default_value' => '0',
654 'type' => 'checkbox',
655 'placeholder' => '',
656 ),
657 array(
658 'name' => __('Import Fonts', 'e2pdf'),
659 'key' => 'options[fonts]',
660 'value' => '1',
661 'default_value' => '0',
662 'type' => 'checkbox',
663 'placeholder' => '',
664 )
665 )
666 ),
667 'item' => array(
668 'name' => __('Item Options', 'e2pdf'),
669 'options' => array(
670 array(
671 'name' => __('Import Item', 'e2pdf'),
672 'key' => 'options[item]',
673 'value' => '0',
674 'default_value' => '0',
675 'type' => 'checkbox',
676 'placeholder' => '',
677 'class' => 'e2pdf-collapse',
678 'data-collapse' => 'e2pdf-import-extension-option'
679 ),
680 )
681 )
682 );
683
684 $options = apply_filters('e2pdf_controller_templates_import_options', $options);
685 $this->view('options', $options);
686 $this->view('upload_max_filesize', $this->helper->load('files')->get_upload_max_filesize());
687 }
688
689 /**
690 * @url admin.php?page=e2pdf-templates&action=backup&id=$id
691 */
692 public function backup_action() {
693
694 if ($this->post->get()) {
695
696 $this->check_nonce($this->post->get('_nonce'), 'e2pdf_post');
697
698 if ($this->helper->load('xml')->check()) {
699
700 $options = $this->post->get('options');
701 $xml = $this->helper->load('xml')->create('backup');
702
703 $template = new Model_E2pdf_Template();
704 if ($template->load($this->post->get('id'))) {
705
706 if ($template->get('title') != '') {
707 $filename = $template->get('title') . "." . date('Y-m-d.H-i-s') . ".e2pdf";
708 } else {
709 $filename = date('Y-m-d.H-i-s') . ".e2pdf";
710 }
711
712 $pages = $template->get('pages');
713 if ($options['images']) {
714 foreach ($pages as $page_key => $page) {
715 if (isset($page['elements']) && !empty($page['elements'])) {
716 foreach ($page['elements'] as $element_key => $element) {
717 if ($element['type'] === 'e2pdf-image') {
718 $pages[$page_key]['elements'][$element_key]['base64'] = $this->helper->load('image')->get_image($element['value']);
719 }
720 }
721 }
722 }
723 }
724
725 $pages = apply_filters('e2pdf_controller_templates_backup_pages', $pages, $options, $template, $template->extension());
726 $dataset_title = apply_filters('e2pdf_controller_templates_backup_dataset_title', $template->get('dataset_title'), $options, $template, $template->extension());
727 $button_title = apply_filters('e2pdf_controller_templates_backup_button_title', $template->get('button_title'), $options, $template, $template->extension());
728 $name = apply_filters('e2pdf_controller_templates_backup_name', $template->get('name'), $options, $template, $template->extension());
729 $password = apply_filters('e2pdf_controller_templates_backup_password', $template->get('password'), $options, $template, $template->extension());
730 $meta_title = apply_filters('e2pdf_controller_templates_backup_meta_title', $template->get('meta_title'), $options, $template, $template->extension());
731 $meta_subject = apply_filters('e2pdf_controller_templates_backup_meta_subject', $template->get('meta_subject'), $options, $template, $template->extension());
732 $meta_author = apply_filters('e2pdf_controller_templates_backup_meta_author', $template->get('meta_author'), $options, $template, $template->extension());
733 $meta_keywords = apply_filters('e2pdf_controller_templates_backup_meta_keywords', $template->get('meta_keywords'), $options, $template, $template->extension());
734
735 // build xml
736 $xml->addChildCData('version', $this->helper->get('version'));
737 $xml->addChildCData('date', date('Y-m-d H:i:s'));
738
739 $options_xml = $xml->addChild('options');
740 foreach ($options as $option_key => $option) {
741 $options_xml->addChildCData($option_key, $option);
742 }
743
744 $template_xml = $xml->addChild('template');
745 $template_xml->addChildCData('ID', $template->get('ID'));
746 $template_xml->addChildCData('title', $template->get('title'));
747 $template_xml->addChildCData('created_at', $template->get('created_at'));
748 $template_xml->addChildCData('updated_at', $template->get('updated_at'));
749 $template_xml->addChildCData('flatten', $template->get('flatten'));
750 $template_xml->addChildCData('format', $template->get('format'));
751 $template_xml->addChildCData('compression', $template->get('compression'));
752 $template_xml->addChildCData('appearance', $template->get('appearance'));
753 $template_xml->addChildCData('width', $template->get('width'));
754 $template_xml->addChildCData('height', $template->get('height'));
755 $template_xml->addChildCData('extension', $template->get('extension'));
756 $template_xml->addChildCData('item', $template->get('item'));
757 $template_xml->addChildCData('dataset_title', $dataset_title);
758 $template_xml->addChildCData('button_title', $button_title);
759 $template_xml->addChildCData('inline', $template->get('inline'));
760 $template_xml->addChildCData('auto', $template->get('auto'));
761 $template_xml->addChildCData('name', $name);
762 $template_xml->addChildCData('password', $password);
763 $template_xml->addChildCData('meta_title', $meta_title);
764 $template_xml->addChildCData('meta_subject', $meta_subject);
765 $template_xml->addChildCData('meta_author', $meta_author);
766 $template_xml->addChildCData('meta_keywords', $meta_keywords);
767 $template_xml->addChildCData('font', $template->get('font'));
768 $template_xml->addChildCData('font_size', $template->get('font_size'));
769 $template_xml->addChildCData('font_color', $template->get('font_color'));
770 $template_xml->addChildCData('line_height', $template->get('line_height'));
771 $template_xml->addChildCData('fonts', base64_encode(serialize($template->get('fonts'))));
772 $template_xml->addChildCData('pages', base64_encode(serialize($pages)));
773
774 //include pdf
775 if ($template->get('pdf')) {
776 $pdf_xml = $xml->addChild('pdf');
777 $pdf_xml->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . "/" . $template->get('pdf') . ".pdf")));
778 $pdf_xml_images = $pdf_xml->addChild('images');
779 foreach ($template->get('pages') as $key => $page) {
780 $pdf_xml_image = $pdf_xml_images->addChild('image');
781 $pdf_xml_image->addChildCData('page_id', $page['page_id']);
782 $pdf_xml_image->addChildCData('source', base64_encode(file_get_contents($this->helper->get('pdf_dir') . $template->get('pdf') . "/images/" . $page['page_id'] . ".png")));
783 }
784 }
785
786 //include fonts
787 if ($options['fonts']) {
788 $fonts_xml = $xml->addChild('fonts');
789 if ($template->get('fonts')) {
790 $model_e2pdf_font = new Model_E2pdf_Font();
791 foreach ($template->get('fonts') as $key => $value) {
792 $font = $fonts_xml->addChild('font');
793 $font->addChildCData('title', $value);
794 $font->addChildCData('name', $key);
795 $font->addChildCData('value', $model_e2pdf_font->get_font($key));
796 }
797 }
798 }
799
800 //include item
801 if ($options['item']) {
802 if ($template->extension()->method('backup')) {
803 $item = $xml->addChild('item');
804 $template->extension()->backup($item);
805 }
806 }
807
808 $file = $this->helper->load('xml')->get_xml();
809 $this->download_response('xml', $file, $filename);
810 } else {
811 $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
812 $this->render('blocks', 'notifications');
813 }
814 }
815 }
816
817 $this->view('export_disabled', false);
818
819 if (!$this->helper->load('xml')->check()) {
820 $this->add_notification('error', sprintf(__("PHP Extension <strong>%s</strong> not found", 'e2pdf'), 'SimpleXml'));
821 $this->view('export_disabled', true);
822 }
823
824 $template = new Model_E2pdf_Template();
825 if ($template->load($this->get->get('id'))) {
826
827 $this->view('template', $template);
828
829 $options = array(
830 'common' => array(
831 'name' => __('Options', 'e2pdf'),
832 'options' => array(
833 array(
834 'name' => __('Include Item', 'e2pdf'),
835 'key' => 'options[item]',
836 'value' => $template->extension()->method('backup') ? 1 : 0,
837 'default_value' => 0,
838 'type' => 'checkbox',
839 'placeholder' => '',
840 'disabled' => $template->extension()->method('backup') ? false : 'disabled'
841 ),
842 array(
843 'name' => __('Include Images', 'e2pdf'),
844 'key' => 'options[images]',
845 'value' => 1,
846 'default_value' => 0,
847 'type' => 'checkbox',
848 'placeholder' => '',
849 ),
850 array(
851 'name' => __('Include Fonts', 'e2pdf'),
852 'key' => 'options[fonts]',
853 'value' => 1,
854 'default_value' => 0,
855 'type' => 'checkbox',
856 'placeholder' => '',
857 )
858 )
859 )
860 );
861
862 $options = apply_filters('e2pdf_controller_templates_backup_options', $options, $template, $template->extension());
863 $this->view('options', $options);
864 } else {
865 $this->add_notification('error', __("Template can't be loaded", 'e2pdf'));
866 $this->render('blocks', 'notifications');
867 }
868 }
869
870 public function screen_action() {
871 if ($this->post->get()) {
872 $this->check_nonce($this->post->get('screenoptionnonce'), 'screen-options-nonce');
873 $option = $this->post->get('wp_screen_options');
874 if (is_array($option) && isset($option['option']) && isset($option['value']) && $option['value']) {
875 update_option($option['option'], $option['value']);
876 }
877 }
878 $location = $this->helper->get_url(
879 array(
880 'page' => 'e2pdf-templates',
881 )
882 );
883 $this->redirect($location);
884 }
885
886 /**
887 * Save template via ajax
888 * action: wp_ajax_e2pdf_save_form
889 * function: e2pdf_save_form
890 *
891 * @return json
892 */
893 public function ajax_save_form() {
894 global $wpdb;
895 $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
896
897 $data = json_decode($this->post->get('data'), true);
898
899 $template = new Model_E2pdf_Template();
900 if (isset($data['ID'])) {
901 $template->load($data['ID']);
902 if ($template->get('pdf') && $template->get('pdf') !== $data['pdf']) {
903 $old_pdf_path = $this->helper->get('pdf_dir') . $template->get('pdf') . "/";
904 $this->helper->delete_dir($old_pdf_path);
905 }
906 }
907
908 foreach ($data as $key => $value) {
909 $template->set($key, $value);
910 }
911 $template_id = $template->save(true);
912
913 if (!$template_id) {
914 if ($wpdb->last_error) {
915 $response = array(
916 'error' => $wpdb->last_error
917 );
918 } else {
919 $response = array(
920 'error' => __("Something went wrong!", "e2pdf")
921 );
922 }
923 } else {
924 if (isset($data['item']) && $data['item'] == '-1' && $template_id) {
925 $template->load($template_id);
926 if ($template->extension()->method('auto_form')) {
927 $template = $template->extension()->auto_form($template, $data);
928 } else {
929 $template->set('item', '');
930 }
931 $template->save(true);
932 }
933
934 if ($template->get('activated')) {
935 $this->activate_template($template->get('ID'));
936 } elseif (!$template->get('activated')) {
937 $this->deactivate_template($template->get('ID'));
938 }
939
940 $response = array(
941 'redirect' => $this->helper->get_url(array(
942 'page' => 'e2pdf-templates',
943 'action' => 'edit',
944 'id' => $template->get('ID'))
945 )
946 );
947
948 $this->add_notification('update', __('Template saved successfully', 'e2pdf'));
949 }
950
951 $this->json_response($response);
952 }
953
954 public function ajax_get_styles() {
955
956 $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
957 $data = $this->post->get('data');
958
959 $extension = new Model_E2pdf_Extension();
960 if ($extension->load($data)) {
961 $content = $extension->styles();
962 } else {
963 $content = array();
964 }
965 $response = array(
966 'content' => $content,
967 );
968 $this->json_response($response);
969 }
970
971 /**
972 * Get extensions via ajax
973 * action: wp_ajax_e2pdf_extension
974 * function: e2pdf_extension
975 *
976 * @return json
977 */
978 public function ajax_extension() {
979
980 $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
981 $data = $this->post->get('data');
982
983 $extension = new Model_E2pdf_Extension();
984
985 $content = array();
986 if ($extension->load($data)) {
987 $content = $extension->items();
988
989 if ($extension->method('auto_form')) {
990 $select = new stdClass();
991 $select->id = '-1';
992 $select->url = 'javascript:void(0);';
993 $select->name = __("Auto Form from PDF", "e2pdf");
994
995 array_unshift($content, $select);
996 }
997 }
998
999 $select = new stdClass();
1000 $select->id = '';
1001 $select->url = 'javascript:void(0);';
1002 $select->name = __("--- Select ---", "e2pdf");
1003
1004 array_unshift($content, $select);
1005
1006 $response = array(
1007 'content' => $content,
1008 );
1009 $this->json_response($response);
1010 }
1011
1012 /**
1013 * Get extensions via ajax
1014 * action: wp_ajax_e2pdf_extension
1015 * function: e2pdf_extension
1016 *
1017 * @return json
1018 */
1019 public function ajax_visual_mapper() {
1020
1021 $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1022
1023 $data = $this->post->get('data');
1024
1025 $item = isset($data['item']) ? $data['item'] : '';
1026
1027 $extension = new Model_E2pdf_Extension();
1028 $extension->load($data['extension']);
1029 $extension->set('item', $item);
1030
1031 if ($visual_mapper = $extension->visual_mapper()) {
1032 $response = array(
1033 'content' => $visual_mapper
1034 );
1035 } elseif (!$item) {
1036 $response = array(
1037 'content' => __("You must set Item to use <strong>Visual Mapper</strong>", "e2pdf"),
1038 );
1039 } else {
1040 $response = array(
1041 'content' => __("Sorry, this extension doesn't support <strong>Visual Mapper</strong>", "e2pdf"),
1042 );
1043 }
1044
1045 $this->json_response($response);
1046 }
1047
1048 /**
1049 * Upload Template via Ajax
1050 * action: wp_ajax_e2pdf_upload
1051 * function: e2pdf_upload
1052 *
1053 * @return json
1054 */
1055 public function ajax_upload() {
1056
1057 $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1058
1059 $data = $this->post->get();
1060
1061 $post_extension = $data['extension'];
1062 $item = isset($data['item']) ? $data['item'] : '';
1063 $template_id = isset($data['template_id']) && $data['template_id'] ? $data['template_id'] : false;
1064
1065 $font = isset($data['font']) ? $data['font'] : false;
1066 $font_size = isset($data['font_size']) ? $data['font_size'] : false;
1067 $line_height = isset($data['line_height']) ? $data['line_height'] : false;
1068 $activated = isset($data['activated']) ? $data['activated'] : 0;
1069 $title = isset($data['title']) ? $data['title'] : __('(no title)', 'e2pdf');
1070
1071 $allowed_ext = array(
1072 'pdf'
1073 );
1074
1075 $pdf = $this->files->get('pdf');
1076
1077 $name = strtolower($pdf['name']);
1078 $tmp = $pdf['tmp_name'];
1079 $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
1080
1081 if ($pdf['error']) {
1082 $error = $pdf['error'];
1083 } elseif (!in_array($ext, $allowed_ext)) {
1084 $error = __('Incorrect file extension', 'e2pdf');
1085 } elseif ($pdf['type'] != 'application/pdf') {
1086 $error = __('Incompatible type', 'e2pdf');
1087 }
1088
1089 if ($error) {
1090 $response = array(
1091 'error' => $error,
1092 );
1093 } else {
1094
1095 $model_e2pdf_api = new Model_E2pdf_Api();
1096 $model_e2pdf_api->set(array(
1097 'action' => 'template/upload',
1098 'data' => array(
1099 'title' => $name,
1100 'pdf' => base64_encode(file_get_contents($pdf['tmp_name']))
1101 )
1102 ));
1103 $request = $model_e2pdf_api->request();
1104
1105 if (!isset($request['error'])) {
1106
1107 $extension = new Model_E2pdf_Extension();
1108 if ($post_extension) {
1109 $extension->load($post_extension);
1110 if ($item && $item != '-1') {
1111 $extension->set('item', $item);
1112 }
1113 }
1114
1115 $pdf_name = md5(time());
1116 $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/";
1117 $pdf_images_dir = $pdf_dir . "images/";
1118
1119 $this->helper->create_dir($pdf_dir);
1120 $this->helper->create_dir($pdf_images_dir);
1121
1122 move_uploaded_file($pdf['tmp_name'], $pdf_dir . $pdf_name . ".pdf");
1123
1124 $xml = simplexml_load_string(base64_decode($request['file']), "SimpleXMLElement", LIBXML_PARSEHUGE);
1125 $pages = stripslashes_deep(unserialize(base64_decode((String) $xml->template->pages)));
1126
1127 if (is_array($pages) && !empty($pages)) {
1128 foreach ($pages as $page_key => $page) {
1129
1130 $pages[$page_key]['page_id'] = $page_key;
1131
1132 if (isset($page['properties']['background'])) {
1133 file_put_contents($pdf_images_dir . $page_key . ".png", base64_decode($page['properties']['background']));
1134 unset($pages[$page_key]['properties']['background']);
1135 }
1136
1137 if (isset($page['elements']) && !empty($page['elements'])) {
1138 foreach ($page['elements'] as $element_key => $element) {
1139 if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) {
1140
1141 $image = base64_decode($element['base64']);
1142 $file_name = basename($element['value']);
1143
1144 //exif_imagetype
1145 if (!$file_name) {
1146 $ext = $this->helper->load('image')->get_image_extension($image);
1147 if ($ext) {
1148 $file_name = md5(mktime()) . "." . $ext;
1149 }
1150 }
1151
1152 if ($file_name) {
1153 $upload_file = wp_upload_bits($file_name, null, $image);
1154 if (!$upload_file['error']) {
1155 $wp_filetype = wp_check_filetype($file_name, null);
1156 $attachment = array(
1157 'post_mime_type' => $wp_filetype['type'],
1158 'post_parent' => 0,
1159 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
1160 'post_content' => '',
1161 'post_status' => 'inherit'
1162 );
1163 $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0);
1164 if (!is_wp_error($attachment_id)) {
1165 require_once(ABSPATH . "wp-admin" . '/includes/image.php');
1166 $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
1167 wp_update_attachment_metadata($attachment_id, $attachment_data);
1168 $pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url'];
1169 }
1170 }
1171
1172 unset($pages[$page_key]['elements'][$element_key]['base64']);
1173 } else {
1174 unset($pages[$page_key]['elements'][$element_key]);
1175 }
1176 } elseif (isset($element['name']) && $element['name']) {
1177 $el_value = $extension->auto_map($element['name']);
1178 if ($el_value !== false) {
1179 $pages[$page_key]['elements'][$element_key]['value'] = $el_value;
1180 }
1181 }
1182 }
1183 }
1184 }
1185
1186 $this->helper->load('sort')->stable_uasort($pages, "sort_by_pageid");
1187
1188 $template = new Model_E2pdf_Template();
1189
1190 if ($template_id) {
1191 $template->load($template_id);
1192 if ($template->get('pdf')) {
1193 $old_pdf_path = $this->helper->get('pdf_dir') . $template->get('pdf') . "/";
1194 $this->helper->delete_dir($old_pdf_path);
1195 }
1196 $template->set('title', $title);
1197 $template->set('pages', $pages);
1198 $template->set('pdf', $pdf_name);
1199 $template->set('width', (String) $xml->template->width);
1200 $template->set('height', (String) $xml->template->height);
1201 $template->set('extension', $post_extension);
1202 $template->set('item', $item);
1203 $template->set('font', $font ? $font : (String) $xml->template->font);
1204 $template->set('font_size', $font_size ? $font_size : (String) $xml->template->font_size);
1205 $template->set('line_height', $line_height ? $line_height : (String) $xml->template->font_size);
1206 } else {
1207 $template->set('title', $title);
1208 $template->set('flatten', (String) $xml->template->flatten);
1209 $template->set('format', (String) $xml->template->format);
1210 $template->set('compression', (String) $xml->template->compression);
1211 $template->set('appearance', (String) $xml->template->appearance);
1212 $template->set('width', (String) $xml->template->width);
1213 $template->set('height', (String) $xml->template->height);
1214 $template->set('extension', $post_extension);
1215 $template->set('item', $item);
1216 $template->set('dataset_title', "");
1217 $template->set('button_title', "");
1218 $template->set('inline', "");
1219 $template->set('auto', "");
1220 $template->set('name', "");
1221 $template->set('password', "");
1222 $template->set('meta_title', (String) $xml->template->meta_title);
1223 $template->set('meta_subject', (String) $xml->template->meta_subject);
1224 $template->set('meta_author', (String) $xml->template->meta_author);
1225 $template->set('meta_keywords', (String) $xml->template->meta_keywords);
1226 $template->set('font', $font ? $font : (String) $xml->template->font);
1227 $template->set('font_size', $font_size ? $font_size : (String) $xml->template->font_size);
1228 $template->set('line_height', $line_height ? $line_height : (String) $xml->template->font_size);
1229 $template->set('font_color', (String) $xml->template->font_color);
1230 $template->set('pages', $pages);
1231 $template->set('pdf', $pdf_name);
1232 }
1233
1234 if ($xml->fonts) {
1235
1236 $model_e2pdf_font = new Model_E2pdf_Font();
1237 $fonts = $model_e2pdf_font->get_fonts();
1238
1239 foreach ($xml->fonts->children() as $key => $font) {
1240 $font_title = (String) $font->title;
1241 $name = (String) $font->name;
1242 $value = (String) $font->value;
1243
1244 $exist = array_search($font_title, $fonts);
1245 if (!$exist) {
1246 if (!file_exists($this->helper->get('fonts_dir') . $name)) {
1247 $f_name = $name;
1248 } else {
1249 $i = 0;
1250 do {
1251 $f_name = $i . '_' . $name;
1252 $i++;
1253 } while (file_exists($this->helper->get('fonts_dir') . $f_name));
1254 }
1255 file_put_contents($this->helper->get('fonts_dir') . $f_name, base64_decode($value));
1256 }
1257 }
1258 }
1259 $template->save(true);
1260
1261 if ($item == '-1' && $extension->method('auto_form')) {
1262 $template = $extension->auto_form($template, $data);
1263 $template->save(true);
1264 }
1265
1266 if ($activated && !$template->get('activated')) {
1267 $this->activate_template($template->get('ID'));
1268 } elseif (!$activated && $template->get('activated')) {
1269 $this->deactivate_template($template->get('ID'));
1270 }
1271
1272 $response = array(
1273 'redirect' => $this->helper->get_url(array(
1274 'page' => 'e2pdf-templates',
1275 'action' => 'edit',
1276 'id' => $template->get('ID'))
1277 )
1278 );
1279 } else {
1280 $this->helper->delete_dir($pdf_dir);
1281 $response = array(
1282 'error' => __("PDF can't be parsed", 'e2pdf'),
1283 );
1284 }
1285 } else {
1286 $response = array(
1287 'error' => $request['error'],
1288 );
1289 }
1290 }
1291
1292 $this->json_response($response);
1293 }
1294
1295 /**
1296 * Upload Template via Ajax
1297 * action: wp_ajax_e2pdf_upload
1298 * function: e2pdf_upload
1299 *
1300 * @return json
1301 */
1302 public function ajax_reupload() {
1303
1304 $error = false;
1305 $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1306 $data = $this->post->get();
1307
1308 $new = isset($data['new']) ? $data['new'] : array();
1309 $flush = isset($data['flush']) ? $data['flush'] : array();
1310 $positions = isset($data['positions']) ? $data['positions'] : array();
1311 $template_id = isset($data['template_id']) && $data['template_id'] ? $data['template_id'] : false;
1312
1313 if (!$template_id) {
1314 return false;
1315 }
1316
1317 $allowed_ext = array(
1318 'pdf'
1319 );
1320
1321 $pdf = $this->files->get('pdf');
1322
1323 $name = strtolower($pdf['name']);
1324 $tmp = $pdf['tmp_name'];
1325 $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
1326
1327 if ($pdf['error']) {
1328 $error = $pdf['error'];
1329 } elseif (!in_array($ext, $allowed_ext)) {
1330 $error = __('Incorrect file extension', 'e2pdf');
1331 } elseif ($pdf['type'] != 'application/pdf') {
1332 $error = __('Incompatible type', 'e2pdf');
1333 }
1334
1335 if ($error) {
1336 $response = array(
1337 'error' => $error,
1338 );
1339 } else {
1340
1341 $model_e2pdf_api = new Model_E2pdf_Api();
1342 $model_e2pdf_api->set(array(
1343 'action' => 'template/upload',
1344 'data' => array(
1345 'title' => $name,
1346 'pdf' => base64_encode(file_get_contents($pdf['tmp_name']))
1347 )
1348 ));
1349 $request = $model_e2pdf_api->request();
1350
1351 if (!isset($request['error'])) {
1352 $template = new Model_E2pdf_Template();
1353 if ($template->load($template_id)) {
1354 if ($template->get('pdf')) {
1355 $extension = new Model_E2pdf_Extension();
1356 if ($template->get('extension') && $template->get('item')) {
1357 $extension->load($template->get('extension'));
1358 $extension->set('item', $template->get('item'));
1359 }
1360
1361 $xml = simplexml_load_string(base64_decode($request['file']), "SimpleXMLElement", LIBXML_PARSEHUGE);
1362 $pages = stripslashes_deep(unserialize(base64_decode((String) $xml->template->pages)));
1363
1364 foreach ($pages as $page_key => $page) {
1365 $pages[$page_key]['page_id'] = $page_key;
1366 }
1367
1368 $this->helper->load('sort')->stable_uasort($pages, "sort_by_pageid");
1369
1370 $model_e2pdf_element = new Model_E2pdf_Element();
1371 $last_element_id = $model_e2pdf_element->get_last_element_id($template_id);
1372
1373 $original_pages = $template->get('pages');
1374
1375 foreach ($positions as $pos_key => $pos_page) {
1376 if (!in_array($pos_key, $flush)) {
1377 if (!isset($pages[$pos_page])) {
1378 $error = sprintf(__("Position of %s page incorrect", 'e2pdf'), $pos_key);
1379 } elseif ($pages[$pos_page]['properties']['width'] < $original_pages[$pos_key]['properties']['width']) {
1380 $error = sprintf(__("Width of PDF Page %s can't be less width of Template Page %s", 'e2pdf'), $pos_page, $pos_key);
1381 } elseif ($pages[$pos_page]['properties']['height'] < $original_pages[$pos_key]['properties']['height']) {
1382 $error = sprintf(__("Height of PDF Page %s can't be less height of Template Page %s", 'e2pdf'), $pos_page, $pos_key);
1383 }
1384 }
1385 }
1386
1387 if (!$error) {
1388
1389 $pdf_name = md5(time());
1390 $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/";
1391 $pdf_images_dir = $pdf_dir . "images/";
1392
1393 $this->helper->create_dir($pdf_dir);
1394 $this->helper->create_dir($pdf_images_dir);
1395
1396 move_uploaded_file($pdf['tmp_name'], $pdf_dir . $pdf_name . ".pdf");
1397
1398 if (is_array($pages) && !empty($pages)) {
1399 foreach ($pages as $page_key => $page) {
1400
1401 if (isset($page['properties']['background'])) {
1402 file_put_contents($pdf_images_dir . $page_key . ".png", base64_decode($page['properties']['background']));
1403 unset($pages[$page_key]['properties']['background']);
1404 }
1405
1406 if (isset($page['elements']) && !empty($page['elements'])) {
1407 foreach ($page['elements'] as $element_key => $element) {
1408
1409 $last_element_id++;
1410 $pages[$page_key]['elements'][$element_key]['element_id'] = $last_element_id;
1411
1412 if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) {
1413
1414 $image = base64_decode($element['base64']);
1415 $file_name = basename($element['value']);
1416
1417 //exif_imagetype
1418 if (!$file_name) {
1419 $ext = $this->helper->load('image')->get_image_extension($image);
1420 if ($ext) {
1421 $file_name = md5(mktime()) . "." . $ext;
1422 }
1423 }
1424
1425 if ($file_name) {
1426 $upload_file = wp_upload_bits($file_name, null, $image);
1427 if (!$upload_file['error']) {
1428 $wp_filetype = wp_check_filetype($file_name, null);
1429 $attachment = array(
1430 'post_mime_type' => $wp_filetype['type'],
1431 'post_parent' => 0,
1432 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name),
1433 'post_content' => '',
1434 'post_status' => 'inherit'
1435 );
1436 $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0);
1437 if (!is_wp_error($attachment_id)) {
1438 require_once(ABSPATH . "wp-admin" . '/includes/image.php');
1439 $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
1440 wp_update_attachment_metadata($attachment_id, $attachment_data);
1441 $pages[$page_key]['elements'][$element_key]['value'] = $upload_file['url'];
1442 }
1443 }
1444 unset($pages[$page_key]['elements'][$element_key]['base64']);
1445 } else {
1446 unset($pages[$page_key]['elements'][$element_key]);
1447 }
1448 } elseif (isset($element['name']) && $element['name']) {
1449 $el_value = $extension->auto_map($element['name']);
1450 if ($el_value !== false) {
1451 $pages[$page_key]['elements'][$element_key]['value'] = $el_value;
1452 }
1453 }
1454 }
1455 }
1456
1457 foreach ($positions as $pos_key => $pos_page) {
1458 if ($pos_page == $page_key) {
1459 if (in_array($pos_key, $new)) {
1460 if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) {
1461 $elements = $original_pages[$pos_key]['elements'];
1462 $pages[$page_key]['elements'] = array_merge($pages[$page_key]['elements'], $elements);
1463 }
1464 } else {
1465 if (isset($original_pages[$pos_key]['elements']) && !in_array($pos_key, $flush)) {
1466 $elements = $original_pages[$pos_key]['elements'];
1467 $pages[$page_key]['elements'] = $elements;
1468 } else {
1469 $pages[$page_key]['elements'] = array();
1470 }
1471 }
1472 if (isset($original_pages[$pos_key]['actions'])) {
1473 $pages[$page_key]['actions'] = $original_pages[$pos_key]['actions'];
1474 }
1475 }
1476 }
1477 }
1478
1479 $pdf_path = $this->helper->get('pdf_dir') . $template->get('pdf') . "/";
1480 $this->helper->delete_dir($pdf_path);
1481
1482 $template->set('pdf', $pdf_name);
1483 $template->set('pages', $pages);
1484 $template->save(true);
1485
1486 $response = array(
1487 'redirect' => $this->helper->get_url(array(
1488 'page' => 'e2pdf-templates',
1489 'action' => 'edit',
1490 'id' => $template->get('ID'))
1491 )
1492 );
1493 } else {
1494 $this->helper->delete_dir($pdf_dir);
1495 $response = array(
1496 'error' => __("PDF can't be parsed", 'e2pdf'),
1497 );
1498 }
1499 } else {
1500 $response = array(
1501 'error' => $error,
1502 );
1503 }
1504 }
1505 }
1506 } else {
1507 $response = array(
1508 'error' => $request['error'],
1509 );
1510 }
1511 }
1512
1513 $this->json_response($response);
1514 }
1515
1516 /**
1517 * Auto Generation of Template
1518 * action: wp_ajax_e2pdf_auto
1519 * function: e2pdf_auto
1520 *
1521 * @return json
1522 */
1523 public function ajax_auto() {
1524 $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1525 $data = $this->post->get('data');
1526
1527 $extension = $data['extension'];
1528 $item = $data['item'];
1529
1530 if (!$extension || !$item) {
1531 return;
1532 }
1533
1534 $model_e2pdf_extension = new Model_E2pdf_Extension();
1535 $model_e2pdf_extension->load($extension);
1536 $model_e2pdf_extension->set('item', $item);
1537
1538 if (method_exists($model_e2pdf_extension->extension(), 'auto')) {
1539 $content = $model_e2pdf_extension->auto();
1540 } else {
1541 $content = array();
1542 }
1543
1544 $response = array(
1545 'content' => $content,
1546 );
1547 $this->json_response($response);
1548 }
1549
1550 public function ajax_activate_template() {
1551
1552 $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1553 $data = $this->post->get('data');
1554
1555 $id = $data['id'];
1556
1557 if (!$id) {
1558 return;
1559 }
1560
1561 $request = $this->activate_template($id);
1562 if (isset($request['error'])) {
1563 $response = array(
1564 'error' => $request['error'],
1565 );
1566 } else {
1567 $response = array(
1568 'content' => true,
1569 );
1570 }
1571
1572
1573 $this->json_response($response);
1574 }
1575
1576 public function ajax_deactivate_template() {
1577 $this->check_nonce($this->get->get('_nonce'), 'e2pdf_ajax');
1578 $data = $this->post->get('data');
1579
1580 $id = $data['id'];
1581
1582 if (!$id) {
1583 return;
1584 }
1585
1586 $request = $this->deactivate_template($id);
1587 if (isset($request['error'])) {
1588 $response = array(
1589 'error' => $request['error'],
1590 );
1591 } else {
1592 $response = array(
1593 'content' => true,
1594 );
1595 }
1596 $this->json_response($response);
1597 }
1598
1599 /**
1600 * Move template to trash
1601 *
1602 * @param int $id - ID of template
1603 */
1604 public function trash_template($id = false) {
1605
1606 $id = (int) $id;
1607
1608 $template = new Model_E2pdf_Template();
1609 if ($id && $template->load($id)) {
1610 $template->set('trash', '1');
1611 $template->save();
1612 $this->deactivate_template($template->get('ID'));
1613 return true;
1614 }
1615 return false;
1616 }
1617
1618 /**
1619 * Delete template
1620 *
1621 * @param int $id - ID of template
1622 */
1623 public function delete_template($id = false) {
1624
1625 $id = (int) $id;
1626
1627 $template = new Model_E2pdf_Template();
1628 if ($id && $template->load($id)) {
1629 $this->deactivate_template($template->get('ID'));
1630 $template->delete();
1631 return true;
1632 }
1633 return false;
1634 }
1635
1636 /**
1637 * Restore template
1638 *
1639 * @param int $id - ID of template
1640 */
1641 public function restore_template($id = false) {
1642
1643 $id = (int) $id;
1644
1645 $template = new Model_E2pdf_Template();
1646 if ($id && $template->load($id)) {
1647 $template->set('trash', '0');
1648 $template->save();
1649 return true;
1650 }
1651 return false;
1652 }
1653
1654 /**
1655 * Duplicate template
1656 *
1657 * @param int $id - ID of template
1658 */
1659 public function duplicate_template($id = false) {
1660
1661 $id = (int) $id;
1662
1663 $template = new Model_E2pdf_Template();
1664 if ($id && $template->load($id)) {
1665 $title = $template->get('title') . " " . __('(Copy)', 'e2pdf');
1666 $template->set('ID', false);
1667 $template->set('uid', '');
1668 $template->set('title', $title);
1669 $template->set('activated', '0');
1670
1671 if ($template->get('pdf')) {
1672 $pdf_name = md5(time());
1673 $pdf_dir = $this->helper->get('pdf_dir') . $pdf_name . "/";
1674 $pdf_images_dir = $pdf_dir . "images/";
1675
1676 $this->helper->create_dir($pdf_dir);
1677 $this->helper->create_dir($pdf_images_dir);
1678
1679 if (file_exists($this->helper->get('pdf_dir') . $template->get('pdf') . "/" . $template->get('pdf') . ".pdf")) {
1680 $images = glob($this->helper->get('pdf_dir') . $template->get('pdf') . "/images/*");
1681 foreach ($images as $image) {
1682 copy($image, $pdf_images_dir . pathinfo($image, PATHINFO_BASENAME));
1683 }
1684 copy($this->helper->get('pdf_dir') . $template->get('pdf') . "/" . $template->get('pdf') . ".pdf", $this->helper->get('pdf_dir') . $pdf_name . "/" . $pdf_name . ".pdf");
1685 $template->set('pdf', $pdf_name);
1686 }
1687 }
1688 $template->save(true);
1689 return true;
1690 }
1691 return false;
1692 }
1693
1694 /**
1695 * Activate template
1696 *
1697 * @param int $id - ID of template
1698 */
1699 public function activate_template($id = false) {
1700
1701 $id = (int) $id;
1702
1703 $template = new Model_E2pdf_Template();
1704 if ($id && $template->load($id)) {
1705 $model_e2pdf_api = new Model_E2pdf_Api();
1706 $model_e2pdf_api->set(array(
1707 'action' => 'template/activate',
1708 'data' => array(
1709 'template_id' => $template->get('ID'),
1710 'template_uid' => $template->get('uid'),
1711 'template_title' => $template->get('title')
1712 )
1713 ));
1714 $request = $model_e2pdf_api->request();
1715
1716 if (!isset($request['error'])) {
1717 $template->set('activated', 1);
1718 $template->set('uid', $request['template_uid']);
1719 $template->save();
1720 }
1721
1722 return $request;
1723 }
1724 return false;
1725 }
1726
1727 /**
1728 * Deactivate template
1729 *
1730 * @param int $id - ID of template
1731 */
1732 public function deactivate_template($id = false) {
1733
1734 $id = (int) $id;
1735
1736 $template = new Model_E2pdf_Template();
1737 if ($id && $template->load($id)) {
1738 $model_e2pdf_api = new Model_E2pdf_Api();
1739 $model_e2pdf_api->set(array(
1740 'action' => 'template/deactivate',
1741 'data' => array(
1742 'template_id' => $template->get('ID'),
1743 'template_uid' => $template->get('uid'),
1744 )
1745 ));
1746 $request = $model_e2pdf_api->request();
1747
1748 if (!isset($request['error'])) {
1749 $template->set('activated', 0);
1750 $template->set('uid', '');
1751 $template->save();
1752 }
1753 return $request;
1754 }
1755 return false;
1756 }
1757
1758 /**
1759 * Get templates list
1760 *
1761 * @param array() $filters - Array of filter/order conditions
1762 * @param bool $count - Return number of templates
1763 *
1764 * @return mixed - IF $count - int ELSE array()
1765 */
1766 public function get_templates_list($filters = array(), $count = false) {
1767 global $wpdb;
1768 $model_e2pdf_template = new Model_E2pdf_Template();
1769
1770 $condition = array();
1771 if (isset($filters['status']) && $filters['status'] == 'trash') {
1772 $condition = array(
1773 'trash' => array(
1774 'condition' => '=',
1775 'value' => '1',
1776 'type' => '%d'
1777 )
1778 );
1779 } else {
1780 $condition = array(
1781 'trash' => array(
1782 'condition' => '<>',
1783 'value' => '1',
1784 'type' => '%d'
1785 )
1786 );
1787 }
1788
1789 if (isset($filters['s']) && $filters['s']) {
1790 $condition['title'] = array(
1791 'condition' => 'LIKE',
1792 'value' => '%%' . $filters['s'] . '%%',
1793 'type' => '%s'
1794 );
1795 }
1796
1797 $order_condition = array();
1798 if (isset($filters['orderby']) && isset($filters['order'])) {
1799 $order_condition = array(
1800 'orderby' => $filters['orderby'],
1801 'order' => $filters['order'],
1802 );
1803 } else {
1804 $order_condition = array(
1805 'orderby' => 'id',
1806 'order' => 'desc',
1807 );
1808 }
1809
1810 $limit_condition = array();
1811 if (!$count) {
1812 $paged = isset($filters['paged']) && $filters['paged'] ? $filters['paged'] : '0';
1813 $paged = (int) $paged <= 0 ? 1 : (int) $paged;
1814 $per_page = get_option('e2pdf_templates_screen_per_page') ? get_option('e2pdf_templates_screen_per_page') : '20';
1815
1816 $limit_condition = array(
1817 'limit' => $per_page,
1818 'offset' => (int) ($paged - 1) * $per_page
1819 );
1820 }
1821
1822 $where = $this->helper->load('db')->prepare_where($condition);
1823 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
1824 $limit = $this->helper->load('db')->prepare_limit($limit_condition);
1825
1826 $tpls = $wpdb->get_results($wpdb->prepare("SELECT `ID` FROM " . $model_e2pdf_template->get_table() . $where['sql'] . $orderby . $limit . "", $where['filter']));
1827
1828 if ($count) {
1829 return count($tpls);
1830 }
1831
1832 $templates = array();
1833 foreach ($tpls as $key => $tpl) {
1834 $template = new Model_E2pdf_Template();
1835 $template->load($tpl->ID, false);
1836 if (is_array($this->helper->get('license')->get('activated_templates'))) {
1837 if ($template->get('activated') && !in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
1838 $this->deactivate_template($template->get('ID'));
1839 } elseif (!$template->get('activated') && in_array($template->get('uid'), $this->helper->get('license')->get('activated_templates'))) {
1840 $this->activate_template($template->get('ID'));
1841 }
1842 }
1843
1844 $templates[] = $template;
1845 }
1846
1847 return $templates;
1848 }
1849
1850 /**
1851 * Get paper sizes list
1852 *
1853 * @return array() - Sizes list
1854 */
1855 public function get_sizes_list($size = false, $attr = false) {
1856
1857 $sizes = array(
1858 'A4' => array(
1859 'width' => '595',
1860 'height' => '842',
1861 ),
1862 'LETTER' => array(
1863 'width' => '612',
1864 'height' => '792',
1865 ),
1866 'NOTE' => array(
1867 'width' => '540',
1868 'height' => '720',
1869 ),
1870 'LEGAL' => array(
1871 'width' => '612',
1872 'height' => '1008',
1873 ),
1874 'TABLOID' => array(
1875 'width' => '792',
1876 'height' => '1224',
1877 ),
1878 'EXECUTIVE' => array(
1879 'width' => '522',
1880 'height' => '756',
1881 ),
1882 'POSTCARD' => array(
1883 'width' => '283',
1884 'height' => '416',
1885 ),
1886 );
1887
1888 if (!$size) {
1889 return $sizes;
1890 } else {
1891 if (isset($sizes[$size][$attr])) {
1892 return $sizes[$size][$attr];
1893 }
1894 return false;
1895 }
1896 }
1897
1898 /**
1899 * Get fonts list
1900 *
1901 * @return array() - Fonts list
1902 */
1903 public function get_fonts($with_path = false) {
1904
1905 $model_e2pdf_font = new Model_E2pdf_Font();
1906 $fonts = $model_e2pdf_font->get_fonts();
1907
1908 if ($with_path) {
1909 foreach ($fonts as $key => $value) {
1910 $fonts[$key] = array(
1911 'key' => $value,
1912 'subfield' => array(
1913 'path' => $key,
1914 ),
1915 'value' => $value
1916 );
1917 }
1918 }
1919
1920 return $fonts;
1921 }
1922
1923 /**
1924 * Get font sizes list
1925 *
1926 * @return array() - Font sizes list
1927 */
1928 public function get_font_sizes() {
1929
1930 $sizes = array();
1931 for ($i = 1; $i <= 72; $i++) {
1932 $sizes[$i] = $i . 'px';
1933 }
1934 return $sizes;
1935 }
1936
1937 /**
1938 * Get line heights list
1939 *
1940 * @return array() - Line heights list
1941 */
1942 public function get_line_heights() {
1943
1944 $line_heights = array();
1945 for ($i = 1; $i <= 512; $i++) {
1946 $line_heights[$i] = $i . 'px';
1947 }
1948 return $line_heights;
1949 }
1950
1951 /**
1952 * Load metaboxes on template edit/create action
1953 */
1954 public function load_metaboxes() {
1955
1956 add_meta_box(
1957 'e2pdf_templates_save', __('Save', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_save')
1958 );
1959 add_meta_box(
1960 'e2pdf_templates_builder', __('PDF Builder', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_builder')
1961 );
1962 add_meta_box(
1963 'e2pdf_templates_settings', __('Settings', 'e2pdf'), array($this, 'render_metabox'), null, 'side', 'default', array('tpl' => 'e2pdf_templates_settings')
1964 );
1965 }
1966
1967 /**
1968 * Load javascript on template edit/create action
1969 */
1970 public function load_scripts() {
1971
1972 wp_enqueue_script('postbox');
1973 wp_enqueue_script('jquery-ui-droppable');
1974 wp_enqueue_script('jquery-ui-resizable');
1975 wp_enqueue_script('jquery-ui-selectable');
1976 wp_enqueue_script('jquery-ui-dialog');
1977 wp_enqueue_script('jquery-ui-autocomplete');
1978 wp_enqueue_script('wp-color-picker');
1979 wp_enqueue_media();
1980 }
1981
1982 /**
1983 * Load styles on template edit/create action
1984 */
1985 public function load_styles($ext = false) {
1986 wp_enqueue_style('plugin_name-admin-ui-css', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css', false, false, false);
1987 wp_enqueue_style('wp-color-picker');
1988
1989 if ($ext) {
1990 $extension = new Model_E2pdf_Extension();
1991 if ($extension->load($ext)) {
1992 $styles = $extension->styles();
1993 if ($styles && is_array($styles)) {
1994 foreach ($styles as $key => $value) {
1995 wp_enqueue_style('e2pdf-dynamic-style-' . $key, $value, false, false, false);
1996 }
1997 }
1998 }
1999 }
2000 }
2001
2002 }
2003