diff app/controllers/icon_controller.php @ 4:c3adb28473d6 default tip

modified icon feature
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Tue, 16 Aug 2011 18:31:36 +0900
parents 7201fd354bdf
children
line wrap: on
line diff
--- a/app/controllers/icon_controller.php	Sun Aug 14 23:52:31 2011 +0900
+++ b/app/controllers/icon_controller.php	Tue Aug 16 18:31:36 2011 +0900
@@ -8,19 +8,31 @@
 
 	public function index()
 	{
+		$this->autoRender = false;
 		$icon_dir = "../icon"; //relative path without slash
-		$this->autoRender = false;
 		$icon_default = "/default.png"; //file name must include slash
 		$icon_file = $icon_default;
 
-		if(isset($this->params["url"]["uuid"])){
-			$icon_file = "/".$this->params["url"]["uuid"]."png";
+		if(isset($_GET["uuid"])){
+			$uuid = $_GET["uuid"];
+			if($this->IsUUID($uuid)){	
+				$icon_file = "/".$uuid.".png";
+			}else{
+				$this->RenderError("Invalid UUID.");
+				return;
+			}
 		}
 
 		$icon = @file_get_contents($icon_dir.$icon_file);
 		if($icon === FALSE){
 			//failed to get icon
-			$icon = file_get_contents($icon_dir.$icon_default);
+			$icon = @file_get_contents($icon_dir.$icon_default);
+			if($icon === FALSE){
+				//print error code here.
+				$err = error_get_last();
+				$this->RenderErorr($err["message"]);
+				return;
+			}
 		}
 
 		header("Content-Type: image/png");
@@ -29,10 +41,99 @@
 
 	public function EditIcon()
 	{
+		if(!isset($this->params["url"]["uuid"])){
+			$this->set("content","Domain ID is required.");
+			$this->render("error");
+			return;
+		}
+
+		$uuid = $this->params["url"]["uuid"];
+		if(!$this->IsUUID($uuid)){
+			$this->RenderError("Invalid UUID.");	
+		}
+
+		$this->autoRender = true;
+
+		$this->set("max_size",ini_get("post_max_size"));
+		$this->set("uuid",$uuid);
+	}
+
+	public function DeleteIcon()
+	{
+		$this->autoRender = true;
+
+		if(!isset($_POST["uuid"])){
+			$this->RenderError("Domain ID is required.");
+			return;
+		}
+
+		$uuid = $_POST["uuid"];
+		if($this->IsUUID($uuid)){
+			$this->RenderError("Invalid UUID.");
+			return;
+		}
+
+		$icon_name = "../icon/".$_POST["uuid"].".png";	
+		$result = @unlink($icon_name);
+		if(!$result){
+			$err = error_get_last();
+			$this->RenderError($err["message"]);
+		}
 	}
 
 	public function SaveIcon()
 	{
+		if(!isset($_POST["uuid"])){
+			$this->set("content","Domain ID is required.");
+			$this->render("error");
+			return;
+		}
+
+		$uuid = $_POST["uuid"];
+		if(!$this->IsUUID($uuid)){
+			$this->RenderErorr("Invalid UUID");
+		}
+
+		$this->set("uuid",$uuid);
+		$tmp_name = $_FILES["icon"]["tmp_name"];
+
+		if(is_uploaded_file($tmp_name) === FALSE){
+			$this->RenderError("Failed to upload file");
+			return;
+		}
+
+		$img = @imagecreatefrompng($tmp_name);
+		if($img === FALSE){
+			$this->RenderError("Failed to create image from uploaded file");
+			return;
+		}
+
+		$width = 81;
+		$height = 81;
+		$resized_img = @imagecreatetruecolor($width,$height);
+		@imagecopyresampled($resized_img,$img,0,0,0,0,$width,$height,imagesx($img),imagesy($img));
+		
+		$target_name = "../icon/".$_POST["uuid"].".png";
+		if(@imagepng($resized_img,$target_name) === FALSE){
+			$this->RenderError("Failed to save image");
+			return;
+		}
+
+		$this->autoRender = true;
+	}
+
+	public function RenderError($_msg)
+	{
+		$this->set("content",$_msg);
+		$this->render("error");
+	}
+
+	public function IsUUID($_uuid)
+	{
+		if(preg_match("/[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}/",$_uuid)){
+			return true;
+		}
+		return false;
 	}
 }