Webdav Schnittstelle für DMS - Erstversion

This commit is contained in:
Andreas Österreicher
2012-03-12 14:36:53 +00:00
parent ded8485d69
commit 1ac791563c
319 changed files with 41687 additions and 6 deletions
+254
View File
@@ -0,0 +1,254 @@
<?php
/* Copyright (C) 2012 FH Technikum-Wien
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Authors: Andreas Oesterreicher <andreas.oesterreicher@technikum-wien.at>
*/
/**
* Webdav Directory fuer DMS
*/
require_once(dirname(__FILE__).'/../include/dms.class.php');
require_once(dirname(__FILE__).'/../include/benutzerberechtigung.class.php');
class DMSDirectory extends Sabre_DAV_Collection
{
private $myPath;
private $kategorie_kurzbz;
private $kategorie_bezeichnung;
private $auth;
function __construct($myPath, $auth=null)
{
if($myPath=='')
{
$this->kategorie_kurzbz=null;
$this->kategorie_bezeichnung='';
$this->myPath='/';
}
else
{
$this->myPath = $myPath;
//KategorieKurzbz ermitteln
$this->kategorie_kurzbz = $this->getKategorie($myPath);
//Kategorie Bezeichnung ermitteln
$this->kategorie_bezeichnung = mb_substr($myPath,mb_strrpos($myPath,"/"));
}
$this->auth = $auth;
}
function getUser()
{
return $this->auth->getCurrentUser();
}
/**
* ermittelt die Kategorie Kurzbz aus dem Pfadnamen
*
*/
function getKategorie($path)
{
$parts = explode("/",$path);
$parent = null;
foreach($parts as $kat)
{
$dms = new dms();
$dms->getKategorieFromBezeichnung($kat, $parent);
if(isset($dms->result[0]))
$parent = $dms->result[0]->kategorie_kurzbz;
//else
// error_log("error katbezsearch kat: $kat parent: $parent");
}
return $parent;
}
/**
* Liefert die Kindelemente des Ordners / der Kategorie
*/
function getChildren()
{
$dms = new dms();
//Kategorien holen
$dms->getKategorie($this->kategorie_kurzbz);
$children = array();
// Loop through the directory, and create objects for each node
foreach($dms->result as $row)
{
if($dms->isBerechtigtKategorie($row->kategorie_kurzbz, $this->getUser()))
$children[] = $this->getChild($row->bezeichnung);
}
if($this->kategorie_kurzbz!='')
{
//Dokumente holen
$dms->getDocuments($this->kategorie_kurzbz);
foreach($dms->result as $row)
{
if(!$dms->isLocked($row->dms_id) || $dms->isBerechtigt($row->dms_id, $this->getUser()))
{
$children[] = $this->getChild($row->name);
}
}
}
return $children;
}
/**
* Holt das Objekt mit dem entsprechenden Namen
*/
function getChild($name)
{
if($name!='')
{
$dms = new dms();
if($dms->getDocumentFromName($name, $this->kategorie_kurzbz) && count($dms->result)>0)
return new DMSFile($this->myPath.'/'.$name, $this->auth);
elseif($dms->getKategorieFromBezeichnung($name, $this->kategorie_kurzbz) && count($dms->result)>0)
return new DMSDirectory($this->myPath.'/'.$name, $this->auth);
else
{
// We have to throw a FileNotFound exception if the file didn't exist
throw new Sabre_DAV_Exception_FileNotFound('The file with name: ' . $name . ' could not be found');
}
}
else
return new DMSDirectory($this->myPath.'/'.$name,$this->auth);
}
/**
* Prueft ob ein Kindelement mit dem Namen existiert
*/
function childExists($name)
{
$dms = new dms();
if($dms->getDocumentFromName($name, $this->kategorie_kurzbz) && count($dms->result)>0)
return true;
elseif($dms->getKategorieFromBezeichnung($name, $this->kategorie_kurzbz) && count($dms->result)>0)
return true;
else
return false;
}
/**
* Liefert den Namen der Kategorie / des Ordners
*/
function getName()
{
return $this->kategorie_bezeichnung;
}
/**
* Loescht die Kategorie / den Ordner
*/
function delete()
{
$dms = new dms();
$rechte = new benutzerberechtigung();
$rechte->getBerechtigungen($this->getUser());
if($rechte->isBerechtigt('basis/dms',null, 'suid'))
{
if(!$dms->deleteKategorie($this->kategorie_kurzbz))
throw new Sabre_DAV_Exception_MethodNotAllowed('Failed '.$dms->errormsg);
}
else
throw new Sabre_DAV_Exception_MethodNotAllowed('Keine Berechtigung');
}
/**
* Aendert den Namen einer Kategorie
* @oaram $newName
*/
function setName($newName)
{
$dms = new dms();
if($dms->loadKategorie($this->kategorie_kurzbz))
{
$dms->bezeichnung = $newName;
$dms->kategorie_kurzbz = $newName;
$dms->beschreibung = $newName;
$this->kategorie_bezeichnung = $newName;
if(!$dms->saveKategorie(false))
{
throw new Sabre_DAV_Exception_FileNotFound('Rename Failed');
}
}
else
{
throw new Sabre_DAV_Exception_FileNotFound('Directory not found');
}
}
/**
* Erstellt eine neue Datei
*/
function createFile($name, $data=null)
{
$dms = new dms();
$pos = mb_strrpos($name,'.')+1;
if($pos>1)
$ext = '.'.mb_substr($name, $pos);
else
$ext ='';
$filename=uniqid();
$dms->version='0';
$dms->kategorie_kurzbz=$this->kategorie_kurzbz;
$dms->insertamum=date('Y-m-d H:i:s');
$dms->insertvon = $this->getUser();
//$dms->mimetype= mime_content_type(IMPORT_PATH.$importFile);
$dms->filename = $filename;
$dms->name = $name;
if($dms->save(true))
{
file_put_contents(DMS_PATH.$filename, $data);
if(!chgrp(DMS_PATH.$filename,'dms'))
echo 'CHGRP failed';
if(!chmod(DMS_PATH.$filename, 0774))
echo 'CHMOD failed';
exec('sudo chown wwwrun '.$filename);
}
else
throw new Sabre_DAV_Exception_MethodNotAllowed('Failed '.$dms->errormsg);
}
/**
* Erstellt ein neues Directory / Kategorie
* @param name Name des Ordners
*/
function CreateDirectory($name)
{
$dms = new dms();
if(!$dms->loadKategorie($name))
{
$dms->kategorie_kurzbz=$name;
$dms->bezeichnung = $name;
$dms->beschreibung = $name;
$dms->parent_kategorie_kurzbz = $this->kategorie_kurzbz;
if(!$dms->saveKategorie(true))
{
throw new Sabre_DAV_Exception_MethodNotAllowed('Failed '.$dms->errormsg);
}
}
else
{
throw new Sabre_DAV_Exception_MethodNotAllowed('Directory already exists');
}
}
}
?>
+206
View File
@@ -0,0 +1,206 @@
<?php
/* Copyright (C) 2012 FH Technikum-Wien
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Authors: Andreas Oesterreicher <andreas.oesterreicher@technikum-wien.at>
*/
/**
* Webdav File fuer DMS
*/
require_once(dirname(__FILE__).'/../include/dms.class.php');
require_once(dirname(__FILE__).'/../include/datum.class.php');
require_once(dirname(__FILE__).'/../include/benutzerberechtigung.class.php');
class DMSFile extends Sabre_DAV_File
{
private $myPath;
private $kategorie_kurzbz;
private $name;
private $dms_id;
private $auth;
function __construct($myPath, $auth=null)
{
$this->myPath = $myPath;
//Dateinamen ermitteln
$this->name = mb_substr($myPath, mb_strrpos($myPath,"/")+1);
//Kategorie ermitteln
$katpath = mb_substr($myPath,0,mb_strrpos($myPath,"/"));
$this->kategorie_kurzbz = $this->getKategorie($katpath);
//DMS ID ermitteln
$dms = new dms();
$dms->getDocumentFromName($this->name, $this->kategorie_kurzbz);
if(isset($dms->result[0]))
$this->dms_id = $dms->result[0]->dms_id;
//else
// error_log("fileNotFound $this->name in kat $this->kategorie_kurzbz");
$this->auth = $auth;
}
/**
* Liefert den eingeloggten User
*/
function getUser()
{
return $this->auth->getCurrentUser();
}
/**
* Parst die Kategorie aus dem Pfad
*/
function getKategorie($path)
{
$parts = explode($path, "/");
$parent = null;
foreach($parts as $kat)
{
$dms = new dms();
$dms->getKategorieFromBezeichnung($kat, $parent);
if(isset($dms->result[0]))
$parent = $dms->result[0]->kategorie_kurzbz;
}
return $parent;
}
/**
* Liefert den Dateinamen
*/
function getName()
{
return $this->name;
}
/**
* Liefert die Datei
*/
function get()
{
$dms = new dms();
$dms->load($this->dms_id);
return fopen(DMS_PATH.$dms->filename,'r');
}
/**
* Liefert die Dateigroesse
*/
function getSize()
{
$dms = new dms();
$dms->load($this->dms_id);
return filesize(DMS_PATH.$dms->filename);
}
/**
* Liefert einen eindeutige Identifier fuer die Datei
*/
function getETag()
{
$dms = new dms();
$dms->load($this->dms_id);
return '"' . md5_file(DMS_PATH.$dms->filename) . '"';
}
/**
* Entfernt eine Datei
*/
function delete()
{
$dms = new dms();
$rechte = new benutzerberechtigung();
$rechte->getBerechtigungen($this->getUser());
if($rechte->isBerechtigt('basis/dms',null,'suid'))
{
$dms->load($this->dms_id);
if(!$dms->deleteDms($this->dms_id))
{
throw new Sabre_DAV_Exception_FileNotFound('Failed '.$dms->errormsg);
}
else
{
unlink(DMS_PATH.$dms->filename);
}
}
else
{
throw new Sabre_DAV_Exception_MethodNotAllowed('Keine Berechtigung');
}
}
/**
* Benennt eine Datei um
*/
function setName($newName)
{
$dms = new dms();
if($dms->load($this->dms_id))
{
$dms->updateamum = date('Y-m-d H:i:s');
$dms->updatevon = $this->getUser();
$dms->name = $newName;
if(!$dms->save(false))
{
throw new Sabre_DAV_Exception_FileNotFound('Failed '.$dms->errormsg);
}
else
$this->name = $newName;
}
else
{
throw new Sabre_DAV_Exception_FileNotFound('Failed '.$dms->errormsg);
}
}
/**
* Liefert Timestamp der letzten Aenderung
*/
function getLastModified()
{
$dms = new dms();
$dms->load($this->dms_id);
$datum = new datum();
if($dms->updateamum!='')
$ts = $datum->mktime_fromtimestamp($dms->updateamum);
else
$ts = $datum->mktime_fromtimestamp($dms->insertamum);
return $ts;
}
/**
* Speichert Daten in eine Datei
* @param $data
*/
function put($data)
{
$dms = new dms();
if($dms->load($this->dms_id))
{
$dms->updateamum = date('Y-m-d H:i:s');
$dms->updatevon = $this->getUser();
file_put_contents(DMS_PATH.$dms->filename, $data);
$dms->save(false);
}
else
{
throw new Sabre_DAV_Exception_FileNotFound('Failed '.$dms->errormsg);
}
}
}
?>
+36
View File
@@ -0,0 +1,36 @@
<?php
/* Copyright (C) 2012 FH Technikum-Wien
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Authors: Andreas Oesterreicher <andreas.oesterreicher@technikum-wien.at>
*/
/**
* Authentifizierung fuer DMS Webdav Schnittstelle
*/
require_once(dirname(__FILE__).'/../config/cis.config.inc.php');
require_once(dirname(__FILE__).'/../include/functions.inc.php');
class myauth extends Sabre_DAV_Auth_Backend_AbstractBasic
{
function validateUserPass($username, $password)
{
if(checkldapuser($username,$password))
return true;
else
return false;
}
}
?>
+63
View File
@@ -0,0 +1,63 @@
<?php
/* Copyright (C) 2012 FH Technikum-Wien
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Authors: Andreas Oesterreicher <andreas.oesterreicher@technikum-wien.at>
*/
/**
* Webdav Server fuer DMS-Zugriff
*/
require_once('../config/cis.config.inc.php');
require_once('../include/sabredav/lib/Sabre/autoload.php');
require_once('DMSdirectory.class.php');
require_once('DMSfile.class.php');
require_once('auth.class.php');
$authBackend = new MyAuth();
// Creating the plugin. We're assuming that the realm
// name is called 'SabreDAV'.
$authPlugin = new Sabre_DAV_Auth_Plugin($authBackend,'FHTW');
// Change public to something else, if you are using a different directory for your files
$rootDirectory = new DMSDirectory('',$authPlugin);
// The server object is responsible for making sense out of the WebDAV protocol
$server = new Sabre_DAV_Server($rootDirectory);
// Adding the plugin to the server
$server->addPlugin($authPlugin);
// If your server is not on your webroot, make sure the following line has the correct information
// $server->setBaseUri('/~evert/mydavfolder'); // if its in some kind of home directory
// $server->setBaseUri('/dav/index.php/'); // if you can't use mod_rewrite, use index.php as a base uri
// $server->setBaseUri('/'); // ideally, SabreDAV lives on a root directory with mod_rewrite sending every request to index.php
$path = str_replace($_SERVER['DOCUMENT_ROOT'],'',__FILE__).'/';
$server->setBaseUri($path);
// The lock manager is reponsible for making sure users don't overwrite each others changes. Change 'data' to a different
// directory, if you're storing your data somewhere else.
//$lockBackend = new Sabre_DAV_Locks_Backend_File('data/locks');
//$lockPlugin = new Sabre_DAV_Locks_Plugin($lockBackend);
//$server->addPlugin($lockPlugin);
//GUI fuer Browser
$browser = new Sabre_DAV_Browser_Plugin();
$server->addPlugin($browser);
// All we need to do now, is to fire up the server
$server->exec();
?>