view 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 source

<?php

class IconController extends AppController
{
	public $name = "icon";
	public $autoRender = false;
	public $uses = null;

	public function index()
	{
		$this->autoRender = false;
		$icon_dir = "../icon"; //relative path without slash
		$icon_default = "/default.png"; //file name must include slash
		$icon_file = $icon_default;

		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);
			if($icon === FALSE){
				//print error code here.
				$err = error_get_last();
				$this->RenderErorr($err["message"]);
				return;
			}
		}

		header("Content-Type: image/png");
		print($icon);
	}

	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;
	}
}

?>