changed git call from https to git readonly
[atutor.git] / mods / photo_album / classes / image_upload.class.php
1 <?php\r
2 /*==============================================================\r
3   Photo Album\r
4  ==============================================================\r
5   Copyright (c) 2006 by Dylan Cheon & Kelvin Wong\r
6   Institute for Assistive Technology / University of Victoria\r
7   http://www.canassist.ca/                                    \r
8                                                                \r
9   This program is free software. You can redistribute it and/or\r
10   modify it under the terms of the GNU General Public License  \r
11   as published by the Free Software Foundation.                \r
12  ==============================================================\r
13  */\r
14 // $Id:\r
15 \r
16 /**\r
17  * @desc        This file handles all the image upload operations\r
18  * @author      Dylan Cheon & Kelvin Wong\r
19  * @copyright   2006, Institute for Assistive Technology / University of Victoria \r
20  * @link        http://www.canassist.ca/                                    \r
21  * @license GNU\r
22  */\r
23 \r
24 require_once(PATH.'classes/phpThumb_1.7.2/phpthumb.class.php');\r
25 require_once(PATH.'classes/phpThumb_1.7.2/phpthumb.functions.php');\r
26 require_once(PATH.'classes/pa.class.php');\r
27 require_once(PATH.'define.php');\r
28 \r
29 /**\r
30  * @desc        image upload class to upload an image\r
31  * @see         class Pa\r
32  */\r
33 \r
34 class IMAGE_UPLOAD extends PA{\r
35         var $name='';\r
36         var $file_size=0;\r
37         var $file_tmp_src='';\r
38         var $file_type='';\r
39         var $thumb_image_name='';\r
40         var $view_image_name='';\r
41         var $image_copy_required=true;\r
42         var $user_input_error=0;\r
43         var $user_input_array=Array();\r
44         var $temp_folder_path='';\r
45         \r
46         /**\r
47          * @desc        image upload constructor\r
48          * @param       Array   $file_array                     array which contains the image file information (name, size, tmp_src, etc)\r
49          * @param       String  $temp_folder_path       temp folder path string to copy the image file in.\r
50          */\r
51         function IMAGE_UPLOAD($file_array, $temp_folder_path){\r
52                 parent::init();\r
53                 $this->setVariable('temp_folder_path', $temp_folder_path);\r
54                 $this->checkImageFile($file_array);\r
55                 if (($this->getVariable('user_input_error')==0) && ($this->getVariable('image_copy_required')==true)){  //no error, so process image copy operation\r
56                         $this->setVariable('name', $file_array['name']);\r
57                         $this->copyViewImage();\r
58                         $this->copyThumbImage();\r
59                 }       \r
60         }\r
61         \r
62         /**\r
63          * @desc        This function checks the given file array is properly set up\r
64          * @param       Array   $file   file array\r
65          */\r
66         function checkImageFile(&$file){\r
67                 $this->checkImageEmpty($file['name']);\r
68                 $this->checkValidType($file['type']);\r
69                 $this->checkValidNameLength($file['name']);\r
70                 $this->checkValidEscape($file['name']);\r
71                 $this->checkValidSize($file['size']);           \r
72                 $this->setVariable('file_tmp_src', $file['tmp_name']);\r
73                 \r
74         }\r
75         \r
76         /**\r
77          * @desc        This function checks whether the image is submitted empty or not\r
78          * @param       String  $file   image name\r
79          */\r
80         function checkImageEmpty(&$file){\r
81                 if (empty($file)){\r
82                         $this->storeUserError("file_empty");\r
83                 }\r
84         }\r
85         \r
86         /**\r
87          * @desc        This function stores the user input error \r
88          * @param       String  $string         error string\r
89          */\r
90         function storeUserError($string){\r
91                 $error_count=$this->getVariable('user_input_error');\r
92                 $error_array=&$this->user_input_array;\r
93                 $error_array[$error_count]=&$string;\r
94                 $this->setVariable('user_input_error', $error_count+1);\r
95         }\r
96         \r
97         /**\r
98          * @desc        This function checks whether the image size is valid\r
99          * @param       int     $file   image size\r
100          */\r
101         function checkValidSize($file){\r
102                 $max_size=get_max_file_size(PA::getVariable('course_id'));\r
103                 if ($max_size==-1){\r
104                         global $_config;\r
105                         $max_size=$_config['max_file_size'];\r
106                 } \r
107                 if ($file > $max_size){\r
108                         $this->storeUserError('file_size');\r
109                 } else {\r
110                         $this->setVariable('file_size', $file);\r
111                 }\r
112         }\r
113         \r
114         /**\r
115          * @desc        This function checks whether the image file name uses invalid escape characters or not\r
116          * @param       String  $file   image file name\r
117          */\r
118         function checkValidEscape(&$file){\r
119                 if (ereg(INVALID_ESCAPE, $file)){\r
120                         $this->storeUserError('file_escape');\r
121                 }\r
122         }       \r
123         \r
124         /**\r
125          * @desc        This function checks whether the image file name length is valid\r
126          * @param       String  $file   image name\r
127          */\r
128         function checkValidNameLength(&$file){\r
129                 if (count($file)>MAX_FILENAME_LENGTH){\r
130                         $this->storeUserError('file_length');\r
131                 }\r
132         }\r
133         \r
134         /**\r
135          * @desc        This function checks whether the image type is valid\r
136          * @param       String  $type   image type string\r
137          */\r
138         function checkValidType($type){\r
139                 global $IMAGE_TYPE;\r
140                 if (in_array($type, $IMAGE_TYPE)){\r
141                         $this->setVariable('file_type', $type);\r
142                 } else {\r
143                         $this->storeUserError('file_type');\r
144                 }\r
145         }\r
146         \r
147         /**\r
148          * @desc        This function sets the string value for the class object\r
149          * @param       String  $string         string name to be set\r
150          * @param       mixed   $value          string value \r
151          */\r
152         function setVariable($string, $value){\r
153                 switch ($string){\r
154                         case 'mode_edit':\r
155                         case 'image_copy_required':\r
156                                 if (is_bool($value)){\r
157                                         $this->{$string}=$value;\r
158                                 } else {\r
159                                         parent::storeError("string ".$string." is not boolean");\r
160                                 }\r
161                         break;\r
162                         case 'file_type':\r
163                         case 'file_tmp_src':\r
164                         case 'name':\r
165                         case 'thumb_image_name':\r
166                         case 'view_image_name':\r
167                         case 'temp_folder_path':\r
168                                 if (is_string($value)){\r
169                                         $this->{$string}=$value;\r
170                                 } else {\r
171                                         parent::storeError("string ".$string." is not string");\r
172                                 }       \r
173                         break;\r
174                         case 'user_input_error':\r
175                                 if (is_int($value)){\r
176                                         $this->{$string}=$value;\r
177                                 } else {\r
178                                         parent::storeError("string ".$string." is not int");\r
179                                 }\r
180                         break;\r
181                 }\r
182         }\r
183         \r
184         /**\r
185          * @desc        This function creates a full-sized image of the image file and copies it to the temp folder\r
186          */\r
187         function copyViewImage(){\r
188                 $temp_folder_path=$this->getVariable('temp_folder_path');\r
189                 $view_image_name=modify_image_name($temp_folder_path, $this->getVariable('name'));\r
190                 $this->setVariable('view_image_name', $view_image_name);        \r
191                 image_to_this_location($view_image_name, $this->getVariable('file_tmp_src'), $temp_folder_path);\r
192                 $thumb=new phpThumb();\r
193                 $view_image_path=AT_CONTENT_DIR.$temp_folder_path.$view_image_name;\r
194                 $thumb->setSourceFilename($view_image_path);\r
195                 $size=getimagesize($view_image_path);\r
196                 /* set size for view image */\r
197                 if ($size[0]>MAX_IMAGE_WIDTH){\r
198                         $thumb->w=MAX_IMAGE_WIDTH;\r
199                 }\r
200                 if (defined(MAX_IMAGE_HEIGHT) && $size[1]> MAX_IMAGE_HEIGHT){\r
201                         $thumb->h=MAX_IMAGE_HEIGHT;\r
202                 }\r
203                 \r
204                 $thumb->iar='l';\r
205                 $thumb->config_output_format='jpeg';\r
206                 \r
207                 /* generate for view image */   \r
208                 if ($thumb->GenerateThumbnail()){\r
209                         if ($view_image_path){\r
210                                 if (!$thumb->RenderToFile($view_image_path)){\r
211                                         parent::storeError("render operation failed for view image");   \r
212                                 }\r
213                         } else {\r
214                                 parent::storeError("view image path does not exist");\r
215                         }\r
216                 } else {\r
217                         parent::storeError("View generateThumbnail operation is failed");\r
218                 }\r
219         }\r
220         \r
221         /**\r
222          * @desc        This function creates a thumbnail image of the image file and stores it in the temp folder\r
223          */     \r
224         function copyThumbImage(){\r
225                 $temp_folder_path=$this->getVariable('temp_folder_path');\r
226                 $thumb_image_name=insert_into_image_name($this->getVariable('view_image_name'), THUMB_EXT);\r
227                 $thumb_image_name=modify_image_name($temp_folder_path, $thumb_image_name);\r
228                 $this->setVariable('thumb_image_name', $thumb_image_name);\r
229                 $thumb=new phpThumb();\r
230                 $view_image_path=AT_CONTENT_DIR.$temp_folder_path.$this->getVariable('view_image_name');\r
231                 $thumb->setSourceFilename($view_image_path);\r
232                 $size=getimagesize($view_image_path);\r
233                 /* set size for thumb image */\r
234                 $thumb->iar='l';\r
235                 $thumb->config_output_format='jpeg';\r
236                 \r
237                 /* set size for thumb image */\r
238                 $thumb->h=THUMB_IMAGE_HEIGHT;\r
239                 $thumb->w=THUMB_IMAGE_WIDTH;\r
240                 \r
241                 /* generate for thumb image */  \r
242                 $thumb_image_path=AT_CONTENT_DIR.$temp_folder_path.$thumb_image_name;\r
243                 if ($thumb->GenerateThumbnail()){\r
244                         if ($view_image_path){\r
245                                 if (!$thumb->RenderToFile($thumb_image_path)){\r
246                                         parent::storeError("render operation failed for thumb image");  \r
247                                 }\r
248                         } else {\r
249                                 parent::storeError("view image path does not exist");\r
250                         }\r
251                 } else {\r
252                         parent::storeError("Thumb generateThumbnail operation is failed");\r
253                 }\r
254         }\r
255 }       \r
256 ?>\r