Skip to content

Commit

Permalink
Make sure images appear in the downloaded word document from the
Browse files Browse the repository at this point in the history
documentation page
  • Loading branch information
Tom Reijnders committed Sep 20, 2019
1 parent 91f4d10 commit 89d5cf7
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 52 deletions.
181 changes: 131 additions & 50 deletions download.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,135 @@
<?PHP
/**
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
$data = json_decode($_POST['data'], true);

$filename = "file";
if ($data["filename"]) $filename = $data["filename"];



header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '.DOC"');

echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
echo '<head>';
echo '<style>@page Section1 {size:'.$data['size'].';mso-page-orientation:'.$data['orientation'].';}div.Section1 {page:Section1;}</style>';
echo "<style>".$data['styles']."</style>";
echo '</head>';

echo "<body>";

echo "<div class=\"Section1\">";
echo "<h1>".$data['documentName']."</h1>";
echo "<p>".$data['documentText']."</p>";
echo "<p>".$data['documentIntro']."</p>";

foreach ($data['pages'] as $pagekey => $pagevalue) {
echo "<h1>".$pagevalue['pageName']."</h1>";
echo "<p>".$pagevalue['pageText']."</p>";
echo "<div class=\"page\">";

foreach ($pagevalue['sections'] as $sectionkey => $sectionvalue) {
if (array_key_exists('sectionName', $sectionvalue)) {
echo "<div class=\"section\">";
echo "<h2>".$sectionvalue['sectionName']."</h2>";
echo "<p>".$sectionvalue['sectionText']."</p>";
}
foreach ($sectionvalue["items"] as $itemkey => $itemvalue) {
echo "<div class=\"item\">";
echo "<h3>".$itemvalue['itemName']."</h3>";
echo "<p>".$itemvalue['itemText']."</p>";
echo "<p class=\"item\"><i>".$itemvalue['itemValue']."</i></p>";
echo "</div>";
}
if (array_key_exists('sectionName', $sectionvalue)) {
echo "</div>";
}
}
echo "</div>";
* The Apereo Foundation licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* This routine expects data that will be ransformed into a document that word can accept
*
* This is su=imply building an html file and storing as .doc file. Word will do the work
*
* However, to be able to handle images we need to build a mime 1.0 document, like Word does with mhtml files
*
* The images are saved in a subfolder, and the src needs to be adapted for that
*
* See the website below for an excellent explanation. It also show how we can extend this in the future.
*
* @ref https://sebsauvage.net/wiki/doku.php?id=word_document_generation
*
*/
require_once("config.php");

class mime10class
{
private $data;
const boundary='----=_NextPart_XERTE.DOCUMENTATION.PARTS.EYUUREZ';
function __construct() { $this->data="MIME-Version: 1.0\nContent-Type: multipart/related; boundary=\"".self::boundary."\"\n\n"; }
public function addFile($filename,$contenttype,$data)
{
$this->data = $this->data . '--'.self::boundary . "\nContent-Location: file:///C:/" . preg_replace('!\\\!', '/', $filename) . "\nContent-Transfer-Encoding: base64\nContent-Type: " . $contenttype . "\n\n";
$this->data = $this->data . base64_encode($data) . "\n\n";
}
public function getFile() { return $this->data . '--' . self::boundary . '--'; }
}

echo "</div>";
echo "</body>";
echo "</html>";
if (isset($_SESSION['toolkits_logon_id'])) {
$data = json_decode($_POST['data'], true);

$filename = "file";
if ($data["filename"]) $filename = $data["filename"];


header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '.DOC"');

$doc = "";
$doc .= "<html>";
$doc .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
$doc .= '<head>';
$doc .= '<style>@page Section1 {size:' . $data['size'] . ';mso-page-orientation:' . $data['orientation'] . ';}div.Section1 {page:Section1;}</style>';
$doc .= "<style>" . $data['styles'] . "</style>";
$doc .= '</head>';

$doc .= "<body>";

