Mercurial > hg > Members > shoshi > webvirt
comparison 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 |
comparison
equal
deleted
inserted
replaced
3:7201fd354bdf | 4:c3adb28473d6 |
---|---|
6 public $autoRender = false; | 6 public $autoRender = false; |
7 public $uses = null; | 7 public $uses = null; |
8 | 8 |
9 public function index() | 9 public function index() |
10 { | 10 { |
11 $this->autoRender = false; | |
11 $icon_dir = "../icon"; //relative path without slash | 12 $icon_dir = "../icon"; //relative path without slash |
12 $this->autoRender = false; | |
13 $icon_default = "/default.png"; //file name must include slash | 13 $icon_default = "/default.png"; //file name must include slash |
14 $icon_file = $icon_default; | 14 $icon_file = $icon_default; |
15 | 15 |
16 if(isset($this->params["url"]["uuid"])){ | 16 if(isset($_GET["uuid"])){ |
17 $icon_file = "/".$this->params["url"]["uuid"]."png"; | 17 $uuid = $_GET["uuid"]; |
18 if($this->IsUUID($uuid)){ | |
19 $icon_file = "/".$uuid.".png"; | |
20 }else{ | |
21 $this->RenderError("Invalid UUID."); | |
22 return; | |
23 } | |
18 } | 24 } |
19 | 25 |
20 $icon = @file_get_contents($icon_dir.$icon_file); | 26 $icon = @file_get_contents($icon_dir.$icon_file); |
21 if($icon === FALSE){ | 27 if($icon === FALSE){ |
22 //failed to get icon | 28 //failed to get icon |
23 $icon = file_get_contents($icon_dir.$icon_default); | 29 $icon = @file_get_contents($icon_dir.$icon_default); |
30 if($icon === FALSE){ | |
31 //print error code here. | |
32 $err = error_get_last(); | |
33 $this->RenderErorr($err["message"]); | |
34 return; | |
35 } | |
24 } | 36 } |
25 | 37 |
26 header("Content-Type: image/png"); | 38 header("Content-Type: image/png"); |
27 print($icon); | 39 print($icon); |
28 } | 40 } |
29 | 41 |
30 public function EditIcon() | 42 public function EditIcon() |
31 { | 43 { |
44 if(!isset($this->params["url"]["uuid"])){ | |
45 $this->set("content","Domain ID is required."); | |
46 $this->render("error"); | |
47 return; | |
48 } | |
49 | |
50 $uuid = $this->params["url"]["uuid"]; | |
51 if(!$this->IsUUID($uuid)){ | |
52 $this->RenderError("Invalid UUID."); | |
53 } | |
54 | |
55 $this->autoRender = true; | |
56 | |
57 $this->set("max_size",ini_get("post_max_size")); | |
58 $this->set("uuid",$uuid); | |
59 } | |
60 | |
61 public function DeleteIcon() | |
62 { | |
63 $this->autoRender = true; | |
64 | |
65 if(!isset($_POST["uuid"])){ | |
66 $this->RenderError("Domain ID is required."); | |
67 return; | |
68 } | |
69 | |
70 $uuid = $_POST["uuid"]; | |
71 if($this->IsUUID($uuid)){ | |
72 $this->RenderError("Invalid UUID."); | |
73 return; | |
74 } | |
75 | |
76 $icon_name = "../icon/".$_POST["uuid"].".png"; | |
77 $result = @unlink($icon_name); | |
78 if(!$result){ | |
79 $err = error_get_last(); | |
80 $this->RenderError($err["message"]); | |
81 } | |
32 } | 82 } |
33 | 83 |
34 public function SaveIcon() | 84 public function SaveIcon() |
35 { | 85 { |
86 if(!isset($_POST["uuid"])){ | |
87 $this->set("content","Domain ID is required."); | |
88 $this->render("error"); | |
89 return; | |
90 } | |
91 | |
92 $uuid = $_POST["uuid"]; | |
93 if(!$this->IsUUID($uuid)){ | |
94 $this->RenderErorr("Invalid UUID"); | |
95 } | |
96 | |
97 $this->set("uuid",$uuid); | |
98 $tmp_name = $_FILES["icon"]["tmp_name"]; | |
99 | |
100 if(is_uploaded_file($tmp_name) === FALSE){ | |
101 $this->RenderError("Failed to upload file"); | |
102 return; | |
103 } | |
104 | |
105 $img = @imagecreatefrompng($tmp_name); | |
106 if($img === FALSE){ | |
107 $this->RenderError("Failed to create image from uploaded file"); | |
108 return; | |
109 } | |
110 | |
111 $width = 81; | |
112 $height = 81; | |
113 $resized_img = @imagecreatetruecolor($width,$height); | |
114 @imagecopyresampled($resized_img,$img,0,0,0,0,$width,$height,imagesx($img),imagesy($img)); | |
115 | |
116 $target_name = "../icon/".$_POST["uuid"].".png"; | |
117 if(@imagepng($resized_img,$target_name) === FALSE){ | |
118 $this->RenderError("Failed to save image"); | |
119 return; | |
120 } | |
121 | |
122 $this->autoRender = true; | |
123 } | |
124 | |
125 public function RenderError($_msg) | |
126 { | |
127 $this->set("content",$_msg); | |
128 $this->render("error"); | |
129 } | |
130 | |
131 public function IsUUID($_uuid) | |
132 { | |
133 if(preg_match("/[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}/",$_uuid)){ | |
134 return true; | |
135 } | |
136 return false; | |
36 } | 137 } |
37 } | 138 } |
38 | 139 |
39 ?> | 140 ?> |