plugins/SpecialUserPrefs.php
changeset 1086 6a59951b70e4
parent 1083 ef2dbcac5d56
child 1095 5f623b0de18e
equal deleted inserted replaced
1085:3343a05e7e5b 1086:6a59951b70e4
   182   
   182   
   183   $errors = '';
   183   $errors = '';
   184   
   184   
   185   switch ( $section )
   185   switch ( $section )
   186   {
   186   {
       
   187     case 'Avatar':
       
   188       $template->preload_js('jquery');
       
   189       $template->preload_js('jquery-ui');
       
   190       break;
   187     case 'EmailPassword':
   191     case 'EmailPassword':
   188       // Require elevated privileges (well sortof)
   192       // Require elevated privileges (well sortof)
   189       if ( $session->auth_level < USER_LEVEL_CHPREF )
   193       if ( $session->auth_level < USER_LEVEL_CHPREF )
   190       {
   194       {
   191         redirect(makeUrlNS('Special', 'Login/' . $paths->fullpage, 'level=' . USER_LEVEL_CHPREF, true), 'Authentication required', 'You need to re-authenticate to access this page.', 0);
   195         redirect(makeUrlNS('Special', 'Login/' . $paths->fullpage, 'level=' . USER_LEVEL_CHPREF, true), 'Authentication required', 'You need to re-authenticate to access this page.', 0);
   791       {
   795       {
   792         echo '<div class="error-box"><b>' . $lang->get('usercp_avatar_err_disabled_title') . '</b><br />' . $lang->get('usercp_avatar_err_disabled_body') . '</div>';
   796         echo '<div class="error-box"><b>' . $lang->get('usercp_avatar_err_disabled_title') . '</b><br />' . $lang->get('usercp_avatar_err_disabled_body') . '</div>';
   793         break;
   797         break;
   794       }
   798       }
   795       
   799       
   796       // Determine current avatar
       
   797       $q = $db->sql_query('SELECT user_has_avatar, avatar_type FROM ' . table_prefix . 'users WHERE user_id = ' . $session->user_id . ';');
       
   798       if ( !$q )
       
   799         $db->_die('Avatar CP selecting user\'s avatar data');
       
   800       
       
   801       list($has_avi, $avi_type) = $db->fetchrow_num();
       
   802       
       
   803       if ( isset($_POST['submit']) )
   800       if ( isset($_POST['submit']) )
   804       {
   801       {
   805         $action = ( isset($_POST['avatar_action']) ) ? $_POST['avatar_action'] : 'keep';
   802         list($has_avi, $avi_type) = avatar_post($session->user_id);
   806         $avi_path = ENANO_ROOT . '/' . getConfig('avatar_directory') . '/' . $session->user_id . '.' . $avi_type;
   803       }
   807         switch($action)
   804       else
   808         {
   805       {
   809           case 'keep':
   806         // Determine current avatar
   810           default:
   807         $q = $db->sql_query('SELECT user_has_avatar, avatar_type FROM ' . table_prefix . 'users WHERE user_id = ' . $session->user_id . ';');
   811             break;
   808         if ( !$q )
   812           case 'remove':
   809           $db->_die('Avatar CP selecting user\'s avatar data');
   813             if ( $has_avi )
   810         
   814             {
   811         list($has_avi, $avi_type) = $db->fetchrow_num();
   815               // First switch the avatar off
       
   816               $q = $db->sql_query('UPDATE ' . table_prefix . 'users SET user_has_avatar = 0 WHERE user_id = ' . $session->user_id . ';');
       
   817               if ( !$q )
       
   818                 $db->_die('Avatar CP switching user avatar off');
       
   819               
       
   820               if ( @unlink($avi_path) )
       
   821               {
       
   822                 echo '<div class="info-box">' . $lang->get('usercp_avatar_delete_success') . '</div>';
       
   823               }
       
   824               $has_avi = 0;
       
   825             }
       
   826             break;
       
   827           case 'set_http':
       
   828           case 'set_file':
       
   829             // Hackish way to preserve the UNIX philosophy of reusing as much code as possible
       
   830             if ( $action == 'set_http' )
       
   831             {
       
   832               // Check if this action is enabled
       
   833               if ( getConfig('avatar_upload_http', 1) !== 1 )
       
   834               {
       
   835                 // non-localized, only appears on hack attempt
       
   836                 echo '<div class="error-box">Uploads over HTTP are disabled.</div>';
       
   837                 break;
       
   838               }
       
   839               // Download the file
       
   840               require_once( ENANO_ROOT . '/includes/http.php' );
       
   841               
       
   842               if ( !preg_match('/^http:\/\/([a-z0-9-\.]+)(:([0-9]+))?\/(.+)$/', $_POST['avatar_http_url'], $match) )
       
   843               {
       
   844                 echo '<div class="error-box">' . $lang->get('usercp_avatar_invalid_url') . '</div>';
       
   845                 break;
       
   846               }
       
   847               
       
   848               $hostname = $match[1];
       
   849               $uri = '/' . $match[4];
       
   850               $port = ( $match[3] ) ? intval($match[3]) : 80;
       
   851               $max_size = intval(getConfig('avatar_max_size'));
       
   852               
       
   853               // Get temporary file
       
   854               $tempfile = tempnam(false, "enanoavatar_{$session->user_id}");
       
   855               if ( !$tempfile )
       
   856                 echo '<div class="error-box">Error getting temp file.</div>';
       
   857               
       
   858               @unlink($tempfile);
       
   859               $request = new Request_HTTP($hostname, $uri, 'GET', $port);
       
   860               $result = $request->write_response_to_file($tempfile, 50, $max_size);
       
   861               if ( !$result || $request->response_code != HTTP_OK )
       
   862               {
       
   863                 @unlink($tempfile);
       
   864                 echo '<div class="error-box">' . $lang->get('usercp_avatar_bad_write') . '</div>';
       
   865                 break;
       
   866               }
       
   867               
       
   868               // Response written. Proceed to validation...
       
   869             }
       
   870             else
       
   871             {
       
   872               // Check if this action is enabled
       
   873               if ( getConfig('avatar_upload_file', 1) !== 1 )
       
   874               {
       
   875                 // non-localized, only appears on hack attempt
       
   876                 echo '<div class="error-box">Uploads from the browser are disabled.</div>';
       
   877                 break;
       
   878               }
       
   879               
       
   880               $max_size = intval(getConfig('avatar_max_size'));
       
   881               
       
   882               $file =& $_FILES['avatar_file'];
       
   883               $tempfile =& $file['tmp_name'];
       
   884               if ( filesize($tempfile) > $max_size )
       
   885               {
       
   886                 @unlink($tempfile);
       
   887                 echo '<div class="error-box">' . $lang->get('usercp_avatar_file_too_large') . '</div>';
       
   888                 break;
       
   889               }
       
   890             }
       
   891             $file_type = get_image_filetype($tempfile);
       
   892             if ( !$file_type )
       
   893             {
       
   894               unlink($tempfile);
       
   895               echo '<div class="error-box">' . $lang->get('usercp_avatar_bad_filetype') . '</div>';
       
   896               break;
       
   897             }
       
   898             
       
   899             $avi_path_new = ENANO_ROOT . '/' . getConfig('avatar_directory') . '/' . $session->user_id . '.' . $file_type;
       
   900             
       
   901             // The file type is good - validate dimensions and animation
       
   902             switch($file_type)
       
   903             {
       
   904               case 'png':
       
   905                 $is_animated = is_png_animated($tempfile);
       
   906                 $dimensions = png_get_dimensions($tempfile);
       
   907                 break;
       
   908               case 'gif':
       
   909                 $is_animated = is_gif_animated($tempfile);
       
   910                 $dimensions = gif_get_dimensions($tempfile);
       
   911                 break;
       
   912               case 'jpg':
       
   913                 $is_animated = false;
       
   914                 $dimensions = jpg_get_dimensions($tempfile);
       
   915                 break;
       
   916               default:
       
   917                 echo '<div class="error-box">API mismatch</div>';
       
   918                 break 2;
       
   919             }
       
   920             // Did we get invalid size data? If so the image is probably corrupt.
       
   921             if ( !$dimensions )
       
   922             {
       
   923               @unlink($tempfile);
       
   924               echo '<div class="error-box">' . $lang->get('usercp_avatar_corrupt_image') . '</div>';
       
   925               break;
       
   926             }
       
   927             // Is the image animated?
       
   928             if ( $is_animated && getConfig('avatar_enable_anim') !== '1' )
       
   929             {
       
   930               @unlink($tempfile);
       
   931               echo '<div class="error-box">' . $lang->get('usercp_avatar_disallowed_animation') . '</div>';
       
   932               break;
       
   933             }
       
   934             // Check image dimensions
       
   935             list($image_x, $image_y) = $dimensions;
       
   936             $max_x = intval(getConfig('avatar_max_width'));
       
   937             $max_y = intval(getConfig('avatar_max_height'));
       
   938             if ( $image_x > $max_x || $image_y > $max_y )
       
   939             {
       
   940               @unlink($tempfile);
       
   941               echo '<div class="error-box">' . $lang->get('usercp_avatar_too_large') . '</div>';
       
   942               break;
       
   943             }
       
   944             // All good!
       
   945             @unlink($avi_path);
       
   946             if ( rename($tempfile, $avi_path_new) )
       
   947             {
       
   948               $q = $db->sql_query('UPDATE ' . table_prefix . "users SET user_has_avatar = 1, avatar_type = '$file_type' WHERE user_id = {$session->user_id};");
       
   949               if ( !$q )
       
   950                 $db->_die('Avatar CP updating users table after successful avatar upload');
       
   951               $has_avi = 1;
       
   952               $avi_type = $file_type;
       
   953               echo '<div class="info-box">' . $lang->get('usercp_avatar_upload_success') . '</div>';
       
   954             }
       
   955             else
       
   956             {
       
   957               echo '<div class="error-box">' . $lang->get('usercp_avatar_move_failed') . '</div>';
       
   958             }
       
   959             break;
       
   960           case 'set_gravatar':
       
   961             // set avatar to use Gravatar
       
   962             // make sure we're allowed to do this
       
   963             if ( getConfig('avatar_upload_gravatar') != '1' )
       
   964             {
       
   965               // access denied
       
   966               break;
       
   967             }
       
   968             // first, remove old image
       
   969             if ( $has_avi )
       
   970             {
       
   971               // First switch the avatar off
       
   972               $q = $db->sql_query('UPDATE ' . table_prefix . 'users SET user_has_avatar = 0 WHERE user_id = ' . $session->user_id . ';');
       
   973               if ( !$q )
       
   974                 $db->_die('Avatar CP switching user avatar off');
       
   975               
       
   976               @unlink($avi_path);
       
   977             }
       
   978             // set to gravatar mode
       
   979             $q = $db->sql_query('UPDATE ' . table_prefix . 'users SET user_has_avatar = 1, avatar_type = \'grv\' WHERE user_id = ' . $session->user_id . ';');
       
   980             if ( !$q )
       
   981               $db->_die('Avatar CP switching user avatar off');
       
   982               
       
   983             $has_avi = 1;
       
   984             echo '<div class="info-box">' . $lang->get('usercp_avatar_gravatar_success') . '</div>';
       
   985             break;
       
   986         }
       
   987       }
   812       }
   988       
   813       
   989       ?>
   814       ?>
   990       <script type="text/javascript">
   815       <script type="text/javascript">
   991       
   816       
   992         function avatar_select_field(elParent)
   817         function avatar_select_field(elParent)
   993         {
   818         {
       
   819           $('td#avatar_upload_btns > div:visible').hide('blind');
   994           switch(elParent.value)
   820           switch(elParent.value)
   995           {
   821           {
   996             case 'keep':
       
   997             case 'remove':
       
   998               $('avatar_upload_http').object.style.display = 'none';
       
   999               $('avatar_upload_file').object.style.display = 'none';
       
  1000               $('avatar_upload_gravatar').object.style.display = 'none';
       
  1001               break;
       
  1002             case 'set_http':
   822             case 'set_http':
  1003               $('avatar_upload_http').object.style.display = 'block';
   823               $('#avatar_upload_http').show('blind');
  1004               $('avatar_upload_file').object.style.display = 'none';
       
  1005               $('avatar_upload_gravatar').object.style.display = 'none';
       
  1006               break;
   824               break;
  1007             case 'set_file':
   825             case 'set_file':
  1008               $('avatar_upload_http').object.style.display = 'none';
   826               $('#avatar_upload_file').show('blind');
  1009               $('avatar_upload_file').object.style.display = 'block';
       
  1010               $('avatar_upload_gravatar').object.style.display = 'none';
       
  1011               break;
   827               break;
  1012             case 'set_gravatar':
   828             case 'set_gravatar':
  1013               $('avatar_upload_gravatar').object.style.display = 'block';
   829               $('#avatar_upload_gravatar').show('blind');
  1014               $('avatar_upload_http').object.style.display = 'none';
       
  1015               $('avatar_upload_file').object.style.display = 'none';
       
  1016               break;
   830               break;
  1017           }
   831           }
  1018         }
   832         }
  1019       
   833       
  1020       </script>
   834       </script>
  1028                 ' . $lang->get('usercp_avatar_table_title') . '
   842                 ' . $lang->get('usercp_avatar_table_title') . '
  1029               </th>
   843               </th>
  1030             </tr>';
   844             </tr>';
  1031             
   845             
  1032       echo '<tr>
   846       echo '<tr>
  1033               <td class="row2" style="width: 50%;">
   847               <td class="row2" style="width: 150px;">
  1034                 ' . $lang->get('usercp_avatar_label_current') . '
   848                 ' . $lang->get('usercp_avatar_label_current') . '
  1035               </td>
   849               </td>
  1036               <td class="row1" style="text-align: center;">';
   850               <td class="row1" style="text-align: center;">';
  1037               
   851               
  1038       if ( $has_avi == 1 )
   852       if ( $has_avi == 1 )
  1049               
   863               
  1050       echo '  <tr>
   864       echo '  <tr>
  1051                 <td class="row2">
   865                 <td class="row2">
  1052                   ' . $lang->get('usercp_avatar_lbl_change') . '
   866                   ' . $lang->get('usercp_avatar_lbl_change') . '
  1053                 </td>
   867                 </td>
  1054                 <td class="row1">
   868                 <td class="row1" id="avatar_upload_btns">
  1055                   <label><input type="radio" name="avatar_action" value="keep" onclick="avatar_select_field(this);" checked="checked" /> ' . $lang->get('usercp_avatar_lbl_keep') . '</label><br />
   869                   <label><input type="radio" name="avatar_action" value="keep" onclick="avatar_select_field(this);" checked="checked" /> ' . $lang->get('usercp_avatar_lbl_keep') . '</label><br />
  1056                   <label><input type="radio" name="avatar_action" value="remove" onclick="avatar_select_field(this);" /> ' . $lang->get('usercp_avatar_lbl_remove') . '</label><br />';
   870                   <label><input type="radio" name="avatar_action" value="remove" onclick="avatar_select_field(this);" /> ' . $lang->get('usercp_avatar_lbl_remove') . '</label><br />';
  1057       if ( getConfig('avatar_upload_http') == '1' )
   871       if ( getConfig('avatar_upload_http') == '1' )
  1058       {
   872       {
  1059         echo '    <label><input type="radio" name="avatar_action" value="set_http" onclick="avatar_select_field(this);" /> ' . $lang->get('usercp_avatar_lbl_set_http') . '</label><br />
   873         echo '    <label><input type="radio" name="avatar_action" value="set_http" onclick="avatar_select_field(this);" /> ' . $lang->get('usercp_avatar_lbl_set_http') . '</label><br />
  1060                   <div id="avatar_upload_http" style="display: none; margin: 10px 0 0 2.2em;">
   874                   <div id="avatar_upload_http" style="display: none; margin: 10px 0 0 2.2em;">
  1061                     ' . $lang->get('usercp_avatar_lbl_url') . ' <input type="text" name="avatar_http_url" size="40" value="http://" /><br />
   875                     ' . $lang->get('usercp_avatar_lbl_url') . ' <input type="text" name="avatar_http_url" size="40" value="http://" /><br />
  1062                     <small>' . $lang->get('usercp_avatar_lbl_url_desc') . ' ' . $lang->get('usercp_avatar_limits') . '</small>
   876                     <small>' . $lang->get('usercp_avatar_lbl_url_desc') . ' ' . $lang->get('usercp_avatar_limits') . '</small>
  1063                   </div>';
   877                   </div>';
  1064       }
   878       }
  1065       else
       
  1066       {
       
  1067         echo '    <div id="avatar_upload_http" style="display: none;"></div>';
       
  1068       }
       
  1069       if ( getConfig('avatar_upload_file') == '1' )
   879       if ( getConfig('avatar_upload_file') == '1' )
  1070       {
   880       {
  1071         echo '    <label><input type="radio" name="avatar_action" value="set_file" onclick="avatar_select_field(this);" /> ' . $lang->get('usercp_avatar_lbl_set_file') . '</label><br />
   881         echo '    <label><input type="radio" name="avatar_action" value="set_file" onclick="avatar_select_field(this);" /> ' . $lang->get('usercp_avatar_lbl_set_file') . '</label><br />
  1072                   <div id="avatar_upload_file" style="display: none; margin: 10px 0 0 2.2em;">
   882                   <div id="avatar_upload_file" style="display: none; margin: 10px 0 0 2.2em;">
  1073                     ' . $lang->get('usercp_avatar_lbl_file') . ' <input type="file" name="avatar_file" size="40" /><br />
   883                     ' . $lang->get('usercp_avatar_lbl_file') . ' <input type="file" name="avatar_file" size="40" /><br />
  1074                     <small>' . $lang->get('usercp_avatar_lbl_file_desc') . ' ' . $lang->get('usercp_avatar_limits') . '</small>
   884                     <small>' . $lang->get('usercp_avatar_lbl_file_desc') . ' ' . $lang->get('usercp_avatar_limits') . '</small>
  1075                   </div>';
   885                   </div>';
  1076       }
       
  1077       else
       
  1078       {
       
  1079         echo '    <div id="avatar_upload_file" style="display: none;"></div>';
       
  1080       }
   886       }
  1081       if ( getConfig('avatar_upload_gravatar') == '1' )
   887       if ( getConfig('avatar_upload_gravatar') == '1' )
  1082       {
   888       {
  1083         $rating_images = array('g' => '0', 'pg' => '1', 'r' => '2', 'x' => '3');
   889         $rating_images = array('g' => '0', 'pg' => '1', 'r' => '2', 'x' => '3');
  1084         $rating_id = $rating_images[ getConfig('gravatar_rating', 'g') ];
   890         $rating_id = $rating_images[ getConfig('gravatar_rating', 'g') ];
  1090                       <img alt=" " src="' . $rating_image . '" />
   896                       <img alt=" " src="' . $rating_image . '" />
  1091                     </div>
   897                     </div>
  1092                     ' . $lang->get("usercp_avatar_gravatar_rating_$max_rating") . '
   898                     ' . $lang->get("usercp_avatar_gravatar_rating_$max_rating") . '
  1093                   </div>';
   899                   </div>';
  1094       }
   900       }
  1095       else
       
  1096       {
       
  1097         echo '    <div id="avatar_upload_gravatar" style="display: none;"></div>';
       
  1098       }
       
  1099       echo '    </td>
   901       echo '    </td>
  1100               </tr>';
   902               </tr>';
  1101               
   903               
  1102       echo '  <tr>
   904       echo '  <tr>
  1103                 <th class="subhead" colspan="2">
   905                 <th class="subhead" colspan="2">
  1126   }
   928   }
  1127   
   929   
  1128   $template->footer();
   930   $template->footer();
  1129 }
   931 }
  1130 
   932 
       
   933 // Avatar POST processor
       
   934 function avatar_post($user_id, $quiet = false)
       
   935 {
       
   936   global $db, $session, $paths, $template, $plugins; // Common objects
       
   937   global $lang;
       
   938   
       
   939   $had_a_boo_boo = true;
       
   940   
       
   941   // Determine current avatar
       
   942   $q = $db->sql_query('SELECT user_has_avatar, avatar_type FROM ' . table_prefix . 'users WHERE user_id = ' . $session->user_id . ';');
       
   943   if ( !$q )
       
   944     $db->_die('Avatar CP selecting user\'s avatar data');
       
   945   
       
   946   list($has_avi, $avi_type) = $db->fetchrow_num();
       
   947   
       
   948   $action = ( isset($_POST['avatar_action']) ) ? $_POST['avatar_action'] : 'keep';
       
   949   $avi_path = ENANO_ROOT . '/' . getConfig('avatar_directory') . '/' . $user_id . '.' . $avi_type;
       
   950   switch($action)
       
   951   {
       
   952     case 'keep':
       
   953     default:
       
   954       $had_a_boo_boo = false;
       
   955       break;
       
   956     case 'remove':
       
   957       if ( $has_avi )
       
   958       {
       
   959         // First switch the avatar off
       
   960         $q = $db->sql_query('UPDATE ' . table_prefix . 'users SET user_has_avatar = 0 WHERE user_id = ' . $user_id . ';');
       
   961         if ( !$q )
       
   962           $db->_die('Avatar CP switching user avatar off');
       
   963         
       
   964         if ( @unlink($avi_path) )
       
   965         {
       
   966           $quiet || print '<div class="info-box">' . $lang->get('usercp_avatar_delete_success') . '</div>';
       
   967         }
       
   968         $has_avi = 0;
       
   969       }
       
   970       $had_a_boo_boo = false;
       
   971       break;
       
   972     case 'set_http':
       
   973     case 'set_file':
       
   974       // Hackish way to preserve the UNIX philosophy of reusing as much code as possible
       
   975       if ( $action == 'set_http' )
       
   976       {
       
   977         // Check if this action is enabled
       
   978         if ( getConfig('avatar_upload_http', 1) !== 1 )
       
   979         {
       
   980           // non-localized, only appears on hack attempt
       
   981           echo '<div class="error-box">Uploads over HTTP are disabled.</div>';
       
   982           break;
       
   983         }
       
   984         // Download the file
       
   985         require_once( ENANO_ROOT . '/includes/http.php' );
       
   986         
       
   987         if ( !preg_match('/^http:\/\/((?:[a-z0-9-\.]+|\[[a-f0-9:]+\]))(:([0-9]+))?\/(.+)$/', $_POST['avatar_http_url'], $match) )
       
   988         {
       
   989           echo '<div class="error-box">' . $lang->get('usercp_avatar_invalid_url') . '</div>';
       
   990           break;
       
   991         }
       
   992         
       
   993         $hostname = $match[1];
       
   994         $uri = '/' . $match[4];
       
   995         $port = ( $match[3] ) ? intval($match[3]) : 80;
       
   996         $max_size = intval(getConfig('avatar_max_size'));
       
   997         
       
   998         // Get temporary file
       
   999         $tempfile = tempnam(false, "enanoavatar_{$user_id}");
       
  1000         if ( !$tempfile )
       
  1001           echo '<div class="error-box">Error getting temp file.</div>';
       
  1002         
       
  1003         @unlink($tempfile);
       
  1004         $request = new Request_HTTP($hostname, $uri, 'GET', $port);
       
  1005         // max download size: 2MB, keeps things reasonable
       
  1006         // note: we'll try to scale the image down before checking filesize
       
  1007         $result = $request->write_response_to_file($tempfile, 1160, 2097152);
       
  1008         if ( !$result || $request->response_code != HTTP_OK )
       
  1009         {
       
  1010           @unlink($tempfile);
       
  1011           echo '<div class="error-box">' . $lang->get('usercp_avatar_bad_write') . '</div>';
       
  1012           break;
       
  1013         }
       
  1014         
       
  1015         // Response written. Proceed to validation...
       
  1016       }
       
  1017       else
       
  1018       {
       
  1019         // Check if this action is enabled
       
  1020         if ( getConfig('avatar_upload_file', 1) !== 1 )
       
  1021         {
       
  1022           // non-localized, only appears on hack attempt
       
  1023           echo '<div class="error-box">Uploads from the browser are disabled.</div>';
       
  1024           break;
       
  1025         }
       
  1026         
       
  1027         $max_size = intval(getConfig('avatar_max_size'));
       
  1028         
       
  1029         $file =& $_FILES['avatar_file'];
       
  1030         $tempfile =& $file['tmp_name'];
       
  1031       }
       
  1032       $file_type = get_image_filetype($tempfile);
       
  1033       if ( !$file_type )
       
  1034       {
       
  1035         @unlink($tempfile);
       
  1036         echo '<div class="error-box">' . $lang->get('usercp_avatar_bad_filetype') . '</div>';
       
  1037         break;
       
  1038       }
       
  1039       
       
  1040       $avi_path_new = ENANO_ROOT . '/' . getConfig('avatar_directory') . '/' . $user_id . '.' . $file_type;
       
  1041       
       
  1042       // The file type is good - validate dimensions and animation
       
  1043       switch($file_type)
       
  1044       {
       
  1045         case 'png':
       
  1046           $is_animated = is_png_animated($tempfile);
       
  1047           $dimensions = png_get_dimensions($tempfile);
       
  1048           break;
       
  1049         case 'gif':
       
  1050           $is_animated = is_gif_animated($tempfile);
       
  1051           $dimensions = gif_get_dimensions($tempfile);
       
  1052           break;
       
  1053         case 'jpg':
       
  1054           $is_animated = false;
       
  1055           $dimensions = jpg_get_dimensions($tempfile);
       
  1056           break;
       
  1057         default:
       
  1058           echo '<div class="error-box">API mismatch</div>';
       
  1059           break 2;
       
  1060       }
       
  1061       // Did we get invalid size data? If so the image is probably corrupt.
       
  1062       if ( !$dimensions )
       
  1063       {
       
  1064         @unlink($tempfile);
       
  1065         echo '<div class="error-box">' . $lang->get('usercp_avatar_corrupt_image') . '</div>';
       
  1066         break;
       
  1067       }
       
  1068       // Is the image animated?
       
  1069       if ( $is_animated && getConfig('avatar_enable_anim') !== '1' )
       
  1070       {
       
  1071         @unlink($tempfile);
       
  1072         echo '<div class="error-box">' . $lang->get('usercp_avatar_disallowed_animation') . '</div>';
       
  1073         break;
       
  1074       }
       
  1075       // Check image dimensions
       
  1076       list($image_x, $image_y) = $dimensions;
       
  1077       $max_x = intval(getConfig('avatar_max_width'));
       
  1078       $max_y = intval(getConfig('avatar_max_height'));
       
  1079       if ( $image_x > $max_x || $image_y > $max_y )
       
  1080       {
       
  1081         // try to scale the image
       
  1082         try
       
  1083         {
       
  1084           @rename($tempfile, "$tempfile-unscaled.$file_type");
       
  1085           $scale_result = scale_image("$tempfile-unscaled.$file_type", "$tempfile.$file_type", $max_x, $max_y, true);
       
  1086           if ( $scale_result )
       
  1087           {
       
  1088             if ( !(@unlink("$tempfile-unscaled.$file_type") && @rename("$tempfile.$file_type", $tempfile)) )
       
  1089             {
       
  1090               // scale failed
       
  1091               @unlink("$tempfile-scale.$file_type");
       
  1092               echo '<div class="error-box">Rename failure: ' . $lang->get('usercp_avatar_too_large') . '</div>';
       
  1093               break;
       
  1094             }
       
  1095           }
       
  1096           else
       
  1097           {
       
  1098             @unlink($tempfile);
       
  1099             @unlink("$tempfile-unscaled.$file_type");
       
  1100             echo '<div class="error-box">Scale failure: ' . $lang->get('usercp_avatar_too_large') . '</div>';
       
  1101             break;
       
  1102           }
       
  1103         }
       
  1104         catch ( Exception $e )
       
  1105         {
       
  1106           // If we get here, the scaling process most definitely failed.
       
  1107           echo '<div class="error-box">EXCEPTION: ' . $lang->get('usercp_avatar_too_large') . '</div>';
       
  1108           break;
       
  1109         }
       
  1110       }
       
  1111       // Check file size last, so that the scale operation is considered
       
  1112       if ( filesize($tempfile) > $max_size )
       
  1113       {
       
  1114         @unlink($tempfile);
       
  1115         echo '<div class="error-box">' . $lang->get('usercp_avatar_file_too_large') . '</div>';
       
  1116         break;
       
  1117       }
       
  1118       // All good!
       
  1119       @unlink($avi_path);
       
  1120       if ( rename($tempfile, $avi_path_new) )
       
  1121       {
       
  1122         $q = $db->sql_query('UPDATE ' . table_prefix . "users SET user_has_avatar = 1, avatar_type = '$file_type' WHERE user_id = {$user_id};");
       
  1123         if ( !$q )
       
  1124           $db->_die('Avatar CP updating users table after successful avatar upload');
       
  1125         $has_avi = 1;
       
  1126         $avi_type = $file_type;
       
  1127         $quiet || print '<div class="info-box">' . $lang->get('usercp_avatar_upload_success') . '</div>';
       
  1128       }
       
  1129       else
       
  1130       {
       
  1131         echo '<div class="error-box">' . $lang->get('usercp_avatar_move_failed') . '</div>';
       
  1132       }
       
  1133       $had_a_boo_boo = false;
       
  1134       break;
       
  1135     case 'set_gravatar':
       
  1136       // set avatar to use Gravatar
       
  1137       // make sure we're allowed to do this
       
  1138       if ( getConfig('avatar_upload_gravatar') != '1' )
       
  1139       {
       
  1140         // access denied
       
  1141         break;
       
  1142       }
       
  1143       // first, remove old image
       
  1144       if ( $has_avi )
       
  1145       {
       
  1146         // First switch the avatar off
       
  1147         $q = $db->sql_query('UPDATE ' . table_prefix . 'users SET user_has_avatar = 0 WHERE user_id = ' . $user_id . ';');
       
  1148         if ( !$q )
       
  1149           $db->_die('Avatar CP switching user avatar off');
       
  1150         
       
  1151         @unlink($avi_path);
       
  1152       }
       
  1153       // set to gravatar mode
       
  1154       $q = $db->sql_query('UPDATE ' . table_prefix . 'users SET user_has_avatar = 1, avatar_type = \'grv\' WHERE user_id = ' . $user_id . ';');
       
  1155       if ( !$q )
       
  1156         $db->_die('Avatar CP switching user avatar off');
       
  1157         
       
  1158       $has_avi = 1;
       
  1159       $quiet || print '<div class="info-box">' . $lang->get('usercp_avatar_gravatar_success') . '</div>';
       
  1160       $had_a_boo_boo = false;
       
  1161       break;
       
  1162   }
       
  1163   return array($has_avi, $avi_type, $had_a_boo_boo);
       
  1164 }
       
  1165 
  1131 ?>
  1166 ?>