3 * FusionForge Documentation Manager
5 * Copyright 2010-2011, Franck Villaume - Capgemini
6 * Copyright 2012, Franck Villaume - TrivialDev
7 * http://fusionforge.org
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * webdav extended class based on pear package
27 * http://pear.php.net/package/HTTP_WebDAV_Server/
31 * INFORMATION : this PHP Webdav implementation is based on experience only.
32 * I did not find any helpful php documentation.
33 * I added as much as possible comments to explain how it works.
34 * Feel free to add you on input.
37 require_once "HTTP/WebDAV/Server.php";
39 class HTTP_WebDAV_Server_Docman extends HTTP_WebDAV_Server {
42 * checkAuth - implement checkAuth called by HTTP_WebDAV_Server
43 * to ensure authentification against user and pass
46 * @param string username
47 * @param string password
48 * @return bool success
50 function checkAuth($group_id, $user, $pass) {
51 $this->doWeUseDocman($group_id);
52 if (session_login_valid($user, $pass)) {
53 if (forge_check_perm('docman', $group_id, 'read')) {
63 * @todo Do a correct implementation
65 function HEAD(&$options) {
70 * PROPFIND - use by any webdav client like cadaver
71 * called by HTTP_WebDAV_Server
73 * @param array options passed by previous functions in HTTP_WebDAV_Server
74 * @param array files passed by previous functions in HTTP_WebDAV_Server
76 function PROPFIND(&$options, &$files) {
77 $arr_path = explode('/',$options['path']);
78 $group_id = $arr_path[3];
80 $this->doWeUseDocman($group_id);
83 * 4 is coming from the url: http://yourforge/docman/6/webdav/the/directory
84 * 1 = http://yourforge
88 * the rest is the path /the/directory
90 if ( 4 < count($arr_path)) {
92 for ($i = 5; $i < count($arr_path); $i++){
93 $subpath .= '/'.$arr_path[$i];
97 if (empty($subpath)) {
101 $analysed_path = $this->analyse($subpath, $group_id);
103 if ($analysed_path['isdir']) {
105 $files["files"] = array();
106 $path = $options['path'];
107 $res = db_query_params('select * from doc_groups where group_id = $1 and doc_group = $2',
108 array($group_id, $analysed_path['doc_group']));
112 $arr = db_fetch_array($res);
113 if ($arr['updatedate']) {
114 $lastmodifieddate = $arr['updatedate'];
116 $lastmodifieddate = $arr['createdate'];
118 $files["files"][$i] = array();
119 $files["files"][$i]["path"] = $path;
120 $files["files"][$i]["props"] = array();
121 $files["files"][$i]["props"][] = $this->mkprop("displayname", $arr['groupname']);
122 $files["files"][$i]["props"][] = $this->mkprop("creationdate", $arr['createdate']);
123 $files["files"][$i]["props"][] = $this->mkprop("getlastmodified", $lastmodifieddate);
124 $files["files"][$i]["props"][] = $this->mkprop("lastaccessed", '');
125 $files["files"][$i]["props"][] = $this->mkprop("ishidden", false);
126 $files["files"][$i]["props"][] = $this->mkprop("resourcetype", "collection");
127 $files["files"][$i]["props"][] = $this->mkprop("getcontenttype", "httpd/unix-directory");
128 $res = db_query_params('select * from doc_groups where group_id = $1 and parent_doc_group = $2',
129 array($group_id, $analysed_path['doc_group']));
133 while ($arr = db_fetch_array($res)) {
135 if ($arr['updatedate']) {
136 $lastmodifieddate = $arr['updatedate'];
138 $lastmodifieddate = $arr['createdate'];
140 $files["files"][$i] = array();
141 $files["files"][$i]["path"] = $path.'/'.$arr['groupname'];
142 $files["files"][$i]["props"] = array();
143 $files["files"][$i]["props"][] = $this->mkprop("displayname", $arr['groupname']);
144 $files["files"][$i]["props"][] = $this->mkprop("creationdate", $arr['createdate']);
145 $files["files"][$i]["props"][] = $this->mkprop("getlastmodified", $lastmodifieddate);
146 $files["files"][$i]["props"][] = $this->mkprop("lastaccessed", '');
147 $files["files"][$i]["props"][] = $this->mkprop("ishidden", false);
148 $files["files"][$i]["props"][] = $this->mkprop("resourcetype","collection");
149 $files["files"][$i]["props"][] = $this->mkprop("getcontenttype","httpd/unix-directory");
151 $res = db_query_params('select filename,filetype,filesize,createdate,updatedate from doc_data where group_id = $1 and doc_group = $2',
152 array($group_id, $analysed_path['doc_group']));
156 while ($arr = db_fetch_array($res)) {
158 if ($arr['updatedate']) {
159 $lastmodifieddate = $arr['updatedate'];
161 $lastmodifieddate = $arr['createdate'];
163 $files["files"][$i] = array();
164 $files["files"][$i]["path"] = $path.'/'.$arr['filename'];
165 $files["files"][$i]["props"] = array();
166 $files["files"][$i]["props"][] = $this->mkprop("displayname", $arr['filename']);
167 $files["files"][$i]["props"][] = $this->mkprop("creationdate", $arr['createdate']);
168 $files["files"][$i]["props"][] = $this->mkprop("getlastmodified", $lastmodifieddate);
169 $files["files"][$i]["props"][] = $this->mkprop("lastaccessed", '');
170 $files["files"][$i]["props"][] = $this->mkprop("ishidden", false);
171 $files["files"][$i]["props"][] = $this->mkprop("getcontentlength", $arr['filesize']);
172 $files["files"][$i]["props"][] = $this->mkprop("getcontenttype", $arr['filetype']);
180 * GET - use by http webdav client like your browser firefox
181 * called by HTTP_WebDAV_Server
183 * @param array options passed by previous functions in HTTP_WebDAV_Server
185 function GET(&$options) {
186 $arr_path = explode('/', $options['path']);
187 $group_id = $arr_path[3];
189 $this->doWeUseDocman($group_id);
191 if ( 4 < count($arr_path)) {
193 for ($i=5; $i<count($arr_path); $i++){
194 $subpath .= '/'.$arr_path[$i];
198 if (empty($subpath)) {
202 $analysed_path = $this->analyse($subpath, $group_id);
204 if ($analysed_path['isdir']) {
205 echo "<html><meta http-equiv='Content-Type' content='text/html charset=UTF-8' /><head><title>Index of ".htmlspecialchars($subpath)."</title></head>\n";
207 echo "<h1>Index of ".htmlspecialchars($subpath)."</h1>\n";
209 if ( '/' != $subpath ) {
210 if ('/' == strrchr($options['path'], '/')) {
211 $lastpath = substr($options['path'], 0, -1);
213 $lastpath = $options['path'];
215 $back_url = substr($options['path'], 0, strrpos($options['path'], strrchr($lastpath,'/')));
216 echo '<a href="'.util_make_uri($back_url).'">..</a>';
218 $res = db_query_params('select * from doc_groups where group_id = $1 and parent_doc_group = $2',
219 array($group_id, $analysed_path['doc_group']));
221 exit_error(_('webdav db error:').' '.db_error(),'docman');
223 if ( '/' != substr($subpath,-1)) {
226 while ($arr = db_fetch_array($res)) {
227 echo '<li><a href="'.util_make_uri('/docman/view.php/'.$group_id.'/webdav'.$subpath.$arr['groupname']).'">'.$arr['groupname'].'</a></li>';
229 $res = db_query_params('select filename, filetype from doc_data where group_id = $1 and doc_group = $2 and stateid = 1',
230 array($group_id, $analysed_path['doc_group']));
232 exit_error(_('webdav db error:').' '.db_error(),'docman');
234 while ($arr = db_fetch_array($res)) {
235 switch ($arr['filetype']) {
237 echo '<li><a href="'.$arr['filename'].'">'.$arr['filename'].'</a></li>';
241 echo '<li><a href="'.util_make_uri('/docman/view.php/'.$group_id.'/webdav'.$subpath.$arr['filename']).'">'.$arr['filename'].'</a></li>';
247 echo "</body></html>\n";
249 session_redirect('/docman/view.php/'.$group_id.'/'.$analysed_path['docid'].'/'.$analysed_path['filename']);
256 * analyse - find if the path is a file or a directory
258 * @param string the path to analyse
259 * @param int group id
260 * @return array the analysed path
262 function analyse($path, $group_id) {
263 $analysed_path['isdir'] = true;
264 $analysed_path['doc_group'] = 0;
265 $analysed_path['docid'] = NULL;
267 return $analysed_path;
270 $path_arr = explode('/', $path);
271 for ($i = 1; $i < count($path_arr); $i++) {
272 if ($path_arr[$i] == '') {
275 $analysed_path = $this->whatIsIt($path_arr[$i], $group_id, $analysed_path);
278 return $analysed_path;
282 * whatIsIt - do the analyse
284 * @param string the path to analyse
285 * @param int group id
286 * @param array the previous path analysed
287 * @return array the path analysed
289 function whatIsIt($string, $group_id, $path_array) {
290 $return_path_array['isdir'] = false;
291 $res = db_query_params('select doc_group from doc_groups where group_id = $1 and groupname = $2 and parent_doc_group = $3',
292 array($group_id, $string, $path_array['doc_group']));
294 exit_error(_('webdav db error:').' '.db_error(),'docman');
297 while ($arr = db_fetch_array($res)) {
298 $return_path_array['isdir'] = true;
299 $return_path_array['doc_group'] = $arr['doc_group'];
302 if ($return_path_array['isdir']) {
303 return $return_path_array;
306 $res = db_query_params('select docid from doc_data where group_id = $1 and doc_group = $2 and filename = $3',
307 array($group_id, $path_array['doc_group'], $string));
308 while ($arr = db_fetch_array($res)) {
309 $return_path_array['docid'] = $arr['docid'];
310 $return_path_array['filename'] = $string;
313 return $return_path_array;
317 * doWeUseDocman - verify if this group_id is using docman and webdav extension
318 * @param int group_id
319 * @return bool true on success
321 function doWeUseDocman($group_id) {
322 $g = group_get_object($group_id);
323 if (!$g || !is_object($g))
326 if (!$g->usesDocman())
329 if (!$g->useWebdav())
333 exit_error($g->getErrorMessage(), 'docman');