2 /*==============================================================
\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
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
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
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
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
37 while (image_name_duplicate_check($location, $temp)!=true){
\r
38 $temp=insert_into_image_name($new_name, $i);
\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
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
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
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
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
79 function is_admin_for_course(){
\r
80 if ($_SESSION['privileges'] != NORMAL_USER){
\r
82 } else if ($_SESSION['is_admin']==true){
\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
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
99 if (!@copy($temp_image_file, $store_image)){
\r
101 $msg->addError('pa_func_copy');
\r
102 redirect('index.php');
\r
104 @chmod($store_image, 0777);
\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
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
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
129 function convert_newlines($string){
\r
130 $input=eregi_replace("\n", "<br/>", $string);
\r
136 * @desc This function redirect the user to the requested address
\r
137 * @param String $addr redirection destination URI
\r
139 function redirect($addr){
\r
140 $url=ATUTOR_PREFIX.BASE_PATH.$addr;
\r
142 <META http-equiv='refresh' content='0;URL=$url'>
\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
154 function to_pos_int($input){
\r
156 if (is_numeric($input)){
\r
157 $temp=intval($input);
\r
158 $result=max(1, $temp);
\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
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
181 * @desc This function creates a folder.
\r
182 * @param String $folder folder name
\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
189 $msg->addError('pa_func_mkdir');
\r
190 redirect('index.php');
\r
197 * @desc This function makes a temp folder under the users folder
\r
198 * @return String temp folder path
\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
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
213 return $temp_folder;
\r
218 * @desc This function deletes all the files in the temp folder directory
\r
220 function clear_temp_folder(){
\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
229 while ($obj=readdir($files)){
\r
230 if ($obj == '.' || $obj=='..' || $obj=='.svn'){
\r
233 if (!@unlink($temp_dir.$obj)){
\r
234 $msg->addError('pa_func_clear_temp_folder_unlink');
\r
235 redirect('../../index.php');
\r
244 * @desc This function redirects to the appropriate page when an error occurs depending on the user type
\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
254 redirect('view.php?image_id='.$_SESSION['pa']['image_id']);
\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
262 redirect('index.php');
\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
270 redirect('index.php');
\r
273 } else { //choose is comment
\r
274 redirect('view.php?image_id='.$_SESSION['pa']['image_id']);
\r