xref: /plugin/zwidoku/open_file.php (revision f82bbc904bd835fc66a3f52ffaef251433904ec2)
1<?php
2
3/**
4 * ZWIdoku Plugin: Submit the article.
5 *
6 * @license  LGPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author   S.Chekanov
8 * @based_on "pageindex" plugin by Kite <Kite@puzzlers.org>
9 * @based_on "externallink" plugin by Otto Vainio <plugins@valjakko.net>
10 * @based_on "pagelist" plugin by Esther Brunner <wikidesign@gmail.com>
11 *
12 */
13
14
15if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
16if(!defined('DOKU_LIB')) define('DOKU_LIB',realpath(dirname(__FILE__).'/../../').'/');
17if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_LIB.'plugins/');
18//define('DOKU_MAIN',realpath(dirname(__FILE__).'/../../../').'/');
19
20// must be run within Dokuwiki
21if(!defined('DOKU_INC')) die("Cannot find installed DokuWiki");
22
23
24$CURRENT_URL="https://enhub.org/dokuwiki";
25
26
27
28//require_once(DOKU_INC.'/inc/pageutils.php');
29require_once(DOKU_INC.'/inc/init.php');
30require_once(DOKU_INC.'/inc/template.php');
31
32$target_dir = DOKU_INC. "/data/tmp/";
33$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
34
35// start page
36echo file_get_contents(DOKU_INC.'/lib/plugins/zwidoku/html_start');
37
38$uploadOk = 1;
39$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
40// Check if image file is a actual image or fake image
41if(isset($_POST["submit"])) {
42
43
44   // Check file size
45  if ($_FILES["fileToUpload"]["size"] > 10000000) {
46    echo "<h3>Sorry, your file is too large (more than 10MB)</h3>";
47    $uploadOk = 0;
48    die();
49  }
50
51
52   // Check if file already exists
53  if (file_exists($target_file)) {
54    echo "<h3>Sorry, file already exists.</h3>";
55    $uploadOk = 0;
56    die();
57  }
58
59// Allow certain file formats
60 if($imageFileType != "zwi" && $imageFileType != "ZWI" ) {
61    echo "<h3>Sorry, only ZWI  files are allowed.</h3>";
62    echo "<h3>Please return and select correct ZWI file.</h3>";
63    $uploadOk = 0;
64    die();
65 }
66
67
68// Check if $uploadOk is set to 0 by an error
69if ($uploadOk == 0) {
70    echo "Sorry, your file was not uploaded.";
71// if everything is ok, try to upload file
72} else {
73    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
74        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
75
76
77     $zip = new ZipArchive;
78     $zip->open( $target_file);
79     $json=$zip->getFromName('metadata.json');
80
81     $infowik=json_decode($json, TRUE);
82     $ZWIversion = $infowik['ZWIversion'];
83     $Title = $infowik['Title'];
84     $Namespace = $infowik['Namespace'];
85
86
87     # check plugins
88     $json1=$zip->getFromName('plugins.json');
89     $infoplugins=json_decode( $json1, TRUE);
90     // list used plugins for consistency
91     foreach(glob(DOKU_INC."lib/plugins/*", GLOB_ONLYDIR) as $dir) {
92        $usedplugins[] = basename($dir);
93     }
94
95
96
97     $json2=$zip->getFromName('metadata.json');
98     $infopage=json_decode( $json2, TRUE);
99     $Xmeta=$infopage["meta"];
100     $Xtitle= $Xmeta["title"];
101     $Xcreator= $Xmeta["creator"];
102     echo "<center><h1>" . $Xtitle . "</h1>";
103     echo "original author: " . $Xcreator . "</center></br>";
104     // get article
105     $article=$zip->getFromName('article.dokuwiki');
106     //echo "Title=". $Title ."<br>";
107     echo "Namespace:". $Namespace ."<br>";
108
109     $newtitle = str_replace(":", "/", $Title) . ".txt";
110     $out_file= DOKU_INC."data/pages/".$newtitle;
111
112     $articleExist=False;
113     if (file_exists($out_file)) {
114
115           $ss="<a href='/doukuwiki/doku.php?id=".$Title."&do=revisions'> Old Revisions </a>";
116           echo "<p style=\"color:#cc7a00;  font-weight: bold;\">";
117           echo "Status: replacement for the existing article with the same name.</br>";
118           echo "Article $Title was replaced. If this is not your intension, revert it using $ss</br>";
119            echo "</p>";
120           $articleExist=true;
121     } else {
122            echo "<p style=\"color:#009900;  font-weight: bold;\">";
123            echo "Status: New article";
124            echo "</p>";
125           //echo "Fill name of the new article: ". $Title ."<br>";
126     }
127
128     echo "ZWI format version=". $ZWIversion ."<br>";
129     echo "Checking compartibility of used plugins<br>";
130     $compat=true;
131     foreach ($usedplugins as &$value) {
132              $ex=false;
133              foreach ($infoplugins as &$val)
134                     if ($val == $value) $ex=true;
135              if ($ex == false) {
136                 echo "Warning: plugin \"$value\" used for this ZWI file is missing<br>";
137                 $compat=false;
138              };
139         }
140    if ($compat) echo "All plugins used for this file are available in the system<br>";
141
142
143     file_put_contents($out_file, $article);
144     $xpath=str_replace(":", "/", $Title);
145
146     $out_dir= DOKU_INC."data/tmp/".$xpath;
147     $zip->extractTo($out_dir);
148
149
150     // we always copy (add) media from ZWI
151     $xm="/data/media/".$Namespace."/";
152     $out_media = DOKU_INC.$xm;
153     $input_media = $out_dir . $xm;
154
155     $cmd="cp -rf $input_media $out_media";
156     shell_exec("mkdir -p " . $out_meta);
157     shell_exec($cmd);
158
159     // new article. Then copy meta and history
160   if  ($articleExist==false) {
161     // if this is new article, we also copy previous revisions and meta
162     $xm="/data/attic/".$Namespace."/";
163     $input_attic = $out_dir . $xm;
164     $out_attic = DOKU_INC.$xm;
165     $cmd="cp -rf $input_attic $out_attic";
166     #echo  $cmd;
167     shell_exec("mkdir -p " . $out_attic);
168     shell_exec($cmd);
169
170    // if this is new article, we also copy metadata
171     $xm="/data/meta/".$Namespace."/";
172     $input_meta = $out_dir . $xm;
173     $out_meta = DOKU_INC.$xm;
174     $cmd="cp -rf $input_meta $out_meta";
175     shell_exec("mkdir -p " . $out_meta);
176     shell_exec($cmd);
177
178     echo "Old revisions were imported from the ZWI file.<br>";
179
180   } //
181
182
183
184
185     echo "All media files for this article were imported<br>";
186     echo "<b>Done! Your article in the <a href='/dokuwiki/doku.php?id=articles'> Staged article area </a> </b>";
187
188
189
190
191
192
193
194     $zip->close();
195
196
197    // cleaning
198    //$cmd="rm -rf $out_dir";
199    //shell_exec($cmd);
200
201    unlink( $target_file  );
202   // reading zip file end
203    } else {
204        die("Sorry, there was an error uploading your file.");
205
206    }
207 }
208
209}
210
211
212echo file_get_contents(DOKU_INC.'/lib/plugins/zwidoku/html_end');
213
214?>
215