Пример #1
0
 /**
  * Create new re-sized version of the original image.
  *
  * @param  string  $file        Incoming file
  * @param  string  $folder      Folder for the new image.
  * @param  string  $filename    Filename for the new image.
  * @param  int     $maxWidth    Maximum width for the image.
  * @param  int     $maxHeight   Maximum height for the image.
  * @param  int     $quality     Quality for the file (1-100).
  * @param  int     $scale       See available KunenaImage constants.
  * @param  int     $crop        Define if you want crop the image.
  *
  * @return bool    True on success.
  */
 public static function version($file, $folder, $filename, $maxWidth = 800, $maxHeight = 800, $quality = 70, $scale = KunenaImage::SCALE_INSIDE, $crop = 0)
 {
     try {
         // Create target directory if it does not exist.
         if (!KunenaFolder::exists($folder) && !KunenaFolder::create($folder)) {
             return false;
         }
         // Make sure that index.html exists in the folder.
         KunenaFolder::createIndex($folder);
         $info = KunenaImage::getImageFileProperties($file);
         if ($info->width > $maxWidth || $info->height > $maxHeight) {
             // Make sure that quality is in allowed range.
             if ($quality < 1 || $quality > 100) {
                 $quality = 70;
             }
             // Calculate quality for PNG.
             if ($info->type == IMAGETYPE_PNG) {
                 $quality = intval(($quality - 1) / 10);
             }
             $options = array('quality' => $quality);
             // Resize image and copy it to temporary file.
             $image = new KunenaImage($file);
             if ($crop && $info->width > $info->height) {
                 $image = $image->resize($info->width * $maxHeight / $info->height, $maxHeight, false, $scale);
                 $image = $image->crop($maxWidth, $maxHeight);
             } elseif ($crop && $info->width < $info->height) {
                 $image = $image->resize($maxWidth, $info->height * $maxWidth / $info->width, false, $scale);
                 $image = $image->crop($maxWidth, $maxHeight);
             } else {
                 $image = $image->resize($maxWidth, $maxHeight, false, $scale);
             }
             $temp = KunenaPath::tmpdir() . '/kunena_' . md5(rand());
             $image->toFile($temp, $info->type, $options);
             unset($image);
             // Move new file to its proper location.
             if (!KunenaFile::move($temp, "{$folder}/{$filename}")) {
                 unlink($temp);
                 return false;
             }
         } else {
             // Copy original file to the new location.
             if (!KunenaFile::copy($file, "{$folder}/{$filename}")) {
                 return false;
             }
         }
     } catch (Exception $e) {
         return false;
     }
     return true;
 }
