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.22 2005/10/30 15:17:14 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 $avatar_file = basename($avatar_file);
55 if ( $avatar_type == USER_AVATAR_UPLOAD && $avatar_file != '' )
57 if ( @file_exists(@phpbb_realpath('./' . $board_config['avatar_path'] . '/' . $avatar_file)) )
59 @unlink('./' . $board_config['avatar_path'] . '/' . $avatar_file);
63 return ", user_avatar = '', user_avatar_type = " . USER_AVATAR_NONE;
66 function user_avatar_gallery($mode, &$error, &$error_msg, $avatar_filename, $avatar_category)
70 $avatar_filename = phpbb_ltrim(basename($avatar_filename), "'");
71 $avatar_category = phpbb_ltrim(basename($avatar_category), "'");
73 if(!preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $avatar_filename))
78 if ($avatar_filename == "" || $avatar_category == "")
83 if ( file_exists(@phpbb_realpath($board_config['avatar_gallery_path'] . '/' . $avatar_category . '/' . $avatar_filename)) && ($mode == 'editprofile') )
85 $return = ", user_avatar = '" . str_replace("\'", "''", $avatar_category . '/' . $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_GALLERY;
94 function user_avatar_url($mode, &$error, &$error_msg, $avatar_filename)
98 if ( !preg_match('#^(http)|(ftp):\/\/#i', $avatar_filename) )
100 $avatar_filename = 'http://' . $avatar_filename;
103 if ( !preg_match("#^((ht|f)tp://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png))$)#is", $avatar_filename) )
106 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Wrong_remote_avatar_format'] : $lang['Wrong_remote_avatar_format'];
110 return ( $mode == 'editprofile' ) ? ", user_avatar = '" . str_replace("\'", "''", $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_REMOTE : '';
114 function user_avatar_upload($mode, $avatar_mode, &$current_avatar, &$current_type, &$error, &$error_msg, $avatar_filename, $avatar_realname, $avatar_filesize, $avatar_filetype)
116 global $board_config, $db, $lang;
118 $ini_val = ( @phpversion() >= '4.0.0' ) ? 'ini_get' : 'get_cfg_var';
120 $width = $height = 0;
123 if ( $avatar_mode == 'remote' && preg_match('/^(http:\/\/)?([\w\-\.]+)\:?([0-9]*)\/(.*)$/', $avatar_filename, $url_ary) )
125 if ( empty($url_ary[4]) )
128 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Incomplete_URL'] : $lang['Incomplete_URL'];
132 $base_get = '/' . $url_ary[4];
133 $port = ( !empty($url_ary[3]) ) ? $url_ary[3] : 80;
135 if ( !($fsock = @fsockopen($url_ary[2], $port, $errno, $errstr)) )
138 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['No_connection_URL'] : $lang['No_connection_URL'];
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");
147 while( !@feof($fsock) )
149 $avatar_data .= @fread($fsock, $board_config['avatar_filesize']);
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))
156 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['File_no_data'] : $lang['File_no_data'];
160 $avatar_filesize = $file_data1[1];
161 $avatar_filetype = $file_data2[1];
163 if ( !$error && $avatar_filesize > 0 && $avatar_filesize < $board_config['avatar_filesize'] )
165 $avatar_data = substr($avatar_data, strlen($avatar_data) - $avatar_filesize, $avatar_filesize);
167 $tmp_path = ( !@$ini_val('safe_mode') ) ? '/tmp' : './' . $board_config['avatar_path'] . '/tmp';
168 $tmp_filename = tempnam($tmp_path, uniqid(rand()) . '-');
170 $fptr = @fopen($tmp_filename, 'wb');
171 $bytes_written = @fwrite($fptr, $avatar_data, $avatar_filesize);
174 if ( $bytes_written != $avatar_filesize )
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__);
180 list($width, $height, $type) = @getimagesize($tmp_filename);
184 $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
187 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
190 else if ( ( file_exists(@phpbb_realpath($avatar_filename)) ) && preg_match('/\.(jpg|jpeg|gif|png)$/i', $avatar_realname) )
192 if ( $avatar_filesize <= $board_config['avatar_filesize'] && $avatar_filesize > 0 )
194 preg_match('#image\/[x\-]*([a-z]+)#', $avatar_filetype, $avatar_filetype);
195 $avatar_filetype = $avatar_filetype[1];
199 $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
202 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
206 list($width, $height, $type) = @getimagesize($avatar_filename);
209 if ( !($imgtype = check_image_type($avatar_filetype, $error, $error_msg)) )
218 if ($imgtype != '.gif')
220 @unlink($tmp_filename);
221 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
225 // JPG, JPC, JP2, JPX, JB2
231 if ($imgtype != '.jpg' && $imgtype != '.jpeg')
233 @unlink($tmp_filename);
234 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
240 if ($imgtype != '.png')
242 @unlink($tmp_filename);
243 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
248 @unlink($tmp_filename);
249 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
252 if ( $width > 0 && $height > 0 && $width <= $board_config['avatar_max_width'] && $height <= $board_config['avatar_max_height'] )
254 $new_filename = uniqid(rand()) . $imgtype;
256 if ( $mode == 'editprofile' && $current_type == USER_AVATAR_UPLOAD && $current_avatar != '' )
258 user_avatar_delete($current_type, $current_avatar);
261 if( $avatar_mode == 'remote' )
263 @copy($tmp_filename, './' . $board_config['avatar_path'] . "/$new_filename");
264 @unlink($tmp_filename);
268 if ( @$ini_val('open_basedir') != '' )
270 if ( @phpversion() < '4.0.3' )
272 message_die(GENERAL_ERROR, 'open_basedir is set and your PHP version does not allow move_uploaded_file', '', __LINE__, __FILE__);
275 $move_file = 'move_uploaded_file';
282 if (!is_uploaded_file($avatar_filename))
284 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
286 $move_file($avatar_filename, './' . $board_config['avatar_path'] . "/$new_filename");
289 @chmod('./' . $board_config['avatar_path'] . "/$new_filename", 0777);
291 $avatar_sql = ( $mode == 'editprofile' ) ? ", user_avatar = '$new_filename', user_avatar_type = " . USER_AVATAR_UPLOAD : "'$new_filename', " . USER_AVATAR_UPLOAD;
295 $l_avatar_size = sprintf($lang['Avatar_imagesize'], $board_config['avatar_max_width'], $board_config['avatar_max_height']);
298 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
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)
306 global $board_config, $db, $template, $lang, $images, $theme;
307 global $phpbb_root_path, $phpEx;
309 $dir = @opendir($board_config['avatar_gallery_path']);
311 $avatar_images = array();
312 while( $file = @readdir($dir) )
314 if( $file != '.' && $file != '..' && !is_file($board_config['avatar_gallery_path'] . '/' . $file) && !is_link($board_config['avatar_gallery_path'] . '/' . $file) )
316 $sub_dir = @opendir($board_config['avatar_gallery_path'] . '/' . $file);
318 $avatar_row_count = 0;
319 $avatar_col_count = 0;
320 while( $sub_file = @readdir($sub_dir) )
322 if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $sub_file) )
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)));
328 if( $avatar_col_count == 5 )
331 $avatar_col_count = 0;
340 @ksort($avatar_images);
341 @reset($avatar_images);
343 if( empty($category) )
345 list($category, ) = each($avatar_images);
347 @reset($avatar_images);
349 $s_categories = '<select name="avatarcategory">';
350 while( list($key) = each($avatar_images) )
352 $selected = ( $key == $category ) ? ' selected="selected"' : '';
353 if( count($avatar_images[$key]) )
355 $s_categories .= '<option value="' . $key . '"' . $selected . '>' . ucfirst($key) . '</option>';
358 $s_categories .= '</select>';
361 for($i = 0; $i < count($avatar_images[$category]); $i++)
363 $template->assign_block_vars("avatar_row", array());
365 $s_colspan = max($s_colspan, count($avatar_images[$category][$i]));
367 for($j = 0; $j < count($avatar_images[$category][$i]); $j++)
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])
374 $template->assign_block_vars('avatar_row.avatar_option_column', array(
375 "S_OPTIONS_AVATAR" => $avatar_images[$category][$i][$j])
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');
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 . '" />';
384 for($i = 0; $i < count($params); $i++)
386 $s_hidden_vars .= '<input type="hidden" name="' . $params[$i] . '" value="' . str_replace('"', '"', $$params[$i]) . '" />';
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'],
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)