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 $sysdebug_enable = false;
38 $arr = explode('/', getStringFromServer('REQUEST_URI'));
42 $g = group_get_object($group_id);
43 if (!$g || !is_object($g)) {
45 } elseif ($g->isError()) {
46 exit_error($g->getErrorMessage(), 'docman');
49 if ($docid != 'backup' && $docid != 'webdav' && $docid != 'zip') {
50 session_require_perm('docman', $group_id, 'read');
51 $docname = urldecode($arr[5]);
53 $d = new Document($g, $docid);
55 if (!$d || !is_object($d)) {
56 exit_error(_('Document is not available.'), 'docman');
57 } elseif ($d->isError()) {
58 exit_error($d->getErrorMessage(), 'docman');
62 * except for active (1), we need more right access than just read
64 switch ($d->getStateID()) {
69 session_require_perm('docman', $group_id, 'approve');
75 * If the served document has wrong relative links, then
76 * theses links may redirect to the same document with another
77 * name, this way a search engine may loop and stress the
80 * A workaround is to serve only the document if the given
83 if ($d->getFileName() != $docname) {
84 exit_error(_('No document to display - invalid or inactive document number'), 'docman');
87 header('Content-disposition: attachment; filename="'.str_replace('"', '', $d->getFileName()) . '"');
89 if (strstr($d->getFileType(), 'app')) {
90 header("Content-type: application/binary");
92 header("Content-type: ".$d->getFileType());
94 header("Content-Transfer-Encoding: binary");
96 echo $d->getFileData();
98 } elseif ($docid === 'backup') {
99 if (extension_loaded('zip')) {
100 session_require_perm('docman', $group_id, 'admin');
102 $df = new DocumentFactory($g);
104 exit_error($df->getErrorMessage(), 'docman');
106 $dgf = new DocumentGroupFactory($g);
108 exit_error($dgf->getErrorMessage(), 'docman');
110 $nested_groups = $dgf->getNested();
112 if ( $nested_groups != NULL ) {
113 $filename = 'docman-'.$g->getUnixName().'-'.$docid.'.zip';
114 $file = forge_get_config('data_path').'/'.$filename;
115 $zip = new ZipArchive;
116 if ( !$zip->open($file, ZIPARCHIVE::OVERWRITE)) {
117 exit_error(_('Unable to open zip archive for backup'), 'docman');
120 if ( !docman_fill_zip($zip, $nested_groups, $df))
121 exit_error(_('Unable to fill zip archive for backup'), 'docman');
124 exit_error(_('Unable to close zip archive for backup'), 'docman');
126 header('Content-disposition: attachment; filename="'.$filename.'"');
127 header('Content-type: application/zip');
128 header("Content-Transfer-Encoding: binary");
131 if(!readfile_chunked($file)) {
133 $error_msg = _('Unable to download backup file');
134 session_redirect('/docman/?group_id='.$group_id.'&view=admin&error_msg='.urlencode($error_msg));
138 $warning_msg = _('No documents to backup.');
139 session_redirect('/docman/?group_id='.$group_id.'&view=admin&warning_msg='.urlencode($warning_msg));
142 $warning_msg = _('Zip extension is missing: no backup function');
143 session_redirect('/docman/?group_id='.$group_id.'&view=admin&warning_msg='.urlencode($warning_msg));
145 } elseif ($docid === 'webdav') {
146 if (forge_get_config('use_webdav') && $g->useWebDav()) {
147 require_once $gfcommon.'docman/include/webdav.php';
148 $_SERVER['SCRIPT_NAME'] = '';
149 /* we need the group id for check authentification. */
150 $_SERVER["AUTH_TYPE"] = $group_id;
151 if (!isset($_SERVER['PHP_AUTH_USER'])) {
152 header('WWW-Authenticate: Basic realm="Webdav Access" (For anonymous access : click enter)');
153 header('HTTP/1.0 401 Unauthorized');
154 echo _('Webdav Access Canceled by user');
157 $server = new HTTP_WebDAV_Server_Docman;
158 $server->ServeRequest();
160 $warning_msg = _('No webdav interface enabled.');
161 session_redirect('/docman/?group_id='.$group_id.'&warning_msg='.urlencode($warning_msg));
163 } elseif ($docid === 'zip') {
164 session_require_perm('docman', $group_id, 'read');
165 if (extension_loaded('zip')) {
166 if ( $arr[5] === 'full' ) {
169 $dg = new DocumentGroup($g, $dirid);
171 exit_error($dg->getErrorMessage(), 'docman');
173 $df = new DocumentFactory($g);
175 exit_error($df->getErrorMessage(), 'docman');
177 $dgf = new DocumentGroupFactory($g);
179 exit_error($dgf->getErrorMessage(), 'docman');
181 $nested_groups = $dgf->getNested();
183 if ($dg->hasDocuments($nested_groups, $df)) {
184 $filename = 'docman-'.$g->getUnixName().'-'.$dg->getID().'.zip';
185 $file = forge_get_config('data_path').'/'.$filename;
186 $zip = new ZipArchive;
187 if ( !$zip->open($file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE))
188 exit_error(_('Unable to open zip archive for download as zip'), 'docman');
190 // ugly workaround to get the files at doc_group_id level
191 $df->setDocGroupID($dg->getID());
192 $docs = $df->getDocuments(1); // no caching
193 if (is_array($docs) && count($docs) > 0) { // this group has documents
194 foreach ($docs as $doc) {
195 if (!$zip->addFromString($doc->getFileName(),$doc->getFileData()))
196 exit_error(_('Unable to fill zipfile.'), 'docman');
199 if ( !docman_fill_zip($zip, $nested_groups, $df, $dg->getID()))
200 exit_error(_('Unable to fill zip archive for download as zip'), 'docman');
203 exit_error(_('Unable to close zip archive for download as zip'), 'docman');
205 header('Content-disposition: attachment; filename="'.$filename.'"');
206 header('Content-type: application/zip');
207 header("Content-Transfer-Encoding: binary");
210 if(!readfile_chunked($file)) {
212 $error_msg = _('Unable to download zip archive');
213 session_redirect('/docman/?group_id='.$group_id.'&view=admin&error_msg='.urlencode($error_msg));
217 $warning_msg = _('This documents folder is empty.');
218 session_redirect('/docman/?group_id='.$group_id.'&view=listfile&dirid='.$dirid.'&warning_msg='.urlencode($warning_msg));
220 } elseif ( $arr[5] === 'selected' ) {
222 $arr_fileid = explode(',',$arr[7]);
223 $filename = 'docman-'.$g->getUnixName().'-selected-'.time().'.zip';
224 $file = forge_get_config('data_path').'/'.$filename;
225 $zip = new ZipArchive;
226 if (!$zip->open($file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE))
227 exit_error(_('Unable to open zip archive for download as zip'), 'docman');
229 foreach($arr_fileid as $docid) {
230 if (!empty($docid)) {
231 $d = new Document($g, $docid);
232 if (!$d || !is_object($d)) {
233 exit_error(_('Document is not available.'), 'docman');
234 } elseif ($d->isError()) {
235 exit_error($d->getErrorMessage(), 'docman');
238 if (!$zip->addFromString($d->getFileName(), $d->getFileData()))
239 exit_error(_('Unable to fill zipfile.'), 'docman');
243 $warning_msg = _('No action to perform');
244 session_redirect('/docman/?group_id='.$group_id.'&view=listfile&dirid='.$dirid.'&warning_msg='.urlencode($warning_msg));
248 exit_error(_('Unable to close zip archive for download as zip'), 'docman');
250 header('Content-disposition: attachment; filename="'.$filename.'"');
251 header('Content-type: application/zip');
252 header("Content-Transfer-Encoding: binary");
255 if(!readfile_chunked($file)) {
257 $error_msg = _('Unable to download zip archive');
258 session_redirect('/docman/?group_id='.$group_id.'&view=admin&error_msg='.urlencode($error_msg));
262 exit_error(_('No document to display - invalid or inactive document number.'), 'docman');
265 exit_error(_('PHP extension is missing.'), 'docman');
268 exit_error(_('No document to display - invalid or inactive document number.'), 'docman');
273 // c-file-style: "bsd"