/**
  * @see Page::readParameters()
  */
 public function readParameters()
 {
     parent::readParameters();
     // check permission
     WCF::getUser()->checkPermission('user.profile.canView');
     // get user
     if (isset($_REQUEST['userID'])) {
         $this->userID = intval($_REQUEST['userID']);
     }
     $this->user = new UserProfile($this->userID);
     if (!$this->user->userID || !$this->user->canViewProfile()) {
         throw new IllegalLinkException();
     }
 }
 /**
  * Initializes the profile.
  */
 public function init()
 {
     // call init event
     EventHandler::fireAction($this, 'init');
     // get user information
     if (!empty($this->userID)) {
         $this->user = new UserProfile($this->userID, null, null, null, $this->sqlSelects, $this->sqlJoins);
     } else {
         if (!empty($this->username)) {
             $this->user = new UserProfile(null, null, $this->username, null, $this->sqlSelects, $this->sqlJoins);
         } else {
             throw new IllegalLinkException();
         }
     }
     if (!$this->user->userID) {
         throw new IllegalLinkException();
     }
     $this->userID = $this->user->userID;
     $this->username = $this->user->username;
     // set page menu
     PageMenu::setActiveMenuItem('wcf.header.menu.memberslist');
     // set user profile menu
     UserProfileMenu::getInstance()->userID = $this->userID;
     // user symbols
     $this->initUserSymbols();
     // friends
     if (WCF::getUser()->userID) {
         if (WCF::getUser()->userID != $this->userID) {
             // get friend connection
             $connectionUserIDArray = array();
             if ($this->user->buddy) {
                 $connectionUserIDArray[] = WCF::getUser()->userID;
             } else {
                 if (WCF::getUser()->buddies) {
                     // one step
                     $sql = "SELECT\t\tfriends_2nd.userID\n\t\t\t\t\t\tFROM\t\twcf" . WCF_N . "_user_whitelist my_friends,\n\t\t\t\t\t\t\t\twcf" . WCF_N . "_user_whitelist friends_2nd\n\t\t\t\t\t\tWHERE\t\tmy_friends.userID = " . WCF::getUser()->userID . " AND my_friends.confirmed = 1\n\t\t\t\t\t\t\t\tAND friends_2nd.userID = my_friends.whiteUserID AND friends_2nd.confirmed = 1\n\t\t\t\t\t\t\t\tAND friends_2nd.whiteUserID = " . $this->userID;
                     $row = WCF::getDB()->getFirstRow($sql);
                     if (!empty($row['userID'])) {
                         $connectionUserIDArray[] = WCF::getUser()->userID;
                         $connectionUserIDArray[] = $row['userID'];
                     } else {
                         // two steps
                         $sql = "SELECT\t\tfriends_2nd.userID AS 1st, friends_3rd.userID AS 2nd\n\t\t\t\t\t\t\tFROM\t\twcf" . WCF_N . "_user_whitelist my_friends,\n\t\t\t\t\t\t\t\t\twcf" . WCF_N . "_user_whitelist friends_2nd,\n\t\t\t\t\t\t\t\t\twcf" . WCF_N . "_user_whitelist friends_3rd\n\t\t\t\t\t\t\tWHERE\t\tmy_friends.userID = " . WCF::getUser()->userID . " AND my_friends.confirmed = 1\n\t\t\t\t\t\t\t\t\tAND friends_2nd.userID = my_friends.whiteUserID AND friends_2nd.confirmed = 1\n\t\t\t\t\t\t\t\t\tAND friends_3rd.userID = friends_2nd.whiteUserID AND friends_3rd.confirmed = 1\n\t\t\t\t\t\t\t\t\tAND friends_3rd.whiteUserID = " . $this->userID;
                         $row = WCF::getDB()->getFirstRow($sql);
                         if (!empty($row['1st'])) {
                             $connectionUserIDArray[] = WCF::getUser()->userID;
                             $connectionUserIDArray[] = $row['1st'];
                             $connectionUserIDArray[] = $row['2nd'];
                         } else {
                             // three steps
                             $sql = "SELECT\t\tfriends_2nd.userID AS 1st, friends_3rd.userID AS 2nd, friends_4th.userID AS 3rd\n\t\t\t\t\t\t\t\tFROM\t\twcf" . WCF_N . "_user_whitelist my_friends,\n\t\t\t\t\t\t\t\t\t\twcf" . WCF_N . "_user_whitelist friends_2nd,\n\t\t\t\t\t\t\t\t\t\twcf" . WCF_N . "_user_whitelist friends_3rd,\n\t\t\t\t\t\t\t\t\t\twcf" . WCF_N . "_user_whitelist friends_4th\n\t\t\t\t\t\t\t\tWHERE\t\tmy_friends.userID = " . WCF::getUser()->userID . " AND my_friends.confirmed = 1\n\t\t\t\t\t\t\t\t\t\tAND friends_2nd.userID = my_friends.whiteUserID AND friends_2nd.confirmed = 1\n\t\t\t\t\t\t\t\t\t\tAND friends_3rd.userID = friends_2nd.whiteUserID AND friends_3rd.confirmed = 1\n\t\t\t\t\t\t\t\t\t\tAND friends_4th.userID = friends_3rd.whiteUserID AND friends_4th.confirmed = 1\n\t\t\t\t\t\t\t\t\t\tAND friends_4th.whiteUserID = " . $this->userID;
                             $row = WCF::getDB()->getFirstRow($sql);
                             if (!empty($row['1st'])) {
                                 $connectionUserIDArray[] = WCF::getUser()->userID;
                                 $connectionUserIDArray[] = $row['1st'];
                                 $connectionUserIDArray[] = $row['2nd'];
                                 $connectionUserIDArray[] = $row['3rd'];
                             }
                             /*else {
                             			// four steps
                             			$sql = "SELECT		friends_2nd.userID AS 1st, friends_3rd.userID AS 2nd, friends_4th.userID AS 3rd, friends_5th.userID AS 4th
                             				FROM		wcf".WCF_N."_user_whitelist my_friends,
                             						wcf".WCF_N."_user_whitelist friends_2nd,
                             						wcf".WCF_N."_user_whitelist friends_3rd,
                             						wcf".WCF_N."_user_whitelist friends_4th,
                             						wcf".WCF_N."_user_whitelist friends_5th
                             				WHERE		my_friends.userID = ".WCF::getUser()->userID." AND my_friends.confirmed = 1
                             						AND friends_2nd.userID = my_friends.whiteUserID AND friends_2nd.confirmed = 1
                             						AND friends_3rd.userID = friends_2nd.whiteUserID AND friends_3rd.confirmed = 1
                             						AND friends_4th.userID = friends_3rd.whiteUserID AND friends_4th.confirmed = 1
                             						AND friends_5th.userID = friends_4th.whiteUserID AND friends_5th.confirmed = 1
                             						AND friends_5th.whiteUserID = ".$this->userID;
                             			$row = WCF::getDB()->getFirstRow($sql);
                             			if (!empty($row['1st'])) {
                             				$connectionUserIDArray[] = WCF::getUser()->userID;
                             				$connectionUserIDArray[] = $row['1st'];
                             				$connectionUserIDArray[] = $row['2nd'];
                             				$connectionUserIDArray[] = $row['3rd'];
                             				$connectionUserIDArray[] = $row['4th'];
                             			}
                             		}*/
                         }
                     }
                 }
             }
             if (count($connectionUserIDArray) > 0) {
                 $sql = "SELECT\t\tavatar.*, user_table.*\n\t\t\t\t\t\tFROM\t\twcf" . WCF_N . "_user user_table\n\t\t\t\t\t\tLEFT JOIN \twcf" . WCF_N . "_avatar avatar\n\t\t\t\t\t\tON \t\t(avatar.avatarID = user_table.avatarID)\n\t\t\t\t\t\tWHERE\t\tuser_table.userID IN (" . implode(',', $connectionUserIDArray) . ")";
                 $result = WCF::getDB()->sendQuery($sql);
                 while ($row = WCF::getDB()->fetchArray($result)) {
                     $this->connection[array_search($row['userID'], $connectionUserIDArray)] = new UserProfile(null, $row);
                 }
                 // sorting
                 ksort($this->connection);
                 $this->connection = array_reverse($this->connection);
             }
         } else {
             // get network
             $sql = "SELECT\tCOUNT(*) AS count\n\t\t\t\t\tFROM\twcf" . WCF_N . "_user_whitelist\n\t\t\t\t\tWHERE\tuserID = " . $this->userID . "\n\t\t\t\t\t\tAND confirmed = 1";
             $row = WCF::getDB()->getFirstRow($sql);
             if ($row['count']) {
                 $this->network['friends'] = $row['count'];
                 $sql = "SELECT\t\tCOUNT(DISTINCT friends_2nd.whiteUserID) AS count\n\t\t\t\t\t\tFROM\t\twcf" . WCF_N . "_user_whitelist my_friends\n\t\t\t\t\t\tLEFT JOIN\twcf" . WCF_N . "_user_whitelist friends_2nd\n\t\t\t\t\t\tON\t\t(friends_2nd.userID = my_friends.whiteUserID AND friends_2nd.confirmed = 1)\n\t\t\t\t\t\tWHERE\t\tmy_friends.userID = " . $this->userID . "\n\t\t\t\t\t\t\t\tAND my_friends.confirmed = 1";
                 $row = WCF::getDB()->getFirstRow($sql);
                 if ($row['count']) {
                     $this->network['friendsOfFriends'] = $row['count'];
                     $sql = "SELECT\t\tCOUNT(DISTINCT friends_3rd.whiteUserID) AS count\n\t\t\t\t\t\t\tFROM\t\twcf" . WCF_N . "_user_whitelist my_friends\n\t\t\t\t\t\t\tLEFT JOIN\twcf" . WCF_N . "_user_whitelist friends_2nd\n\t\t\t\t\t\t\tON\t\t(friends_2nd.userID = my_friends.whiteUserID AND friends_2nd.confirmed = 1)\n\t\t\t\t\t\t\tLEFT JOIN\twcf" . WCF_N . "_user_whitelist friends_3rd\n\t\t\t\t\t\t\tON\t\t(friends_3rd.userID = friends_2nd.whiteUserID AND friends_3rd.confirmed = 1)\n\t\t\t\t\t\t\tWHERE\t\tmy_friends.userID = " . $this->userID . "\n\t\t\t\t\t\t\t\t\tAND my_friends.confirmed = 1";
                     $row = WCF::getDB()->getFirstRow($sql);
                     if ($row['count']) {
                         $this->network['friends3rdGrade'] = $row['count'];
                     } else {
                         $this->network['friends3rdGrade'] = 0;
                     }
                 } else {
                     $this->network['friendsOfFriends'] = 0;
                 }
             }
         }
     }
     // check permission
     WCF::getUser()->checkPermission('user.profile.canView');
     try {
         if ($this->user->ignoredUser) {
             throw new NamedUserException(WCF::getLanguage()->getDynamicVariable('wcf.user.profile.error.ignoredUser', array('username' => $this->user->username)));
         }
         if (!$this->user->canViewProfile()) {
             throw new NamedUserException(WCF::getLanguage()->getDynamicVariable('wcf.user.profile.error.protectedProfile', array('username' => $this->user->username)));
         }
     } catch (NamedUserException $e) {
         WCF::getTPL()->assign(array('user' => $this->user, 'userID' => $this->userID, 'connection' => $this->connection, 'network' => $this->network, 'userSymbols' => $this->userSymbols, 'errorMessage' => $e->getMessage()));
         WCF::getTPL()->display('userProfileAccessDenied');
         exit;
     }
 }