Esempio n. 1
0
	/**
	 * Returns KunenaAttachment object.
	 *
	 * @param int $identifier	The attachment to load - Can be only an integer.
	 * @param bool $reload
	 *
	 * @return KunenaAttachment
	 */
	static public function get($identifier = null, $reload = false)
	{
		if ($identifier instanceof KunenaAttachment)
		{
			return $identifier;
		}

		$id = (int) $identifier;

		if ($id < 1)
		{
			return new KunenaAttachment;
		}

		if (empty(self::$_instances[$id]))
		{
			$instance = new KunenaAttachment;

			// Only load messages which haven't been preloaded before (including missing ones).
			$instance->load(!array_key_exists($id, self::$_instances) ? $id : null);
			$instance->id = $id;
			self::$_instances[$id] = $instance;
		}
		elseif ($reload)
		{
			self::$_instances[$id]->load();
		}

		return self::$_instances[$id];
	}
Esempio n. 2
0
 /**
  * @param int    $tmpid
  * @param string $postvar
  * @param null   $catid
  *
  * @return bool
  */
 public function uploadAttachment($tmpid, $postvar, $catid = null)
 {
     $attachment = new KunenaAttachment();
     $attachment->userid = $this->userid;
     $success = $attachment->upload($postvar, $catid);
     $this->_attachments_add[$tmpid] = $attachment;
     return $success;
 }
Esempio n. 3
0
 /**
  * Upload files with AJAX.
  *
  * @throws RuntimeException
  */
 public function upload()
 {
     // Only support JSON requests.
     if ($this->input->getWord('format', 'html') != 'json') {
         throw new RuntimeException(JText::_('Bad Request'), 400);
     }
     $upload = KunenaUpload::getInstance();
     // We are converting all exceptions into JSON.
     try {
         if (!JSession::checkToken('request')) {
             throw new RuntimeException(JText::_('Forbidden'), 403);
         }
         $me = KunenaUserHelper::getMyself();
         $catid = $this->input->getInt('catid', 0);
         $mesid = $this->input->getInt('mesid', 0);
         if ($mesid) {
             $message = KunenaForumMessageHelper::get($mesid);
             $message->tryAuthorise('attachment.create');
             $category = $message->getCategory();
         } else {
             $category = KunenaForumCategoryHelper::get($catid);
             // TODO: Some room for improvements in here... (maybe ask user to pick up category first)
             if ($category->id) {
                 if (stripos($this->input->getString('mime'), 'image/') !== false) {
                     $category->tryAuthorise('topic.post.attachment.createimage');
                 } else {
                     $category->tryAuthorise('topic.post.attachment.createfile');
                 }
             }
         }
         $caption = $this->input->getString('caption');
         $options = array('filename' => $this->input->getString('filename'), 'size' => $this->input->getInt('size'), 'mime' => $this->input->getString('mime'), 'hash' => $this->input->getString('hash'), 'chunkStart' => $this->input->getInt('chunkStart', 0), 'chunkEnd' => $this->input->getInt('chunkEnd', 0));
         // Upload!
         $upload->addExtensions(KunenaAttachmentHelper::getExtensions($category->id, $me->userid));
         $response = (object) $upload->ajaxUpload($options);
         if (!empty($response->completed)) {
             // We have it all, lets create the attachment.
             $uploadFile = $upload->getProtectedFile();
             list($basename, $extension) = $upload->splitFilename();
             $attachment = new KunenaAttachment();
             $attachment->bind(array('mesid' => 0, 'userid' => (int) $me->userid, 'protected' => null, 'hash' => $response->hash, 'size' => $response->size, 'folder' => null, 'filetype' => $response->mime, 'filename' => null, 'filename_real' => $response->filename, 'caption' => $caption));
             // Resize image if needed.
             if ($attachment->isImage()) {
                 $imageInfo = KunenaImage::getImageFileProperties($uploadFile);
                 $config = KunenaConfig::getInstance();
                 if ($imageInfo->width > $config->imagewidth || $imageInfo->height > $config->imageheight) {
                     // Calculate quality for both JPG and PNG.
                     $quality = $config->imagequality;
                     if ($quality < 1 || $quality > 100) {
                         $quality = 70;
                     }
                     if ($imageInfo->type == IMAGETYPE_PNG) {
                         $quality = intval(($quality - 1) / 10);
                     }
                     $image = new KunenaImage($uploadFile);
                     $image = $image->resize($config->imagewidth, $config->imageheight, false);
                     $options = array('quality' => $quality);
                     $image->toFile($uploadFile, $imageInfo->type, $options);
                     unset($image);
                     $attachment->hash = md5_file($uploadFile);
                     $attachment->size = filesize($uploadFile);
                 }
             }
             $attachment->saveFile($uploadFile, $basename, $extension, true);
             // Set id and override response variables just in case if attachment was modified.
             $response->id = $attachment->id;
             $response->hash = $attachment->hash;
             $response->size = $attachment->size;
             $response->mime = $attachment->filetype;
             $response->filename = $attachment->filename_real;
         }
     } catch (Exception $response) {
         $upload->cleanup();
         // Use the exception as the response.
     }
     header('Content-type: application/json');
     header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
     header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
     header("Cache-Control: no-store, no-cache, must-revalidate");
     header("Cache-Control: post-check=0, pre-check=0", false);
     header("Pragma: no-cache");
     while (@ob_end_clean()) {
     }
     echo $upload->ajaxResponse($response);
     jexit();
 }