Skip to content

Commit

Permalink
add export data feature with authorisation by token
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent Kaisser committed May 9, 2019
1 parent bb97f39 commit e29074a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ COPY ./assets/images/* ${HTML_DIR}/
# The files uploaded are in the data volume
RUN mv ./images ./images.origin && ln -s /var/www/data/images ./images

COPY ./export_data.php ../

# Remove configuration by web
#RUN rm -rf mw-config

Expand Down
44 changes: 44 additions & 0 deletions export_data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
$excludeArgs = "--exclude=data/log* --exclude=data/images/archive*";
$workingDir = "/var/www/";
$exportDir = "data";
$wgSitename = "mywiki";
$wgLanguageCode = "all";
$IP = "w";
$tokenFile = "/var/www/data/.export_token";

if( file_exists($tokenFile) ) {
trim(fgets(fopen($tokenFile, 'r')), " \t\n\r\0\x0B");
}else{
http_response_code(404);
exit();
}

if ( $accessToken === $_GET["token"] ) {

include "$IP/LocalSettings.custom.php" ;

$filename=strtolower($wgSitename."-".$wgLanguageCode."_".date('Y-m-d'));

if (isset($_GET["format"]) && $_GET["format"] == "tar") {
$type="x-tar";
$ext="tar";
$cmd="tar -C ".$workingDir." -c ".$excludeArgs." ".$exportDir;
} else {
$type="x-bzip2";
$ext="tar.bz2";
$cmd="tar -C ".$workingDir." -cj ".$excludeArgs." ".$exportDir;
}

$headerContentType = "Content-Type: application/".$type;
$headerContentDisposition = "Content-Disposition: attachment; filename=\"".$filename.".".$ext."\"";

header($headerContentType);
header($headerContentDisposition);
passthru($cmd,$err);

} else
http_response_code(401);

exit();
?>

0 comments on commit e29074a

Please sign in to comment.