Browse Source

ajout resource type set de fichiers

Idleman 8 years ago
parent
commit
ea340249bb

+ 154 - 172
action.php

@@ -44,7 +44,7 @@ switch ($_['action']){
 		header('location: account.php');
 	break;
 
-
+	
 
 	// SKETCH
 	case 'create_sketch':
@@ -73,34 +73,16 @@ switch ($_['action']){
 		$response = array();
 		try{
 		
-		$sketch = Sketch::getById($_['id']);
-		if(!$sketch->public)throw new Exception('Ce sketch est privé');
-		
-		
-		
-		$response['sketch'] = $sketch->toArray();
-		$response['resources'] = array();
-		
-		foreach(Resource::loadAll(array('sketch'=>$_['id'])) as $resource):
+			$sketch = Sketch::getById($_['id']);
 			
-			$resource = $resource->toArray();
-			if($resource['type']=='image'){
-				$resource['content'] = base64_encode(file_get_contents(SKETCH_PATH.$resource['content']));
-			}else if($resource['type']=='part'){
-				$resource['content'] = array();
-				foreach(ResourcePart::loadAll(array('resource'=>$resource['id'])) as $resourcePart):
-					$part = $resourcePart->part_object;
-					$part = $part->toArray();
-					$resourcePart = $resourcePart->toArray();
-					if($part['image']!='') $part['image'] = base64_encode(file_get_contents(PART_PATH.$part['image']));
-					$resource['content'][] = array('resourcePart'=>$resourcePart,'part'=>$part);
-				
-				endforeach;
-			}else{
-				$resource['content'] = htmlspecialchars(SKETCH_PATH.$resource['content']);
-			}
-			$response['resources'][] = $resource;
-		endforeach;
+			if(!$sketch->public && $myUser->id!=$sketch->owner)throw new Exception('Ce sketch est privé');
+			
+			$response['sketch'] = $sketch->toArray();
+			$response['resources'] = array();
+			
+			foreach(Resource::loadAll(array('sketch'=>$_['id'])) as $resource)
+				$response['resources'][] = Type::toExport($resource);
+
 		
 		}catch(Exception $e){
 			$response['error'] = $e->getMessage();
@@ -119,8 +101,7 @@ switch ($_['action']){
 			header('Pragma: no-cache');
 			header("Content-Disposition: attachment; filename=\"".$filename."\"");
 		}
-		
-		
+
 		echo $response;
 	break;
 	
@@ -163,58 +144,9 @@ switch ($_['action']){
 			
 			
 			
-			foreach($json['resources'] as $res):
-				$resource = new Resource();
-				$resource->fromArray($res);
-				$resource->id = null;
-				$resource->sketch = $sketch->id;
-				$stream = '';
-				
-				if($resource->type=='image'){
-					$stream = base64_decode($resource->content);
-					$resource->content = '';
-				}
-				
-				if($resource->type=='part'){
-					$parts = $resource->content;
-					$resource->content = '';
-				}
-				
-				if(is_string($resource->content)){
-					$resource->content = htmlspecialchars_decode($resource->content);
-				}
-				
-				$resource->save();
-				if($resource->type=='image'){
-					$resource->content = $resource->id.'.png';
-					file_put_contents(SKETCH_PATH.$resource->content,$stream);
-					$resource->save();
-				}else if($resource->type=='part'){
-					
-					foreach($parts as $p):
-						
-						$part = new Part();
-						$part->fromArray($p['part']);
-						$part->id = null;
-						$stream = base64_decode($part->image);
-						$part->owner = $myUser->id;
-						$part->save();
-						$name = $part->id.'.png';
-						file_put_contents(PART_PATH.$name,$stream);
-						$part->image = $name;
-						$part->save();
-						
-						$resourcePart = new ResourcePart();
-						$resourcePart->fromArray($p['resourcePart']);
-						$resourcePart->part = $part->id;
-						$resourcePart->resource = $resource->id;
-						$resourcePart->save();
-						
-					endforeach;
-					$resource->content = '';
-					$resource->save();
-				}
-			endforeach;
+			foreach($json['resources'] as $res)
+				Type::fromImport($res,$sketch);
+
 		});
 	break;
 	case 'search_sketch':
@@ -305,9 +237,32 @@ switch ($_['action']){
 			move_uploaded_file($_FILES['file']['tmp_name'], $path);
 			$resource->content = $name;
 			$resource->save();
+			$response = array_merge(Type::get($resource->type));
+			$response['url'] = $path.'?v='.time();
+		});
+	break;
+	
+	case 'upload_resource_file':
+		Action::write(function($_,&$response){
+			global $myUser;
+			$resource = Resource::getByid($_['id']);
+			$sketch = Sketch::getById($resource->sketch);
+			$ext = getExt($_FILES['file']['name']);
+			if($myUser->id != $sketch->owner) throw new Exception("Seul le propriétaire du sketch peux faire ça");
+			if(!empty(ALLOWED_RESOURCE_FILE) && !in_array($ext,explode(',',ALLOWED_RESOURCE_FILE))) throw new Exception('Extensions autorisées '.ALLOWED_RESOURCE_FILE);
+			if($_FILES['file']['size']>ALLOWED_RESOURCE_SIZE) throw new Exception('Taille maximum autorisée '.ALLOWED_RESOURCE_SIZE.' o');
+			$name = $resource->id;
+			$folder = SKETCH_PATH.$name;	
+			if(!file_exists($folder)) mkdir($folder);
+			
+			$path = $folder.'/'.$_FILES['file']['name'];
+			
+			move_uploaded_file($_FILES['file']['tmp_name'], $path);
+			$response = array_merge(Type::get($resource->type));
 			$response['url'] = $path.'?v='.time();
 		});
 	break;
+	
 	case 'search_resources':
 	    
 		
@@ -336,38 +291,7 @@ switch ($_['action']){
 		foreach($types as $uid=>$tp)
 			if(isset($tp['extension']) && $ext == $tp['extension']) $type = $uid;
 		
-		
-		$resource = new Resource();
-		$resource->sketch = $sketch->id;
-		$stream = file_get_contents($_FILES['file']['tmp_name']);
-		$resource->label = $_FILES['file']['name'];
-		$resource->type = $type;
-		switch($type){
-			case 'arduino':
-			case 'php':
-			case 'python':
-			case 'c':
-			case 'java':
-			case 'readme':
-				$resource->content = file_get_contents($_FILES['file']['tmp_name']);
-				$enc = mb_detect_encoding($resource->content,"UTF-8, ISO-8859-1, GBK");
-				if($enc!='UTF-8')
-					$resource->content = iconv($enc,"utf-8",$resource->content); 
-				
-				
-				$resource->save();
-			break;
-			case 'image':
-				
-				$resource->save();
-				$name = $resource->id.'.'.$ext;
-				file_put_contents(SKETCH_PATH.$name,$stream);
-				$resource->content = $name;
-				$resource->save();
-			break;
-			default:
-			break;
-		}
+		Type::fromFileImport($_FILES['file'],$sketch,$type);
 		
 	break;
 	
@@ -377,12 +301,13 @@ switch ($_['action']){
 		global $myUser;
 		$ext = explode('.',$_FILES['file']['name']);
 		$ext = strtolower(array_pop($ext));
-		if(!in_array($ext,array('jpg','png','jpeg','gif'))) exit();
+		if(!in_array($ext,explode(',',ALLOWED_RESOURCE_IMAGE))) exit();
 		imageResize($_FILES['file']['tmp_name'],100,100);
 		echo 'data:image/png;base64,'.base64_encode(file_get_contents($_FILES['file']['tmp_name']));
-		
 	break;
 	
+	
+	
 	case 'search_component':
 		Action::write(function($_,&$response){
 			global $myUser;
@@ -474,80 +399,137 @@ switch ($_['action']){
 			$resource->save();
 		});
 	break;
+	
+	case 'get_resource_file':
+		global $myUser;
+		$resource = Resource::getById($_['id']);
+		$sketch =$resource->sketch_object;
+		if($myUser->id != $sketch->owner && !$sketch->public){
+			echo 'Désolé, vous n\'avez pas d\'accès à cette ressource...';
+			return;
+		}
+		$filepath = SKETCH_PATH.$resource->id.'/'.$_['file'];
+		$finfo = finfo_open(FILEINFO_MIME_TYPE); 
+		
+		$mime = finfo_file($finfo, $filepath);
+		
+		header('Content-Type: '.$mime);
+		header("Content-Length: " . filesize($filepath));
+		header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
+		header('Cache-Control: no-store, no-cache, must-revalidate');
+		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
+		header('Cache-Control: post-check=0, pre-check=0', FALSE);
+		header('Pragma: no-cache');
+		header("Content-Disposition: attachment; filename=\"".basename($filepath)."\"");
+		
+		echo file_get_contents($filepath);
+		finfo_close($finfo);
+	break;
+	
+	case 'get_resource_image':
+		global $myUser;
+		
+		$resource = Resource::getById($_['id']);
+		$sketch =$resource->sketch_object;
+		if($myUser->id != $sketch->owner && !$sketch->public){
+			readfile('img/default_image.png');
+			return;
+		}
+		$finfo = finfo_open(FILEINFO_MIME_TYPE);
+		$filepath = SKETCH_PATH.$resource->content;
+		$mime = finfo_file($finfo, $filepath);
+		header('Content-Type: '.$mime);
+		readfile($filepath);
+	break;
+	
 	case 'edit_resource':
 		Action::write(function($_,&$response){
 		
 			$resource = Resource::getById($_['id']);
 			global $myUser;
 			$sketch = Sketch::getById($resource->sketch);
-			 
 			$resource->label = html_entity_decode($resource->label);
-			$response = $resource->toArray();
-			$type = Type::get($resource->type);
+			$response = Type::toHtml($resource,$sketch);
+		});
+	break;
+	case 'delete_resource':
+		Action::write(function($_,&$response){
+			global $myUser;
+			$resource = Resource::getById($_['id']);
+			$sketch = Sketch::getById($resource->sketch);
+			if($myUser->id != $sketch->owner) throw new Exception("Permission refusée, seul l'auteur du sketch peux faire ça");
+			Resource::getById($_['id']);
+			$resource->remove();
+		});
+	break;
+	
+	/*FILES*/
+	case 'search_file':
+	
+	Action::write(function($_,&$response){
+			global $myUser;
+			if(!isset($_['id']) || !is_numeric($_['id'])) throw new Exception("Ressource non spécifiée");
+			
+			$resource = Resource::getById($_['id']);
+			$sketch = $resource->sketch_object;
+			
+			if($myUser->id != $sketch->owner && !$sketch->public) throw new Exception("Désolé, le sketch est privé");
 			
-			switch($resource->type){
+			foreach(glob(SKETCH_PATH.'/'.$_['id'].'/*') as $file):
+				$icon = getExtIcon(getExt($file));
 				
-				case 'image':
-					
-					if($myUser->id == $sketch->owner){
-						$image = $response['content']==''?'img/default_image.png':SKETCH_PATH.$response['content'].'?v='.time();
-						$response['content'] = '<img style="width:100%;height:auto;" class="dropzone" src="'.$image.'" />';
-						$response['upload'] = $type['upload'];
-					}
-				break;
-				case 'part':
-					$response['callback'] = 'init_part();';
-					$response['content'] = '<table class="table table-stripped table-bordered" id="parts"><thead>
-						<tr>
-							<th>Libellé</th>
-							<!--<th>Lien</th>
-							<th>Prix</th>-->';
-					if($myUser->id == $sketch->owner)
-							$response['content'] .= '<th></th>';
-							
-					$response['content'] .= '</tr>';
-						
-					if($myUser->id == $sketch->owner){
-						$response['content'] .= '<tr id="partForm" data-action="save_part" data-id="">
-							<td><input type="text" id="label" class="form-control"></td>
-							<!--<td><input type="url" id="link"  class="form-control"></td>
-							<td><input type="text" id="price"  class="form-control input-mini"></td>-->
-							<td><div class="btn btn-success" onclick="save_part();"><i class="fa fa-plus"></i></div></td>
-						</tr>';
-					}
-						
-					$response['content'] .= '</thead><tbody>';
-					
-					$response['content'] .= '<tr style="display:none" data-id="{{id}}">
-							<td ><a href="{{link}}"><div class="componentImage"><img src="{{image}}"/></div> {{label}}</a> {{#price}}<code>{{price}} €</code>{{/price}}{{#brand}} <small>{{brand}}</small>{{/brand}}</td>';
-					
-					if($myUser->id == $sketch->owner)
-						$response['content'] .= '<td><div class="btn btn-danger" onclick="delete_part(this);"><i class="fa fa-times" ></i></div></td>';
-					
-					$response['content'] .= '</tr>';
-					$response['content'] .='</tbody></table>';
-				break;
-			}
+				$response['rows'][] = array('id'=>basename($file),'icon'=>$icon,'label'=>basename($file),'resource'=>$resource->id);
+			endforeach;
+		});
+	break;
+	
+	case 'download_file':
 			
+			global $myUser;
+			$path = SKETCH_PATH.'/'.$_['resource'];
 			
 			
-			//for sources
-			if(isset($type['codemirror'])){
-				$response['content'] = '<textarea>'.$response['content'].'</textarea>';
-				$response['code'] = $type['codemirror'];
-				if($myUser->id != $sketch->owner) $response['code']['readOnly'] = true;
+			$resource = Resource::getById($_['resource']);
+			$sketch = $resource->sketch_object;
+			if($myUser->id != $sketch->owner && !$sketch->public) throw new Exception("Permission refusée, le sketch est privé");
+			
+		
+			
+			$filename = $resource->label.'-'.time().'.zip';
+			$filepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename;
+			$zip = new ZipArchive;
+			if(file_exists($filepath))unlink($filepath); 
+			$res = $zip->open($filepath, ZipArchive::CREATE);
+			if ($res === TRUE) {
+				foreach(glob($path.'/*') as $file)
+					$zip->addFile($file,basename($file));
+				
+				
+				$zip->close();
 			}
+		
+			header("Content-Type: application/zip");
+			header("Content-Length: " . filesize($filepath));
+			header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
+			header('Cache-Control: no-store, no-cache, must-revalidate');
+			header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
+			header('Cache-Control: post-check=0, pre-check=0', FALSE);
+			header('Pragma: no-cache');
+			header("Content-Disposition: attachment; filename=\"".$filename."\"");
+			readfile($filepath);
+			unlink($filepath); 
 
-		});
+		
 	break;
-	case 'delete_resource':
+	
+	case 'delete_file':
 		Action::write(function($_,&$response){
 			global $myUser;
-			$resource = Resource::getById($_['id']);
-			$sketch = Sketch::getById($resource->sketch);
+			$path = SKETCH_PATH.'/'.$_['resource'].'/'.$_['id'];
+			$resource = Resource::getById($_['resource']);
+			$sketch = $resource->sketch_object;
 			if($myUser->id != $sketch->owner) throw new Exception("Permission refusée, seul l'auteur du sketch peux faire ça");
-			Resource::getById($_['id']);
-			$resource->remove();
+			if(file_exists($path)) unlink($path);
 		});
 	break;
 	

+ 284 - 3
class/Type.class.php

@@ -85,7 +85,21 @@ class Type{
 
 		$types['image'] = array(
 			'label' => 'Image',
-			'upload' => array()
+			'upload' => array(
+				'url'     => 'action.php?action=upload_resource',
+				'element' => '#resource p img:eq(0)',
+				'callback' => '$(\'#resource img:eq(0)\').attr(\'src\',r.url);'
+			)
+		);
+		
+		$types['files'] = array(
+			'label' => 'Set de fichiers',
+			'upload' => array(
+				'url'=>'action.php?action=upload_resource_file',
+				'element' => '#dropZoneFiles',
+				'callback' => "search_file();",
+			),
+			'extension' => 'zip'
 		);
 
 		$types['part'] = array(
@@ -97,6 +111,251 @@ class Type{
 		return $types;
 	}
 	
+	public static function fromFileImport($file,$sketch,$type){
+		$resource = new Resource();
+		$resource->sketch = $sketch->id;
+		$stream = file_get_contents($file['tmp_name']);
+		$resource->label = $file['name'];
+		$resource->type = $type;
+		switch($resource->type){
+			case 'arduino':
+			case 'php':
+			case 'python':
+			case 'c':
+			case 'java':
+			case 'readme':
+				$resource->content = file_get_contents($file['tmp_name']);
+				$enc = mb_detect_encoding($resource->content,"UTF-8, ISO-8859-1, GBK");
+				if($enc!='UTF-8')
+					$resource->content = iconv($enc,"utf-8",$resource->content); 
+				
+				
+				$resource->save();
+			break;
+			case 'files':
+				$resource->save();
+				$path = SKETCH_PATH.'/'.$resource->id;
+				if(!file_exists($path)) mkdir($path);
+				$zip = new ZipArchive;
+				$res = $zip->open($file['tmp_name']);
+				if ($res === TRUE) {
+				  $zip->extractTo($path);
+				  $zip->close();
+				}
+			break;
+			case 'image':
+				
+				$resource->save();
+				$name = $resource->id.'.'.$ext;
+				file_put_contents(SKETCH_PATH.$name,$stream);
+				$resource->content = $name;
+				$resource->save();
+			break;
+			default:
+			break;
+		}
+	}
+	
+	public static function fromImport($res,$sketch){
+		global $myUser;
+		$resource = new Resource();
+		$resource->fromArray($res);
+		$resource->id = null;
+		$resource->sketch = $sketch->id;
+		$stream = '';
+		
+		switch($resource->type){
+			case 'image':
+				$stream = base64_decode($resource->content);
+				$resource->content = '';
+			break;
+			case 'part':
+				$parts = $resource->content;
+				$resource->content = '';
+			break;
+			case 'files':
+				$files = $resource->content;
+				$resource->content = '';
+			break;
+			default:
+			break;
+		}
+
+
+		if(is_string($resource->content))
+			$resource->content = htmlspecialchars_decode($resource->content);
+	
+		$resource->save();
+		
+		switch($resource->type){
+			case 'image':
+				$resource->content = $resource->id.'.png';
+				file_put_contents(SKETCH_PATH.$resource->content,$stream);
+				$resource->save();
+			break;
+			case 'part':
+				foreach($parts as $p):
+				
+					$part = new Part();
+					$part->fromArray($p['part']);
+					$part->id = null;
+					$stream = base64_decode($part->image);
+					$part->owner = $myUser->id;
+					$part->save();
+					$name = $part->id.'.png';
+					file_put_contents(PART_PATH.$name,$stream);
+					$part->image = $name;
+					$part->save();
+					
+							
+					$resourcePart = new ResourcePart();
+					$resourcePart->fromArray($p['resourcePart']);
+					$resourcePart->id = null;
+					$resourcePart->part = $part->id;
+					$resourcePart->resource = $resource->id;
+					$resourcePart->save();
+					
+				endforeach;
+				$resource->content = '';
+				$resource->save();
+			break;
+			case 'files':
+				$folder = SKETCH_PATH.'/'.$resource->id;
+				if(!file_exists($folder)) mkdir($folder);
+				foreach($files as $file):
+					
+					$stream = base64_decode($file['stream']);
+					file_put_contents($folder.'/'.$file['label'],$stream);
+
+				endforeach;
+				$resource->content = '';
+				$resource->save();
+			break;
+			default:
+			break;
+		}
+	}
+	
+	
+	public static function toExport($resource){
+		$resource = $resource->toArray();
+		
+		switch($resource['type']){
+			case 'image':
+				$resource['content'] = base64_encode(file_get_contents(SKETCH_PATH.$resource['content']));
+			break;
+			case 'part':
+				$resource['content'] = array();
+				foreach(ResourcePart::loadAll(array('resource'=>$resource['id'])) as $resourcePart):
+					$part = $resourcePart->part_object;
+					$part = $part->toArray();
+					$resourcePart = $resourcePart->toArray();
+					if($part['image']!='') $part['image'] = base64_encode(file_get_contents(PART_PATH.$part['image']));
+					$resource['content'][] = array('resourcePart'=>$resourcePart,'part'=>$part);
+				endforeach;
+			break;
+			case 'files':
+				$resource['content'] = array();
+				$folder = SKETCH_PATH.'/'.$resource['id'];
+				foreach(glob($folder.'/*') as $file):
+					$resource['content'][] = array('label'=>basename($file),'stream'=>base64_encode(file_get_contents($file)));
+				endforeach;
+			break;
+			default:
+				$resource['content'] = htmlspecialchars(SKETCH_PATH.$resource['content']);
+			break;
+		}
+	
+		return $resource;
+	}
+	
+	public static function toHtml($resource,$sketch){
+		global $myUser;
+		$response = array();
+		$response = $resource->toArray();
+		$type = self::get($resource->type);
+		switch($resource->type){
+				
+				case 'image':
+					
+					
+						$image = $response['content']==''?'img/default_image.png':'action.php?action=get_resource_image&id='.$response['id'];
+						$response['content'] = '<img style="width:100%;height:auto;" class="dropzone" src="'.$image.'" />';
+					if($myUser->id == $sketch->owner){
+						$response['upload'] = $type['upload'];
+					}
+				break;
+				
+				case 'files':
+
+					if($myUser->id == $sketch->owner){
+						$response['upload'] = $type['upload'];
+						$response['content'] = '<div id="dropZoneFiles"><i class="fa fa-file-text-o"></i> Faites glisser vos fichiers ici</div>';
+					}
+					$response['callback'] = 'init_file();';
+					$response['content'] .= '<table class="table table-stripped table-bordered" id="files"><thead>
+						<tr>
+							<th>Fichiers disponibles <a class="btn btn-primary" href="action.php?action=download_file&resource='.$resource->id.'" style="float:right;" title="Télécharger les fichiers"><i class="fa fa-file-zip-o"></i> Télécharger</a></th>';
+					
+					$response['content'] .= '<th style="width:50px;"></th>';
+							
+					$response['content'] .= '</tr></thead><tbody>';
+					
+					$response['content'] .= '<tr style="display:none" data-id="{{id}}">
+							<td ><a href="action.php?action=get_resource_file&id={{resource}}&file={{label}}"><i class="fa {{icon}}"></i> {{label}}</a></td>';
+					
+					
+						$response['content'] .= '<td>';
+						if($myUser->id == $sketch->owner)
+						$response['content'] .= '<div class="btn btn-danger" onclick="delete_file(this);"><i class="fa fa-times" ></i></div>';
+						$response['content'] .= '</td>';
+					
+					$response['content'] .= '</tr>';
+					$response['content'] .='</tbody></table>';
+				break;
+				case 'part':
+					$response['callback'] = 'init_part();';
+					$response['content'] = '<table class="table table-stripped table-bordered" id="parts"><thead>
+						<tr>
+							<th>Libellé</th>
+							<!--<th>Lien</th>
+							<th>Prix</th>-->';
+					if($myUser->id == $sketch->owner)
+							$response['content'] .= '<th></th>';
+							
+					$response['content'] .= '</tr>';
+						
+					if($myUser->id == $sketch->owner){
+						$response['content'] .= '<tr id="partForm" data-action="save_part" data-id="">
+							<td><input type="text" id="label" class="form-control"></td>
+							<!--<td><input type="url" id="link"  class="form-control"></td>
+							<td><input type="text" id="price"  class="form-control input-mini"></td>-->
+							<td><div class="btn btn-success" onclick="save_part();"><i class="fa fa-plus"></i></div></td>
+						</tr>';
+					}
+						
+					$response['content'] .= '</thead><tbody>';
+					
+					$response['content'] .= '<tr style="display:none" data-id="{{id}}">
+							<td ><a href="{{link}}"><div class="componentImage"><img src="{{image}}"/></div> {{label}}</a> {{#price}}<code>{{price}} €</code>{{/price}}{{#brand}} <small>{{brand}}</small>{{/brand}}</td>';
+					
+					if($myUser->id == $sketch->owner)
+						$response['content'] .= '<td><div class="btn btn-danger" onclick="delete_part(this);"><i class="fa fa-times" ></i></div></td>';
+					
+					$response['content'] .= '</tr>';
+					$response['content'] .='</tbody></table>';
+				break;
+			}
+			//for sources
+			if(isset($type['codemirror'])){
+				$response['content'] = '<textarea>'.$response['content'].'</textarea>';
+				$response['code'] = $type['codemirror'];
+				if($myUser->id != $sketch->owner) $response['code']['readOnly'] = true;
+			}
+			
+			return $response;
+	}
+	
 	public static function toFileStream($resource){
 		$type = self::get($resource->type);
 		$file = (object) array('name'=>slugify($resource->label),'content'=>'');
@@ -109,17 +368,39 @@ class Type{
 					$part = $resourcePart->part_object;
 					$file->content .= $part->label."\t";
 					if(isset($part->link) && !empty($part->link)) $file->content .= $part->link."\t";
-					if(isset($part->price) && !empty($part->price)) $file->content .= $part->price.'€'.PHP_EOL;
+					if(isset($part->price) && !empty($part->price)) $file->content .= $part->price.'€';
+					$file->content .= PHP_EOL;
 				endforeach;
 			break;
+			case 'files':
+
+				global $myUser;
+				$path = SKETCH_PATH.'/'.$resource->id;
+
+				$filename = $resource->label.'-'.time().'.zip';
+				$filepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename;
+				$zip = new ZipArchive;
+				if(file_exists($filepath))unlink($filepath); 
+				$res = $zip->open($filepath, ZipArchive::CREATE);
+				if ($res === TRUE) {
+					foreach(glob($path.'/*') as $f)
+						$zip->addFile($f,basename($f));
+					
+					$zip->close();
+				}
+			
+				$file->name = slugify($resource->label).'.zip';
+				$file->content = file_get_contents($filepath);
+
+			break;
 			case 'image':
 				$ext = getExt($resource->content);
 				$file->name = slugify($resource->label).'.'.$ext;
+				if(file_exists(SKETCH_PATH.$resource->content))
 				$file->content = file_get_contents(SKETCH_PATH.$resource->content);
 			break;
 			default:
 				$file->content = html_entity_decode($resource->content);
-			
 			break;
 		}
 		return $file;

+ 3 - 0
constant.php

@@ -11,6 +11,9 @@ define('UPLOAD_PATH','upload');
 define('SKETCH_PATH',UPLOAD_PATH.'/sketch/');
 define('PART_PATH',UPLOAD_PATH.'/part/');
 define('ALLOWED_RESOURCE_IMAGE','jpg,png,jpeg,gif,bmp');
+//Laisser vide si pas de vérification sur les resources de fichiers, sinon placez les extension
+//autorisées sous la forme : jpg,png,jpeg,gif,bmp,txt,doc,docx,cmd,bat,exe,cpp,c,h,hpp,ino,php,js,css,ttf,woff,svg,pdf,xls,xlsx,sql,ico,doc,docx
+define('ALLOWED_RESOURCE_FILE','');
 define('ALLOWED_RESOURCE_SIZE',1000000);
 
 define('PROGRAM_NAME','Hackpoint');

+ 24 - 0
css/main.css

@@ -120,6 +120,16 @@ div.CodeMirror{
 	margin:0;
 }
 
+#dropZoneFiles{
+	background: #f8f8f8 none repeat scroll 0 0;
+    border-bottom: 5px solid #f2f2f2;
+    color: #818181;
+    cursor: pointer;
+    font-size: 20px;
+    padding: 15px;
+	text-transform:uppercase;
+    text-align: center;
+}
 
 
 #resource{
@@ -141,6 +151,20 @@ div.CodeMirror{
 	height:100%;
 }
 
+#files tbody a{
+	color:#222222;
+	font-size:20px;
+	line-height:32px;
+	transition: color 0.2s linear;
+}
+#files tbody a:hover{
+	color:#4FC1E9;
+}
+#files tbody a i{
+	color:#cecece;
+	font-size:25px;
+	font-weight:normal;
+}
 
 #parts tbody td{
 	vertical-align:middle;

BIN
database/.db


+ 95 - 0
function.php

@@ -55,6 +55,101 @@ function getExt($file){
 	return strtolower(array_pop($ext));
 }
 
+function getExtIcon($ext){
+	$icon = '';
+	switch($ext){
+		case '7z':
+		case 'rar':
+		case 'gz':
+		case 'zip':
+			$icon = 'fa-file-archive-o';
+		break;
+		
+		case 'php':
+		case 'js':
+		case 'py':
+		case 'c':
+		case 'cpp':
+		case 'css':
+		case 'h':
+		case 'hpp':
+		case 'html':
+		case 'htm':
+		case 'asp':
+		case 'jsp':
+			$icon = 'fa-file-code-o';
+		break;
+		
+		case 'xls':
+		case 'xlsx':
+		case 'csv':
+			$icon = 'fa-file-excel-o';
+		break;
+		
+		case 'bmp':
+		case 'jpg':
+		case 'jpeg':
+		case 'ico':
+		case 'gif':
+		case 'png':
+		case 'svg':
+			$icon = 'fa-file-image-o';
+		break;
+		
+		case 'pdf':
+			$icon = 'fa-file-pdf-o';
+		break;
+		case 'ppt':
+		case 'pptx':
+			$icon = 'fa-file-powerpoint-o';
+		break;
+		
+		case 'txt':
+		case 'htaccess':
+		case 'md':
+			$icon = 'fa-file-text-o';
+		break;
+		
+		case 'doc':
+		case 'docx':
+		case 'word':
+			$icon = 'fa-file-word-o';
+		break;
+		
+		case 'avi':
+		case 'wmv':
+		case 'mov':
+		case 'divx':
+		case 'xvid':
+		case 'mkv':
+		case 'flv':
+		case 'mpeg':
+		case 'h264':
+		case 'rmvb':
+		case 'mp4':
+			$icon = 'fa-file-movie-o';
+		break;
+		
+		case 'wav':
+		case 'ogg':
+		case 'ogv':
+		case 'ogx':
+		case 'oga':
+		case 'riff':
+		case 'bwf':
+		case 'wma':
+		case 'flac':
+		case 'aac':
+		case 'mp3':
+			$icon = 'fa-file-audio-o';
+		break;
+		default:
+			$icon = 'fa-file-o';
+		break;
+	}
+	return $icon;
+};
+
 function imageResize($image,$w,$h){
 	$resource = imagecreatefromstring(file_get_contents($image));
 	$size = getimagesize($image);

+ 1 - 1
install.php

@@ -48,7 +48,7 @@ date_default_timezone_set(TIME_ZONE);
 set_error_handler('errorToException');
 spl_autoload_register('app_autoloader');
 	
-$_ = array_map('htmlentities',array_merge($_POST,$_GET));
+$_ = array_map('secure_user_vars',array_merge($_POST,$_GET));
 	
 if(isset($_SESSION['currentUser']))
 $myUser =unserialize($_SESSION['currentUser']);	

+ 56 - 8
js/main.js

@@ -1,6 +1,6 @@
 $(document).ready(function(){
 
-	if($.hashData('embeded') =="1"){
+	if($.urlParam('embeded') =="1"){
 		$('.navbar,.resources-options,#sketch').hide();
 		$('.container-fluid,.col-md-9,.col-md-3').css('padding','0');
 		$('.list-group-item,#resource').css('padding','3px').css('border-radius','0px');
@@ -10,6 +10,10 @@ $(document).ready(function(){
 		$('.col-md-3,.col-md-9').attr('style','width:20%;float:left;padding:0;');
 		$('.col-md-9').attr('style','width:70%;float:left;padding:0;');
 	}
+	if($.urlParam('sidebar') =="0"){
+		$('#resourceMenu').hide();
+		$('#resourceContent').removeClass('col-md-9').addClass('col-md-12');
+	}
 
 	var init = 'init_'+$.page();
 
@@ -30,11 +34,15 @@ function init_index(){
 	
 	$('#importJsonSketch').dropzone({
 			url : 'action.php?action=import_sketch',
-			complete : function(useless,server){
+			complete : function(useless){
+			
 				if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
-        			search_sketch();
+					search_sketch();
       			}
 			},
+			success: function(file, response){
+                if(response.errors.length>0) alert(response.errors.join(','));
+            },
 			sending : function(file, xhr, formData){
 				formData.append('from','file');
 			}
@@ -188,8 +196,23 @@ function toggle_share_sketch(element){
 
 function toggle_embed_sketch(){
 	$('#embedModal').modal('show');
-	$('#embedModal textarea').val('<a href="'+window.location+'"><small>Voir en taille réelle<small></a><br/><iframe frameborder="0" width="100%" align="center" height="400px" src="'+window.location+'&embeded=1"></iframe>');
+	get_embeded_code();
+	
+}
+
+function get_embeded_code(){
+	var url = window.location.protocol+'//'+window.location.host+window.location.pathname+window.location.search;
 
+	url+= '&embeded=1';
+	if(!$('#enableSideBar').prop('checked'))
+		url+= '&sidebar=0';
+	
+	url+= window.location.hash;
+	
+	var html = '<a href="'+window.location+'"><small>Voir en taille réelle<small></a><br/>';
+	html += '<iframe frameborder="0" width="100%" align="center" height="400px" src="'+url+'"></iframe>';
+	$('#embedModal textarea').val(html);
+	
 	$("#embedModal textarea").focus(function() {
 	    var $this = $(this);
 	    $this.select();
@@ -198,7 +221,6 @@ function toggle_embed_sketch(){
 	        return false;
 	    });
 	});
-
 }
 
 //RESOURCE
@@ -276,19 +298,20 @@ function load_resource(){
 
 		if(r.upload !=null){
 				var data = {};
-				data.url = 'action.php?action=upload_resource';
+				data.url = r.upload.url;
 				data.success = function(useless,r){
 					if(r.errors.length!=0){
 						alert('Erreur : '+r.errors.join(','));
 					}else{
-						$('#resource img:eq(0)').attr('src',r.url);
+						eval(r.upload.callback);
+						
 					}
 				}
 				data.sending = function(file, xhr, formData){
 					formData.append('id', $('#resource').attr('data-id'));
 				}
 				data.createImageThumbnails = false;
-				$('#resource p img:eq(0)').dropzone(data);
+				$(r.upload.element).dropzone(data);
 		}
 
 		if(r.code != null){
@@ -325,6 +348,27 @@ function load_resource(){
 	});
 }
 
+/*FILE*/
+
+function init_file(){
+	search_file();
+};
+
+function search_file(){
+	$('#files').fill({action:'search_file',id:$('#resource').attr('data-id')});
+
+}
+
+function delete_file(element){
+	if(!confirm('Êtes vous sûr de vouloir supprimer ça?')) return;
+	var line = $(element).closest('tr');
+	$.action({action : 'delete_file',resource : $('#resource').attr('data-id'),id : line.attr('data-id')},function(r){
+		line.remove();
+	});
+}
+
+/*PART*/
+
 function init_part(){
 	$('#label').autocomplete({
 		source : 'action.php?action=autocomplete_part',
@@ -343,6 +387,9 @@ function init_part(){
 	search_part();
 };
 
+
+
+
 function search_part(){
 	$('#parts').fill({action:'search_part',id:$('#resource').attr('data-id')});
 }
@@ -370,6 +417,7 @@ function edit_part(element){
 	});
 }
 */
+
 function delete_part(element){
 	if(!confirm('Êtes vous sûr de vouloir supprimer ça?')) return;
 	var line = $(element).closest('tr');

+ 6 - 3
sketch.php

@@ -10,7 +10,7 @@ if(!$sketch->public && $myUser->id != $sketch->owner) throw new Exception("Sketc
 <div class="row" id="sketch" data-id="<?php echo $sketch->id; ?>">
 	<div class="col-md-4">
 	
-		<h5 id="sketchTitle"><input onblur="save_sketch_title(this);" type="text" value="<?php echo htmlentities(html_entity_decode($sketch->label)); ?>"/></h5>
+		<h5 id="sketchTitle"><input onblur="save_sketch_title(this);" type="text" value="<?php echo htmlspecialchars(html_entity_decode($sketch->label)); ?>"/></h5>
 	</div>
 	<div class="col-md-8">
 		<ul class="sketchOptions">
@@ -24,7 +24,7 @@ if(!$sketch->public && $myUser->id != $sketch->owner) throw new Exception("Sketc
 </div>
 <div class="row">
 	
-	<div class="col-md-3">
+	<div class="col-md-3" id="resourceMenu">
 
 		<?php if($myUser->id == $sketch->owner) : ?>
 			   <div class="btn-group" style="margin:10px auto;">
@@ -53,7 +53,7 @@ if(!$sketch->public && $myUser->id != $sketch->owner) throw new Exception("Sketc
 					 
 		</div>
 	</div>
-	<div class="col-md-9">
+	<div class="col-md-9" id="resourceContent">
 		<div class="jumbotron">
 			<div class="jumbotron-contents" id="resource">
 				<h2 style="margin:0 0 5px 0;">Aucune ressource sélectionnée</h2>
@@ -109,6 +109,9 @@ if(!$sketch->public && $myUser->id != $sketch->owner) throw new Exception("Sketc
         <h4 class="modal-title">Copier coller le code suivant</h4>
       </div>
       <div class="modal-body">
+	  
+	  <label><input id="enableSideBar" checked="checked" onchange="get_embeded_code();" type="checkbox"> Afficher la barre de menu</label>
+	  <br/>
       <label for="code">Code</label>
       <textarea class="form-control"></textarea>
       </div>

BIN
upload/part/1.png


BIN
upload/part/10.png


BIN
upload/part/11.png


BIN
upload/part/12.png


BIN
upload/part/13.png


BIN
upload/part/14.png


BIN
upload/part/15.png


BIN
upload/part/16.png


BIN
upload/part/17.png


BIN
upload/part/18.png


BIN
upload/part/19.png


BIN
upload/part/2.png


BIN
upload/part/3.png


BIN
upload/part/33.png


BIN
upload/part/34.png


BIN
upload/part/35.png


BIN
upload/part/36.png


BIN
upload/part/37.png


BIN
upload/part/38.png


BIN
upload/part/39.png


BIN
upload/part/4.png


BIN
upload/part/40.png


BIN
upload/part/6.png


BIN
upload/part/8.png


BIN
upload/part/9.png


+ 1 - 0
upload/sketch/.htaccess

@@ -0,0 +1 @@
+deny for all

+ 0 - 69
upload/sketch/10.h

@@ -1,69 +0,0 @@
-#ifndef EEPROMAnything_H
-#define EEPROMAnything_H
-
-#include <EEPROM.h>
-#include <Arduino.h>  // for type definitions
-
-// Source: http://arduino.cc/playground/Code/EEPROMWriteAnything
-
-
-// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
-// []
-// []   V-Strom Mk1B - An extra display for a Suzuki DL-650 ("V-Strom") that adds the following functionality:
-// []     1. Battery Level display in Volts - e.g. 14.5V
-// []     2. Gear Position Indicator on LCD - e.g. 1, 2, 3, 4, 5, 6, N
-// []     3. Ambient Temperature in Farenheight or Celsius - e.g. 62.5F
-// []     4. [Future] LED display of gear position (one led for each gear 1-6, in different colors, N will be blinking on 1)
-// []     5. [Future] Accurate display of the fuel level (in percentage)
-// []     6. [Future] Show Fuel consumption - MPG or KM/L, TBD: need to tap into motorcycle's speed sensor (PWM)
-// []     7. [Future] Fix the OEM V-Strom Fuel Gauge to become linear
-// []     License: GPL V3
-/*
-    Stromputer - Enhanced display for Suzuki V-Strom Motorcycles (DL-650, DL-1000, years 2004-2011)
-    Copyright (C) 2011 Yuval Naveh
-
-    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 3 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, see <http://www.gnu.org/licenses/>.
-*/
-
-/*
- .::::::.:::::::::::::::::::..       ...     .        :::::::::::. ...    :::::::::::::::.,:::::: :::::::..   
-;;;`    `;;;;;;;;'''';;;;``;;;;   .;;;;;;;.  ;;,.    ;;;`;;;```.;;;;;     ;;;;;;;;;;;'''';;;;'''' ;;;;``;;;;  
-'[==/[[[[,    [[      [[[,/[[['  ,[[     \[[,[[[[, ,[[[[,`]]nnn]]'[['     [[[     [[      [[cccc   [[[,/[[['  
-  '''    $    $$      $$$$$$c    $$$,     $$$$$$$$$$$"$$$ $$$""   $$      $$$     $$      $$""""   $$$$$$c    
- 88b    dP    88,     888b "88bo,"888,_ _,88P888 Y88" 888o888o    88    .d888     88,     888oo,__ 888b "88bo,
-  "YMmMY"     MMM     MMMM   "W"   "YMMMMMP" MMM  M'  "MMMYMMMb    "YmmMMMM""     MMM     """"YUMMMMMMM   "W" 
-*/
-
-
-// Write a template value into EEPROM address [ee]
-template <class T> int EEPROM_writeAnything(int ee, const T& value)
-{
-    const byte* p = (const byte*)(const void*)&value;
-    unsigned int i;
-    for (i = 0; i < sizeof(value); i++)
-          EEPROM.write(ee++, *p++);
-    return i;
-}
-
-// Read a template value from EEPROM address [ee]
-template <class T> int EEPROM_readAnything(int ee, T& value)
-{
-    byte* p = (byte*)(void*)&value;
-    unsigned int i;
-    for (i = 0; i < sizeof(value); i++)
-          *p++ = EEPROM.read(ee++);
-    return i;
-}
-
-#endif

BIN
upload/sketch/13.jpg


BIN
upload/sketch/2.png