home *** CD-ROM | disk | FTP | other *** search
/ PC Advisor 2006 January / PCA126_DVD.iso / ADVISORS / phpBB-2.0.17 / phpBB2 / includes / usercp_avatar.php < prev    next >
Encoding:
PHP Script  |  2005-07-19  |  11.6 KB  |  359 lines

  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.21 2005/07/19 20:01:16 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)
  67. {
  68.     global $board_config;
  69.  
  70.     $avatar_filename = str_replace(array('../', '..\\', './', '.\\'), '', $avatar_filename);
  71.     if ($avatar_filename{0} == '/' || $avatar_filename{0} == "\\")
  72.     {
  73.         return '';
  74.     }
  75.  
  76.     if ( file_exists(@phpbb_realpath($board_config['avatar_gallery_path'] . '/' . $avatar_filename)) && ($mode == 'editprofile') )
  77.     {
  78.         $return = ", user_avatar = '" . str_replace("\'", "''", $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_GALLERY;
  79.     }
  80.     else
  81.     {
  82.         $return = '';
  83.     }
  84.     return $return;
  85. }
  86.  
  87. function user_avatar_url($mode, &$error, &$error_msg, $avatar_filename)
  88. {
  89.     global $lang;
  90.  
  91.     if ( !preg_match('#^(http)|(ftp):\/\/#i', $avatar_filename) )
  92.     {
  93.         $avatar_filename = 'http://' . $avatar_filename;
  94.     }
  95.  
  96.     if ( !preg_match("#^((ht|f)tp://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png))$)#is", $avatar_filename) )
  97.     {
  98.         $error = true;
  99.         $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Wrong_remote_avatar_format'] : $lang['Wrong_remote_avatar_format'];
  100.         return;
  101.     }
  102.  
  103.     return ( $mode == 'editprofile' ) ? ", user_avatar = '" . str_replace("\'", "''", $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_REMOTE : '';
  104.  
  105. }
  106.  
  107. function user_avatar_upload($mode, $avatar_mode, &$current_avatar, &$current_type, &$error, &$error_msg, $avatar_filename, $avatar_realname, $avatar_filesize, $avatar_filetype)
  108. {
  109.     global $board_config, $db, $lang;
  110.  
  111.     $ini_val = ( @phpversion() >= '4.0.0' ) ? 'ini_get' : 'get_cfg_var';
  112.  
  113.     if ( $avatar_mode == 'remote' && preg_match('/^(http:\/\/)?([\w\-\.]+)\:?([0-9]*)\/(.*)$/', $avatar_filename, $url_ary) )
  114.     {
  115.         if ( empty($url_ary[4]) )
  116.         {
  117.             $error = true;
  118.             $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Incomplete_URL'] : $lang['Incomplete_URL'];
  119.             return;
  120.         }
  121.  
  122.         $base_get = '/' . $url_ary[4];
  123.         $port = ( !empty($url_ary[3]) ) ? $url_ary[3] : 80;
  124.  
  125.         if ( !($fsock = @fsockopen($url_ary[2], $port, $errno, $errstr)) )
  126.         {
  127.             $error = true;
  128.             $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['No_connection_URL'] : $lang['No_connection_URL'];
  129.             return;
  130.         }
  131.  
  132.         @fputs($fsock, "GET $base_get HTTP/1.1\r\n");
  133.         @fputs($fsock, "HOST: " . $url_ary[2] . "\r\n");
  134.         @fputs($fsock, "Connection: close\r\n\r\n");
  135.  
  136.         unset($avatar_data);
  137.         while( !@feof($fsock) )
  138.         {
  139.             $avatar_data .= @fread($fsock, $board_config['avatar_filesize']);
  140.         }
  141.         @fclose($fsock);
  142.  
  143.         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))
  144.         {
  145.             $error = true;
  146.             $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['File_no_data'] : $lang['File_no_data'];
  147.             return;
  148.         }
  149.  
  150.         $avatar_filesize = $file_data1[1]; 
  151.         $avatar_filetype = $file_data2[1]; 
  152.  
  153.         if ( !$error && $avatar_filesize > 0 && $avatar_filesize < $board_config['avatar_filesize'] )
  154.         {
  155.             $avatar_data = substr($avatar_data, strlen($avatar_data) - $avatar_filesize, $avatar_filesize);
  156.  
  157.             $tmp_path = ( !@$ini_val('safe_mode') ) ? '/tmp' : './' . $board_config['avatar_path'] . '/tmp';
  158.             $tmp_filename = tempnam($tmp_path, uniqid(rand()) . '-');
  159.  
  160.             $fptr = @fopen($tmp_filename, 'wb');
  161.             $bytes_written = @fwrite($fptr, $avatar_data, $avatar_filesize);
  162.             @fclose($fptr);
  163.  
  164.             if ( $bytes_written != $avatar_filesize )
  165.             {
  166.                 @unlink($tmp_filename);
  167.                 message_die(GENERAL_ERROR, 'Could not write avatar file to local storage. Please contact the board administrator with this message', '', __LINE__, __FILE__);
  168.             }
  169.  
  170.             list($width, $height) = @getimagesize($tmp_filename);
  171.         }
  172.         else
  173.         {
  174.             $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
  175.  
  176.             $error = true;
  177.             $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
  178.         }
  179.     }
  180.     else if ( ( file_exists(@phpbb_realpath($avatar_filename)) ) && preg_match('/\.(jpg|jpeg|gif|png)$/i', $avatar_realname) )
  181.     {
  182.         if ( $avatar_filesize <= $board_config['avatar_filesize'] && $avatar_filesize > 0 )
  183.         {
  184.             preg_match('#image\/[x\-]*([a-z]+)#', $avatar_filetype, $avatar_filetype);
  185.             $avatar_filetype = $avatar_filetype[1];
  186.         }
  187.         else
  188.         {
  189.             $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
  190.  
  191.             $error = true;
  192.             $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
  193.             return;
  194.         }
  195.  
  196.         list($width, $height) = @getimagesize($avatar_filename);
  197.     }
  198.  
  199.     if ( !($imgtype = check_image_type($avatar_filetype, $error, $error_msg)) )
  200.     {
  201.         return;
  202.     }
  203.  
  204.     if ( $width > 0 && $height > 0 && $width <= $board_config['avatar_max_width'] && $height <= $board_config['avatar_max_height'] )
  205.     {
  206.         $new_filename = uniqid(rand()) . $imgtype;
  207.  
  208.         if ( $mode == 'editprofile' && $current_type == USER_AVATAR_UPLOAD && $current_avatar != '' )
  209.         {
  210.             if ( file_exists(@phpbb_realpath('./' . $board_config['avatar_path'] . '/' . $current_avatar)) )
  211.             {
  212.                 @unlink('./' . $board_config['avatar_path'] . '/' . $current_avatar);
  213.             }
  214.         }
  215.  
  216.         if( $avatar_mode == 'remote' )
  217.         {
  218.             @copy($tmp_filename, './' . $board_config['avatar_path'] . "/$new_filename");
  219.             @unlink($tmp_filename);
  220.         }
  221.         else
  222.         {
  223.             if ( @$ini_val('open_basedir') != '' )
  224.             {
  225.                 if ( @phpversion() < '4.0.3' )
  226.                 {
  227.                     message_die(GENERAL_ERROR, 'open_basedir is set and your PHP version does not allow move_uploaded_file', '', __LINE__, __FILE__);
  228.                 }
  229.  
  230.                 $move_file = 'move_uploaded_file';
  231.             }
  232.             else
  233.             {
  234.                 $move_file = 'copy';
  235.             }
  236.  
  237.             if (!is_uploaded_file($avatar_filename))
  238.             {
  239.                 message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
  240.             }
  241.             $move_file($avatar_filename, './' . $board_config['avatar_path'] . "/$new_filename");
  242.         }
  243.  
  244.         @chmod('./' . $board_config['avatar_path'] . "/$new_filename", 0777);
  245.  
  246.         $avatar_sql = ( $mode == 'editprofile' ) ? ", user_avatar = '$new_filename', user_avatar_type = " . USER_AVATAR_UPLOAD : "'$new_filename', " . USER_AVATAR_UPLOAD;
  247.     }
  248.     else
  249.     {
  250.         $l_avatar_size = sprintf($lang['Avatar_imagesize'], $board_config['avatar_max_width'], $board_config['avatar_max_height']);
  251.  
  252.         $error = true;
  253.         $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
  254.     }
  255.  
  256.     return $avatar_sql;
  257. }
  258.  
  259. 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)
  260. {
  261.     global $board_config, $db, $template, $lang, $images, $theme;
  262.     global $phpbb_root_path, $phpEx;
  263.  
  264.     $dir = @opendir($board_config['avatar_gallery_path']);
  265.  
  266.     $avatar_images = array();
  267.     while( $file = @readdir($dir) )
  268.     {
  269.         if( $file != '.' && $file != '..' && !is_file($board_config['avatar_gallery_path'] . '/' . $file) && !is_link($board_config['avatar_gallery_path'] . '/' . $file) )
  270.         {
  271.             $sub_dir = @opendir($board_config['avatar_gallery_path'] . '/' . $file);
  272.  
  273.             $avatar_row_count = 0;
  274.             $avatar_col_count = 0;
  275.             while( $sub_file = @readdir($sub_dir) )
  276.             {
  277.                 if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $sub_file) )
  278.                 {
  279.                     $avatar_images[$file][$avatar_row_count][$avatar_col_count] = $file . '/' . $sub_file; 
  280.                     $avatar_name[$file][$avatar_row_count][$avatar_col_count] = ucfirst(str_replace("_", " ", preg_replace('/^(.*)\..*$/', '\1', $sub_file)));
  281.  
  282.                     $avatar_col_count++;
  283.                     if( $avatar_col_count == 5 )
  284.                     {
  285.                         $avatar_row_count++;
  286.                         $avatar_col_count = 0;
  287.                     }
  288.                 }
  289.             }
  290.         }
  291.     }
  292.  
  293.     @closedir($dir);
  294.  
  295.     @ksort($avatar_images);
  296.     @reset($avatar_images);
  297.  
  298.     if( empty($category) )
  299.     {
  300.         list($category, ) = each($avatar_images);
  301.     }
  302.     @reset($avatar_images);
  303.  
  304.     $s_categories = '<select name="avatarcategory">';
  305.     while( list($key) = each($avatar_images) )
  306.     {
  307.         $selected = ( $key == $category ) ? ' selected="selected"' : '';
  308.         if( count($avatar_images[$key]) )
  309.         {
  310.             $s_categories .= '<option value="' . $key . '"' . $selected . '>' . ucfirst($key) . '</option>';
  311.         }
  312.     }
  313.     $s_categories .= '</select>';
  314.  
  315.     $s_colspan = 0;
  316.     for($i = 0; $i < count($avatar_images[$category]); $i++)
  317.     {
  318.         $template->assign_block_vars("avatar_row", array());
  319.  
  320.         $s_colspan = max($s_colspan, count($avatar_images[$category][$i]));
  321.  
  322.         for($j = 0; $j < count($avatar_images[$category][$i]); $j++)
  323.         {
  324.             $template->assign_block_vars('avatar_row.avatar_column', array(
  325.                 "AVATAR_IMAGE" => $board_config['avatar_gallery_path'] . '/' . $avatar_images[$category][$i][$j], 
  326.                 "AVATAR_NAME" => $avatar_name[$category][$i][$j])
  327.             );
  328.  
  329.             $template->assign_block_vars('avatar_row.avatar_option_column', array(
  330.                 "S_OPTIONS_AVATAR" => $avatar_images[$category][$i][$j])
  331.             );
  332.         }
  333.     }
  334.  
  335.     $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');
  336.  
  337.     $s_hidden_vars = '<input type="hidden" name="sid" value="' . $session_id . '" /><input type="hidden" name="agreed" value="true" />';
  338.  
  339.     for($i = 0; $i < count($params); $i++)
  340.     {
  341.         $s_hidden_vars .= '<input type="hidden" name="' . $params[$i] . '" value="' . str_replace('"', '"', $$params[$i]) . '" />';
  342.     }
  343.     
  344.     $template->assign_vars(array(
  345.         'L_AVATAR_GALLERY' => $lang['Avatar_gallery'], 
  346.         'L_SELECT_AVATAR' => $lang['Select_avatar'], 
  347.         'L_RETURN_PROFILE' => $lang['Return_profile'], 
  348.         'L_CATEGORY' => $lang['Select_category'], 
  349.  
  350.         'S_CATEGORY_SELECT' => $s_categories, 
  351.         'S_COLSPAN' => $s_colspan, 
  352.         'S_PROFILE_ACTION' => append_sid("profile.$phpEx?mode=$mode"), 
  353.         'S_HIDDEN_FIELDS' => $s_hidden_vars)
  354.     );
  355.  
  356.     return;
  357. }
  358.  
  359. ?>