]> scripts.mit.edu Git - autoinstallsdev/phpBB.git/blob - includes/usercp_avatar.php
phpBB 2.0.19
[autoinstallsdev/phpBB.git] / includes / usercp_avatar.php
1 <?php
2 /***************************************************************************
3  *                             usercp_avatar.php
4  *                            -------------------
5  *   begin                : Saturday, Feb 13, 2001
6  *   copyright            : (C) 2001 The phpBB Group
7  *   email                : support@phpbb.com
8  *
9  *   $Id: usercp_avatar.php,v 1.8.2.22 2005/10/30 15:17:14 acydburn Exp $
10  *
11  *
12  ***************************************************************************/
13
14 /***************************************************************************
15  *
16  *   This program is free software; you can redistribute it and/or modify
17  *   it under the terms of the GNU General Public License as published by
18  *   the Free Software Foundation; either version 2 of the License, or
19  *   (at your option) any later version.
20  *
21  *
22  ***************************************************************************/
23
24 function check_image_type(&$type, &$error, &$error_msg)
25 {
26         global $lang;
27
28         switch( $type )
29         {
30                 case 'jpeg':
31                 case 'pjpeg':
32                 case 'jpg':
33                         return '.jpg';
34                         break;
35                 case 'gif':
36                         return '.gif';
37                         break;
38                 case 'png':
39                         return '.png';
40                         break;
41                 default:
42                         $error = true;
43                         $error_msg = (!empty($error_msg)) ? $error_msg . '<br />' . $lang['Avatar_filetype'] : $lang['Avatar_filetype'];
44                         break;
45         }
46
47         return false;
48 }
49
50 function user_avatar_delete($avatar_type, $avatar_file)
51 {
52         global $board_config, $userdata;
53
54         $avatar_file = basename($avatar_file);
55         if ( $avatar_type == USER_AVATAR_UPLOAD && $avatar_file != '' )
56         {
57                 if ( @file_exists(@phpbb_realpath('./' . $board_config['avatar_path'] . '/' . $avatar_file)) )
58                 {
59                         @unlink('./' . $board_config['avatar_path'] . '/' . $avatar_file);
60                 }
61         }
62
63         return ", user_avatar = '', user_avatar_type = " . USER_AVATAR_NONE;
64 }
65
66 function user_avatar_gallery($mode, &$error, &$error_msg, $avatar_filename, $avatar_category)
67 {
68         global $board_config;
69
70         $avatar_filename = phpbb_ltrim(basename($avatar_filename), "'");
71         $avatar_category = phpbb_ltrim(basename($avatar_category), "'");
72         
73         if(!preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $avatar_filename))
74         {
75                 return '';
76         }
77
78         if ($avatar_filename == "" || $avatar_category == "")
79         {
80                 return '';
81         } 
82
83         if ( file_exists(@phpbb_realpath($board_config['avatar_gallery_path'] . '/' . $avatar_category . '/' . $avatar_filename)) && ($mode == 'editprofile') )
84         {
85                 $return = ", user_avatar = '" . str_replace("\'", "''", $avatar_category . '/' . $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_GALLERY;
86         }
87         else
88         {
89                 $return = '';
90         }
91         return $return;
92 }
93
94 function user_avatar_url($mode, &$error, &$error_msg, $avatar_filename)
95 {
96         global $lang;
97
98         if ( !preg_match('#^(http)|(ftp):\/\/#i', $avatar_filename) )
99         {
100                 $avatar_filename = 'http://' . $avatar_filename;
101         }
102
103         if ( !preg_match("#^((ht|f)tp://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png))$)#is", $avatar_filename) )
104         {
105                 $error = true;
106                 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Wrong_remote_avatar_format'] : $lang['Wrong_remote_avatar_format'];
107                 return;
108         }
109
110         return ( $mode == 'editprofile' ) ? ", user_avatar = '" . str_replace("\'", "''", $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_REMOTE : '';
111
112 }
113
114 function user_avatar_upload($mode, $avatar_mode, &$current_avatar, &$current_type, &$error, &$error_msg, $avatar_filename, $avatar_realname, $avatar_filesize, $avatar_filetype)
115 {
116         global $board_config, $db, $lang;
117
118         $ini_val = ( @phpversion() >= '4.0.0' ) ? 'ini_get' : 'get_cfg_var';
119
120         $width = $height = 0;
121         $type = '';
122
123         if ( $avatar_mode == 'remote' && preg_match('/^(http:\/\/)?([\w\-\.]+)\:?([0-9]*)\/(.*)$/', $avatar_filename, $url_ary) )
124         {
125                 if ( empty($url_ary[4]) )
126                 {
127                         $error = true;
128                         $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Incomplete_URL'] : $lang['Incomplete_URL'];
129                         return;
130                 }
131
132                 $base_get = '/' . $url_ary[4];
133                 $port = ( !empty($url_ary[3]) ) ? $url_ary[3] : 80;
134
135                 if ( !($fsock = @fsockopen($url_ary[2], $port, $errno, $errstr)) )
136                 {
137                         $error = true;
138                         $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['No_connection_URL'] : $lang['No_connection_URL'];
139                         return;
140                 }
141
142                 @fputs($fsock, "GET $base_get HTTP/1.1\r\n");
143                 @fputs($fsock, "HOST: " . $url_ary[2] . "\r\n");
144                 @fputs($fsock, "Connection: close\r\n\r\n");
145
146                 unset($avatar_data);
147                 while( !@feof($fsock) )
148                 {
149                         $avatar_data .= @fread($fsock, $board_config['avatar_filesize']);
150                 }
151                 @fclose($fsock);
152
153                 if (!preg_match('#Content-Length\: ([0-9]+)[^ /][\s]+#i', $avatar_data, $file_data1) || !preg_match('#Content-Type\: image/[x\-]*([a-z]+)[\s]+#i', $avatar_data, $file_data2))
154                 {
155                         $error = true;
156                         $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['File_no_data'] : $lang['File_no_data'];
157                         return;
158                 }
159
160                 $avatar_filesize = $file_data1[1]; 
161                 $avatar_filetype = $file_data2[1]; 
162
163                 if ( !$error && $avatar_filesize > 0 && $avatar_filesize < $board_config['avatar_filesize'] )
164                 {
165                         $avatar_data = substr($avatar_data, strlen($avatar_data) - $avatar_filesize, $avatar_filesize);
166
167                         $tmp_path = ( !@$ini_val('safe_mode') ) ? '/tmp' : './' . $board_config['avatar_path'] . '/tmp';
168                         $tmp_filename = tempnam($tmp_path, uniqid(rand()) . '-');
169
170                         $fptr = @fopen($tmp_filename, 'wb');
171                         $bytes_written = @fwrite($fptr, $avatar_data, $avatar_filesize);
172                         @fclose($fptr);
173
174                         if ( $bytes_written != $avatar_filesize )
175                         {
176                                 @unlink($tmp_filename);
177                                 message_die(GENERAL_ERROR, 'Could not write avatar file to local storage. Please contact the board administrator with this message', '', __LINE__, __FILE__);
178                         }
179
180                         list($width, $height, $type) = @getimagesize($tmp_filename);
181                 }
182                 else
183                 {
184                         $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
185
186                         $error = true;
187                         $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
188                 }
189         }
190         else if ( ( file_exists(@phpbb_realpath($avatar_filename)) ) && preg_match('/\.(jpg|jpeg|gif|png)$/i', $avatar_realname) )
191         {
192                 if ( $avatar_filesize <= $board_config['avatar_filesize'] && $avatar_filesize > 0 )
193                 {
194                         preg_match('#image\/[x\-]*([a-z]+)#', $avatar_filetype, $avatar_filetype);
195                         $avatar_filetype = $avatar_filetype[1];
196                 }
197                 else
198                 {
199                         $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
200
201                         $error = true;
202                         $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
203                         return;
204                 }
205
206                 list($width, $height, $type) = @getimagesize($avatar_filename);
207         }
208
209         if ( !($imgtype = check_image_type($avatar_filetype, $error, $error_msg)) )
210         {
211                 return;
212         }
213
214         switch ($type)
215         {
216                 // GIF
217                 case 1:
218                         if ($imgtype != '.gif')
219                         {
220                                 @unlink($tmp_filename);
221                                 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
222                         }
223                 break;
224
225                 // JPG, JPC, JP2, JPX, JB2
226                 case 2:
227                 case 9:
228                 case 10:
229                 case 11:
230                 case 12:
231                         if ($imgtype != '.jpg' && $imgtype != '.jpeg')
232                         {
233                                 @unlink($tmp_filename);
234                                 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
235                         }
236                 break;
237
238                 // PNG
239                 case 3:
240                         if ($imgtype != '.png')
241                         {
242                                 @unlink($tmp_filename);
243                                 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
244                         }
245                 break;
246
247                 default:
248                         @unlink($tmp_filename);
249                         message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
250         }
251
252         if ( $width > 0 && $height > 0 && $width <= $board_config['avatar_max_width'] && $height <= $board_config['avatar_max_height'] )
253         {
254                 $new_filename = uniqid(rand()) . $imgtype;
255
256                 if ( $mode == 'editprofile' && $current_type == USER_AVATAR_UPLOAD && $current_avatar != '' )
257                 {
258                         user_avatar_delete($current_type, $current_avatar);
259                 }
260
261                 if( $avatar_mode == 'remote' )
262                 {
263                         @copy($tmp_filename, './' . $board_config['avatar_path'] . "/$new_filename");
264                         @unlink($tmp_filename);
265                 }
266                 else
267                 {
268                         if ( @$ini_val('open_basedir') != '' )
269                         {
270                                 if ( @phpversion() < '4.0.3' )
271                                 {
272                                         message_die(GENERAL_ERROR, 'open_basedir is set and your PHP version does not allow move_uploaded_file', '', __LINE__, __FILE__);
273                                 }
274
275                                 $move_file = 'move_uploaded_file';
276                         }
277                         else
278                         {
279                                 $move_file = 'copy';
280                         }
281
282                         if (!is_uploaded_file($avatar_filename))
283                         {
284                                 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
285                         }
286                         $move_file($avatar_filename, './' . $board_config['avatar_path'] . "/$new_filename");
287                 }
288
289                 @chmod('./' . $board_config['avatar_path'] . "/$new_filename", 0777);
290
291                 $avatar_sql = ( $mode == 'editprofile' ) ? ", user_avatar = '$new_filename', user_avatar_type = " . USER_AVATAR_UPLOAD : "'$new_filename', " . USER_AVATAR_UPLOAD;
292         }
293         else
294         {
295                 $l_avatar_size = sprintf($lang['Avatar_imagesize'], $board_config['avatar_max_width'], $board_config['avatar_max_height']);
296
297                 $error = true;
298                 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
299         }
300
301         return $avatar_sql;
302 }
303
304 function display_avatar_gallery($mode, &$category, &$user_id, &$email, &$current_email, &$coppa, &$username, &$email, &$new_password, &$cur_password, &$password_confirm, &$icq, &$aim, &$msn, &$yim, &$website, &$location, &$occupation, &$interests, &$signature, &$viewemail, &$notifypm, &$popup_pm, &$notifyreply, &$attachsig, &$allowhtml, &$allowbbcode, &$allowsmilies, &$hideonline, &$style, &$language, &$timezone, &$dateformat, &$session_id)
305 {
306         global $board_config, $db, $template, $lang, $images, $theme;
307         global $phpbb_root_path, $phpEx;
308
309         $dir = @opendir($board_config['avatar_gallery_path']);
310
311         $avatar_images = array();
312         while( $file = @readdir($dir) )
313         {
314                 if( $file != '.' && $file != '..' && !is_file($board_config['avatar_gallery_path'] . '/' . $file) && !is_link($board_config['avatar_gallery_path'] . '/' . $file) )
315                 {
316                         $sub_dir = @opendir($board_config['avatar_gallery_path'] . '/' . $file);
317
318                         $avatar_row_count = 0;
319                         $avatar_col_count = 0;
320                         while( $sub_file = @readdir($sub_dir) )
321                         {
322                                 if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $sub_file) )
323                                 {
324                                         $avatar_images[$file][$avatar_row_count][$avatar_col_count] = $sub_file; 
325                                         $avatar_name[$file][$avatar_row_count][$avatar_col_count] = ucfirst(str_replace("_", " ", preg_replace('/^(.*)\..*$/', '\1', $sub_file)));
326
327                                         $avatar_col_count++;
328                                         if( $avatar_col_count == 5 )
329                                         {
330                                                 $avatar_row_count++;
331                                                 $avatar_col_count = 0;
332                                         }
333                                 }
334                         }
335                 }
336         }
337
338         @closedir($dir);
339
340         @ksort($avatar_images);
341         @reset($avatar_images);
342
343         if( empty($category) )
344         {
345                 list($category, ) = each($avatar_images);
346         }
347         @reset($avatar_images);
348
349         $s_categories = '<select name="avatarcategory">';
350         while( list($key) = each($avatar_images) )
351         {
352                 $selected = ( $key == $category ) ? ' selected="selected"' : '';
353                 if( count($avatar_images[$key]) )
354                 {
355                         $s_categories .= '<option value="' . $key . '"' . $selected . '>' . ucfirst($key) . '</option>';
356                 }
357         }
358         $s_categories .= '</select>';
359
360         $s_colspan = 0;
361         for($i = 0; $i < count($avatar_images[$category]); $i++)
362         {
363                 $template->assign_block_vars("avatar_row", array());
364
365                 $s_colspan = max($s_colspan, count($avatar_images[$category][$i]));
366
367                 for($j = 0; $j < count($avatar_images[$category][$i]); $j++)
368                 {
369                         $template->assign_block_vars('avatar_row.avatar_column', array(
370                                 "AVATAR_IMAGE" => $board_config['avatar_gallery_path'] . '/' . $category . '/' . $avatar_images[$category][$i][$j], 
371                                 "AVATAR_NAME" => $avatar_name[$category][$i][$j])
372                         );
373
374                         $template->assign_block_vars('avatar_row.avatar_option_column', array(
375                                 "S_OPTIONS_AVATAR" => $avatar_images[$category][$i][$j])
376                         );
377                 }
378         }
379
380         $params = array('coppa', 'user_id', 'username', 'email', 'current_email', 'cur_password', 'new_password', 'password_confirm', 'icq', 'aim', 'msn', 'yim', 'website', 'location', 'occupation', 'interests', 'signature', 'viewemail', 'notifypm', 'popup_pm', 'notifyreply', 'attachsig', 'allowhtml', 'allowbbcode', 'allowsmilies', 'hideonline', 'style', 'language', 'timezone', 'dateformat');
381
382         $s_hidden_vars = '<input type="hidden" name="sid" value="' . $session_id . '" /><input type="hidden" name="agreed" value="true" /><input type="hidden" name="avatarcatname" value="' . $category . '" />';
383
384         for($i = 0; $i < count($params); $i++)
385         {
386                 $s_hidden_vars .= '<input type="hidden" name="' . $params[$i] . '" value="' . str_replace('"', '&quot;', $$params[$i]) . '" />';
387         }
388         
389         $template->assign_vars(array(
390                 'L_AVATAR_GALLERY' => $lang['Avatar_gallery'], 
391                 'L_SELECT_AVATAR' => $lang['Select_avatar'], 
392                 'L_RETURN_PROFILE' => $lang['Return_profile'], 
393                 'L_CATEGORY' => $lang['Select_category'], 
394
395                 'S_CATEGORY_SELECT' => $s_categories, 
396                 'S_COLSPAN' => $s_colspan, 
397                 'S_PROFILE_ACTION' => append_sid("profile.$phpEx?mode=$mode"), 
398                 'S_HIDDEN_FIELDS' => $s_hidden_vars)
399         );
400
401         return;
402 }
403
404 ?>