/**
  * @see	\wcf\data\IEditableObject::deleteAll()
  */
 public static function deleteAll(array $objectIDs = array())
 {
     // delete files first
     $conditionBuilder = new PreparedStatementConditionBuilder();
     $conditionBuilder->add("attachmentID IN (?)", array($objectIDs));
     $sql = "SELECT\t*\n\t\t\tFROM\twcf" . WCF_N . "_attachment\n\t\t\t" . $conditionBuilder;
     $statement = WCF::getDB()->prepareStatement($sql);
     $statement->execute($conditionBuilder->getParameters());
     while ($attachment = $statement->fetchObject(static::$baseClass)) {
         $editor = new AttachmentEditor($attachment);
         $editor->deleteFiles();
     }
     return parent::deleteAll($objectIDs);
 }
 /**
  * @see	\wcf\system\importer\IImporter::import()
  */
 public function import($oldID, array $data, array $additionalData = array())
 {
     // check file location
     if (!@file_exists($additionalData['fileLocation'])) {
         return 0;
     }
     // get file hash
     if (empty($data['fileHash'])) {
         $data['fileHash'] = sha1_file($additionalData['fileLocation']);
     }
     // get image size
     if (!empty($data['isImage'])) {
         $imageData = @getimagesize($additionalData['fileLocation']);
         if ($imageData !== false) {
             $data['width'] = $imageData[0];
             $data['height'] = $imageData[1];
         }
     }
     // get user id
     $data['userID'] = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.user', $data['userID']);
     // check existing attachment id
     if (is_numeric($oldID)) {
         $attachment = new Attachment($oldID);
         if (!$attachment->attachmentID) {
             $data['attachmentID'] = $oldID;
         }
     }
     // save attachment
     $attachment = AttachmentEditor::create(array_merge($data, array('objectTypeID' => $this->objectTypeID)));
     // check attachment directory
     // and create subdirectory if necessary
     $dir = dirname($attachment->getLocation());
     if (!@file_exists($dir)) {
         @mkdir($dir, 0777);
     }
     // copy file
     try {
         if (!copy($additionalData['fileLocation'], $attachment->getLocation())) {
             throw new SystemException();
         }
         return $attachment->attachmentID;
     } catch (SystemException $e) {
         // copy failed; delete attachment
         $editor = new AttachmentEditor($attachment);
         $editor->delete();
     }
     return 0;
 }
 /**
  * @see	\wcf\system\cronjob\ICronjob::execute()
  */
 public function execute(Cronjob $cronjob)
 {
     parent::execute($cronjob);
     // delete orphaned attachments
     $attachmentIDs = array();
     $sql = "SELECT\tattachmentID\n\t\t\tFROM\twcf" . WCF_N . "_attachment\n\t\t\tWHERE\tobjectID = ?\n\t\t\t\tAND uploadTime < ?";
     $statement = WCF::getDB()->prepareStatement($sql);
     $statement->execute(array(0, TIME_NOW - 86400));
     while ($row = $statement->fetchArray()) {
         $attachmentIDs[] = $row['attachmentID'];
     }
     if (!empty($attachmentIDs)) {
         AttachmentEditor::deleteAll($attachmentIDs);
     }
 }
Example #4
0
 /**
  * @see	\wcf\page\IPage::show()
  */
 public function show()
 {
     parent::show();
     if (!$this->tiny && !$this->thumbnail) {
         // update download count
         $editor = new AttachmentEditor($this->attachment);
         $editor->update(array('downloads' => $this->attachment->downloads + 1, 'lastDownloadTime' => TIME_NOW));
     }
     // send file to client
     $this->fileReader->send();
     exit;
 }
 /**
  * Copies attachments from one object id to another.
  */
 public function copy()
 {
     $sourceObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.attachment.objectType', $this->parameters['sourceObjectType']);
     $targetObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.attachment.objectType', $this->parameters['targetObjectType']);
     $attachmentList = new AttachmentList();
     $attachmentList->getConditionBuilder()->add("attachment.objectTypeID = ?", array($sourceObjectType->objectTypeID));
     $attachmentList->getConditionBuilder()->add("attachment.objectID = ?", array($this->parameters['sourceObjectID']));
     $attachmentList->readObjects();
     $newAttachmentIDs = array();
     foreach ($attachmentList as $attachment) {
         $newAttachment = AttachmentEditor::create(array('objectTypeID' => $targetObjectType->objectTypeID, 'objectID' => $this->parameters['targetObjectID'], 'userID' => $attachment->userID, 'filename' => $attachment->filename, 'filesize' => $attachment->filesize, 'fileType' => $attachment->fileType, 'fileHash' => $attachment->fileHash, 'isImage' => $attachment->isImage, 'width' => $attachment->width, 'height' => $attachment->height, 'tinyThumbnailType' => $attachment->tinyThumbnailType, 'tinyThumbnailSize' => $attachment->tinyThumbnailSize, 'tinyThumbnailWidth' => $attachment->tinyThumbnailWidth, 'tinyThumbnailHeight' => $attachment->tinyThumbnailHeight, 'thumbnailType' => $attachment->thumbnailType, 'thumbnailSize' => $attachment->thumbnailSize, 'thumbnailWidth' => $attachment->thumbnailWidth, 'thumbnailHeight' => $attachment->thumbnailHeight, 'downloads' => $attachment->downloads, 'lastDownloadTime' => $attachment->lastDownloadTime, 'uploadTime' => $attachment->uploadTime, 'showOrder' => $attachment->showOrder));
         // copy attachment
         @copy($attachment->getLocation(), $newAttachment->getLocation());
         if ($attachment->tinyThumbnailSize) {
             @copy($attachment->getTinyThumbnailLocation(), $newAttachment->getTinyThumbnailLocation());
         }
         if ($attachment->thumbnailSize) {
             @copy($attachment->getThumbnailLocation(), $newAttachment->getThumbnailLocation());
         }
         $newAttachmentIDs[$attachment->attachmentID] = $newAttachment->attachmentID;
     }
     return array('attachmentIDs' => $newAttachmentIDs);
 }