Browse Source

Dav : suite

Valentin CARRUESCO 4 years ago
parent
commit
5e5c6fb5fd
4 changed files with 2082 additions and 99 deletions
  1. 19 0
      class/User.class.php
  2. 36 1
      function.php
  3. 84 64
      plugin/hackpoint/hackpoint.plugin.php
  4. 1943 34
      plugin/hackpoint/logs/dav-logs.txt

+ 19 - 0
class/User.class.php

@@ -170,6 +170,25 @@ class User extends Entity
         return in_array($id, array_keys($this->firms));
     }
 
+        //Lance les exception appropriées en fonction du droit ou des droits spécifiés
+    // ex : User::checkAccess('document','configure');
+    public static function check_access($section,$right){
+        global $myUser;
+        if(!isset($myUser) || !is_object($myUser) || !$myUser->connected()) throw new Exception("Contrôle d'accès - Vous devez être connecté",401);
+        if(!$myUser->can($section,$right)) throw new Exception("Contrôle d'accès - Permissions insuffisantes",403);
+    }
+
+    public function hasRank($rankId){
+        if($this->superadmin) return true;
+        $rankIds = array();
+        global $myFirm;
+        if(empty($this->ranks) || !isset($this->ranks[$myFirm->id])) return false;
+        foreach ($this->ranks[$myFirm->id] as $rank)
+            $rankIds[$rank->id] = true;
+        return isset($rankIds[$rankId]);
+    }
+
+
     public function getAvatar($getPath = false){
         $avatar = 'img/default-avatar.png';
         $files = glob(__ROOT__.FILE_PATH.AVATAR_PATH.$this->login.'.{jpg,png,jpeg,gif}',GLOB_BRACE);

+ 36 - 1
function.php

@@ -1300,4 +1300,39 @@ function escape_json_string($value) { # list from www.json.org: (\b backspace, \
     $result = str_replace($escapers, $replacements, $value);
     return $result;
 }
-?>
+
+
+if( !function_exists('apache_request_headers') ) {
+	///
+	function apache_request_headers() {
+	  $arh = array();
+	  $rx_http = '/\AHTTP_/';
+	  foreach($_SERVER as $key => $val) {
+	    if( preg_match($rx_http, $key) ) {
+	      $arh_key = preg_replace($rx_http, '', $key);
+	      $rx_matches = array();
+	      // do some nasty string manipulations to restore the original letter case
+	      // this should work in most cases
+	      $rx_matches = explode('_', $arh_key);
+	      if( count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
+	        foreach($rx_matches as $ak_key => $ak_val) $rx_matches[$ak_key] = ucfirst($ak_val);
+	        $arh_key = implode('-', $rx_matches);
+	      }
+	      $arh[$arh_key] = $val;
+	    }
+	  }
+	  return( $arh );
+	}
+}
+
+
+//Effectue un basename en tenant compte des caractères utf8( non pris en compte par basename PHP sous linux)
+function mt_basename($path){
+	$path = str_replace(array("\\","/"),SLASH, $path);
+	$nameSplit = explode(SLASH,$path);
+	return end($nameSplit);
+}
+
+
+?>
+

+ 84 - 64
plugin/hackpoint/hackpoint.plugin.php

@@ -84,7 +84,7 @@ function hackpoint_content_setting(){
 //Types possibles : text,select ( + "values"=> array('1'=>'Val 1'),password,checkbox. Un simple string définit une catégorie.
 Configuration::setting('hackpoint',array(
         "Général",
-        'hackpoint_enable' => array("label"=>"Activer","type"=>"checkbox"),
+        //'hackpoint_enable' => array("label"=>"Activer","type"=>"checkbox"),
 ));
 
 
@@ -96,6 +96,7 @@ Configuration::setting('hackpoint',array(
 function hackpoint_dav($requested){
 	global $conf,$myUser;
 	$requested = trim($requested, '/');
+	
 
 	if(substr($requested, 0,13)!='dav/hackpoint') return;
 	if(empty($requested)) throw new Exception("Unspecified DAV path");
@@ -110,6 +111,7 @@ function hackpoint_dav($requested){
 	$server->logs = WebDav::logPath().SLASH.'dav-logs.txt';
 	$server->lockfile = WebDav::logPath().SLASH.'dav-lock.json';
 	$server->root = str_replace('//','/',$projectPath.'/dav/hackpoint/');
+
 	$server->folder = '';
 
 	//Windows cherche desktop.ini a chaque ouverture de dossier pour le custom
@@ -162,8 +164,7 @@ function hackpoint_dav($requested){
 	$server->do['delete'] = function($isoPath){
 		global $myUser,$conf;
 		$utf8Path = utf8_encode($isoPath);
-		
-		
+		throw new Exception("Méthode non implémentée",501);
 	};
 
 	$server->do['download'] = function($isoPath){
@@ -173,32 +174,55 @@ function hackpoint_dav($requested){
 		//pour verfiier si le fichier existe, on récupere son chemin système avec le bon encodage
 		$osPath = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? $isoPath: $utf8Path; 
 
-		if(!file_exists($osPath))  throw new Exception("Fichier inexistant",404);
-	    if(!is_file($osPath)) throw new Exception("Méthode non autorisée sur autre chose qu'un fichier",501);
 
+		require_once(__DIR__.SLASH.'Resource.class.php');
+		require_once(__DIR__.SLASH.'ResourceType.class.php');
+
+		preg_match_all('/#([0-9]*)/i', $utf8Path,$matches,PREG_SET_ORDER);
+		$id = end($matches);
+		$id = $id[1];
+
+		$resource = Resource::getById($id);
 		$stream = 'not implemented';
 
+		if(!$resource)  throw new Exception("Fichier inexistant",404);
+
+		$type = ResourceType::types($resource->type);
+		$stream = $type['class']::toFile($resource);
+		$stream = $stream['content'];
+
+	
 		return $stream;
 	};
 
 	$server->do['edit'] = function($isoPath,$body,$type){
 		global $myUser,$conf;
-		User::check_access('document','edit');
+		User::check_access('hackpoint','edit');
 		$utf8Path = utf8_encode($isoPath);
 
-		if($type=='file'){
-			$maxSize = $conf->get('document_allowed_size');
-			$extension = getExt($utf8Path);
-			$extensions = explode(',',str_replace(' ', '', $conf->get('document_allowed_extensions')));
-			if(strlen($body) > $maxSize) throw new Exception("Taille du fichier  trop grande, taille maximum :".readable_size($maxSize).' ('.$maxSize.' octets)',406);
-			if(!in_array($extension , $extensions)) throw new Exception("Extension '".$extension."' du fichier ".$path." non permise, autorisé :".implode(', ',$extensions),415);
-			$element = Element::addFile($utf8Path, $body);
-			if($conf->get('document_enable_logs') || $conf->get('document_enable_logs_verbose')) Log::put('Enregistrement fichier '.$utf8Path,'document');
-		}else{
-			
-			if($conf->get('document_enable_logs') || $conf->get('document_enable_logs_verbose')) Log::put('Enregistrement dossier '.$utf8Path,'document');
-		}
 
+		require_once(__DIR__.SLASH.'Resource.class.php');
+		require_once(__DIR__.SLASH.'ResourceType.class.php');
+		preg_match_all('/#([0-9]*)/i', $utf8Path,$matches,PREG_SET_ORDER);
+		$id = end($matches);
+		$id = $id[1];
+
+		$resource = Resource::getById($id);
+
+		if(!$resource)  throw new Exception("Fichier inexistant",404);
+
+		if($type!='file')throw new Exception("Non implémenté",501);
+
+		/*$maxSize = $conf->get('document_allowed_size');*/
+		$extension = getExt($utf8Path);
+		/*$extensions = explode(',',str_replace(' ', '', $conf->get('document_allowed_extensions')));
+		if(strlen($body) > $maxSize) throw new Exception("Taille du fichier  trop grande, taille maximum :".readable_size($maxSize).' ('.$maxSize.' octets)',406);
+		if(!in_array($extension , $extensions)) throw new Exception("Extension '".$extension."' du fichier ".$path." non permise, autorisé :".implode(', ',$extensions),415);
+	
+		if($conf->get('document_enable_logs') || $conf->get('document_enable_logs_verbose')) Log::put('Enregistrement fichier '.$utf8Path,'hackpoint');
+	*/
+		$resource->content = $body;
+		$resource->save();
 		
 	};
 
@@ -206,73 +230,69 @@ function hackpoint_dav($requested){
 		global $myUser,$conf;
 		$utf8Path = utf8_encode($isoPath);
 		$utf8To = utf8_encode($isoTo);
-		User::check_access('document','edit');
-		if(!document_check_element_name(mt_basename($utf8To), ENT_QUOTES)) throw new Exception("Caractères interdits : \\/:*?\"<>|");
-		
+		User::check_access('hackpoint','edit');
+		throw new Exception("Méthode non implémentée",501);
 	};
 
 	$server->do['copy'] = function($isoPath,$isoTo){
 		global $myUser,$conf;
 		$utf8Path = utf8_encode($isoPath);
 		$utf8To = utf8_encode($isoTo);
-		User::check_access('document','edit');
-		if(!document_check_element_name(mt_basename($utf8To), ENT_QUOTES)) throw new Exception("Caractères interdits : \\/:*?\"<>|");
+		User::check_access('hackpoint','edit');
+		throw new Exception("Méthode non implémentée",501);
 		
 	};
 
 	$server->do['list'] = function($isoPath,$depth =0){
+
 		global $myUser,$conf;
 		$utf8Path = utf8_encode($isoPath);
-		User::check_access('document','read');
+		require_once(__DIR__.SLASH.'Sketch.class.php');
+		require_once(__DIR__.SLASH.'Resource.class.php');
+		User::check_access('hackpoint','read');
 
-		//pour verfier si le fichier existe, on récupere son chemin système avec le bon encodage
-		$osPath = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? $isoPath: $utf8Path; 
-		/*if(!file_exists($osPath)) throw new Exception("Not found", 404);
 
-		$files = array();
 
-		//Si la cible est la racine dav, on la retourne sous forme d'element fictif, sinon on utilise le systeme d'element standard
-		if($utf8Path == __DIR__.SLASH.'hackpoint'.SLASH.'dav'.SLASH.'hackpoint'){
-			$file = (object) array();
-			$file->label = '';
-			$file->type = 'directory';
-			$file->path = '';
-			$toScan = array($file);
-		}else{
-			
-			$file = (object) array();
-			$file->label = '';
-			$file->type = 'directory';
-			$file->path = '';
-			$toScan = array($file);
-		}
-		*/
 		
+		$files  = array();
 		
-		//Si le dav demande un scan en profondeur du dossier, on scan les enfants du dossier ciblé
-		if($depth>0)
-			$toScan = array_merge($toScan,Element::browse($utf8Path.SLASH.'*'));
 		
-		foreach($toScan as $element){
-			
-		    $file = array(
-				'type' => $element->type,
-				'label' =>  $element->label
-		    );
-
-		    if($element->type == 'directory'){
-		    	
-				$file['create_time']  = time();
-				$file['update_time']  =  time();
-				$file['length'] =  1000;
+		$file = array();
+		$file['label'] = '';
+		$file['type'] = 'directory';
+		$file['path'] = '';
+		$files[] = $file;
+		
+		if($depth>0){
+			if(!empty($utf8Path)){
+
+				preg_match('/#([0-9]*)/i', $utf8Path,$matches);
+
+				$sketch = Sketch::load(array('id'=>$matches[1]));
+				foreach(Resource::loadAll(array('sketch'=>$sketch->id)) as $resource){
+					$type = $resource->type();
+					$file = array();
+					$file['label'] = $resource->label.' #'.$resource->id.'.'.$type['toExtension'];
+					$file['type'] = 'file';
+					$file['create_time']  = $resource->created;
+					$file['update_time']  =  $resource->updated;
+					$file['length'] =  1000;
+					$files[] = $file;
+				}
 			}else{
-				$file['create_time']  = time();
-				$file['update_time']  =  time();
-				$file['length'] =  1000;
+				foreach(Sketch::loadAll() as $sketch){
+					$file = array();
+					$file['label'] = $sketch->label.' #'.$sketch->id;
+					$file['type'] = 'directory';
+					$file['create_time']  = $sketch->created;
+					$file['update_time']  =  $sketch->updated;
+					$file['length'] =  1000;
+					$files[] = $file;
+				}
 			}
-
-			$files[] = $file;
 		}
+		
+
 
 		return $files;
 	};

File diff suppressed because it is too large
+ 1943 - 34
plugin/hackpoint/logs/dav-logs.txt


Some files were not shown because too many files changed in this diff