/** * Creates a new UploadHandler object. * * @param array<mixed> $rawFileData */ protected function __construct(array $rawFileData) { if (is_array($rawFileData['name'])) { // multiple uploads for ($i = 0, $l = count($rawFileData['name']); $i < $l; $i++) { $this->files[] = new UploadFile($rawFileData['name'][$i], $rawFileData['tmp_name'][$i], $rawFileData['size'][$i], $rawFileData['error'][$i], FileUtil::getMimeType($rawFileData['tmp_name'][$i]) ?: $rawFileData['type'][$i]); } } else { $this->files[] = new UploadFile($rawFileData['name'], $rawFileData['tmp_name'], $rawFileData['size'], $rawFileData['error'], FileUtil::getMimeType($rawFileData['tmp_name']) ?: $rawFileData['type']); } }
public function exportAttachments($type, $objectType, $offset, $limit) { $sql = "SELECT\t\tattachment.*, data.*\n\t\tFROM\t\txf_attachment attachment\n\t\tLEFT JOIN\txf_attachment_data data\n\t\tON\t\tattachment.data_id = data.data_id\n\t\tWHERE\t\tattachment.content_type = ?\n\t\tORDER BY\tattachment.attachment_id"; $statement = $this->database->prepareStatement($sql, $limit, $offset); $statement->execute(array($type)); while ($row = $statement->fetchArray()) { $config = self::getConfig(); $fileLocation = $this->fileSystemPath . $config['internalDataPath'] . '/attachments/' . floor($row['data_id'] / 1000) . '/' . $row['data_id'] . '-' . $row['file_hash'] . '.data'; if (!file_exists($fileLocation)) { continue; } if ($imageSize = @getimagesize($fileLocation)) { $row['isImage'] = 1; $row['width'] = $imageSize[0]; $row['height'] = $imageSize[1]; } else { $row['isImage'] = $row['width'] = $row['height'] = 0; } ImportHandler::getInstance()->getImporter($objectType)->import($row['attachment_id'], array('objectID' => $row['content_id'], 'userID' => $row['user_id'] ?: null, 'filename' => $row['filename'], 'filesize' => $row['file_size'], 'fileType' => FileUtil::getMimeType($fileLocation) ?: 'application/octet-stream', 'isImage' => $row['isImage'], 'width' => $row['width'], 'height' => $row['height'], 'downloads' => $row['view_count'], 'uploadTime' => $row['upload_date']), array('fileLocation' => $fileLocation)); } }
/** * Constructs a new instance of HTTPRequest. * * @param string $url URL to connect to * @param array<string> $options * @param mixed $postParameters Parameters to send via POST * @param array $files Files to attach to the request */ public function __construct($url, array $options = array(), $postParameters = array(), array $files = array()) { $this->setURL($url); $this->postParameters = $postParameters; $this->files = $files; $this->setOptions($options); // set default headers $this->addHeader('user-agent', "HTTP.PHP (HTTPRequest.class.php; WoltLab Community Framework/" . WCF_VERSION . "; " . WCF::getLanguage()->languageCode . ")"); $this->addHeader('accept', '*/*'); $this->addHeader('accept-language', WCF::getLanguage()->getFixedLanguageCode()); if (isset($this->options['maxLength'])) { $this->addHeader('Range', 'bytes=0-' . ($this->options['maxLength'] - 1)); } if ($this->options['method'] !== 'GET') { if (empty($this->files)) { if (is_array($postParameters)) { $this->body = http_build_query($this->postParameters, '', '&'); } else { if (is_string($postParameters) && !empty($postParameters)) { $this->body = $postParameters; } } $this->addHeader('content-type', 'application/x-www-form-urlencoded'); } else { $boundary = StringUtil::getRandomID(); $this->addHeader('content-type', 'multipart/form-data; boundary=' . $boundary); // source of the iterators: http://stackoverflow.com/a/7623716/782822 if (!empty($this->postParameters)) { $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->postParameters), \RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $k => $v) { if (!$iterator->hasChildren()) { $key = ''; for ($i = 0, $max = $iterator->getDepth(); $i <= $max; $i++) { if ($i === 0) { $key .= $iterator->getSubIterator($i)->key(); } else { $key .= '[' . $iterator->getSubIterator($i)->key() . ']'; } } $this->body .= "--" . $boundary . "\r\n"; $this->body .= 'Content-Disposition: form-data; name="' . $key . '"' . "\r\n\r\n"; $this->body .= $v . "\r\n"; } } } $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->files), \RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $k => $v) { if (!$iterator->hasChildren()) { $key = ''; for ($i = 0, $max = $iterator->getDepth(); $i <= $max; $i++) { if ($i === 0) { $key .= $iterator->getSubIterator($i)->key(); } else { $key .= '[' . $iterator->getSubIterator($i)->key() . ']'; } } $this->body .= "--" . $boundary . "\r\n"; $this->body .= 'Content-Disposition: form-data; name="' . $k . '"; filename="' . basename($v) . '"' . "\r\n"; $this->body .= 'Content-Type: ' . (FileUtil::getMimeType($v) ?: 'application/octet-stream.') . "\r\n\r\n"; $this->body .= file_get_contents($v) . "\r\n"; } } $this->body .= "--" . $boundary . "--"; } $this->addHeader('content-length', strlen($this->body)); } if (isset($this->options['auth'])) { $this->addHeader('authorization', "Basic " . base64_encode($options['auth']['username'] . ":" . $options['auth']['password'])); } $this->addHeader('connection', 'Close'); }
/** * Returns the mime type of a file. * * @param string $file * @param string $mimeType mime type transferred by client * @return string */ protected static function getMimeType($file, $mimeType) { if ($mimeType == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' || $mimeType == 'application/vnd.openxmlformats-officedocument.presentationml.presentation' || $mimeType == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') { // libmagic can not detect mime type of docx, xlsx and pttx files return $mimeType; } $finfoMimeType = FileUtil::getMimeType($file); if ($finfoMimeType) { return $finfoMimeType; } return $mimeType; }
/** * Exports post attachments. */ public function exportPostAttachments($offset, $limit) { $sql = "SELECT\t\tchild.*, attach.*, filedata.*\n\t\t\tFROM\t\t" . $this->databasePrefix . "node child\n\t\t\tINNER JOIN\t" . $this->databasePrefix . "node parent\n\t\t\tON\t\tchild.parentid = parent.nodeid\n\t\t\tINNER JOIN\t" . $this->databasePrefix . "node grandparent\n\t\t\tON\t\tparent.parentid = grandparent.nodeid\n\t\t\tINNER JOIN\t" . $this->databasePrefix . "attach attach\n\t\t\tON\t\tchild.nodeid = attach.nodeid\n\t\t\tINNER JOIN\t" . $this->databasePrefix . "filedata filedata\n\t\t\tON\t\tattach.filedataid = filedata.filedataid\n\t\t\t\n\t\t\tINNER JOIN\t(SELECT contenttypeid FROM " . $this->databasePrefix . "contenttype WHERE class IN(?, ?, ?)) x\n\t\t\tON\t\tx.contenttypeid = grandparent.contenttypeid\n\t\t\tINNER JOIN\t(SELECT contenttypeid FROM " . $this->databasePrefix . "contenttype WHERE class = ?) y\n\t\t\tON\t\ty.contenttypeid = parent.contenttypeid\n\t\t\tINNER JOIN\t(SELECT contenttypeid FROM " . $this->databasePrefix . "contenttype WHERE class = ?) z\n\t\t\tON\t\tz.contenttypeid = child.contenttypeid\n\t\t\t\n\t\t\tWHERE\t\tchild.nodeid BETWEEN ? AND ?\n\t\t\tORDER BY\tchild.nodeid ASC"; $statement = $this->database->prepareStatement($sql); // Text in a Text or Poll should be a post // Text in a Channel should be a thread $statement->execute(array('Text', 'Poll', 'Channel', 'Text', 'Attach', $offset + 1, $offset + $limit)); while ($row = $statement->fetchArray()) { $file = null; try { switch ($this->readOption('attachfile')) { case self::ATTACHFILE_DATABASE: $file = FileUtil::getTemporaryFilename('attachment_'); file_put_contents($file, $row['filedata']); break; } // unable to read file -> abort if (!is_file($file) || !is_readable($file)) { continue; } if ($imageSize = @getimagesize($file)) { $row['isImage'] = 1; $row['width'] = $imageSize[0]; $row['height'] = $imageSize[1]; } else { $row['isImage'] = $row['width'] = $row['height'] = 0; } ImportHandler::getInstance()->getImporter('com.woltlab.wbb.attachment')->import($row['nodeid'], array('objectID' => $row['parentid'], 'userID' => $row['userid'] ?: null, 'filename' => $row['filename'], 'filesize' => isset($row['filesize']) ? $row['filesize'] : filesize($file), 'fileType' => FileUtil::getMimeType($file), 'isImage' => $row['isImage'], 'width' => $row['width'], 'height' => $row['height'], 'downloads' => $row['counter'], 'uploadTime' => $row['dateline'], 'showOrder' => isset($row['displayOrder']) ? $row['displayOrder'] : 0), array('fileLocation' => $file)); if ($this->readOption('attachfile') == self::ATTACHFILE_DATABASE) { unlink($file); } } catch (\Exception $e) { if ($this->readOption('attachfile') == self::ATTACHFILE_DATABASE && $file) { @unlink($file); } throw $e; } } }
$objectAction = new CategoryAction(array(), 'create', array('data' => array('description' => '', 'isDisabled' => 0, 'objectTypeID' => $objectType->objectTypeID, 'parentCategoryID' => null, 'showOrder' => null, 'title' => 'news images'))); $objectAction->executeAction(); $returnValues = $objectAction->getReturnValues(); $categoryID = $returnValues['returnValues']->categoryID; //get old news images $list = new NewsImageList(); $list->readObjects(); $oldIDs = array(); foreach ($list->getObjects() as $image) { //get file hash $fileHash = sha1_file(CMS_DIR . 'images/news/' . $image->filename); $folder = substr($fileHash, 0, 2); //get size $size = filesize(CMS_DIR . 'images/news/' . $image->filename); //mime type $mime = FileUtil::getMimeType(CMS_DIR . 'images/news/' . $image->filename); //create db entry $action = new FileAction(array(), 'create', array('data' => array('title' => $image->getTitle(), 'filesize' => $size, 'fileType' => $mime, 'fileHash' => $fileHash, 'uploadTime' => TIME_NOW))); $action->executeAction(); $returnValues = $action->getReturnValues(); //set old IDs $oldIDs[$image->imageID] = $returnValues['returnValues']->fileID; if (!is_dir(CMS_DIR . 'files/' . $folder)) { FileUtil::makePath(CMS_DIR . 'files/' . $folder); } copy(CMS_DIR . 'images/news/' . $image->filename, CMS_DIR . 'files/' . $folder . '/' . $returnValues['returnValues']->fileID . '-' . $fileHash); @unlink(CMS_DIR . 'images/news/' . $image->filename); //insert into news image category $sql = "INSERT INTO cms" . WCF_N . "_file_to_category VALUES (?, ?)"; $statement = WCF::getDB()->prepareStatement($sql); $statement->execute(array($returnValues['returnValues']->fileID, $categoryID));
/** * Exports post attachments. */ public function exportPostAttachments($offset, $limit) { try { // vb 4 $sql = "SELECT\t\tattachment.*, attachment.contentid AS postid, filedata.filedata\n\t\t\t\tFROM\t\t" . $this->databasePrefix . "attachment attachment\n\t\t\t\tLEFT JOIN\t" . $this->databasePrefix . "filedata filedata\n\t\t\t\tON\t\tattachment.filedataid = filedata.filedataid\n\t\t\t\tWHERE\t\tattachment.contenttypeid = (SELECT contenttypeid FROM " . $this->databasePrefix . "contenttype contenttype WHERE contenttype.class = 'Post')\n\t\t\t\tORDER BY\tattachment.attachmentid"; $statement = $this->database->prepareStatement($sql, $limit, $offset); $statement->execute(); } catch (DatabaseException $e) { // vb 3 $sql = "SELECT\t\t*\n\t\t\t\tFROM\t\t" . $this->databasePrefix . "attachment\n\t\t\t\tORDER BY\tattachmentid"; $statement = $this->database->prepareStatement($sql, $limit, $offset); $statement->execute(); } while ($row = $statement->fetchArray()) { $file = null; try { switch ($this->readOption('attachfile')) { case self::ATTACHFILE_DATABASE: $file = FileUtil::getTemporaryFilename('attachment_'); file_put_contents($file, $row['filedata']); break; case self::ATTACHFILE_FILESYSTEM: $file = $this->readOption('attachpath'); if (!StringUtil::startsWith($file, '/')) { $file = realpath($this->fileSystemPath . $file); } $file = FileUtil::addTrailingSlash($file); $file .= $row['userid'] . '/' . (isset($row['filedataid']) ? $row['filedataid'] : $row['attachmentid']) . '.attach'; break; case self::ATTACHFILE_FILESYSTEM_SUBFOLDER: $file = $this->readOption('attachpath'); if (!StringUtil::startsWith($file, '/')) { $file = realpath($this->fileSystemPath . $file); } $file = FileUtil::addTrailingSlash($file); $file .= implode('/', str_split($row['userid'])) . '/' . (isset($row['filedataid']) ? $row['filedataid'] : $row['attachmentid']) . '.attach'; break; } // unable to read file -> abort if (!is_file($file) || !is_readable($file)) { continue; } if ($imageSize = @getimagesize($file)) { $row['isImage'] = 1; $row['width'] = $imageSize[0]; $row['height'] = $imageSize[1]; } else { $row['isImage'] = $row['width'] = $row['height'] = 0; } ImportHandler::getInstance()->getImporter('com.woltlab.wbb.attachment')->import($row['attachmentid'], array('objectID' => $row['postid'], 'userID' => $row['userid'] ?: null, 'filename' => $row['filename'], 'filesize' => isset($row['filesize']) ? $row['filesize'] : filesize($file), 'fileType' => FileUtil::getMimeType($file), 'isImage' => $row['isImage'], 'width' => $row['width'], 'height' => $row['height'], 'downloads' => $row['counter'], 'uploadTime' => $row['dateline'], 'showOrder' => isset($row['displayOrder']) ? $row['displayOrder'] : 0), array('fileLocation' => $file)); if ($this->readOption('attachfile') == self::ATTACHFILE_DATABASE) { unlink($file); } } catch (\Exception $e) { if ($this->readOption('attachfile') == self::ATTACHFILE_DATABASE && $file) { @unlink($file); } throw $e; } } }
private function exportAttachments($indexName, $objectType, $offset, $limit) { $sql = "SELECT\t\t*\n\t\t\tFROM\t\t" . $this->databasePrefix . "attachments\n\t\t\tWHERE\t\t" . $indexName . " > ?\n\t\t\tORDER BY\tattachmentid DESC"; $statement = $this->database->prepareStatement($sql, $limit, $offset); $statement->execute(array(0)); while ($row = $statement->fetchArray()) { $fileLocation = $this->fileSystemPath . 'attachments/attachment-' . $row['attachmentid'] . '.' . $row['attachmentextension']; if (!@file_exists($fileLocation)) { continue; } $fileType = FileUtil::getMimeType($fileLocation); $isImage = 0; if ($fileType == 'image/jpeg' || $fileType == 'image/png' || $fileType == 'image/gif') { $isImage = 1; } ImportHandler::getInstance()->getImporter($objectType)->import($row['attachmentid'], array('objectID' => $row[$indexName], 'userID' => $row['userid'] ?: null, 'filename' => $row['attachmentname'] . '.' . $row['attachmentextension'], 'filesize' => $row['attachmentsize'], 'fileType' => $fileType, 'isImage' => $isImage, 'downloads' => $row['counter'], 'uploadTime' => $row['uploadtime'], 'showOrder' => 0), array('fileLocation' => $fileLocation)); } }