3 * FusionForge Documentation Manager
5 * Copyright 2010-2011, Franck Villaume - Capgemini
6 * http://fusionforge.org
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 /* webdav extended class based on pear package */
25 /* http://pear.php.net/package/HTTP_WebDAV_Server/ */
27 require_once "HTTP/WebDAV/Server.php";
29 class HTTP_WebDAV_Server_Docman extends HTTP_WebDAV_Server {
32 * checkAuth - implement checkAuth called by HTTP_WebDAV_Server
33 * to ensure authentification against user and pass
36 * @param string username
37 * @param string password
38 * @return bool success
40 function checkAuth($group_id, $user, $pass) {
41 $g = group_get_object($group_id);
42 if (!$g || !is_object($g))
45 /* is this group using docman ? */
46 if (!$g->usesDocman())
55 if (!session_login_valid($user,$pass)) {
56 if (forge_check_perm('docman',$group_id,'read')) {
61 $u = &user_get_object_by_name($user);
62 foreach ($u->getGroups() as $key => $memberOfThisGroup) {
63 if ($memberOfThisGroup->getID() == $group_id) {
73 * @todo Do a correct implementation
75 function HEAD(&$options) {
80 * PROPFIND - use by any webdav client like cadaver
81 * called by HTTP_WebDAV_Server
83 * @param array options passed by previous functions in HTTP_WebDAV_Server
84 * @param array files passed by previous functions in HTTP_WebDAV_Server
86 function PROPFIND(&$options, &$files) {
87 $arr_path = explode('/',$options['path']);
88 $group_id = $arr_path[3];
93 $g = group_get_object($group_id);
94 if (!$g || !is_object($g))
97 /* is this group using docman ? */
98 if (!$g->usesDocman())
101 if (!$g->useWebdav())
107 if ( 4 < count($arr_path)) {
109 for ($i=5; $i<count($arr_path); $i++){
110 $subpath .= '/'.$arr_path[$i];
114 if (empty($subpath)) {
118 $analysed_path = $this->analyse($subpath,$group_id);
120 if ($analysed_path['isdir']) {
122 $files["files"] = array();
123 $path = $options['path'];
124 $name = basename($path);
125 $files["files"][$i] = array();
126 $files["files"][$i]["path"] = $path;
127 $files["files"][$i]["props"] = array();
128 $files["files"][$i]["props"][] = $this->mkprop("displayname", $name);
129 $files["files"][$i]["props"][] = $this->mkprop("creationdate", '');
130 $files["files"][$i]["props"][] = $this->mkprop("getlastmodified", '');
131 $files["files"][$i]["props"][] = $this->mkprop("lastaccessed", '');
132 $files["files"][$i]["props"][] = $this->mkprop("ishidden", false);
133 $files["files"][$i]["props"][] = $this->mkprop("resourcetype", "collection");
134 $files["files"][$i]["props"][] = $this->mkprop("getcontenttype", "httpd/unix-directory");
135 $res = db_query_params('select * from doc_groups where group_id = $1 and parent_doc_group = $2',
136 array($group_id, $analysed_path['doc_group']));
140 while ($arr = db_fetch_array($res)) {
142 $files["files"][$i] = array();
143 $files["files"][$i]["path"] = $path.'/'.$arr['groupname'];
144 $files["files"][$i]["props"] = array();
145 $files["files"][$i]["props"][] = $this->mkprop("displayname", $arr['groupname']);
146 $files["files"][$i]["props"][] = $this->mkprop("creationdate", '');
147 $files["files"][$i]["props"][] = $this->mkprop("getlastmodified", '');
148 $files["files"][$i]["props"][] = $this->mkprop("lastaccessed", '');
149 $files["files"][$i]["props"][] = $this->mkprop("ishidden", false);
150 $files["files"][$i]["props"][] = $this->mkprop("resourcetype","collection");
151 $files["files"][$i]["props"][] = $this->mkprop("getcontenttype","httpd/unix-directory");
153 $res = db_query_params('select filename,filetype,filesize,createdate,updatedate from doc_data where group_id = $1 and doc_group = $2',
154 array($group_id, $analysed_path['doc_group']));
158 while ($arr = db_fetch_array($res)) {
160 $files["files"][$i] = array();
161 $files["files"][$i]["path"] = $path.'/'.$arr['filename'];
162 $files["files"][$i]["props"] = array();
163 $files["files"][$i]["props"][] = $this->mkprop("displayname", $arr['filename']);
164 $files["files"][$i]["props"][] = $this->mkprop("creationdate", $arr['createdate']);
165 $files["files"][$i]["props"][] = $this->mkprop("getlastmodified", $arr['updatedate']);
166 $files["files"][$i]["props"][] = $this->mkprop("lastaccessed", '');
167 $files["files"][$i]["props"][] = $this->mkprop("ishidden", false);
168 $files["files"][$i]["props"][] = $this->mkprop("getcontentlength", $arr['filesize']);
169 $files["files"][$i]["props"][] = $this->mkprop("getcontenttype", $arr['filetype']);
176 * GET - use by http webdav client like your browser firefox
177 * called by HTTP_WebDAV_Server
179 * @param array options passed by previous functions in HTTP_WebDAV_Server
181 function GET(&$options) {
182 $arr_path = explode('/', $options['path']);
183 $group_id = $arr_path[3];
188 $g = group_get_object($group_id);
189 if (!$g || !is_object($g))
192 /* is this group using docman ? */
193 if (!$g->usesDocman())
196 if (!$g->useWebdav())
200 exit_error($g->getErrorMessage(), 'docman');
202 if ( 4 < count($arr_path)) {
204 for ($i=5; $i<count($arr_path); $i++){
205 $subpath .= '/'.$arr_path[$i];
209 if (empty($subpath)) {
213 $analysed_path = $this->analyse($subpath, $group_id);
215 if ($analysed_path['isdir']) {
216 echo '<?xml version="1.0" encoding="utf-8"?>';
217 echo "<html><head><title>Index of ".htmlspecialchars($subpath)."</title></head>\n";
219 echo "<h1>Index of ".htmlspecialchars($subpath)."</h1>\n";
221 if ( '/' != $subpath ) {
222 if ('/' == strrchr($options['path'], '/')) {
223 $lastpath = substr($options['path'], 0, -1);
225 $lastpath = $options['path'];
227 $back_url = substr($options['path'], 0, strrpos($options['path'], strrchr($lastpath,'/')));
228 echo '<a href="'.util_make_uri($back_url).'">..</a>';
230 $res = db_query_params('select * from doc_groups where group_id = $1 and parent_doc_group = $2',
231 array($group_id, $analysed_path['doc_group']));
233 exit_error(_('webdav db error:').' '.db_error(),'docman');
235 if ( '/' != substr($subpath,-1)) {
238 while ($arr = db_fetch_array($res)) {
239 echo '<li><a href="'.util_make_uri('/docman/view.php/'.$group_id.'/webdav'.$subpath.$arr['groupname']).'">'.$arr['groupname'].'</a></li>';
241 $res = db_query_params('select filename, filetype from doc_data where group_id = $1 and doc_group = $2 and stateid = 1',
242 array($group_id, $analysed_path['doc_group']));
244 exit_error(_('webdav db error:').' '.db_error(),'docman');
246 while ($arr = db_fetch_array($res)) {
247 switch ($arr['filetype']) {
249 echo '<li><a href="'.$arr['filename'].'">'.$arr['filename'].'</a></li>';
253 echo '<li><a href="'.util_make_uri('/docman/view.php/'.$group_id.'/webdav'.$subpath.$arr['filename']).'">'.$arr['filename'].'</a></li>';
259 echo "</body></html>\n";
261 session_redirect('/docman/view.php/'.$group_id.'/'.$analysed_path['docid'].'/'.$analysed_path['filename']);
268 * analyse - find if the path is a file or a directory
270 * @param string the path to analyse
271 * @param int group id
272 * @return array the analysed path
274 function analyse($path, $group_id) {
275 $analysed_path['isdir'] = true;
276 $analysed_path['doc_group'] = 0;
277 $analysed_path['docid'] = NULL;
279 return $analysed_path;
282 $path_arr = explode('/', $path);
283 for ($i = 1; $i < count($path_arr); $i++) {
284 if ($path_arr[$i] == '') {
287 $analysed_path = $this->whatIsIt($path_arr[$i], $group_id, $analysed_path);
289 return $analysed_path;
293 * whatIsIt - do the analyse
295 * @param string the path to analyse
296 * @param int group id
297 * @param array the previous path analysed
298 * @return array the path analysed
300 function whatIsIt($string, $group_id, $path_array) {
301 $return_path_array['isdir'] = false;
302 $res = db_query_params('select doc_group from doc_groups where group_id = $1 and groupname = $2 and parent_doc_group = $3',
303 array($group_id, $string, $path_array['doc_group']));
305 exit_error(_('webdav db error:').' '.db_error(),'docman');
308 while ($arr = db_fetch_array($res)) {
309 $return_path_array['isdir'] = true;
310 $return_path_array['doc_group'] = $arr['doc_group'];
313 if ($return_path_array['isdir']) {
314 return $return_path_array;
317 $res = db_query_params('select docid from doc_data where group_id = $1 and doc_group = $2 and filename = $3',
318 array($group_id, $path_array['doc_group'], $string));
319 while ($arr = db_fetch_array($res)) {
320 $return_path_array['docid'] = $arr['docid'];
321 $return_path_array['filename'] = $string;
324 return $return_path_array;