<?php
// +---------------------------------------------+
// |     Copyright  2003-2006 weenCompany        |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

if(!defined('IN_WEENCOMPANY'))
{
  die("File not found!");
}


function m10_LoginPanel()
{
  global $DB, $categoryid, $dbname, $userinfo, $weenurl, $inputsize,
         $usersystem, $loginerrors;

  // forum information
  $forumdbname = $usersystem['dbname'];
  $forumname   = $usersystem['name'];
  $forumpath   = $usersystem['folderpath'];
  $tableprefix = $usersystem['tblprefix'];

  // get settings
  $getsettings = $DB->query("SELECT title, value FROM " . TABLE_PREFIX . "modulesettings WHERE moduleid = 10");
  while($setting = $DB->fetch_array($getsettings))
  {
    $settings[$setting['title']] = $setting['value'];
  }

  if($userinfo['loggedin'] == 1 AND $settings['显示头像'] == 1)
  {
    // switch to forum database
    if($dbname != $forumdbname)
      $DB->select_db($forumdbname);

    // get users avatar
    if($forumname == 'Discuz5')
    {
      // get avatar settings
      $useravatar = $DB->query_first("SELECT avatar FROM " . $tableprefix . "memberfields
                                     WHERE uid = '" . $userinfo['userid'] . "' ");

      if(strlen($useravatar['avatar']))
      {
         $avatar = '<img alt="avatar" src="' . $weenurl . $forumpath . $useravatar['avatar'] . '" />';
        }

    }
    
    else if($forumname == 'vBulletin 2')
    {
      $useravatar = $DB->query_first("SELECT userid, avatarid FROM " . $tableprefix . "user WHERE username = '" . addslashes($userinfo['username']) . "'");

      // custom avatar?
      if( ($useravatar['avatarid'] == 0) AND ($customavatar = $DB->query_first("SELECT * FROM " . $tableprefix . "customavatar WHERE userid = '" . $useravatar['userid'] . "'")) )
      {
        $avatar = '<img alt="avatar" src="./modules/m10_loginpanel/avatar.php?userid=' . $useravatar['userid'] . '" />';
      }
      else if($useravatar['avatarid'] != 0)
      {
        $getavatar = $DB->query_first("SELECT avatarpath FROM " . $tableprefix . "avatar WHERE avatarid = '" . $useravatar['avatarid'] . "'");
        $avatar = '<img alt="avatar" src="' . $weenurl . $forumpath . $getavatar['avatarpath'] . '" />';
      }
    }
	
	else if($forumname == 'vBulletin 3')
    {
      $ver = $DB->query_first("SELECT value FROM " . $tableprefix . "setting WHERE varname='templateversion'");

      $version = explode('.', $ver[0]);

          if($version[0] == 3)
          {
            if($version[1] < 5)
            {
                    $query = "SELECT user.avatarid, user.avatarrevision, avatarpath, NOT ISNULL(avatardata) AS hascustom, customavatar.dateline
                    FROM " . $tableprefix . "user AS user
                    LEFT JOIN " . $tableprefix . "avatar AS avatar ON avatar.avatarid = user.avatarid
                    LEFT JOIN " . $tableprefix . "customavatar AS customavatar ON customavatar.userid = user.userid
                    WHERE user.userid = " . $userinfo['userid'];
            }
            else
            {
              $query = "SELECT user.avatarid, user.avatarrevision, avatarpath, NOT ISNULL(filedata) AS hascustom, customavatar.dateline
                    FROM " . $tableprefix . "user AS user
                    LEFT JOIN " . $tableprefix . "avatar AS avatar ON avatar.avatarid = user.avatarid
                    LEFT JOIN " . $tableprefix . "customavatar AS customavatar ON customavatar.userid = user.userid
                    WHERE user.userid = " . $userinfo['userid'];
            }
          }
          else
          {
                  echo 'This doesn\'t appear to be vBulletin 3. The version number is ' . $ver[0];
          }

      if ($avatarinfo = $DB->query_first($query))
          {
                if (!empty($avatarinfo['avatarpath']))
                {
                        $avatar = '<img alt="avatar" src="' . $weenurl . $forumpath . $avatarinfo['avatarpath'] . '">';
                }
                else if ($avatarinfo['hascustom'])
                {
                  $usefileavatar = $DB->query_first("SELECT value FROM " . $tableprefix . "setting WHERE varname='usefileavatar'");
          if(isset($usefileavatar[0]) && $usefileavatar[0])
          {
            $avatarurl = $DB->query_first("SELECT value FROM " . $tableprefix . "setting WHERE varname='avatarurl'");
            $avatar = '<img alt="avatar" src="' . $weenurl . $forumpath . $avatarurl[0] . '/avatar' . $userinfo['userid'] . '_' . $avatarinfo['avatarrevision'] . '.gif" />';
                  }
                  else
                  {
                        $avatar = '<img alt="avatar" src="' . $weenurl . $forumpath . 'image.php?u=' . $userinfo['userid'] . '&amp;dateline=' . $avatarinfo['dateline'] . '">';
                  }
                }
          }
    }
    else if($forumname == 'phpBB2')
    {
      // get avatar settings
      $avatarpath        = $DB->query_first("SELECT config_value FROM " . $tableprefix . "config WHERE config_name = 'avatar_path'");
      $avatargallerypath = $DB->query_first("SELECT config_value FROM " . $tableprefix . "config WHERE config_name = 'avatar_gallery_path'");

      $useravatar = $DB->query_first("SELECT user_avatar, user_avatar_type FROM " . $tableprefix . "users
                                     WHERE username = '" . addslashes($userinfo['username']) . "' ");

      if(strlen($useravatar['user_avatar']))
      {
        switch($useravatar['user_avatar_type'])
        {
          case 1:  // uploaded avatar in avatars folder
            $avatar = '<img alt="avatar" src="' . $weenurl . $forumpath . $avatarpath['config_value'] . '/' . $useravatar['user_avatar'] . '" />';
          break;

          case 2:  // an external link to an avatar (offsite)
            $avatar = '<img alt="avatar" src="' . $useravatar['user_avatar'] . '" />';
          break;

          case 3:  // uploaded avatar in the avatars -> gallery folder
            $avatar = '<img alt="avatar" src="' . $weenurl . $forumpath . $avatargallerypath['config_value'] . '/' . $useravatar['user_avatar'] . '" />';
          break;
        }
      }

    }
    else if($forumname == 'Invision Power Board 2')
    {
      $useravatar = $DB->query_first("SELECT avatar_location, avatar_size, avatar_type FROM " . $tableprefix . "member_extra WHERE id = '" . $userinfo['userid'] . "'");

      if($useravatar['avatar_type'] == 'local' AND strlen($useravatar['avatar_location']))
      {
        $avatar = '<img alt="avatar" src="' . $weenurl . $forumpath . 'style_avatars/' . $useravatar['avatar_location'] . '" />';
      }
      else if($useravatar['avatar_type'] == 'url')
      {
        $avatar = '<img alt="avatar" src="' . $useravatar['avatar_location'] . '" />';
      }
      else if($useravatar['avatar_type'] == 'upload')
      {
        $avatar = '<img alt="avatar" src="' . $weenurl . $forumpath . 'uploads/' . $useravatar['avatar_location'] . '" />';
      }
    }
    else if($forumname == 'Simple Machines Forum 1')
    {
      $useravatar = $DB->query_first("SELECT avatar FROM " . $tableprefix . "members WHERE ID_MEMBER = '" . $userinfo['userid'] . "'");

      if(substr($useravatar['avatar'], 0, 4) == 'http')
      {
        // user entered url
        $avatar = '<img alt="avatar" src="' . $useravatar['avatar'] . '" />';
      }
      else if(strlen($useravatar['avatar']))
      {
        // selected avatar from forum avatars folder
        $avatar = '<img alt="avatar" src="' . $weenurl . $forumpath . 'avatars/' . $useravatar['avatar'] . '" / >';
      }
      else if($attachment = $DB->query_first("SELECT filename FROM " . $tableprefix . "attachments WHERE ID_MEMBER = " . $userinfo['userid']))
      {
        // user uploaded avatar
        $avatar = '<img alt="avatar" src="' . $weenurl . $forumpath . 'attachments/' . $attachment['filename'] . '" / >';
      }
    }

    // switch back to weencompany database
    if($dbname != $forumdbname)
      $DB->select_db($dbname);

  }

  // Get Language
  $language = GetLanguage(10);

  $regpath = ForumLink(1);
  $cppath = ForumLink(2);
  $pwdpath = ForumLink(3);

  // $cppath has already gone through rewritelink (or $weenurl added) in the integration files
  $myaccount = '<a href="' . $cppath . '">' . $language['my_account'] . '</a>';

  if($usersystem)
  {
    if($userinfo['loggedin'])
    {
      echo '<table border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td>' . $language['welcome_back'] . ' ' . $userinfo['username'] . '</td>
            </tr>';

      if(isset($avatar))
      {
        echo '<tr>
                <td><br />' . $avatar . '</td>
              </tr>';
      }

      echo '<tr>
              <td style="padding-bottom: 8px;"><br />'.iif(strlen($cppath) > 0, $myaccount, '').'</td>
            </tr>';

      if($settings['管理链接'] && ($userinfo['adminaccess'] OR count($userinfo['moduleadminids']) > 0))
      {
              echo '<tr>
              <td style="padding-bottom: 8px;"><a href="' . RewriteLink(ADMIN_DIR.'/index.php') . '">' . $language['admin_panel'] . '</a></td>
            </tr>';
      }

      echo '<tr>
              <td style="padding-bottom: 8px;"><a href="' . RewriteLink('index.php?logout=1') . '">' . $language['logout'] . '</a></td>
            </tr>';

      if($settings['短信统计'] == 1)
      {
        echo '<tr>
                <td><br/>';

        require('./modules/m10_loginpanel/pminfo.php');

        echo '    </td>
            </tr>';
      }

      echo '</table>';
    }
    else
    {
      if(count($loginerrors))
      {
        foreach($loginerrors as $key => $value)
        {
          echo $value . '<br />';
        }
      }

      echo '<form action="' . RewriteLink('index.php?categoryid=' . $categoryid) . '" method="post">
            <input type="hidden" name="login" value="login" />
            <table border="0" cellspacing="0" cellpadding="0">
            <tr><td style="padding-bottom: 4px;">' . $language['username'] . '</td></tr>
            <tr><td style="padding-bottom: 4px;"><input name="loginusername" type="text" maxlength="58" size="'.$inputsize.'" /></td></tr>
            <tr><td style="padding-bottom: 4px;">' . $language['password'] . '</td></tr>
            <tr><td style="padding-bottom: 4px;"><input name="loginpassword" type="password" maxlength="58" size="'.$inputsize.'" /></td></tr>
            <tr><td style="padding-bottom: 4px;"><input class="checkbox" type="checkbox" name="rememberme" checked="checked" /> ' . $language['remember_me'] . '</td></tr>
            <tr><td><input type="submit" name="Submit now" value=" ' . strip_tags($language['login']) . ' " />';if(isset($regpath)){
        echo '&nbsp&nbsp<a href="' . $pwdpath . '">' . $language['forgot_password'] . '</a>';} echo '</td></tr>';


      // $regpath has already gone through rewritelink (or $weenurl added) in the integration files
      if(isset($regpath))
      {
        echo '<tr>
                <td><br />' . $language['not_registered'] . '&nbsp&nbsp<a href="' . $regpath . '">' . $language['register_now'] . '</a></td>
              </tr>';
      }

      echo '</table>
            </form>';
    }
  }
  else
  {
    echo 'User System is turned off. Please turn it on to display the login panel.';
  }

}



m10_LoginPanel();


?>