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 2012, Franck Villaume - TrivialDev
9 * Copyright (C) 2010 Alain Peyrat - Alcatel-Lucent
10 * http://fusionforge.org
12 * This file is part of FusionForge. FusionForge is free software;
13 * you can redistribute it and/or modify it under the terms of the
14 * GNU General Public License as published by the Free Software
15 * Foundation; either version 2 of the Licence, or (at your option)
18 * FusionForge is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 require_once('../env.inc.php');
31 require_once $gfcommon.'include/pre.php';
32 require_once $gfcommon.'docman/Document.class.php';
33 require_once $gfcommon.'docman/DocumentFactory.class.php';
34 require_once $gfcommon.'docman/DocumentGroupFactory.class.php';
35 require_once $gfcommon.'docman/include/utils.php';
37 $sysdebug_enable = false;
39 $arr = explode('/', getStringFromServer('REQUEST_URI'));
43 $g = group_get_object($group_id);
44 if (!$g || !is_object($g)) {
46 } elseif ($g->isError()) {
47 exit_error($g->getErrorMessage(), 'docman');
50 if ($docid != 'backup' && $docid != 'webdav' && $docid != 'zip') {
51 session_require_perm('docman', $group_id, 'read');
52 $docname = urldecode($arr[5]);
54 $d = new Document($g, $docid);
56 if (!$d || !is_object($d)) {
57 exit_error(_('Document is not available.'), 'docman');
58 } elseif ($d->isError()) {
59 exit_error($d->getErrorMessage(), 'docman');
63 * except for active (1), we need more right access than just read
65 switch ($d->getStateID()) {
70 session_require_perm('docman', $group_id, 'approve');
76 * If the served document has wrong relative links, then
77 * theses links may redirect to the same document with another
78 * name, this way a search engine may loop and stress the
81 if ($d->getFileName() != $docname) {
82 session_redirect('/docman/view.php/'.$group_id.'/'.$docid.'/'.urlencode($d->getFileName()));
85 header('Content-disposition: attachment; filename="'.str_replace('"', '', $d->getFileName()) . '"');
87 if (strstr($d->getFileType(), 'app')) {
88 header("Content-type: application/binary");
90 header("Content-type: ".$d->getFileType());
92 header("Content-Transfer-Encoding: binary");
94 echo $d->getFileData();
96 } elseif ($docid === 'backup') {
97 if (extension_loaded('zip')) {
98 session_require_perm('docman', $group_id, 'admin');
100 $df = new DocumentFactory($g);
102 exit_error($df->getErrorMessage(), 'docman');
104 $dgf = new DocumentGroupFactory($g);
106 exit_error($dgf->getErrorMessage(), 'docman');
108 $nested_groups = $dgf->getNested();
110 if ( $nested_groups != NULL ) {
111 $filename = 'docman-'.$g->getUnixName().'-'.$docid.'.zip';
112 $file = forge_get_config('data_path').'/'.$filename;
113 $zip = new ZipArchive;
114 if ( !$zip->open($file, ZIPARCHIVE::OVERWRITE)) {
115 exit_error(_('Unable to open zip archive for backup'), 'docman');
118 if ( !docman_fill_zip($zip, $nested_groups, $df))
119 exit_error(_('Unable to fill zip archive for backup'), 'docman');
122 exit_error(_('Unable to close zip archive for backup'), 'docman');
124 header('Content-disposition: attachment; filename="'.$filename.'"');
125 header('Content-type: application/zip');
126 header("Content-Transfer-Encoding: binary");
129 if(!readfile_chunked($file)) {
131 $error_msg = _('Unable to download backup file');
132 session_redirect('/docman/?group_id='.$group_id.'&view=admin&error_msg='.urlencode($error_msg));
136 $warning_msg = _('No documents to backup.');
137 session_redirect('/docman/?group_id='.$group_id.'&view=admin&warning_msg='.urlencode($warning_msg));
140 $warning_msg = _('Zip extension is missing: no backup function');
141 session_redirect('/docman/?group_id='.$group_id.'&view=admin&warning_msg='.urlencode($warning_msg));
143 } elseif ($docid === 'webdav') {
144 if (forge_get_config('use_webdav') && $g->useWebDav()) {
145 require_once $gfcommon.'docman/include/webdav.php';
146 $_SERVER['SCRIPT_NAME'] = '';
147 /* we need the group id for check authentification. */
148 $_SERVER["AUTH_TYPE"] = $group_id;
149 if (!isset($_SERVER['PHP_AUTH_USER'])) {
150 header('WWW-Authenticate: Basic realm="Webdav Access" (For anonymous access : click enter)');
151 header('HTTP/1.0 401 Unauthorized');
152 echo _('Webdav Access Canceled by user');
155 $server = new HTTP_WebDAV_Server_Docman;
156 $server->ServeRequest();
158 $warning_msg = _('No webdav interface enabled.');
159 session_redirect('/docman/?group_id='.$group_id.'&warning_msg='.urlencode($warning_msg));
161 } elseif ($docid === 'zip') {
162 session_require_perm('docman', $group_id, 'read');
163 if (extension_loaded('zip')) {
164 if ( $arr[5] === 'full' ) {
167 $dg = new DocumentGroup($g, $dirid);
169 exit_error($dg->getErrorMessage(), 'docman');
171 $df = new DocumentFactory($g);
173 exit_error($df->getErrorMessage(), 'docman');
175 $dgf = new DocumentGroupFactory($g);
177 exit_error($dgf->getErrorMessage(), 'docman');
179 $nested_groups = $dgf->getNested();
181 if ($dg->hasDocuments($nested_groups, $df)) {
182 $filename = 'docman-'.$g->getUnixName().'-'.$dg->getID().'.zip';
183 $file = forge_get_config('data_path').'/'.$filename;
184 $zip = new ZipArchive;
185 if ( !$zip->open($file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE))
186 exit_error(_('Unable to open zip archive for download as zip'), 'docman');
188 // ugly workaround to get the files at doc_group_id level
189 $df->setDocGroupID($dg->getID());
190 $docs = $df->getDocuments(1); // no caching
191 if (is_array($docs) && count($docs) > 0) { // this group has documents
192 foreach ($docs as $doc) {
193 if (!$zip->addFromString($doc->getFileName(), $doc->getFileData()))
194 exit_error(_('Unable to fill zipfile.'), 'docman');
197 if ( !docman_fill_zip($zip, $nested_groups, $df, $dg->getID()))
198 exit_error(_('Unable to fill zip archive for download as zip'), 'docman');
201 exit_error(_('Unable to close zip archive for download as zip'), 'docman');
203 header('Content-disposition: attachment; filename="'.$filename.'"');
204 header('Content-type: application/zip');
205 header("Content-Transfer-Encoding: binary");
208 if(!readfile_chunked($file)) {
210 $error_msg = _('Unable to download zip archive');
211 session_redirect('/docman/?group_id='.$group_id.'&view=admin&error_msg='.urlencode($error_msg));
215 $warning_msg = _('This documents folder is empty.');
216 session_redirect('/docman/?group_id='.$group_id.'&view=listfile&dirid='.$dirid.'&warning_msg='.urlencode($warning_msg));
218 } elseif ( $arr[5] === 'selected' ) {
220 $arr_fileid = explode(',',$arr[7]);
221 $filename = 'docman-'.$g->getUnixName().'-selected-'.time().'.zip';
222 $file = forge_get_config('data_path').'/'.$filename;
223 $zip = new ZipArchive;
224 if (!$zip->open($file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE))
225 exit_error(_('Unable to open zip archive for download as zip'), 'docman');
227 foreach($arr_fileid as $docid) {
228 if (!empty($docid)) {
229 $d = new Document($g, $docid);
230 if (!$d || !is_object($d)) {
231 exit_error(_('Document is not available.'), 'docman');
232 } elseif ($d->isError()) {
233 exit_error($d->getErrorMessage(), 'docman');
236 if (!$zip->addFromString($d->getFileName(), $d->getFileData()))
237 exit_error(_('Unable to fill zipfile.'), 'docman');
241 $warning_msg = _('No action to perform');
242 session_redirect('/docman/?group_id='.$group_id.'&view=listfile&dirid='.$dirid.'&warning_msg='.urlencode($warning_msg));
246 exit_error(_('Unable to close zip archive for download as zip'), 'docman');
248 header('Content-disposition: attachment; filename="'.$filename.'"');
249 header('Content-type: application/zip');
250 header("Content-Transfer-Encoding: binary");
253 if(!readfile_chunked($file)) {
255 $error_msg = _('Unable to download zip archive');
256 session_redirect('/docman/?group_id='.$group_id.'&view=admin&error_msg='.urlencode($error_msg));
260 exit_error(_('No document to display - invalid or inactive document number.'), 'docman');
263 exit_error(_('PHP extension is missing.'), 'docman');
266 exit_error(_('No document to display - invalid or inactive document number.'), 'docman');
271 // c-file-style: "bsd"