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.24 2006/05/23 21:09:27 grahamje 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 $avatar_filename = substr($avatar_filename, 0, 100);
105 if ( !preg_match("#^((ht|f)tp://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png))$)#is", $avatar_filename) )
108 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Wrong_remote_avatar_format'] : $lang['Wrong_remote_avatar_format'];
112 return ( $mode == 'editprofile' ) ? ", user_avatar = '" . str_replace("\'", "''", $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_REMOTE : '';
116 function user_avatar_upload($mode, $avatar_mode, &$current_avatar, &$current_type, &$error, &$error_msg, $avatar_filename, $avatar_realname, $avatar_filesize, $avatar_filetype)
118 global $board_config, $db, $lang;
120 $ini_val = ( @phpversion() >= '4.0.0' ) ? 'ini_get' : 'get_cfg_var';
122 $width = $height = 0;
125 if ( $avatar_mode == 'remote' && preg_match('/^(http:\/\/)?([\w\-\.]+)\:?([0-9]*)\/([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png)))$/', $avatar_filename, $url_ary) )
127 if ( empty($url_ary[4]) )
130 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Incomplete_URL'] : $lang['Incomplete_URL'];
134 $base_get = '/' . $url_ary[4];
135 $port = ( !empty($url_ary[3]) ) ? $url_ary[3] : 80;
137 if ( !($fsock = @fsockopen($url_ary[2], $port, $errno, $errstr)) )
140 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['No_connection_URL'] : $lang['No_connection_URL'];
144 @fputs($fsock, "GET $base_get HTTP/1.1\r\n");
145 @fputs($fsock, "HOST: " . $url_ary[2] . "\r\n");
146 @fputs($fsock, "Connection: close\r\n\r\n");
149 while( !@feof($fsock) )
151 $avatar_data .= @fread($fsock, $board_config['avatar_filesize']);
155 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))
158 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['File_no_data'] : $lang['File_no_data'];
162 $avatar_filesize = $file_data1[1];
163 $avatar_filetype = $file_data2[1];
165 if ( !$error && $avatar_filesize > 0 && $avatar_filesize < $board_config['avatar_filesize'] )
167 $avatar_data = substr($avatar_data, strlen($avatar_data) - $avatar_filesize, $avatar_filesize);
169 $tmp_path = ( !@$ini_val('safe_mode') ) ? '/tmp' : './' . $board_config['avatar_path'] . '/tmp';
170 $tmp_filename = tempnam($tmp_path, uniqid(rand()) . '-');
172 $fptr = @fopen($tmp_filename, 'wb');
173 $bytes_written = @fwrite($fptr, $avatar_data, $avatar_filesize);
176 if ( $bytes_written != $avatar_filesize )
178 @unlink($tmp_filename);
179 message_die(GENERAL_ERROR, 'Could not write avatar file to local storage. Please contact the board administrator with this message', '', __LINE__, __FILE__);
182 list($width, $height, $type) = @getimagesize($tmp_filename);
186 $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
189 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
192 else if ( ( file_exists(@phpbb_realpath($avatar_filename)) ) && preg_match('/\.(jpg|jpeg|gif|png)$/i', $avatar_realname) )
194 if ( $avatar_filesize <= $board_config['avatar_filesize'] && $avatar_filesize > 0 )
196 preg_match('#image\/[x\-]*([a-z]+)#', $avatar_filetype, $avatar_filetype);
197 $avatar_filetype = $avatar_filetype[1];
201 $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
204 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
208 list($width, $height, $type) = @getimagesize($avatar_filename);
211 if ( !($imgtype = check_image_type($avatar_filetype, $error, $error_msg)) )
220 if ($imgtype != '.gif')
222 @unlink($tmp_filename);
223 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
227 // JPG, JPC, JP2, JPX, JB2
233 if ($imgtype != '.jpg' && $imgtype != '.jpeg')
235 @unlink($tmp_filename);
236 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
242 if ($imgtype != '.png')
244 @unlink($tmp_filename);
245 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
250 @unlink($tmp_filename);
251 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
254 if ( $width > 0 && $height > 0 && $width <= $board_config['avatar_max_width'] && $height <= $board_config['avatar_max_height'] )
256 $new_filename = uniqid(rand()) . $imgtype;
258 if ( $mode == 'editprofile' && $current_type == USER_AVATAR_UPLOAD && $current_avatar != '' )
260 user_avatar_delete($current_type, $current_avatar);
263 if( $avatar_mode == 'remote' )
265 @copy($tmp_filename, './' . $board_config['avatar_path'] . "/$new_filename");
266 @unlink($tmp_filename);
270 if ( @$ini_val('open_basedir') != '' )
272 if ( @phpversion() < '4.0.3' )
274 message_die(GENERAL_ERROR, 'open_basedir is set and your PHP version does not allow move_uploaded_file', '', __LINE__, __FILE__);
277 $move_file = 'move_uploaded_file';
284 if (!is_uploaded_file($avatar_filename))
286 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
288 $move_file($avatar_filename, './' . $board_config['avatar_path'] . "/$new_filename");
291 @chmod('./' . $board_config['avatar_path'] . "/$new_filename", 0777);
293 $avatar_sql = ( $mode == 'editprofile' ) ? ", user_avatar = '$new_filename', user_avatar_type = " . USER_AVATAR_UPLOAD : "'$new_filename', " . USER_AVATAR_UPLOAD;
297 $l_avatar_size = sprintf($lang['Avatar_imagesize'], $board_config['avatar_max_width'], $board_config['avatar_max_height']);
300 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
306 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)
308 global $board_config, $db, $template, $lang, $images, $theme;
309 global $phpbb_root_path, $phpEx;
311 $dir = @opendir($board_config['avatar_gallery_path']);
313 $avatar_images = array();
314 while( $file = @readdir($dir) )
316 if( $file != '.' && $file != '..' && !is_file($board_config['avatar_gallery_path'] . '/' . $file) && !is_link($board_config['avatar_gallery_path'] . '/' . $file) )
318 $sub_dir = @opendir($board_config['avatar_gallery_path'] . '/' . $file);
320 $avatar_row_count = 0;
321 $avatar_col_count = 0;
322 while( $sub_file = @readdir($sub_dir) )
324 if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $sub_file) )
326 $avatar_images[$file][$avatar_row_count][$avatar_col_count] = $sub_file;
327 $avatar_name[$file][$avatar_row_count][$avatar_col_count] = ucfirst(str_replace("_", " ", preg_replace('/^(.*)\..*$/', '\1', $sub_file)));
330 if( $avatar_col_count == 5 )
333 $avatar_col_count = 0;
342 @ksort($avatar_images);
343 @reset($avatar_images);
345 if( empty($category) )
347 list($category, ) = each($avatar_images);
349 @reset($avatar_images);
351 $s_categories = '<select name="avatarcategory">';
352 while( list($key) = each($avatar_images) )
354 $selected = ( $key == $category ) ? ' selected="selected"' : '';
355 if( count($avatar_images[$key]) )
357 $s_categories .= '<option value="' . $key . '"' . $selected . '>' . ucfirst($key) . '</option>';
360 $s_categories .= '</select>';
363 for($i = 0; $i < count($avatar_images[$category]); $i++)
365 $template->assign_block_vars("avatar_row", array());
367 $s_colspan = max($s_colspan, count($avatar_images[$category][$i]));
369 for($j = 0; $j < count($avatar_images[$category][$i]); $j++)
371 $template->assign_block_vars('avatar_row.avatar_column', array(
372 "AVATAR_IMAGE" => $board_config['avatar_gallery_path'] . '/' . $category . '/' . $avatar_images[$category][$i][$j],
373 "AVATAR_NAME" => $avatar_name[$category][$i][$j])
376 $template->assign_block_vars('avatar_row.avatar_option_column', array(
377 "S_OPTIONS_AVATAR" => $avatar_images[$category][$i][$j])
382 $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');
384 $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 . '" />';
386 for($i = 0; $i < count($params); $i++)
388 $s_hidden_vars .= '<input type="hidden" name="' . $params[$i] . '" value="' . str_replace('"', '"', $$params[$i]) . '" />';
391 $template->assign_vars(array(
392 'L_AVATAR_GALLERY' => $lang['Avatar_gallery'],
393 'L_SELECT_AVATAR' => $lang['Select_avatar'],
394 'L_RETURN_PROFILE' => $lang['Return_profile'],
395 'L_CATEGORY' => $lang['Select_category'],
397 'S_CATEGORY_SELECT' => $s_categories,
398 'S_COLSPAN' => $s_colspan,
399 'S_PROFILE_ACTION' => append_sid("profile.$phpEx?mode=$mode"),
400 'S_HIDDEN_FIELDS' => $s_hidden_vars)