Пример #1
0
 /**
  * Create a thumbnail image
  *
  * @copyright
  * @author 		RolandD
  * @todo
  * @see
  * @access 		public
  * @param		string 	$original		the full path and name of the large image
  * @param		string	$output_path	the path to store the thumbnail
  * @param		string	$output_name	the name of the thumbnail
  * @return
  * @since 		4.0
  */
 public function createThumbnail($original, $output_path, $output_name)
 {
     $jinput = JFactory::getApplication()->input;
     $template = $jinput->get('template', null, null);
     $csvilog = $jinput->get('csvilog', null, null);
     $base = JPath::clean(JPATH_SITE, '/');
     // Make sure the thumbnail is the same file type as the full image
     if ($template->get('thumb_check_filetype', 'image') && JFile::getExt($original) != JFile::getExt($output_name)) {
         $output_name = JFile::stripExt($output_name) . '.' . JFile::getExt($original);
     }
     $output_name = $this->_setCase($output_name);
     // Check if the original is an external image
     if (!$this->isRemote($original)) {
         $original = $base . '/' . $original;
         $file_exists = JFile::exists($original);
         $remote = false;
     } else {
         $file_exists = CsviHelper::fileExistsRemote($original);
         $remote = true;
     }
     // Check if thumbsize is greater than 0
     if ($template->get('thumb_width', 'image') >= 1 && $template->get('thumb_height', 'image') >= 1) {
         // Check if the image folders exists
         $thumb_folder = JPATH_SITE . '/' . $output_path . dirname($output_name);
         if (!JFolder::exists($thumb_folder)) {
             $csvilog->addDebug(JText::sprintf('COM_CSVI_CREATE_THUMB_FOLDER', $thumb_folder));
             JFolder::create($thumb_folder);
         }
         // Check if the target thumb exists, if yes delete it
         if (JFile::exists($base . '/' . $output_path . $output_name)) {
             JFile::delete($base . '/' . $output_path . $output_name);
         }
         // Check if the original file exists
         $csvilog->addDebug(JText::sprintf('COM_CSVI_CHECK_ORIGINAL', $original));
         if ($file_exists) {
             // Collect all thumbnail details
             $thumb_file_details = array();
             $thumb_file_details['file'] = $original;
             $thumb_file_details['file_extension'] = JFile::getExt($original);
             $thumb_file_details['file_out'] = $base . '/' . $output_path . $output_name;
             $thumb_file_details['maxsize'] = 0;
             $thumb_file_details['bgred'] = 255;
             $thumb_file_details['bggreen'] = 255;
             $thumb_file_details['bgblue'] = 255;
             $thumb_file_details['file_out_width'] = $template->get('thumb_width', 'image');
             $thumb_file_details['file_out_height'] = $template->get('thumb_height', 'image');
             $thumb_file_details['file_out_extension'] = JFile::getExt($output_name);
             $thumb_file_details['mime_type'] = $this->findMimeType($original, $remote);
             // We need to resize the image and Save the new one only if it is in a different location
             $csvilog->addDebug(JText::sprintf('COM_CSVI_DEBUG_CREATING_A_THUMBNAIL', $original, $thumb_file_details['file_out']));
             if ($original != $thumb_file_details['file_out']) {
                 $new_img = $this->convertImage($thumb_file_details);
                 // Check if an image was created
                 if ($new_img) {
                     // Get the details of the thumb image
                     if (JFile::exists($new_img)) {
                         $csvilog->addDebug(JText::_('COM_CSVI_DEBUG_THUMB_CREATED'));
                         return $output_path . $output_name;
                     } else {
                         $csvilog->addDebug(JText::_('COM_CSVI_THUMBNAIL_NOT_CREATED'));
                         return false;
                     }
                 } else {
                     $csvilog->addDebug(JText::_('COM_CSVI_THUMBNAIL_NOT_CREATED'));
                     return false;
                 }
             } else {
                 $csvilog->addDebug('COM_CSVI_THUMB_SAME_AS_FULL');
                 $csvilog->AddStats('incorrect', 'COM_CSVI_THUMB_SAME_AS_FULL');
                 return false;
             }
         } else {
             $csvilog->addDebug(JText::sprintf('COM_CSVI_FILE_DOES_NOT_EXIST_NOTHING_TO_DO', $original));
             $csvilog->AddStats('nofiles', JText::sprintf('COM_CSVI_FILE_DOES_NOT_EXIST_NOTHING_TO_DO', $original));
             return false;
         }
     } else {
         $csvilog->addDebug('COM_CSVI_THUMBNAIL_SIZE_TOO_SMALL');
         $csvilog->AddStats('incorrect', 'COM_CSVI_THUMBNAIL_SIZE_TOO_SMALL');
         return false;
     }
 }