Пример #2
0
 /**
  * @param string $key
  * @param null|int   $catid
  *
  * @return bool
  *
  * @since  K4.0
  */
 function upload($key = 'kattachment', $catid = null)
 {
     jimport('joomla.filesystem.folder');
     $config = KunenaFactory::getConfig();
     $input = JFactory::getApplication()->input;
     $fileInput = $input->files->get($key, null, 'raw');
     $upload = KunenaUpload::getInstance(KunenaAttachmentHelper::getExtensions($catid, $this->userid));
     $uploadBasePath = JPATH_ROOT . '/media/kunena/attachments/' . $this->userid . '/';
     if (!JFolder::exists($uploadBasePath)) {
         mkdir(JPATH_ROOT . '/media/kunena/attachments/' . $this->userid . '/');
     }
     $upload->splitFilename($fileInput['name']);
     $fileInput['name'] = preg_replace('/[[:space:]]/', '', $fileInput['name']);
     $fileNameWithoutExt = JFile::stripExt($fileInput['name']);
     $fileExt = JFile::getExt($fileInput['name']);
     $fileNameWithExt = $fileInput['name'];
     if (file_exists($uploadBasePath . $fileInput['name'])) {
         for ($i = 2; file_exists($uploadBasePath . $fileNameWithoutExt . '.' . $fileExt); $i++) {
             $fileNameWithoutExt = $fileNameWithoutExt . "-{$i}";
             $fileNameWithExt = $fileNameWithoutExt . '.' . $fileExt;
         }
     }
     $fileInput['name'] = $fileNameWithExt;
     $file = $upload->upload($fileInput, $uploadBasePath . $fileNameWithoutExt);
     if ($file->success) {
         if (extension_loaded('fileinfo')) {
             $finfo = new finfo(FILEINFO_MIME);
             $type = $finfo->file($uploadBasePath . $fileNameWithExt);
         } else {
             $info = getimagesize($uploadBasePath . $fileNameWithExt);
             $type = $info['mime'];
         }
         if (stripos($type, 'image/') !== false) {
             $imageInfo = KunenaImage::getImageFileProperties($uploadBasePath . $fileNameWithExt);
             if (number_format($file->size / 1024, 2) > $config->imagesize || $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);
                 }
                 $options = array('quality' => $quality);
                 try {
                     $image = new KunenaImage($uploadBasePath . $fileNameWithExt);
                     $image = $image->resize($config->imagewidth, $config->imagewidth, false);
                     $image->toFile($uploadBasePath . $fileNameWithExt, $imageInfo->type, $options);
                     unset($image);
                 } catch (Exception $e) {
                     // TODO: better error message.
                     echo $e->getMessage();
                     return false;
                 }
             }
             $this->filetype = $imageInfo->mime;
         }
         $this->protected = (bool) $config->attachment_protection;
         $this->hash = md5_file($uploadBasePath . $fileNameWithExt);
         $this->size = $file->size;
         $this->folder = 'media/kunena/attachments/' . $this->userid;
         $this->filename = $fileInput['name'];
         $this->filename_real = $uploadBasePath . $fileNameWithExt;
         $this->caption = '';
         return true;
     }
 }
Пример #3
0
 /**
  * Upload and resize if needed the new avatar for user, or set one from the gallery or the default one
  *
  * @return boolean
  */
 protected function saveAvatar()
 {
     $action = JRequest::getString('avatar', 'keep');
     $current_avatar = $this->me->avatar;
     $avatarFile = $this->app->input->files->get('avatarfile');
     if (!empty($avatarFile['tmp_name'])) {
         $this->deleteOldAvatars();
         $upload = KunenaUpload::getInstance(array('gif, jpeg, jpg, png'));
         $uploaded = $upload->upload($avatarFile, KPATH_MEDIA . '/avatars/users/avatar' . $this->me->userid, 'avatar');
         if (!empty($uploaded)) {
             $imageInfo = KunenaImage::getImageFileProperties($uploaded->destination);
             // If image is not inside allowed size limits, resize it
             if ($uploaded->size > intval($this->config->avatarsize) * 1024 || $imageInfo->width > '200' || $imageInfo->height > '200') {
                 if ($this->config->avatarquality < 1 || $this->config->avatarquality > 100) {
                     $quality = 70;
                 } else {
                     $quality = $this->config->avatarquality;
                 }
                 $resized = KunenaImageHelper::version($uploaded->destination, KPATH_MEDIA . '/avatars/users', 'avatar' . $this->me->userid . '.' . $uploaded->ext, 200, 200, $quality, KunenaImage::SCALE_INSIDE, $this->config->avatarcrop);
             }
             $this->app->enqueueMessage(JText::sprintf('COM_KUNENA_PROFILE_AVATAR_UPLOADED'));
             $this->me->avatar = 'users/avatar' . $this->me->userid . '.' . $uploaded->ext;
         } else {
             $this->me->avatar = $current_avatar;
             return false;
         }
     } elseif ($action == 'delete') {
         $this->deleteOldAvatars();
         // Set default avatar
         $this->me->avatar = '';
     } elseif (substr($action, 0, 8) == 'gallery/' && strpos($action, '..') === false) {
         $this->me->avatar = $action;
     }
     return true;
 }
Пример #4
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();
 }