3 * FusionForge Documentation Manager
5 * Copyright 2000, Quentin Cregan/Sourceforge
6 * Copyright 2002-2003, Tim Perdue/GForge, LLC
7 * Copyright 2010-2011, Franck Villaume - Capgemini
8 * Copyright (C) 2010 Alain Peyrat - Alcatel-Lucent
9 * http://fusionforge.org
11 * This file is part of FusionForge. FusionForge is free software;
12 * you can redistribute it and/or modify it under the terms of the
13 * GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the Licence, or (at your option)
17 * FusionForge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 require_once('../env.inc.php');
30 require_once $gfcommon.'include/pre.php';
31 require_once $gfcommon.'docman/Document.class.php';
32 require_once $gfcommon.'docman/DocumentFactory.class.php';
33 require_once $gfcommon.'docman/DocumentGroupFactory.class.php';
34 require_once $gfcommon.'docman/include/utils.php';
36 $arr = explode('/', getStringFromServer('REQUEST_URI'));
40 $g = group_get_object($group_id);
41 if (!$g || !is_object($g)) {
43 } elseif ($g->isError()) {
44 exit_error($g->getErrorMessage(), 'docman');
47 if ($docid != 'backup' && $docid != 'webdav' && $docid != 'zip') {
48 session_require_perm('docman', $group_id, 'read');
49 $docname = urldecode($arr[5]);
51 $d = new Document($g, $docid);
52 if (!$d || !is_object($d)) {
53 exit_error(_('Document is not available.'), 'docman');
54 } elseif ($d->isError()) {
55 exit_error($d->getErrorMessage(), 'docman');
59 * If the served document has wrong relative links, then
60 * theses links may redirect to the same document with another
61 * name, this way a search engine may loop and stress the
64 * A workaround is to serve only the document if the given
67 if ($d->getFileName() != $docname) {
68 exit_error(_('No document to display - invalid or inactive document number'), 'docman');
71 header('Content-disposition: filename="'.str_replace('"', '', $d->getFileName()) . '"');
73 if (strstr($d->getFileType(), 'app')) {
74 header("Content-type: application/binary");
76 header("Content-type: ".$d->getFileType());
79 echo $d->getFileData();
81 } elseif ($docid === 'backup') {
82 session_require_perm('docman', $group_id, 'admin');
84 $df = new DocumentFactory($g);
86 exit_error($df->getErrorMessage(), 'docman');
88 $dgf = new DocumentGroupFactory($g);
90 exit_error($dgf->getErrorMessage(), 'docman');
92 $nested_groups = $dgf->getNested();
94 if ( $nested_groups != NULL ) {
95 $filename = 'docman-'.$g->getUnixName().'-'.$docid.'.zip';
96 $file = forge_get_config('data_path').'/'.$filename;
97 $zip = new ZipArchive;
98 if ( !$zip->open($file, ZIPARCHIVE::OVERWRITE)) {
99 exit_error(_('Unable to open zip archive for backup'),'docman');
102 if ( !docman_fill_zip($zip, $nested_groups, $df))
103 exit_error(_('Unable to fill zip archive for backup'), 'docman');
106 exit_error(_('Unable to close zip archive for backup'), 'docman');
108 header('Content-disposition: filename="'.$filename.'"');
109 header('Content-type: application/binary');
114 $warning_msg = _('No documents to backup.');
115 session_redirect('/docman/?group_id='.$group_id.'&view=admin&warning_msg='.urlencode($warning_msg));
117 } elseif ($docid === 'webdav') {
118 if (forge_get_config('use_webdav') && $g->useWebDav()) {
119 require_once $gfcommon.'docman/include/webdav.php';
120 $_SERVER['SCRIPT_NAME'] = '';
121 /* we need the group id for check authentification. */
122 $_SERVER["AUTH_TYPE"] = $group_id;
123 if (!isset($_SERVER['PHP_AUTH_USER'])) {
124 header('WWW-Authenticate: Basic realm="Webdav Access" (For anonymous access : click enter)');
125 header('HTTP/1.0 401 Unauthorized');
126 echo _('Webdav Access Canceled by user');
129 $server = new HTTP_WebDAV_Server_Docman;
130 $server->ServeRequest();
132 $warning_msg = _('No webdav interface enabled.');
133 session_redirect('/docman/?group_id='.$group_id.'&warning_msg='.urlencode($warning_msg));
135 } elseif ($docid === 'zip') {
136 session_require_perm('docman', $group_id, 'read');
137 if (extension_loaded('zip')) {
138 if ( $arr[5] === 'full' ) {
141 $dg = new DocumentGroup($g, $dirid);
143 exit_error($dg->getErrorMessage(), 'docman');
145 $df = new DocumentFactory($g);
147 exit_error($df->getErrorMessage(), 'docman');
149 $dgf = new DocumentGroupFactory($g);
151 exit_error($dgf->getErrorMessage(), 'docman');
153 $nested_groups = $dgf->getNested();
155 if ($dg->hasDocuments($nested_groups, $df)) {
156 $filename = 'docman-'.$g->getUnixName().'-'.$dg->getID().'.zip';
157 $file = forge_get_config('data_path').'/'.$filename;
158 $zip = new ZipArchive;
159 if ( !$zip->open($file, ZIPARCHIVE::OVERWRITE))
160 exit_error(_('Unable to open zip archive for download as zip'), 'docman');
162 // ugly workaround to get the files at doc_group_id level
163 $df->setDocGroupID($dg->getID());
164 $docs = $df->getDocuments(1); // no caching
165 if (is_array($docs) && count($docs) > 0) { // this group has documents
166 foreach ($docs as $doc) {
167 if ( !$zip->addFromString($doc->getFileName(),$doc->getFileData()))
171 if ( !docman_fill_zip($zip, $nested_groups, $df, $dg->getID()))
172 exit_error(_('Unable to fill zip archive for download as zip'), 'docman');
175 exit_error(_('Unable to close zip archive for download as zip'), 'docman');
177 header('Content-disposition: filename="'.$filename.'"');
178 header('Content-type: application/binary');
183 $warning_msg = _('This directory is empty.');
184 session_redirect('/docman/?group_id='.$group_id.'&view=listfile&dirid='.$dirid.'&warning_msg='.urlencode($warning_msg));
186 } elseif ( $arr[5] === 'selected' ) {
188 $arr_fileid = explode(',',$arr[7]);
189 $filename = 'docman-'.$g->getUnixName().'-selected-'.time().'.zip';
190 $file = forge_get_config('data_path').'/'.$filename;
191 $zip = new ZipArchive;
192 if ( !$zip->open($file, ZIPARCHIVE::OVERWRITE))
193 exit_error(_('Unable to open zip archive for download as zip'), 'docman');
195 foreach($arr_fileid as $docid) {
196 if (!empty($docid)) {
197 $d = new Document($g, $docid);
198 if (!$d || !is_object($d)) {
199 exit_error(_('Document is not available.'), 'docman');
200 } elseif ($d->isError()) {
201 exit_error($d->getErrorMessage(), 'docman');
204 if ( !$zip->addFromString($d->getFileName(),$d->getFileData()))
209 $warning_msg = _('No action to perform');
210 session_redirect('/docman/?group_id='.$group_id.'&view=listfile&dirid='.$dirid.'&warning_msg='.urlencode($warning_msg));
214 exit_error(_('Unable to close zip archive for download as zip'), 'docman');
216 header('Content-disposition: filename="'.$filename.'"');
217 header('Content-type: application/binary');
222 exit_error(_('No document to display - invalid or inactive document number.'), 'docman');
225 exit_error(_('PHP extension is missing.'), 'docman');
228 exit_error(_('No document to display - invalid or inactive document number.'), 'docman');
233 // c-file-style: "bsd"