Пример #2
0
 /**
  * Validate the file
  *
  * Validate the file is of the supported type
  * Types supported are csv, txt, xls, ods, xml
  *
  * @copyright
  * @author 		RolandD
  * @todo 		See if this code can be optimized
  * @see
  * @access 		public
  * @param
  * @return 		bool true if all OK | false if not OK
  * @since 		3.0
  */
 public function validateFile()
 {
     $jinput = JFactory::getApplication()->input;
     $csvilog = $jinput->get('csvilog', null, null);
     $template = $jinput->get('template', null, null);
     // Workaround as it is always true
     //if ($jinput->get('filepos', 0, 'int') >= 0) {
     //	$csv_file = $template->get('local_csv_file', 'general', false);
     //	if (!$csv_file) {
     //		$csv_file = urldecode($jinput->get('csv_file'));
     //		$jinput->set('local_csv_file', $csv_file);
     //	}
     //	$this->folder = dirname($csv_file);
     //	$jinput->set('csv_file', $csv_file);
     //}
     $loadfrom = $template->get('source', 'general');
     switch (strtolower($loadfrom)) {
         // Uploaded file
         case 'fromupload':
             $upload['name'] = $_FILES['jform']['name']['general']['import_file'];
             $upload['type'] = $_FILES['jform']['type']['general']['import_file'];
             $upload['tmp_name'] = $_FILES['jform']['tmp_name']['general']['import_file'];
             $upload['error'] = $_FILES['jform']['error']['general']['import_file'];
             // Check if the file upload has an error
             if (empty($upload)) {
                 $csvilog->AddStats('incorrect', JText::_('COM_CSVI_NO_UPLOADED_FILE_PROVIDED'));
                 return false;
             } else {
                 if ($upload['error'] == 0) {
                     if (is_uploaded_file($upload['tmp_name'])) {
                         // Get some basic info
                         $folder = $this->_unpackpath . '/' . time();
                         $upload_parts = pathinfo($upload['name']);
                         // Create the temp folder
                         if (JFolder::create($folder)) {
                             $this->folder = $folder;
                             // Move the uploaded file to its temp location
                             if (JFile::upload($upload['tmp_name'], $folder . '/' . $upload['name'])) {
                                 $this->_uploaded = true;
                                 // Let's see if the uploaded file is an archive
                                 if (in_array($upload_parts['extension'], $this->archives)) {
                                     // It is an archive, unpack first
                                     if (JArchive::extract($folder . '/' . $upload['name'], $folder)) {
                                         // File is unpacked, let's get the filename
                                         $foundfiles = scandir($folder);
                                         foreach ($foundfiles as $ffkey => $filename) {
                                             $ff_parts = pathinfo($filename);
                                             if (in_array(strtolower($ff_parts['extension']), $this->suffixes)) {
                                                 $jinput->set('csv_file', $folder . '/' . $filename);
                                                 $jinput->set('upload_file_error', false);
                                                 $this->extension = strtolower($ff_parts["extension"]);
                                                 end($foundfiles);
                                             } else {
                                                 $found = false;
                                             }
                                         }
                                         if (!$found) {
                                             $jinput->set('upload_file_error', true);
                                         }
                                     } else {
                                         $csvilog->AddStats('incorrect', JText::_('COM_CSVI_CANNOT_UNPACK_UPLOADED_FILE'));
                                         return false;
                                     }
                                 } else {
                                     $jinput->set('csv_file', $folder . '/' . $upload['name']);
                                     $this->extension = strtolower($upload_parts['extension']);
                                 }
                             }
                         } else {
                             $csvilog->AddStats('incorrect', JText::sprintf('COM_CSVI_CANNOT_CREATE_UNPACK_FOLDER', $folder));
                             return false;
                         }
                     } else {
                         $csvilog->AddStats('incorrect', JText::sprintf('COM_CSVI_NO_UPLOADED_FILE_PROVIDED', $upload['tmp_name']));
                         return false;
                     }
                 } else {
                     // There was a problem uploading the file
                     switch ($upload['error']) {
                         case '1':
                             $csvilog->AddStats('incorrect', JText::_('COM_CSVI_THE_UPLOADED_FILE_EXCEEDS_THE_MAXIMUM_UPLOADED_FILE_SIZE'));
                             break;
                         case '2':
                             $csvilog->AddStats('incorrect', JText::_('COM_CSVI_THE_UPLOADED_FILE_EXCEEDS_THE_MAXIMUM_UPLOADED_FILE_SIZE'));
                             break;
                         case '3':
                             $csvilog->AddStats('incorrect', JText::_('COM_CSVI_THE_UPLOADED_FILE_WAS_ONLY_PARTIALLY_UPLOADED'));
                             break;
                         case '4':
                             $csvilog->AddStats('incorrect', JText::_('COM_CSVI_NO_FILE_WAS_UPLOADED'));
                             break;
                         case '6':
                             $csvilog->AddStats('incorrect', JText::_('COM_CSVI_MISSING_A_TEMPORARY_FOLDER'));
                             break;
                         case '7':
                             $csvilog->AddStats('incorrect', JText::_('COM_CSVI_FAILED_TO_WRITE_FILE_TO_DISK'));
                             break;
                         case '8':
                             $csvilog->AddStats('incorrect', JText::_('COM_CSVI_FILE_UPLOAD_STOPPED_BY_EXTENSION'));
                             break;
                         default:
                             $csvilog->AddStats('incorrect', JText::_('COM_CSVI_THERE_WAS_A_PROBLEM_UPLOADING_THE_FILE'));
                             break;
                     }
                     return false;
                 }
             }
             break;
             // Local file
         // Local file
         case 'fromserver':
             $csv_file = JPath::clean($template->get('local_csv_file', 'general'), '/');
             // Set the file name to use
             $jinput->set('csv_file', $csv_file);
             if (!JFile::exists($csv_file)) {
                 $csvilog->addDebug('[VALIDATEFILE] ' . JText::sprintf('COM_CSVI_LOCAL_FILE_DOESNT_EXIST', $csv_file));
                 $csvilog->AddStats('incorrect', JText::sprintf('COM_CSVI_LOCAL_FILE_DOESNT_EXIST', $csv_file));
                 return false;
             } else {
                 $jinput->set('upload_file_error', false);
             }
             $fileinfo = pathinfo($csv_file);
             if (isset($fileinfo["extension"])) {
                 $this->extension = strtolower($fileinfo["extension"]);
                 if ($this->extension == 'txt') {
                     $this->extension = 'csv';
                 }
             }
             break;
         case 'fromurl':
             // The temporary folder
             $folder = $this->_unpackpath . '/' . time();
             $urlfile = $template->get('urlfile', 'general', false);
             $tempfile = basename($urlfile);
             // Check if the remote file exists
             if ($urlfile) {
                 if (CsviHelper::fileExistsRemote($urlfile)) {
                     // Copy the remote file to a local location
                     if (JFolder::create($folder)) {
                         if (touch($folder . '/' . $tempfile)) {
                             if (JFile::write($folder . '/' . $tempfile, JFile::read($urlfile))) {
                                 $csvilog->addDebug(JText::sprintf('COM_CSVI_RETRIEVE_FROM_URL', $urlfile));
                                 $jinput->set('csv_file', $folder . '/' . $tempfile);
                                 $jinput->set('upload_file_error', false);
                                 $this->extension = JFile::getExt($tempfile);
                             } else {
                                 $csvilog->AddStats('incorrect', JText::_('COM_CSVI_CANNOT_READ_FROM_URL'));
                                 return false;
                             }
                         } else {
                             $csvilog->AddStats('incorrect', JText::sprintf('COM_CSVI_CANNOT_CREATE_TEMP_FILE', $folder . '/' . $tempfile));
                             return false;
                         }
                     } else {
                         $csvilog->AddStats('incorrect', JText::sprintf('COM_CSVI_CANNOT_CREATE_TEMP_FOLDER', $folder));
                         return false;
                     }
                 } else {
                     $csvilog->AddStats('incorrect', JText::_('COM_CSVI_CANNOT_READ_FROM_URL'));
                     return false;
                 }
             } else {
                 $csvilog->AddStats('incorrect', JText::_('COM_CSVI_NO_FILENAME_GIVEN'));
                 return false;
             }
             break;
         case 'fromftp':
             // The temporary folder
             $folder = $this->_unpackpath . '/' . time();
             $ftpfile = $template->get('ftpfile', 'general', false);
             if ($ftpfile) {
                 // Create the output file
                 if (JFolder::create($folder)) {
                     if (touch($folder . '/' . $ftpfile)) {
                         // Start the FTP
                         jimport('joomla.client.ftp');
                         $ftp = JFTP::getInstance($template->get('ftphost', 'general'), $template->get('ftpport', 'general'), null, $template->get('ftpusername', 'general'), $template->get('ftppass', 'general'));
                         if ($ftp->get($folder . '/' . $ftpfile, $template->get('ftproot', 'general', '/') . $ftpfile)) {
                             $csvilog->addDebug(JText::sprintf('COM_CSVI_RETRIEVE_FROM_FTP', $template->get('ftproot', 'general', '/') . $ftpfile));
                             $jinput->set('csv_file', $folder . '/' . $ftpfile);
                             $jinput->set('upload_file_error', false);
                             $this->extension = JFile::getExt($ftpfile);
                         } else {
                             $csvilog->AddStats('incorrect', JText::_('COM_CSVI_CANNOT_READ_FROM_FTP'));
                             return false;
                         }
                         $ftp->quit();
                     } else {
                         $csvilog->AddStats('incorrect', JText::sprintf('COM_CSVI_CANNOT_CREATE_TEMP_FILE', $folder . '/' . $ftpfile));
                         return false;
                     }
                 } else {
                     $csvilog->AddStats('incorrect', JText::sprintf('COM_CSVI_CANNOT_CREATE_TEMP_FOLDER', $folder));
                     return false;
                 }
             } else {
                 $csvilog->AddStats('incorrect', JText::_('COM_CSVI_NO_FILENAME_GIVEN'));
                 return false;
             }
             break;
             // No file given
         // No file given
         default:
             $csvilog->AddStats('incorrect', JText::_('COM_CSVI_NO_FILE_PROVIDED'));
             return false;
             break;
     }
     // Make sure txt files are not ignored
     if ($this->extension == 'txt') {
         $this->extension = 'csv';
     }
     // Set the filename
     $csv_file = $jinput->get('csv_file', '', 'string');
     if (JFile::exists($csv_file)) {
         $this->filename = JPath::clean($csv_file, '/');
         // Store the users filename for display purposes
         $csvilog->setFilename(basename($this->filename));
     } else {
         $csvilog->addDebug(JText::sprintf('COM_CSVI_LOCAL_FILE_DOESNT_EXIST', $jinput->get('csv_file')));
         return false;
     }
     if (in_array($this->extension, $this->suffixes)) {
         $this->valid_extension = true;
     } else {
         // Test the mime type
         if (!in_array($this->extension, $this->mimetypes)) {
             $csvilog->AddStats('information', JText::sprintf('COM_CSVI_EXTENSION_NOT_ACCEPTED', $this->extension));
             return false;
         }
     }
     // Debug message to know what filetype the user is uploading
     $csvilog->addDebug(JText::sprintf('COM_CSVI_IMPORT_FILETYPE', $this->extension));
     // All is fine
     return true;
 }