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 ( $arr[5] === 'full' ) {
140 $dg = new DocumentGroup($g, $dirid);
142 exit_error($dg->getErrorMessage(), 'docman');
144 $df = new DocumentFactory($g);
146 exit_error($df->getErrorMessage(), 'docman');
148 $dgf = new DocumentGroupFactory($g);
150 exit_error($dgf->getErrorMessage(), 'docman');
152 $nested_groups = $dgf->getNested();
154 if ($dg->hasDocuments($nested_groups, $df)) {
155 $filename = 'docman-'.$g->getUnixName().'-'.$dg->getID().'.zip';
156 $file = forge_get_config('data_path').'/'.$filename;
157 $zip = new ZipArchive;
158 if ( !$zip->open($file, ZIPARCHIVE::OVERWRITE))
159 exit_error(_('Unable to open zip archive for download as zip'),'docman');
161 // ugly workaround to get the files at doc_group_id level
162 $df->setDocGroupID($dg->getID());
163 $docs = $df->getDocuments(1); // no caching
164 if (is_array($docs) && count($docs) > 0) { // this group has documents
165 foreach ($docs as $doc) {
166 if ( !$zip->addFromString($doc->getFileName(),$doc->getFileData()))
170 if ( !docman_fill_zip($zip, $nested_groups, $df, $dg->getID()))
171 exit_error(_('Unable to fill zip archive for download as zip'), 'docman');
174 exit_error(_('Unable to close zip archive for download as zip'), 'docman');
176 header('Content-disposition: filename="'.$filename.'"');
177 header('Content-type: application/binary');
182 $warning_msg = _('This directory is empty.');
183 session_redirect('/docman/?group_id='.$group_id.'&view=listfile&dirid='.$dirid.'&warning_msg='.urlencode($warning_msg));
185 } elseif ( $arr[5] === 'selected' ) {
186 $arr_fileid = explode(',',$arr[6]);
187 $filename = 'docman-'.$g->getUnixName().'-selected-'.time().'.zip';
188 $file = forge_get_config('data_path').'/'.$filename;
189 $zip = new ZipArchive;
190 if ( !$zip->open($file, ZIPARCHIVE::OVERWRITE))
191 exit_error(_('Unable to open zip archive for download as zip'),'docman');
193 foreach($arr_fileid as $docid) {
194 $d = new Document($g, $docid);
195 if (!$d || !is_object($d)) {
196 exit_error(_('Document is not available.'), 'docman');
197 } elseif ($d->isError()) {
198 exit_error($d->getErrorMessage(), 'docman');
201 if ( !$zip->addFromString($d->getFileName(),$d->getFileData()))
205 exit_error(_('Unable to close zip archive for download as zip'), 'docman');
207 header('Content-disposition: filename="'.$filename.'"');
208 header('Content-type: application/binary');
213 exit_error(_('No document to display - invalid or inactive document number.'), 'docman');
216 exit_error(_('No document to display - invalid or inactive document number.'), 'docman');
221 // c-file-style: "bsd"