a439a96c6554f409b0bd5c999318e42535278c65
[atutor.git] / mods / photo_album / include / general_func.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 defines some general purpose functions used on almost every page\r
18  * @author      Dylan Cheon\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  \r
25 /**\r
26  * @desc        The function prevents file name collision. It also converts spaces into underscores and renames files to jpeg extensions\r
27  * @param       String  $location               location string\r
28  * @param       String  $image_name             image name\r
29  * @return      String                                  new image name\r
30  */\r
31 function modify_image_name($location, $image_name){\r
32         $image_name=ereg_replace(" ", "_", $image_name);\r
33         $temp_image=without_ext($image_name);\r
34         $new_name=$temp_image.'.jpg';\r
35         $temp=$new_name;\r
36         $i=0;\r
37         while (image_name_duplicate_check($location, $temp)!=true){\r
38                 $temp=insert_into_image_name($new_name, $i);\r
39                 $i++;\r
40         }\r
41         return $temp;\r
42 }\r
43 \r
44 \r
45 /**\r
46  * @desc        This helper function checks if there is an image with the same name in the folder \r
47  * @param       String  $location               location folder name\r
48  * @param       String  $image name             image file name\r
49  * @return      Boolean                                 true if the image name is unique to the folder\r
50  */     \r
51 function image_name_duplicate_check($location, $image_name){\r
52         $image_file=AT_CONTENT_DIR.$location.$image_name;\r
53         if (is_file($image_file)){\r
54                 return false;\r
55         } else {\r
56                 return true;\r
57         }\r
58 }\r
59         \r
60         \r
61 /**\r
62  * @desc        This function inserts the given string into the image file name string. It is used to prevent file name collisions\r
63  * @param       String  $image name     image name string\r
64  * @param       String  $insert                 insert string\r
65  * @return      String                                  new image name\r
66  */\r
67 function insert_into_image_name($image_name, $insert){\r
68         $position=strpos($image_name, '.');\r
69         $string1=substr($image_name, 0, $position);\r
70         $string2=substr($image_name, $position+1);\r
71         $result=$string1.'_'.$insert.'.'.$string2;\r
72         return $result;\r
73 }\r
74         \r
75 /**\r
76  * @desc        This function checks whether the user is an instructor or administrator.  If the user is neither student nor guest, it returns true\r
77  * @return      Boolean         true if the user is either administrator or instructor\r
78  */\r
79 function is_admin_for_course(){\r
80   if ($_SESSION['privileges'] != NORMAL_USER){ \r
81     return true;\r
82   } else if ($_SESSION['is_admin']==true){\r
83     return true;\r
84   } else {\r
85     return false;\r
86   }\r
87 }\r
88         \r
89 /**\r
90  * @desc        This function copies the image file to given location\r
91  * @param       String  $image                          filename to be named\r
92  * @param       String  $temp_image_file        image source to be copied\r
93  * @param       String  $location                       location folder name\r
94  */     \r
95 function image_to_this_location($image, $temp_image_file, $location){\r
96         $store_folder=AT_CONTENT_DIR.$location;\r
97         $store_image=$store_folder.$image;\r
98         \r
99         if (!@copy($temp_image_file, $store_image)){\r
100                 global $msg;\r
101                 $msg->addError('pa_func_copy');\r
102                 redirect('index.php');\r
103         } \r
104         @chmod($store_image, 0777);\r
105 }\r
106         \r
107         \r
108 /**\r
109  * @desc        This function deletes the files of the given Array (array should be mysql resource)\r
110  * @param       Array   $array  array which contains the file names and locations\r
111  */\r
112 function delete_image_files($array){\r
113         error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);\r
114         while ($row=mysql_fetch_array($array)){ \r
115                 $location=AT_CONTENT_DIR.$row['location'];\r
116                 $image_path=$location.$row['view_image_name'];\r
117                 $thumb_path=$location.$row['thumb_image_name'];\r
118                 unlink($image_path);\r
119                 unlink($thumb_path);\r
120         }\r
121 }\r
122         \r
123         \r
124 /**\r
125  * @desc        This function convert the newline character to <br>\r
126  * @param       String  $string         string to be converted\r
127  * @return      String                          the new string\r
128  */\r
129 function convert_newlines($string){\r
130         $input=eregi_replace("\n", "<br/>", $string);\r
131         return $input;\r
132 }       \r
133         \r
134         \r
135 /**\r
136  * @desc        This function redirect the user to the requested address\r
137  * @param       String  $addr   redirection destination URI\r
138  */\r
139 function redirect($addr){\r
140         $url=ATUTOR_PREFIX.BASE_PATH.$addr;\r
141         echo ("\r
142                 <META http-equiv='refresh' content='0;URL=$url'>\r
143         ");     \r
144         exit;   \r
145 }\r
146         \r
147         \r
148         \r
149 /**\r
150  * @desc        This function returns the positive int value of the given input.  If the given input is not numeric, it returns 1\r
151  * @param       int     $input  input value\r
152  * @return      int                     positive int value\r
153  */\r
154 function to_pos_int($input){\r
155         $result;\r
156         if (is_numeric($input)){\r
157                 $temp=intval($input);\r
158                 $result=max(1, $temp);\r
159         } else {\r
160                 $result=1;\r
161         }\r
162         return $result;\r
163 }       \r
164         \r
165 /**\r
166  * @desc        This function returns the file name without an extention\r
167  * @param       String  $input  file name \r
168  * @return      String                  file name without extention\r
169  */\r
170 function without_ext($input){\r
171         $pos=strpos($input, ".");\r
172         $string=substr($input, 0, $pos);\r
173         if (empty($string)){\r
174                 return "unknown_image";\r
175         } else {\r
176                 return $string;\r
177         }\r
178 }\r
179 \r
180 /**\r
181  * @desc        This function creates a folder.\r
182  * @param       String  $folder         folder name\r
183  */     \r
184 function create_folder($folder){\r
185         $folder=AT_CONTENT_DIR.$folder;\r
186         if (!is_dir($folder)){\r
187                 if (!mkdir($folder)){\r
188                         global $msg;\r
189                         $msg->addError('pa_func_mkdir');\r
190                         redirect('index.php');\r
191                 }\r
192                 \r
193         }\r
194 }\r
195 \r
196 /**\r
197  * @desc        This function makes a temp folder under the users folder\r
198  * @return      String  temp folder path\r
199  */\r
200 function make_temp_folder(){\r
201         $my_dir=ALBUM_IMAGE_STORE.$_SESSION['login'].'/';\r
202         create_folder($my_dir);\r
203         if(!is_writable($my_dir) && @chmod($my_dir, 0777)){\r
204                 $msg->addError('pa_func_make_temp_folder');\r
205                 redirect('index.php');\r
206         }\r
207         $temp_folder=$my_dir.TEMP_FOLDER_NAME;\r
208         create_folder($temp_folder);\r
209         if(!is_writable($temp_folder) && @chmod($temp_folder, 0777)){\r
210                 $msg->addError('pa_func_make_temp_folder');\r
211                 redirect('index.php');\r
212         }\r
213         return $temp_folder;\r
214 }\r
215 \r
216 \r
217 /**\r
218  * @desc        This function deletes all the files in the temp folder directory\r
219  */\r
220 function clear_temp_folder(){\r
221         global $msg;\r
222         $temp_dir=AT_CONTENT_DIR.ALBUM_IMAGE_STORE.$_SESSION['login'].'/temp/';\r
223         if (is_dir($temp_dir)){ \r
224                 $temp=substr($temp_dir, 0, -1);\r
225                 if (!$files=@opendir($temp)){\r
226                         $msg->addError('pa_func_clear_temp_folder_open');\r
227                         redirect('../../index.php');\r
228                 }\r
229                 while ($obj=readdir($files)){\r
230                         if ($obj == '.' || $obj=='..' || $obj=='.svn'){\r
231                                 continue;\r
232                         } else {\r
233                                 if (!@unlink($temp_dir.$obj)){\r
234                                         $msg->addError('pa_func_clear_temp_folder_unlink');\r
235                                         redirect('../../index.php');\r
236                                 }\r
237                         }\r
238                 }\r
239         }\r
240 }\r
241 \r
242 \r
243 /**\r
244  * @desc        This function redirects to the appropriate page when an error occurs depending on the user type\r
245  */\r
246 function out(){\r
247         if ($_SESSION['pa']['choose']==IMAGE){\r
248                 if ($_SESSION['pa']['mode']=='edit'){\r
249                         if ($_SESSION['pa']['my_pic_mode']==true){\r
250                                 redirect('my_photo.php');\r
251                         } else if ((!is_admin_for_course()) && (get_config_mode($_SESSION['pa']['course_id'])==CONFIG_ENABLED)){\r
252                                 redirect('index.php');\r
253                         } else {\r
254                                 redirect('view.php?image_id='.$_SESSION['pa']['image_id']);\r
255                         }\r
256                 } else if ($_SESSION['pa']['mode']=='add'){\r
257                         if ($_SESSION['pa']['instructor_mode']==true){\r
258                                 redirect('instructor_image.php');\r
259                         } else if ($_SESSION['pa']['administrator_mode']==true){\r
260                                 redirect('admin_image_list.php');\r
261                         } else {\r
262                                 redirect('index.php');\r
263                         }\r
264                 } else {        //mode is delete\r
265                         if ($_SESSION['pa']['instructor_mode']==true){\r
266                                 redirect('instructor_image.php');\r
267                         } else if ($_SESSION['pa']['administrator_mode']==true){\r
268                                 redirect('admin_image_list.php');\r
269                         } else {\r
270                                 redirect('index.php');\r
271                         }\r
272                 }\r
273         } else {        //choose is comment\r
274                 redirect('view.php?image_id='.$_SESSION['pa']['image_id']);\r
275         }\r
276 }\r
277 \r
278                         \r
279 ?>\r