$doc .= "<div class=\"Section1\">";
$doc .= "<h1>" . $data['documentName'] . "</h1>";
$doc .= "<p>" . $data['documentText'] . "</p>";
$doc .= "<p>" . $data['documentIntro'] . "</p>";

foreach ($data['pages'] as $pagekey => $pagevalue) {
$doc .= "<h1>" . $pagevalue['pageName'] . "</h1>";
$doc .= "<p>" . $pagevalue['pageText'] . "</p>";
$doc .= "<div class=\"page\">";

foreach ($pagevalue['sections'] as $sectionkey => $sectionvalue) {
if (array_key_exists('sectionName', $sectionvalue)) {
$doc .= "<div class=\"section\">";
$doc .= "<h2>" . $sectionvalue['sectionName'] . "</h2>";
$doc .= "<p>" . $sectionvalue['sectionText'] . "</p>";
}
foreach ($sectionvalue["items"] as $itemkey => $itemvalue) {
$doc .= "<div class=\"item\">";
$doc .= "<h3>" . $itemvalue['itemName'] . "</h3>";
$doc .= "<p>" . $itemvalue['itemText'] . "</p>";
$doc .= "<p class=\"item\"><i>" . $itemvalue['itemValue'] . "</i></p>";
$doc .= "</div>";
}
if (array_key_exists('sectionName', $sectionvalue)) {
$doc .= "</div>";
}
}
$doc .= "</div>";
}

$doc .= "</div>";
$doc .= "</body>";
$doc .= "</html>";

// Replace all images by inline images
$worddoc = new mime10class();
$ipos = strpos($doc, "<img");
while ($ipos !== false) {
// Get the value of the src attribute
$bpos = strpos($doc, "src=", $ipos);
if ($bpos !== false) {
// Skip Needle
$bpos += 4;
// get quote used
$quote = $doc[$bpos];
$bpos += 1; // skip quote
$epos = strpos($doc, $quote, $bpos);
if ($epos !== false) {
$imgfile = substr($doc, $bpos, $epos - $bpos);
$imgdata = file_get_contents($imgfile);
$imgparts = pathinfo($imgfile);
$new_imgfile = 'images/' . $imgparts['basename'];

$src = $new_imgfile;
// Add image to mime file
$worddoc->addFile($new_imgfile, 'image/' . $imgparts['extension'], $imgdata);
// Replace old src with new src
$doc = substr($doc, 0, $bpos) . $src . substr($doc, $epos);
}
}
$ipos = strpos($doc, "<img", $ipos+1);
}
$filename_parts = pathinfo($filename);
$worddoc->addFile($filename_parts['filename'] . '.htm', 'text/html', $doc);
echo $worddoc->getFile();
}
103 changes: 103 additions & 0 deletions migrate_user_templates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Created by PhpStorm.
* User: tom
* Date: 26-3-2019
* Time: 18:11
*/

require_once(dirname(__FILE__) . "/config.php");
require_once(dirname(__FILE__) . "/website_code/php/folder_library.php");

global $xerte_toolkits_site;

$prefix = $xerte_toolkits_site->database_table_prefix;

