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-2012 Alain Peyrat - Alcatel-Lucent
9 * Copyright 2012, Franck Villaume - TrivialDev
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'));
40 $group_id = (int) $arr[3];
41 $docid = isset($arr[4])? $arr[4]: '';
43 $g = group_get_object($group_id);
44 if (!$g || !is_object($g)) {
46 } elseif ($g->isError()) {
47 if ($g->isPermissionDeniedError()) {
48 exit_permission_denied();
50 exit_error($g->getErrorMessage(), 'docman');
53 if (is_numeric($docid)) {
54 session_require_perm('docman', $group_id, 'read');
55 $docname = urldecode($arr[5]);
57 $d = new Document($g, $docid);
59 if (!$d || !is_object($d)) {
60 exit_error(_('Document is not available.'), 'docman');
61 } elseif ($d->isError()) {
62 exit_error($d->getErrorMessage(), 'docman');
66 * except for active (1), we need more right access than just read
68 switch ($d->getStateID()) {
73 session_require_perm('docman', $group_id, 'approve');
79 * If the served document has wrong relative links, then
80 * theses links may redirect to the same document with another
81 * name, this way a search engine may loop and stress the
84 if ($d->getFileName() != $docname) {
85 session_redirect('/docman/view.php/'.$group_id.'/'.$docid.'/'.urlencode($d->getFileName()));
88 header('Content-disposition: attachment; filename="'.str_replace('"', '', $d->getFileName()) . '"');
90 if (strstr($d->getFileType(), 'app')) {
91 header("Content-type: application/binary");
93 header("Content-type: ".$d->getFileType());
95 header("Content-Transfer-Encoding: binary");
97 echo $d->getFileData();
99 } elseif ($docid === 'backup') {
100 if (extension_loaded('zip')) {
101 session_require_perm('docman', $group_id, 'admin');
103 $df = new DocumentFactory($g);
105 exit_error($df->getErrorMessage(), 'docman');
107 $dgf = new DocumentGroupFactory($g);
109 exit_error($dgf->getErrorMessage(), 'docman');
111 $nested_groups = $dgf->getNested();
113 if ( $nested_groups != NULL ) {
114 $filename = 'docman-'.$g->getUnixName().'-'.$docid.'.zip';
115 $file = forge_get_config('data_path').'/'.$filename;
116 $zip = new ZipArchive;
117 if ( !$zip->open($file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)) {
118 exit_error(_('Unable to open zip archive for backup'), 'docman');
121 if ( !docman_fill_zip($zip, $nested_groups, $df))
122 exit_error(_('Unable to fill zip archive for backup'), 'docman');
125 exit_error(_('Unable to close zip archive for backup'), 'docman');
127 header('Content-disposition: attachment; filename="'.$filename.'"');
128 header('Content-type: application/zip');
129 header("Content-Transfer-Encoding: binary");
132 if(!readfile_chunked($file)) {
134 $error_msg = _('Unable to download backup file');
135 session_redirect('/docman/?group_id='.$group_id.'&view=admin&error_msg='.urlencode($error_msg));
139 $warning_msg = _('No documents to backup.');
140 session_redirect('/docman/?group_id='.$group_id.'&view=admin&warning_msg='.urlencode($warning_msg));
143 $warning_msg = _('Zip extension is missing: no backup function');
144 session_redirect('/docman/?group_id='.$group_id.'&view=admin&warning_msg='.urlencode($warning_msg));
146 } elseif ($docid === 'webdav') {
147 if (forge_get_config('use_webdav') && $g->useWebDav()) {
148 require_once $gfcommon.'docman/include/webdav.php';
149 $_SERVER['SCRIPT_NAME'] = '';
150 /* we need the group id for check authentification. */
151 $_SERVER["AUTH_TYPE"] = $group_id;
152 if (!isset($_SERVER['PHP_AUTH_USER'])) {
153 header('WWW-Authenticate: Basic realm="Webdav Access" (For anonymous access : click enter)');
154 header('HTTP/1.0 401 Unauthorized');
155 echo _('Webdav Access Canceled by user');
158 $server = new HTTP_WebDAV_Server_Docman;
159 $server->ServeRequest();
161 $warning_msg = _('No webdav interface enabled.');
162 session_redirect('/docman/?group_id='.$group_id.'&warning_msg='.urlencode($warning_msg));
164 } elseif ($docid === 'zip') {
165 session_require_perm('docman', $group_id, 'read');
166 if (extension_loaded('zip')) {
167 if ( $arr[5] === 'full' ) {
170 $dg = new DocumentGroup($g, $dirid);
172 exit_error($dg->getErrorMessage(), 'docman');
174 $df = new DocumentFactory($g);
176 exit_error($df->getErrorMessage(), 'docman');
178 $dgf = new DocumentGroupFactory($g);
180 exit_error($dgf->getErrorMessage(), 'docman');
182 $nested_groups = $dgf->getNested();
184 if ($dg->hasDocuments($nested_groups, $df)) {
185 $filename = 'docman-'.$g->getUnixName().'-'.$dg->getID().'.zip';
186 $file = forge_get_config('data_path').'/'.$filename;
187 $zip = new ZipArchive;
188 if ( !$zip->open($file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE))
189 exit_error(_('Unable to open zip archive for download as zip'), 'docman');
191 // ugly workaround to get the files at doc_group_id level
192 $df->setDocGroupID($dg->getID());
193 $docs = $df->getDocuments(1); // no caching
194 if (is_array($docs) && count($docs) > 0) { // this group has documents
195 foreach ($docs as $doc) {
196 if (!$zip->addFromString(iconv("UTF-8", "ASCII//TRANSLIT", $doc->getFileName()), $doc->getFileData()))
197 exit_error(_('Unable to fill zipfile.'), 'docman');
200 if ( !docman_fill_zip($zip, $nested_groups, $df, $dg->getID()))
201 exit_error(_('Unable to fill zip archive for download as zip'), 'docman');
204 exit_error(_('Unable to close zip archive for download as zip'), 'docman');
206 header('Content-disposition: attachment; filename="'.$filename.'"');
207 header('Content-type: application/zip');
208 header("Content-Transfer-Encoding: binary");
211 if(!readfile_chunked($file)) {
213 $error_msg = _('Unable to download zip archive');
214 session_redirect('/docman/?group_id='.$group_id.'&view=admin&error_msg='.urlencode($error_msg));
218 $warning_msg = _('This documents folder is empty.');
219 session_redirect('/docman/?group_id='.$group_id.'&view=listfile&dirid='.$dirid.'&warning_msg='.urlencode($warning_msg));
221 } elseif ( $arr[5] === 'selected' ) {
223 $arr_fileid = explode(',',$arr[7]);
224 $filename = 'docman-'.$g->getUnixName().'-selected-'.time().'.zip';
225 $file = forge_get_config('data_path').'/'.$filename;
226 $zip = new ZipArchive;
227 if (!$zip->open($file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE))
228 exit_error(_('Unable to open zip archive for download as zip'), 'docman');
230 foreach($arr_fileid as $docid) {
231 if (!empty($docid)) {
232 $d = new Document($g, $docid);
233 if (!$d || !is_object($d)) {
234 exit_error(_('Document is not available.'), 'docman');
235 } elseif ($d->isError()) {
236 exit_error($d->getErrorMessage(), 'docman');
239 if (!$zip->addFromString(iconv("UTF-8", "ASCII//TRANSLIT", $d->getFileName()), $d->getFileData()))
240 exit_error(_('Unable to fill zipfile.'), 'docman');
244 $warning_msg = _('No action to perform');
245 session_redirect('/docman/?group_id='.$group_id.'&view=listfile&dirid='.$dirid.'&warning_msg='.urlencode($warning_msg));
249 exit_error(_('Unable to close zip archive for download as zip'), 'docman');
251 header('Content-disposition: attachment; filename="'.$filename.'"');
252 header('Content-type: application/zip');
253 header("Content-Transfer-Encoding: binary");
256 if(!readfile_chunked($file)) {
258 $error_msg = _('Unable to download zip archive');
259 session_redirect('/docman/?group_id='.$group_id.'&view=admin&error_msg='.urlencode($error_msg));
263 exit_error(_('No document to display - invalid or inactive document number.'), 'docman');
266 exit_error(_('PHP extension is missing.'), 'docman');
269 exit_error(_('No document to display - invalid or inactive document number.'), 'docman');
274 // c-file-style: "bsd"