plugins/SpecialUserPrefs.php
changeset 328 dc838fd61a06
parent 322 5f1cd51bf1be
child 329 0437a7cf1acc
equal deleted inserted replaced
327:c2f4c900c507 328:dc838fd61a06
   101   global $userprefs_menu_links;
   101   global $userprefs_menu_links;
   102   
   102   
   103   userprefs_menu_add('Profile/membership', 'Edit e-mail address and password', makeUrlNS('Special', 'Preferences/EmailPassword') . '" onclick="ajaxLoginNavTo(\'Special\', \'Preferences/EmailPassword\', '.USER_LEVEL_CHPREF.'); return false;');
   103   userprefs_menu_add('Profile/membership', 'Edit e-mail address and password', makeUrlNS('Special', 'Preferences/EmailPassword') . '" onclick="ajaxLoginNavTo(\'Special\', \'Preferences/EmailPassword\', '.USER_LEVEL_CHPREF.'); return false;');
   104   userprefs_menu_add('Profile/membership', 'Edit signature', makeUrlNS('Special', 'Preferences/Signature'));
   104   userprefs_menu_add('Profile/membership', 'Edit signature', makeUrlNS('Special', 'Preferences/Signature'));
   105   userprefs_menu_add('Profile/membership', 'Edit public profile', makeUrlNS('Special', 'Preferences/Profile'));
   105   userprefs_menu_add('Profile/membership', 'Edit public profile', makeUrlNS('Special', 'Preferences/Profile'));
       
   106   if ( getConfig('avatar_enable') == '1' )
       
   107   {
       
   108     userprefs_menu_add('Profile/membership', 'Avatar settings', makeUrlNS('Special', 'Preferences/Avatar'));
       
   109   }
   106   userprefs_menu_add('Private messages', 'Inbox', makeUrlNS('Special', 'PrivateMessages/Folder/Inbox'));
   110   userprefs_menu_add('Private messages', 'Inbox', makeUrlNS('Special', 'PrivateMessages/Folder/Inbox'));
   107   userprefs_menu_add('Private messages', 'Outbox', makeUrlNS('Special', 'PrivateMessages/Folder/Outbox'));
   111   userprefs_menu_add('Private messages', 'Outbox', makeUrlNS('Special', 'PrivateMessages/Folder/Outbox'));
   108   userprefs_menu_add('Private messages', 'Sent items', makeUrlNS('Special', 'PrivateMessages/Folder/Sent'));
   112   userprefs_menu_add('Private messages', 'Sent items', makeUrlNS('Special', 'PrivateMessages/Folder/Sent'));
   109   userprefs_menu_add('Private messages', 'Drafts', makeUrlNS('Special', 'PrivateMessages/Folder/Drafts'));
   113   userprefs_menu_add('Private messages', 'Drafts', makeUrlNS('Special', 'PrivateMessages/Folder/Drafts'));
   110   userprefs_menu_add('Private messages', 'Archive', makeUrlNS('Special', 'PrivateMessages/Folder/Archive'));
   114   userprefs_menu_add('Private messages', 'Archive', makeUrlNS('Special', 'PrivateMessages/Folder/Archive'));
   122 $plugins->attachHook('session_started', 'userprefs_menu_init();');
   126 $plugins->attachHook('session_started', 'userprefs_menu_init();');
   123 
   127 
   124 function page_Special_Preferences()
   128 function page_Special_Preferences()
   125 {
   129 {
   126   global $db, $session, $paths, $template, $plugins; // Common objects
   130   global $db, $session, $paths, $template, $plugins; // Common objects
       
   131   global $lang;
   127   
   132   
   128   // We need a login to continue
   133   // We need a login to continue
   129   if ( !$session->user_logged_in )
   134   if ( !$session->user_logged_in )
   130     redirect(makeUrlNS('Special', 'Login/' . $paths->page), 'Login required', 'You need to be logged in to access this page. Please wait while you are redirected to the login page.');
   135     redirect(makeUrlNS('Special', 'Login/' . $paths->page), 'Login required', 'You need to be logged in to access this page. Please wait while you are redirected to the login page.');
   131   
   136   
   592         </table>
   597         </table>
   593       </div>
   598       </div>
   594       <?php
   599       <?php
   595       echo '</form>';
   600       echo '</form>';
   596       break;
   601       break;
       
   602     case 'Avatar':
       
   603       if ( getConfig('avatar_enable') != '1' )
       
   604       {
       
   605         echo '<div class="error-box"><b>' . $lang->get('usercp_avatar_err_disabled_title') . '</b><br />' . $lang->get('usercp_avatar_err_disabled_body') . '</div>';
       
   606       }
       
   607       
       
   608       // Determine current avatar
       
   609       $q = $db->sql_query('SELECT user_has_avatar, avatar_type FROM ' . table_prefix . 'users WHERE user_id = ' . $session->user_id . ';');
       
   610       if ( !$q )
       
   611         $db->_die('Avatar CP selecting user\'s avatar data');
       
   612       
       
   613       list($has_avi, $avi_type) = $db->fetchrow_num();
       
   614       
       
   615       if ( isset($_POST['submit']) )
       
   616       {
       
   617         $action = ( isset($_POST['avatar_action']) ) ? $_POST['avatar_action'] : 'keep';
       
   618         $avi_path = ENANO_ROOT . '/' . getConfig('avatar_directory') . '/' . $session->user_id . '.' . $avi_type;
       
   619         switch($action)
       
   620         {
       
   621           case 'keep':
       
   622           default:
       
   623             break;
       
   624           case 'remove':
       
   625             if ( $has_avi )
       
   626             {
       
   627               // First switch the avatar off
       
   628               $q = $db->sql_query('UPDATE ' . table_prefix . 'users SET user_has_avatar = 0 WHERE user_id = ' . $session->user_id . ';');
       
   629               if ( !$q )
       
   630                 $db->_die('Avatar CP switching user avatar off');
       
   631               
       
   632               if ( @unlink($avi_path) )
       
   633               {
       
   634                 echo '<div class="info-box">' . $lang->get('usercp_avatar_delete_success') . '</div>';
       
   635               }
       
   636               $has_avi = 0;
       
   637             }
       
   638             break;
       
   639           case 'set_http':
       
   640           case 'set_file':
       
   641             // Hackish way to preserve the UNIX philosophy of reusing as much code as possible
       
   642             if ( $action == 'set_http' )
       
   643             {
       
   644               // Check if this action is enabled
       
   645               if ( getConfig('avatar_upload_http') !== '1' )
       
   646               {
       
   647                 // non-localized, only appears on hack attempt
       
   648                 echo '<div class="error-box">Uploads over HTTP are disabled.</div>';
       
   649                 break;
       
   650               }
       
   651               // Download the file
       
   652               require_once( ENANO_ROOT . '/includes/http.php' );
       
   653               
       
   654               if ( !preg_match('/^http:\/\/([a-z0-9-\.]+)(:([0-9]+))?\/(.+)$/', $_POST['avatar_http_url'], $match) )
       
   655               {
       
   656                 echo '<div class="error-box">' . $lang->get('usercp_avatar_invalid_url') . '</div>';
       
   657                 break;
       
   658               }
       
   659               
       
   660               $hostname = $match[1];
       
   661               $uri = '/' . $match[4];
       
   662               $port = ( $match[3] ) ? intval($match[3]) : 80;
       
   663               $max_size = intval(getConfig('avatar_max_size'));
       
   664               
       
   665               // Get temporary file
       
   666               $tempfile = tempnam(false, "enanoavatar_{$session->user_id}");
       
   667               if ( !$tempfile )
       
   668                 echo '<div class="error-box">Error getting temp file.</div>';
       
   669               
       
   670               @unlink($tempfile);
       
   671               $request = new Request_HTTP($hostname, $uri, 'GET', $port);
       
   672               $result = $request->write_response_to_file($tempfile, 50, $max_size);
       
   673               if ( !$result || $request->response_code != HTTP_OK )
       
   674               {
       
   675                 @unlink($tempfile);
       
   676                 echo '<div class="error-box">' . $lang->get('usercp_avatar_bad_write') . '</div>';
       
   677                 break;
       
   678               }
       
   679               
       
   680               // Response written. Proceed to validation...
       
   681             }
       
   682             else
       
   683             {
       
   684               // Check if this action is enabled
       
   685               if ( getConfig('avatar_upload_file') !== '1' )
       
   686               {
       
   687                 // non-localized, only appears on hack attempt
       
   688                 echo '<div class="error-box">Uploads from the browser are disabled.</div>';
       
   689                 break;
       
   690               }
       
   691               
       
   692               $max_size = intval(getConfig('avatar_max_size'));
       
   693               
       
   694               $file =& $_FILES['avatar_file'];
       
   695               $tempfile =& $file['tmp_name'];
       
   696               if ( filesize($tempfile) > $max_size )
       
   697               {
       
   698                 @unlink($tempfile);
       
   699                 echo '<div class="error-box">' . $lang->get('usercp_avatar_file_too_large') . '</div>';
       
   700                 break;
       
   701               }
       
   702             }
       
   703             $file_type = get_image_filetype($tempfile);
       
   704             if ( !$file_type )
       
   705             {
       
   706               unlink($tempfile);
       
   707               echo '<div class="error-box">' . $lang->get('usercp_avatar_bad_filetype') . '</div>';
       
   708               break;
       
   709             }
       
   710             
       
   711             // The file type is good - validate dimensions and animation
       
   712             switch($file_type)
       
   713             {
       
   714               case 'png':
       
   715                 $is_animated = is_png_animated($tempfile);
       
   716                 $dimensions = png_get_dimensions($tempfile);
       
   717                 break;
       
   718               case 'gif':
       
   719                 $is_animated = is_gif_animated($tempfile);
       
   720                 $dimensions = gif_get_dimensions($tempfile);
       
   721                 break;
       
   722               case 'jpg':
       
   723                 $is_animated = false;
       
   724                 $dimensions = jpg_get_dimensions($tempfile);
       
   725                 break;
       
   726               default:
       
   727                 echo '<div class="error-box">API mismatch</div>';
       
   728                 break 2;
       
   729             }
       
   730             // Did we get invalid size data? If so the image is probably corrupt.
       
   731             if ( !$dimensions )
       
   732             {
       
   733               @unlink($tempfile);
       
   734               echo '<div class="error-box">' . $lang->get('usercp_avatar_corrupt_image') . '</div>';
       
   735               break;
       
   736             }
       
   737             // Is the image animated?
       
   738             if ( $is_animated && getConfig('avatar_enable_anim') !== '1' )
       
   739             {
       
   740               @unlink($tempfile);
       
   741               echo '<div class="error-box">' . $lang->get('usercp_avatar_disallowed_animation') . '</div>';
       
   742               break;
       
   743             }
       
   744             // Check image dimensions
       
   745             list($image_x, $image_y) = $dimensions;
       
   746             $max_x = intval(getConfig('avatar_max_width'));
       
   747             $max_y = intval(getConfig('avatar_max_height'));
       
   748             if ( $image_x > $max_x || $image_y > $max_y )
       
   749             {
       
   750               @unlink($tempfile);
       
   751               echo '<div class="error-box">' . $lang->get('usercp_avatar_too_large') . '</div>';
       
   752               break;
       
   753             }
       
   754             // All good!
       
   755             if ( rename($tempfile, $avi_path) )
       
   756             {
       
   757               $q = $db->sql_query('UPDATE ' . table_prefix . "users SET user_has_avatar = 1, avatar_type = '$file_type' WHERE user_id = {$session->user_id};");
       
   758               if ( !$q )
       
   759                 $db->_die('Avatar CP updating users table after successful avatar upload');
       
   760               $has_avi = 1;
       
   761               $avi_type = $file_type;
       
   762               echo '<div class="info-box">' . $lang->get('usercp_avatar_upload_success') . '</div>';
       
   763             }
       
   764             else
       
   765             {
       
   766               echo '<div class="error-box">' . $lang->get('usercp_avatar_move_failed') . '</div>';
       
   767             }
       
   768             break;
       
   769         }
       
   770       }
       
   771       
       
   772       ?>
       
   773       <script type="text/javascript">
       
   774       
       
   775         function avatar_select_field(elParent)
       
   776         {
       
   777           switch(elParent.value)
       
   778           {
       
   779             case 'keep':
       
   780             case 'remove':
       
   781               $('avatar_upload_http').object.style.display = 'none';
       
   782               $('avatar_upload_file').object.style.display = 'none';
       
   783               break;
       
   784             case 'set_http':
       
   785               $('avatar_upload_http').object.style.display = 'block';
       
   786               $('avatar_upload_file').object.style.display = 'none';
       
   787               break;
       
   788             case 'set_file':
       
   789               $('avatar_upload_http').object.style.display = 'none';
       
   790               $('avatar_upload_file').object.style.display = 'block';
       
   791               break;
       
   792           }
       
   793         }
       
   794       
       
   795       </script>
       
   796       <?php
       
   797       
       
   798       echo '<form action="' . makeUrl($paths->fullpage) . '" method="post" enctype="multipart/form-data">';
       
   799       echo '<div class="tblholder">';
       
   800       echo '<table border="0" cellspacing="1" cellpadding="4">';
       
   801       echo '<tr>
       
   802               <th colspan="2">
       
   803                 ' . $lang->get('usercp_avatar_table_title') . '
       
   804               </th>
       
   805             </tr>';
       
   806             
       
   807       echo '<tr>
       
   808               <td class="row2" style="width: 50%;">
       
   809                 ' . $lang->get('usercp_avatar_label_current') . '
       
   810               </td>
       
   811               <td class="row1" style="text-align: center;">';
       
   812               
       
   813       if ( $has_avi == 1 )
       
   814       {
       
   815         echo '<img alt="' . $lang->get('usercp_avatar_image_alt', array('username' => $session->username)) . '" src="' . make_avatar_url($session->user_id, $avi_type) . '" />';
       
   816       }
       
   817       else
       
   818       {
       
   819         echo $lang->get('usercp_avatar_image_none');
       
   820       }
       
   821       
       
   822       echo '    </td>
       
   823               </tr>';
       
   824               
       
   825       echo '  <tr>
       
   826                 <td class="row2">
       
   827                   ' . $lang->get('usercp_avatar_lbl_change') . '
       
   828                 </td>
       
   829                 <td class="row1">
       
   830                   <label><input type="radio" name="avatar_action" value="keep" onclick="avatar_select_field(this);" checked="checked" /> ' . $lang->get('usercp_avatar_lbl_keep') . '</label><br />
       
   831                   <label><input type="radio" name="avatar_action" value="remove" onclick="avatar_select_field(this);" /> ' . $lang->get('usercp_avatar_lbl_remove') . '</label><br />';
       
   832       if ( getConfig('avatar_upload_http') == '1' )
       
   833       {
       
   834         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 />
       
   835                   <div id="avatar_upload_http" style="display: none; margin: 10px 0 0 2.2em;">
       
   836                     ' . $lang->get('usercp_avatar_lbl_url') . ' <input type="text" name="avatar_http_url" size="40" value="http://" /><br />
       
   837                     <small>' . $lang->get('usercp_avatar_lbl_url_desc') . ' ' . $lang->get('usercp_avatar_limits') . '</small>
       
   838                   </div>';
       
   839       }
       
   840       else
       
   841       {
       
   842         echo '    <div id="avatar_upload_http" style="display: none;"></div>';
       
   843       }
       
   844       if ( getConfig('avatar_upload_file') == '1' )
       
   845       {
       
   846         echo '    <label><input type="radio" name="avatar_action" value="set_file" onclick="avatar_select_field(this);" /> ' . $lang->get('usercp_avatar_lbl_set_file') . '</label>
       
   847                   <div id="avatar_upload_file" style="display: none; margin: 10px 0 0 2.2em;">
       
   848                     ' . $lang->get('usercp_avatar_lbl_file') . ' <input type="file" name="avatar_file" size="40" /><br />
       
   849                     <small>' . $lang->get('usercp_avatar_lbl_file_desc') . ' ' . $lang->get('usercp_avatar_limits') . '</small>
       
   850                   </div>';
       
   851       }
       
   852       else
       
   853       {
       
   854         echo '    <div id="avatar_upload_file" style="display: none;"></div>';
       
   855       }
       
   856       echo '    </td>
       
   857               </tr>';
       
   858               
       
   859       echo '  <tr>
       
   860                 <th class="subhead" colspan="2">
       
   861                   <input type="submit" name="submit" value="' . $lang->get('etc_save_changes') . '" />
       
   862                 </th>
       
   863               </tr>';
       
   864               
       
   865       echo '</table>
       
   866             </div>';
       
   867       
       
   868       break;
   597     default:
   869     default:
   598       $good = false;
   870       $good = false;
   599       $code = $plugins->setHook('userprefs_body');
   871       $code = $plugins->setHook('userprefs_body');
   600       foreach ( $code as $cmd )
   872       foreach ( $code as $cmd )
   601       {
   873       {