2 /***************************************************************************
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: usercp_avatar.php,v 1.8.2.18 2004/07/11 16:46:20 acydburn Exp $
12 ***************************************************************************/
14 /***************************************************************************
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.
22 ***************************************************************************/
24 function check_image_type(&$type, &$error, &$error_msg)
43 $error_msg = (!empty($error_msg)) ? $error_msg . '<br />' . $lang['Avatar_filetype'] : $lang['Avatar_filetype'];
50 function user_avatar_delete($avatar_type, $avatar_file)
52 global $board_config, $userdata;
54 if ( $avatar_type == USER_AVATAR_UPLOAD && $avatar_file != '' )
56 if ( @file_exists(@phpbb_realpath('./' . $board_config['avatar_path'] . '/' . $avatar_file)) )
58 @unlink('./' . $board_config['avatar_path'] . '/' . $avatar_file);
62 return ", user_avatar = '', user_avatar_type = " . USER_AVATAR_NONE;
65 function user_avatar_gallery($mode, &$error, &$error_msg, $avatar_filename)
68 if ( file_exists(@phpbb_realpath($board_config['avatar_gallery_path'] . '/' . $avatar_filename)) && ($mode == 'editprofile') )
70 $return = ", user_avatar = '" . str_replace("\'", "''", $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_GALLERY;
79 function user_avatar_url($mode, &$error, &$error_msg, $avatar_filename)
81 if ( !preg_match('#^(http)|(ftp):\/\/#i', $avatar_filename) )
83 $avatar_filename = 'http://' . $avatar_filename;
86 if ( !preg_match("#^((ht|f)tp://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png))$)#is", $avatar_filename) )
89 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Wrong_remote_avatar_format'] : $lang['Wrong_remote_avatar_format'];
93 return ( $mode == 'editprofile' ) ? ", user_avatar = '" . str_replace("\'", "''", $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_REMOTE : '';
97 function user_avatar_upload($mode, $avatar_mode, &$current_avatar, &$current_type, &$error, &$error_msg, $avatar_filename, $avatar_realname, $avatar_filesize, $avatar_filetype)
99 global $board_config, $db, $lang;
101 $ini_val = ( @phpversion() >= '4.0.0' ) ? 'ini_get' : 'get_cfg_var';
103 if ( $avatar_mode == 'remote' && preg_match('/^(http:\/\/)?([\w\-\.]+)\:?([0-9]*)\/(.*)$/', $avatar_filename, $url_ary) )
105 if ( empty($url_ary[4]) )
108 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Incomplete_URL'] : $lang['Incomplete_URL'];
112 $base_get = '/' . $url_ary[4];
113 $port = ( !empty($url_ary[3]) ) ? $url_ary[3] : 80;
115 if ( !($fsock = @fsockopen($url_ary[2], $port, $errno, $errstr)) )
118 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['No_connection_URL'] : $lang['No_connection_URL'];
122 @fputs($fsock, "GET $base_get HTTP/1.1\r\n");
123 @fputs($fsock, "HOST: " . $url_ary[2] . "\r\n");
124 @fputs($fsock, "Connection: close\r\n\r\n");
127 while( !@feof($fsock) )
129 $avatar_data .= @fread($fsock, $board_config['avatar_filesize']);
133 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))
136 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['File_no_data'] : $lang['File_no_data'];
140 $avatar_filesize = $file_data1[1];
141 $avatar_filetype = $file_data2[1];
143 if ( !$error && $avatar_filesize > 0 && $avatar_filesize < $board_config['avatar_filesize'] )
145 $avatar_data = substr($avatar_data, strlen($avatar_data) - $avatar_filesize, $avatar_filesize);
147 $tmp_path = ( !@$ini_val('safe_mode') ) ? '/tmp' : './' . $board_config['avatar_path'] . '/tmp';
148 $tmp_filename = tempnam($tmp_path, uniqid(rand()) . '-');
150 $fptr = @fopen($tmp_filename, 'wb');
151 $bytes_written = @fwrite($fptr, $avatar_data, $avatar_filesize);
154 if ( $bytes_written != $avatar_filesize )
156 @unlink($tmp_filename);
157 message_die(GENERAL_ERROR, 'Could not write avatar file to local storage. Please contact the board administrator with this message', '', __LINE__, __FILE__);
160 list($width, $height) = @getimagesize($tmp_filename);
164 $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
167 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
170 else if ( ( file_exists(@phpbb_realpath($avatar_filename)) ) && preg_match('/\.(jpg|jpeg|gif|png)$/i', $avatar_realname) )
172 if ( $avatar_filesize <= $board_config['avatar_filesize'] && $avatar_filesize > 0 )
174 preg_match('#image\/[x\-]*([a-z]+)#', $avatar_filetype, $avatar_filetype);
175 $avatar_filetype = $avatar_filetype[1];
179 $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
182 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
186 list($width, $height) = @getimagesize($avatar_filename);
189 if ( !($imgtype = check_image_type($avatar_filetype, $error, $error_msg)) )
194 if ( $width <= $board_config['avatar_max_width'] && $height <= $board_config['avatar_max_height'] )
196 $new_filename = uniqid(rand()) . $imgtype;
198 if ( $mode == 'editprofile' && $current_type == USER_AVATAR_UPLOAD && $current_avatar != '' )
200 if ( file_exists(@phpbb_realpath('./' . $board_config['avatar_path'] . '/' . $current_avatar)) )
202 @unlink('./' . $board_config['avatar_path'] . '/' . $current_avatar);
206 if( $avatar_mode == 'remote' )
208 @copy($tmp_filename, './' . $board_config['avatar_path'] . "/$new_filename");
209 @unlink($tmp_filename);
213 if ( @$ini_val('open_basedir') != '' )
215 if ( @phpversion() < '4.0.3' )
217 message_die(GENERAL_ERROR, 'open_basedir is set and your PHP version does not allow move_uploaded_file', '', __LINE__, __FILE__);
220 $move_file = 'move_uploaded_file';
227 $move_file($avatar_filename, './' . $board_config['avatar_path'] . "/$new_filename");
230 @chmod('./' . $board_config['avatar_path'] . "/$new_filename", 0777);
232 $avatar_sql = ( $mode == 'editprofile' ) ? ", user_avatar = '$new_filename', user_avatar_type = " . USER_AVATAR_UPLOAD : "'$new_filename', " . USER_AVATAR_UPLOAD;
236 $l_avatar_size = sprintf($lang['Avatar_imagesize'], $board_config['avatar_max_width'], $board_config['avatar_max_height']);
239 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
245 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)
247 global $board_config, $db, $template, $lang, $images, $theme;
248 global $phpbb_root_path, $phpEx;
250 $dir = @opendir($board_config['avatar_gallery_path']);
252 $avatar_images = array();
253 while( $file = @readdir($dir) )
255 if( $file != '.' && $file != '..' && !is_file($board_config['avatar_gallery_path'] . '/' . $file) && !is_link($board_config['avatar_gallery_path'] . '/' . $file) )
257 $sub_dir = @opendir($board_config['avatar_gallery_path'] . '/' . $file);
259 $avatar_row_count = 0;
260 $avatar_col_count = 0;
261 while( $sub_file = @readdir($sub_dir) )
263 if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $sub_file) )
265 $avatar_images[$file][$avatar_row_count][$avatar_col_count] = $file . '/' . $sub_file;
266 $avatar_name[$file][$avatar_row_count][$avatar_col_count] = ucfirst(str_replace("_", " ", preg_replace('/^(.*)\..*$/', '\1', $sub_file)));
269 if( $avatar_col_count == 5 )
272 $avatar_col_count = 0;
281 @ksort($avatar_images);
282 @reset($avatar_images);
284 if( empty($category) )
286 list($category, ) = each($avatar_images);
288 @reset($avatar_images);
290 $s_categories = '<select name="avatarcategory">';
291 while( list($key) = each($avatar_images) )
293 $selected = ( $key == $category ) ? ' selected="selected"' : '';
294 if( count($avatar_images[$key]) )
296 $s_categories .= '<option value="' . $key . '"' . $selected . '>' . ucfirst($key) . '</option>';
299 $s_categories .= '</select>';
302 for($i = 0; $i < count($avatar_images[$category]); $i++)
304 $template->assign_block_vars("avatar_row", array());
306 $s_colspan = max($s_colspan, count($avatar_images[$category][$i]));
308 for($j = 0; $j < count($avatar_images[$category][$i]); $j++)
310 $template->assign_block_vars('avatar_row.avatar_column', array(
311 "AVATAR_IMAGE" => $board_config['avatar_gallery_path'] . '/' . $avatar_images[$category][$i][$j],
312 "AVATAR_NAME" => $avatar_name[$category][$i][$j])
315 $template->assign_block_vars('avatar_row.avatar_option_column', array(
316 "S_OPTIONS_AVATAR" => $avatar_images[$category][$i][$j])
321 $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');
323 $s_hidden_vars = '<input type="hidden" name="sid" value="' . $session_id . '" /><input type="hidden" name="agreed" value="true" />';
325 for($i = 0; $i < count($params); $i++)
327 $s_hidden_vars .= '<input type="hidden" name="' . $params[$i] . '" value="' . str_replace('"', '"', $$params[$i]) . '" />';
330 $template->assign_vars(array(
331 'L_AVATAR_GALLERY' => $lang['Avatar_gallery'],
332 'L_SELECT_AVATAR' => $lang['Select_avatar'],
333 'L_RETURN_PROFILE' => $lang['Return_profile'],
334 'L_CATEGORY' => $lang['Select_category'],
336 'S_CATEGORY_SELECT' => $s_categories,
337 'S_COLSPAN' => $s_colspan,
338 'S_PROFILE_ACTION' => append_sid("profile.$phpEx?mode=$mode"),
339 'S_HIDDEN_FIELDS' => $s_hidden_vars)