home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / unitednuke / unitednuke.exe / html / includes / usercp_avatar.php < prev    next >
PHP Script  |  2004-01-10  |  18KB  |  382 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.17 2003/03/04 21:02:36 acydburn Exp $
  10.  *
  11.  *
  12.  ***************************************************************************/
  13. /***************************************************************************
  14. * phpbb2 forums port version 2.0.5 (c) 2003 - Nuke Cops (http://nukecops.com)
  15. *
  16. * Ported by Nuke Cops to phpbb2 standalone 2.0.5 Test
  17. * and debugging completed by the Elite Nukers and site members.
  18. *
  19. * You run this package at your sole risk. Nuke Cops and affiliates cannot
  20. * be held liable if anything goes wrong. You are advised to test this
  21. * package on a development system. Backup everything before implementing
  22. * in a production environment. If something goes wrong, you can always
  23. * backout and restore your backups.
  24. *
  25. * Installing and running this also means you agree to the terms of the AUP
  26. * found at Nuke Cops.
  27. *
  28. * This is version 2.0.5 of the phpbb2 forum port for PHP-Nuke. Work is based
  29. * on Tom Nitzschner's forum port version 2.0.6. Tom's 2.0.6 port was based
  30. * on the phpbb2 standalone version 2.0.3. Our version 2.0.5 from Nuke Cops is
  31. * now reflecting phpbb2 standalone 2.0.5 that fixes some bugs and the
  32. * invalid_session error message.
  33. ***************************************************************************/
  34. /***************************************************************************
  35.  *   This file is part of the phpBB2 port to Nuke 6.0 (c) copyright 2002
  36.  *   by Tom Nitzschner (tom@toms-home.com)
  37.  *   http://bbtonuke.sourceforge.net (or http://www.toms-home.com)
  38.  *
  39.  *   As always, make a backup before messing with anything. All code
  40.  *   release by me is considered sample code only. It may be fully
  41.  *   functual, but you use it at your own risk, if you break it,
  42.  *   you get to fix it too. No waranty is given or implied.
  43.  *
  44.  *   Please post all questions/request about this port on http://bbtonuke.sourceforge.net first,
  45.  *   then on my site. All original header code and copyright messages will be maintained
  46.  *   to give credit where credit is due. If you modify this, the only requirement is
  47.  *   that you also maintain all original copyright messages. All my work is released
  48.  *   under the GNU GENERAL PUBLIC LICENSE. Please see the README for more information.
  49.  *
  50.  ***************************************************************************/
  51. /***************************************************************************
  52.  *
  53.  *   This program is free software; you can redistribute it and/or modify
  54.  *   it under the terms of the GNU General Public License as published by
  55.  *   the Free Software Foundation; either version 2 of the License, or
  56.  *   (at your option) any later version.
  57.  *
  58.  *
  59.  ***************************************************************************/
  60.  
  61. function check_image_type(&$type, &$error, &$error_msg)
  62. {
  63.         global $lang;
  64.  
  65.         switch( $type )
  66.         {
  67.                 case 'jpeg':
  68.                 case 'pjpeg':
  69.                 case 'jpg':
  70.                         return '.jpg';
  71.                         break;
  72.                 case 'gif':
  73.                         return '.gif';
  74.                         break;
  75.                 case 'png':
  76.                         return '.png';
  77.                         break;
  78.                 default:
  79.                         $error = true;
  80.                         $error_msg = (!empty($error_msg)) ? $error_msg . '<br />' . $lang['Avatar_filetype'] : $lang['Avatar_filetype'];
  81.                         break;
  82.         }
  83.  
  84.         return false;
  85. }
  86.  
  87. function user_avatar_delete($avatar_type, $avatar_file)
  88. {
  89.         global $board_config, $userdata;
  90.  
  91.         if ( $avatar_type == USER_AVATAR_UPLOAD && $avatar_file != '' )
  92.         {
  93.                 if ( @file_exists(@phpbb_realpath('./' . $board_config['avatar_path'] . '/' . $avatar_file)) )
  94.                 {
  95.                         @unlink('./' . $board_config['avatar_path'] . '/' . $avatar_file);
  96.                 }
  97.         }
  98.  
  99.         return ", user_avatar = '', user_avatar_type = " . USER_AVATAR_NONE;
  100. }
  101.  
  102. function user_avatar_gallery($mode, &$error, &$error_msg, $avatar_filename)
  103. {
  104.         global $board_config;
  105.         if ( file_exists(@phpbb_realpath($board_config['avatar_gallery_path'] . '/' . $avatar_filename)) && ($mode == 'editprofile') )
  106.         {
  107.                 $return = ", user_avatar = '" . str_replace("\'", "''", $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_GALLERY;
  108.         }
  109.         else
  110.         {
  111.                 $return = '';
  112.         }
  113.         return $return;
  114. }
  115.  
  116. function user_avatar_url($mode, &$error, &$error_msg, $avatar_filename)
  117. {
  118.         if ( !preg_match('#^(http)|(ftp):\/\/#i', $avatar_filename) )
  119.         {
  120.                 $avatar_filename = 'http://' . $avatar_filename;
  121.         }
  122.  
  123.         if ( !preg_match('#^((http)|(ftp):\/\/[\w\-]+?\.([\w\-]+\.)+[\w]+(:[0-9]+)*\/.*?\.(gif|jpg|jpeg|png)$)#is', $avatar_filename) )
  124.         {
  125.                 $error = true;
  126.                 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Wrong_remote_avatar_format'] : $lang['Wrong_remote_avatar_format'];
  127.                 return;
  128.         }
  129.  
  130.         return ( $mode == 'editprofile' ) ? ", user_avatar = '" . str_replace("\'", "''", $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_REMOTE : '';
  131.  
  132. }
  133.  
  134. function user_avatar_upload($mode, $avatar_mode, &$current_avatar, &$current_type, &$error, &$error_msg, $avatar_filename, $avatar_realname, $avatar_filesize, $avatar_filetype)
  135. {
  136.         global $board_config, $db, $lang;
  137.  
  138.         $ini_val = ( @phpversion() >= '4.0.0' ) ? 'ini_get' : 'get_cfg_var';
  139.  
  140.         if ( $avatar_mode == 'remote' && preg_match('/^(http:\/\/)?([\w\-\.]+)\:?([0-9]*)\/(.*)$/', $avatar_filename, $url_ary) )
  141.         {
  142.                 if ( empty($url_ary[4]) )
  143.                 {
  144.                         $error = true;
  145.                         $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Incomplete_URL'] : $lang['Incomplete_URL'];
  146.                         return;
  147.                 }
  148.  
  149.                 $base_get = '/' . $url_ary[4];
  150.                 $port = ( !empty($url_ary[3]) ) ? $url_ary[3] : 80;
  151.  
  152.                 if ( !($fsock = @fsockopen($url_ary[2], $port, $errno, $errstr)) )
  153.                 {
  154.                         $error = true;
  155.                         $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['No_connection_URL'] : $lang['No_connection_URL'];
  156.                         return;
  157.                 }
  158.  
  159.                 @fputs($fsock, "GET $base_get HTTP/1.1\r\n");
  160.                 @fputs($fsock, "HOST: " . $url_ary[2] . "\r\n");
  161.                 @fputs($fsock, "Connection: close\r\n\r\n");
  162.  
  163.                 unset($avatar_data);
  164.                 while( !@feof($fsock) )
  165.                 {
  166.                         $avatar_data .= @fread($fsock, $board_config['avatar_filesize']);
  167.                 }
  168.                 @fclose($fsock);
  169.  
  170.                 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))
  171.                 {
  172.                         $error = true;
  173.                         $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['File_no_data'] : $lang['File_no_data'];
  174.                         return;
  175.                 }
  176.  
  177.                 $avatar_filesize = $file_data1[1];
  178.                 $avatar_filetype = $file_data2[1];
  179.  
  180.                 if ( !$error && $avatar_filesize > 0 && $avatar_filesize < $board_config['avatar_filesize'] )
  181.                 {
  182.                         $avatar_data = substr($avatar_data, strlen($avatar_data) - $avatar_filesize, $avatar_filesize);
  183.  
  184.                         $tmp_path = ( !@$ini_val('safe_mode') ) ? '/tmp' : './' . $board_config['avatar_path'] . '/tmp';
  185.                         $tmp_filename = tempnam($tmp_path, uniqid(rand()) . '-');
  186.  
  187.                         $fptr = @fopen($tmp_filename, 'wb');
  188.                         $bytes_written = @fwrite($fptr, $avatar_data, $avatar_filesize);
  189.                         @fclose($fptr);
  190.  
  191.                         if ( $bytes_written != $avatar_filesize )
  192.                         {
  193.                                 @unlink($tmp_filename);
  194.                                 message_die(GENERAL_ERROR, 'Could not write avatar file to local storage. Please contact the board administrator with this message', '', __LINE__, __FILE__);
  195.                         }
  196.  
  197.                         list($width, $height) = @getimagesize($tmp_filename);
  198.                 }
  199.                 else
  200.                 {
  201.                         $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
  202.  
  203.                         $error = true;
  204.                         $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
  205.                 }
  206.         }
  207.         else if ( ( file_exists(@phpbb_realpath($avatar_filename)) ) && preg_match('/\.(jpg|jpeg|gif|png)$/i', $avatar_realname) )
  208.         {
  209.                 if ( $avatar_filesize <= $board_config['avatar_filesize'] && $avatar_filesize > 0 )
  210.                 {
  211.                         preg_match('#image\/[x\-]*([a-z]+)#', $avatar_filetype, $avatar_filetype);
  212.                         $avatar_filetype = $avatar_filetype[1];
  213.                 }
  214.                 else
  215.                 {
  216.                         $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
  217.  
  218.                         $error = true;
  219.                         $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
  220.                         return;
  221.                 }
  222.  
  223.                 list($width, $height) = @getimagesize($avatar_filename);
  224.         }
  225.  
  226.         if ( !($imgtype = check_image_type($avatar_filetype, $error, $error_msg)) )
  227.         {
  228.                 return;
  229.         }
  230.  
  231.         if ( $width <= $board_config['avatar_max_width'] && $height <= $board_config['avatar_max_height'] )
  232.         {
  233.                 $new_filename = uniqid(rand()) . $imgtype;
  234.  
  235.                 if ( $mode == 'editprofile' && $current_type == USER_AVATAR_UPLOAD && $current_avatar != '' )
  236.                 {
  237.                         if ( file_exists(@phpbb_realpath('./' . $board_config['avatar_path'] . '/' . $current_avatar)) )
  238.                         {
  239.                                 @unlink('./' . $board_config['avatar_path'] . '/' . $current_avatar);
  240.                         }
  241.                 }
  242.  
  243.                 if( $avatar_mode == 'remote' )
  244.                 {
  245.                         @copy($tmp_filename, './' . $board_config['avatar_path'] . "/$new_filename");
  246.                         @unlink($tmp_filename);
  247.                 }
  248.                 else
  249.                 {
  250.                         if ( @$ini_val('open_basedir') != '' )
  251.                         {
  252.                                 if ( @phpversion() < '4.0.3' )
  253.                                 {
  254.                                         message_die(GENERAL_ERROR, 'open_basedir is set and your PHP version does not allow move_uploaded_file', '', __LINE__, __FILE__);
  255.                                 }
  256.  
  257.                                 $move_file = 'move_uploaded_file';
  258.                         }
  259.                         else
  260.                         {
  261.                                 $move_file = 'copy';
  262.                         }
  263.  
  264.                         $move_file($avatar_filename, './' . $board_config['avatar_path'] . "/$new_filename");
  265.                 }
  266.  
  267.                 @chmod('./' . $board_config['avatar_path'] . "/$new_filename", 0777);
  268.  
  269.                 $avatar_sql = ( $mode == 'editprofile' ) ? ", user_avatar = '$new_filename', user_avatar_type = " . USER_AVATAR_UPLOAD : "'$new_filename', " . USER_AVATAR_UPLOAD;
  270.         }
  271.         else
  272.         {
  273.                 $l_avatar_size = sprintf($lang['Avatar_imagesize'], $board_config['avatar_max_width'], $board_config['avatar_max_height']);
  274.  
  275.                 $error = true;
  276.                 $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
  277.         }
  278.  
  279.         return $avatar_sql;
  280. }
  281.  
  282. 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)
  283. {
  284.         global $board_config, $db, $template, $lang, $images, $theme;
  285.         global $phpbb_root_path, $phpEx;
  286.  
  287.         $dir = @opendir($board_config['avatar_gallery_path']);
  288.  
  289.         $avatar_images = array();
  290.         while( $file = @readdir($dir) )
  291.         {
  292.                 if( $file != '.' && $file != '..' && !is_file($board_config['avatar_gallery_path'] . '/' . $file) && !is_link($board_config['avatar_gallery_path'] . '/' . $file) )
  293.                 {
  294.                         $sub_dir = @opendir($board_config['avatar_gallery_path'] . '/' . $file);
  295.  
  296.                         $avatar_row_count = 0;
  297.                         $avatar_col_count = 0;
  298.                         while( $sub_file = @readdir($sub_dir) )
  299.                         {
  300.                                 if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $sub_file) )
  301.                                 {
  302.                                         $avatar_images[$file][$avatar_row_count][$avatar_col_count] = $file . '/' . $sub_file;
  303.                                         $avatar_name[$file][$avatar_row_count][$avatar_col_count] = ucfirst(str_replace("_", " ", preg_replace('/^(.*)\..*$/', '\1', $sub_file)));
  304.  
  305.                                         $avatar_col_count++;
  306.                                         if( $avatar_col_count == 5 )
  307.                                         {
  308.                                                 $avatar_row_count++;
  309.                                                 $avatar_col_count = 0;
  310.                                         }
  311.                                 }
  312.                         }
  313.                 }
  314.         }
  315.  
  316.         @closedir($dir);
  317.  
  318.         @ksort($avatar_images);
  319.         @reset($avatar_images);
  320.  
  321.         if( empty($category) )
  322.         {
  323.                 list($category, ) = each($avatar_images);
  324.         }
  325.         @reset($avatar_images);
  326.  
  327.         $s_categories = '<select name="avatarcategory">';
  328.         while( list($key) = each($avatar_images) )
  329.         {
  330.                 $selected = ( $key == $category ) ? ' selected="selected"' : '';
  331.                 if( count($avatar_images[$key]) )
  332.                 {
  333.                         $s_categories .= '<option value="' . $key . '"' . $selected . '>' . ucfirst($key) . '</option>';
  334.                 }
  335.         }
  336.         $s_categories .= '</select>';
  337.  
  338.         $s_colspan = 0;
  339.         for($i = 0; $i < count($avatar_images[$category]); $i++)
  340.         {
  341.                 $template->assign_block_vars("avatar_row", array());
  342.  
  343.                 $s_colspan = max($s_colspan, count($avatar_images[$category][$i]));
  344.  
  345.                 for($j = 0; $j < count($avatar_images[$category][$i]); $j++)
  346.                 {
  347.                         $template->assign_block_vars('avatar_row.avatar_column', array(
  348.                                 "AVATAR_IMAGE" => $board_config['avatar_gallery_path'] . '/' . $avatar_images[$category][$i][$j],
  349.                                 "AVATAR_NAME" => $avatar_name[$category][$i][$j])
  350.                         );
  351.  
  352.                         $template->assign_block_vars('avatar_row.avatar_option_column', array(
  353.                                 "S_OPTIONS_AVATAR" => $avatar_images[$category][$i][$j])
  354.                         );
  355.                 }
  356.         }
  357.  
  358.         $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');
  359.  
  360.         $s_hidden_vars = '<input type="hidden" name="sid" value="' . $session_id . '" /><input type="hidden" name="agreed" value="true" />';
  361.  
  362.         for($i = 0; $i < count($params); $i++)
  363.         {
  364.                 $s_hidden_vars .= '<input type="hidden" name="' . $params[$i] . '" value="' . str_replace('"', '"', $$params[$i]) . '" />';
  365.         }
  366.  
  367.         $template->assign_vars(array(
  368.                 'L_AVATAR_GALLERY' => $lang['Avatar_gallery'],
  369.                 'L_SELECT_AVATAR' => $lang['Select_avatar'],
  370.                 'L_RETURN_PROFILE' => $lang['Return_profile'],
  371.                 'L_CATEGORY' => $lang['Select_category'],
  372.  
  373.                 'S_CATEGORY_SELECT' => $s_categories,
  374.                 'S_COLSPAN' => $s_colspan,
  375.                 'S_PROFILE_ACTION' => append_sid("profile.$phpEx?mode=$mode"),
  376.                 'S_HIDDEN_FIELDS' => $s_hidden_vars)
  377.         );
  378.  
  379.         return;
  380. }
  381.  
  382. ?>