if (isset($_SESSION['toolkits_logon_id']) && $_SESSION['toolkits_logon_id'] == "site_administrator")
{
if (isset($_GET['olduser']) && isset($_GET['newuser'])) {
$q = "select td.*, tr.*, otd.template_name as orgtemplate_name from {$prefix}templatedetails td, {$prefix}templaterights tr, {$prefix}originaltemplatesdetails otd, {$prefix}logindetails ld
where td.template_id=tr.template_id and tr.role='creator' and td.creator_id=ld.login_id and td.template_type_id=otd.template_type_id and ld.username=?";
$templates_to_move = db_query($q, array($_GET['olduser']));
if ($templates_to_move !== false) {
// Get folder parent name of newuser
$q = "select fd.folder_id, fd.login_id from {$prefix}folderdetails fd, {$prefix}logindetails ld where ld.login_id=fd.login_id and ld.username=? AND folder_name = ?";
$rootfolder = db_query($q, array($_GET['newuser'], $_GET['newuser']));
if ($rootfolder === false)
{
die("Could not find workspace folder of user " . $_GET['newuser']);
}
$foldername = $_GET['olduser'];
if (isset($_GET['newfoldername']))
{
$foldername = $_GET['newfoldername'];
}
// Check if folder $foldername exists for user 'newuser'
$q = "select folder_id from {$prefix}folderdetails fd, {$prefix}logindetails ld where ld.login_id=fd.login_id nad ld.username=? AND folder_name = ?";
$folder = db_query($q, array($_GET['newuser'], $foldername));
if ($folder === false)
{
die("Error checking existence of folder " . $foldername);
}
if ($folder === null)
{
// create folder
$q = "INSERT INTO {$prefix}folderdetails (login_id,folder_parent,folder_name,date_created) values (?,?,?,?)";
$params = array($rootfolder['login_id'], $rootfolder['folder_id'], $foldername, date('Y-m-d'));
$folderid = db_query($q, $params);
}
else
{
$folderid = $folder['folder_id'];
}
// Ok, so now we move all the templates to move to user $_GET['newuser'], in folder $folderid
foreach($templates_to_move as $template)
{
// Correct the database
// 1. templatedetails
$q = "update {$prefix}templatedetails set creator_id=? where template_id=?";
$res = db_query($q, arary($template['template_id']));
if ($res === false)
{
die("Error updating templatedetails of template " . $template['template_id']);
}

// 2. templaterights
// 2a. put in correct folder
$q = "insert {$prefix}templaterights set template_id=?, user_id=?, role='creator', folder=?";
$params = array($template['template_id'], $rootfolder['login_id'], $folderid);
$res = db_query($q, $params);
if ($res === false)
{
die("Error inserting creator record in templaterights of template " . $template['template_id']);
}
// 2b. Remove any access previously already granted to newuser to prevent different roles of this one user
$q = "delete from {$prefix}templaterights where template_id=? and login_id=? and creator != 'creator'";
$params = array($template['template_id'], $rootfolder['login_id']);
$res = db_query($q, $params);
if ($res === false)
{
die("Error deleting any older rights in templaterights of template " . $template['template_id']);
}
// 3. rename USER_FILES folder
// 3a. old folder name
$oldfolder = $xerte_toolkits_site->users_file_area_short . $template['template_id'] . "_" . $_GET['olduser'] . "_" . $template['orgtemplate_name'];
// 3.b new folder name
$newfolder = $xerte_toolkits_site->users_file_area_short . $template['template_id'] . "_" . $_GET['newuser'] . "_" . $template['orgtemplate_name'];

// Move
$res = rename($oldfolder, $newfolder);
if ($res === false)
{
die("Error renaming " . $oldfolder . " to " . $newfolder);
}
// Success
echo "Placed template with id " . $template['template_id'] . " (" . str_replace("_", " ", $template['template_id']) . ") in folder " . $foldername . " of " . $_GET['newuser'] . " and renamed " . $oldfolder . " to " . $newfolder . ".";
}
}
}
echo "Usage: migrate_user.php?olduser=<login old user>&newuser=<login new user>";
}
else{
echo "Permission denied!";
}
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@
break;
case 'media':
if (xml.getAttribute('url') != undefined && xml.getAttribute('url') != '') {
if (exclude != 'doc') {
docData.pages[page].sections[section].items[item].itemText = '<img class="itemImg" src="' + xml.getAttribute('url') + '">';
}
$element = $('<img class="itemImg">')
.attr('src', xml.getAttribute('url'));
}
Expand Down Expand Up @@ -703,7 +706,7 @@
tableString += '</table>';

return $('<div class="table">' + tableString + '</div>');
}
};

this.formatTableForDownload = function($table, notAnsweredText) {
var $tempTable = $('<div>').append($table.find('table').clone());
Expand All @@ -716,7 +719,7 @@
$tempTable.find('table').attr('border', '1');

return '<span style="font-style:">' + $tempTable.html() + '</span>';
}
};

this.download = function () {
if (documentation.checkRequired(currentPage)) {
Expand Down

0 comments on commit 89d5cf7

Please sign in to comment.