コード例 #1
0
ファイル: helper.php プロジェクト: giabmf11/Kunena-Forum
 /**
  * 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
ファイル: attachment.php プロジェクト: densem-2013/exikom
 /**
  * Set attachment file.
  *
  * Copies the attachment into proper location and makes sure that all the unset fields get properly assigned.
  *
  * @param  string  $source     Absolute path to the upcoming attachment.
  * @param  string  $basename   Filename without extension.
  * @param  string  $extension  File extension.
  * @param  bool    $unlink     Whether to delete the original file or not.
  * @param  bool    $overwrite  If not allowed, throw exception if the file exists.
  *
  * @return bool
  * @throws InvalidArgumentException
  * @throws RuntimeException
  *
  * @since  K4.0
  */
 public function saveFile($source, $basename = null, $extension = null, $unlink = false, $overwrite = false)
 {
     if (!is_file($source)) {
         throw new InvalidArgumentException(__CLASS__ . '::' . __METHOD__ . '(): Attachment file not found.');
     }
     // Hash, size and MIME are set during saving, so let's deal with all other variables.
     $this->userid = is_null($this->userid) ? KunenaUserHelper::getMyself() : $this->userid;
     $this->folder = is_null($this->folder) ? "media/kunena/attachments/{$this->userid}" : $this->folder;
     $this->protected = is_null($this->protected) ? (bool) KunenaConfig::getInstance()->attachment_protection : $this->protected;
     if (!$this->filename_real) {
         $this->filename_real = $this->filename;
     }
     if (!$this->filename || $this->filename == $this->filename_real) {
         if (!$basename || !$extension) {
             throw new InvalidArgumentException(__CLASS__ . '::' . __METHOD__ . '(): Parameters $basename or $extension not provided.');
         }
         // Find available filename.
         $this->filename = KunenaAttachmentHelper::getAvailableFilename($this->folder, $basename, $extension, $this->protected);
     }
     // Create target directory if it does not exist.
     if (!KunenaFolder::exists(JPATH_ROOT . "/{$this->folder}") && !KunenaFolder::create(JPATH_ROOT . "/{$this->folder}")) {
         throw new RuntimeException(JText::_('Failed to create attachment directory.'));
     }
     $destination = JPATH_ROOT . "/{$this->folder}/{$this->filename}";
     // Move the file into the final location (if not already in there).
     if ($source != $destination) {
         // Create target directory if it does not exist.
         if (!$overwrite && is_file($destination)) {
             throw new RuntimeException(JText::sprintf('Attachment %s already exists.'), $this->filename_real);
         }
         if ($unlink) {
             @chmod($source, 0644);
         }
         $success = KunenaFile::copy($source, $destination);
         if (!$success) {
             throw new RuntimeException(JText::sprintf('COM_KUNENA_UPLOAD_ERROR_NOT_MOVED', $destination));
         }
         KunenaPath::setPermissions($destination);
         if ($unlink) {
             unlink($source);
         }
     }
     return $this->save();
 }
コード例 #3
0
ファイル: upload.php プロジェクト: anawu2006/PeerLearning
 /**
  * Upload file by passing it by HTML input
  *
  * @param   array   $fileInput    The file object returned by JInput
  * @param   string  $destination  The path of destination of file uploaded
  * @param   string  $type         The type of file uploaded: attachment or avatar
  *
  * @return object
  */
 public function upload($fileInput, $destination, $type = 'attachment')
 {
     $file = new stdClass();
     $file->ext = JFile::getExt($fileInput['name']);
     $file->size = $fileInput['size'];
     $file->tmp_name = $fileInput['tmp_name'];
     $file->error = $fileInput['error'];
     $file->destination = $destination . '.' . $file->ext;
     $file->success = false;
     $file->isAvatar = false;
     if ($type != 'attachment') {
         $file->isAvatar = true;
     }
     if (!is_uploaded_file($file->tmp_name)) {
         $exception = $this->checkUpload($fileInput);
         if ($exception) {
             throw $exception;
         }
     } elseif ($file->error != 0) {
         throw new RuntimeException(JText::_('COM_KUNENA_UPLOAD_ERROR_NOT_UPLOADED'), 500);
     }
     // Check if file extension matches any allowed extensions (case insensitive)
     foreach ($this->validExtensions as $ext) {
         $extension = JString::substr($file->tmp_name, -JString::strlen($ext));
         if (JString::strtolower($extension) == JString::strtolower($ext)) {
             // File must contain one letter before extension
             $name = JString::substr($file->tmp_name, 0, -JString::strlen($ext));
             $extension = JString::substr($extension, 1);
             if (!$name) {
                 throw new RuntimeException(JText::sprintf('COM_KUNENA_UPLOAD_ERROR_EXTENSION_FILE', implode(', ', $this->validExtensions)), 400);
             }
         }
     }
     if (!$this->checkFileSize($file->size, true)) {
         if ($file->isAvatar) {
             throw new RuntimeException(JText::_('COM_KUNENA_UPLOAD_ERROR_AVATAR_EXCEED_LIMIT_IN_CONFIGURATION'), 500);
         } else {
             throw new RuntimeException(JText::_('COM_KUNENA_UPLOAD_ERROR_FILE_EXCEED_LIMIT_IN_CONFIGURATION'), 500);
         }
     }
     if (!KunenaFile::copy($file->tmp_name, $file->destination)) {
         throw new RuntimeException(JText::_('COM_KUNENA_UPLOAD_ERROR_FILE_RIGHT_MEDIA_DIR'), 500);
     }
     unlink($file->tmp_name);
     KunenaPath::setPermissions($file->destination);
     $file->success = true;
     return $file;
 }
コード例 #4
0
 public static function version($file, $newpath, $newfile, $maxwidth = 800, $maxheight = 800, $quality = 70, $scale = CKunenaImage::SCALE_INSIDE)
 {
     require_once KPATH_SITE . '/lib/kunena.file.class.php';
     // create upload directory if it does not exist
     $imageinfo = self::getProperties($file);
     if (!$imageinfo) {
         return false;
     }
     if (!JFolder::exists($newpath)) {
         if (!JFolder::create($newpath)) {
             return false;
         }
     }
     KunenaFolder::createIndex($newpath);
     if ($imageinfo->width > $maxwidth || $imageinfo->height > $maxheight) {
         $image = new CKunenaImage($file);
         if ($image->getError()) {
             return false;
         }
         if ($quality < 1 || $quality > 100) {
             $quality = 70;
         }
         $options = array('quality' => $quality);
         $image = $image->resize($maxwidth, $maxheight, true, $scale);
         $type = $image->getType();
         $temp = KunenaPath::tmpdir() . '/kunena_' . md5(rand());
         $image->toFile($temp, $type, $options);
         unset($image);
         if (!KunenaFile::move($temp, $newpath . '/' . $newfile)) {
             unlink($temp);
             return false;
         }
     } else {
         if (!KunenaFile::copy($file, $newpath . '/' . $newfile)) {
             return false;
         }
     }
     return true;
 }