From faf0c3e89a94a0f2d8cf89453ec48ca5bae40d0a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Ke=C3=9Fler?= Date: Thu, 30 Jan 2020 12:54:49 +0100 Subject: [PATCH] export of a single link as xml. --- webroot/lib/import-export.class.php | 142 ++++++++++++++++++++++++++++ webroot/lib/management.class.php | 35 ++++++- webroot/view/editlink.inc.php | 33 +++++++ webroot/view/linkinfo.php | 6 ++ 4 files changed, 213 insertions(+), 3 deletions(-) create mode 100644 webroot/lib/import-export.class.php diff --git a/webroot/lib/import-export.class.php b/webroot/lib/import-export.class.php new file mode 100644 index 0000000..43f099d --- /dev/null +++ b/webroot/lib/import-export.class.php @@ -0,0 +1,142 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0. + * + */ + +class ImportExport { + + private $_currentXW; + + public function __construct() { + } + + /** + * create a xml file for a given single link + * expects the array from Link->load + * @param array $data + * @return string XML string from xmlwriter + */ + public function createSingleLinkExportXML($data) { + + $this->_currentXW = xmlwriter_open_memory(); + xmlwriter_set_indent($this->_currentXW, 1); + xmlwriter_set_indent_string($this->_currentXW, ' '); + xmlwriter_start_document($this->_currentXW, '1.0', 'UTF-8'); + + xmlwriter_start_element($this->_currentXW, 'insipidlink'); + + xmlwriter_start_attribute($this->_currentXW, 'id'); + xmlwriter_text($this->_currentXW, $data['id']); + xmlwriter_end_attribute($this->_currentXW); + + xmlwriter_start_element($this->_currentXW, 'link'); + xmlwriter_start_cdata($this->_currentXW); + xmlwriter_text($this->_currentXW, $data['link']); + xmlwriter_end_cdata($this->_currentXW); + xmlwriter_end_element($this->_currentXW); + + xmlwriter_start_element($this->_currentXW, 'description'); + xmlwriter_start_cdata($this->_currentXW); + xmlwriter_text($this->_currentXW, $data['description']); + xmlwriter_end_cdata($this->_currentXW); + xmlwriter_end_element($this->_currentXW); + + xmlwriter_start_element($this->_currentXW, 'title'); + xmlwriter_start_cdata($this->_currentXW); + xmlwriter_text($this->_currentXW, $data['title']); + xmlwriter_end_cdata($this->_currentXW); + xmlwriter_end_element($this->_currentXW); + + xmlwriter_start_element($this->_currentXW, 'hash'); + xmlwriter_start_cdata($this->_currentXW); + xmlwriter_text($this->_currentXW, $data['hash']); + xmlwriter_end_cdata($this->_currentXW); + xmlwriter_end_element($this->_currentXW); + + if(!empty($data['tags'])) { + xmlwriter_start_element($this->_currentXW, 'tags'); + foreach($data['tags'] as $k=>$v) { + $this->_elementFromKeyValue('tag',$k,$v); + } + xmlwriter_end_element($this->_currentXW); + } + + if(!empty($data['categories'])) { + xmlwriter_start_element($this->_currentXW, 'categories'); + foreach($data['categories'] as $k=>$v) { + $this->_elementFromKeyValue('category',$k,$v); + } + xmlwriter_end_element($this->_currentXW); + } + + xmlwriter_start_element($this->_currentXW, 'created'); + xmlwriter_start_cdata($this->_currentXW); + xmlwriter_text($this->_currentXW, $data['created']); + xmlwriter_end_cdata($this->_currentXW); + xmlwriter_end_element($this->_currentXW); + + xmlwriter_start_element($this->_currentXW, 'updated'); + xmlwriter_start_cdata($this->_currentXW); + xmlwriter_text($this->_currentXW, $data['updated']); + xmlwriter_end_cdata($this->_currentXW); + xmlwriter_end_element($this->_currentXW); + + xmlwriter_start_element($this->_currentXW, 'exportcreated'); + xmlwriter_start_cdata($this->_currentXW); + xmlwriter_text($this->_currentXW, date('Y-m-d H:i:s')); + xmlwriter_end_cdata($this->_currentXW); + xmlwriter_end_element($this->_currentXW); + + + xmlwriter_end_element($this->_currentXW); // insipidlink + xmlwriter_end_document($this->_currentXW); // document + + return xmlwriter_output_memory($this->_currentXW); + } + + /** + * Create a single xml element for the current loaded xmlwriter + * @param String $name + * @param String $key + * @param String $value + */ + private function _elementFromKeyValue($name, $key, $value) { + + if(!empty($key) && !empty($value) && !empty($name)) { + xmlwriter_start_element($this->_currentXW, $name); + + xmlwriter_start_attribute($this->_currentXW, 'id'); + xmlwriter_text($this->_currentXW, $key); + xmlwriter_end_attribute($this->_currentXW); + + xmlwriter_start_cdata($this->_currentXW); + xmlwriter_text($this->_currentXW, $value); + xmlwriter_end_cdata($this->_currentXW); + + xmlwriter_end_element($this->_currentXW); + } + } +} diff --git a/webroot/lib/management.class.php b/webroot/lib/management.class.php index 9f07ed0..e029087 100644 --- a/webroot/lib/management.class.php +++ b/webroot/lib/management.class.php @@ -586,9 +586,9 @@ class Management { /** * Load link by given hash. Do not use Link class directly. * Otherwise the authentication will be ignored. - * @param $hash - * @param bool $fullInfo - * @param $withObject + * @param String $hash Link hash + * @param bool $fullInfo Load all the info we have + * @param bool $withObject An array with data and the link obj itself * @return array|mixed */ public function loadLink($hash,$fullInfo=true,$withObject=false) { @@ -648,6 +648,35 @@ class Management { return $ret; } + /** + * Export given link for download and later import + * @param $hash + * @param bool $linkObj Use already existing link obj + * @return bool + */ + public function exportLinkData($hash,$linkObj=false) { + $ret = false; + + if (!empty($hash)) { + $linkData = $this->loadLink($hash, true, true); + if (!empty($linkData)) { + $data = $linkData; + } + } + elseif(!empty($linkObj) && is_a($linkObj,'Link')) { + $data = $linkObj->getData(); + } + + if(!empty($data) && isset($data['link'])) { + + require_once 'lib/import-export.class.php'; + $ImEx = new ImportExport(); + $ret = $ImEx->createSingleLinkExportXML($data); + } + + return $ret; + } + /** * for simpler management we have the search data in a separate column * it is not fancy or even technical nice but it damn works diff --git a/webroot/view/editlink.inc.php b/webroot/view/editlink.inc.php index 4cf4bf9..5c480a8 100644 --- a/webroot/view/editlink.inc.php +++ b/webroot/view/editlink.inc.php @@ -46,6 +46,12 @@ if(isset($_GET['awm']) && !empty($_GET['awm'])) { $Management->setShowAwm($_isAwm); } +$_requestMode = false; +if(isset($_GET['m']) && !empty($_GET['m'])) { + $_requestMode = trim($_GET['m']); + $_requestMode = Summoner::validate($_requestMode,'nospace') ? $_requestMode : false; +} + $linkData = $Management->loadLink($_id); if(empty($linkData)) { header("HTTP/1.0 404 Not Found"); @@ -60,6 +66,33 @@ if($_isAwm === true) { $submitFeedback['status'] = 'success'; } +if($_requestMode && $_requestMode == "export") { + $_i = $linkObj->getData('id'); + $_i = false; + if(!empty($_i)) { + + $exportFilename = 'inspid-single-export-'.$_i.'.xml'; + + $exportData = $Management->exportLinkData(false, $linkObj); + if (!empty($exportData)) { + header('Content-Type: text/xml'); + header("Content-Transfer-Encoding: Binary"); + header("Content-disposition: attachment; filename=$exportFilename"); + echo($exportData); + exit(); + } + else { + $submitFeedback['message'] = 'Export could not generated.'; + $submitFeedback['status'] = 'error'; + } + } + else { + $submitFeedback['message'] = 'Required data for export could not be loaded.'; + $submitFeedback['status'] = 'error'; + } + +} + if(isset($_POST['data']) && !empty($_POST['data']) && isset($_POST['editlink'])) { $fData = $_POST['data']; diff --git a/webroot/view/linkinfo.php b/webroot/view/linkinfo.php index 6216904..a345b13 100644 --- a/webroot/view/linkinfo.php +++ b/webroot/view/linkinfo.php @@ -150,6 +150,12 @@ Edit + + + + + Export + -- 2.39.5