/**
  * @see Form::validate()
  */
 public function validate()
 {
     parent::validate();
     if ($this->avatarID) {
         if ($this->avatarID == -1) {
             if (empty($this->gravatar)) {
                 // check permission
                 WCF::getUser()->checkPermission('user.profile.avatar.canUploadAvatar');
                 // upload or download avatar
                 if ($this->avatarUpload && $this->avatarUpload['error'] != 4) {
                     if ($this->avatarUpload['error'] != 0) {
                         throw new UserInputException('avatarUpload', 'uploadFailed');
                     }
                     $this->avatarID = AvatarEditor::create($this->avatarUpload['tmp_name'], $this->avatarUpload['name'], 'avatarUpload', WCF::getUser()->userID);
                 } else {
                     if ($this->avatarURL != 'http://') {
                         if (StringUtil::indexOf($this->avatarURL, 'http://') !== 0) {
                             throw new UserInputException('avatarURL', 'downloadFailed');
                         }
                         try {
                             $tmpName = FileUtil::downloadFileFromHttp($this->avatarURL, 'avatar');
                         } catch (SystemException $e) {
                             throw new UserInputException('avatarURL', 'downloadFailed');
                         }
                         $this->avatarID = AvatarEditor::create($tmpName, $this->avatarURL, 'avatarURL', WCF::getUser()->userID);
                     } else {
                         throw new UserInputException('avatarUpload');
                     }
                 }
             } else {
                 $this->avatarID = 0;
             }
         } else {
             // check permission
             WCF::getUser()->checkPermission('user.profile.avatar.canUseDefaultAvatar');
             // use a default avatar
             $avatar = new AvatarEditor($this->avatarID);
             if (!$avatar->avatarID || $avatar->userID || $avatar->groupID && !in_array($avatar->groupID, WCF::getUser()->getGroupIDs()) || $avatar->neededPoints > WCF::getUser()->activityPoints) {
                 throw new UserInputException('availableAvatars', 'invalid');
             }
             // check category permissions
             if ($avatar->avatarCategoryID) {
                 $category = new AvatarCategory($avatar->avatarCategoryID);
                 if ($category->groupID && !in_array($category->groupID, WCF::getUser()->getGroupIDs()) || $category->neededPoints > WCF::getUser()->activityPoints) {
                     throw new UserInputException('availableAvatars', 'invalid');
                 }
             }
         }
     }
 }
 /**
  * @see Form::validate()
  */
 public function validate()
 {
     parent::validate();
     // validate group id
     $group = new Group($this->groupID);
     if (!$group->groupID) {
         throw new UserInputException('groupID');
     }
     // category
     if ($this->avatarCategoryID != 0) {
         $avatarCategory = new AvatarCategory($this->avatarCategoryID);
         if (!$avatarCategory->avatarCategoryID) {
             throw new UserInputException('avatarCategoryID');
         }
     }
     $savedAvatars = 0;
     WCF::getTPL()->assignByRef('savedAvatars', $savedAvatars);
     // upload avatar(s)
     if ($this->upload && $this->upload['error'] != 4) {
         if ($this->upload['error'] != 0) {
             throw new UserInputException('upload', 'uploadFailed');
         }
         // try to open file as an archive
         if (preg_match('/(?:tar\\.gz|tgz|tar)$/i', $this->upload['name'])) {
             $errors = array();
             $tar = new Tar($this->upload['tmp_name']);
             foreach ($tar->getContentList() as $file) {
                 if ($file['type'] != 'folder') {
                     // extract to tmp dir
                     $tmpname = FileUtil::getTemporaryFilename('avatar_');
                     $tar->extract($file['index'], $tmpname);
                     try {
                         $this->avatarIDs[] = AvatarEditor::create($tmpname, $file['filename'], 'upload', 0, $this->groupID, $this->neededPoints, $this->avatarCategoryID);
                         $savedAvatars++;
                     } catch (UserInputException $e) {
                         $errors[] = array('filename' => $file['filename'], 'errorType' => $e->getType());
                     }
                 }
             }
             $tar->close();
             @unlink($this->upload['tmp_name']);
             if (count($errors)) {
                 throw new UserInputException('upload', $errors);
             } else {
                 if ($savedAvatars == 0) {
                     throw new UserInputException('upload', 'emptyArchive');
                 }
             }
         } else {
             // import as image file
             $this->avatarIDs[] = AvatarEditor::create($this->upload['tmp_name'], $this->upload['name'], 'upload', 0, $this->groupID, $this->neededPoints, $this->avatarCategoryID);
             $savedAvatars++;
         }
     } else {
         if (!empty($this->filename)) {
             if (!file_exists($this->filename)) {
                 throw new UserInputException('filename', 'notFound');
             }
             // copy avatars from a dir
             if (is_dir($this->filename)) {
                 $errors = array();
                 $this->filename = FileUtil::addTrailingSlash($this->filename);
                 $handle = opendir($this->filename);
                 while (($file = readdir($handle)) !== false) {
                     if ($file != '.' && $file != '..' && is_file($this->filename . $file)) {
                         try {
                             $this->avatarIDs[] = AvatarEditor::create($this->filename . $file, $this->filename . $file, 'filename', 0, $this->groupID, $this->neededPoints, $this->avatarCategoryID);
                             $savedAvatars++;
                         } catch (UserInputException $e) {
                             $errors[] = array('filename' => $this->filename . $file, 'errorType' => $e->getType());
                         }
                     }
                 }
                 if (count($errors)) {
                     throw new UserInputException('filename', $errors);
                 } else {
                     if ($savedAvatars == 0) {
                         throw new UserInputException('filename', 'emptyFolder');
                     }
                 }
             } else {
                 $this->avatarIDs[] = AvatarEditor::create($this->filename, $this->filename, 'filename', 0, $this->groupID, $this->neededPoints, $this->avatarCategoryID);
                 $savedAvatars++;
             }
         } else {
             throw new UserInputException('upload');
         }
     }
 }
 /**
  * @see EventListener::execute()
  */
 public function execute($eventObj, $className, $eventName)
 {
     if (MODULE_AVATAR == 1) {
         if ($eventName == 'readFormParameters') {
             if (isset($_POST['avatarID'])) {
                 $this->avatarID = intval($_POST['avatarID']);
             }
             if (isset($_POST['disableAvatar'])) {
                 $this->disableAvatar = intval($_POST['disableAvatar']);
             }
             if (isset($_POST['disableAvatarReason'])) {
                 $this->disableAvatarReason = $_POST['disableAvatarReason'];
             }
             if (isset($_POST['useAvatar'])) {
                 $this->useAvatar = intval($_POST['useAvatar']);
             }
             if (isset($_POST['avatarURL'])) {
                 $this->avatarURL = StringUtil::trim($_POST['avatarURL']);
             }
             if (isset($_FILES['avatarUpload'])) {
                 $this->avatarUpload = $_FILES['avatarUpload'];
             }
             if (MODULE_GRAVATAR == 1 && isset($_POST['gravatar'])) {
                 $this->gravatar = StringUtil::trim($_POST['gravatar']);
             }
         } else {
             if ($eventName == 'validate') {
                 try {
                     if ($this->useAvatar == 1) {
                         if (empty($this->gravatar)) {
                             // upload or download avatar
                             if ($this->avatarUpload && $this->avatarUpload['error'] != 4) {
                                 if ($this->avatarUpload['error'] != 0) {
                                     throw new UserInputException('avatarUpload', 'uploadFailed');
                                 }
                                 $this->avatarID = AvatarEditor::create($this->avatarUpload['tmp_name'], $this->avatarUpload['name'], 'avatarUpload', $eventObj->userID);
                             } else {
                                 if ($this->avatarURL != 'http://') {
                                     if (StringUtil::indexOf($this->avatarURL, 'http://') !== 0) {
                                         throw new UserInputException('avatarURL', 'downloadFailed');
                                     }
                                     try {
                                         $tmpName = FileUtil::downloadFileFromHttp($this->avatarURL, 'avatar');
                                     } catch (SystemException $e) {
                                         throw new UserInputException('avatarURL', 'downloadFailed');
                                     }
                                     $this->avatarID = AvatarEditor::create($tmpName, $this->avatarURL, 'avatarURL', $eventObj->userID);
                                 } else {
                                     $this->avatarID = $eventObj->user->avatarID;
                                 }
                             }
                         }
                     } else {
                         if ($this->useAvatar == 2) {
                             // use a default avatar
                             $avatar = new AvatarEditor($this->avatarID);
                             if (!$avatar->avatarID || $avatar->userID || $avatar->groupID && !in_array($avatar->groupID, $eventObj->user->getGroupIDs()) || $avatar->neededPoints > $eventObj->user->activityPoints) {
                                 throw new UserInputException('availableAvatars', 'invalid');
                             }
                         } else {
                             $this->avatarID = 0;
                         }
                     }
                 } catch (UserInputException $e) {
                     $eventObj->errorType[$e->getField()] = $e->getType();
                 }
             } else {
                 if ($eventName == 'save') {
                     // delete old avatar if necessary
                     if ($eventObj->user->avatarID) {
                         $currentAvatar = new AvatarEditor($eventObj->user->avatarID);
                         if ($currentAvatar->userID && $this->avatarID != $currentAvatar->avatarID) {
                             $currentAvatar->delete();
                         }
                     }
                     // update user
                     $eventObj->additionalFields['avatarID'] = $this->avatarID;
                     $eventObj->additionalFields['disableAvatar'] = $this->disableAvatar;
                     $eventObj->additionalFields['disableAvatarReason'] = $this->disableAvatarReason;
                     $eventObj->additionalFields['gravatar'] = $this->gravatar;
                 } else {
                     if ($eventName == 'show') {
                         // get default values
                         if (!count($_POST)) {
                             $this->avatarID = $eventObj->user->avatarID;
                             $this->disableAvatar = $eventObj->user->disableAvatar;
                             $this->disableAvatarReason = $eventObj->user->disableAvatarReason;
                             $this->gravatar = $eventObj->user->gravatar;
                         }
                         $currentAvatar = null;
                         if ($this->avatarID) {
                             $currentAvatar = new AvatarEditor($this->avatarID);
                             $this->useAvatar = $currentAvatar->userID ? 1 : 2;
                         } else {
                             if ($this->gravatar) {
                                 require_once WCF_DIR . 'lib/data/user/avatar/Gravatar.class.php';
                                 $currentAvatar = new Gravatar($this->gravatar);
                                 $this->useAvatar = 1;
                             }
                         }
                         $availableAvatarCategories = $this->getAvailableAvatars(implode(',', $eventObj->user->getGroupIDs()), intval($eventObj->user->activityPoints));
                         $avatarCount = 0;
                         foreach ($availableAvatarCategories as $availableAvatarCategory) {
                             $avatarCount += count($availableAvatarCategory['avatars']);
                         }
                         WCF::getTPL()->assign(array('avatarID' => $this->avatarID, 'disableAvatar' => $this->disableAvatar, 'disableAvatarReason' => $this->disableAvatarReason, 'avatarURL' => $this->avatarURL, 'currentAvatar' => $currentAvatar, 'avatarCategories' => $availableAvatarCategories, 'items' => $avatarCount, 'useAvatar' => $this->useAvatar, 'gravatar' => $this->gravatar));
                         WCF::getTPL()->append(array('additionalTabs' => '<li id="avatar"><a onclick="tabMenu.showSubTabMenu(\'avatar\');"><span>' . WCF::getLanguage()->get('wcf.user.avatar') . '</span></a></li>', 'additionalTabContents' => WCF::getTPL()->fetch('userEditAvatar')));
                     }
                 }
             }
         }
     }
 }