plugins/SpecialUserPrefs.php
changeset 1103 90225c988124
parent 1095 5f623b0de18e
child 1169 d5474f54a525
equal deleted inserted replaced
1102:faef5e62e1e0 1103:90225c988124
    10 }
    10 }
    11 **!*/
    11 **!*/
    12 
    12 
    13 /*
    13 /*
    14  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
    14  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
    15  * Version 1.1.6 (Caoineag beta 1)
    15  * Copyright (C) 2006-2009 Dan Fuhry
    16  * Copyright (C) 2006-2008 Dan Fuhry
       
    17  *
    16  *
    18  * This program is Free Software; you can redistribute it and/or modify it under the terms of the GNU General Public License
    17  * This program is Free Software; you can redistribute it and/or modify it under the terms of the GNU General Public License
    19  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    18  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    20  *
    19  *
    21  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
    20  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
   183   
   182   
   184   $errors = '';
   183   $errors = '';
   185   
   184   
   186   switch ( $section )
   185   switch ( $section )
   187   {
   186   {
       
   187     case 'Avatar':
       
   188       $template->preload_js('jquery');
       
   189       $template->preload_js('jquery-ui');
       
   190       break;
   188     case 'EmailPassword':
   191     case 'EmailPassword':
   189       // Require elevated privileges (well sortof)
   192       // Require elevated privileges (well sortof)
   190       if ( $session->auth_level < USER_LEVEL_CHPREF )
   193       if ( $session->auth_level < USER_LEVEL_CHPREF )
   191       {
   194       {
   192         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);
   304       break;
   307       break;
   305     case 'Signature':
   308     case 'Signature':
   306       $template->tpl_strings['PAGE_NAME'] = $lang->get('usercp_signature_title');
   309       $template->tpl_strings['PAGE_NAME'] = $lang->get('usercp_signature_title');
   307       break;
   310       break;
   308     case 'Profile':
   311     case 'Profile':
       
   312     case 'Home':
       
   313       if ( isset($_POST['submit']) )
       
   314         csrf_request_confirm();
       
   315       
   309       $template->tpl_strings['PAGE_NAME'] = $lang->get('usercp_publicinfo_title');
   316       $template->tpl_strings['PAGE_NAME'] = $lang->get('usercp_publicinfo_title');
   310       break;
   317       break;
   311   }
   318   }
   312   
   319   
   313   $template->header();
   320   $template->header();
   452         $occupation = $db->escape($occupation);
   459         $occupation = $db->escape($occupation);
   453         
   460         
   454         $hobbies = htmlspecialchars($_POST['hobbies']);
   461         $hobbies = htmlspecialchars($_POST['hobbies']);
   455         $hobbies = $db->escape($hobbies);
   462         $hobbies = $db->escape($hobbies);
   456         
   463         
       
   464         $date_format = $db->escape(htmlspecialchars($_POST['date_format']));
       
   465         $time_format = $db->escape(htmlspecialchars($_POST['time_format']));
       
   466         
   457         $email_public = ( isset($_POST['email_public']) ) ? '1' : '0';
   467         $email_public = ( isset($_POST['email_public']) ) ? '1' : '0';
   458         $disable_js_fx = ( isset($_POST['disable_js_fx']) ) ? '1' : '0';
   468         $disable_js_fx = ( isset($_POST['disable_js_fx']) ) ? '1' : '0';
   459         
   469         
   460         $session->real_name = $real_name;
   470         $session->real_name = $real_name;
   461         
   471         
   481         $session->user_extra['user_homepage'] = $homepage;
   491         $session->user_extra['user_homepage'] = $homepage;
   482         $session->user_extra['user_location'] = $location;
   492         $session->user_extra['user_location'] = $location;
   483         $session->user_extra['user_job'] = $occupation;
   493         $session->user_extra['user_job'] = $occupation;
   484         $session->user_extra['user_hobbies'] = $hobbies;
   494         $session->user_extra['user_hobbies'] = $hobbies;
   485         $session->user_extra['email_public'] = intval($email_public);
   495         $session->user_extra['email_public'] = intval($email_public);
       
   496         $session->date_format = $date_format;
       
   497         $session->time_format = $time_format;
   486         
   498         
   487         // user title
   499         // user title
   488         $user_title_col = '';
   500         $user_title_col = '';
   489         if ( $session->get_permissions('custom_user_title') && isset($_POST['user_title']) )
   501         if ( $session->get_permissions('custom_user_title') && isset($_POST['user_title']) )
   490         {
   502         {
   534         if ( !$q )
   546         if ( !$q )
   535           $db->_die();
   547           $db->_die();
   536         
   548         
   537         $q = $db->sql_query('UPDATE '.table_prefix."users_extra SET user_aim='$imaddr_aim',user_yahoo='$imaddr_yahoo',user_msn='$imaddr_msn',
   549         $q = $db->sql_query('UPDATE '.table_prefix."users_extra SET user_aim='$imaddr_aim',user_yahoo='$imaddr_yahoo',user_msn='$imaddr_msn',
   538                                user_xmpp='$imaddr_xmpp',user_homepage='$homepage',user_location='$location',user_job='$occupation',
   550                                user_xmpp='$imaddr_xmpp',user_homepage='$homepage',user_location='$location',user_job='$occupation',
   539                                user_hobbies='$hobbies',email_public=$email_public,disable_js_fx=$disable_js_fx
   551                                user_hobbies='$hobbies',email_public=$email_public,disable_js_fx=$disable_js_fx,date_format='$date_format',
       
   552                                time_format='$time_format'
   540                                WHERE user_id=$session->user_id;");
   553                                WHERE user_id=$session->user_id;");
   541         
   554         
   542         if ( !$q )
   555         if ( !$q )
   543           $db->_die();
   556           $db->_die();
   544         
   557         
   628             <td class="row1"><?php echo $lang_box; ?></td>
   641             <td class="row1"><?php echo $lang_box; ?></td>
   629           </tr>
   642           </tr>
   630           <tr>
   643           <tr>
   631             <td class="row2"><?php echo $lang->get('usercp_publicinfo_field_changetheme_title'); ?></td>
   644             <td class="row2"><?php echo $lang->get('usercp_publicinfo_field_changetheme_title'); ?></td>
   632             <td class="row1"><?php echo $lang->get('usercp_publicinfo_field_changetheme_hint'); ?> <a href="<?php echo makeUrlNS('Special', 'ChangeStyle/' . $paths->page); ?>" onclick="ajaxChangeStyle(); return false;"><?php echo $lang->get('usercp_publicinfo_field_changetheme'); ?></a></td>
   645             <td class="row1"><?php echo $lang->get('usercp_publicinfo_field_changetheme_hint'); ?> <a href="<?php echo makeUrlNS('Special', 'ChangeStyle/' . $paths->page); ?>" onclick="ajaxChangeStyle(); return false;"><?php echo $lang->get('usercp_publicinfo_field_changetheme'); ?></a></td>
       
   646           </tr>
       
   647           <tr>
       
   648             <td class="row2"><?php echo $lang->get('usercp_publicinfo_field_dateformat'); ?></td>
       
   649             <td class="row1">
       
   650             <select name="date_format">
       
   651               <?php
       
   652               foreach ( array(DATE_1, DATE_2, DATE_3, DATE_4) as $format )
       
   653               {
       
   654                 $selected = $format === $session->date_format ? ' selected="selected"' : '';
       
   655                 echo '<option value="' . $format . '"' . $selected . '>' . enano_date($format) . '</option>';
       
   656               }
       
   657               ?>
       
   658             </select>
       
   659             </td>
       
   660           </tr>
       
   661           <tr>
       
   662             <td class="row2"><?php echo $lang->get('usercp_publicinfo_field_timeformat'); ?></td>
       
   663             <td class="row1">
       
   664             <select name="time_format">
       
   665               <?php
       
   666               foreach ( array(TIME_12_NS, TIME_12_S, TIME_24_NS, TIME_24_S) as $format )
       
   667               {
       
   668                 $selected = $format === $session->time_format ? ' selected="selected"' : '';
       
   669                 echo '<option value="' . $format . '"' . $selected . '>' . enano_date($format) . '</option>';
       
   670               }
       
   671               ?>
       
   672             </select>
       
   673             </td>
   633           </tr>
   674           </tr>
   634           <tr>
   675           <tr>
   635             <td class="row3" colspan="2"><?php echo $lang->get('usercp_publicinfo_field_timezone'); ?> <?php echo $tz_select; ?><br /><small><?php echo $lang->get('usercp_publicinfo_field_timezone_hint'); ?></small></td>
   676             <td class="row3" colspan="2"><?php echo $lang->get('usercp_publicinfo_field_timezone'); ?> <?php echo $tz_select; ?><br /><small><?php echo $lang->get('usercp_publicinfo_field_timezone_hint'); ?></small></td>
   636           </tr>
   677           </tr>
   637           <tr>
   678           <tr>
   743             </th>
   784             </th>
   744           </tr>
   785           </tr>
   745         </table>
   786         </table>
   746       </div>
   787       </div>
   747       <?php
   788       <?php
       
   789       // CSRF protection
       
   790       echo '<input type="hidden" name="cstok" value="' . $session->csrf_token . '" />';
   748       echo '</form>';
   791       echo '</form>';
   749       break;
   792       break;
   750     case 'Avatar':
   793     case 'Avatar':
   751       if ( getConfig('avatar_enable', 0) !== 1 )
   794       if ( getConfig('avatar_enable', 0) !== 1 )
   752       {
   795       {
   753         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>';
   754         break;
   797         break;
   755       }
   798       }
   756       
   799       
   757       // Determine current avatar
       
   758       $q = $db->sql_query('SELECT user_has_avatar, avatar_type FROM ' . table_prefix . 'users WHERE user_id = ' . $session->user_id . ';');
       
   759       if ( !$q )
       
   760         $db->_die('Avatar CP selecting user\'s avatar data');
       
   761       
       
   762       list($has_avi, $avi_type) = $db->fetchrow_num();
       
   763       
       
   764       if ( isset($_POST['submit']) )
   800       if ( isset($_POST['submit']) )
   765       {
   801       {
   766         $action = ( isset($_POST['avatar_action']) ) ? $_POST['avatar_action'] : 'keep';
   802         list($has_avi, $avi_type) = avatar_post($session->user_id);
   767         $avi_path = ENANO_ROOT . '/' . getConfig('avatar_directory') . '/' . $session->user_id . '.' . $avi_type;
   803       }
   768         switch($action)
   804       else
   769         {
   805       {
   770           case 'keep':
   806         // Determine current avatar
   771           default:
   807         $q = $db->sql_query('SELECT user_has_avatar, avatar_type FROM ' . table_prefix . 'users WHERE user_id = ' . $session->user_id . ';');
   772             break;
   808         if ( !$q )
   773           case 'remove':
   809           $db->_die('Avatar CP selecting user\'s avatar data');
   774             if ( $has_avi )
   810         
   775             {
   811         list($has_avi, $avi_type) = $db->fetchrow_num();
   776               // First switch the avatar off
       
   777               $q = $db->sql_query('UPDATE ' . table_prefix . 'users SET user_has_avatar = 0 WHERE user_id = ' . $session->user_id . ';');
       
   778               if ( !$q )
       
   779                 $db->_die('Avatar CP switching user avatar off');
       
   780               
       
   781               if ( @unlink($avi_path) )
       
   782               {
       
   783                 echo '<div class="info-box">' . $lang->get('usercp_avatar_delete_success') . '</div>';
       
   784               }
       
   785               $has_avi = 0;
       
   786             }
       
   787             break;
       
   788           case 'set_http':
       
   789           case 'set_file':
       
   790             // Hackish way to preserve the UNIX philosophy of reusing as much code as possible
       
   791             if ( $action == 'set_http' )
       
   792             {
       
   793               // Check if this action is enabled
       
   794               if ( getConfig('avatar_upload_http', 1) !== 1 )
       
   795               {
       
   796                 // non-localized, only appears on hack attempt
       
   797                 echo '<div class="error-box">Uploads over HTTP are disabled.</div>';
       
   798                 break;
       
   799               }
       
   800               // Download the file
       
   801               require_once( ENANO_ROOT . '/includes/http.php' );
       
   802               
       
   803               if ( !preg_match('/^http:\/\/([a-z0-9-\.]+)(:([0-9]+))?\/(.+)$/', $_POST['avatar_http_url'], $match) )
       
   804               {
       
   805                 echo '<div class="error-box">' . $lang->get('usercp_avatar_invalid_url') . '</div>';
       
   806                 break;
       
   807               }
       
   808               
       
   809               $hostname = $match[1];
       
   810               $uri = '/' . $match[4];
       
   811               $port = ( $match[3] ) ? intval($match[3]) : 80;
       
   812               $max_size = intval(getConfig('avatar_max_size'));
       
   813               
       
   814               // Get temporary file
       
   815               $tempfile = tempnam(false, "enanoavatar_{$session->user_id}");
       
   816               if ( !$tempfile )
       
   817                 echo '<div class="error-box">Error getting temp file.</div>';
       
   818               
       
   819               @unlink($tempfile);
       
   820               $request = new Request_HTTP($hostname, $uri, 'GET', $port);
       
   821               $result = $request->write_response_to_file($tempfile, 50, $max_size);
       
   822               if ( !$result || $request->response_code != HTTP_OK )
       
   823               {
       
   824                 @unlink($tempfile);
       
   825                 echo '<div class="error-box">' . $lang->get('usercp_avatar_bad_write') . '</div>';
       
   826                 break;
       
   827               }
       
   828               
       
   829               // Response written. Proceed to validation...
       
   830             }
       
   831             else
       
   832             {
       
   833               // Check if this action is enabled
       
   834               if ( getConfig('avatar_upload_file', 1) !== 1 )
       
   835               {
       
   836                 // non-localized, only appears on hack attempt
       
   837                 echo '<div class="error-box">Uploads from the browser are disabled.</div>';
       
   838                 break;
       
   839               }
       
   840               
       
   841               $max_size = intval(getConfig('avatar_max_size'));
       
   842               
       
   843               $file =& $_FILES['avatar_file'];
       
   844               $tempfile =& $file['tmp_name'];
       
   845               if ( filesize($tempfile) > $max_size )
       
   846               {
       
   847                 @unlink($tempfile);
       
   848                 echo '<div class="error-box">' . $lang->get('usercp_avatar_file_too_large') . '</div>';
       
   849                 break;
       
   850               }
       
   851             }
       
   852             $file_type = get_image_filetype($tempfile);
       
   853             if ( !$file_type )
       
   854             {
       
   855               unlink($tempfile);
       
   856               echo '<div class="error-box">' . $lang->get('usercp_avatar_bad_filetype') . '</div>';
       
   857               break;
       
   858             }
       
   859             
       
   860             $avi_path_new = ENANO_ROOT . '/' . getConfig('avatar_directory') . '/' . $session->user_id . '.' . $file_type;
       
   861             
       
   862             // The file type is good - validate dimensions and animation
       
   863             switch($file_type)
       
   864             {
       
   865               case 'png':
       
   866                 $is_animated = is_png_animated($tempfile);
       
   867                 $dimensions = png_get_dimensions($tempfile);
       
   868                 break;
       
   869               case 'gif':
       
   870                 $is_animated = is_gif_animated($tempfile);
       
   871                 $dimensions = gif_get_dimensions($tempfile);
       
   872                 break;
       
   873               case 'jpg':
       
   874                 $is_animated = false;
       
   875                 $dimensions = jpg_get_dimensions($tempfile);
       
   876                 break;
       
   877               default:
       
   878                 echo '<div class="error-box">API mismatch</div>';
       
   879                 break 2;
       
   880             }
       
   881             // Did we get invalid size data? If so the image is probably corrupt.
       
   882             if ( !$dimensions )
       
   883             {
       
   884               @unlink($tempfile);
       
   885               echo '<div class="error-box">' . $lang->get('usercp_avatar_corrupt_image') . '</div>';
       
   886               break;
       
   887             }
       
   888             // Is the image animated?
       
   889             if ( $is_animated && getConfig('avatar_enable_anim') !== '1' )
       
   890             {
       
   891               @unlink($tempfile);
       
   892               echo '<div class="error-box">' . $lang->get('usercp_avatar_disallowed_animation') . '</div>';
       
   893               break;
       
   894             }
       
   895             // Check image dimensions
       
   896             list($image_x, $image_y) = $dimensions;
       
   897             $max_x = intval(getConfig('avatar_max_width'));
       
   898             $max_y = intval(getConfig('avatar_max_height'));
       
   899             if ( $image_x > $max_x || $image_y > $max_y )
       
   900             {
       
   901               @unlink($tempfile);
       
   902               echo '<div class="error-box">' . $lang->get('usercp_avatar_too_large') . '</div>';
       
   903               break;
       
   904             }
       
   905             // All good!
       
   906             @unlink($avi_path);
       
   907             if ( rename($tempfile, $avi_path_new) )
       
   908             {
       
   909               $q = $db->sql_query('UPDATE ' . table_prefix . "users SET user_has_avatar = 1, avatar_type = '$file_type' WHERE user_id = {$session->user_id};");
       
   910               if ( !$q )
       
   911                 $db->_die('Avatar CP updating users table after successful avatar upload');
       
   912               $has_avi = 1;
       
   913               $avi_type = $file_type;
       
   914               echo '<div class="info-box">' . $lang->get('usercp_avatar_upload_success') . '</div>';
       
   915             }
       
   916             else
       
   917             {
       
   918               echo '<div class="error-box">' . $lang->get('usercp_avatar_move_failed') . '</div>';
       
   919             }
       
   920             break;
       
   921           case 'set_gravatar':
       
   922             // set avatar to use Gravatar
       
   923             // make sure we're allowed to do this
       
   924             if ( getConfig('avatar_upload_gravatar') != '1' )
       
   925             {
       
   926               // access denied
       
   927               break;
       
   928             }
       
   929             // first, remove old image
       
   930             if ( $has_avi )
       
   931             {
       
   932               // First switch the avatar off
       
   933               $q = $db->sql_query('UPDATE ' . table_prefix . 'users SET user_has_avatar = 0 WHERE user_id = ' . $session->user_id . ';');
       
   934               if ( !$q )
       
   935                 $db->_die('Avatar CP switching user avatar off');
       
   936               
       
   937               @unlink($avi_path);
       
   938             }
       
   939             // set to gravatar mode
       
   940             $q = $db->sql_query('UPDATE ' . table_prefix . 'users SET user_has_avatar = 1, avatar_type = \'grv\' WHERE user_id = ' . $session->user_id . ';');
       
   941             if ( !$q )
       
   942               $db->_die('Avatar CP switching user avatar off');
       
   943               
       
   944             $has_avi = 1;
       
   945             echo '<div class="info-box">' . $lang->get('usercp_avatar_gravatar_success') . '</div>';
       
   946             break;
       
   947         }
       
   948       }
   812       }
   949       
   813       
   950       ?>
   814       ?>
   951       <script type="text/javascript">
   815       <script type="text/javascript">
   952       
   816       
   953         function avatar_select_field(elParent)
   817         function avatar_select_field(elParent)
   954         {
   818         {
       
   819           $('td#avatar_upload_btns > div:visible').hide('blind');
   955           switch(elParent.value)
   820           switch(elParent.value)
   956           {
   821           {
   957             case 'keep':
       
   958             case 'remove':
       
   959               $('avatar_upload_http').object.style.display = 'none';
       
   960               $('avatar_upload_file').object.style.display = 'none';
       
   961               $('avatar_upload_gravatar').object.style.display = 'none';
       
   962               break;
       
   963             case 'set_http':
   822             case 'set_http':
   964               $('avatar_upload_http').object.style.display = 'block';
   823               $('#avatar_upload_http').show('blind');
   965               $('avatar_upload_file').object.style.display = 'none';
       
   966               $('avatar_upload_gravatar').object.style.display = 'none';
       
   967               break;
   824               break;
   968             case 'set_file':
   825             case 'set_file':
   969               $('avatar_upload_http').object.style.display = 'none';
   826               $('#avatar_upload_file').show('blind');
   970               $('avatar_upload_file').object.style.display = 'block';
       
   971               $('avatar_upload_gravatar').object.style.display = 'none';
       
   972               break;
   827               break;
   973             case 'set_gravatar':
   828             case 'set_gravatar':
   974               $('avatar_upload_gravatar').object.style.display = 'block';
   829               $('#avatar_upload_gravatar').show('blind');
   975               $('avatar_upload_http').object.style.display = 'none';
       
   976               $('avatar_upload_file').object.style.display = 'none';
       
   977               break;
   830               break;
   978           }
   831           }
   979         }
   832         }
   980       
   833       
   981       </script>
   834       </script>
   989                 ' . $lang->get('usercp_avatar_table_title') . '
   842                 ' . $lang->get('usercp_avatar_table_title') . '
   990               </th>
   843               </th>
   991             </tr>';
   844             </tr>';
   992             
   845             
   993       echo '<tr>
   846       echo '<tr>
   994               <td class="row2" style="width: 50%;">
   847               <td class="row2" style="width: 150px;">
   995                 ' . $lang->get('usercp_avatar_label_current') . '
   848                 ' . $lang->get('usercp_avatar_label_current') . '
   996               </td>
   849               </td>
   997               <td class="row1" style="text-align: center;">';
   850               <td class="row1" style="text-align: center;">';
   998               
   851               
   999       if ( $has_avi == 1 )
   852       if ( $has_avi == 1 )
  1010               
   863               
  1011       echo '  <tr>
   864       echo '  <tr>
  1012                 <td class="row2">
   865                 <td class="row2">
  1013                   ' . $lang->get('usercp_avatar_lbl_change') . '
   866                   ' . $lang->get('usercp_avatar_lbl_change') . '
  1014                 </td>
   867                 </td>
  1015                 <td class="row1">
   868                 <td class="row1" id="avatar_upload_btns">
  1016                   <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 />
  1017                   <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 />';
  1018       if ( getConfig('avatar_upload_http') == '1' )
   871       if ( getConfig('avatar_upload_http') == '1' )
  1019       {
   872       {
  1020         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 />
  1021                   <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;">
  1022                     ' . $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 />
  1023                     <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>
  1024                   </div>';
   877                   </div>';
  1025       }
   878       }
  1026       else
       
  1027       {
       
  1028         echo '    <div id="avatar_upload_http" style="display: none;"></div>';
       
  1029       }
       
  1030       if ( getConfig('avatar_upload_file') == '1' )
   879       if ( getConfig('avatar_upload_file') == '1' )
  1031       {
   880       {
  1032         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 />
  1033                   <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;">
  1034                     ' . $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 />
  1035                     <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>
  1036                   </div>';
   885                   </div>';
  1037       }
       
  1038       else
       
  1039       {
       
  1040         echo '    <div id="avatar_upload_file" style="display: none;"></div>';
       
  1041       }
   886       }
  1042       if ( getConfig('avatar_upload_gravatar') == '1' )
   887       if ( getConfig('avatar_upload_gravatar') == '1' )
  1043       {
   888       {
  1044         $rating_images = array('g' => '0', 'pg' => '1', 'r' => '2', 'x' => '3');
   889         $rating_images = array('g' => '0', 'pg' => '1', 'r' => '2', 'x' => '3');
  1045         $rating_id = $rating_images[ getConfig('gravatar_rating', 'g') ];
   890         $rating_id = $rating_images[ getConfig('gravatar_rating', 'g') ];
  1051                       <img alt=" " src="' . $rating_image . '" />
   896                       <img alt=" " src="' . $rating_image . '" />
  1052                     </div>
   897                     </div>
  1053                     ' . $lang->get("usercp_avatar_gravatar_rating_$max_rating") . '
   898                     ' . $lang->get("usercp_avatar_gravatar_rating_$max_rating") . '
  1054                   </div>';
   899                   </div>';
  1055       }
   900       }
  1056       else
       
  1057       {
       
  1058         echo '    <div id="avatar_upload_gravatar" style="display: none;"></div>';
       
  1059       }
       
  1060       echo '    </td>
   901       echo '    </td>
  1061               </tr>';
   902               </tr>';
  1062               
   903               
  1063       echo '  <tr>
   904       echo '  <tr>
  1064                 <th class="subhead" colspan="2">
   905                 <th class="subhead" colspan="2">
  1087   }
   928   }
  1088   
   929   
  1089   $template->footer();
   930   $template->footer();
  1090 }
   931 }
  1091 
   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', 0) !== 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 
  1092 ?>
  1166 ?>