]> 91.132.146.200 Git - insipid.git/commitdiff
export of a single link as xml.
authorJohannes Keßler <johannes.kessler@bechtle.com>
Thu, 30 Jan 2020 11:54:49 +0000 (12:54 +0100)
committerJohannes Keßler <johannes.kessler@bechtle.com>
Thu, 30 Jan 2020 11:54:49 +0000 (12:54 +0100)
webroot/lib/import-export.class.php [new file with mode: 0644]
webroot/lib/management.class.php
webroot/view/editlink.inc.php
webroot/view/linkinfo.php

diff --git a/webroot/lib/import-export.class.php b/webroot/lib/import-export.class.php
new file mode 100644 (file)
index 0000000..43f099d
--- /dev/null
@@ -0,0 +1,142 @@
+<?php
+/**
+ * Insipid
+ * Personal web-bookmark-system
+ *
+ * Copyright 2016-2020 Johannes Keßler
+ *
+ * Development starting from 2011: Johannes Keßler
+ * https://www.bananas-playground.net/projekt/insipid/
+ *
+ * creator:
+ * Luke Reeves <luke@neuro-tech.net>
+ *
+ * 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);
+               }
+       }
+}
index 9f07ed076f1684c851c099ca242f08c7f419b927..e029087cde62f67eb3cdf916297f4219c0932e28 100644 (file)
@@ -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
index 4cf4bf9f4825709530d2b85d238795549fc859d0..5c480a83e3c904d4964fbbedfb03143cbdf26c41 100644 (file)
@@ -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'];
 
index 6216904a3ed969fd125e43e1936b0fca410d99a2..a345b13253d46ac5f3d516338b7b2456bff9774a 100644 (file)
                                </span>
                                <span>Edit</span>
                        </a>
+            <a href="index.php?p=editlink&id=<?php echo $linkData['hash']; ?>&m=export" class="button is-small is-success">
+                               <span class="icon">
+                                       <i class="ion-md-download"></i>
+                               </span>
+                <span>Export</span>
+            </a>
                </div>
        </div>
        <?php } ?>