| @@ -1,14 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * E2pdf Zip Helper | |
| 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 | |
| 4 | + * File: /helper/e2pdf-xml.php | |
| 5 | + * | |
| 6 | + * @package E2Pdf | |
| 7 | + * @license GPLv3 | |
| 8 | + * @link https://e2pdf.com | |
| 11 | 9 | */ |
| 12 | 10 | if (!defined('ABSPATH')) { |
| 13 | 11 | die('Access denied.'); |
| 14 | 12 | } |
| @@ -15,14 +13,15 @@ | ||
| 15 | 13 | |
| 16 | 14 | class Helper_E2pdf_Xml { |
| 17 | 15 | |
| 18 | 16 | private $xml; |
| 17 | + private $helper; | |
| 19 | 18 | |
| 20 | - /** | |
| 21 | - * Check if extension simplexml available | |
| 22 | - * | |
| 23 | - * @return bool | |
| 24 | - */ | |
| 19 | + public function __construct() { | |
| 20 | + $this->helper = Helper_E2pdf_Helper::instance(); | |
| 21 | + } | |
| 22 | + | |
| 23 | + // check | |
| 25 | 24 | public function check() { |
| 26 | 25 | if (extension_loaded('simplexml')) { |
| 27 | 26 | return true; |
| 28 | 27 | } else { |
| @@ -29,40 +28,23 @@ | ||
| 29 | 28 | return false; |
| 30 | 29 | } |
| 31 | 30 | } |
| 32 | 31 | |
| 33 | - /** | |
| 34 | - * Create new XML | |
| 35 | - * | |
| 36 | - * @param string $key - Wrapper of XML | |
| 37 | - * | |
| 38 | - * @return object - XML Object | |
| 39 | - */ | |
| 32 | + // create | |
| 40 | 33 | public function create($key = false) { |
| 41 | 34 | $this->set('xml', new Helper_E2pdf_SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><' . $key . '></' . $key . '>')); |
| 42 | 35 | return $this->get('xml'); |
| 43 | 36 | } |
| 44 | 37 | |
| 45 | - /** | |
| 46 | - * Get XML file in Base64 | |
| 47 | - * | |
| 48 | - * @return string | |
| 49 | - */ | |
| 38 | + // get xml | |
| 50 | 39 | public function get_xml() { |
| 51 | 40 | if ($this->get('xml')) { |
| 52 | - return base64_encode($this->get('xml')->asXML()); | |
| 41 | + return $this->get('xml')->asXML(); | |
| 53 | 42 | } |
| 54 | 43 | return ''; |
| 55 | 44 | } |
| 56 | 45 | |
| 57 | - /** | |
| 58 | - * Set option | |
| 59 | - * | |
| 60 | - * @param string $key - Key of option | |
| 61 | - * @param string $value - Value of option | |
| 62 | - * | |
| 63 | - * @return bool - Status of setting option | |
| 64 | - */ | |
| 46 | + // set | |
| 65 | 47 | public function set($key, $value) { |
| 66 | 48 | if (!isset($this->xml)) { |
| 67 | 49 | $this->xml = new stdClass(); |
| 68 | 50 | } |
| @@ -68,15 +50,9 @@ | ||
| 68 | 50 | } |
| 69 | 51 | $this->xml->$key = $value; |
| 70 | 52 | } |
| 71 | 53 | |
| 72 | - /** | |
| 73 | - * Get option by key | |
| 74 | - * | |
| 75 | - * @param string $key - Key to get assigned option value | |
| 76 | - * | |
| 77 | - * @return mixed | |
| 78 | - */ | |
| 54 | + // get | |
| 79 | 55 | public function get($key) { |
| 80 | 56 | if (isset($this->xml->$key)) { |
| 81 | 57 | return $this->xml->$key; |
| 82 | 58 | } else { |
| @@ -83,18 +59,11 @@ | ||
| 83 | 59 | return false; |
| 84 | 60 | } |
| 85 | 61 | } |
| 86 | 62 | |
| 87 | - /** | |
| 88 | - * Get Element Attribute Value | |
| 89 | - * | |
| 90 | - * @param object $element - XML Element | |
| 91 | - * @param string $attribute - Attribute Name | |
| 92 | - * | |
| 93 | - * @return string|bool | |
| 94 | - */ | |
| 63 | + // get node value | |
| 95 | 64 | public function get_node_value($element, $attribute = '') { |
| 96 | - $value = ""; | |
| 65 | + $value = ''; | |
| 97 | 66 | if (is_object($element) && $attribute && isset($element->attributes) && $element->attributes->getNamedItem($attribute)) { |
| 98 | 67 | $value = $element->attributes->getNamedItem($attribute)->nodeValue; |
| 99 | 68 | } |
| 100 | 69 | return $value; |
| @@ -99,57 +68,143 @@ | ||
| 99 | 68 | } |
| 100 | 69 | return $value; |
| 101 | 70 | } |
| 102 | 71 | |
| 103 | - /** | |
| 104 | - * Check Element Attribute | |
| 105 | - * | |
| 106 | - * @param object $element - XML Element | |
| 107 | - * @param string $attribute - Attribute Name | |
| 108 | - * | |
| 109 | - * @return string|bool | |
| 110 | - */ | |
| 111 | - public function check_node_value($element, $attribute = '') { | |
| 112 | - if (is_object($element) && $attribute && isset($element->attributes) && $element->attributes->getNamedItem($attribute)) { | |
| 113 | - return true; | |
| 72 | + // set node value | |
| 73 | + public function set_node_value($element, $attribute = '', $value = '', $parent = false) { | |
| 74 | + | |
| 75 | + if (!is_object($element) || !method_exists($element, 'setAttribute') || !$attribute) { | |
| 76 | + return $element; | |
| 114 | 77 | } |
| 115 | - return false; | |
| 78 | + | |
| 79 | + if ($parent && (!is_object($element->parentNode) || !method_exists($element->parentNode, 'setAttribute'))) { | |
| 80 | + return $element; | |
| 81 | + } | |
| 82 | + | |
| 83 | + if ($parent) { | |
| 84 | + $target = $element->parentNode; | |
| 85 | + } else { | |
| 86 | + $target = $element; | |
| 87 | + } | |
| 88 | + $target->setAttribute($attribute, $value); | |
| 89 | + | |
| 90 | + return $element; | |
| 116 | 91 | } |
| 117 | 92 | |
| 118 | - /** | |
| 119 | - * Set Element Attribute | |
| 120 | - * | |
| 121 | - * @param object $element - XML Element | |
| 122 | - * @param string $attribute - Attribute Name | |
| 123 | - * @param string $value - Attribute Value | |
| 124 | - * @param bool $parent - if need to modify parent node | |
| 125 | - * | |
| 126 | - * @return object | |
| 127 | - */ | |
| 128 | - public function set_node_value($element, $attribute = '', $value = '', $parent = false) { | |
| 93 | + // parse xml page | |
| 94 | + public function parse_xml_page($page_node, &$pages, $pdf_images_dir, $extension) { | |
| 129 | 95 | |
| 130 | - if ($parent) { | |
| 131 | - if (is_object($element) && $attribute && isset($element->parentNode->attributes)) { | |
| 132 | - if ($element->parentNode->attributes->getNamedItem($attribute)) { | |
| 133 | - $element->parentNode->attributes->getNamedItem($attribute)->nodeValue = $value; | |
| 134 | - } elseif ($this->get('dom')) { | |
| 135 | - $attr = $this->get('dom')->createAttribute($attribute); | |
| 136 | - $attr->value = $value; | |
| 137 | - $element->parentNode->appendChild($attr); | |
| 96 | + $page_number = (string) $page_node['number']; | |
| 97 | + $pages[$page_number] = []; | |
| 98 | + $pages[$page_number]['page_id'] = $page_number; | |
| 99 | + | |
| 100 | + if (isset($page_node->properties)) { | |
| 101 | + $pages[$page_number]['properties'] = []; | |
| 102 | + foreach ($page_node->properties->children() as $prop_name => $prop_value) { | |
| 103 | + $prop_string = (string) $prop_value; | |
| 104 | + if ($prop_name === 'background' && !empty($prop_string)) { | |
| 105 | + file_put_contents($pdf_images_dir . $page_number . '.png', base64_decode($prop_string), LOCK_EX); | |
| 106 | + do_action('e2pdf_pdf_upload_background_save_after'); | |
| 107 | + } else { | |
| 108 | + $pages[$page_number]['properties'][$prop_name] = $prop_string; | |
| 138 | 109 | } |
| 139 | 110 | } |
| 140 | - } else { | |
| 141 | - if (is_object($element) && $attribute && isset($element->attributes)) { | |
| 142 | - if ($element->attributes->getNamedItem($attribute)) { | |
| 143 | - $element->attributes->getNamedItem($attribute)->nodeValue = $value; | |
| 144 | - } elseif ($this->get('dom')) { | |
| 145 | - $attr = $this->get('dom')->createAttribute($attribute); | |
| 146 | - $attr->value = $value; | |
| 147 | - $element->appendChild($attr); | |
| 111 | + } | |
| 112 | + if (isset($page_node->elements)) { | |
| 113 | + $pages[$page_number]['elements'] = []; | |
| 114 | + $element_index = 0; | |
| 115 | + foreach ($page_node->elements->element as $element_node) { | |
| 116 | + $pages[$page_number]['elements'][$element_index] = []; | |
| 117 | + foreach ($element_node->children() as $elem_name => $elem_value) { | |
| 118 | + if ($elem_name == 'properties') { | |
| 119 | + $pages[$page_number]['elements'][$element_index]['properties'] = []; | |
| 120 | + foreach ($elem_value->children() as $inner_prop_name => $inner_prop_value) { | |
| 121 | + $pages[$page_number]['elements'][$element_index]['properties'][$inner_prop_name] = (string) $inner_prop_value; | |
| 122 | + } | |
| 123 | + } else { | |
| 124 | + $pages[$page_number]['elements'][$element_index][$elem_name] = (string) $elem_value; | |
| 125 | + } | |
| 148 | 126 | } |
| 127 | + $element = $pages[$page_number]['elements'][$element_index]; | |
| 128 | + if ($element['type'] === 'e2pdf-image' && isset($element['base64']) && $element['base64']) { | |
| 129 | + $image = base64_decode($element['base64']); | |
| 130 | + $file_name = basename($element['value']); | |
| 131 | + if (!$file_name) { | |
| 132 | + $ext = $this->helper->load('image')->get_extension($image); | |
| 133 | + if ($ext) { | |
| 134 | + $file_name = md5(mktime()) . '.' . $ext; | |
| 135 | + } | |
| 136 | + } | |
| 137 | + if ($file_name) { | |
| 138 | + $upload_file = wp_upload_bits($file_name, null, $image); | |
| 139 | + if (!$upload_file['error']) { | |
| 140 | + $wp_filetype = wp_check_filetype($file_name, null); | |
| 141 | + $attachment = array( | |
| 142 | + 'post_mime_type' => $wp_filetype['type'], | |
| 143 | + 'post_parent' => 0, | |
| 144 | + 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name), | |
| 145 | + 'post_content' => '', | |
| 146 | + 'post_status' => 'inherit', | |
| 147 | + ); | |
| 148 | + $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], 0); | |
| 149 | + if (!is_wp_error($attachment_id)) { | |
| 150 | + require_once ABSPATH . 'wp-admin/includes/image.php'; | |
| 151 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']); | |
| 152 | + wp_update_attachment_metadata($attachment_id, $attachment_data); | |
| 153 | + $pages[$page_number]['elements'][$element_index]['value'] = $upload_file['url']; | |
| 154 | + } | |
| 155 | + } | |
| 156 | + unset($pages[$page_number]['elements'][$element_index]['base64']); | |
| 157 | + } else { | |
| 158 | + unset($pages[$page_number]['elements'][$element_index]); | |
| 159 | + } | |
| 160 | + } elseif (isset($element['name']) && $element['name']) { | |
| 161 | + $el_value = $extension->auto_map($element['name']); | |
| 162 | + if ($el_value !== false) { | |
| 163 | + $pages[$page_number]['elements'][$element_index]['value'] = $el_value; | |
| 164 | + } | |
| 165 | + } | |
| 166 | + $element_index++; | |
| 149 | 167 | } |
| 150 | 168 | } |
| 169 | + } | |
| 151 | 170 | |
| 152 | - return $element; | |
| 171 | + // parse xml font | |
| 172 | + public function parse_xml_font($font) { | |
| 173 | + | |
| 174 | + if (!$font) { | |
| 175 | + return; | |
| 176 | + } | |
| 177 | + | |
| 178 | + $font_title = isset($font->title) ? (string) $font->title : ''; | |
| 179 | + $font_name = isset($font->name) ? (string) $font->name : ''; | |
| 180 | + $font_value = isset($font->value) ? (string) $font->value : ''; | |
| 181 | + | |
| 182 | + if (!$font_title || !$font_name || !$font_value) { | |
| 183 | + return; | |
| 184 | + } | |
| 185 | + | |
| 186 | + $model_e2pdf_font = new Model_E2pdf_Font(); | |
| 187 | + $fonts = $model_e2pdf_font->get_fonts(); | |
| 188 | + $fonts_dir = $this->helper->get('fonts_dir'); | |
| 189 | + | |
| 190 | + $exist = array_search($font_title, $fonts, true); | |
| 191 | + if ($exist === false) { | |
| 192 | + if (!file_exists($fonts_dir . $font_name)) { | |
| 193 | + $f_name = $font_name; | |
| 194 | + } else { | |
| 195 | + $i = 0; | |
| 196 | + do { | |
| 197 | + $f_name = $i . '_' . $font_name; | |
| 198 | + $i++; | |
| 199 | + } while (file_exists($fonts_dir . $f_name)); | |
| 200 | + } | |
| 201 | + $font_ext = strtolower(pathinfo($f_name, PATHINFO_EXTENSION)); | |
| 202 | + if (in_array($font_ext, $model_e2pdf_font->get_allowed_extensions())) { | |
| 203 | + $font_data = base64_decode($font_value); | |
| 204 | + if ($font_data !== false) { | |
| 205 | + file_put_contents($fonts_dir . $f_name, $font_data, LOCK_EX); | |
| 206 | + } | |
| 207 | + } | |
| 208 | + } | |
| 153 | 209 | } |
| 154 | - | |
| 155 | 210 | } |