public static function getTitle($name, $html = false)
 {
     $item = 'wcf.sketchbook.sketchTitles.' . self::nameToLangVar($name);
     $value = WCF::getLanguage()->getDynamicVariable($item);
     $new = false;
     if ($value == $title) {
         $value = StringUtil::substring($title, StringUtil::lastIndexOf($title, '.'));
         $new = true;
     }
     if ($html) {
         return '<span class="sketch' . ($new ? ' sketchNew' : '') . '">' . StringUtil::encodeHTML($title) . '</span>';
     }
     return $title;
 }
 /**
  * Returns the right file type icon for the given attachment.
  * 
  * @return	string
  */
 public function getFileTypeIcon()
 {
     if ($this->fileTypeIcon === null) {
         $this->fileTypeIcon = '';
         // get file extension
         $extension = StringUtil::firstCharToUpperCase(StringUtil::toLowerCase(StringUtil::substring($this->attachmentName, StringUtil::lastIndexOf($this->attachmentName, '.') + 1)));
         // get file type icon
         if (file_exists(WCF_DIR . 'icon/fileTypeIcon' . $extension . 'M.png')) {
             $this->fileTypeIcon = 'fileTypeIcon' . $extension . 'M.png';
         } else {
             foreach (self::$fileTypeGroups as $key => $group) {
                 if (in_array($extension, $group)) {
                     $this->fileTypeIcon = 'fileTypeIcon' . $key . 'M.png';
                     break;
                 }
             }
             if (empty($this->fileTypeIcon)) {
                 $this->fileTypeIcon = 'fileTypeIconDefaultM.png';
             }
         }
     }
     if (!class_exists('StyleManager')) {
         return RELATIVE_WCF_DIR . 'icon/' . $this->fileTypeIcon;
     } else {
         return StyleManager::getStyle()->getIconPath($this->fileTypeIcon);
     }
 }
 /**
  * Handles a request on the attachment edit form.
  * Deletes old or uploads new attachments.
  */
 public function handleRequest()
 {
     // delete uploaded attachments
     if (isset($_POST['delete']) && is_array($_POST['delete']) && count($_POST['delete'])) {
         // delete selected attachments
         $keys = array_keys($_POST['delete']);
         $this->delete(intval(array_shift($keys)));
     }
     // move uploaded attachments
     if (isset($_POST['attachmentListPositions']) && is_array($_POST['attachmentListPositions'])) {
         $positionChanged = false;
         $positions = ArrayUtil::toIntegerArray($_POST['attachmentListPositions']);
         foreach ($positions as $attachmentID => $position) {
             $attachmentID = intval($attachmentID);
             if (isset($this->attachments[$this->messageID][$attachmentID]) && $this->attachments[$this->messageID][$attachmentID]['showOrder'] != $position) {
                 $this->attachments[$this->messageID][$attachmentID]['showOrder'] = $position;
                 $sql = "UPDATE\twcf" . WCF_N . "_attachment\n\t\t\t\t\t\tSET\tshowOrder = " . $position . "\n\t\t\t\t\t\tWHERE\tattachmentID = " . $attachmentID;
                 WCF::getDB()->registerShutdownUpdate($sql);
                 $positionChanged = true;
             }
         }
         if ($positionChanged) {
             uasort($this->attachments[$this->messageID], array('self', 'compareAttachments'));
         }
     }
     // upload new attachments
     if (isset($_FILES) && count($_FILES) && isset($_FILES['upload'])) {
         // upload new attachments
         for ($x = 0, $y = count($_FILES['upload']['name']); $x < $y; $x++) {
             $attachment = array();
             $attachment['attachmentName'] = $_FILES['upload']['name'][$x];
             if ($attachment['attachmentName']) {
                 $attachment['attachment'] = $_FILES['upload']['tmp_name'][$x];
                 $attachment['attachmentSize'] = $_FILES['upload']['size'][$x];
                 $attachment['sha1Hash'] = sha1_file($attachment['attachment']);
                 $attachment['attachmentExtension'] = StringUtil::toLowerCase(StringUtil::substring($attachment['attachmentName'], StringUtil::lastIndexOf($attachment['attachmentName'], '.') + 1));
                 $attachment['fileType'] = $_FILES['upload']['type'][$x];
                 $attachment['isImage'] = 0;
                 if (strchr($attachment['fileType'], 'image')) {
                     // check mime
                     $attachment['fileType'] = 'application/octet-stream';
                     if (($imageData = @getImageSize($attachment['attachment'])) !== false) {
                         if (strchr($imageData['mime'], 'image')) {
                             $attachment['fileType'] = $imageData['mime'];
                             if ($attachment['fileType'] == 'image/bmp') {
                                 $attachment['fileType'] = 'image/x-ms-bmp';
                             }
                             $attachment['isImage'] = 1;
                         }
                     }
                 }
                 $attachment['showOrder'] = (isset($this->attachments[$this->messageID]) ? count($this->attachments[$this->messageID]) : 0) + 1;
                 if ($this->checkAttachment($attachment['attachment'], $attachment['attachmentName'] . ':' . $attachment['sha1Hash'], $attachment['attachmentName'], $attachment['attachmentSize'], $attachment['attachmentExtension'], $attachment['isImage'])) {
                     $attachment['messageID'] = $this->messageID;
                     $attachment['idHash'] = $this->idHash;
                     $attachment['userID'] = WCF::getUser()->userID;
                     $attachment['uploadTime'] = TIME_NOW;
                     $attachment['thumbnailType'] = '';
                     if ($this->setAttachment($attachment)) {
                         $this->attachmentHashes[count($this->attachmentHashes)] = $attachment['attachmentName'] . ':' . $attachment['sha1Hash'];
                         $attachment['fileTypeIcon'] = $this->getFileTypeIcon($attachment);
                         $this->attachments[$this->messageID][$attachment['attachmentID']] = $attachment;
                     }
                 }
             }
         }
     }
     $this->assign();
     if (count($this->errors)) {
         // throw user exception
         throw new UserInputException('attachments', $this->errors);
     }
 }
 /**
  * Returns the right file type icon for the given attachment.
  * 
  * @param	array		$data
  * @return	string
  */
 protected static function getFileTypeIcon($data)
 {
     // get file extension
     $extension = StringUtil::firstCharToUpperCase(StringUtil::toLowerCase(StringUtil::substring($data['attachmentName'], StringUtil::lastIndexOf($data['attachmentName'], '.') + 1)));
     // get file type icon
     if (file_exists(WCF_DIR . 'icon/fileTypeIcon' . $extension . 'M.png')) {
         return StyleManager::getStyle()->getIconPath('fileTypeIcon' . $extension . 'M.png');
     } else {
         foreach (self::$fileTypeGroups as $key => $group) {
             if (in_array($extension, $group)) {
                 return StyleManager::getStyle()->getIconPath('fileTypeIcon' . $key . 'M.png');
             }
         }
         return StyleManager::getStyle()->getIconPath('fileTypeIconDefaultM.png');
     }
 }
 public function getAttachments($userID, $sortField, $sortOrder, $itemsPerPage, $pageNo, $isACP = false, $showThumbnails = 0, $showOnlyImages = 0, $showOnlyMessageType = '', $showOnlyFileType = '')
 {
     $ret = array();
     $i = 0;
     if ($userID > 0) {
         $sortField2 = '';
         if ($sortField == 'username') {
             $sortField = 'uploadTime';
         }
         if ($sortField != 'attachmentName') {
             $sortField2 .= ', LOWER(attachmentName) ASC';
         }
         $sql = "SELECT *" . "\n  FROM wcf" . WCF_N . "_attachment" . "\n WHERE 1 = 1" . "\n   AND userID = " . $userID;
         if (!empty($showOnlyImages)) {
             $sql .= "\n   AND isImage = 1";
         }
         if (!empty($showOnlyMessageType)) {
             $sql .= "\n   AND messageType = '" . $showOnlyMessageType . "'";
         }
         if (!empty($showOnlyFileType)) {
             $sql .= "\n   AND fileType = '" . $showOnlyFileType . "'";
         }
         $sql .= "\n ORDER BY " . $sortField . " " . $sortOrder . $sortField2 . "\n LIMIT " . $itemsPerPage . "\nOFFSET " . ($pageNo - 1) * $itemsPerPage;
     } else {
         if (!WCF::getUser()->getPermission('admin.general.attachmentManager.canView')) {
             return $ret;
         }
         $sortField2 = '';
         if ($sortField == 'username') {
             $sortField = 'LOWER(' . $sortField . ')';
         } else {
             $sortField2 .= ', LOWER(username) ASC';
         }
         if ($sortField != 'attachmentName') {
             $sortField2 .= ', LOWER(attachmentName) ASC';
         }
         $sql = "SELECT *" . "\n  FROM wcf" . WCF_N . "_attachment at" . "\n  LEFT JOIN wcf" . WCF_N . "_user us ON (us.userID = at.userID)" . "\n WHERE 1 = 1";
         if (!empty($showOnlyImages)) {
             $sql .= "\n   AND isImage = 1";
         }
         if (!empty($showOnlyMessageType)) {
             $sql .= "\n   AND messageType = '" . $showOnlyMessageType . "'";
         }
         if (!empty($showOnlyFileType)) {
             $sql .= "\n   AND fileType = '" . $showOnlyFileType . "'";
         }
         $sql .= "\n ORDER BY " . $sortField . " " . $sortOrder . $sortField2 . "\n LIMIT " . $itemsPerPage . "\nOFFSET " . ($pageNo - 1) * $itemsPerPage;
     }
     $result = WCF::getDB()->sendQuery($sql);
     while ($row = WCF::getDB()->fetchArray($result)) {
         // username
         if (self::wbbExists() && empty($row['username']) && $row['messageType'] == 'post') {
             $tmp = WCF::getDB()->getFirstRow('SELECT username FROM wbb' . WBB_N . '_post WHERE postID = ' . $row['messageID']);
             if (isset($tmp['username'])) {
                 $row['username'] = $tmp['username'];
             }
         } else {
             if (empty($row['username']) && $row['messageType'] == 'pm') {
                 $tmp = WCF::getDB()->getFirstRow('SELECT username FROM wcf' . WCF_N . '_pm WHERE pmID = ' . $row['messageID']);
                 if (isset($tmp['username'])) {
                     $row['username'] = $tmp['username'];
                 }
             }
         }
         if (!empty($row['username'])) {
             $row['username'] = StringUtil::encodeHTML($row['username']);
         } else {
             $row['username'] = '******';
         }
         if (!empty($row['userID'])) {
             $row['username'] = '******' . $row['userID'] . '&packageID=' . PACKAGE_ID . SID_ARG_2ND_NOT_ENCODED . '" title="' . WCF::getLanguage()->get('wcf.acp.user.edit') . '">' . $row['username'] . '</a>';
         }
         // pm
         $ownPM = false;
         if ($row['messageType'] == 'pm' && !empty($row['userID']) && ($row['userID'] == $userID || $row['userID'] == WCF::getUser()->userID)) {
             if (!empty($userID)) {
                 $tUserID = $userID;
             } else {
                 $tUserID = WCF::getUser()->userID;
             }
             $sql = "SELECT COUNT(*) AS cnt" . "\n  FROM wcf" . WCF_N . "_pm" . "\n WHERE pmID = " . $row['messageID'] . "\n   AND userID = " . $tUserID . "\n   AND saveInOutbox != 0";
             $tmp = WCF::getDB()->getFirstRow($sql);
             if (!empty($tmp['cnt'])) {
                 $ownPM = true;
             }
         }
         if (!empty($row['attachmentSize'])) {
             $row['attachmentSize'] = round($row['attachmentSize'] / 1024, 2) . ' kB';
         }
         // message type urls
         $row['messageTypeUrl'] = $row['messageType'];
         if (self::wbbExists() && preg_match('/^(post|pm)$/', $row['messageType'])) {
             if ($row['messageType'] == 'post') {
                 $row['messageTypeUrl'] = '<a href="' . RELATIVE_WBB_DIR . 'index.php?page=Thread&postID=' . $row['messageID'] . '#post' . $row['messageID'] . '" target="' . ATTACHMENTMANAGER_TARGETWINDOW . '">' . $row['messageType'] . '</a>';
             } else {
                 if ($ownPM) {
                     $row['messageTypeUrl'] = '<a href="' . RELATIVE_WBB_DIR . 'index.php?page=PMView&pmID=' . $row['messageID'] . '#pm' . $row['messageID'] . '" target="' . ATTACHMENTMANAGER_TARGETWINDOW . '">' . $row['messageType'] . '</a>';
                 }
             }
         }
         // thumbnails / files
         $maxLength = 0;
         $shortFileName = $row['attachmentName'];
         if ($isACP && ATTACHMENTMANAGER_MAXLENGTHACP > 0) {
             $maxLength = ATTACHMENTMANAGER_MAXLENGTHACP;
         } else {
             if (ATTACHMENTMANAGER_MAXLENGTHUCP > 0) {
                 $maxLength = ATTACHMENTMANAGER_MAXLENGTHUCP;
             }
         }
         if ($maxLength > 0 && strlen($shortFileName) > $maxLength) {
             preg_match('/^(.*)(\\..*)$/', $shortFileName, $match);
             if (isset($match[2])) {
                 $shortFileName = substr($match[1], 0, $maxLength - (strlen($match[2]) + 2)) . '..' . $match[2];
             } else {
                 $shortFileName = substr($shortFileName, 0, $maxLength);
             }
         }
         $row['attachmentUrl'] = '<span title="' . $row['attachmentName'] . '">' . $shortFileName . '</span>';
         if (self::wbbExists()) {
             if ($row['messageType'] == 'pm' && !$ownPM) {
                 $row['attachmentUrl'] = '<span title="' . $row['attachmentName'] . '">' . $shortFileName . '</span>';
             } else {
                 if (!empty($showThumbnails) && !empty($row['isImage'])) {
                     if (!empty($row['thumbnailSize'])) {
                         $row['attachmentUrl'] = '<a href="' . RELATIVE_WBB_DIR . 'index.php?page=Attachment&attachmentID=' . $row['attachmentID'] . '&h=' . $row['sha1Hash'] . '" target="' . ATTACHMENTMANAGER_TARGETWINDOW . '" style="width:' . ATTACHMENT_THUMBNAIL_WIDTH . 'px; height:' . ATTACHMENT_THUMBNAIL_HEIGHT . 'px;"><img src="' . RELATIVE_WBB_DIR . 'index.php?page=Attachment&attachmentID=' . $row['attachmentID'] . '&h=' . $row['sha1Hash'] . '&thumbnail=1" alt="' . $row['attachmentName'] . '" title="' . $row['attachmentName'] . '" style="max-width:' . ATTACHMENT_THUMBNAIL_WIDTH . 'px; max-height:' . ATTACHMENT_THUMBNAIL_HEIGHT . 'px;" /></a>';
                     } else {
                         $row['attachmentUrl'] = '<a href="' . RELATIVE_WBB_DIR . 'index.php?page=Attachment&attachmentID=' . $row['attachmentID'] . '&h=' . $row['sha1Hash'] . '" target="' . ATTACHMENTMANAGER_TARGETWINDOW . '" style="width:' . ATTACHMENT_THUMBNAIL_WIDTH . 'px; height:' . ATTACHMENT_THUMBNAIL_HEIGHT . 'px;"><img src="' . RELATIVE_WBB_DIR . 'index.php?page=Attachment&attachmentID=' . $row['attachmentID'] . '&h=' . $row['sha1Hash'] . '" alt="' . $row['attachmentName'] . '" title="' . $row['attachmentName'] . '" style="max-width:' . ATTACHMENT_THUMBNAIL_WIDTH . 'px; max-height:' . ATTACHMENT_THUMBNAIL_HEIGHT . 'px;" /></a>';
                     }
                 } else {
                     $row['attachmentUrl'] = '<a href="' . RELATIVE_WBB_DIR . 'index.php?page=Attachment&attachmentID=' . $row['attachmentID'] . '&h=' . $row['sha1Hash'] . '" target="' . ATTACHMENTMANAGER_TARGETWINDOW . '" title="' . $row['attachmentName'] . '">' . $shortFileName . '</a>';
                 }
             }
         }
         $icon = RELATIVE_WCF_DIR . 'icon/fileTypeIconDefaultM.png';
         // get file extension
         $extension = StringUtil::firstCharToUpperCase(StringUtil::toLowerCase(StringUtil::substring($row['attachmentName'], StringUtil::lastIndexOf($row['attachmentName'], '.') + 1)));
         // get file type icon
         if (file_exists(WCF_DIR . 'icon/fileTypeIcon' . $extension . 'M.png')) {
             $icon = RELATIVE_WCF_DIR . 'icon/fileTypeIcon' . $extension . 'M.png';
         } else {
             foreach (self::$fileTypeGroups as $key => $group) {
                 if (in_array($extension, $group)) {
                     $icon = RELATIVE_WCF_DIR . 'icon/fileTypeIcon' . $key . 'M.png';
                     break;
                 }
             }
         }
         $row['mimeIcon'] = '<img src="' . $icon . '"' . ($isACP ? ' height="16" width="16"' : '') . ' alt="' . $row['fileType'] . '" title="' . $row['fileType'] . '" />';
         $ret[$i] = $row;
         $i++;
     }
     return $ret;
 }
 /**
  * Handles a request on the attachment edit form.
  * Deletes old or uploads new attachments.
  */
 public function handleRequest()
 {
     // delete uploaded attachments
     if (isset($_POST['delete']) && is_array($_POST['delete']) && count($_POST['delete'])) {
         // delete selected attachments
         $keys = array_keys($_POST['delete']);
         $this->delete(intval(array_shift($keys)));
     }
     // move uploaded attachments
     if (isset($_POST['attachmentListPositions']) && is_array($_POST['attachmentListPositions'])) {
         $tmpContainerIDArray = count($this->containerIDArray) ? $this->containerIDArray : array(0);
         $positionChanged = false;
         $positions = ArrayUtil::toIntegerArray($_POST['attachmentListPositions']);
         foreach ($positions as $attachmentID => $position) {
             $attachmentID = intval($attachmentID);
             foreach ($tmpContainerIDArray as $containerID) {
                 if (isset($this->attachments[$containerID][$attachmentID]) && $this->attachments[$containerID][$attachmentID]->showOrder != $position) {
                     $this->attachments[$containerID][$attachmentID]->setShowOrder($position);
                     $positionChanged = true;
                 }
             }
         }
         if ($positionChanged) {
             foreach ($tmpContainerIDArray as $containerID) {
                 uasort($this->attachments[$containerID], array('self', 'compareAttachments'));
             }
         }
     }
     // upload new attachments
     $containerID = count($this->containerIDArray) ? reset($this->containerIDArray) : 0;
     if (isset($_FILES) && count($_FILES) && isset($_FILES['upload'])) {
         // upload new attachments
         for ($x = 0, $y = count($_FILES['upload']['name']); $x < $y; $x++) {
             $attachmentData = array();
             $attachmentData['attachmentName'] = $_FILES['upload']['name'][$x];
             if ($attachmentData['attachmentName']) {
                 $tmpFile = $_FILES['upload']['tmp_name'][$x];
                 $attachmentData['attachmentSize'] = $_FILES['upload']['size'][$x];
                 $attachmentData['sha1Hash'] = @sha1_file($tmpFile);
                 $fileExtension = StringUtil::toLowerCase(StringUtil::substring($attachmentData['attachmentName'], StringUtil::lastIndexOf($attachmentData['attachmentName'], '.') + 1));
                 $attachmentData['fileType'] = $_FILES['upload']['type'][$x];
                 $attachmentData['isImage'] = 0;
                 if (strchr($attachmentData['fileType'], 'image')) {
                     // check mime
                     $attachmentData['fileType'] = 'application/octet-stream';
                     if (($imageData = @getImageSize($tmpFile)) !== false) {
                         if (strchr($imageData['mime'], 'image')) {
                             $attachmentData['fileType'] = $imageData['mime'];
                             if ($attachmentData['fileType'] == 'image/bmp') {
                                 $attachmentData['fileType'] = 'image/x-ms-bmp';
                             }
                             $attachmentData['isImage'] = 1;
                         }
                     }
                 }
                 $attachmentData['showOrder'] = (isset($this->attachments[$containerID]) ? count($this->attachments[$containerID]) : 0) + 1;
                 if ($this->checkAttachment($tmpFile, $attachmentData['attachmentName'] . ':' . $attachmentData['sha1Hash'], $attachmentData['attachmentName'], $attachmentData['attachmentSize'], $fileExtension, $attachmentData['isImage'])) {
                     $attachmentData['packageID'] = $this->packageID;
                     $attachmentData['containerID'] = $containerID;
                     $attachmentData['containerType'] = $this->containerType;
                     $attachmentData['idHash'] = $this->idHash;
                     $attachmentData['userID'] = WCF::getUser()->userID;
                     $attachmentData['uploadTime'] = TIME_NOW;
                     $attachmentData['thumbnailType'] = '';
                     $attachmentData['width'] = $attachmentData['height'] = 0;
                     if ($attachmentData['isImage']) {
                         list($width, $height, ) = @getImagesize($tmpFile);
                         $attachmentData['width'] = $width;
                         $attachmentData['height'] = $height;
                     }
                     // save attachment
                     if ($attachment = AttachmentEditor::create($tmpFile, $attachmentData)) {
                         $this->attachmentHashes[count($this->attachmentHashes)] = $attachmentData['attachmentName'] . ':' . $attachmentData['sha1Hash'];
                         $this->attachments[$containerID][$attachment->attachmentID] = $attachment;
                         // save thumbnails
                         if (ATTACHMENT_ENABLE_THUMBNAILS && $attachment->isImage) {
                             $attachment->createThumbnail($this->thumbnailWidth, $this->thumbnailHeight, $this->addSourceInfo, $this->useEmbedded);
                         }
                     }
                 }
             }
         }
     }
     $this->assign();
     if (count($this->errors)) {
         // throw user exception
         throw new UserInputException('attachments', $this->errors);
     }
 }